Skip to content

Add --secret-namespaces to restrict Secret reads on the service cluster - #222

Open
ifdotpy wants to merge 10 commits into
kcp-dev:mainfrom
ifdotpy:fix/195-secret-namespace-cache
Open

ifdotpy wants to merge 10 commits into
kcp-dev:mainfrom
ifdotpy:fix/195-secret-namespace-cache

Conversation

@ifdotpy

@ifdotpy ifdotpy commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Summary

Today the Agent caches Secrets on the service cluster in all namespaces, so it needs cluster-wide read access to Secrets. #195 asks to limit this.

This PR adds --secret-namespaces. When set:

  • The Secret informers run only in the given namespaces and the Agent's own --namespace (the Agent stores object state there).
  • The manager client reads Secrets (typed and unstructured) from this cache. The live API reader never sends a cluster-wide Secret request: a List without a namespace becomes one namespaced request per allowed namespace (the merged list has no continue token or resourceVersion), and a paginated List without a namespace is rejected. List and delete options are evaluated once, so the checked namespace is the one used.
  • A Secret read outside the set returns an error, and a direct lookup of such a Secret fails the reconciliation.
  • Secret writes outside the set (create, update, patch, apply, delete, delete-all-of, and the status and subresource clients) are refused. The subresource secrets (the Secret collection of a Namespace parent) and apply configurations whose kind cannot be resolved are refused.
  • Secrets are recognized through the client's RESTMapper. Only the kind Secret (and SecretList for a List call) is accepted. A request that would reach Secrets with another kind (for example the lowercase secret that discovery also maps, or SecretList for a single object) is refused, because the cache is keyed by the canonical kind.
  • An explicitly empty flag is rejected.

The guarantee covers the client and the API reader of the local manager (GetClient, GetAPIReader) and the namespaces of the Secret informer. Direct use of the manager's cache, field indexer, REST config or HTTP client is outside of it; the Agent uses the REST config only for discovery and CRDs.

Without the flag the Secret cache and reads keep their current scope.

One change applies with and without the flag: when the syncer looks up a related object on the destination side, it now treats only NotFound as "absent". Before, any read error counted as absent, so the object was written again or its cleanup was skipped.

Assumption and limits: the list is static. If target namespaces are created per object (we do this), the flag alone does not remove the cluster-wide read. Cleanup that enumerates Secrets cannot see copies in a namespace that was removed from the set; the getting-started guide describes this. A dynamic namespace set and informers that follow permission changes, as proposed in #195, need a larger design. This PR is a first step.

Tests: per-informer tests check the list and watch paths; scope tests run against a server that forbids cluster-wide Secret requests (client, live reader, excluded namespace, status client, kind aliases); a real flag parser test. make lint and make test pass. e2e not run (needs a kcp setup).

What Type of PR Is This?

/kind feature

Related Issue(s)

Partially addresses #195

Release Notes

New flag `--secret-namespaces` limits the Agent's Secret reads and writes on the service cluster (cache and live reads) to the given namespaces and the Agent's own namespace.

@kcp-ci-bot kcp-ci-bot added kind/feature Categorizes issue or PR as related to a new feature. release-note Denotes a PR that will be considered when it comes time to generate release notes. dco-signoff: yes Indicates the PR's author has signed the DCO. labels Dec 2, 2026
@kcp-ci-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign xrstf for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kcp-ci-bot kcp-ci-bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Dec 2, 2026
@kcp-ci-bot

Copy link
Copy Markdown
Contributor

Hi @ifdotpy. Thanks for your PR.

I'm waiting for a kcp-dev member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kcp-ci-bot kcp-ci-bot added size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Dec 2, 2026
…luster

The local manager cached every type cluster-wide. A Secret informer
therefore needed list and watch on Secrets in all namespaces, and
namespace RoleBindings could not authorize it.

With --secret-namespaces, the local cache lists and watches Secrets
only in the given namespaces and in the agent namespace, where the
object state is stored. This covers the typed state store and the
unstructured related-resource watches. Other types keep the
cluster-wide cache. Without the flag nothing changes.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The cache was scoped, but the manager client read unstructured Secrets
(primary objects and related resources) live and cluster-wide, and the
pre-delete confirmation used the uncached API reader. With namespace-scoped
RBAC these reads failed, and with broader RBAC they reached Secrets outside
the configured namespaces.

