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 <xorokhotnikov@gmail.com>
This commit is contained in:
IvanHunters committed 2026-11-17 20:27:41 +03:00
1 parent c834848043
commit 66cf74f4cb
2 files changed
+27

No files matched your search

+13
View File
@@ -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")
}
+14
View File
@@ -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) {