From 66cf74f4cb7121c560055c7dbfeb7fa1acdc0e24 Mon Sep 17 00:00:00 2001 From: IvanHunters Date: Tue, 17 Nov 2026 16:12:38 +0300 Subject: [PATCH] fix(credentials): reject reserved keys in secretLabels An ApplicationDefinition's credentials.secretLabels is copied over the account marker and the application identity labels, which store.List, store.Prune and the Credential view select the account Secrets by, so a block setting one of those keys would silently make its Secrets unreachable. Validate now refuses such a block. Assisted-by: LLM Signed-off-by: IvanHunters --- pkg/credentials/account/account.go | 13 +++++++++++++ pkg/credentials/account/account_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/pkg/credentials/account/account.go b/pkg/credentials/account/account.go index cefbf2b59..9857cafb3 100644 --- a/pkg/credentials/account/account.go +++ b/pkg/credentials/account/account.go @@ -112,6 +112,19 @@ func Validate(c *cozyv1alpha1.ApplicationDefinitionCredentials) error { if c.ValuesPath != "" && !strings.HasPrefix(c.ValuesPath, "_") { return fmt.Errorf("valuesPath %q must start with \"_\"", c.ValuesPath) } + // Labels copies secretLabels over the account marker and the application + // identity labels, which store.List, store.Prune and the Credential view + // select the account Secrets by. A block that sets one of them would make + // those Secrets unreachable, so refuse it rather than lose the selector. + for k := range c.SecretLabels { + switch k { + case AccountLabel, + appsv1alpha1.ApplicationGroupLabel, + appsv1alpha1.ApplicationKindLabel, + appsv1alpha1.ApplicationNameLabel: + return fmt.Errorf("secretLabels key %q is reserved: it is the account marker or an application identity label the account Secrets are selected by", k) + } + } if len(c.Accounts) == 0 { return errors.New("accounts is empty") } diff --git a/pkg/credentials/account/account_test.go b/pkg/credentials/account/account_test.go index 1ea9cc35c..9bd310924 100644 --- a/pkg/credentials/account/account_test.go +++ b/pkg/credentials/account/account_test.go @@ -12,6 +12,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" + appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" ) @@ -78,6 +79,12 @@ func static(name, when string, owner cozyv1alpha1.CredentialAccountOwner) cozyv1 return cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsStatic, Name: name, When: when, Owner: owner} } +func withSecretLabel(key string) *cozyv1alpha1.ApplicationDefinitionCredentials { + b := block(valuesMap("users")) + b.SecretLabels = map[string]string{key: "x"} + return b +} + func TestValidate(t *testing.T) { if err := Validate(nil); err != nil { t.Errorf("nil block: %v", err) @@ -85,6 +92,11 @@ func TestValidate(t *testing.T) { if err := Validate(block(valuesMap("users"))); err != nil { t.Errorf("valid block: %v", err) } + withLabels := block(valuesMap("users")) + withLabels.SecretLabels = map[string]string{"cnpg.io/reload": "true"} + if err := Validate(withLabels); err != nil { + t.Errorf("block with a non-reserved secretLabel: %v", err) + } notBuilt := []*cozyv1alpha1.ApplicationDefinitionCredentials{ {Format: cozyv1alpha1.CredentialFormat{Name: "apr1", Kind: cozyv1alpha1.CredentialFormatWebhook}, Accounts: []cozyv1alpha1.CredentialAccountSource{valuesMap("users")}}, @@ -106,6 +118,8 @@ func TestValidate(t *testing.T) { block(cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsValuesMap}), block(cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsStatic}), block(static("x", "", "nobody")), + withSecretLabel(AccountLabel), + withSecretLabel(appsv1alpha1.ApplicationNameLabel), } for i, b := range invalid { if err := Validate(b); err == nil || errors.Is(err, ErrNotBuilt) {