With --secret-namespaces, the local manager now hands out a client that
serves Secret reads from the scoped cache and an API reader that sends one
namespaced request per allowed namespace. A Secret read in any other
namespace returns an error, which also stops a prune.

An explicitly empty --secret-namespaces is rejected instead of caching
Secrets cluster-wide.

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
A related destination lookup ignored every Get error, so an out-of-scope or
forbidden Secret counted as missing: the Agent could write it again or skip
its cleanup and release the finalizer. Only NotFound now means absent.

With --secret-namespaces, the local client also refuses to create, update,
patch or delete a Secret outside the configured namespaces, because the
Agent could not read it back to adopt or clean it up. The live reader
rejects a paginated List of Secrets without a namespace.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The syncer applies destination objects with client.Apply when server-side
apply is enabled. The scoped client did not override Apply, so an
out-of-scope Secret could be created that the Agent then could not find or
clean up. Apply now gets the same namespace check as the other writes.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
Status() and SubResource() of the scoped client now check the parent
object like the other writes. The apply check parses the API version, so a
noncanonical spelling of core v1 is recognized as a Secret.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The scoped Status() client also exposed a Get method with no reader behind
it. It now returns a wrapper with the write methods only.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The scope recognized Secrets only by the kind Secret. The discovery
RESTMapper also maps the lowercase kind "secret" to the Secrets
resource, so an unstructured object with that kind passed the scope as
a non-Secret and was read cluster-wide.

The scope now resolves other core kinds with the client's RESTMapper.
A kind other than Secret that maps to Secrets is refused, because the
cache and its namespace restriction are keyed by the canonical kind.

The live reader also builds a new option slice for each namespace, so
it no longer writes into the caller's slice.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The client removes the List suffix of a kind when the object has list
shape, and caches the resolved resource by kind. So a single object
with the kind SecretList and an items field reached the Secrets
resource, while the scope resolved SecretList to another resource.

The scope now accepts only Secret for one object and SecretList for a
List call. For every other kind it resolves the kind and the kind
without the List suffix, and refuses the request when one of them maps
to Secrets.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
The subresource "secrets" of a Namespace parent is the Secret
collection of that namespace, and the subresource clients only checked
the parent object. The scoped subresource clients now refuse this
subresource.

Apply let a configuration pass when it had no apiVersion and kind
getters. It now resolves a runtime.Object through the scheme, like the
client does, and refuses a configuration that it cannot resolve.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
List and DeleteAllOf applied the caller's options to check the
namespace and then passed the original options on, which applied them
again. An option that returns a different namespace on the second
evaluation could so reach a namespace outside --secret-namespaces. The
wrappers now resolve the options once and pass the resolved options.

The live reader also clears continue, remainingItemCount and
resourceVersion of an aggregate list: the result spans several
requests, so the metadata of an earlier request does not apply.

Refs kcp-dev#195

Signed-off-by: Igor Fominykh <ifdotpy@gmail.com>
@ifdotpy
ifdotpy force-pushed the fix/195-secret-namespace-cache branch from 1a735d5 to ca6a721 Compare December 2, 2026 18:21
@kcp-ci-bot kcp-ci-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Dec 2, 2026

// secretNamespacesSet records that --secret-namespaces was given, so that an empty
// value is rejected instead of silently caching Secrets cluster-wide.
secretNamespacesSet bool

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.

I think pflag already tells these cases apart. With the nil default, an omitted flag leaves SecretNamespaces nil, and --secret-namespaces= sets it to []string{} (readAsCSV("") returns an empty slice). So we could drop this field and RecordParsedFlags, and check o.SecretNamespaces != nil && len(o.SecretNamespaces) == 0 in Validate(). That also removes the call in main.go, which if forgotten turns the empty check off without any error. TestSecretNamespacesFlag already covers it.

// SecretNamespaces restricts the local cache for Secrets to these namespaces. The
// agent's own Namespace is always included because the object state is stored there.
// When empty, Secrets are cached in all namespaces.
SecretNamespaces []string

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.

If we switch to the nil check, can you add a note here: nil means the flag was not given, an empty slice means it was given empty.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dco-signoff: yes Indicates the PR's author has signed the DCO. kind/feature Categorizes issue or PR as related to a new feature. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants