diff --git a/pkg/credentials/account/account.go b/pkg/credentials/account/account.go new file mode 100644 index 000000000..88012e5d9 --- /dev/null +++ b/pkg/credentials/account/account.go @@ -0,0 +1,208 @@ +// SPDX-License-Identifier: Apache-2.0 + +// Package account is what the Cozystack API, the CredentialTemplate controller +// and the migration tooling share about account Secrets: their names, the +// labels and annotations that carry the record, and how the stored value +// reaches a chart through valuesFrom. It writes nothing itself. +package account + +import ( + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "regexp" + "strings" + + helmv2 "github.com/fluxcd/helm-controller/api/v2" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/util/validation" + + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" +) + +const ( + // AccountLabel marks the account Secret of a CredentialTemplate, which + // cozystack-controller seeds and keeps, so the controller can watch these + // Secrets by label without matching any other Secret of the application. + AccountLabel = "credentials.cozystack.io/account" + // OriginAnnotation holds the CredentialOrigin of the live password. + OriginAnnotation = "credentials.cozystack.io/origin" + // AtAnnotation holds when the live password was set, RFC 3339. + AtAnnotation = "credentials.cozystack.io/at" + // ByAnnotation holds who set the live password. + ByAnnotation = "credentials.cozystack.io/by" + // HistoryAnnotation holds the last HistoryLimit operations as a JSON list, + // newest first. + HistoryAnnotation = "credentials.cozystack.io/history" + // HistoryLimit bounds the history kept on the Secret; the audit log keeps + // every operation. + HistoryLimit = 10 + // FormatAnnotation names the format the Secret's password is stored in, so + // the controller sees when a template's format changed. + FormatAnnotation = "credentials.cozystack.io/format" + // SeedAnnotation on a HelmRelease lets the controller seed the account + // Secrets of its templates. The API sets it on every release it creates, + // the conversion on a release whose Secrets it wrote; a release without it + // keeps passwords the platform never saw, so its accounts are left for a + // mint. + SeedAnnotation = "credentials.cozystack.io/seed" + // ReleaseNameAnnotation is what Helm puts on every object it renders, + // naming the release; it ties a template to its HelmRelease. + ReleaseNameAnnotation = "meta.helm.sh/release-name" + // SecretSuffix follows the template name in the account Secret's name. + SecretSuffix = ".account" + // UsernameKey and PasswordKey are the two data keys of every account + // Secret, whatever the format. + UsernameKey = "username" + PasswordKey = "password" +) + +var nonNameChars = regexp.MustCompile(`[^a-z0-9-]`) + +// Account is one tenant account of an application. +type Account struct { + // Username as the application declares it and the engine knows it. + Username string + // Mapped is Username lowercased with every character outside [a-z0-9-] + // replaced by "-"; it names the Secret and the Credential. + Mapped string +} + +// MapName returns the object-name form of a user name, or an error when the +// form is not a valid DNS label, which no Secret or Credential name can carry. +func MapName(username string) (string, error) { + mapped := nonNameChars.ReplaceAllString(strings.ToLower(username), "-") + if errs := validation.IsDNS1123Label(mapped); len(errs) > 0 { + return "", fmt.Errorf("user name %q maps to %q, which is not a valid name: %s", username, mapped, strings.Join(errs, "; ")) + } + return mapped, nil +} + +// SecretName is the account Secret of a user of a release. +func SecretName(release, mapped string) string { + return release + "." + mapped + SecretSuffix +} + +// Record is the per-account record an account Secret carries in its +// annotations and the Credential view shows. +type Record struct { + Origin corev1alpha1.CredentialOrigin + At metav1.Time + By string + History []corev1alpha1.CredentialHistoryEntry +} + +// ReadRecord decodes the record of an account Secret. A Secret with no +// annotations yields an empty record rather than an error, so a Secret +// written by hand still lists. +func ReadRecord(sec *corev1.Secret) Record { + var r Record + ann := sec.GetAnnotations() + r.Origin = corev1alpha1.CredentialOrigin(ann[OriginAnnotation]) + r.By = ann[ByAnnotation] + if at := ann[AtAnnotation]; at != "" { + if t, err := parseTime(at); err == nil { + r.At = t + } + } + if h := ann[HistoryAnnotation]; h != "" { + _ = json.Unmarshal([]byte(h), &r.History) + } + return r +} + +// WriteRecord encodes the record into the Secret's annotations. +func WriteRecord(sec *corev1.Secret, r Record) { + if sec.Annotations == nil { + sec.Annotations = map[string]string{} + } + sec.Annotations[OriginAnnotation] = string(r.Origin) + sec.Annotations[ByAnnotation] = r.By + sec.Annotations[AtAnnotation] = r.At.UTC().Format(timeLayout) + history, _ := json.Marshal(r.History) + sec.Annotations[HistoryAnnotation] = string(history) +} + +// Apply records an operation: the origin becomes the operation's, At and By +// are the operation's, and the entry goes to the front of the history, which +// keeps HistoryLimit entries. +func (r Record) Apply(op corev1alpha1.CredentialOperation, at metav1.Time, by string) Record { + r.Origin = originOf(op) + r.At = at + r.By = by + entry := corev1alpha1.CredentialHistoryEntry{Operation: op, At: at, By: by} + history := make([]corev1alpha1.CredentialHistoryEntry, 0, len(r.History)+1) + history = append(history, entry) + history = append(history, r.History...) + if len(history) > HistoryLimit { + history = history[:HistoryLimit] + } + r.History = history + return r +} + +func originOf(op corev1alpha1.CredentialOperation) corev1alpha1.CredentialOrigin { + switch op { + case corev1alpha1.CredentialOperationMigrate: + return corev1alpha1.CredentialOriginMigrated + case corev1alpha1.CredentialOperationMint: + return corev1alpha1.CredentialOriginMinted + case corev1alpha1.CredentialOperationRevoke: + return corev1alpha1.CredentialOriginRevoked + } + return corev1alpha1.CredentialOriginNotIssued +} + +const timeLayout = "2006-01-02T15:04:05Z" + +func parseTime(s string) (metav1.Time, error) { + var t metav1.Time + err := t.UnmarshalQueryParameter(s) + return t, err +} + +// Labels are the labels an account Secret carries: those its template names, +// and the account marker, which a template cannot override. +func Labels(templateLabels map[string]string) map[string]string { + labels := make(map[string]string, len(templateLabels)+1) + for k, v := range templateLabels { + labels[k] = v + } + labels[AccountLabel] = "true" + return labels +} + +// DigestKey is the values key one account's stored value lands under: the +// SHA-256 of the user name, which fits a valuesFrom targetPath whatever the +// name holds. +func DigestKey(username string) string { + sum := sha256.Sum256([]byte(username)) + return "u" + hex.EncodeToString(sum[:]) +} + +// ValuesFrom is the HelmRelease valuesFrom entry that carries an account's +// stored value into .. It is optional, so the release +// renders while the account has no Secret yet. +func ValuesFrom(secretName, valuesPath, username string) helmv2.ValuesReference { + return helmv2.ValuesReference{ + Kind: "Secret", + Name: secretName, + ValuesKey: PasswordKey, + TargetPath: valuesPath + "." + DigestKey(username), + Optional: true, + } +} + +// IsAccountValuesFrom reports whether a valuesFrom entry is one ValuesFrom +// builds, which the controller owns and the Application API keeps. +func IsAccountValuesFrom(ref helmv2.ValuesReference) bool { + return ref.Kind == "Secret" && ref.ValuesKey == PasswordKey && strings.HasSuffix(ref.Name, SecretSuffix) +} + +// IsAccountSecret reports whether a Secret is the account Secret of a +// CredentialTemplate. +func IsAccountSecret(sec *corev1.Secret) bool { + return sec != nil && sec.Labels[AccountLabel] == "true" +} diff --git a/pkg/credentials/account/account_test.go b/pkg/credentials/account/account_test.go new file mode 100644 index 000000000..cc7a7c0b9 --- /dev/null +++ b/pkg/credentials/account/account_test.go @@ -0,0 +1,109 @@ +// SPDX-License-Identifier: Apache-2.0 + +package account + +import ( + "strings" + "testing" + "time" + + helmv2 "github.com/fluxcd/helm-controller/api/v2" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" +) + +func TestMapName(t *testing.T) { + tests := []struct { + in, want string + wantErr bool + }{ + {in: "web", want: "web"}, + {in: "Bob.Smith", want: "bob-smith"}, + {in: "app_user", want: "app-user"}, + {in: "-leading", wantErr: true}, + {in: "", wantErr: true}, + {in: strings.Repeat("a", 64), wantErr: true}, + } + for _, tt := range tests { + got, err := MapName(tt.in) + if tt.wantErr { + if err == nil { + t.Errorf("MapName(%q) = %q, want an error", tt.in, got) + } + continue + } + if err != nil { + t.Errorf("MapName(%q): %v", tt.in, err) + continue + } + if got != tt.want { + t.Errorf("MapName(%q) = %q, want %q", tt.in, got, tt.want) + } + } +} + +func TestNames(t *testing.T) { + if got := SecretName("postgres-orders", "web"); got != "postgres-orders.web.account" { + t.Errorf("SecretName = %q", got) + } +} + +func TestRecordRoundTrip(t *testing.T) { + sec := &corev1.Secret{} + if r := ReadRecord(sec); r.Origin != "" || len(r.History) != 0 { + t.Errorf("empty Secret read as %+v", r) + } + + at := metav1.NewTime(time.Date(2026, 10, 2, 9, 14, 7, 0, time.UTC)) + r := Record{}.Apply(corev1alpha1.CredentialOperationCreate, at, "jane@example.org") + r = r.Apply(corev1alpha1.CredentialOperationMint, at, "jane@example.org") + WriteRecord(sec, r) + back := ReadRecord(sec) + if back.Origin != corev1alpha1.CredentialOriginMinted || back.By != "jane@example.org" || !back.At.Equal(&at) { + t.Errorf("record read back as %+v", back) + } + if len(back.History) != 2 || back.History[0].Operation != corev1alpha1.CredentialOperationMint || back.History[1].Operation != corev1alpha1.CredentialOperationCreate { + t.Errorf("history read back as %+v", back.History) + } + + for range HistoryLimit + 5 { + r = r.Apply(corev1alpha1.CredentialOperationRevoke, at, "bob") + } + if len(r.History) != HistoryLimit { + t.Errorf("history has %d entries, want %d", len(r.History), HistoryLimit) + } + if r.Origin != corev1alpha1.CredentialOriginRevoked { + t.Errorf("origin %s after revoke", r.Origin) + } +} + +func TestLabels(t *testing.T) { + l := Labels(map[string]string{"cnpg.io/reload": "true", AccountLabel: "false"}) + if l[AccountLabel] != "true" || l["cnpg.io/reload"] != "true" || len(l) != 2 { + t.Errorf("labels = %v, want the template's plus the account marker, which a template cannot override", l) + } + if !IsAccountSecret(&corev1.Secret{ObjectMeta: metav1.ObjectMeta{Labels: l}}) { + t.Error("labelled Secret not recognised") + } + if IsAccountSecret(&corev1.Secret{}) { + t.Error("bare Secret recognised") + } +} + +func TestValuesFrom(t *testing.T) { + ref := ValuesFrom("clickhouse-logs.web.account", "_accounts", "web") + if ref.Kind != "Secret" || ref.Name != "clickhouse-logs.web.account" || ref.ValuesKey != PasswordKey || !ref.Optional { + t.Errorf("entry = %+v", ref) + } + if !strings.HasPrefix(ref.TargetPath, "_accounts.u") || len(ref.TargetPath) != len("_accounts.u")+64 { + t.Errorf("targetPath = %q, want _accounts.u", ref.TargetPath) + } + if !IsAccountValuesFrom(ref) { + t.Error("an entry ValuesFrom builds is not recognised") + } + if IsAccountValuesFrom(helmv2.ValuesReference{Kind: "Secret", Name: "cozystack-values"}) { + t.Error("the platform values entry is taken for an account entry") + } +}