Skip to content

make the ReferenceGrant source identity configurable and stop leaking selector matches - #14643

Open
puertomontt wants to merge 6 commits into
kgateway-dev:mainfrom
puertomontt:omar/refgrant-source-gk
Open

puertomontt wants to merge 6 commits into
kgateway-dev:mainfrom
puertomontt:omar/refgrant-source-gk

Conversation

@puertomontt

@puertomontt puertomontt commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Description

  • WithSourceGroupKind(gk) sets the identity ReferenceGrants are evaluated against for the
    references TrafficPolicySpec owns: basicAuth.secretRef, apiKeyAuth.secretRef,
    apiKeyAuth.secretSelector, secret-backed headerModifiers values, and extensionRef in
    Strict mode. It defaults to TrafficPolicy and is exposed through pkg/plugins/gwextbase.
    The identity replaces the default rather than being ORed with it: from.kind scopes a
    grant to one referencing kind.
  • Denials now return MissingReferenceGrantError, naming the namespace the grant belongs in and
    the identity it has to allow, so the grant to write is readable off the policy status.
  • A denied label selector names neither the matched Secret nor its namespace.
  • Reference-grant behavior itself is unchanged for any existing config: with no option set the
    source identity is still TrafficPolicy, and the same references are permitted and denied as
    before. Only the message text changes.

Change Type

/kind fix

Changelog

NONE

…aking selector matches

Every cross-namespace reference in TrafficPolicySpec is evaluated against
gateway.kgateway.dev/TrafficPolicy, whichever kind actually carries the spec
being translated. When that is a different kind, the grant a user writes names
the kind they created, the API server accepts it, and it never matches: the
reference stays denied and nothing about the config changes.

Add WithSourceGroupKind so the kind that holds the reference is the identity
grants are evaluated against. It replaces the default rather than being ORed
with it: from.kind scopes a grant to one referencing kind, and the kinds
involved are typically creatable by different sets of users, so accepting
either would hand out access the grant's author did not intend. It covers the
references TrafficPolicySpec owns - basicAuth and apiKeyAuth secrets,
secret-backed headerModifiers values, and extensionRef in Strict mode.

Denials now report MissingReferenceGrantError, which names the namespace the
grant belongs in and the identity it has to allow, instead of a bare "missing
reference grant" that leaves the user guessing which kind to name. A denied
label selector names neither the matched Secret nor its namespace: which
secrets carry a label is not observable without a grant, so reporting one in
policy status would let a referrer probe labels to learn that a Secret exists
in a namespace that never granted it access.

Signed-off-by: omar <omar.hammami@solo.io>
@gateway-bot gateway-bot added kind/fix Categorizes issue or PR as related to a bug. release-note labels Aug 31, 2026
… on backend refs

GetSecretsBySelector matched labels across every namespace and then filtered by
grant, so "missing reference grant" versus "no secrets found" still told a
referrer whether a matching Secret existed in a namespace it had no access to.
It now looks up the namespaces whose ReferenceGrants permit the referrer and
searches only those plus its own, and returns a single SelectorNoMatchError
naming the selector and the grant identity for every no-match outcome.

BackendIndex.GetBackendFromRef returns MissingReferenceGrantError, so a denied
GatewayExtension backend ref names the grant to create.

Signed-off-by: omar <omar.hammami@solo.io>
Signed-off-by: omar <omar.hammami@solo.io>
@puertomontt
puertomontt marked this pull request as ready for review September 23, 2026 16:56
Copilot AI lite review requested due to automatic review settings September 23, 2026 16:56
@puertomontt puertomontt changed the title fix: make the ReferenceGrant source identity configurable and stop leaking selector matches make the ReferenceGrant source identity configurable and stop leaking selector matches Sep 23, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🔵 Needs a closer look

Preserve denied-reference classification and add constructor-level coverage for custom source identity wiring.

Review effort: Lite
Findings: None

What changed in this PR

Configures ReferenceGrant source identities for TrafficPolicy references and prevents selector errors from exposing inaccessible Secrets.

Changes:

  • Adds WithSourceGroupKind and propagates configurable identities.
  • Improves grant diagnostics and selector privacy.
  • Updates tests, documentation, and expected outputs.
File Summary
pkg/​plugins/​gwextbase/​base.go Exposes constructor options.
pkg/​krtcollections/​secrets.go Implements grant-scoped selector lookup and safe errors.
pkg/​krtcollections/​refgrant_source_test.go Tests source identity and selector privacy.
pkg/​krtcollections/​policy.go Adds descriptive grant errors and namespace indexing.
pkg/​krtcollections/​configmaps.go Uses descriptive grant errors.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​traffic-policy/​ratelimit-cross-namespace.yaml Updates expected output.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​traffic-policy/​header-modifiers-from-secret-cross-namespace-no-refgrant.yaml Updates expected output.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​traffic-policy/​extproc-cross-namespace.yaml Updates expected output.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​traffic-policy/​extauth-cross-namespace.yaml Updates expected output.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​traffic-policy/​api-key-auth-selector-no-matching-secret.yaml Updates selector error output.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​reference-grant-mode/​strict-extensionref-no-grant.yaml Updates expected grant error.
pkg/​kgateway/​translator/​gateway/​testutils/​outputs/​jwt/​cross-namespace.yaml Updates expected output.
pkg/​kgateway/​extensions2/​plugins/​trafficpolicy/​header_modifiers.go Propagates source identity.
pkg/​kgateway/​extensions2/​plugins/​trafficpolicy/​constructor.go Adds configurable source identity support.
pkg/​kgateway/​extensions2/​plugins/​trafficpolicy/​basic_auth_policy.go Applies configurable grant identity.
pkg/​kgateway/​extensions2/​plugins/​trafficpolicy/​api_key_auth.go Applies configurable grant identity.
devel/​reference_grant/​reference-grant-mode.md Documents the new behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

puertomontt and others added 3 commits September 23, 2026 18:22
The uniform SelectorNoMatchError is what stops a selector revealing matches in
ungranted namespaces; searching only granting namespaces was an optimization
on top. Match across all secrets and filter by grant as before, and drop the
source-keyed grant index and per-namespace secret index it needed.

Signed-off-by: omar <omar.hammami@solo.io>
Naming the grant identity in ReferenceGrant errors is left for a follow-up, so
drop MissingReferenceGrantError and SelectorNoMatchError and restore the
original messages. The selector keeps the fix for the leak: a match skipped for
a missing grant no longer returns ErrMissingReferenceGrant, so it reads the
same as no match at all.

Signed-off-by: omar <omar.hammami@solo.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/fix Categorizes issue or PR as related to a bug. release-note-none

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants