mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-06 07:03:45 +09:00
feat(lineage-webhook): tell whether a definition shows a Secret
The webhook decides a Secret's tenant visibility from the owning ApplicationDefinition on every write that drops the lineage labels, as an operator's rewrite does. A conversion that hides an operator Secret has to know whether the definition in the API already excludes it, because a label it writes earlier is undone by the next rewrite. SecretTenantVisible evaluates a definition exactly as the webhook does. The tests also load the converted RabbitMQ, MongoDB, MariaDB and ClickHouse definitions and pin that each one hides the platform Secrets its conversion keeps, as an operator rewrite would present them. Assisted-by: LLM Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
2 files changed
+81
No files matched your search
@@ -3,6 +3,7 @@ package lineagecontrollerwebhook
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
@@ -71,3 +72,21 @@ func matchResourceToExcludeInclude(ctx context.Context, name string, templateCon
|
||||
}
|
||||
return matchResourceToSelectorArray(ctx, name, templateContext, l, resources.Include)
|
||||
}
|
||||
|
||||
// SecretTenantVisible reports whether the webhook stamps a Secret of the
|
||||
// named application tenant-visible under the definition's selectors, the way
|
||||
// computeLabels evaluates them on admission. Callers outside the webhook use
|
||||
// it to tell whether a definition already in the API hides a Secret before
|
||||
// they label it, since the webhook re-evaluates on every write that drops the
|
||||
// lineage labels.
|
||||
func SecretTenantVisible(ctx context.Context, appDef *cozyv1alpha1.ApplicationDefinition, appName, namespace, secretName string, secretLabels map[string]string) bool {
|
||||
if appDef == nil {
|
||||
return false
|
||||
}
|
||||
templateContext := map[string]string{
|
||||
"kind": strings.ToLower(appDef.Spec.Application.Kind),
|
||||
"name": appName,
|
||||
"namespace": namespace,
|
||||
}
|
||||
return matchResourceToExcludeInclude(ctx, secretName, templateContext, secretLabels, &appDef.Spec.Secrets)
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package lineagecontrollerwebhook
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/yaml"
|
||||
|
||||
cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1"
|
||||
)
|
||||
|
||||
func loadDefinition(t *testing.T, path string) *cozyv1alpha1.ApplicationDefinition {
|
||||
t.Helper()
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
def := &cozyv1alpha1.ApplicationDefinition{}
|
||||
if err := yaml.Unmarshal(raw, def); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// The platform Secrets a converted engine keeps must stay hidden on every
|
||||
// write that drops their lineage labels, not only until an operator rewrites
|
||||
// them: the webhook stamps them from these definitions each time.
|
||||
func TestConvertedDefinitionsHidePlatformSecrets(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for _, c := range []struct{ rd, secret string }{
|
||||
{"rabbitmq", "rabbitmq-rmq-default-user"},
|
||||
{"mongodb", "mongodb-rmq-credentials"},
|
||||
{"mongodb", "mongodb-rmq-percona-server-mongodb-users"},
|
||||
{"mariadb", "mariadb-rmq-credentials"},
|
||||
{"clickhouse", "clickhouse-rmq-credentials"},
|
||||
} {
|
||||
def := loadDefinition(t, "../../packages/system/"+c.rd+"-rd/cozyrds/"+c.rd+".yaml")
|
||||
// An operator's rewrite carries its own labels, none of the lineage ones.
|
||||
if SecretTenantVisible(ctx, def, "rmq", "tenant-foo", c.secret, map[string]string{"app.kubernetes.io/name": "rmq"}) {
|
||||
t.Errorf("%s: the definition stamps %s tenant-visible", c.rd, c.secret)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretTenantVisibleFollowsIncludeAndExclude(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
sel := []*cozyv1alpha1.ApplicationDefinitionResourceSelector{{ResourceNames: []string{"{{ .kind }}-{{ .name }}-default-user"}}}
|
||||
def := &cozyv1alpha1.ApplicationDefinition{}
|
||||
def.Spec.Application.Kind = "RabbitMQ"
|
||||
def.Spec.Secrets.Include = sel
|
||||
if !SecretTenantVisible(ctx, def, "rmq", "tenant-foo", "rabbitmq-rmq-default-user", nil) {
|
||||
t.Fatal("an included Secret must be visible")
|
||||
}
|
||||
def.Spec.Secrets.Exclude = sel
|
||||
if SecretTenantVisible(ctx, def, "rmq", "tenant-foo", "rabbitmq-rmq-default-user", nil) {
|
||||
t.Fatal("exclude must win over include")
|
||||
}
|
||||
if SecretTenantVisible(ctx, nil, "rmq", "tenant-foo", "rabbitmq-rmq-default-user", nil) {
|
||||
t.Fatal("a kind with no definition stamps nothing visible")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user