diff --git a/api/v1alpha1/applicationdefinition_credentials_types.go b/api/v1alpha1/applicationdefinition_credentials_types.go deleted file mode 100644 index 4d3e9b19a..000000000 --- a/api/v1alpha1/applicationdefinition_credentials_types.go +++ /dev/null @@ -1,162 +0,0 @@ -/* -Copyright 2026. - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ - -package v1alpha1 - -import ( - admissionregistrationv1 "k8s.io/api/admissionregistration/v1" -) - -// ApplicationDefinitionCredentials declares the accounts of an application -// kind and how the Cozystack API stores their passwords. Its presence converts -// the kind to platform-minted credentials. See -// design-proposals/user-secrets-api in cozystack/community. -type ApplicationDefinitionCredentials struct { - // Format is how a password is stored in the account Secret: the verifier the - // engine checks a login against, or the plaintext for an engine that needs it. - Format CredentialFormat `json:"format"` - - // SecretLabels are added to every account Secret of the kind, for an operator - // that notices a changed Secret only when it carries a label. - // +optional - SecretLabels map[string]string `json:"secretLabels,omitempty"` - - // ValuesPath makes the HelmRelease pull each tenant account's stored value - // into this values key, under u, for a chart - // whose operator cannot read a Secret. It starts with "_", which the - // Application API hides on read and refuses on write, so the value never - // appears in the application. - // +optional - // +kubebuilder:validation:Pattern=`^_[A-Za-z0-9_]*$` - ValuesPath string `json:"valuesPath,omitempty"` - - // Accounts are the sources the kind's accounts come from. A kind may list - // several, for example a values map of tenant users and a static platform - // account. - // +kubebuilder:validation:MinItems=1 - Accounts []CredentialAccountSource `json:"accounts"` -} - -// CredentialFormatKind says where the function that turns a password into -// its stored value lives. -// +kubebuilder:validation:Enum=BuiltIn;Webhook -type CredentialFormatKind string - -const ( - // CredentialFormatBuiltIn names a function compiled into the API. - CredentialFormatBuiltIn CredentialFormatKind = "BuiltIn" - // CredentialFormatWebhook names an endpoint the API calls with the - // plaintext. Declared, not served yet. - CredentialFormatWebhook CredentialFormatKind = "Webhook" -) - -// CredentialFormat selects the stored form of a password. -type CredentialFormat struct { - // Name of the format. The built-in names are scram-sha-256, mysql-native, - // sha256 and plaintext; a Webhook format carries any name. - // +kubebuilder:validation:MinLength=1 - Name string `json:"name"` - - // Kind defaults to BuiltIn. - // +optional - Kind CredentialFormatKind `json:"kind,omitempty"` - - // Webhook is the endpoint of a Webhook format. - // +optional - Webhook *CredentialWebhook `json:"webhook,omitempty"` -} - -// CredentialWebhook is the client configuration of a credentials webhook, in -// the shape every Kubernetes webhook uses. -type CredentialWebhook struct { - ClientConfig admissionregistrationv1.WebhookClientConfig `json:"clientConfig"` - - // +optional - // +kubebuilder:validation:Minimum=1 - // +kubebuilder:validation:Maximum=30 - TimeoutSeconds *int32 `json:"timeoutSeconds,omitempty"` -} - -// CredentialAccountSourceKind says how a source yields account names. -// +kubebuilder:validation:Enum=ValuesMap;ValuesList;Static;Expression;Webhook -type CredentialAccountSourceKind string - -const ( - // CredentialAccountsValuesMap takes the keys of a values map as accounts. - CredentialAccountsValuesMap CredentialAccountSourceKind = "ValuesMap" - // CredentialAccountsValuesList takes the name field of each object in a - // values list as an account. Declared, not served yet. - CredentialAccountsValuesList CredentialAccountSourceKind = "ValuesList" - // CredentialAccountsStatic is one fixed account. - CredentialAccountsStatic CredentialAccountSourceKind = "Static" - // CredentialAccountsExpression evaluates a CEL expression over the - // application. Declared, not served yet. - CredentialAccountsExpression CredentialAccountSourceKind = "Expression" - // CredentialAccountsWebhook asks an endpoint for the account list. - // Declared, not served yet. - CredentialAccountsWebhook CredentialAccountSourceKind = "Webhook" -) - -// CredentialAccountOwner says whose account it is. -// +kubebuilder:validation:Enum=tenant;platform -type CredentialAccountOwner string - -const ( - // CredentialAccountOwnerTenant is the default: the account gets a - // Credential, an account Secret and a mint. - CredentialAccountOwnerTenant CredentialAccountOwner = "tenant" - // CredentialAccountOwnerPlatform marks an account the platform's own - // components log in with. The API does nothing for it, and a ValuesMap - // source skips its name. - CredentialAccountOwnerPlatform CredentialAccountOwner = "platform" -) - -// CredentialAccountSource is one source of a kind's accounts. Which fields -// apply depends on Kind. -type CredentialAccountSource struct { - Kind CredentialAccountSourceKind `json:"kind"` - - // Path is the values key a ValuesMap or ValuesList source reads, as a - // dotted path from the top of the application spec. - // +optional - Path string `json:"path,omitempty"` - - // NameField is the field of each object a ValuesList source takes the - // account name from. - // +optional - NameField string `json:"nameField,omitempty"` - - // Name is the account of a Static source. - // +optional - Name string `json:"name,omitempty"` - - // When is a values key, as a dotted path, that switches a Static account - // on when it holds true; absent, the account is always on. - // +optional - When string `json:"when,omitempty"` - - // Owner defaults to tenant. - // +optional - Owner CredentialAccountOwner `json:"owner,omitempty"` - - // Expression is the CEL expression of an Expression source. - // +optional - Expression string `json:"expression,omitempty"` - - // Webhook is the endpoint of a Webhook source. - // +optional - Webhook *CredentialWebhook `json:"webhook,omitempty"` -} diff --git a/api/v1alpha1/applicationdefinitions_types.go b/api/v1alpha1/applicationdefinitions_types.go index 62969257c..0a6166254 100644 --- a/api/v1alpha1/applicationdefinitions_types.go +++ b/api/v1alpha1/applicationdefinitions_types.go @@ -61,10 +61,6 @@ type ApplicationDefinitionSpec struct { // Dashboard configuration for this resource Dashboard *ApplicationDefinitionDashboard `json:"dashboard,omitempty"` - - // Credentials converts the kind to platform-minted credentials. - // +optional - Credentials *ApplicationDefinitionCredentials `json:"credentials,omitempty"` } type ApplicationDefinitionApplication struct { diff --git a/api/v1alpha1/credentialtemplate_types.go b/api/v1alpha1/credentialtemplate_types.go new file mode 100644 index 000000000..e23709965 --- /dev/null +++ b/api/v1alpha1/credentialtemplate_types.go @@ -0,0 +1,138 @@ +// SPDX-License-Identifier: Apache-2.0 + +package v1alpha1 + +import ( + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +// CredentialTemplateSpec describes one account of an application and the +// Secret that stores its password. A kind's chart renders one per account; a +// tenant cannot write it. +type CredentialTemplateSpec struct { + // Username is the account name as the engine knows it. + // +kubebuilder:validation:MinLength=1 + // +kubebuilder:validation:XValidation:rule="self == oldSelf",message="username is immutable" + Username string `json:"username"` + + // Format is how a password is stored in the account Secret. + Format CredentialTemplateFormat `json:"format"` + + // Secret describes the account Secret beyond its fixed username and + // password keys. + // +optional + Secret CredentialTemplateSecret `json:"secret,omitempty"` + + // ValuesPath, for an engine whose operator reads no Secret, asks for the + // stored value to be pulled into the release's values under + // .u. It starts with "_", which + // the Application API hides on read and refuses on write. + // +optional + // +kubebuilder:validation:Pattern=`^_[A-Za-z0-9_]*$` + ValuesPath string `json:"valuesPath,omitempty"` +} + +// CredentialTemplateFormat names a stored form of a password. +type CredentialTemplateFormat struct { + // Name is a built-in format: scram-sha-256, mysql-native, sha256 or + // plaintext. + // +kubebuilder:validation:MinLength=1 + Name string `json:"name"` +} + +// CredentialTemplateSecret describes the account Secret. +type CredentialTemplateSecret struct { + // Labels are put on the account Secret, for an operator that notices a + // changed Secret only when it carries a label. + // +optional + // +kubebuilder:validation:XValidation:rule="!('credentials.cozystack.io/account' in self)",message="the credentials.cozystack.io/account label is reserved" + Labels map[string]string `json:"labels,omitempty"` +} + +// CredentialTemplateStatus mirrors the record the account Secret keeps, so the +// Credential view reads templates only. +type CredentialTemplateStatus struct { + // SecretResourceVersion is the resourceVersion of the account Secret this + // status mirrors; empty while the account has no Secret. + // +optional + SecretResourceVersion string `json:"secretResourceVersion,omitempty"` + + // Password says where the live password came from. + // +optional + Password CredentialTemplatePasswordState `json:"password,omitempty"` + + // History holds the last ten operations, newest first. + // +optional + // +listType=atomic + History []CredentialTemplateHistoryEntry `json:"history,omitempty"` + + // ObservedGeneration is the generation the controller last acted on. + // +optional + ObservedGeneration int64 `json:"observedGeneration,omitempty"` + + // +optional + // +listType=map + // +listMapKey=type + Conditions []metav1.Condition `json:"conditions,omitempty"` +} + +// CredentialTemplatePasswordState records the operation that set the live +// password. +type CredentialTemplatePasswordState struct { + // Origin is NotIssued, Migrated, Minted or Revoked. + // +optional + Origin string `json:"origin,omitempty"` + // +optional + At *metav1.Time `json:"at,omitempty"` + // +optional + By string `json:"by,omitempty"` +} + +// CredentialTemplateHistoryEntry is one recorded operation on the account. +type CredentialTemplateHistoryEntry struct { + // Operation is Create, Migrate, Mint or Revoke. + Operation string `json:"operation"` + At metav1.Time `json:"at"` + By string `json:"by"` +} + +// Condition types a CredentialTemplate reports. +const ( + // CredentialTemplateSecretConflict: a Secret at the account Secret's name + // that the controller did not create or adopt; it is left alone. + CredentialTemplateSecretConflict = "SecretConflict" + // CredentialTemplateUnknownFormat: the format names no built-in format. + CredentialTemplateUnknownFormat = "UnknownFormat" +) + +// +kubebuilder:object:root=true +// +kubebuilder:subresource:status +// +kubebuilder:printcolumn:name="User",type="string",JSONPath=".spec.username" +// +kubebuilder:printcolumn:name="Format",type="string",JSONPath=".spec.format.name" +// +kubebuilder:printcolumn:name="Origin",type="string",JSONPath=".status.password.origin" +// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp" + +// CredentialTemplate is one account of an application, rendered by the kind's +// chart. A controller keeps its account Secret, named .account, and the +// Cozystack API mints and revokes through it. See +// design-proposals/user-secrets-api in cozystack/community. +type CredentialTemplate struct { + metav1.TypeMeta `json:",inline"` + metav1.ObjectMeta `json:"metadata,omitempty"` + + Spec CredentialTemplateSpec `json:"spec,omitempty"` + Status CredentialTemplateStatus `json:"status,omitempty"` +} + +// +kubebuilder:object:root=true + +// CredentialTemplateList contains a list of CredentialTemplate. +type CredentialTemplateList struct { + metav1.TypeMeta `json:",inline"` + metav1.ListMeta `json:"metadata,omitempty"` + Items []CredentialTemplate `json:"items"` +} + +func init() { + SchemeBuilder.Register(&CredentialTemplate{}, &CredentialTemplateList{}) +} diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index 7f374f54e..f9f4ccc5d 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -23,9 +23,9 @@ package v1alpha1 import ( "github.com/fluxcd/helm-controller/api/v2" "github.com/fluxcd/pkg/apis/kustomize" - "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/api/resource" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) @@ -70,36 +70,6 @@ func (in *ApplicationDefinitionApplication) DeepCopy() *ApplicationDefinitionApp return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ApplicationDefinitionCredentials) DeepCopyInto(out *ApplicationDefinitionCredentials) { - *out = *in - in.Format.DeepCopyInto(&out.Format) - if in.SecretLabels != nil { - in, out := &in.SecretLabels, &out.SecretLabels - *out = make(map[string]string, len(*in)) - for key, val := range *in { - (*out)[key] = val - } - } - if in.Accounts != nil { - in, out := &in.Accounts, &out.Accounts - *out = make([]CredentialAccountSource, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationDefinitionCredentials. -func (in *ApplicationDefinitionCredentials) DeepCopy() *ApplicationDefinitionCredentials { - if in == nil { - return nil - } - out := new(ApplicationDefinitionCredentials) - in.DeepCopyInto(out) - return out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *ApplicationDefinitionDashboard) DeepCopyInto(out *ApplicationDefinitionDashboard) { *out = *in @@ -271,11 +241,6 @@ func (in *ApplicationDefinitionSpec) DeepCopyInto(out *ApplicationDefinitionSpec *out = new(ApplicationDefinitionDashboard) (*in).DeepCopyInto(*out) } - if in.Credentials != nil { - in, out := &in.Credentials, &out.Credentials - *out = new(ApplicationDefinitionCredentials) - (*in).DeepCopyInto(*out) - } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ApplicationDefinitionSpec. @@ -344,62 +309,179 @@ func (in *ComponentInstall) DeepCopy() *ComponentInstall { } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CredentialAccountSource) DeepCopyInto(out *CredentialAccountSource) { +func (in *CredentialTemplate) DeepCopyInto(out *CredentialTemplate) { *out = *in - if in.Webhook != nil { - in, out := &in.Webhook, &out.Webhook - *out = new(CredentialWebhook) - (*in).DeepCopyInto(*out) - } + out.TypeMeta = in.TypeMeta + in.ObjectMeta.DeepCopyInto(&out.ObjectMeta) + in.Spec.DeepCopyInto(&out.Spec) + in.Status.DeepCopyInto(&out.Status) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialAccountSource. -func (in *CredentialAccountSource) DeepCopy() *CredentialAccountSource { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplate. +func (in *CredentialTemplate) DeepCopy() *CredentialTemplate { if in == nil { return nil } - out := new(CredentialAccountSource) + out := new(CredentialTemplate) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CredentialTemplate) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplateFormat) DeepCopyInto(out *CredentialTemplateFormat) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateFormat. +func (in *CredentialTemplateFormat) DeepCopy() *CredentialTemplateFormat { + if in == nil { + return nil + } + out := new(CredentialTemplateFormat) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CredentialFormat) DeepCopyInto(out *CredentialFormat) { +func (in *CredentialTemplateHistoryEntry) DeepCopyInto(out *CredentialTemplateHistoryEntry) { *out = *in - if in.Webhook != nil { - in, out := &in.Webhook, &out.Webhook - *out = new(CredentialWebhook) - (*in).DeepCopyInto(*out) - } + in.At.DeepCopyInto(&out.At) } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialFormat. -func (in *CredentialFormat) DeepCopy() *CredentialFormat { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateHistoryEntry. +func (in *CredentialTemplateHistoryEntry) DeepCopy() *CredentialTemplateHistoryEntry { if in == nil { return nil } - out := new(CredentialFormat) + out := new(CredentialTemplateHistoryEntry) in.DeepCopyInto(out) return out } // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *CredentialWebhook) DeepCopyInto(out *CredentialWebhook) { +func (in *CredentialTemplateList) DeepCopyInto(out *CredentialTemplateList) { *out = *in - in.ClientConfig.DeepCopyInto(&out.ClientConfig) - if in.TimeoutSeconds != nil { - in, out := &in.TimeoutSeconds, &out.TimeoutSeconds - *out = new(int32) - **out = **in + out.TypeMeta = in.TypeMeta + in.ListMeta.DeepCopyInto(&out.ListMeta) + if in.Items != nil { + in, out := &in.Items, &out.Items + *out = make([]CredentialTemplate, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } } } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialWebhook. -func (in *CredentialWebhook) DeepCopy() *CredentialWebhook { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateList. +func (in *CredentialTemplateList) DeepCopy() *CredentialTemplateList { if in == nil { return nil } - out := new(CredentialWebhook) + out := new(CredentialTemplateList) + in.DeepCopyInto(out) + return out +} + +// DeepCopyObject is an autogenerated deepcopy function, copying the receiver, creating a new runtime.Object. +func (in *CredentialTemplateList) DeepCopyObject() runtime.Object { + if c := in.DeepCopy(); c != nil { + return c + } + return nil +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplatePasswordState) DeepCopyInto(out *CredentialTemplatePasswordState) { + *out = *in + if in.At != nil { + in, out := &in.At, &out.At + *out = (*in).DeepCopy() + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplatePasswordState. +func (in *CredentialTemplatePasswordState) DeepCopy() *CredentialTemplatePasswordState { + if in == nil { + return nil + } + out := new(CredentialTemplatePasswordState) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplateSecret) DeepCopyInto(out *CredentialTemplateSecret) { + *out = *in + if in.Labels != nil { + in, out := &in.Labels, &out.Labels + *out = make(map[string]string, len(*in)) + for key, val := range *in { + (*out)[key] = val + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateSecret. +func (in *CredentialTemplateSecret) DeepCopy() *CredentialTemplateSecret { + if in == nil { + return nil + } + out := new(CredentialTemplateSecret) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplateSpec) DeepCopyInto(out *CredentialTemplateSpec) { + *out = *in + out.Format = in.Format + in.Secret.DeepCopyInto(&out.Secret) +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateSpec. +func (in *CredentialTemplateSpec) DeepCopy() *CredentialTemplateSpec { + if in == nil { + return nil + } + out := new(CredentialTemplateSpec) + in.DeepCopyInto(out) + return out +} + +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplateStatus) DeepCopyInto(out *CredentialTemplateStatus) { + *out = *in + in.Password.DeepCopyInto(&out.Password) + if in.History != nil { + in, out := &in.History, &out.History + *out = make([]CredentialTemplateHistoryEntry, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } + if in.Conditions != nil { + in, out := &in.Conditions, &out.Conditions + *out = make([]v1.Condition, len(*in)) + for i := range *in { + (*in)[i].DeepCopyInto(&(*out)[i]) + } + } +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateStatus. +func (in *CredentialTemplateStatus) DeepCopy() *CredentialTemplateStatus { + if in == nil { + return nil + } + out := new(CredentialTemplateStatus) in.DeepCopyInto(out) return out } @@ -471,7 +553,7 @@ func (in *PackageComponent) DeepCopyInto(out *PackageComponent) { } if in.Values != nil { in, out := &in.Values, &out.Values - *out = new(v1.JSON) + *out = new(apiextensionsv1.JSON) (*in).DeepCopyInto(*out) } } @@ -624,7 +706,7 @@ func (in *PackageSourceStatus) DeepCopyInto(out *PackageSourceStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]metav1.Condition, len(*in)) + *out = make([]v1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -673,7 +755,7 @@ func (in *PackageStatus) DeepCopyInto(out *PackageStatus) { *out = *in if in.Conditions != nil { in, out := &in.Conditions, &out.Conditions - *out = make([]metav1.Condition, len(*in)) + *out = make([]v1.Condition, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } diff --git a/cmd/cozystack-controller/main.go b/cmd/cozystack-controller/main.go index 59ba3b515..69b46157f 100644 --- a/cmd/cozystack-controller/main.go +++ b/cmd/cozystack-controller/main.go @@ -337,6 +337,25 @@ func main() { os.Exit(1) } + // Account Secrets are watched as metadata on the same unscoped metadata + // cache, for the reason the CA-extraction reconciler gives above, and read + // uncached: an account Secret may hold a plaintext. + if err = (&controller.CredentialTemplateReconciler{ + Client: mgr.GetClient(), + Reader: mgr.GetAPIReader(), + Scheme: mgr.GetScheme(), + Recorder: mgr.GetEventRecorderFor("credentialtemplate-controller"), + }).SetupWithManager(mgr, caSecretCluster.GetCache()); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "CredentialTemplate") + os.Exit(1) + } + if err = (&controller.CredentialValuesReconciler{ + Client: mgr.GetClient(), + }).SetupWithManager(mgr); err != nil { + setupLog.Error(err, "unable to create controller", "controller", "CredentialValues") + os.Exit(1) + } + // +kubebuilder:scaffold:builder if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { diff --git a/internal/controller/applicationdefinition_helmreconciler.go b/internal/controller/applicationdefinition_helmreconciler.go index e8461b3e2..b370e18fc 100644 --- a/internal/controller/applicationdefinition_helmreconciler.go +++ b/internal/controller/applicationdefinition_helmreconciler.go @@ -7,9 +7,8 @@ import ( cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" "github.com/cozystack/cozystack/internal/shared/appdefowner" - "github.com/cozystack/cozystack/pkg/apis/apps/defaulting" "github.com/cozystack/cozystack/pkg/config" - "github.com/cozystack/cozystack/pkg/credentials/store" + "github.com/cozystack/cozystack/pkg/credentials/account" helmv2 "github.com/fluxcd/helm-controller/api/v2" corev1 "k8s.io/api/core/v1" @@ -153,31 +152,14 @@ func (r *ApplicationDefinitionHelmReconciler) updateHelmReleasesForAppDef(ctx co return errors.Join(errs...) } -// expectedValuesFrom returns the valuesFrom a release of a kind without a -// credentials block carries. +// expectedValuesFrom returns the expected valuesFrom configuration for HelmReleases func expectedValuesFrom() []helmv2.ValuesReference { - return store.ValuesFrom(nil, "", nil) -} - -// releaseValuesFrom returns the valuesFrom a release carries under its -// definition, resolving the accounts with the definition's schema defaults as -// the API does on a write, so a release created before the block appeared -// gets its entries on reconcile. An error means values the API would refuse; -// the release is left as it is. -func releaseValuesFrom(hr *helmv2.HelmRelease, appDef *cozyv1alpha1.ApplicationDefinition) ([]helmv2.ValuesReference, error) { - block := appDef.Spec.Credentials - if block == nil || block.ValuesPath == "" { - return store.ValuesFrom(block, hr.Name, nil), nil + return []helmv2.ValuesReference{ + { + Kind: "Secret", + Name: "cozystack-values", + }, } - schema, err := defaulting.BuildSchema(appDef.Spec.Application.OpenAPISchema) - if err != nil { - return nil, fmt.Errorf("build schema: %w", err) - } - accounts, err := store.ResolveAccounts(block, schema, hr.Spec.Values) - if err != nil { - return nil, err - } - return store.ValuesFrom(block, hr.Name, accounts), nil } // valuesFromEqual compares two ValuesReference slices @@ -229,10 +211,16 @@ func (r *ApplicationDefinitionHelmReconciler) updateHelmReleaseChart(ctx context updated = true } - // Check and update valuesFrom configuration - if expected, err := releaseValuesFrom(hr, appDef); err != nil { - logger.Error(err, "Skipping HelmRelease valuesFrom update: accounts cannot be resolved", "name", hr.Name, "namespace", hr.Namespace) - } else if !valuesFromEqual(hrCopy.Spec.ValuesFrom, expected) { + // Check and update valuesFrom configuration. The entries that carry an + // account's stored value into the chart belong to the CredentialTemplate + // controller, so they are kept as they are. + expected := expectedValuesFrom() + for _, ref := range hrCopy.Spec.ValuesFrom { + if account.IsAccountValuesFrom(ref) { + expected = append(expected, ref) + } + } + if !valuesFromEqual(hrCopy.Spec.ValuesFrom, expected) { logger.V(4).Info("Updating HelmRelease valuesFrom", "name", hr.Name, "namespace", hr.Namespace) hrCopy.Spec.ValuesFrom = expected updated = true diff --git a/internal/controller/applicationdefinition_helmreconciler_test.go b/internal/controller/applicationdefinition_helmreconciler_test.go index ac18211f6..fa293f073 100644 --- a/internal/controller/applicationdefinition_helmreconciler_test.go +++ b/internal/controller/applicationdefinition_helmreconciler_test.go @@ -11,10 +11,8 @@ import ( cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" "github.com/cozystack/cozystack/pkg/config" "github.com/cozystack/cozystack/pkg/credentials/account" - "github.com/cozystack/cozystack/pkg/credentials/store" helmv2 "github.com/fluxcd/helm-controller/api/v2" "github.com/fluxcd/pkg/apis/kustomize" - apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -963,49 +961,45 @@ func TestAppDefHelm_UpdateFailureRequeues(t *testing.T) { } } -// TestAppDefHelm_CredentialsValuesFrom pins that a definition with a -// credentials block naming a valuesPath gives every release of the kind one -// valuesFrom entry per account, resolved with the schema defaults applied, so -// a release created before the block appeared gets them on reconcile. -func TestAppDefHelm_CredentialsValuesFrom(t *testing.T) { +// TestAppDefHelm_KeepsAccountValuesFrom pins that the canonical valuesFrom +// leaves the entries the CredentialTemplate controller owns in place: they +// carry each account's stored value into the chart, and stripping them on +// every definition change would take the accounts out of the release. +func TestAppDefHelm_KeepsAccountValuesFrom(t *testing.T) { scheme := newAppDefHelmScheme(t) - appDef := vmInstanceAppDef(nil) - appDef.Spec.Application.OpenAPISchema = `{"type":"object","properties":{"backup":{"type":"object","default":{},"properties":{"enabled":{"type":"boolean","default":true}}}}}` - appDef.Spec.Credentials = &cozyv1alpha1.ApplicationDefinitionCredentials{ - Format: cozyv1alpha1.CredentialFormat{Name: "scram-sha-256"}, - ValuesPath: "_accounts", - Accounts: []cozyv1alpha1.CredentialAccountSource{ - {Kind: cozyv1alpha1.CredentialAccountsValuesMap, Path: "users"}, - {Kind: cozyv1alpha1.CredentialAccountsStatic, Name: "backup", When: "backup.enabled"}, + chartRef := &helmv2.CrossNamespaceSourceReference{Kind: "OCIRepository", Name: "clickhouse-app", Namespace: "cozy-public"} + appDef := &cozyv1alpha1.ApplicationDefinition{ + ObjectMeta: metav1.ObjectMeta{Name: "clickhouse"}, + Spec: cozyv1alpha1.ApplicationDefinitionSpec{ + Application: cozyv1alpha1.ApplicationDefinitionApplication{Kind: "ClickHouse"}, + Release: cozyv1alpha1.ApplicationDefinitionRelease{ChartRef: chartRef}, + }, + } + entry := account.ValuesFrom("clickhouse-logs.web.account", "_accounts", "web") + hr := &helmv2.HelmRelease{ + ObjectMeta: metav1.ObjectMeta{ + Name: "clickhouse-logs", + Namespace: "tenant-foo", + Labels: map[string]string{ + "apps.cozystack.io/application.kind": "ClickHouse", + "apps.cozystack.io/application.group": "apps.cozystack.io", + }, + }, + Spec: helmv2.HelmReleaseSpec{ + ChartRef: chartRef, + ValuesFrom: []helmv2.ValuesReference{{Kind: "ConfigMap", Name: "wrong"}, entry}, }, } - hr := vmInstanceRelease(nil, nil) - hr.Spec.ValuesFrom = expectedValuesFrom() - hr.Spec.Values = &apiextv1.JSON{Raw: []byte(`{"users":{"web":{}}}`)} fakeClient := fake.NewClientBuilder().WithScheme(scheme).WithObjects(appDef, hr).Build() - r := &ApplicationDefinitionHelmReconciler{Client: fakeClient, Scheme: scheme} - if _, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: appDef.Name}}); err != nil { + if _, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: "clickhouse"}}); err != nil { t.Fatalf("unexpected error: %v", err) } got := &helmv2.HelmRelease{} - if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, got); err != nil { - t.Fatalf("get HR: %v", err) + if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: "clickhouse-logs", Namespace: "tenant-foo"}, got); err != nil { + t.Fatal(err) } - want := store.ValuesFrom(appDef.Spec.Credentials, hr.Name, []account.Account{{Username: "web", Mapped: "web"}, {Username: "backup", Mapped: "backup"}}) - if !valuesFromEqual(got.Spec.ValuesFrom, want) { - t.Fatalf("valuesFrom = %+v, want %+v", got.Spec.ValuesFrom, want) - } - - // Idempotent once in line. - before := got.ResourceVersion - if _, err := r.Reconcile(context.TODO(), reconcile.Request{NamespacedName: types.NamespacedName{Name: appDef.Name}}); err != nil { - t.Fatalf("unexpected error: %v", err) - } - if err := fakeClient.Get(context.TODO(), types.NamespacedName{Name: hr.Name, Namespace: hr.Namespace}, got); err != nil { - t.Fatalf("get HR: %v", err) - } - if got.ResourceVersion != before { - t.Fatalf("release rewritten with nothing to change: %q -> %q", before, got.ResourceVersion) + if len(got.Spec.ValuesFrom) != 2 || got.Spec.ValuesFrom[0].Name != "cozystack-values" || got.Spec.ValuesFrom[1] != entry { + t.Fatalf("valuesFrom = %+v, want the platform values and the account entry", got.Spec.ValuesFrom) } } diff --git a/internal/controller/credentialtemplate_controller.go b/internal/controller/credentialtemplate_controller.go new file mode 100644 index 000000000..21a728f42 --- /dev/null +++ b/internal/controller/credentialtemplate_controller.go @@ -0,0 +1,404 @@ +// SPDX-License-Identifier: Apache-2.0 + +package controller + +import ( + "context" + "fmt" + "sort" + "time" + + helmv2 "github.com/fluxcd/helm-controller/api/v2" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/equality" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/tools/record" + ctrl "sigs.k8s.io/controller-runtime" + "sigs.k8s.io/controller-runtime/pkg/builder" + "sigs.k8s.io/controller-runtime/pkg/cache" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" + "sigs.k8s.io/controller-runtime/pkg/handler" + "sigs.k8s.io/controller-runtime/pkg/log" + "sigs.k8s.io/controller-runtime/pkg/predicate" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + "sigs.k8s.io/controller-runtime/pkg/source" + + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" + "github.com/cozystack/cozystack/pkg/credentials/account" + "github.com/cozystack/cozystack/pkg/credentials/format" +) + +// +kubebuilder:rbac:groups=cozystack.io,resources=credentialtemplates,verbs=get;list;watch +// +kubebuilder:rbac:groups=cozystack.io,resources=credentialtemplates/status,verbs=get;update;patch +// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch +// +kubebuilder:rbac:groups=helm.toolkit.fluxcd.io,resources=helmreleases,verbs=get;list;watch;update;patch + +// CredentialControllerActor is who the record names for the operations the +// controller performs: seeding a Secret, or seeding it again after a format +// change. +const CredentialControllerActor = "system:serviceaccount:cozy-system:cozystack-controller" + +// Event reasons the controller records on a CredentialTemplate. +const ( + reasonCredentialReset = "CredentialReset" + reasonCredentialAdopted = "CredentialAdopted" +) + +// CredentialTemplateReconciler keeps each CredentialTemplate and its account +// Secret in step: it seeds a missing Secret on a release marked for seeding, +// seeds it again when the template's format changes, adopts an unowned Secret +// the conversion wrote, leaves any other Secret at the name alone, and mirrors +// the Secret's record into the template's status. See +// design-proposals/user-secrets-api in cozystack/community. +// +// Secrets are read through Reader, uncached: the manager's Secret cache is +// scoped to other reconcilers, and an account Secret may hold a plaintext +// that has no business in a cache. Their changes reach the controller through +// a metadata-only watch. +type CredentialTemplateReconciler struct { + client.Client + Reader client.Reader + Scheme *runtime.Scheme + Recorder record.EventRecorder + Now func() time.Time +} + +func (r *CredentialTemplateReconciler) now() metav1.Time { + if r.Now != nil { + return metav1.NewTime(r.Now()) + } + return metav1.Now() +} + +func (r *CredentialTemplateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + logger := log.FromContext(ctx) + + tmpl := &cozyv1alpha1.CredentialTemplate{} + if err := r.Get(ctx, req.NamespacedName, tmpl); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + if !tmpl.DeletionTimestamp.IsZero() { + return ctrl.Result{}, nil + } + status := tmpl.Status.DeepCopy() + status.ObservedGeneration = tmpl.Generation + + if !format.Known(tmpl.Spec.Format.Name) { + meta.SetStatusCondition(&status.Conditions, metav1.Condition{ + Type: cozyv1alpha1.CredentialTemplateUnknownFormat, + Status: metav1.ConditionTrue, + Reason: "NotBuiltIn", + Message: fmt.Sprintf("format %q is not a built-in format", tmpl.Spec.Format.Name), + }) + return ctrl.Result{}, r.writeStatus(ctx, tmpl, status) + } + meta.RemoveStatusCondition(&status.Conditions, cozyv1alpha1.CredentialTemplateUnknownFormat) + + sec, err := r.secret(ctx, tmpl) + if err != nil { + return ctrl.Result{}, err + } + + switch { + case sec == nil: + seed, err := r.seedAllowed(ctx, tmpl) + if err != nil { + return ctrl.Result{}, err + } + if seed { + if sec, err = r.seed(ctx, tmpl); err != nil { + return ctrl.Result{}, err + } + } + case metav1.IsControlledBy(sec, tmpl): + if sec.Annotations[account.FormatAnnotation] != tmpl.Spec.Format.Name { + if sec, err = r.reseed(ctx, tmpl, sec); err != nil { + return ctrl.Result{}, err + } + } else if sec, err = r.alignLabels(ctx, tmpl, sec); err != nil { + return ctrl.Result{}, err + } + case adoptable(sec, tmpl): + if sec, err = r.adopt(ctx, tmpl, sec); err != nil { + return ctrl.Result{}, err + } + default: + logger.Info("leaving a Secret the controller did not create alone", "secret", sec.Name) + meta.SetStatusCondition(&status.Conditions, metav1.Condition{ + Type: cozyv1alpha1.CredentialTemplateSecretConflict, + Status: metav1.ConditionTrue, + Reason: "ForeignSecret", + Message: fmt.Sprintf("Secret %s exists and was not created for this template; remove it to let the account be seeded", sec.Name), + }) + mirror(status, nil) + return ctrl.Result{}, r.writeStatus(ctx, tmpl, status) + } + meta.RemoveStatusCondition(&status.Conditions, cozyv1alpha1.CredentialTemplateSecretConflict) + mirror(status, sec) + return ctrl.Result{}, r.writeStatus(ctx, tmpl, status) +} + +// secret reads the template's account Secret, nil when it does not exist. +func (r *CredentialTemplateReconciler) secret(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate) (*corev1.Secret, error) { + sec := &corev1.Secret{} + err := r.Reader.Get(ctx, types.NamespacedName{Namespace: tmpl.Namespace, Name: tmpl.Name + account.SecretSuffix}, sec) + switch { + case apierrors.IsNotFound(err): + return nil, nil + case err != nil: + return nil, err + } + return sec, nil +} + +// seedAllowed reports whether the template's release is marked for seeding. +// A release without the mark keeps passwords the platform never saw, which a +// seeded random password would replace under its clients. +func (r *CredentialTemplateReconciler) seedAllowed(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate) (bool, error) { + release := tmpl.Annotations[account.ReleaseNameAnnotation] + if release == "" { + return false, nil + } + hr := &helmv2.HelmRelease{} + if err := r.Get(ctx, types.NamespacedName{Namespace: tmpl.Namespace, Name: release}, hr); err != nil { + return false, client.IgnoreNotFound(err) + } + return hr.Annotations[account.SeedAnnotation] == "true", nil +} + +// newSecret builds the account Secret of a template holding the stored form of +// a random password that is discarded here. +func (r *CredentialTemplateReconciler) newSecret(tmpl *cozyv1alpha1.CredentialTemplate, rec account.Record) (*corev1.Secret, error) { + password, err := format.GeneratePassword() + if err != nil { + return nil, err + } + stored, err := format.Encode(tmpl.Spec.Format.Name, password) + if err != nil { + return nil, err + } + sec := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: tmpl.Name + account.SecretSuffix, + Namespace: tmpl.Namespace, + Labels: account.Labels(tmpl.Spec.Secret.Labels), + Annotations: map[string]string{account.FormatAnnotation: tmpl.Spec.Format.Name}, + }, + Type: corev1.SecretTypeBasicAuth, + Data: map[string][]byte{ + account.UsernameKey: []byte(tmpl.Spec.Username), + account.PasswordKey: []byte(stored), + }, + } + account.WriteRecord(sec, rec.Apply(corev1alpha1.CredentialOperationCreate, r.now(), CredentialControllerActor)) + if err := controllerutil.SetControllerReference(tmpl, sec, r.Scheme); err != nil { + return nil, err + } + return sec, nil +} + +func (r *CredentialTemplateReconciler) seed(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate) (*corev1.Secret, error) { + sec, err := r.newSecret(tmpl, account.Record{}) + if err != nil { + return nil, err + } + if err := r.Create(ctx, sec); err != nil { + if apierrors.IsAlreadyExists(err) { + // A mint created it in between; the next reconcile mirrors it. + return r.secret(ctx, tmpl) + } + return nil, err + } + return sec, nil +} + +// reseed replaces a Secret whose value is stored in another format than the +// template now names: a stored value cannot be converted without the +// password, so the account starts over as NotIssued. +func (r *CredentialTemplateReconciler) reseed(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate, cur *corev1.Secret) (*corev1.Secret, error) { + sec, err := r.newSecret(tmpl, account.ReadRecord(cur)) + if err != nil { + return nil, err + } + sec.ResourceVersion = cur.ResourceVersion + if err := r.Update(ctx, sec); err != nil { + return nil, err + } + if r.Recorder != nil { + r.Recorder.Eventf(tmpl, corev1.EventTypeWarning, reasonCredentialReset, + "format changed from %q to %q; the account was given a new password nobody holds, mint one", cur.Annotations[account.FormatAnnotation], tmpl.Spec.Format.Name) + } + return sec, nil +} + +// alignLabels brings the Secret's labels in line with the template's, without +// touching its value or record. +func (r *CredentialTemplateReconciler) alignLabels(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate, sec *corev1.Secret) (*corev1.Secret, error) { + want := account.Labels(tmpl.Spec.Secret.Labels) + if equality.Semantic.DeepEqual(sec.Labels, want) { + return sec, nil + } + updated := sec.DeepCopy() + updated.Labels = want + if err := r.Update(ctx, updated); err != nil { + return nil, err + } + return updated, nil +} + +// adoptable reports whether an unowned Secret at the template's name is one +// the conversion wrote for this account: the account marker, no controller, +// and the same username. +func adoptable(sec *corev1.Secret, tmpl *cozyv1alpha1.CredentialTemplate) bool { + return account.IsAccountSecret(sec) && + metav1.GetControllerOf(sec) == nil && + string(sec.Data[account.UsernameKey]) == tmpl.Spec.Username +} + +// adopt takes over a Secret the conversion wrote, keeping its value and +// record. A Secret without a format annotation was written in the format its +// kind converted to, which the template names. +func (r *CredentialTemplateReconciler) adopt(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate, cur *corev1.Secret) (*corev1.Secret, error) { + sec := cur.DeepCopy() + if err := controllerutil.SetControllerReference(tmpl, sec, r.Scheme); err != nil { + return nil, err + } + if sec.Annotations == nil { + sec.Annotations = map[string]string{} + } + if sec.Annotations[account.FormatAnnotation] == "" { + sec.Annotations[account.FormatAnnotation] = tmpl.Spec.Format.Name + } + sec.Labels = account.Labels(tmpl.Spec.Secret.Labels) + if err := r.Update(ctx, sec); err != nil { + return nil, err + } + if r.Recorder != nil { + r.Recorder.Event(tmpl, corev1.EventTypeNormal, reasonCredentialAdopted, "adopted the account Secret the conversion wrote") + } + return sec, nil +} + +// mirror copies the record of the account Secret into the template's status; +// nil clears it. +func mirror(status *cozyv1alpha1.CredentialTemplateStatus, sec *corev1.Secret) { + if sec == nil { + status.SecretResourceVersion = "" + status.Password = cozyv1alpha1.CredentialTemplatePasswordState{Origin: string(corev1alpha1.CredentialOriginNotIssued)} + status.History = nil + return + } + rec := account.ReadRecord(sec) + status.SecretResourceVersion = sec.ResourceVersion + status.Password = cozyv1alpha1.CredentialTemplatePasswordState{Origin: string(rec.Origin), By: rec.By} + if rec.Origin == "" { + status.Password.Origin = string(corev1alpha1.CredentialOriginNotIssued) + } + if !rec.At.IsZero() { + at := rec.At + status.Password.At = &at + } + status.History = nil + for _, h := range rec.History { + status.History = append(status.History, cozyv1alpha1.CredentialTemplateHistoryEntry{Operation: string(h.Operation), At: h.At, By: h.By}) + } +} + +func (r *CredentialTemplateReconciler) writeStatus(ctx context.Context, tmpl *cozyv1alpha1.CredentialTemplate, status *cozyv1alpha1.CredentialTemplateStatus) error { + if equality.Semantic.DeepEqual(&tmpl.Status, status) { + return nil + } + updated := tmpl.DeepCopy() + updated.Status = *status + return r.Status().Update(ctx, updated) +} + +// SetupWithManager registers the reconciler. accountSecrets is a metadata +// cache of Secrets, so a change to an account Secret (a mint, a revoke) +// reaches the template that owns it without any Secret data being cached. +func (r *CredentialTemplateReconciler) SetupWithManager(mgr ctrl.Manager, accountSecrets cache.Cache) error { + secretMeta := &metav1.PartialObjectMetadata{} + secretMeta.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret")) + isAccount := predicate.NewTypedPredicateFuncs(func(o *metav1.PartialObjectMetadata) bool { + return o.GetLabels()[account.AccountLabel] == "true" + }) + owner := handler.TypedEnqueueRequestForOwner[*metav1.PartialObjectMetadata]( + mgr.GetScheme(), mgr.GetRESTMapper(), &cozyv1alpha1.CredentialTemplate{}, handler.OnlyControllerOwner()) + return ctrl.NewControllerManagedBy(mgr). + Named("credentialtemplate"). + For(&cozyv1alpha1.CredentialTemplate{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})). + WatchesRawSource(source.Kind(accountSecrets, secretMeta, owner, isAccount)). + Complete(r) +} + +// CredentialValuesReconciler keeps the valuesFrom entries of a HelmRelease in +// step with the templates of its release that name a valuesPath, for an +// engine whose operator reads no Secret. It is keyed by the HelmRelease but +// watches only templates, so helm-controller's status writes do not wake it. +type CredentialValuesReconciler struct { + client.Client +} + +// +kubebuilder:rbac:groups=helm.toolkit.fluxcd.io,resources=helmreleases,verbs=get;list;watch;update;patch + +func (r *CredentialValuesReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { + hr := &helmv2.HelmRelease{} + if err := r.Get(ctx, req.NamespacedName, hr); err != nil { + return ctrl.Result{}, client.IgnoreNotFound(err) + } + templates := &cozyv1alpha1.CredentialTemplateList{} + if err := r.List(ctx, templates, client.InNamespace(hr.Namespace)); err != nil { + return ctrl.Result{}, err + } + want := DesiredValuesFrom(hr, templates.Items) + if equality.Semantic.DeepEqual(hr.Spec.ValuesFrom, want) { + return ctrl.Result{}, nil + } + updated := hr.DeepCopy() + updated.Spec.ValuesFrom = want + return ctrl.Result{}, r.Update(ctx, updated) +} + +// DesiredValuesFrom returns the valuesFrom of a HelmRelease with its account +// entries replaced by one per live template of the release that names a +// valuesPath, ordered by template name; every other entry stays where it is. +func DesiredValuesFrom(hr *helmv2.HelmRelease, templates []cozyv1alpha1.CredentialTemplate) []helmv2.ValuesReference { + var out []helmv2.ValuesReference + for _, ref := range hr.Spec.ValuesFrom { + if !account.IsAccountValuesFrom(ref) { + out = append(out, ref) + } + } + var mine []cozyv1alpha1.CredentialTemplate + for _, t := range templates { + if t.Annotations[account.ReleaseNameAnnotation] == hr.Name && t.Spec.ValuesPath != "" && t.DeletionTimestamp.IsZero() { + mine = append(mine, t) + } + } + sort.Slice(mine, func(i, j int) bool { return mine[i].Name < mine[j].Name }) + for _, t := range mine { + out = append(out, account.ValuesFrom(t.Name+account.SecretSuffix, t.Spec.ValuesPath, t.Spec.Username)) + } + return out +} + +// SetupWithManager registers the reconciler. +func (r *CredentialValuesReconciler) SetupWithManager(mgr ctrl.Manager) error { + toRelease := handler.EnqueueRequestsFromMapFunc(func(_ context.Context, o client.Object) []reconcile.Request { + release := o.GetAnnotations()[account.ReleaseNameAnnotation] + if release == "" { + return nil + } + return []reconcile.Request{{NamespacedName: types.NamespacedName{Namespace: o.GetNamespace(), Name: release}}} + }) + return ctrl.NewControllerManagedBy(mgr). + Named("credentialvalues"). + Watches(&cozyv1alpha1.CredentialTemplate{}, toRelease). + Complete(r) +} diff --git a/internal/controller/credentialtemplate_controller_test.go b/internal/controller/credentialtemplate_controller_test.go new file mode 100644 index 000000000..b87e9684d --- /dev/null +++ b/internal/controller/credentialtemplate_controller_test.go @@ -0,0 +1,262 @@ +// SPDX-License-Identifier: Apache-2.0 + +package controller + +import ( + "context" + "testing" + "time" + + helmv2 "github.com/fluxcd/helm-controller/api/v2" + corev1 "k8s.io/api/core/v1" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "k8s.io/client-go/tools/record" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/fake" + "sigs.k8s.io/controller-runtime/pkg/reconcile" + + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" + corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" + "github.com/cozystack/cozystack/pkg/credentials/account" +) + +const ( + ctNamespace = "tenant-foo" + ctRelease = "postgres-orders" + ctName = ctRelease + ".web" +) + +type ctFixture struct { + c client.Client + r *CredentialTemplateReconciler + recorder *record.FakeRecorder +} + +func newCTFixture(t *testing.T, seed bool, objs ...client.Object) *ctFixture { + t.Helper() + scheme := runtime.NewScheme() + for _, add := range []func(*runtime.Scheme) error{corev1.AddToScheme, helmv2.AddToScheme, cozyv1alpha1.AddToScheme} { + if err := add(scheme); err != nil { + t.Fatal(err) + } + } + hr := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: ctRelease, Namespace: ctNamespace}} + if seed { + hr.Annotations = map[string]string{account.SeedAnnotation: "true"} + } + c := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(append(objs, hr)...). + WithStatusSubresource(&cozyv1alpha1.CredentialTemplate{}). + Build() + rec := record.NewFakeRecorder(10) + return &ctFixture{ + c: c, + recorder: rec, + r: &CredentialTemplateReconciler{ + Client: c, Reader: c, Scheme: scheme, Recorder: rec, + Now: func() time.Time { return time.Date(2026, 11, 21, 9, 0, 0, 0, time.UTC) }, + }, + } +} + +func ctTemplate(formatName string) *cozyv1alpha1.CredentialTemplate { + return &cozyv1alpha1.CredentialTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: ctName, Namespace: ctNamespace, UID: "tmpl-uid", + Annotations: map[string]string{account.ReleaseNameAnnotation: ctRelease}, + }, + Spec: cozyv1alpha1.CredentialTemplateSpec{ + Username: "web", + Format: cozyv1alpha1.CredentialTemplateFormat{Name: formatName}, + Secret: cozyv1alpha1.CredentialTemplateSecret{Labels: map[string]string{"cnpg.io/reload": "true"}}, + }, + } +} + +func (f *ctFixture) reconcile(t *testing.T) { + t.Helper() + if _, err := f.r.Reconcile(context.Background(), reconcile.Request{NamespacedName: types.NamespacedName{Namespace: ctNamespace, Name: ctName}}); err != nil { + t.Fatal(err) + } +} + +func (f *ctFixture) secret(t *testing.T) *corev1.Secret { + t.Helper() + sec := &corev1.Secret{} + err := f.c.Get(context.Background(), types.NamespacedName{Namespace: ctNamespace, Name: ctName + account.SecretSuffix}, sec) + if apierrors.IsNotFound(err) { + return nil + } + if err != nil { + t.Fatal(err) + } + return sec +} + +func (f *ctFixture) template(t *testing.T) *cozyv1alpha1.CredentialTemplate { + t.Helper() + tmpl := &cozyv1alpha1.CredentialTemplate{} + if err := f.c.Get(context.Background(), types.NamespacedName{Namespace: ctNamespace, Name: ctName}, tmpl); err != nil { + t.Fatal(err) + } + return tmpl +} + +func TestCredentialTemplateSeedsAMarkedRelease(t *testing.T) { + f := newCTFixture(t, true, ctTemplate("scram-sha-256")) + f.reconcile(t) + sec := f.secret(t) + if sec == nil { + t.Fatal("no Secret seeded") + } + if ref := metav1.GetControllerOf(sec); ref == nil || ref.Name != ctName { + t.Errorf("controller = %v, want the template", ref) + } + if string(sec.Data[account.UsernameKey]) != "web" || len(sec.Data[account.PasswordKey]) == 0 { + t.Errorf("data = %v", sec.Data) + } + if sec.Labels["cnpg.io/reload"] != "true" || sec.Labels[account.AccountLabel] != "true" || sec.Annotations[account.FormatAnnotation] != "scram-sha-256" { + t.Errorf("labels %v annotations %v", sec.Labels, sec.Annotations) + } + if rec := account.ReadRecord(sec); rec.Origin != corev1alpha1.CredentialOriginNotIssued || rec.By != CredentialControllerActor { + t.Errorf("record = %+v", rec) + } + st := f.template(t).Status + if st.SecretResourceVersion != sec.ResourceVersion || st.Password.Origin != "NotIssued" || len(st.History) != 1 { + t.Errorf("status = %+v, want the Secret's record mirrored", st) + } +} + +func TestCredentialTemplateLeavesAnUnmarkedReleaseToAMint(t *testing.T) { + f := newCTFixture(t, false, ctTemplate("scram-sha-256")) + f.reconcile(t) + if f.secret(t) != nil { + t.Fatal("a Secret was seeded on a release whose clients may hold the old password") + } + if st := f.template(t).Status; st.SecretResourceVersion != "" || st.Password.Origin != "NotIssued" { + t.Errorf("status = %+v", st) + } +} + +func TestCredentialTemplateReseedsWhenTheFormatChanges(t *testing.T) { + f := newCTFixture(t, true, ctTemplate("scram-sha-256")) + f.reconcile(t) + before := f.secret(t) + + tmpl := f.template(t) + tmpl.Spec.Format.Name = "sha256" + if err := f.c.Update(context.Background(), tmpl); err != nil { + t.Fatal(err) + } + f.reconcile(t) + after := f.secret(t) + if after.Annotations[account.FormatAnnotation] != "sha256" || string(after.Data[account.PasswordKey]) == string(before.Data[account.PasswordKey]) { + t.Errorf("Secret not seeded again in the new format: %v", after.Annotations) + } + if rec := account.ReadRecord(after); rec.Origin != corev1alpha1.CredentialOriginNotIssued || len(rec.History) != 2 { + t.Errorf("record = %+v, want a second Create on top of the first", rec) + } + select { + case ev := <-f.recorder.Events: + if ev == "" { + t.Error("empty event") + } + default: + t.Error("no event recorded for the reset") + } +} + +func TestCredentialTemplateAdoptsAMigratedSecret(t *testing.T) { + migrated := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: ctName + account.SecretSuffix, Namespace: ctNamespace, + Labels: map[string]string{account.AccountLabel: "true"}, + }, + Data: map[string][]byte{account.UsernameKey: []byte("web"), account.PasswordKey: []byte("SCRAM-SHA-256$migrated")}, + } + account.WriteRecord(migrated, account.Record{}.Apply(corev1alpha1.CredentialOperationMigrate, metav1.Now(), "migration")) + f := newCTFixture(t, true, ctTemplate("scram-sha-256"), migrated) + f.reconcile(t) + sec := f.secret(t) + if ref := metav1.GetControllerOf(sec); ref == nil || ref.Name != ctName { + t.Fatalf("controller = %v, want the template", ref) + } + if string(sec.Data[account.PasswordKey]) != "SCRAM-SHA-256$migrated" { + t.Error("the migrated verifier was replaced") + } + if sec.Labels["cnpg.io/reload"] != "true" || sec.Annotations[account.FormatAnnotation] != "scram-sha-256" { + t.Errorf("labels %v annotations %v", sec.Labels, sec.Annotations) + } + if st := f.template(t).Status; st.Password.Origin != "Migrated" { + t.Errorf("origin = %q, want Migrated", st.Password.Origin) + } +} + +func TestCredentialTemplateLeavesAForeignSecretAlone(t *testing.T) { + foreign := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{Name: ctName + account.SecretSuffix, Namespace: ctNamespace}, + Data: map[string][]byte{"key": []byte("value")}, + } + f := newCTFixture(t, true, ctTemplate("scram-sha-256"), foreign) + f.reconcile(t) + sec := f.secret(t) + if metav1.GetControllerOf(sec) != nil || string(sec.Data["key"]) != "value" { + t.Error("the controller touched a Secret it did not create") + } + if !meta.IsStatusConditionTrue(f.template(t).Status.Conditions, cozyv1alpha1.CredentialTemplateSecretConflict) { + t.Error("no SecretConflict condition") + } +} + +func TestCredentialTemplateReportsAnUnknownFormat(t *testing.T) { + f := newCTFixture(t, true, ctTemplate("apr1")) + f.reconcile(t) + if f.secret(t) != nil { + t.Error("a Secret was seeded in a format the controller does not have") + } + if !meta.IsStatusConditionTrue(f.template(t).Status.Conditions, cozyv1alpha1.CredentialTemplateUnknownFormat) { + t.Error("no UnknownFormat condition") + } +} + +func TestCredentialTemplateMirrorsAMint(t *testing.T) { + f := newCTFixture(t, true, ctTemplate("scram-sha-256")) + f.reconcile(t) + sec := f.secret(t) + account.WriteRecord(sec, account.ReadRecord(sec).Apply(corev1alpha1.CredentialOperationMint, metav1.Now(), "jane")) + if err := f.c.Update(context.Background(), sec); err != nil { + t.Fatal(err) + } + f.reconcile(t) + st := f.template(t).Status + if st.Password.Origin != "Minted" || st.Password.By != "jane" || st.SecretResourceVersion != f.secret(t).ResourceVersion { + t.Errorf("status = %+v, want the mint mirrored", st) + } +} + +func TestDesiredValuesFrom(t *testing.T) { + hr := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "clickhouse-logs"}} + hr.Spec.ValuesFrom = []helmv2.ValuesReference{ + {Kind: "Secret", Name: "cozystack-values"}, + account.ValuesFrom("clickhouse-logs.gone.account", "_accounts", "gone"), + } + mk := func(name, user, path, release string) cozyv1alpha1.CredentialTemplate { + return cozyv1alpha1.CredentialTemplate{ + ObjectMeta: metav1.ObjectMeta{Name: name, Annotations: map[string]string{account.ReleaseNameAnnotation: release}}, + Spec: cozyv1alpha1.CredentialTemplateSpec{Username: user, ValuesPath: path}, + } + } + got := DesiredValuesFrom(hr, []cozyv1alpha1.CredentialTemplate{ + mk("clickhouse-logs.web", "web", "_accounts", "clickhouse-logs"), + mk("clickhouse-logs.api", "api", "_accounts", "clickhouse-logs"), + mk("clickhouse-logs.nopath", "nopath", "", "clickhouse-logs"), + mk("other.web", "web", "_accounts", "other"), + }) + if len(got) != 3 || got[0].Name != "cozystack-values" || got[1].Name != "clickhouse-logs.api.account" || got[2].Name != "clickhouse-logs.web.account" { + t.Errorf("valuesFrom = %+v, want the platform values, then api and web, and no entry for a removed account", got) + } +} diff --git a/packages/system/application-definition-crd/definition/cozystack.io_applicationdefinitions.yaml b/packages/system/application-definition-crd/definition/cozystack.io_applicationdefinitions.yaml index 62108cc19..9229e7601 100644 --- a/packages/system/application-definition-crd/definition/cozystack.io_applicationdefinitions.yaml +++ b/packages/system/application-definition-crd/definition/cozystack.io_applicationdefinitions.yaml @@ -62,270 +62,6 @@ spec: - plural - singular type: object - credentials: - description: Credentials converts the kind to platform-minted credentials. - properties: - accounts: - description: |- - Accounts are the sources the kind's accounts come from. A kind may list - several, for example a values map of tenant users and a static platform - account. - items: - description: |- - CredentialAccountSource is one source of a kind's accounts. Which fields - apply depends on Kind. - properties: - expression: - description: Expression is the CEL expression of an Expression - source. - type: string - kind: - description: CredentialAccountSourceKind says how a source - yields account names. - enum: - - ValuesMap - - ValuesList - - Static - - Expression - - Webhook - type: string - name: - description: Name is the account of a Static source. - type: string - nameField: - description: |- - NameField is the field of each object a ValuesList source takes the - account name from. - type: string - owner: - description: Owner defaults to tenant. - enum: - - tenant - - platform - type: string - path: - description: |- - Path is the values key a ValuesMap or ValuesList source reads, as a - dotted path from the top of the application spec. - type: string - webhook: - description: Webhook is the endpoint of a Webhook source. - properties: - clientConfig: - description: |- - WebhookClientConfig contains the information to make a TLS - connection with the webhook - properties: - caBundle: - description: |- - caBundle is a PEM encoded CA bundle which will be used to validate the webhook's server certificate. - If unspecified, system trust roots on the apiserver are used. - format: byte - type: string - service: - description: |- - service is a reference to the service for this webhook. Either - `service` or `url` must be specified. - - If the webhook is running within the cluster, then you should use `service`. - properties: - name: - description: |- - name is the name of the service. - Required - type: string - namespace: - description: |- - namespace is the namespace of the service. - Required - type: string - path: - description: |- - path is an optional URL path which will be sent in any request to - this service. - type: string - port: - description: |- - port is the port on the service that hosts the webhook. - Default to 443 for backward compatibility. - `port` should be a valid port number (1-65535, inclusive). - format: int32 - type: integer - required: - - name - - namespace - type: object - url: - description: |- - url gives the location of the webhook, in standard URL form - (`scheme://host:port/path`). Exactly one of `url` or `service` - must be specified. - - The `host` should not refer to a service running in the cluster; use - the `service` field instead. The host might be resolved via external - DNS in some apiservers (e.g., `kube-apiserver` cannot resolve - in-cluster DNS as that would be a layering violation). `host` may - also be an IP address. - - Please note that using `localhost` or `127.0.0.1` as a `host` is - risky unless you take great care to run this webhook on all hosts - which run an apiserver which might need to make calls to this - webhook. Such installs are likely to be non-portable, i.e., not easy - to turn up in a new cluster. - - The scheme must be "https"; the URL must begin with "https://". - - A path is optional, and if present may be any string permissible in - a URL. You may use the path to pass an arbitrary string to the - webhook, for example, a cluster identifier. - - Attempting to use a user or basic auth e.g. "user:password@" is not - allowed. Fragments ("#...") and query parameters ("?...") are not - allowed, either. - type: string - type: object - timeoutSeconds: - format: int32 - maximum: 30 - minimum: 1 - type: integer - required: - - clientConfig - type: object - when: - description: |- - When is a values key, as a dotted path, that switches a Static account - on when it holds true; absent, the account is always on. - type: string - required: - - kind - type: object - minItems: 1 - type: array - format: - description: |- - Format is how a password is stored in the account Secret: the verifier the - engine checks a login against, or the plaintext for an engine that needs it. - properties: - kind: - description: Kind defaults to BuiltIn. - enum: - - BuiltIn - - Webhook - type: string - name: - description: |- - Name of the format. The built-in names are scram-sha-256, mysql-native, - sha256 and plaintext; a Webhook format carries any name. - minLength: 1 - type: string - webhook: - description: Webhook is the endpoint of a Webhook format. - properties: - clientConfig: - description: |- - WebhookClientConfig contains the information to make a TLS - connection with the webhook - properties: - caBundle: - description: |- - caBundle is a PEM encoded CA bundle which will be used to validate the webhook's server certificate. - If unspecified, system trust roots on the apiserver are used. - format: byte - type: string - service: - description: |- - service is a reference to the service for this webhook. Either - `service` or `url` must be specified. - - If the webhook is running within the cluster, then you should use `service`. - properties: - name: - description: |- - name is the name of the service. - Required - type: string - namespace: - description: |- - namespace is the namespace of the service. - Required - type: string - path: - description: |- - path is an optional URL path which will be sent in any request to - this service. - type: string - port: - description: |- - port is the port on the service that hosts the webhook. - Default to 443 for backward compatibility. - `port` should be a valid port number (1-65535, inclusive). - format: int32 - type: integer - required: - - name - - namespace - type: object - url: - description: |- - url gives the location of the webhook, in standard URL form - (`scheme://host:port/path`). Exactly one of `url` or `service` - must be specified. - - The `host` should not refer to a service running in the cluster; use - the `service` field instead. The host might be resolved via external - DNS in some apiservers (e.g., `kube-apiserver` cannot resolve - in-cluster DNS as that would be a layering violation). `host` may - also be an IP address. - - Please note that using `localhost` or `127.0.0.1` as a `host` is - risky unless you take great care to run this webhook on all hosts - which run an apiserver which might need to make calls to this - webhook. Such installs are likely to be non-portable, i.e., not easy - to turn up in a new cluster. - - The scheme must be "https"; the URL must begin with "https://". - - A path is optional, and if present may be any string permissible in - a URL. You may use the path to pass an arbitrary string to the - webhook, for example, a cluster identifier. - - Attempting to use a user or basic auth e.g. "user:password@" is not - allowed. Fragments ("#...") and query parameters ("?...") are not - allowed, either. - type: string - type: object - timeoutSeconds: - format: int32 - maximum: 30 - minimum: 1 - type: integer - required: - - clientConfig - type: object - required: - - name - type: object - secretLabels: - additionalProperties: - type: string - description: |- - SecretLabels are added to every account Secret of the kind, for an operator - that notices a changed Secret only when it carries a label. - type: object - valuesPath: - description: |- - ValuesPath makes the HelmRelease pull each tenant account's stored value - into this values key, under u, for a chart - whose operator cannot read a Secret. It starts with "_", which the - Application API hides on read and refuses on write, so the value never - appears in the application. - pattern: ^_[A-Za-z0-9_]*$ - type: string - required: - - accounts - - format - type: object dashboard: description: Dashboard configuration for this resource properties: diff --git a/packages/system/cozystack-api/templates/rbac.yaml b/packages/system/cozystack-api/templates/rbac.yaml index 1cc5d8e0e..70f1bec97 100644 --- a/packages/system/cozystack-api/templates/rbac.yaml +++ b/packages/system/cozystack-api/templates/rbac.yaml @@ -90,10 +90,3 @@ rules: - apiGroups: ["cilium.io"] resources: ["ciliumnetworkpolicies"] verbs: ["get", "watch", "list", "create", "update", "patch", "delete"] -# The per-application credential lock (a Lease in cozy-system) serializes a -# create's or update's account-Secret seeding and pruning across the API -# server's replicas, so two concurrent writes cannot interleave into deleting a -# Secret the other reused. -- apiGroups: ["coordination.k8s.io"] - resources: ["leases"] - verbs: ["get", "create", "update", "delete"] diff --git a/packages/system/cozystack-api/tests/rbac_test.yaml b/packages/system/cozystack-api/tests/rbac_test.yaml index af24136b5..03b5e9d26 100644 --- a/packages/system/cozystack-api/tests/rbac_test.yaml +++ b/packages/system/cozystack-api/tests/rbac_test.yaml @@ -15,7 +15,7 @@ release: # the API group string exactly. # # The last two tests bound the role from two directions, and neither covers the -# other. The count bound pins the list at exactly twenty-one: templates/rbac.yaml +# other. The count bound pins the list at exactly twenty-two: templates/rbac.yaml # carries no template directives, so anything appended makes rules[22] exist and # anything dropped takes rules[21] away, whatever the rule is called. It says # nothing about a grant swapped in for an existing one, because the count does @@ -94,18 +94,11 @@ tests: resources: ["events"] verbs: ["create"] - - it: ships exactly the twenty-three rules this role is pinned to + - it: ships exactly the twenty-two rules this role is pinned to asserts: # Both directions. A dropped grant is the failure this suite is about: # the apiserver returns forbidden and List silently drops the dropdown. - exists: - path: rules[22] + path: rules[21] - notExists: - path: rules[23] - # The last rule is the credential lock's Lease access. - - equal: path: rules[22] - value: - apiGroups: ["coordination.k8s.io"] - resources: ["leases"] - verbs: ["get", "create", "update", "delete"] diff --git a/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml b/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml new file mode 100644 index 000000000..b56c24c73 --- /dev/null +++ b/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml @@ -0,0 +1,221 @@ +--- +apiVersion: apiextensions.k8s.io/v1 +kind: CustomResourceDefinition +metadata: + annotations: + controller-gen.kubebuilder.io/version: v0.16.4 + name: credentialtemplates.cozystack.io +spec: + group: cozystack.io + names: + kind: CredentialTemplate + listKind: CredentialTemplateList + plural: credentialtemplates + singular: credentialtemplate + scope: Namespaced + versions: + - additionalPrinterColumns: + - jsonPath: .spec.username + name: User + type: string + - jsonPath: .spec.format.name + name: Format + type: string + - jsonPath: .status.password.origin + name: Origin + type: string + - jsonPath: .metadata.creationTimestamp + name: Age + type: date + name: v1alpha1 + schema: + openAPIV3Schema: + description: |- + CredentialTemplate is one account of an application, rendered by the kind's + chart. A controller keeps its account Secret, named .account, and the + Cozystack API mints and revokes through it. See + design-proposals/user-secrets-api in cozystack/community. + properties: + apiVersion: + description: |- + APIVersion defines the versioned schema of this representation of an object. + Servers should convert recognized schemas to the latest internal value, and + may reject unrecognized values. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources + type: string + kind: + description: |- + Kind is a string value representing the REST resource this object represents. + Servers may infer this from the endpoint the client submits requests to. + Cannot be updated. + In CamelCase. + More info: https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds + type: string + metadata: + type: object + spec: + description: |- + CredentialTemplateSpec describes one account of an application and the + Secret that stores its password. A kind's chart renders one per account; a + tenant cannot write it. + properties: + format: + description: Format is how a password is stored in the account Secret. + properties: + name: + description: |- + Name is a built-in format: scram-sha-256, mysql-native, sha256 or + plaintext. + minLength: 1 + type: string + required: + - name + type: object + secret: + description: |- + Secret describes the account Secret beyond its fixed username and + password keys. + properties: + labels: + additionalProperties: + type: string + description: |- + Labels are put on the account Secret, for an operator that notices a + changed Secret only when it carries a label. + type: object + x-kubernetes-validations: + - message: the credentials.cozystack.io/account label is reserved + rule: '!(''credentials.cozystack.io/account'' in self)' + type: object + username: + description: Username is the account name as the engine knows it. + minLength: 1 + type: string + x-kubernetes-validations: + - message: username is immutable + rule: self == oldSelf + valuesPath: + description: |- + ValuesPath, for an engine whose operator reads no Secret, asks for the + stored value to be pulled into the release's values under + .u. It starts with "_", which + the Application API hides on read and refuses on write. + pattern: ^_[A-Za-z0-9_]*$ + type: string + required: + - format + - username + type: object + status: + description: |- + CredentialTemplateStatus mirrors the record the account Secret keeps, so the + Credential view reads templates only. + properties: + conditions: + items: + description: Condition contains details for one aspect of the current + state of this API Resource. + properties: + lastTransitionTime: + description: |- + lastTransitionTime is the last time the condition transitioned from one status to another. + This should be when the underlying condition changed. If that is not known, then using the time when the API field changed is acceptable. + format: date-time + type: string + message: + description: |- + message is a human readable message indicating details about the transition. + This may be an empty string. + maxLength: 32768 + type: string + observedGeneration: + description: |- + observedGeneration represents the .metadata.generation that the condition was set based upon. + For instance, if .metadata.generation is currently 12, but the .status.conditions[x].observedGeneration is 9, the condition is out of date + with respect to the current state of the instance. + format: int64 + minimum: 0 + type: integer + reason: + description: |- + reason contains a programmatic identifier indicating the reason for the condition's last transition. + Producers of specific condition types may define expected values and meanings for this field, + and whether the values are considered a guaranteed API. + The value should be a CamelCase string. + This field may not be empty. + maxLength: 1024 + minLength: 1 + pattern: ^[A-Za-z]([A-Za-z0-9_,:]*[A-Za-z0-9_])?$ + type: string + status: + description: status of the condition, one of True, False, Unknown. + enum: + - "True" + - "False" + - Unknown + type: string + type: + description: type of condition in CamelCase or in foo.example.com/CamelCase. + maxLength: 316 + pattern: ^([a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*/)?(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])$ + type: string + required: + - lastTransitionTime + - message + - reason + - status + - type + type: object + type: array + x-kubernetes-list-map-keys: + - type + x-kubernetes-list-type: map + history: + description: History holds the last ten operations, newest first. + items: + description: CredentialTemplateHistoryEntry is one recorded operation + on the account. + properties: + at: + format: date-time + type: string + by: + type: string + operation: + description: Operation is Create, Migrate, Mint or Revoke. + type: string + required: + - at + - by + - operation + type: object + type: array + x-kubernetes-list-type: atomic + observedGeneration: + description: ObservedGeneration is the generation the controller last + acted on. + format: int64 + type: integer + password: + description: Password says where the live password came from. + properties: + at: + format: date-time + type: string + by: + type: string + origin: + description: Origin is NotIssued, Migrated, Minted or Revoked. + type: string + type: object + secretResourceVersion: + description: |- + SecretResourceVersion is the resourceVersion of the account Secret this + status mirrors; empty while the account has no Secret. + type: string + type: object + type: object + served: true + storage: true + subresources: + status: {} diff --git a/pkg/apiserver/apiserver.go b/pkg/apiserver/apiserver.go index 92b081f9c..7986d6a99 100644 --- a/pkg/apiserver/apiserver.go +++ b/pkg/apiserver/apiserver.go @@ -23,7 +23,6 @@ import ( cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" helmv2 "github.com/fluxcd/helm-controller/api/v2" - coordinationv1 "k8s.io/api/coordination/v1" corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -94,10 +93,6 @@ func init() { if err := rbacv1.AddToScheme(mgrScheme); err != nil { panic(fmt.Errorf("failed to add RBAC types to scheme: %w", err)) } - // Register Lease types for the per-application credential lock. - if err := coordinationv1.AddToScheme(mgrScheme); err != nil { - panic(fmt.Errorf("failed to add coordination types to scheme: %w", err)) - } // Register Cozystack types for WorkloadMonitor queries. if err := cozyv1alpha1.AddToScheme(mgrScheme); err != nil { @@ -219,19 +214,6 @@ func (c completedConfig) New() (*CozyServer, error) { if err != nil { return nil, fmt.Errorf("failed to build dynamic client: %w", err) } - // --- dynamically-configured, per-tenant resources --- - appsV1alpha1Storage := map[string]rest.Storage{} - // credentialKinds carries every converted application kind, by kind name, - // for the credentials view that is served from the core group below. - credentialKinds := map[string]credentialstorage.Kind{} - for _, resConfig := range c.ResourceConfig.Resources { - storage := applicationstorage.NewREST(cli, watchCli, &resConfig) - appsV1alpha1Storage[resConfig.Application.Plural] = cozyregistry.RESTInPeace(storage) - if block := storage.Credentials(); block != nil { - credentialKinds[resConfig.Application.Kind] = credentialstorage.Kind{Block: block, Accounts: storage.AccountsOf} - } - } - // --- static, cluster-scoped resource for core group --- coreV1alpha1Storage := map[string]rest.Storage{} coreV1alpha1Storage["tenantnamespaces"] = cozyregistry.RESTInPeace( @@ -252,7 +234,7 @@ func (c completedConfig) New() (*CozyServer, error) { coreV1alpha1Storage["taps"] = cozyregistry.RESTInPeace( tapstorage.NewREST(dyn), ) - credentials := credentialstorage.NewREST(cli, watchCli, credentialKinds) + credentials := credentialstorage.NewREST(cli, watchCli) coreV1alpha1Storage["credentials"] = cozyregistry.RESTInPeace(credentials) coreV1alpha1Storage["credentials/mint"] = credentialstorage.NewMintREST(credentials) coreV1alpha1Storage["credentials/revoke"] = credentialstorage.NewRevokeREST(credentials) @@ -275,6 +257,12 @@ func (c completedConfig) New() (*CozyServer, error) { return nil, err } + // --- dynamically-configured, per-tenant resources --- + appsV1alpha1Storage := map[string]rest.Storage{} + for _, resConfig := range c.ResourceConfig.Resources { + storage := applicationstorage.NewREST(cli, watchCli, &resConfig) + appsV1alpha1Storage[resConfig.Application.Plural] = cozyregistry.RESTInPeace(storage) + } if err := InstallAppsAPIGroup(s.GenericAPIServer, appsV1alpha1Storage); err != nil { return nil, err } diff --git a/pkg/cmd/server/start.go b/pkg/cmd/server/start.go index ad921c1a2..2a4d34e05 100644 --- a/pkg/cmd/server/start.go +++ b/pkg/cmd/server/start.go @@ -34,7 +34,6 @@ import ( sdnv1alpha1 "github.com/cozystack/cozystack/pkg/apis/sdn/v1alpha1" "github.com/cozystack/cozystack/pkg/apiserver" "github.com/cozystack/cozystack/pkg/config" - "github.com/cozystack/cozystack/pkg/credentials/account" "github.com/spf13/cobra" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/runtime" @@ -289,12 +288,6 @@ func resourceConfigFrom(defs []v1alpha1.ApplicationDefinition, hrFlags helmRelea } resourceConfig := &config.ResourceConfig{} for _, crd := range owned { - // A block the API cannot honour leaves the kind unserved; see - // account.Validate. - if err := account.Validate(crd.Spec.Credentials); err != nil { - fmt.Printf("Skipping ApplicationDefinition %q: credentials block cannot be served: %v\n", crd.Name, err) - continue - } resource, err := buildResourceFromCRD(crd, hrFlags) if err != nil { return nil, err @@ -415,8 +408,7 @@ func buildResourceFromCRD(crd v1alpha1.ApplicationDefinition, hrFlags helmReleas ShortNames: []string{}, // TODO: implement shortnames OpenAPISchema: crd.Spec.Application.OpenAPISchema, }, - Release: release, - Credentials: crd.Spec.Credentials, + Release: release, }, nil } diff --git a/pkg/cmd/server/start_owner_test.go b/pkg/cmd/server/start_owner_test.go index 9cdddbf07..c1f62bd6b 100644 --- a/pkg/cmd/server/start_owner_test.go +++ b/pkg/cmd/server/start_owner_test.go @@ -62,45 +62,3 @@ func TestResourceConfigFromRegistersOwnerOnly(t *testing.T) { t.Fatalf("resources = %+v, want the owner's redis-artifact only", rc.Resources) } } - -// TestResourceConfigFromSkipsUnservableCredentials pins that a definition -// whose credentials block the API cannot honour leaves its kind unserved, -// while a block it can honour reaches the resource config. -func TestResourceConfigFromSkipsUnservableCredentials(t *testing.T) { - mk := func(name, kind string, block *v1alpha1.ApplicationDefinitionCredentials) v1alpha1.ApplicationDefinition { - d := v1alpha1.ApplicationDefinition{ObjectMeta: metav1.ObjectMeta{Name: name}} - d.Spec.Application.Kind = kind - d.Spec.Release.ChartRef = &helmv2.CrossNamespaceSourceReference{Kind: "ExternalArtifact", Name: name, Namespace: "cozy-system"} - d.Spec.Credentials = block - return d - } - served := &v1alpha1.ApplicationDefinitionCredentials{ - Format: v1alpha1.CredentialFormat{Name: "scram-sha-256"}, - Accounts: []v1alpha1.CredentialAccountSource{{Kind: v1alpha1.CredentialAccountsValuesMap, Path: "users"}}, - } - unbuilt := &v1alpha1.ApplicationDefinitionCredentials{ - Format: v1alpha1.CredentialFormat{Name: "apr1", Kind: v1alpha1.CredentialFormatWebhook}, - Accounts: []v1alpha1.CredentialAccountSource{{Kind: v1alpha1.CredentialAccountsValuesMap, Path: "users"}}, - } - rc, err := resourceConfigFrom([]v1alpha1.ApplicationDefinition{ - mk("postgres", "Postgres", served), - mk("foo", "Foo", unbuilt), - mk("redis", "Redis", nil), - }, helmReleaseFlagValues{}) - if err != nil { - t.Fatal(err) - } - kinds := map[string]*v1alpha1.ApplicationDefinitionCredentials{} - for _, r := range rc.Resources { - kinds[r.Application.Kind] = r.Credentials - } - if len(kinds) != 2 || kinds["Postgres"] == nil || kinds["Postgres"].Format.Name != "scram-sha-256" { - t.Fatalf("resources = %v, want Postgres with its block and Redis without", kinds) - } - if _, ok := kinds["Foo"]; ok { - t.Fatal("a kind with an unbuilt format must not be served") - } - if _, ok := kinds["Redis"]; !ok || kinds["Redis"] != nil { - t.Fatalf("Redis = %v, want served with no block", kinds["Redis"]) - } -} diff --git a/pkg/config/config.go b/pkg/config/config.go index 3f004177b..0223fe407 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -24,8 +24,6 @@ import ( helmv2 "github.com/fluxcd/helm-controller/api/v2" "github.com/fluxcd/pkg/apis/kustomize" - - "github.com/cozystack/cozystack/api/v1alpha1" ) // ReleaseAnnotationPrefix is the prefix shared by every ApplicationDefinition @@ -197,9 +195,6 @@ type ResourceConfig struct { type Resource struct { Application ApplicationConfig `yaml:"application"` Release ReleaseConfig `yaml:"release"` - // Credentials is the definition's spec.credentials; nil for a kind that is - // not converted. - Credentials *v1alpha1.ApplicationDefinitionCredentials `yaml:"credentials,omitempty"` } // ApplicationConfig contains the application settings. diff --git a/pkg/credentials/account/account.go b/pkg/credentials/account/account.go index 9857cafb3..1b2078fd5 100644 --- a/pkg/credentials/account/account.go +++ b/pkg/credentials/account/account.go @@ -1,26 +1,25 @@ // SPDX-License-Identifier: Apache-2.0 -// Package account is what the Cozystack API and the migration tooling share -// about account Secrets: their names, the labels and annotations that carry -// the record, and how a kind's credentials block yields the accounts of one -// application. It writes nothing itself. +// 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" - "errors" "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" - 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" - "github.com/cozystack/cozystack/pkg/credentials/format" ) const ( @@ -40,7 +39,19 @@ const ( // HistoryLimit bounds the history kept on the Secret; the audit log keeps // every operation. HistoryLimit = 10 - // SecretSuffix follows the mapped account name in the Secret's name. + // 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. @@ -48,9 +59,6 @@ const ( PasswordKey = "password" ) -// ErrNotBuilt marks a declared format or source kind the API cannot serve yet. -var ErrNotBuilt = errors.New("declared but not built") - var nonNameChars = regexp.MustCompile(`[^a-z0-9-]`) // Account is one tenant account of an application. @@ -92,191 +100,6 @@ func SplitCredentialName(name string) (release, mapped string, ok bool) { return release, mapped, true } -// Validate reports whether the API can serve a kind with this block: a format -// or a source kind that is declared but not built, an unknown built-in format, -// or a source missing the field its kind needs, refuses the whole kind. -func Validate(c *cozyv1alpha1.ApplicationDefinitionCredentials) error { - if c == nil { - return nil - } - switch c.Format.Kind { - case "", cozyv1alpha1.CredentialFormatBuiltIn: - if !format.Known(c.Format.Name) { - return fmt.Errorf("format %q is not a built-in format", c.Format.Name) - } - case cozyv1alpha1.CredentialFormatWebhook: - return fmt.Errorf("format kind Webhook: %w", ErrNotBuilt) - default: - return fmt.Errorf("format kind %q is unknown", c.Format.Kind) - } - 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") - } - for i, s := range c.Accounts { - switch s.Kind { - case cozyv1alpha1.CredentialAccountsValuesMap: - if s.Path == "" { - return fmt.Errorf("accounts[%d]: a ValuesMap source needs a path", i) - } - case cozyv1alpha1.CredentialAccountsStatic: - if s.Name == "" { - return fmt.Errorf("accounts[%d]: a Static source needs a name", i) - } - case cozyv1alpha1.CredentialAccountsValuesList, cozyv1alpha1.CredentialAccountsExpression, cozyv1alpha1.CredentialAccountsWebhook: - return fmt.Errorf("accounts[%d]: source kind %s: %w", i, s.Kind, ErrNotBuilt) - default: - return fmt.Errorf("accounts[%d]: source kind %q is unknown", i, s.Kind) - } - switch s.Owner { - case "", cozyv1alpha1.CredentialAccountOwnerTenant, cozyv1alpha1.CredentialAccountOwnerPlatform: - default: - return fmt.Errorf("accounts[%d]: owner %q is unknown", i, s.Owner) - } - } - return nil -} - -// Resolve returns the tenant accounts of an application from its defaulted -// spec values, in declaration order. Platform accounts are left out, and a -// ValuesMap key that names one is skipped. Two users that map to one name, -// or a user whose name maps to an invalid one, are an error naming them. -func Resolve(c *cozyv1alpha1.ApplicationDefinitionCredentials, values map[string]any) ([]Account, error) { - if c == nil { - return nil, nil - } - platform := map[string]bool{} - for _, s := range c.Accounts { - if s.Kind == cozyv1alpha1.CredentialAccountsStatic && s.Owner == cozyv1alpha1.CredentialAccountOwnerPlatform { - platform[s.Name] = true - } - } - var accounts []Account - byMapped := map[string]string{} - add := func(username string) error { - mapped, err := MapName(username) - if err != nil { - return err - } - if other, dup := byMapped[mapped]; dup { - if other == username { - return nil - } - return fmt.Errorf("users %q and %q both map to the name %q; rename one of them", other, username, mapped) - } - byMapped[mapped] = username - accounts = append(accounts, Account{Username: username, Mapped: mapped}) - return nil - } - for _, s := range c.Accounts { - switch s.Kind { - case cozyv1alpha1.CredentialAccountsValuesMap: - m, _ := Lookup(values, s.Path).(map[string]any) - for _, name := range sortedKeys(m) { - if platform[name] { - continue - } - if err := add(name); err != nil { - return nil, err - } - } - case cozyv1alpha1.CredentialAccountsStatic: - if s.Owner == cozyv1alpha1.CredentialAccountOwnerPlatform { - continue - } - if s.When != "" { - on, _ := Lookup(values, s.When).(bool) - if !on { - continue - } - } - if err := add(s.Name); err != nil { - return nil, err - } - } - } - return accounts, nil -} - -// PlatformAccounts returns the names of the kind's platform accounts. -func PlatformAccounts(c *cozyv1alpha1.ApplicationDefinitionCredentials) []string { - if c == nil { - return nil - } - var names []string - for _, s := range c.Accounts { - if s.Kind == cozyv1alpha1.CredentialAccountsStatic && s.Owner == cozyv1alpha1.CredentialAccountOwnerPlatform { - names = append(names, s.Name) - } - } - return names -} - -// ValuesMapPaths returns the paths of the kind's ValuesMap sources. -func ValuesMapPaths(c *cozyv1alpha1.ApplicationDefinitionCredentials) []string { - if c == nil { - return nil - } - var paths []string - for _, s := range c.Accounts { - if s.Kind == cozyv1alpha1.CredentialAccountsValuesMap { - paths = append(paths, s.Path) - } - } - return paths -} - -// Lookup walks a dotted path through nested maps and returns what it finds, -// or nil where the path leaves the maps. -func Lookup(values map[string]any, path string) any { - var cur any = values - for _, key := range strings.Split(path, ".") { - m, ok := cur.(map[string]any) - if !ok { - return nil - } - cur, ok = m[key] - if !ok { - return nil - } - } - return cur -} - -func sortedKeys(m map[string]any) []string { - keys := make([]string, 0, len(m)) - for k := range m { - keys = append(keys, k) - } - // Declaration order inside a map is the sorted order: it is what a chart's - // range sees and what makes the seeding deterministic. - sortStrings(keys) - return keys -} - -func sortStrings(s []string) { - for i := 1; i < len(s); i++ { - for j := i; j > 0 && s[j] < s[j-1]; j-- { - s[j], s[j-1] = s[j-1], s[j] - } - } -} - // Record is the per-account record an account Secret carries in its // annotations and the Credential view shows. type Record struct { @@ -355,24 +178,44 @@ func parseTime(s string) (metav1.Time, error) { return t, err } -// Labels are the labels every account Secret of an application carries: the -// account marker, the application identity the view lists by, and the labels -// the kind's block names for its operator. -func Labels(c *cozyv1alpha1.ApplicationDefinitionCredentials, group, kind, name string) map[string]string { - labels := map[string]string{ - AccountLabel: "true", - appsv1alpha1.ApplicationGroupLabel: group, - appsv1alpha1.ApplicationKindLabel: kind, - appsv1alpha1.ApplicationNameLabel: name, - } - if c != nil { - for k, v := range c.SecretLabels { - labels[k] = v - } +// 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 one the API writes for an // account. func IsAccountSecret(sec *corev1.Secret) bool { diff --git a/pkg/credentials/account/account_test.go b/pkg/credentials/account/account_test.go index 9bd310924..a3198f076 100644 --- a/pkg/credentials/account/account_test.go +++ b/pkg/credentials/account/account_test.go @@ -3,16 +3,14 @@ package account import ( - "errors" "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" - 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" ) @@ -64,122 +62,6 @@ func TestNames(t *testing.T) { } } -func block(sources ...cozyv1alpha1.CredentialAccountSource) *cozyv1alpha1.ApplicationDefinitionCredentials { - return &cozyv1alpha1.ApplicationDefinitionCredentials{ - Format: cozyv1alpha1.CredentialFormat{Name: "scram-sha-256"}, - Accounts: sources, - } -} - -func valuesMap(path string) cozyv1alpha1.CredentialAccountSource { - return cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsValuesMap, Path: path} -} - -func static(name, when string, owner cozyv1alpha1.CredentialAccountOwner) cozyv1alpha1.CredentialAccountSource { - 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) - } - 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")}}, - block(cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsValuesList, Path: "accounts", NameField: "name"}), - block(cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsExpression, Expression: "spec.users"}), - block(cozyv1alpha1.CredentialAccountSource{Kind: cozyv1alpha1.CredentialAccountsWebhook}), - } - for i, b := range notBuilt { - err := Validate(b) - if !errors.Is(err, ErrNotBuilt) { - t.Errorf("notBuilt[%d]: got %v, want ErrNotBuilt", i, err) - } - } - - invalid := []*cozyv1alpha1.ApplicationDefinitionCredentials{ - {Format: cozyv1alpha1.CredentialFormat{Name: "apr1"}, Accounts: []cozyv1alpha1.CredentialAccountSource{valuesMap("users")}}, - {Format: cozyv1alpha1.CredentialFormat{Name: "sha256"}, ValuesPath: "accounts", Accounts: []cozyv1alpha1.CredentialAccountSource{valuesMap("users")}}, - {Format: cozyv1alpha1.CredentialFormat{Name: "sha256"}}, - 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) { - t.Errorf("invalid[%d]: got %v, want a validation error", i, err) - } - } -} - -func TestResolve(t *testing.T) { - values := map[string]any{ - "users": map[string]any{ - "web": map[string]any{}, - "Bob.Smith": map[string]any{}, - "backup": map[string]any{}, - }, - "authEnabled": true, - "nested": map[string]any{"accounts": map[string]any{"deep": map[string]any{}}}, - } - - b := block(valuesMap("users"), static("backup", "", cozyv1alpha1.CredentialAccountOwnerPlatform), static("default", "authEnabled", ""), static("off", "missing", "")) - got, err := Resolve(b, values) - if err != nil { - t.Fatalf("resolve: %v", err) - } - want := []Account{{"Bob.Smith", "bob-smith"}, {"web", "web"}, {"default", "default"}} - if len(got) != len(want) { - t.Fatalf("got %v, want %v", got, want) - } - for i := range want { - if got[i] != want[i] { - t.Errorf("accounts[%d] = %v, want %v", i, got[i], want[i]) - } - } - - if got, err := Resolve(block(valuesMap("nested.accounts")), values); err != nil || len(got) != 1 || got[0].Username != "deep" { - t.Errorf("dotted path: %v, %v", got, err) - } - - if got, err := Resolve(block(valuesMap("absent")), values); err != nil || len(got) != 0 { - t.Errorf("absent path: %v, %v", got, err) - } - - collide := map[string]any{"users": map[string]any{"a_b": map[string]any{}, "a-b": map[string]any{}}} - if _, err := Resolve(block(valuesMap("users")), collide); err == nil || !strings.Contains(err.Error(), "a-b") { - t.Errorf("collision: %v", err) - } - - bad := map[string]any{"users": map[string]any{"-x": map[string]any{}}} - if _, err := Resolve(block(valuesMap("users")), bad); err == nil { - t.Error("invalid name accepted") - } - - if names := PlatformAccounts(b); len(names) != 1 || names[0] != "backup" { - t.Errorf("PlatformAccounts = %v", names) - } - if paths := ValuesMapPaths(b); len(paths) != 1 || paths[0] != "users" { - t.Errorf("ValuesMapPaths = %v", paths) - } -} - func TestRecordRoundTrip(t *testing.T) { sec := &corev1.Secret{} if r := ReadRecord(sec); r.Origin != "" || len(r.History) != 0 { @@ -210,18 +92,9 @@ func TestRecordRoundTrip(t *testing.T) { } func TestLabels(t *testing.T) { - b := &cozyv1alpha1.ApplicationDefinitionCredentials{SecretLabels: map[string]string{"cnpg.io/reload": "true"}} - l := Labels(b, "apps.cozystack.io", "Postgres", "orders") - for k, v := range map[string]string{ - AccountLabel: "true", - "apps.cozystack.io/application.group": "apps.cozystack.io", - "apps.cozystack.io/application.kind": "Postgres", - "apps.cozystack.io/application.name": "orders", - "cnpg.io/reload": "true", - } { - if l[k] != v { - t.Errorf("label %s = %q, want %q", k, l[k], v) - } + 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") @@ -230,3 +103,19 @@ func TestLabels(t *testing.T) { 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") + } +} diff --git a/pkg/credentials/applock/applock.go b/pkg/credentials/applock/applock.go deleted file mode 100644 index 8a8b673ae..000000000 --- a/pkg/credentials/applock/applock.go +++ /dev/null @@ -1,151 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -// Package applock serializes an application's credential operations across the -// API server's replicas. A create or update seeds and prunes account Secrets -// outside the HelmRelease write's optimistic concurrency, so two concurrent -// writes to one application can interleave: one deletes a Secret the other has -// just reused and referenced. The lock makes those operations take turns, keyed -// by the application, through a coordination.k8s.io Lease. -package applock - -import ( - "context" - "crypto/rand" - "crypto/sha256" - "encoding/hex" - "errors" - "fmt" - "os" - "time" - - coordinationv1 "k8s.io/api/coordination/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -// ErrHeld means another operation holds the application's lock. The caller -// turns it into a retryable Conflict rather than waiting, so a stuck holder -// cannot block a write past its lease. -var ErrHeld = errors.New("another change to the application's credentials is in progress") - -// Locker takes and releases the per-application lock. -type Locker struct { - // c reads and writes Leases; it must be uncached so an acquire sees a Lease - // a concurrent operation wrote a moment ago. - c client.Client - // namespace holds the Leases; cozy-system, which tenants cannot write, so a - // tenant cannot hold or delete its own application's lock. - namespace string - // duration is how long a Lease is honored without renewal. An operation - // finishes well within it; a holder that crashed has its Lease stolen once - // it expires, so the lock never wedges. - duration time.Duration -} - -// New returns a Locker that keeps its Leases in namespace (cozy-system). -func New(c client.Client, namespace string) *Locker { - return &Locker{c: c, namespace: namespace, duration: 60 * time.Second} -} - -// leaseName is the Lease name for one application, a hash so any namespace and -// release name map to a valid, collision-free object name. -func leaseName(appNamespace, release string) string { - sum := sha256.Sum256([]byte(appNamespace + "/" + release)) - return "cred-" + hex.EncodeToString(sum[:])[:40] -} - -func holderID() (string, error) { - var b [8]byte - if _, err := rand.Read(b[:]); err != nil { - return "", err - } - host, _ := os.Hostname() - return fmt.Sprintf("%s-%d-%s", host, os.Getpid(), hex.EncodeToString(b[:])), nil -} - -// Acquire takes the application's lock. On success it returns a release -// function the caller defers. It returns ErrHeld when another operation holds a -// live lock. Any other error is an infrastructure failure the caller may treat -// as fail-open, since the lock only guards a rare race and the Lease store being -// unreachable is no worse than not having the lock at all. -func (l *Locker) Acquire(ctx context.Context, appNamespace, release string) (func(), error) { - name := leaseName(appNamespace, release) - id, err := holderID() - if err != nil { - return nil, err - } - now := metav1.NewMicroTime(time.Now()) - dur := int32(l.duration / time.Second) - - cur := &coordinationv1.Lease{} - err = l.c.Get(ctx, client.ObjectKey{Namespace: l.namespace, Name: name}, cur) - switch { - case apierrors.IsNotFound(err): - lease := &coordinationv1.Lease{ - ObjectMeta: metav1.ObjectMeta{Namespace: l.namespace, Name: name}, - Spec: coordinationv1.LeaseSpec{ - HolderIdentity: &id, - LeaseDurationSeconds: &dur, - AcquireTime: &now, - RenewTime: &now, - }, - } - if err := l.c.Create(ctx, lease); err != nil { - if apierrors.IsAlreadyExists(err) { - return nil, ErrHeld // another operation created it between the Get and here - } - return nil, err - } - return l.releaser(name, id), nil - case err != nil: - return nil, err - } - - // A Lease exists: steal it only if it has expired, under its resourceVersion - // so two stealers do not both win. - if held(cur) { - return nil, ErrHeld - } - cur.Spec.HolderIdentity = &id - cur.Spec.LeaseDurationSeconds = &dur - cur.Spec.AcquireTime = &now - cur.Spec.RenewTime = &now - if err := l.c.Update(ctx, cur); err != nil { - if apierrors.IsConflict(err) { - return nil, ErrHeld // another operation stole or renewed it first - } - return nil, err - } - return l.releaser(name, id), nil -} - -// held reports whether a Lease is still honored: it has a holder and its renew -// time plus its duration is in the future. -func held(lease *coordinationv1.Lease) bool { - if lease.Spec.HolderIdentity == nil || *lease.Spec.HolderIdentity == "" || lease.Spec.RenewTime == nil { - return false - } - dur := time.Duration(60) * time.Second - if lease.Spec.LeaseDurationSeconds != nil { - dur = time.Duration(*lease.Spec.LeaseDurationSeconds) * time.Second - } - return lease.Spec.RenewTime.Add(dur).After(time.Now()) -} - -// releaser deletes the Lease if this operation still holds it. Best effort: a -// failed release leaves a Lease that the next acquire steals once it expires. -func (l *Locker) releaser(name, id string) func() { - return func() { - ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) - defer cancel() - cur := &coordinationv1.Lease{} - if err := l.c.Get(ctx, client.ObjectKey{Namespace: l.namespace, Name: name}, cur); err != nil { - return - } - if cur.Spec.HolderIdentity == nil || *cur.Spec.HolderIdentity != id { - return // someone else holds it now; not ours to delete - } - _ = l.c.Delete(ctx, cur, client.Preconditions{UID: &cur.UID, ResourceVersion: &cur.ResourceVersion}) - } -} diff --git a/pkg/credentials/applock/applock_test.go b/pkg/credentials/applock/applock_test.go deleted file mode 100644 index d63e91233..000000000 --- a/pkg/credentials/applock/applock_test.go +++ /dev/null @@ -1,85 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package applock - -import ( - "context" - "errors" - "testing" - "time" - - coordinationv1 "k8s.io/api/coordination/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" -) - -func newClient(t *testing.T) client.Client { - t.Helper() - s := runtime.NewScheme() - if err := coordinationv1.AddToScheme(s); err != nil { - t.Fatal(err) - } - return fake.NewClientBuilder().WithScheme(s).Build() -} - -func TestAcquireBlocksAndReleases(t *testing.T) { - ctx := context.Background() - l := New(newClient(t), "cozy-system") - - unlock, err := l.Acquire(ctx, "tenant-foo", "postgres-orders") - if err != nil { - t.Fatalf("first acquire: %v", err) - } - // Held now: a second acquire of the same application is refused. - if _, err := l.Acquire(ctx, "tenant-foo", "postgres-orders"); !errors.Is(err, ErrHeld) { - t.Fatalf("a held lock must refuse, got %v", err) - } - // A different application locks independently. - if u2, err := l.Acquire(ctx, "tenant-foo", "postgres-other"); err != nil { - t.Fatalf("a different application must lock independently: %v", err) - } else { - u2() - } - // After release, the application can be locked again. - unlock() - if u3, err := l.Acquire(ctx, "tenant-foo", "postgres-orders"); err != nil { - t.Fatalf("acquire after release: %v", err) - } else { - u3() - } -} - -func seedLease(t *testing.T, c client.Client, appNamespace, release, holder string, renew time.Time) { - t.Helper() - rt := metav1.NewMicroTime(renew) - dur := int32(60) - if err := c.Create(context.Background(), &coordinationv1.Lease{ - ObjectMeta: metav1.ObjectMeta{Namespace: "cozy-system", Name: leaseName(appNamespace, release)}, - Spec: coordinationv1.LeaseSpec{HolderIdentity: &holder, LeaseDurationSeconds: &dur, RenewTime: &rt}, - }); err != nil { - t.Fatal(err) - } -} - -func TestAcquireRefusesAFreshLeaseOfAnother(t *testing.T) { - ctx := context.Background() - c := newClient(t) - seedLease(t, c, "tenant-foo", "postgres-orders", "someone-else", time.Now()) - if _, err := New(c, "cozy-system").Acquire(ctx, "tenant-foo", "postgres-orders"); !errors.Is(err, ErrHeld) { - t.Fatalf("a fresh lease held by another must refuse, got %v", err) - } -} - -func TestAcquireStealsAnExpiredLease(t *testing.T) { - ctx := context.Background() - c := newClient(t) - // Held by a holder that last renewed longer than a lease duration ago. - seedLease(t, c, "tenant-foo", "postgres-orders", "crashed-holder", time.Now().Add(-5*time.Minute)) - unlock, err := New(c, "cozy-system").Acquire(ctx, "tenant-foo", "postgres-orders") - if err != nil { - t.Fatalf("an expired lease must be stealable: %v", err) - } - unlock() -} diff --git a/pkg/credentials/store/resolve.go b/pkg/credentials/store/resolve.go deleted file mode 100644 index 980c321f1..000000000 --- a/pkg/credentials/store/resolve.go +++ /dev/null @@ -1,37 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package store - -import ( - "encoding/json" - "fmt" - - apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - structuralschema "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" - - cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" - "github.com/cozystack/cozystack/pkg/apis/apps/defaulting" - "github.com/cozystack/cozystack/pkg/credentials/account" -) - -// ResolveAccounts resolves the tenant accounts an application declares from -// its values with the kind's schema defaults applied: stored values carry no -// defaults, and a Static account switched on by a defaulted flag would -// otherwise go unseeded. -func ResolveAccounts(block *cozyv1alpha1.ApplicationDefinitionCredentials, schema *structuralschema.Structural, values *apiextv1.JSON) ([]account.Account, error) { - if block == nil { - return nil, nil - } - m := map[string]any{} - if values != nil && len(values.Raw) > 0 { - if err := json.Unmarshal(values.Raw, &m); err != nil { - return nil, fmt.Errorf("decode values: %w", err) - } - } - if schema != nil { - if err := defaulting.Apply(&m, schema); err != nil { - return nil, fmt.Errorf("default values: %w", err) - } - } - return account.Resolve(block, m) -} diff --git a/pkg/credentials/store/store.go b/pkg/credentials/store/store.go deleted file mode 100644 index 38e762db9..000000000 --- a/pkg/credentials/store/store.go +++ /dev/null @@ -1,297 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -// Package store writes and removes account Secrets around a HelmRelease write: -// it seeds one per account, prunes the ones a write removes, and builds the -// valuesFrom entries that carry a stored value into a chart whose operator -// cannot read a Secret. -package store - -import ( - "context" - "crypto/sha256" - "encoding/hex" - "errors" - "fmt" - - helmv2 "github.com/fluxcd/helm-controller/api/v2" - corev1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - - 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" - "github.com/cozystack/cozystack/pkg/credentials/account" - "github.com/cozystack/cozystack/pkg/credentials/format" -) - -// App identifies the application whose account Secrets are written. -type App struct { - Namespace string - // Release is the HelmRelease name, prefix plus application name. - Release string - Group string - Kind string - Name string - Block *cozyv1alpha1.ApplicationDefinitionCredentials -} - -// Labels returns the labels every account Secret of the application carries. -func (a App) Labels() map[string]string { - return account.Labels(a.Block, a.Group, a.Kind, a.Name) -} - -// OwnerReference builds the HelmRelease owner reference the account Secrets -// carry, so they are collected with the release. -func OwnerReference(hr *helmv2.HelmRelease) metav1.OwnerReference { - return metav1.OwnerReference{ - APIVersion: helmv2.GroupVersion.String(), - Kind: helmv2.HelmReleaseKind, - Name: hr.Name, - UID: hr.UID, - } -} - -// SeedSecret builds the account Secret of a new account: the stored form of -// a random password that is discarded here, so the account exists in the -// engine while nobody can log in until a mint. owner may be nil on a create, -// where the HelmRelease does not exist yet and the reference is set after. -func SeedSecret(app App, acc account.Account, owner *metav1.OwnerReference, now metav1.Time, by string) (*corev1.Secret, error) { - password, err := format.GeneratePassword() - if err != nil { - return nil, err - } - stored, err := format.Encode(app.Block.Format.Name, password) - if err != nil { - return nil, err - } - sec := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: account.SecretName(app.Release, acc.Mapped), - Namespace: app.Namespace, - Labels: app.Labels(), - }, - Type: corev1.SecretTypeBasicAuth, - Data: map[string][]byte{ - account.UsernameKey: []byte(acc.Username), - account.PasswordKey: []byte(stored), - }, - } - if owner != nil { - sec.OwnerReferences = []metav1.OwnerReference{*owner} - } - account.WriteRecord(sec, account.Record{}.Apply(corev1alpha1.CredentialOperationCreate, now, by)) - return sec, nil -} - -// ErrLiveOwner is returned by Ensure on a create when a Secret at an account's -// name is owned by a HelmRelease that still exists: the application exists -// already, and its Secrets must not be touched. -var ErrLiveOwner = errors.New("account Secret is owned by a live HelmRelease") - -// Seeded is an account Secret a create wrote: its name, and the UID it got on -// creation. A failed create's cleanup deletes only the exact object it seeded, -// so it carries the UID to tell its own Secret from one a concurrent create of -// the same application has since replaced at the same name. -type Seeded struct { - Name string - UID types.UID -} - -// Theirs reports whether an existing Secret is the application's own account -// Secret: the account marker, the same username, and owned by owner. -func Theirs(sec *corev1.Secret, acc account.Account, owner *metav1.OwnerReference) bool { - if owner == nil || !account.IsAccountSecret(sec) || string(sec.Data[account.UsernameKey]) != acc.Username { - return false - } - for _, ref := range sec.OwnerReferences { - if ref.UID == owner.UID { - return true - } - } - return false -} - -// liveOwner reports whether a HelmRelease the Secret names as its owner still -// exists under the same UID. -func liveOwner(ctx context.Context, c client.Reader, sec *corev1.Secret) (bool, error) { - for _, ref := range sec.OwnerReferences { - if ref.Kind != helmv2.HelmReleaseKind { - continue - } - hr := &helmv2.HelmRelease{} - err := c.Get(ctx, types.NamespacedName{Namespace: sec.Namespace, Name: ref.Name}, hr) - switch { - case apierrors.IsNotFound(err): - continue - case err != nil: - return false, err - case hr.UID == ref.UID: - return true, nil - } - } - return false, nil -} - -// Ensure writes an account Secret for every account that has none, or whose -// Secret is not the application's, and returns the names of the Secrets it -// wrote. It never touches a Secret that is the application's own, so a -// password already minted survives every later write of the application. -// -// With owner nil (a create, before the HelmRelease exists) every existing -// Secret at an account's name is a leftover and is replaced, so the account -// gets a new identity and the Secret is among those to be owned; one a live -// HelmRelease owns stops the create with ErrLiveOwner. -func Ensure(ctx context.Context, c client.Client, app App, accounts []account.Account, owner *metav1.OwnerReference, now metav1.Time, by string) ([]Seeded, error) { - var written []Seeded - for _, acc := range accounts { - name := account.SecretName(app.Release, acc.Mapped) - cur := &corev1.Secret{} - err := c.Get(ctx, types.NamespacedName{Namespace: app.Namespace, Name: name}, cur) - switch { - case err == nil && Theirs(cur, acc, owner): - continue - case err == nil: - if owner == nil { - live, err := liveOwner(ctx, c, cur) - if err != nil { - return written, fmt.Errorf("read owner of account Secret %s: %w", name, err) - } - if live { - return written, fmt.Errorf("%s: %w", name, ErrLiveOwner) - } - } - // Delete the exact object read here, by UID and resourceVersion: a - // concurrent create or mint can have adopted and re-passworded this - // leftover between the read above and here, leaving the UID but a new - // resourceVersion, and the precondition then fails rather than destroy - // the Secret a live release now owns. - if err := c.Delete(ctx, cur, client.Preconditions{UID: &cur.UID, ResourceVersion: &cur.ResourceVersion}); err != nil && !apierrors.IsNotFound(err) { - return written, fmt.Errorf("replace account Secret %s: %w", name, err) - } - case !apierrors.IsNotFound(err): - return written, fmt.Errorf("read account Secret %s: %w", name, err) - } - sec, err := SeedSecret(app, acc, owner, now, by) - if err != nil { - return written, fmt.Errorf("seed account %s: %w", acc.Username, err) - } - if err := c.Create(ctx, sec); err != nil { - return written, fmt.Errorf("create account Secret %s: %w", name, err) - } - written = append(written, Seeded{Name: name, UID: sec.UID}) - } - return written, nil -} - -// Added returns the accounts of next whose username prev does not declare. -func Added(prev, next []account.Account) []account.Account { - had := make(map[string]bool, len(prev)) - for _, acc := range prev { - had[acc.Username] = true - } - var added []account.Account - for _, acc := range next { - if !had[acc.Username] { - added = append(added, acc) - } - } - return added -} - -// SetOwner adds the HelmRelease as owner of the named account Secrets. It is -// the second half of a create, once the HelmRelease exists. -func SetOwner(ctx context.Context, c client.Client, namespace string, seeded []Seeded, owner metav1.OwnerReference) error { - for _, s := range seeded { - name := s.Name - sec := &corev1.Secret{} - if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, sec); err != nil { - return fmt.Errorf("read account Secret %s: %w", name, err) - } - owned := false - for _, ref := range sec.OwnerReferences { - if ref.UID == owner.UID { - owned = true - } - } - if owned { - continue - } - patch := client.MergeFrom(sec.DeepCopy()) - sec.OwnerReferences = append(sec.OwnerReferences, owner) - if err := c.Patch(ctx, sec, patch); err != nil { - return fmt.Errorf("own account Secret %s: %w", name, err) - } - } - return nil -} - -// List returns the account Secrets of the application. -func List(ctx context.Context, c client.Client, app App) ([]corev1.Secret, error) { - list := &corev1.SecretList{} - if err := c.List(ctx, list, client.InNamespace(app.Namespace), client.MatchingLabels{ - account.AccountLabel: "true", - appsv1alpha1.ApplicationGroupLabel: app.Group, - appsv1alpha1.ApplicationKindLabel: app.Kind, - appsv1alpha1.ApplicationNameLabel: app.Name, - }); err != nil { - return nil, err - } - return list.Items, nil -} - -// Prune deletes the account Secrets of the application that no account in -// keep names, and returns the names it deleted. It runs after the HelmRelease -// write that removed the accounts, so the chart has already dropped them. -func Prune(ctx context.Context, c client.Client, app App, keep []account.Account) ([]string, error) { - kept := map[string]bool{} - for _, acc := range keep { - kept[account.SecretName(app.Release, acc.Mapped)] = true - } - secrets, err := List(ctx, c, app) - if err != nil { - return nil, err - } - var deleted []string - for i := range secrets { - sec := &secrets[i] - if kept[sec.Name] { - continue - } - if err := c.Delete(ctx, sec, client.Preconditions{UID: &sec.UID}); err != nil && !apierrors.IsNotFound(err) { - return deleted, fmt.Errorf("delete account Secret %s: %w", sec.Name, err) - } - deleted = append(deleted, sec.Name) - } - return deleted, nil -} - -// DigestKey is the values key one account's stored value lands under: a hex -// digest of the user name, because a user name fits neither a targetPath nor -// a valuesKey, while a digest fits both and leaves no order to keep aligned. -func DigestKey(username string) string { - sum := sha256.Sum256([]byte(username)) - return "u" + hex.EncodeToString(sum[:]) -} - -// ValuesFrom returns the HelmRelease valuesFrom entries of an application: the -// platform values, then for a kind with a valuesPath one entry per account -// reading the Secret's stored value into .. The API and -// the ApplicationDefinition reconciler must build the same list. -func ValuesFrom(block *cozyv1alpha1.ApplicationDefinitionCredentials, release string, accounts []account.Account) []helmv2.ValuesReference { - refs := []helmv2.ValuesReference{{Kind: "Secret", Name: "cozystack-values"}} - if block == nil || block.ValuesPath == "" { - return refs - } - for _, acc := range accounts { - refs = append(refs, helmv2.ValuesReference{ - Kind: "Secret", - Name: account.SecretName(release, acc.Mapped), - ValuesKey: account.PasswordKey, - TargetPath: block.ValuesPath + "." + DigestKey(acc.Username), - }) - } - return refs -} diff --git a/pkg/credentials/store/store_test.go b/pkg/credentials/store/store_test.go deleted file mode 100644 index ae5ebd80f..000000000 --- a/pkg/credentials/store/store_test.go +++ /dev/null @@ -1,390 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package store - -import ( - "context" - "errors" - "strings" - "sync/atomic" - "testing" - - helmv2 "github.com/fluxcd/helm-controller/api/v2" - corev1 "k8s.io/api/core/v1" - apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/client/interceptor" - - cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" - "github.com/cozystack/cozystack/pkg/apis/apps/defaulting" - corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" - "github.com/cozystack/cozystack/pkg/credentials/account" -) - -func newScheme(t *testing.T) *runtime.Scheme { - t.Helper() - s := runtime.NewScheme() - if err := corev1.AddToScheme(s); err != nil { - t.Fatal(err) - } - if err := helmv2.AddToScheme(s); err != nil { - t.Fatal(err) - } - return s -} - -func testBlock() *cozyv1alpha1.ApplicationDefinitionCredentials { - return &cozyv1alpha1.ApplicationDefinitionCredentials{ - Format: cozyv1alpha1.CredentialFormat{Name: "scram-sha-256"}, - SecretLabels: map[string]string{"k8s.mariadb.com/watch": ""}, - ValuesPath: "_accounts", - Accounts: []cozyv1alpha1.CredentialAccountSource{ - {Kind: cozyv1alpha1.CredentialAccountsValuesMap, Path: "users"}, - {Kind: cozyv1alpha1.CredentialAccountsStatic, Name: "backup", When: "backup.enabled"}, - {Kind: cozyv1alpha1.CredentialAccountsStatic, Name: "root", Owner: cozyv1alpha1.CredentialAccountOwnerPlatform}, - }, - } -} - -func testApp() App { - return App{Namespace: "tenant-foo", Release: "postgres-orders", Group: "apps.cozystack.io", Kind: "Postgres", Name: "orders", Block: testBlock()} -} - -func release(uid string) *helmv2.HelmRelease { - return &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "postgres-orders", Namespace: "tenant-foo", UID: types.UID(uid)}} -} - -func accounts(names ...string) []account.Account { - var out []account.Account - for _, n := range names { - out = append(out, account.Account{Username: n, Mapped: strings.ToLower(n)}) - } - return out -} - -func getSecret(t *testing.T, c client.Client, name string) *corev1.Secret { - t.Helper() - sec := &corev1.Secret{} - if err := c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-foo", Name: name}, sec); err != nil { - t.Fatalf("get %s: %v", name, err) - } - return sec -} - -func TestEnsureSeedsAndKeepsTheirs(t *testing.T) { - ctx := context.Background() - c := fake.NewClientBuilder().WithScheme(newScheme(t)).Build() - app := testApp() - now := metav1.Now() - - written, err := Ensure(ctx, c, app, accounts("web"), nil, now, "jane") - if err != nil { - t.Fatal(err) - } - if len(written) != 1 || written[0].Name != "postgres-orders.web.account" { - t.Fatalf("written = %v", written) - } - sec := getSecret(t, c, "postgres-orders.web.account") - if sec.Type != corev1.SecretTypeBasicAuth || string(sec.Data[account.UsernameKey]) != "web" { - t.Fatalf("seeded Secret = %+v", sec) - } - if !strings.HasPrefix(string(sec.Data[account.PasswordKey]), "SCRAM-SHA-256$4096:") { - t.Fatalf("password not stored in the kind's format: %q", sec.Data[account.PasswordKey]) - } - if sec.Labels[account.AccountLabel] != "true" || sec.Labels["k8s.mariadb.com/watch"] != "" || sec.Labels["apps.cozystack.io/application.name"] != "orders" { - t.Fatalf("labels = %v", sec.Labels) - } - rec := account.ReadRecord(sec) - if rec.Origin != corev1alpha1.CredentialOriginNotIssued || rec.By != "jane" || len(rec.History) != 1 || rec.History[0].Operation != corev1alpha1.CredentialOperationCreate { - t.Fatalf("record = %+v", rec) - } - first := string(sec.Data[account.PasswordKey]) - - // An update, with the owner known, seeds only what it is given and leaves - // an owned Secret alone. - hr := release("uid-1") - if err := SetOwner(ctx, c, "tenant-foo", written, OwnerReference(hr)); err != nil { - t.Fatal(err) - } - owner := OwnerReference(hr) - written, err = Ensure(ctx, c, app, accounts("web", "api"), &owner, now, "jane") - if err != nil { - t.Fatal(err) - } - if len(written) != 1 || written[0].Name != "postgres-orders.api.account" { - t.Fatalf("an update seeds only the added account, written = %v", written) - } - sec = getSecret(t, c, "postgres-orders.web.account") - if string(sec.Data[account.PasswordKey]) != first { - t.Fatal("an owned Secret must survive a later write untouched") - } - api := getSecret(t, c, "postgres-orders.api.account") - if len(api.OwnerReferences) != 1 || api.OwnerReferences[0].UID != "uid-1" { - t.Fatalf("a Secret seeded on update carries the owner from the start: %v", api.OwnerReferences) - } -} - -func TestEnsureKeepsALeftoverAdoptedMidReplace(t *testing.T) { - ctx := context.Background() - var adopted atomic.Bool - c := fake.NewClientBuilder().WithScheme(newScheme(t)).WithInterceptorFuncs(interceptor.Funcs{ - Delete: func(ctx context.Context, wc client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { - // A concurrent create adopts and re-passwords the leftover between - // Ensure's read and this delete, which bumps its resourceVersion. - if sec, ok := obj.(*corev1.Secret); ok && sec.Name == "postgres-orders.web.account" && adopted.CompareAndSwap(false, true) { - live := &corev1.Secret{} - if err := wc.Get(ctx, client.ObjectKeyFromObject(obj), live); err != nil { - return err - } - live.OwnerReferences = []metav1.OwnerReference{{APIVersion: helmv2.GroupVersion.String(), Kind: helmv2.HelmReleaseKind, Name: "postgres-orders", UID: "uid-winner"}} - live.Data[account.PasswordKey] = []byte("minted") - if err := wc.Update(ctx, live); err != nil { - return err - } - } - return wc.Delete(ctx, obj, opts...) - }, - }).Build() - app := testApp() - - leftover, err := SeedSecret(app, account.Account{Username: "web", Mapped: "web"}, nil, metav1.Now(), "earlier") - if err != nil { - t.Fatal(err) - } - leftover.UID = "uid-leftover" - if err := c.Create(ctx, leftover); err != nil { - t.Fatal(err) - } - - if _, err := Ensure(ctx, c, app, accounts("web"), nil, metav1.Now(), "jane"); err == nil { - t.Fatal("Ensure must fail rather than replace a leftover adopted under it") - } - sec := getSecret(t, c, "postgres-orders.web.account") - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != "uid-winner" || string(sec.Data[account.PasswordKey]) != "minted" { - t.Fatalf("the adopted Secret must survive: %+v data=%q", sec.ObjectMeta, sec.Data[account.PasswordKey]) - } -} - -func TestEnsureReplacesStale(t *testing.T) { - ctx := context.Background() - c := fake.NewClientBuilder().WithScheme(newScheme(t)).Build() - app := testApp() - now := metav1.Now() - - // Owned by a HelmRelease that is gone: a leftover of a deleted application - // of the same name, replaced on create so the account gets a new identity. - old := OwnerReference(release("uid-old")) - if _, err := Ensure(ctx, c, app, accounts("web"), &old, now, "jane"); err != nil { - t.Fatal(err) - } - before := getSecret(t, c, "postgres-orders.web.account") - written, err := Ensure(ctx, c, app, accounts("web"), nil, now, "jane") - if err != nil { - t.Fatal(err) - } - // The fake client assigns no UIDs, so a replacement shows as a new random - // password and no owner. - after := getSecret(t, c, "postgres-orders.web.account") - if len(written) != 1 || string(after.Data[account.PasswordKey]) == string(before.Data[account.PasswordKey]) || len(after.OwnerReferences) != 0 { - t.Fatalf("expected a fresh unowned Secret, written=%v owners=%v", written, after.OwnerReferences) - } - - // Same name, another username: not theirs either. - live := OwnerReference(release("uid-live")) - if err := SetOwner(ctx, c, "tenant-foo", written, live); err != nil { - t.Fatal(err) - } - written, err = Ensure(ctx, c, app, []account.Account{{Username: "Web", Mapped: "web"}}, &live, now, "jane") - if err != nil { - t.Fatal(err) - } - renamed := getSecret(t, c, "postgres-orders.web.account") - if len(written) != 1 || string(renamed.Data[account.PasswordKey]) == string(after.Data[account.PasswordKey]) || string(renamed.Data[account.UsernameKey]) != "Web" { - t.Fatalf("expected the Secret replaced for the new username, got %q", renamed.Data[account.UsernameKey]) - } -} - -func TestPruneDeletesRemovedOnly(t *testing.T) { - ctx := context.Background() - c := fake.NewClientBuilder().WithScheme(newScheme(t)).Build() - app := testApp() - owner := OwnerReference(release("uid-1")) - if _, err := Ensure(ctx, c, app, accounts("web", "api"), &owner, metav1.Now(), "jane"); err != nil { - t.Fatal(err) - } - // A Secret of another application in the namespace is never touched. - other := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{Name: "postgres-billing.web.account", Namespace: "tenant-foo", Labels: account.Labels(app.Block, app.Group, app.Kind, "billing")}} - if err := c.Create(ctx, other); err != nil { - t.Fatal(err) - } - deleted, err := Prune(ctx, c, app, accounts("api")) - if err != nil { - t.Fatal(err) - } - if len(deleted) != 1 || deleted[0] != "postgres-orders.web.account" { - t.Fatalf("deleted = %v", deleted) - } - left, err := List(ctx, c, app) - if err != nil { - t.Fatal(err) - } - if len(left) != 1 || left[0].Name != "postgres-orders.api.account" { - t.Fatalf("left = %v", left) - } - getSecret(t, c, "postgres-billing.web.account") -} - -func TestValuesFrom(t *testing.T) { - refs := ValuesFrom(nil, "postgres-orders", accounts("web")) - if len(refs) != 1 || refs[0].Name != "cozystack-values" { - t.Fatalf("a kind without a block carries the platform values only: %+v", refs) - } - block := testBlock() - block.ValuesPath = "" - if refs := ValuesFrom(block, "postgres-orders", accounts("web")); len(refs) != 1 { - t.Fatalf("a block without a valuesPath adds nothing: %+v", refs) - } - refs = ValuesFrom(testBlock(), "postgres-orders", []account.Account{{Username: "Web.1", Mapped: "web-1"}}) - if len(refs) != 2 { - t.Fatalf("refs = %+v", refs) - } - got := refs[1] - if got.Kind != "Secret" || got.Name != "postgres-orders.web-1.account" || got.ValuesKey != account.PasswordKey || got.TargetPath != "_accounts."+DigestKey("Web.1") { - t.Fatalf("ref = %+v", got) - } - if DigestKey("Web.1") == DigestKey("web.1") || !strings.HasPrefix(DigestKey("x"), "u") { - t.Fatal("the digest key is of the original user name") - } -} - -func TestResolveAccountsAppliesDefaults(t *testing.T) { - schema, err := defaulting.BuildSchema(`{"type":"object","properties":{"backup":{"type":"object","default":{},"properties":{"enabled":{"type":"boolean","default":true}}},"users":{"type":"object","additionalProperties":{"type":"object"}}}}`) - if err != nil { - t.Fatal(err) - } - values := &apiextv1.JSON{Raw: []byte(`{"users":{"web":{},"root":{}}}`)} - got, err := ResolveAccounts(testBlock(), schema, values) - if err != nil { - t.Fatal(err) - } - var names []string - for _, a := range got { - names = append(names, a.Username) - } - // root is a platform account and is skipped; backup is on by default. - if strings.Join(names, ",") != "web,backup" { - t.Fatalf("accounts = %v", names) - } - got, err = ResolveAccounts(testBlock(), nil, values) - if err != nil { - t.Fatal(err) - } - if len(got) != 1 { - t.Fatalf("without the schema the defaulted flag is off: %v", got) - } - if got, err := ResolveAccounts(nil, schema, values); err != nil || got != nil { - t.Fatalf("no block, no accounts: %v %v", got, err) - } -} - -func TestEnsureCreateReplacesLeftovers(t *testing.T) { - ctx := context.Background() - gone := OwnerReference(release("uid-gone")) - live := release("uid-live") - c := fake.NewClientBuilder().WithScheme(newScheme(t)).WithObjects(live).Build() - app := testApp() - now := metav1.Now() - - // Left by an earlier create that never got its owner, and by a deleted - // application of the same name whose HelmRelease is gone. - if _, err := Ensure(ctx, c, app, accounts("web"), nil, now, "x"); err != nil { - t.Fatal(err) - } - if _, err := Ensure(ctx, c, app, accounts("api"), &gone, now, "x"); err != nil { - t.Fatal(err) - } - unowned := getSecret(t, c, "postgres-orders.web.account") - stale := getSecret(t, c, "postgres-orders.api.account") - - written, err := Ensure(ctx, c, app, accounts("web", "api"), nil, now, "jane") - if err != nil { - t.Fatal(err) - } - if len(written) != 2 { - t.Fatalf("both leftovers must be replaced and handed to SetOwner, written = %v", written) - } - if string(getSecret(t, c, "postgres-orders.web.account").Data[account.PasswordKey]) == string(unowned.Data[account.PasswordKey]) { - t.Fatal("an unowned leftover kept its verifier") - } - if string(getSecret(t, c, "postgres-orders.api.account").Data[account.PasswordKey]) == string(stale.Data[account.PasswordKey]) { - t.Fatal("a Secret of a gone release kept its verifier") - } -} - -func TestEnsureCreateRefusesLiveOwner(t *testing.T) { - ctx := context.Background() - live := release("uid-live") - c := fake.NewClientBuilder().WithScheme(newScheme(t)).WithObjects(live).Build() - app := testApp() - owner := OwnerReference(live) - if _, err := Ensure(ctx, c, app, accounts("web"), &owner, metav1.Now(), "x"); err != nil { - t.Fatal(err) - } - before := getSecret(t, c, "postgres-orders.web.account") - - written, err := Ensure(ctx, c, app, accounts("web"), nil, metav1.Now(), "jane") - if !errors.Is(err, ErrLiveOwner) || len(written) != 0 { - t.Fatalf("written=%v err=%v, want ErrLiveOwner and nothing written", written, err) - } - after := getSecret(t, c, "postgres-orders.web.account") - if string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) || len(after.OwnerReferences) != 1 { - t.Fatal("the live application's Secret must be left untouched") - } -} - -// The application labels alone do not make a Secret an account Secret: the -// lineage webhook puts them on every Secret of the application, and Prune -// must never delete the chart's own. -func TestListAndPruneSeeOnlyAccountSecrets(t *testing.T) { - ctx := context.Background() - app := testApp() - chartOwned := &corev1.Secret{ObjectMeta: metav1.ObjectMeta{ - Name: "postgres-orders-credentials", Namespace: "tenant-foo", - Labels: map[string]string{ - "apps.cozystack.io/application.group": app.Group, - "apps.cozystack.io/application.kind": app.Kind, - "apps.cozystack.io/application.name": app.Name, - }, - }} - c := fake.NewClientBuilder().WithScheme(newScheme(t)).WithObjects(chartOwned).Build() - if _, err := Ensure(ctx, c, app, accounts("web"), nil, metav1.Now(), "x"); err != nil { - t.Fatal(err) - } - listed, err := List(ctx, c, app) - if err != nil { - t.Fatal(err) - } - if len(listed) != 1 || listed[0].Name != "postgres-orders.web.account" { - t.Fatalf("listed = %v", listed) - } - if _, err := Prune(ctx, c, app, nil); err != nil { - t.Fatal(err) - } - getSecret(t, c, "postgres-orders-credentials") -} - -func TestAdded(t *testing.T) { - got := Added(accounts("web", "api"), []account.Account{{Username: "web", Mapped: "web"}, {Username: "Api", Mapped: "api"}, {Username: "new", Mapped: "new"}}) - var names []string - for _, a := range got { - names = append(names, a.Username) - } - // Api is a different user from api, however the name maps. - if strings.Join(names, ",") != "Api,new" { - t.Fatalf("added = %v", names) - } -} diff --git a/pkg/registry/apps/application/rest.go b/pkg/registry/apps/application/rest.go index a03c508e1..01e73bff6 100644 --- a/pkg/registry/apps/application/rest.go +++ b/pkg/registry/apps/application/rest.go @@ -44,7 +44,6 @@ import ( "k8s.io/apimachinery/pkg/watch" "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/registry/rest" - "k8s.io/apiserver/pkg/util/dryrun" "k8s.io/apiserver/pkg/warning" "k8s.io/client-go/util/retry" "k8s.io/klog/v2" @@ -56,7 +55,6 @@ import ( appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" "github.com/cozystack/cozystack/pkg/apis/apps/validation" "github.com/cozystack/cozystack/pkg/config" - "github.com/cozystack/cozystack/pkg/credentials/applock" "github.com/cozystack/cozystack/pkg/registry" fieldfilter "github.com/cozystack/cozystack/pkg/registry/fields" "github.com/cozystack/cozystack/pkg/registry/sorting" @@ -103,17 +101,14 @@ type REST struct { specSchema *structuralschema.Structural nameSchema *validation.NameSchema nameSchemaErr error - // credentials is the kind's credentials block, nil for a kind whose - // passwords the chart still generates. - credentials *cozyv1alpha1.ApplicationDefinitionCredentials - // lock serializes one application's credential seeding and pruning across - // the API server's replicas, so two concurrent writes cannot interleave - // into deleting a Secret the other has just reused. - lock *applock.Locker } -// buildSpecSchema parses an OpenAPI-v3 JSON schema string into the -// structural schema the registry defaults and null-strips against. +// buildSpecSchema parses an OpenAPI-v3 JSON schema string and returns the +// structural-schema form used for defaulting. Returns (nil, nil) when raw +// is empty after trimming. Returns (nil, error) for malformed JSON, +// v1→internal conversion failures, or structural-schema construction +// errors — callers can decide whether to log-and-continue (NewREST does) +// or fail hard (tests do). func buildSpecSchema(raw string) (*structuralschema.Structural, error) { return defaulting.BuildSchema(raw) } @@ -131,13 +126,6 @@ func NewREST(c client.Client, w client.WithWatch, config *config.Resource) *REST config.Application.Kind, appsv1alpha1.NameSchemaExtension, config.Application.Kind, nameSchemaErr) } - // The lock reads and writes its Leases uncached, and in cozy-system, which - // tenants cannot write. - lockClient := client.Client(w) - if lockClient == nil { - lockClient = c - } - return &REST{ c: c, w: w, @@ -156,8 +144,6 @@ func NewREST(c client.Client, w client.WithWatch, config *config.Resource) *REST specSchema: specSchema, nameSchema: nameSchema, nameSchemaErr: nameSchemaErr, - credentials: config.Credentials, - lock: applock.New(lockClient, "cozy-system"), } } @@ -240,11 +226,6 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation } } - accounts, err := r.accountsOf(app.Spec) - if err != nil { - return nil, r.invalidAccounts(app, err) - } - // Convert Application to HelmRelease helmRelease, err := r.ConvertApplicationToHelmRelease(app) if err != nil { @@ -264,21 +245,7 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation helmRelease.Labels[ApplicationGroupLabel] = r.gvk.Group helmRelease.Labels[ApplicationNameLabel] = app.Name // Note: Annotations from config are not handled as r.releaseConfig.Annotations is undefined - - // Hold the application's credential lock across the seed, the release write - // and the owning, so a concurrent write cannot interleave with them. - unlock, err := r.lockCredentials(ctx, app) - if err != nil { - return nil, err - } - defer unlock() - - // The account Secrets exist before the release that reads them, and the - // release owns them as soon as it exists. - seeded, err := r.seedBeforeCreate(ctx, app, helmRelease, accounts, options != nil && dryrun.IsDryRun(options.DryRun)) - if err != nil { - return nil, err - } + markForSeeding(helmRelease) klog.V(6).Infof("Creating HelmRelease %s in namespace %s", helmRelease.Name, app.Namespace) @@ -286,10 +253,8 @@ func (r *REST) Create(ctx context.Context, obj runtime.Object, createValidation err = r.c.Create(ctx, helmRelease, registry.ClientCreateOptions(options)) if err != nil { klog.Errorf("Failed to create HelmRelease %s: %v", helmRelease.Name, err) - r.unseed(ctx, app.Namespace, helmRelease.Name, seeded) return nil, registry.WrapPreservingStatus("failed to create HelmRelease", err, r.gvr.GroupResource(), app.Name) } - r.ownSeeded(ctx, helmRelease, seeded) // Convert the created HelmRelease back to Application convertedApp, err := r.ConvertHelmReleaseToApplication(ctx, helmRelease) @@ -611,11 +576,6 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje r.warnRemovedUserPasswords(ctx, app) r.warnUpgradeIntroducedCollisions(ctx, oldObj, app) - accounts, err := r.accountsOf(app.Spec) - if err != nil { - return nil, false, r.invalidAccounts(app, err) - } - // Convert Application to HelmRelease helmRelease, err := r.ConvertApplicationToHelmRelease(app) if err != nil { @@ -623,33 +583,20 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje return nil, false, fmt.Errorf("conversion error: %v", err) } - // Hold the application's credential lock across the read of the live - // release, the seed, the release write and the prune, so a concurrent write - // cannot re-add an account and have this request prune the Secret it reused. - unlock, err := r.lockCredentials(ctx, app) - if err != nil { - return nil, false, err - } - defer unlock() + // Keep the conversion output pristine: the metadata assembly below reads the + // live object, so a conflict retry has to redo it from this copy rather than + // re-apply it on top of its own previous result. + converted := helmRelease.DeepCopy() cur := &helmv2.HelmRelease{} if err := r.c.Get(ctx, client.ObjectKey{Namespace: helmRelease.Namespace, Name: helmRelease.Name}, cur, &client.GetOptions{Raw: &metav1.GetOptions{}}); err != nil { return nil, false, registry.WrapPreservingStatus("failed to fetch current HelmRelease", err, r.gvr.GroupResource(), name) } - - // Accounts the write adds get their Secrets before the release reads them. - if err := r.seedBeforeUpdate(ctx, app, helmRelease, cur, accounts, options != nil && dryrun.IsDryRun(options.DryRun)); err != nil { - return nil, false, err - } - - // Keep the conversion output pristine: the metadata assembly below reads the - // live object, so a conflict retry has to redo it from this copy rather than - // re-apply it on top of its own previous result. - converted := helmRelease.DeepCopy() if helmRelease.ResourceVersion == "" { helmRelease.SetResourceVersion(cur.GetResourceVersion()) } r.applyLiveMetadata(helmRelease, cur, app, previousFinalizers) + keepAccountValuesFrom(helmRelease, cur) // Suspension is not part of the Application, so the rebuilt object always // says suspend=false, and sending it as is resumes a release that an @@ -691,6 +638,7 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje } helmRelease = converted.DeepCopy() r.applyLiveMetadata(helmRelease, cur, app, previousFinalizers) + keepAccountValuesFrom(helmRelease, cur) helmRelease.SetResourceVersion(cur.GetResourceVersion()) helmRelease.Spec.Suspend = cur.Spec.Suspend } @@ -701,10 +649,6 @@ func (r *REST) Update(ctx context.Context, name string, objInfo rest.UpdatedObje return nil, false, registry.WrapPreservingStatus("failed to update HelmRelease", err, r.gvr.GroupResource(), name) } - // Accounts the write removed are gone from the chart now, so their - // Secrets can go. - r.pruneAfterUpdate(ctx, app, options != nil && dryrun.IsDryRun(options.DryRun)) - // Convert the updated HelmRelease back to Application convertedApp, err := r.ConvertHelmReleaseToApplication(ctx, helmRelease) if err != nil { @@ -753,7 +697,7 @@ func (r *REST) Delete(ctx context.Context, name string, deleteValidation rest.Va return nil, false, apierrors.NewNotFound(r.gvr.GroupResource(), name) } - if err := r.refuseDeleteSuspended(name, helmRelease); err != nil { + if err := r.refuseDeleteSuspended(ctx, name, helmRelease); err != nil { return nil, false, err } @@ -2261,13 +2205,7 @@ const ( // warnRemovedUserPasswords emits a client-facing admission warning for every // spec.users[].password still present on a Postgres or MariaDB resource. -// A kind with a credentials block gets the warning that points at the mint -// instead, whatever the kind. func (r *REST) warnRemovedUserPasswords(ctx context.Context, app *appsv1alpha1.Application) { - if r.credentials != nil { - r.warnMintedPasswords(ctx, app) - return - } if !removedUserPasswordKinds[r.kindName] || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 { return } diff --git a/pkg/registry/apps/application/rest_credentials.go b/pkg/registry/apps/application/rest_credentials.go index 74a084f9b..f875356dd 100644 --- a/pkg/registry/apps/application/rest_credentials.go +++ b/pkg/registry/apps/application/rest_credentials.go @@ -4,295 +4,59 @@ package application import ( "context" - "encoding/json" - "errors" "fmt" helmv2 "github.com/fluxcd/helm-controller/api/v2" - corev1 "k8s.io/api/core/v1" - apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/apiserver/pkg/endpoints/request" - "k8s.io/apiserver/pkg/warning" - "k8s.io/klog/v2" "sigs.k8s.io/controller-runtime/pkg/client" cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" - appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" "github.com/cozystack/cozystack/pkg/credentials/account" - "github.com/cozystack/cozystack/pkg/credentials/applock" - "github.com/cozystack/cozystack/pkg/credentials/store" ) -// Credentials returns the kind's credentials block, nil for a kind that is -// not converted. -func (r *REST) Credentials() *cozyv1alpha1.ApplicationDefinitionCredentials { - return r.credentials +// markForSeeding lets the CredentialTemplate controller seed the account +// Secrets of a release the API creates: its accounts are new, so no client +// holds a password a seeded one would replace. A release created before the +// mark existed gets it from the conversion, once the conversion has written +// its Secrets. The mark is outside the apps.cozystack.io- prefix, so an update +// keeps it. +func markForSeeding(hr *helmv2.HelmRelease) { + if hr.Annotations == nil { + hr.Annotations = map[string]string{} + } + hr.Annotations[account.SeedAnnotation] = "true" } -// AccountsOf resolves the tenant accounts a HelmRelease declares; the -// credentials registry asks it on a mint of an account whose Secret is missing. -func (r *REST) AccountsOf(_ context.Context, hr *helmv2.HelmRelease) ([]account.Account, error) { - return r.accountsOf(hr.Spec.Values) -} - -func (r *REST) accountsOf(values *apiextv1.JSON) ([]account.Account, error) { - return store.ResolveAccounts(r.credentials, r.specSchema, values) -} - -// invalidAccounts turns an account resolution failure into the Invalid the -// write is refused with. -func (r *REST) invalidAccounts(app *appsv1alpha1.Application, err error) error { - return apierrors.NewInvalid(r.gvk.GroupKind(), app.Name, field.ErrorList{ - field.Invalid(field.NewPath("spec"), "", err.Error()), - }) -} - -func (r *REST) storeApp(namespace, name string) store.App { - return store.App{ - Namespace: namespace, - Release: r.releaseConfig.Prefix + name, - Group: r.gvk.Group, - Kind: r.kindName, - Name: name, - Block: r.credentials, - } -} - -// actor names the caller for the record an account Secret keeps. -func actor(ctx context.Context) string { - if user, ok := request.UserFrom(ctx); ok { - return user.GetName() - } - return "" -} - -// direct is the client the account Secret writes read through. The cached -// client can miss an object written a moment ago, and a stale read here -// replaces or recreates a Secret that exists. -func (r *REST) direct() client.Client { - if r.w != nil { - return r.w - } - return r.c -} - -// lockCredentials takes the per-application lock so this request's seeding and -// pruning do not interleave with a concurrent write to the same application. It -// returns a release function the caller defers. A lock another request holds is -// a retryable Conflict; a lock store that cannot be reached is logged and the -// request proceeds without it, since the lock only guards a rare race and its -// store being unreachable is no worse than not having it. -func (r *REST) lockCredentials(ctx context.Context, app *appsv1alpha1.Application) (func(), error) { - if r.credentials == nil || r.lock == nil { - return func() {}, nil - } - unlock, err := r.lock.Acquire(ctx, app.Namespace, r.storeApp(app.Namespace, app.Name).Release) - switch { - case errors.Is(err, applock.ErrHeld): - return nil, apierrors.NewConflict(r.gvr.GroupResource(), app.Name, errors.New("another change to this application's credentials is in progress; retry")) - case err != nil: - klog.Warningf("credential lock unavailable for %s/%s, proceeding without it: %v", app.Namespace, app.Name, err) - return func() {}, nil - } - return unlock, nil -} - -// seedBeforeCreate writes the account Secrets of a new application, with no -// owner yet, and the valuesFrom entries that carry them into the chart. -// It returns the Secrets it wrote, for the owner to be set once the -// HelmRelease exists, or for removal if the HelmRelease is not created. -// An existing release fails the create before any Secret is touched, since -// its own Secrets would otherwise be replaced. -func (r *REST) seedBeforeCreate(ctx context.Context, app *appsv1alpha1.Application, hr *helmv2.HelmRelease, accounts []account.Account, dryRun bool) ([]store.Seeded, error) { - if r.credentials == nil { - return nil, nil - } - hr.Spec.ValuesFrom = store.ValuesFrom(r.credentials, hr.Name, accounts) - existing := &helmv2.HelmRelease{} - err := r.direct().Get(ctx, client.ObjectKey{Namespace: hr.Namespace, Name: hr.Name}, existing) - switch { - case err == nil: - return nil, apierrors.NewAlreadyExists(r.gvr.GroupResource(), app.Name) - case !apierrors.IsNotFound(err): - return nil, apierrors.NewInternalError(fmt.Errorf("read HelmRelease %s: %w", hr.Name, err)) - } - if dryRun { - return nil, nil - } - written, err := store.Ensure(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), accounts, nil, metav1.Now(), actor(ctx)) - if err != nil { - r.unseed(ctx, app.Namespace, hr.Name, written) - if errors.Is(err, store.ErrLiveOwner) { - return nil, apierrors.NewAlreadyExists(r.gvr.GroupResource(), app.Name) - } - return nil, apierrors.NewInternalError(fmt.Errorf("seed account Secrets: %w", err)) - } - return written, nil -} - -// unseed removes the Secrets a create wrote before its HelmRelease failed, so -// a retry starts clean. Ownership of an application's account Secrets follows -// its HelmRelease, not a Secret's identity: once a release exists, a concurrent -// create won it and owns the Secrets, and may have adopted by name the very one -// this create seeded, so unseed deletes nothing. A create that lost the race -// reaches here only after its own create failed with AlreadyExists, by when -// that release provably exists, and the read is uncached so it is seen. Only a -// create that left no release behind deletes its orphaned seed, and then only -// the exact object it wrote, so a concurrent retry's fresh Secret survives. -func (r *REST) unseed(ctx context.Context, namespace, release string, seeded []store.Seeded) { - hr := &helmv2.HelmRelease{} - switch err := r.direct().Get(ctx, client.ObjectKey{Namespace: namespace, Name: release}, hr); { - case err == nil: - return - case !apierrors.IsNotFound(err): - klog.Warningf("failed to check HelmRelease %s/%s before cleaning up a failed create; leaving its account Secrets: %v", namespace, release, err) - return - } - for _, s := range seeded { - cur := &corev1.Secret{} - if err := r.direct().Get(ctx, client.ObjectKey{Namespace: namespace, Name: s.Name}, cur); err != nil { - if !apierrors.IsNotFound(err) { - klog.Warningf("failed to read account Secret %s/%s before removing it after a failed create: %v", namespace, s.Name, err) - } - continue - } - if cur.UID != s.UID { - continue - } - rv := cur.ResourceVersion - if err := r.direct().Delete(ctx, cur, client.Preconditions{UID: &s.UID, ResourceVersion: &rv}); err != nil && - !apierrors.IsNotFound(err) && !apierrors.IsConflict(err) { - klog.Warningf("failed to remove account Secret %s/%s after a failed create: %v", namespace, s.Name, err) +// keepAccountValuesFrom carries over the valuesFrom entries the +// CredentialTemplate controller added to the live release: the rebuilt +// release lists only the platform values, and dropping the entries would +// take every account's stored value out of the chart until the controller +// put them back. +func keepAccountValuesFrom(hr, cur *helmv2.HelmRelease) { + for _, ref := range cur.Spec.ValuesFrom { + if account.IsAccountValuesFrom(ref) { + hr.Spec.ValuesFrom = append(hr.Spec.ValuesFrom, ref) } } } -// ownSeeded gives the created HelmRelease the account Secrets seeded before -// it. The application exists by now, so a failure is reported as a warning -// rather than an error. -func (r *REST) ownSeeded(ctx context.Context, hr *helmv2.HelmRelease, seeded []store.Seeded) { - if len(seeded) == 0 { - return - } - if err := store.SetOwner(ctx, r.direct(), hr.Namespace, seeded, store.OwnerReference(hr)); err != nil { - klog.Errorf("failed to own account Secrets of %s/%s: %v", hr.Namespace, hr.Name, err) - warning.AddWarning(ctx, "", fmt.Sprintf("account Secrets were written but not yet owned by the release: %v", err)) - } -} - -// seedBeforeUpdate writes the account Secrets of the accounts the update adds, -// owned by the live HelmRelease from the start, and the valuesFrom entries of -// the result. An account the release already declared and that has no Secret -// is left alone: a seed would replace its working password with a discarded -// one, and a mint creates the Secret when someone needs it. -func (r *REST) seedBeforeUpdate(ctx context.Context, app *appsv1alpha1.Application, hr, cur *helmv2.HelmRelease, accounts []account.Account, dryRun bool) error { - if r.credentials == nil { +// refuseDeleteSuspended refuses to delete an application whose release is +// suspended and carries accounts: Flux skips the uninstall of a suspended +// release, so the database would keep running with its last passwords after +// the account Secrets went with the release's templates. +func (r *REST) refuseDeleteSuspended(ctx context.Context, name string, hr *helmv2.HelmRelease) error { + if !hr.Spec.Suspend { return nil } - hr.Spec.ValuesFrom = store.ValuesFrom(r.credentials, hr.Name, accounts) - if dryRun { - return nil + templates := &cozyv1alpha1.CredentialTemplateList{} + if err := r.c.List(ctx, templates, client.InNamespace(hr.Namespace)); err != nil { + return err } - prev, err := r.accountsOf(cur.Spec.Values) - if err != nil { - // The stored values were written around the API; nothing tells which - // accounts are new, so seed none and leave the rest to a mint. - klog.Warningf("accounts of %s/%s before the update cannot be resolved, seeding none: %v", cur.Namespace, cur.Name, err) - return nil - } - release := r.storeApp(app.Namespace, app.Name).Release - added := store.Added(prev, accounts) - // An added account whose mapped name already holds a different account is a - // rename that changed only the case of a user name: both map to one Secret. - // Seeding it would replace the live account's Secret with a new password - // before the release write, and a failed write would lose that password with - // no way back. Refuse it, since the live account is not actually gone. - for _, acc := range added { - ex := &corev1.Secret{} - switch err := r.direct().Get(ctx, client.ObjectKey{Namespace: app.Namespace, Name: account.SecretName(release, acc.Mapped)}, ex); { - case err == nil && account.IsAccountSecret(ex) && string(ex.Data[account.UsernameKey]) != acc.Username: - return r.invalidAccounts(app, fmt.Errorf("account %q maps to the name %q, which already belongs to account %q; rename it to a name that does not collide", acc.Username, acc.Mapped, string(ex.Data[account.UsernameKey]))) - case err != nil && !apierrors.IsNotFound(err): - return apierrors.NewInternalError(fmt.Errorf("read account Secret %s: %w", account.SecretName(release, acc.Mapped), err)) + for _, t := range templates.Items { + if t.Annotations[account.ReleaseNameAnnotation] == hr.Name { + return apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf( + "release %s is suspended, and deleting it would leave the database running after its account Secrets are gone; resume the release first", hr.Name)) } } - owner := store.OwnerReference(cur) - if _, err := store.Ensure(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), added, &owner, metav1.Now(), actor(ctx)); err != nil { - return apierrors.NewInternalError(fmt.Errorf("seed account Secrets: %w", err)) - } return nil } - -// pruneAfterUpdate deletes the account Secrets of the accounts the update -// removed, once the HelmRelease write has dropped them from the chart. -func (r *REST) pruneAfterUpdate(ctx context.Context, app *appsv1alpha1.Application, dryRun bool) { - if r.credentials == nil || dryRun { - return - } - // Prune by the accounts the live release declares now, not the ones this - // request sent: a concurrent update can have re-added an account and written - // it back, and its Secret is referenced by the live release, so deleting it - // by this request's older list would break the release. Read uncached, and - // prune nothing when the release or its accounts cannot be resolved. - cur := &helmv2.HelmRelease{} - switch err := r.direct().Get(ctx, client.ObjectKey{Namespace: app.Namespace, Name: r.storeApp(app.Namespace, app.Name).Release}, cur); { - case apierrors.IsNotFound(err): - return - case err != nil: - klog.Warningf("failed to read the live HelmRelease of %s/%s before pruning, pruning none: %v", app.Namespace, app.Name, err) - return - } - keep, err := r.accountsOf(cur.Spec.Values) - if err != nil { - klog.Warningf("accounts of the live %s/%s cannot be resolved, pruning none: %v", app.Namespace, app.Name, err) - return - } - if _, err := store.Prune(ctx, r.direct(), r.storeApp(app.Namespace, app.Name), keep); err != nil { - klog.Errorf("failed to prune account Secrets of %s/%s: %v", app.Namespace, app.Name, err) - warning.AddWarning(ctx, "", fmt.Sprintf("account Secrets of removed users were not all deleted: %v", err)) - } -} - -// refuseDeleteSuspended refuses to delete a converted application whose -// release is suspended: Flux skips the uninstall of a suspended release, so -// the database would keep running with its last passwords after the account -// Secrets are gone with the HelmRelease. -func (r *REST) refuseDeleteSuspended(name string, hr *helmv2.HelmRelease) error { - if r.credentials == nil || !hr.Spec.Suspend { - return nil - } - return apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf( - "release %s is suspended, and deleting it would leave the database running after its account Secrets are gone; resume the release first", hr.Name)) -} - -// warnMintedPasswords warns about a password set in the values of a converted -// kind: the chart never reads it, since every password is issued by a mint. -func (r *REST) warnMintedPasswords(ctx context.Context, app *appsv1alpha1.Application) { - if r.credentials == nil || app == nil || app.Spec == nil || len(app.Spec.Raw) == 0 { - return - } - var values map[string]any - if err := json.Unmarshal(app.Spec.Raw, &values); err != nil { - return - } - release := r.releaseConfig.Prefix + app.Name - for _, path := range account.ValuesMapPaths(r.credentials) { - users, _ := account.Lookup(values, path).(map[string]any) - for _, user := range sortedKeys(users) { - u, _ := users[user].(map[string]any) - if _, present := u["password"]; !present { - continue - } - mapped, err := account.MapName(user) - if err != nil { - continue - } - warning.AddWarning(ctx, "", fmt.Sprintf( - "spec.%s[%q].password is ignored: passwords are issued by a mint on the credential %s and cannot be set from values.", - path, user, account.CredentialName(release, mapped))) - } - } -} diff --git a/pkg/registry/apps/application/rest_credentials_race_test.go b/pkg/registry/apps/application/rest_credentials_race_test.go deleted file mode 100644 index a879ca82b..000000000 --- a/pkg/registry/apps/application/rest_credentials_race_test.go +++ /dev/null @@ -1,138 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 - -package application - -import ( - "context" - "fmt" - "sync" - "sync/atomic" - "testing" - - helmv2 "github.com/fluxcd/helm-controller/api/v2" - corev1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - "sigs.k8s.io/controller-runtime/pkg/client" - "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/client/interceptor" - - appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" - "github.com/cozystack/cozystack/pkg/config" -) - -type roleKey struct{} - -func ctxRole(ctx context.Context, role string) context.Context { - return context.WithValue(ctx, roleKey{}, role) -} - -// TestConcurrentCreateLoserSeededLiveSecret exercises the mirror image of -// TestConcurrentCreateKeepsLiveSecret: the create that LOSES the HelmRelease -// race is the one that seeded the Secret which becomes live, and the WINNER -// adopts that very Secret. The loser's recorded UID therefore equals the live -// Secret's UID, so a guard that keyed on Secret identity alone would let the -// loser delete the live release's account Secret; unseed must skip because the -// release exists. -// -// Global schedule enforced by the gates below: -// 1. W seeds its account Secrets (S_W). -// 2. L passes the "HR absent" check, replaces S_W with S_L (its own UID). -// 3. W creates the HelmRelease (wins) and adopts S_L. -// 4. L's HelmRelease create fails AlreadyExists; L.unseed runs. -// 5. L.unseed finds the release live, so it deletes nothing. -func TestConcurrentCreateLoserSeededLiveSecret(t *testing.T) { - scheme := runtime.NewScheme() - if err := helmv2.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - if err := corev1.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - cfg := &config.Resource{ - Application: config.ApplicationConfig{Kind: postgresKind, Singular: "postgres", Plural: "postgreses", OpenAPISchema: credentialsSchema}, - Release: config.ReleaseConfig{Prefix: "postgres-"}, - Credentials: credentialsBlock(), - } - if err := appsv1alpha1.RegisterDynamicTypes(scheme, &config.ResourceConfig{Resources: []config.Resource{*cfg}}); err != nil { - t.Fatal(err) - } - - wSeeded := make(chan struct{}) - lSeeded := make(chan struct{}) - wHRDone := make(chan struct{}) - - var uids atomic.Int64 - assign := func(obj client.Object) { - if obj.GetUID() == "" { - obj.SetUID(types.UID(fmt.Sprintf("uid-%d", uids.Add(1)))) - } - } - - c := fake.NewClientBuilder().WithScheme(scheme).WithInterceptorFuncs(interceptor.Funcs{ - Create: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.CreateOption) error { - role, _ := ctx.Value(roleKey{}).(string) - if _, isHR := obj.(*helmv2.HelmRelease); isHR { - switch role { - case "W": - close(wSeeded) // W has finished seeding S_W - <-lSeeded // wait until L has replaced S_W with S_L - assign(obj) - err := cl.Create(ctx, obj, opts...) - close(wHRDone) // W has won the release - return err - case "L": - close(lSeeded) // L has finished its replace - <-wHRDone // wait until W owns the release - assign(obj) - return cl.Create(ctx, obj, opts...) // AlreadyExists - } - } - assign(obj) - return cl.Create(ctx, obj, opts...) - }, - Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { - if obj.GetUID() == "" { - cur := obj.DeepCopyObject().(client.Object) - if err := cl.Get(ctx, client.ObjectKeyFromObject(obj), cur); err == nil { - obj.SetUID(cur.GetUID()) - } - } - return cl.Update(ctx, obj, opts...) - }, - }).Build() - r := NewREST(c, c, cfg) - - var wg sync.WaitGroup - wg.Add(2) - - go func() { // winner - defer wg.Done() - if _, err := r.Create(ctxRole(credentialsCtx(), "W"), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Errorf("winner create: %v", err) - } - }() - - <-wSeeded // W seeded before L starts, so L's Ensure sees W's Secret as the leftover to replace - - go func() { // loser - defer wg.Done() - _, err := r.Create(ctxRole(credentialsCtx(), "L"), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}) - if !apierrors.IsAlreadyExists(err) { - t.Errorf("loser create: %v, want AlreadyExists", err) - } - }() - - wg.Wait() - - hr := getRelease(t, c) - sec := accountSecrets(t, c)["postgres-orders.web.account"] - if sec == nil { - t.Fatalf("the live release %s lost its account Secret: the losing create deleted the Secret it had seeded and the winner adopted", hr.Name) - } - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != hr.UID { - t.Fatalf("surviving Secret owners = %v, want the live release", sec.OwnerReferences) - } -} diff --git a/pkg/registry/apps/application/rest_credentials_test.go b/pkg/registry/apps/application/rest_credentials_test.go index bf769ae23..4cc8d42bf 100644 --- a/pkg/registry/apps/application/rest_credentials_test.go +++ b/pkg/registry/apps/application/rest_credentials_test.go @@ -4,592 +4,105 @@ package application import ( "context" - "fmt" - "strings" - "sync/atomic" "testing" helmv2 "github.com/fluxcd/helm-controller/api/v2" - coordinationv1 "k8s.io/api/coordination/v1" - corev1 "k8s.io/api/core/v1" - apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/types" - "k8s.io/apiserver/pkg/authentication/user" + "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/endpoints/request" - "k8s.io/apiserver/pkg/warning" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/client/fake" - "sigs.k8s.io/controller-runtime/pkg/client/interceptor" cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" "github.com/cozystack/cozystack/pkg/config" "github.com/cozystack/cozystack/pkg/credentials/account" - "github.com/cozystack/cozystack/pkg/credentials/store" ) -// The schema defaults backup.enabled to true, so the Static backup account -// is seeded unless the application turns it off. -const credentialsSchema = `{"type":"object","properties":{"backup":{"type":"object","default":{},"properties":{"enabled":{"type":"boolean","default":true}}},"users":{"type":"object","additionalProperties":{"type":"object","properties":{"password":{"type":"string"}}}}}}` - -func credentialsBlock() *cozyv1alpha1.ApplicationDefinitionCredentials { - return &cozyv1alpha1.ApplicationDefinitionCredentials{ - Format: cozyv1alpha1.CredentialFormat{Name: "scram-sha-256"}, - ValuesPath: "_accounts", - Accounts: []cozyv1alpha1.CredentialAccountSource{ - {Kind: cozyv1alpha1.CredentialAccountsValuesMap, Path: "users"}, - {Kind: cozyv1alpha1.CredentialAccountsStatic, Name: "backup", When: "backup.enabled"}, - {Kind: cozyv1alpha1.CredentialAccountsStatic, Name: "postgres", Owner: cozyv1alpha1.CredentialAccountOwnerPlatform}, - }, - } -} - -func newCredentialsREST(t *testing.T, objects ...client.Object) (*REST, client.Client) { +func newCredentialsTestREST(t *testing.T, objs ...client.Object) (*REST, client.Client) { t.Helper() scheme := runtime.NewScheme() if err := helmv2.AddToScheme(scheme); err != nil { t.Fatal(err) } - if err := corev1.AddToScheme(scheme); err != nil { + if err := cozyv1alpha1.AddToScheme(scheme); err != nil { t.Fatal(err) } - if err := coordinationv1.AddToScheme(scheme); err != nil { + resourceCfg := &config.ResourceConfig{Resources: []config.Resource{{Application: config.ApplicationConfig{Kind: "Postgres"}}}} + if err := appsv1alpha1.RegisterDynamicTypes(scheme, resourceCfg); err != nil { t.Fatal(err) } - cfg := &config.Resource{ - Application: config.ApplicationConfig{Kind: postgresKind, Singular: "postgres", Plural: "postgreses", OpenAPISchema: credentialsSchema}, - Release: config.ReleaseConfig{Prefix: "postgres-"}, - Credentials: credentialsBlock(), - } - if err := appsv1alpha1.RegisterDynamicTypes(scheme, &config.ResourceConfig{Resources: []config.Resource{*cfg}}); err != nil { - t.Fatal(err) - } - // The fake client assigns no UIDs, and without them every ownership check - // compares two empty strings and passes. - var uids atomic.Int64 - c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objects...).WithInterceptorFuncs(interceptor.Funcs{ - Create: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.CreateOption) error { - if obj.GetUID() == "" { - obj.SetUID(types.UID(fmt.Sprintf("uid-%d", uids.Add(1)))) - } - return cl.Create(ctx, obj, opts...) - }, - // The API server keeps an object's UID on an update that sends none; - // the fake client would blank it. - Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { - if obj.GetUID() == "" { - cur := obj.DeepCopyObject().(client.Object) - if err := cl.Get(ctx, client.ObjectKeyFromObject(obj), cur); err == nil { - obj.SetUID(cur.GetUID()) - } - } - return cl.Update(ctx, obj, opts...) - }, - }).Build() - return NewREST(c, c, cfg), c + c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(objs...).Build() + return &REST{ + c: c, + gvr: schema.GroupVersionResource{Group: appsv1alpha1.GroupName, Version: "v1alpha1", Resource: "postgreses"}, + gvk: schema.GroupVersionKind{Group: appsv1alpha1.GroupName, Version: "v1alpha1", Kind: "Postgres"}, + kindName: "Postgres", + releaseConfig: config.ReleaseConfig{Prefix: "postgres-"}, + }, c } -func credentialsCtx() context.Context { - return request.WithUser(request.WithNamespace(context.Background(), "tenant-foo"), &user.DefaultInfo{Name: "jane@example.org"}) +func accountTemplate(release string) *cozyv1alpha1.CredentialTemplate { + return &cozyv1alpha1.CredentialTemplate{ObjectMeta: metav1.ObjectMeta{ + Name: release + ".web", Namespace: "tenant-foo", + Annotations: map[string]string{account.ReleaseNameAnnotation: release}, + }} } -func postgresApp(spec string) *appsv1alpha1.Application { - return &appsv1alpha1.Application{ - TypeMeta: metav1.TypeMeta{APIVersion: "apps.cozystack.io/v1alpha1", Kind: postgresKind}, - ObjectMeta: metav1.ObjectMeta{Name: "orders", Namespace: "tenant-foo"}, - Spec: &apiextv1.JSON{Raw: []byte(spec)}, +// TestUpdate_KeepsAccountValuesFromAndTheSeedMark pins that an Application +// write leaves the CredentialTemplate controller's state on the release: the +// valuesFrom entries that carry each account's stored value into the chart, +// and the mark that lets the controller seed. The rebuilt release lists only +// the platform values, so dropping the entries would take the accounts out of +// the chart until the controller put them back. +func TestUpdate_KeepsAccountValuesFromAndTheSeedMark(t *testing.T) { + hr := suspendTestHelmRelease(false) + hr.Annotations = map[string]string{account.SeedAnnotation: "true"} + entry := account.ValuesFrom("postgres-pg.web.account", "_accounts", "web") + hr.Spec.ValuesFrom = []helmv2.ValuesReference{{Kind: "Secret", Name: "cozystack-values"}, entry} + r, c := newCredentialsTestREST(t, hr) + + ctx := request.WithNamespace(context.Background(), "tenant-foo") + if _, _, err := r.Update(ctx, "pg", newDefaultUpdatedObjectInfo(suspendTestApplication()), nil, nil, false, &metav1.UpdateOptions{}); err != nil { + t.Fatal(err) + } + got := &helmv2.HelmRelease{} + if err := c.Get(ctx, client.ObjectKey{Namespace: "tenant-foo", Name: "postgres-pg"}, got); err != nil { + t.Fatal(err) + } + if len(got.Spec.ValuesFrom) != 2 || got.Spec.ValuesFrom[1] != entry { + t.Errorf("valuesFrom = %+v, want the platform values and the account entry", got.Spec.ValuesFrom) + } + if got.Annotations[account.SeedAnnotation] != "true" { + t.Error("the seed mark was dropped") } } -func accountSecrets(t *testing.T, c client.Client) map[string]*corev1.Secret { - t.Helper() - list := &corev1.SecretList{} - if err := c.List(context.Background(), list, client.InNamespace("tenant-foo"), client.MatchingLabels{account.AccountLabel: "true"}); err != nil { - t.Fatal(err) - } - out := map[string]*corev1.Secret{} - for i := range list.Items { - out[list.Items[i].Name] = &list.Items[i] - } - return out -} - -func getRelease(t *testing.T, c client.Client) *helmv2.HelmRelease { - t.Helper() +func TestMarkForSeeding(t *testing.T) { hr := &helmv2.HelmRelease{} - if err := c.Get(context.Background(), types.NamespacedName{Namespace: "tenant-foo", Name: "postgres-orders"}, hr); err != nil { - t.Fatal(err) - } - return hr -} - -func TestCreateSeedsAccountsAndOwnsThem(t *testing.T) { - r, c := newCredentialsREST(t) - if _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web":{},"postgres":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - hr := getRelease(t, c) - secrets := accountSecrets(t, c) - // web from the values, backup from the defaulted flag; postgres is the - // platform's and gets no account Secret. - if len(secrets) != 2 || secrets["postgres-orders.web.account"] == nil || secrets["postgres-orders.backup.account"] == nil { - t.Fatalf("secrets = %v", keys(secrets)) - } - for name, sec := range secrets { - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].Name != hr.Name || sec.OwnerReferences[0].Kind != helmv2.HelmReleaseKind { - t.Fatalf("%s owners = %v", name, sec.OwnerReferences) - } - if account.ReadRecord(sec).By != "jane@example.org" { - t.Fatalf("%s record = %+v", name, account.ReadRecord(sec)) - } - } - if len(hr.Spec.ValuesFrom) != 3 || hr.Spec.ValuesFrom[0].Name != "cozystack-values" || hr.Spec.ValuesFrom[1].Name != "postgres-orders.web.account" || !strings.HasPrefix(hr.Spec.ValuesFrom[1].TargetPath, "_accounts.u") { - t.Fatalf("valuesFrom = %+v", hr.Spec.ValuesFrom) + markForSeeding(hr) + if hr.Annotations[account.SeedAnnotation] != "true" { + t.Errorf("annotations = %v", hr.Annotations) } } -func TestCreateRefusesCollidingNames(t *testing.T) { - r, c := newCredentialsREST(t) - _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web_app":{},"web-app":{}}}`), nil, &metav1.CreateOptions{}) - if !apierrors.IsInvalid(err) || !strings.Contains(err.Error(), `both map to the name "web-app"`) { - t.Fatalf("expected Invalid naming the collision, got %v", err) - } - if n := len(accountSecrets(t, c)); n != 0 { - t.Fatalf("a refused write seeds nothing, got %d Secrets", n) +// TestDelete_RefusesASuspendedReleaseWithAccounts: Flux skips the uninstall of +// a suspended release, so deleting it would leave the database running after +// its account Secrets went with the templates. +func TestDelete_RefusesASuspendedReleaseWithAccounts(t *testing.T) { + r, _ := newCredentialsTestREST(t, suspendTestHelmRelease(true), accountTemplate("postgres-pg")) + ctx := request.WithNamespace(context.Background(), "tenant-foo") + if _, _, err := r.Delete(ctx, "pg", nil, &metav1.DeleteOptions{}); !apierrors.IsConflict(err) { + t.Fatalf("err = %v, want a conflict", err) } } -func TestCreateFailureRemovesSeeded(t *testing.T) { - existing := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "postgres-orders", Namespace: "tenant-foo"}} - r, c := newCredentialsREST(t, existing) - if _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); !apierrors.IsAlreadyExists(err) { - t.Fatalf("expected AlreadyExists, got %v", err) - } - if n := len(accountSecrets(t, c)); n != 0 { - t.Fatalf("Secrets seeded for a release that was not created must go, got %d", n) - } -} - -func TestUpdateSeedsAddedAndPrunesRemoved(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{},"api":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - webBefore := accountSecrets(t, c)["postgres-orders.web.account"] - - updated := postgresApp(`{"users":{"web":{},"reports":{}},"backup":{"enabled":false}}`) - if _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(updated), nil, nil, false, &metav1.UpdateOptions{}); err != nil { - t.Fatal(err) - } - secrets := accountSecrets(t, c) - if len(secrets) != 2 || secrets["postgres-orders.web.account"] == nil || secrets["postgres-orders.reports.account"] == nil { - t.Fatalf("secrets = %v, want web kept, reports added, api and backup pruned", keys(secrets)) - } - if string(secrets["postgres-orders.web.account"].Data[account.PasswordKey]) != string(webBefore.Data[account.PasswordKey]) { - t.Fatal("a kept account's Secret must survive the update untouched") - } - hr := getRelease(t, c) - reports := secrets["postgres-orders.reports.account"] - if len(reports.OwnerReferences) != 1 || reports.OwnerReferences[0].UID != hr.UID { - t.Fatalf("added account's owner = %v", reports.OwnerReferences) - } - if len(hr.Spec.ValuesFrom) != 3 { - t.Fatalf("valuesFrom = %+v", hr.Spec.ValuesFrom) - } -} - -func TestUpdateRefusesACaseRename(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - before := accountSecrets(t, c)["postgres-orders.web.account"] - - updated := postgresApp(`{"users":{"Web":{}}}`) - _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(updated), nil, nil, false, &metav1.UpdateOptions{}) - if !apierrors.IsInvalid(err) || !strings.Contains(err.Error(), "already belongs to account") { - t.Fatalf("expected Invalid for a case-only rename, got %v", err) - } - after := accountSecrets(t, c)["postgres-orders.web.account"] - if after == nil || after.UID != before.UID || string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { - t.Fatal("a refused rename must leave the live account's Secret untouched") - } -} - -func TestPruneAfterUpdatePrunesByTheLiveRelease(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{},"api":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - // The live release still declares web and api; a stale request whose app no - // longer lists api must not prune api, because a concurrent update may have - // written it back and the live release references it. - r.pruneAfterUpdate(ctx, postgresApp(`{"users":{"web":{}}}`), false) - if accountSecrets(t, c)["postgres-orders.api.account"] == nil { - t.Fatal("prune deleted a Secret the live release still declares") - } - // Once the live release drops api, pruning removes its Secret. - hr := getRelease(t, c) - hr.Spec.Values = &apiextv1.JSON{Raw: []byte(`{"users":{"web":{}}}`)} - if err := c.Update(ctx, hr); err != nil { - t.Fatal(err) - } - r.pruneAfterUpdate(ctx, postgresApp(`{"users":{"web":{}}}`), false) - if accountSecrets(t, c)["postgres-orders.api.account"] != nil { - t.Fatal("an account the live release no longer declares must be pruned") - } -} - -func TestDeleteRefusesSuspendedRelease(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - hr := getRelease(t, c) - hr.Spec.Suspend = true - if err := c.Update(ctx, hr); err != nil { - t.Fatal(err) - } - _, _, err := r.Delete(ctx, "orders", nil, &metav1.DeleteOptions{}) - if !apierrors.IsConflict(err) || !strings.Contains(err.Error(), "suspended") { - t.Fatalf("expected Conflict on a suspended release, got %v", err) - } - hr = getRelease(t, c) - hr.Spec.Suspend = false - if err := c.Update(ctx, hr); err != nil { - t.Fatal(err) - } - if _, _, err := r.Delete(ctx, "orders", nil, &metav1.DeleteOptions{}); err != nil { - t.Fatal(err) - } -} - -func TestWarnMintedPasswords(t *testing.T) { - r, _ := newCredentialsREST(t) - rec := &fakeWarningRecorder{} - ctx := warning.WithWarningRecorder(credentialsCtx(), rec) - if _, err := r.Create(ctx, postgresApp(`{"users":{"Web":{"password":"hackme"},"api":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - if len(rec.warnings) != 1 || !strings.Contains(rec.warnings[0], `spec.users["Web"].password is ignored`) || !strings.Contains(rec.warnings[0], "postgres-orders.web") { - t.Fatalf("warnings = %v", rec.warnings) - } -} - -func keys(m map[string]*corev1.Secret) []string { - var out []string - for k := range m { - out = append(out, k) - } - return out -} - -func TestCreateDuplicateLeavesLiveSecretsAlone(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - before := accountSecrets(t, c)["postgres-orders.web.account"] - - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); !apierrors.IsAlreadyExists(err) { - t.Fatalf("second create: %v, want AlreadyExists", err) - } - after := accountSecrets(t, c)["postgres-orders.web.account"] - if after == nil || after.UID != before.UID || string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { - t.Fatal("a refused create must leave the live application's Secret untouched") - } -} - -func TestCreateReplacesAnUnownedLeftover(t *testing.T) { - // Left by an earlier create whose owner was never set. - leftover, err := store.SeedSecret(store.App{Namespace: "tenant-foo", Release: "postgres-orders", Group: appsv1alpha1.GroupName, Kind: postgresKind, Name: "orders", Block: credentialsBlock()}, - account.Account{Username: "web", Mapped: "web"}, nil, metav1.Now(), "earlier") - if err != nil { - t.Fatal(err) - } - leftover.UID = "uid-leftover" - r, c := newCredentialsREST(t, leftover) - if _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - sec := accountSecrets(t, c)["postgres-orders.web.account"] - hr := getRelease(t, c) - if sec.UID == "uid-leftover" || string(sec.Data[account.PasswordKey]) == string(leftover.Data[account.PasswordKey]) { - t.Fatal("a leftover must be replaced, so the account gets a new identity") - } - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != hr.UID { - t.Fatalf("owners = %v, want the new release", sec.OwnerReferences) - } -} - -func TestDryRunWritesNoAccountSecrets(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - dry := []string{metav1.DryRunAll} - - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{DryRun: dry}); err != nil { - t.Fatal(err) - } - if n := len(accountSecrets(t, c)); n != 0 { - t.Fatalf("a dry-run create wrote %d account Secrets", n) - } - - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - before := accountSecrets(t, c) - updated := postgresApp(`{"users":{"api":{}}}`) - if _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(updated), nil, nil, false, &metav1.UpdateOptions{DryRun: dry}); err != nil { - t.Fatal(err) - } - after := accountSecrets(t, c) - if len(after) != len(before) || after["postgres-orders.web.account"] == nil || after["postgres-orders.api.account"] != nil { - t.Fatalf("a dry-run update changed the account Secrets: %v -> %v", keys(before), keys(after)) - } -} - -// An account the release already declared and that has no Secret, as after a -// conversion that left the release out, is not seeded by an unrelated write: -// a seed would replace its working password with a discarded one. -func TestUpdateSeedsOnlyAddedAccounts(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}},"backup":{"enabled":false}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - if err := c.Delete(ctx, accountSecrets(t, c)["postgres-orders.web.account"]); err != nil { - t.Fatal(err) - } - updated := postgresApp(`{"users":{"web":{},"api":{}},"backup":{"enabled":false},"size":"20Gi"}`) - if _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(updated), nil, nil, false, &metav1.UpdateOptions{}); err != nil { - t.Fatal(err) - } - got := accountSecrets(t, c) - if got["postgres-orders.api.account"] == nil || got["postgres-orders.web.account"] != nil { - t.Fatalf("secrets = %v, want api seeded and web left alone", keys(got)) - } -} - -// Two creates of one application race. The interceptor runs the second create -// in full at the moment the first is about to write its HelmRelease: the -// second seeds over the first's unowned Secret, creates the release, and -// adopts the Secret. The first then fails AlreadyExists, and its cleanup must -// leave the Secret the live release now owns. Without the release check and -// the UID check in unseed the live release ends with no account Secret. -func TestConcurrentCreateKeepsLiveSecret(t *testing.T) { - scheme := runtime.NewScheme() - if err := helmv2.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - if err := corev1.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - cfg := &config.Resource{ - Application: config.ApplicationConfig{Kind: postgresKind, Singular: "postgres", Plural: "postgreses", OpenAPISchema: credentialsSchema}, - Release: config.ReleaseConfig{Prefix: "postgres-"}, - Credentials: credentialsBlock(), - } - if err := appsv1alpha1.RegisterDynamicTypes(scheme, &config.ResourceConfig{Resources: []config.Resource{*cfg}}); err != nil { - t.Fatal(err) - } - - var r *REST - var raced atomic.Bool - var uids atomic.Int64 - c := fake.NewClientBuilder().WithScheme(scheme).WithInterceptorFuncs(interceptor.Funcs{ - Create: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.CreateOption) error { - // When the first create is about to write its HelmRelease, let a - // second create of the same application run to completion first, so - // it wins the release and adopts the account Secret the first one - // seeded. - if _, ok := obj.(*helmv2.HelmRelease); ok && raced.CompareAndSwap(false, true) { - if _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - return fmt.Errorf("concurrent create: %w", err) - } - } - if obj.GetUID() == "" { - obj.SetUID(types.UID(fmt.Sprintf("uid-%d", uids.Add(1)))) - } - return cl.Create(ctx, obj, opts...) - }, - Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { - if obj.GetUID() == "" { - cur := obj.DeepCopyObject().(client.Object) - if err := cl.Get(ctx, client.ObjectKeyFromObject(obj), cur); err == nil { - obj.SetUID(cur.GetUID()) - } - } - return cl.Update(ctx, obj, opts...) - }, - }).Build() - r = NewREST(c, c, cfg) - - // The create that loses the race. - if _, err := r.Create(credentialsCtx(), postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); !apierrors.IsAlreadyExists(err) { - t.Fatalf("the create that lost the race: %v, want AlreadyExists", err) - } - - hr := getRelease(t, c) - sec := accountSecrets(t, c)["postgres-orders.web.account"] - if sec == nil { - t.Fatal("the live release lost its account Secret to the losing create's cleanup") - } - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != hr.UID { - t.Fatalf("surviving Secret owners = %v, want the live release", sec.OwnerReferences) - } -} - -// A concurrent create of the same application can replace a seeded Secret and -// win the release, but not yet have adopted the replacement when the losing -// create's cleanup runs: the replacement is still unowned. unseed must leave -// it, because its UID is not the one this create seeded. Deleting by name, or -// only skipping an owned Secret, would take it and leave the live release with -// no account Secret. -// A concurrent write to one application's credentials is refused while another -// holds the lock, so the two cannot interleave into deleting a Secret the other -// reused. Request A adds api; while A holds the lock and writes its release, the -// interceptor runs request B (which would remove web and prune its Secret). B -// is refused with Conflict, so web's Secret is untouched. -func TestConcurrentUpdateRefusedByCredentialLock(t *testing.T) { - scheme := runtime.NewScheme() - if err := helmv2.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - if err := corev1.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - if err := coordinationv1.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - cfg := &config.Resource{ - Application: config.ApplicationConfig{Kind: postgresKind, Singular: "postgres", Plural: "postgreses", OpenAPISchema: credentialsSchema}, - Release: config.ReleaseConfig{Prefix: "postgres-"}, - Credentials: credentialsBlock(), - } - if err := appsv1alpha1.RegisterDynamicTypes(scheme, &config.ResourceConfig{Resources: []config.Resource{*cfg}}); err != nil { - t.Fatal(err) - } - - var r *REST - var raced atomic.Bool - var bErr error - var uids atomic.Int64 - c := fake.NewClientBuilder().WithScheme(scheme).WithInterceptorFuncs(interceptor.Funcs{ - Create: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.CreateOption) error { - if obj.GetUID() == "" { - obj.SetUID(types.UID(fmt.Sprintf("uid-%d", uids.Add(1)))) - } - return cl.Create(ctx, obj, opts...) - }, - Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { - // Request A is writing its HelmRelease, so A holds the lock: a second - // update of the same application must be refused now. - if _, ok := obj.(*helmv2.HelmRelease); ok && raced.CompareAndSwap(false, true) { - _, _, bErr = r.Update(credentialsCtx(), "orders", newDefaultUpdatedObjectInfo(postgresApp(`{"users":{}}`)), nil, nil, false, &metav1.UpdateOptions{}) - } - if obj.GetUID() == "" { - cur := obj.DeepCopyObject().(client.Object) - if err := cl.Get(ctx, client.ObjectKeyFromObject(obj), cur); err == nil { - obj.SetUID(cur.GetUID()) - } - } - return cl.Update(ctx, obj, opts...) - }, - }).Build() - r = NewREST(c, c, cfg) - ctx := credentialsCtx() - - if _, err := r.Create(ctx, postgresApp(`{"users":{"web":{}}}`), nil, &metav1.CreateOptions{}); err != nil { - t.Fatal(err) - } - webBefore := accountSecrets(t, c)["postgres-orders.web.account"] - - if _, _, err := r.Update(ctx, "orders", newDefaultUpdatedObjectInfo(postgresApp(`{"users":{"web":{},"api":{}}}`)), nil, nil, false, &metav1.UpdateOptions{}); err != nil { - t.Fatalf("request A: %v", err) - } - if !apierrors.IsConflict(bErr) { - t.Fatalf("a concurrent credential write must be refused with Conflict, got %v", bErr) - } - after := accountSecrets(t, c)["postgres-orders.web.account"] - if after == nil || after.UID != webBefore.UID { - t.Fatal("the refused concurrent update must not have pruned web's Secret") - } - if accountSecrets(t, c)["postgres-orders.api.account"] == nil { - t.Fatal("request A must have seeded api") - } -} - -func TestUnseedLeavesAReplacedSecret(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - const name = "postgres-orders.web.account" - replacement := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-foo", UID: "uid-winner", - Labels: map[string]string{account.AccountLabel: "true"}}, - } - if err := c.Create(ctx, replacement); err != nil { - t.Fatal(err) - } - // This create seeded an object that the winner has since replaced, so the - // UID it recorded is gone and the live Secret carries the winner's UID. No - // release exists here, so the UID guard alone must keep the replacement. - r.unseed(ctx, "tenant-foo", "postgres-orders", []store.Seeded{{Name: name, UID: "uid-loser"}}) - sec := accountSecrets(t, c)[name] - if sec == nil || sec.UID != "uid-winner" { - t.Fatalf("unseed deleted a Secret this create did not seed: %+v", sec) - } -} - -// With no HelmRelease for the application, a failed create's seed is a genuine -// orphan and unseed deletes the exact object it wrote. -func TestUnseedDeletesOrphanWhenNoRelease(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - const name = "postgres-orders.web.account" - orphan := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-foo", UID: "uid-mine", - Labels: map[string]string{account.AccountLabel: "true"}}, - } - if err := c.Create(ctx, orphan); err != nil { - t.Fatal(err) - } - r.unseed(ctx, "tenant-foo", "postgres-orders", []store.Seeded{{Name: name, UID: "uid-mine"}}) - if accountSecrets(t, c)[name] != nil { - t.Fatal("unseed must delete its own orphaned seed when no release exists") - } -} - -// Once a HelmRelease for the application exists, a concurrent create won it and -// owns the account Secrets, even one it adopted that this create had seeded, so -// unseed deletes nothing although the UID still matches. -func TestUnseedLeavesSecretsWhenReleaseExists(t *testing.T) { - r, c := newCredentialsREST(t) - ctx := credentialsCtx() - const name = "postgres-orders.web.account" - hr := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "postgres-orders", Namespace: "tenant-foo", UID: "hr-winner"}} - if err := c.Create(ctx, hr); err != nil { - t.Fatal(err) - } - adopted := &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: "tenant-foo", UID: "uid-seeded", - Labels: map[string]string{account.AccountLabel: "true"}, - OwnerReferences: []metav1.OwnerReference{{APIVersion: helmv2.GroupVersion.String(), Kind: helmv2.HelmReleaseKind, Name: "postgres-orders", UID: "hr-winner"}}}, - } - if err := c.Create(ctx, adopted); err != nil { - t.Fatal(err) - } - // This create seeded the object the winner then adopted, so the UID matches; - // the live release is what must keep unseed from deleting it. - r.unseed(ctx, "tenant-foo", "postgres-orders", []store.Seeded{{Name: name, UID: "uid-seeded"}}) - if accountSecrets(t, c)[name] == nil { - t.Fatal("unseed deleted an account Secret while the application's release exists") +func TestDelete_AllowsASuspendedReleaseWithoutAccounts(t *testing.T) { + r, _ := newCredentialsTestREST(t, suspendTestHelmRelease(true), accountTemplate("postgres-other")) + ctx := request.WithNamespace(context.Background(), "tenant-foo") + if _, _, err := r.Delete(ctx, "pg", nil, &metav1.DeleteOptions{}); apierrors.IsConflict(err) { + t.Fatalf("err = %v, a release without accounts must delete", err) } } diff --git a/pkg/credentials/store/event.go b/pkg/registry/core/credential/event.go similarity index 98% rename from pkg/credentials/store/event.go rename to pkg/registry/core/credential/event.go index 482532091..ad54c47b7 100644 --- a/pkg/credentials/store/event.go +++ b/pkg/registry/core/credential/event.go @@ -1,6 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 -package store +package credential import ( "context" diff --git a/pkg/registry/core/credential/rest.go b/pkg/registry/core/credential/rest.go index 090eac360..0126818fb 100644 --- a/pkg/registry/core/credential/rest.go +++ b/pkg/registry/core/credential/rest.go @@ -1,9 +1,10 @@ // SPDX-License-Identifier: Apache-2.0 // Package credential serves core.cozystack.io/credentials, a view over the -// account Secrets the API writes, with the mint and revoke subresources. The -// view shows the account's record and never the Secret's data. See -// design-proposals/user-secrets-api in cozystack/community. +// CredentialTemplates a kind's chart renders, with the mint and revoke +// subresources. The view shows the account's record as the template's status +// mirrors it, and never reads a Secret. See design-proposals/user-secrets-api +// in cozystack/community. package credential import ( @@ -13,14 +14,11 @@ import ( "time" helmv2 "github.com/fluxcd/helm-controller/api/v2" - corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" metainternal "k8s.io/apimachinery/pkg/apis/meta/internalversion" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" - "k8s.io/apimachinery/pkg/selection" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/duration" "k8s.io/apiserver/pkg/endpoints/request" @@ -50,29 +48,18 @@ const ( kindList = "CredentialList" ) -// Kind is what the registry knows about one converted application kind: its -// credentials block, and how to resolve the accounts a HelmRelease declares, -// which the application registry does with the kind's schema defaults. -type Kind struct { - Block *cozyv1alpha1.ApplicationDefinitionCredentials - Accounts func(ctx context.Context, hr *helmv2.HelmRelease) ([]account.Account, error) -} - // REST serves the credentials view. type REST struct { c client.Client // direct reads and writes for mint and revoke, which the cached client - // would answer with a Secret written a moment ago missing. + // would answer with a template status a moment old. direct client.Client gvr schema.GroupVersionResource - // kinds is keyed by the application kind the HelmRelease labels name. - kinds map[string]Kind - now func() time.Time + now func() time.Time } -// NewREST creates the credentials storage. kinds carries every converted -// application kind; a HelmRelease of a kind absent from it has no credentials. -func NewREST(c, direct client.Client, kinds map[string]Kind) *REST { +// NewREST creates the credentials storage. +func NewREST(c, direct client.Client) *REST { return &REST{ c: c, direct: direct, @@ -81,8 +68,7 @@ func NewREST(c, direct client.Client, kinds map[string]Kind) *REST { Version: "v1alpha1", Resource: "credentials", }, - kinds: kinds, - now: time.Now, + now: time.Now, } } @@ -104,82 +90,84 @@ func nsFrom(ctx context.Context) (string, error) { return ns, nil } -// resolved is one account the registry has located: its HelmRelease, its -// kind, and its Secret where one exists. +// resolved is one account the registry has located: its template and the +// HelmRelease that rendered it. type resolved struct { - hr *helmv2.HelmRelease - kind Kind - mapped string - secret *corev1.Secret - // stale is a Secret at the account's name that is not the application's - // own, left from a deleted application of the same name; a mint replaces - // it rather than reusing it. - stale *corev1.Secret + tmpl *cozyv1alpha1.CredentialTemplate + hr *helmv2.HelmRelease } -// resolve locates the account a Credential name points at. The HelmRelease -// must exist and belong to a converted kind, or the credential does not exist. -// A Secret that is not the application's own, by marker or by owner, counts -// as absent. +// resolve locates the account a Credential name points at. The template must +// exist and the HelmRelease that rendered it must be live, or the credential +// does not exist. func (r *REST) resolve(ctx context.Context, c client.Reader, namespace, name string) (*resolved, error) { - release, mapped, ok := account.SplitCredentialName(name) - if !ok { - return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name) - } - hr := &helmv2.HelmRelease{} - if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: release}, hr); err != nil { + tmpl := &cozyv1alpha1.CredentialTemplate{} + if err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: name}, tmpl); err != nil { if apierrors.IsNotFound(err) { return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name) } return nil, err } - kind, ok := r.kinds[hr.Labels[appsv1alpha1.ApplicationKindLabel]] - if !ok || kind.Block == nil { - return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name) - } - res := &resolved{hr: hr, kind: kind, mapped: mapped} - sec := &corev1.Secret{} - err := c.Get(ctx, types.NamespacedName{Namespace: namespace, Name: account.SecretName(release, mapped)}, sec) - switch { - case err == nil && ownedBy(sec, hr): - res.secret = sec - case err == nil: - res.stale = sec - case !apierrors.IsNotFound(err): + hr, err := r.release(ctx, c, tmpl, nil) + if err != nil { return nil, err } - return res, nil + if hr == nil { + return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name) + } + return &resolved{tmpl: tmpl, hr: hr}, nil } -// ownedBy reports whether a Secret is an account Secret owned by the -// HelmRelease, which is what makes it the application's own. -func ownedBy(sec *corev1.Secret, hr *helmv2.HelmRelease) bool { - if !account.IsAccountSecret(sec) { - return false +// release returns the live HelmRelease that rendered a template, nil when the +// template names none or its release is gone. cache saves a list from reading +// one release per template. +func (r *REST) release(ctx context.Context, c client.Reader, tmpl *cozyv1alpha1.CredentialTemplate, cache map[string]*helmv2.HelmRelease) (*helmv2.HelmRelease, error) { + name := tmpl.Annotations[account.ReleaseNameAnnotation] + if name == "" || !tmpl.DeletionTimestamp.IsZero() { + return nil, nil } - for _, ref := range sec.OwnerReferences { - if ref.UID == hr.UID { - return true + if hr, seen := cache[name]; seen { + return hr, nil + } + hr := &helmv2.HelmRelease{} + if err := c.Get(ctx, types.NamespacedName{Namespace: tmpl.Namespace, Name: name}, hr); err != nil { + if !apierrors.IsNotFound(err) { + return nil, err } + hr = nil } - return false + if cache != nil { + cache[name] = hr + } + return hr, nil } -// toCredential builds the view of one account Secret. -func toCredential(sec *corev1.Secret, hr *helmv2.HelmRelease) corev1alpha1.Credential { - rec := account.ReadRecord(sec) - _, mapped, _ := account.SplitCredentialName(sec.Name[:len(sec.Name)-len(account.SecretSuffix)]) +// toCredential builds the view of one account from its template. +func toCredential(tmpl *cozyv1alpha1.CredentialTemplate, hr *helmv2.HelmRelease) corev1alpha1.Credential { + st := tmpl.Status + origin := corev1alpha1.CredentialOrigin(st.Password.Origin) + if origin == "" { + origin = corev1alpha1.CredentialOriginNotIssued + } + pw := corev1alpha1.CredentialPasswordState{Origin: origin, By: st.Password.By} + if st.Password.At != nil { + pw.At = *st.Password.At + } + var history []corev1alpha1.CredentialHistoryEntry + for _, h := range st.History { + history = append(history, corev1alpha1.CredentialHistoryEntry{Operation: corev1alpha1.CredentialOperation(h.Operation), At: h.At, By: h.By}) + } return corev1alpha1.Credential{ TypeMeta: metav1.TypeMeta{ APIVersion: corev1alpha1.SchemeGroupVersion.String(), Kind: kindCredential, }, ObjectMeta: metav1.ObjectMeta{ - Name: account.CredentialName(hr.Name, mapped), - Namespace: sec.Namespace, - UID: sec.UID, - ResourceVersion: sec.ResourceVersion, - CreationTimestamp: sec.CreationTimestamp, + Name: tmpl.Name, + Namespace: tmpl.Namespace, + UID: tmpl.UID, + ResourceVersion: st.SecretResourceVersion, + CreationTimestamp: tmpl.CreationTimestamp, }, Spec: corev1alpha1.CredentialSpec{ Application: corev1alpha1.CredentialApplicationRef{ @@ -187,12 +175,9 @@ func toCredential(sec *corev1.Secret, hr *helmv2.HelmRelease) corev1alpha1.Crede Kind: hr.Labels[appsv1alpha1.ApplicationKindLabel], Name: hr.Labels[appsv1alpha1.ApplicationNameLabel], }, - User: string(sec.Data[account.UsernameKey]), - }, - Status: corev1alpha1.CredentialStatus{ - Password: corev1alpha1.CredentialPasswordState{Origin: rec.Origin, At: rec.At, By: rec.By}, - History: rec.History, + User: tmpl.Spec.Username, }, + Status: corev1alpha1.CredentialStatus{Password: pw, History: history}, } } @@ -205,10 +190,7 @@ func (r *REST) Get(ctx context.Context, name string, _ *metav1.GetOptions) (runt if err != nil { return nil, err } - if res.secret == nil { - return nil, apierrors.NewNotFound(r.gvr.GroupResource(), name) - } - cred := toCredential(res.secret, res.hr) + cred := toCredential(res.tmpl, res.hr) return &cred, nil } @@ -230,37 +212,29 @@ func (r *REST) List(ctx context.Context, opts *metainternal.ListOptions) (runtim if fieldFilter.Namespace != "" && fieldFilter.Namespace != ns { return out, nil } - - sel := labels.NewSelector() - req, _ := labels.NewRequirement(account.AccountLabel, selection.Equals, []string{"true"}) - sel = sel.Add(*req) + listOpts := &client.ListOptions{Namespace: ns} if opts.LabelSelector != nil { - reqs, selectable := opts.LabelSelector.Requirements() - if !selectable { + if _, selectable := opts.LabelSelector.Requirements(); !selectable { return out, nil } - sel = sel.Add(reqs...) + listOpts.LabelSelector = opts.LabelSelector } - list := &corev1.SecretList{} - if err := r.c.List(ctx, list, &client.ListOptions{Namespace: ns, LabelSelector: sel}); err != nil { + list := &cozyv1alpha1.CredentialTemplateList{} + if err := r.c.List(ctx, list, listOpts); err != nil { return nil, err } releases := map[string]*helmv2.HelmRelease{} for i := range list.Items { - sec := &list.Items[i] - hr, err := r.liveOwner(ctx, sec, releases) + tmpl := &list.Items[i] + hr, err := r.release(ctx, r.c, tmpl, releases) if err != nil { return nil, err } - if hr == nil { + if hr == nil || !fieldFilter.MatchesName(tmpl.Name) { continue } - cred := toCredential(sec, hr) - if !fieldFilter.MatchesName(cred.Name) { - continue - } - out.Items = append(out.Items, cred) + out.Items = append(out.Items, toCredential(tmpl, hr)) } listRV := list.ResourceVersion if listRV == "" { @@ -271,36 +245,6 @@ func (r *REST) List(ctx context.Context, opts *metainternal.ListOptions) (runtim return out, nil } -// liveOwner returns the HelmRelease that owns an account Secret, provided it -// exists and belongs to a converted kind; nil otherwise, so a Secret no live -// release owns is not listed. -func (r *REST) liveOwner(ctx context.Context, sec *corev1.Secret, cache map[string]*helmv2.HelmRelease) (*helmv2.HelmRelease, error) { - for _, ref := range sec.OwnerReferences { - if ref.Kind != helmv2.HelmReleaseKind { - continue - } - hr, seen := cache[ref.Name] - if !seen { - hr = &helmv2.HelmRelease{} - if err := r.c.Get(ctx, types.NamespacedName{Namespace: sec.Namespace, Name: ref.Name}, hr); err != nil { - if !apierrors.IsNotFound(err) { - return nil, err - } - hr = nil - } - cache[ref.Name] = hr - } - if hr == nil || hr.UID != ref.UID { - continue - } - if kind, ok := r.kinds[hr.Labels[appsv1alpha1.ApplicationKindLabel]]; !ok || kind.Block == nil { - continue - } - return hr, nil - } - return nil, nil -} - func (r *REST) ConvertToTable(ctx context.Context, object runtime.Object, _ runtime.Object) (*metav1.Table, error) { table := &metav1.Table{ ColumnDefinitions: []metav1.TableColumnDefinition{ diff --git a/pkg/registry/core/credential/rest_test.go b/pkg/registry/core/credential/rest_test.go index 95e4d19a7..cce84140b 100644 --- a/pkg/registry/core/credential/rest_test.go +++ b/pkg/registry/core/credential/rest_test.go @@ -5,6 +5,8 @@ package credential import ( "bytes" "context" + "crypto/sha256" + "encoding/hex" "flag" "io" "strings" @@ -29,88 +31,107 @@ import ( appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" "github.com/cozystack/cozystack/pkg/credentials/account" - "github.com/cozystack/cozystack/pkg/credentials/store" ) -const ns = "tenant-foo" +const ( + ns = "tenant-foo" + release = "clickhouse-logs" + cred = release + ".web" +) -func testBlock() *cozyv1alpha1.ApplicationDefinitionCredentials { - return &cozyv1alpha1.ApplicationDefinitionCredentials{ - Format: cozyv1alpha1.CredentialFormat{Name: "sha256"}, - Accounts: []cozyv1alpha1.CredentialAccountSource{ - {Kind: cozyv1alpha1.CredentialAccountsValuesMap, Path: "users"}, - }, - } -} - -// fixture is a fake cluster with one converted Postgres application whose -// declared accounts are fixed by the test. +// fixture is a fake cluster with one application release and the templates +// its chart rendered. writes counts every write a request makes, so a test +// can hold the API to one object per request. type fixture struct { - c client.Client - r *REST - hr *helmv2.HelmRelease - ctx context.Context - declare []account.Account + c client.Client + r *REST + ctx context.Context + writes []string } func newFixture(t *testing.T, objs ...client.Object) *fixture { - t.Helper() - return newFixtureWith(t, nil, objs...) -} - -// newFixtureWith builds the fixture on a client whose calls go through funcs. -func newFixtureWith(t *testing.T, funcs *interceptor.Funcs, objs ...client.Object) *fixture { t.Helper() scheme := runtime.NewScheme() - if err := corev1.AddToScheme(scheme); err != nil { - t.Fatal(err) - } - if err := helmv2.AddToScheme(scheme); err != nil { - t.Fatal(err) + for _, add := range []func(*runtime.Scheme) error{corev1.AddToScheme, helmv2.AddToScheme, cozyv1alpha1.AddToScheme} { + if err := add(scheme); err != nil { + t.Fatal(err) + } } hr := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{ - Name: "postgres-orders", Namespace: ns, UID: "hr-uid", + Name: release, Namespace: ns, UID: "hr-uid", Labels: map[string]string{ appsv1alpha1.ApplicationGroupLabel: appsv1alpha1.GroupName, - appsv1alpha1.ApplicationKindLabel: "Postgres", - appsv1alpha1.ApplicationNameLabel: "orders", + appsv1alpha1.ApplicationKindLabel: "ClickHouse", + appsv1alpha1.ApplicationNameLabel: "logs", }, }} - f := &fixture{hr: hr} - b := fake.NewClientBuilder().WithScheme(scheme).WithObjects(append(objs, hr)...) - if funcs != nil { - b = b.WithInterceptorFuncs(*funcs) + f := &fixture{} + record := func(verb string) func(ctx context.Context, c client.WithWatch, obj client.Object, _ ...any) { + return func(_ context.Context, _ client.WithWatch, obj client.Object, _ ...any) { + if _, isEvent := obj.(*corev1.Event); !isEvent { + f.writes = append(f.writes, verb+" "+obj.GetName()) + } + } } - c := b.Build() + c := fake.NewClientBuilder().WithScheme(scheme). + WithObjects(append(objs, hr)...). + WithStatusSubresource(&cozyv1alpha1.CredentialTemplate{}). + WithInterceptorFuncs(interceptor.Funcs{ + Create: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.CreateOption) error { + record("create")(ctx, c, obj) + return c.Create(ctx, obj, opts...) + }, + Update: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { + record("update")(ctx, c, obj) + return c.Update(ctx, obj, opts...) + }, + Delete: func(ctx context.Context, c client.WithWatch, obj client.Object, opts ...client.DeleteOption) error { + record("delete")(ctx, c, obj) + return c.Delete(ctx, obj, opts...) + }, + }).Build() f.c = c - f.r = NewREST(c, c, map[string]Kind{"Postgres": { - Block: testBlock(), - Accounts: func(context.Context, *helmv2.HelmRelease) ([]account.Account, error) { return f.declare, nil }, - }}) + f.r = NewREST(c, c) f.r.now = func() time.Time { return time.Date(2026, 10, 2, 9, 14, 7, 0, time.UTC) } f.ctx = request.WithUser(request.WithNamespace(context.Background(), ns), &user.DefaultInfo{Name: "jane@example.org"}) return f } -func (f *fixture) app() store.App { - return store.App{Namespace: ns, Release: "postgres-orders", Group: appsv1alpha1.GroupName, Kind: "Postgres", Name: "orders", Block: testBlock()} +func template(name, username string) *cozyv1alpha1.CredentialTemplate { + return &cozyv1alpha1.CredentialTemplate{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, Namespace: ns, UID: types.UID("uid-" + name), + Annotations: map[string]string{account.ReleaseNameAnnotation: release}, + }, + Spec: cozyv1alpha1.CredentialTemplateSpec{ + Username: username, + Format: cozyv1alpha1.CredentialTemplateFormat{Name: "sha256"}, + Secret: cozyv1alpha1.CredentialTemplateSecret{Labels: map[string]string{"reconcile.fluxcd.io/watch": "Enabled"}}, + }, + } } -// seed declares user and writes its owned account Secret, and returns the -// Secret as stored. -func (f *fixture) seed(t *testing.T, username string) *corev1.Secret { +// seeded returns a template with its Secret, as the controller leaves them: +// the Secret seeded and the template's status mirroring its version. +func (f *fixture) seeded(t *testing.T, name, username string) (*cozyv1alpha1.CredentialTemplate, *corev1.Secret) { t.Helper() - owner := store.OwnerReference(f.hr) - mapped, _ := account.MapName(username) - f.declare = append(f.declare, account.Account{Username: username, Mapped: mapped}) - sec, err := store.SeedSecret(f.app(), account.Account{Username: username, Mapped: mapped}, &owner, metav1.NewTime(f.r.now()), "seeder") - if err != nil { + tmpl := template(name, username) + if err := f.c.Create(f.ctx, tmpl); err != nil { t.Fatal(err) } + at := metav1.NewTime(f.r.now()) + sec := buildSecret(tmpl, "seed", account.Record{}.Apply(corev1alpha1.CredentialOperationCreate, at, "controller")) if err := f.c.Create(f.ctx, sec); err != nil { t.Fatal(err) } - return f.secret(t, sec.Name) + tmpl.Status.SecretResourceVersion = sec.ResourceVersion + tmpl.Status.Password = cozyv1alpha1.CredentialTemplatePasswordState{Origin: "NotIssued", By: "controller", At: &at} + tmpl.Status.History = []cozyv1alpha1.CredentialTemplateHistoryEntry{{Operation: "Create", At: at, By: "controller"}} + if err := f.c.Status().Update(f.ctx, tmpl); err != nil { + t.Fatal(err) + } + f.writes = nil + return tmpl, sec } func (f *fixture) secret(t *testing.T, name string) *corev1.Secret { @@ -122,13 +143,16 @@ func (f *fixture) secret(t *testing.T, name string) *corev1.Secret { return sec } -func (f *fixture) mint(t *testing.T, name, precondition string) (*corev1alpha1.CredentialRequest, error) { - t.Helper() +func (f *fixture) do(op *OperationREST, name, precondition string, dryRun bool) (*corev1alpha1.CredentialRequest, error) { req := &corev1alpha1.CredentialRequest{} if precondition != "" { req.Spec.Preconditions = &corev1alpha1.CredentialRequestPreconditions{ResourceVersion: precondition} } - out, err := NewMintREST(f.r).Create(f.ctx, name, req, nil, &metav1.CreateOptions{}) + opts := &metav1.CreateOptions{} + if dryRun { + opts.DryRun = []string{metav1.DryRunAll} + } + out, err := op.Create(f.ctx, name, req, nil, opts) if err != nil { return nil, err } @@ -144,252 +168,196 @@ func (f *fixture) events(t *testing.T) []corev1.Event { return list.Items } -func TestGetShowsRecordNeverData(t *testing.T) { +func TestGetShowsTheMirroredRecord(t *testing.T) { f := newFixture(t) - sec := f.seed(t, "Web") - - obj, err := f.r.Get(f.ctx, "postgres-orders.web", &metav1.GetOptions{}) + tmpl, sec := f.seeded(t, cred, "web") + obj, err := f.r.Get(f.ctx, cred, nil) if err != nil { t.Fatal(err) } - cred := obj.(*corev1alpha1.Credential) - if cred.Name != "postgres-orders.web" || cred.Spec.User != "Web" || cred.Spec.Application.Name != "orders" || cred.Spec.Application.Kind != "Postgres" { - t.Fatalf("credential = %+v", cred) + c := obj.(*corev1alpha1.Credential) + if c.UID != tmpl.UID || c.ResourceVersion != sec.ResourceVersion { + t.Errorf("uid %q version %q, want the template's uid and the Secret's version", c.UID, c.ResourceVersion) } - if cred.ResourceVersion != sec.ResourceVersion { - t.Fatalf("resourceVersion is the Secret's: %q != %q", cred.ResourceVersion, sec.ResourceVersion) + if c.Spec.User != "web" || c.Spec.Application.Kind != "ClickHouse" || c.Spec.Application.Name != "logs" { + t.Errorf("spec = %+v", c.Spec) } - if cred.Status.Password.Origin != corev1alpha1.CredentialOriginNotIssued || cred.Status.Password.By != "seeder" { - t.Fatalf("status = %+v", cred.Status) - } - table, err := f.r.ConvertToTable(f.ctx, cred, nil) - if err != nil { - t.Fatal(err) - } - if len(table.Rows) != 1 || table.Rows[0].Cells[1] != "Postgres/orders" || table.Rows[0].Cells[3] != "NotIssued" { - t.Fatalf("table = %+v", table.Rows) - } - for _, name := range []string{"postgres-orders.nobody", "postgres-orders", "postgres-orders.web.account"} { - if _, err := f.r.Get(f.ctx, name, &metav1.GetOptions{}); !apierrors.IsNotFound(err) { - t.Errorf("Get(%q) = %v, want NotFound", name, err) - } + if c.Status.Password.Origin != corev1alpha1.CredentialOriginNotIssued || len(c.Status.History) != 1 { + t.Errorf("status = %+v", c.Status) } } -func TestListSkipsSecretsNoLiveReleaseOwns(t *testing.T) { - gone := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "postgres-gone", Namespace: ns, UID: "gone-uid"}} - f := newFixture(t) - f.seed(t, "web") - // Owned by a release that no longer exists. - orphan, _ := store.SeedSecret(store.App{Namespace: ns, Release: "postgres-gone", Group: appsv1alpha1.GroupName, Kind: "Postgres", Name: "gone", Block: testBlock()}, account.Account{Username: "web", Mapped: "web"}, ptr(store.OwnerReference(gone)), metav1.Now(), "x") - if err := f.c.Create(f.ctx, orphan); err != nil { - t.Fatal(err) +func TestGetAndListSkipATemplateWithoutALiveRelease(t *testing.T) { + orphan := template(release+"-gone.web", "web") + orphan.Annotations[account.ReleaseNameAnnotation] = release + "-gone" + unrendered := template(release+".api", "api") + delete(unrendered.Annotations, account.ReleaseNameAnnotation) + f := newFixture(t, orphan, unrendered) + f.seeded(t, cred, "web") + for _, name := range []string{orphan.Name, unrendered.Name} { + if _, err := f.r.Get(f.ctx, name, nil); !apierrors.IsNotFound(err) { + t.Errorf("get %s: err = %v, want NotFound", name, err) + } } - // A Secret of an unconverted kind carrying the marker by accident. - other := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "redis-cache", Namespace: ns, UID: "redis-uid", Labels: map[string]string{appsv1alpha1.ApplicationKindLabel: "Redis", appsv1alpha1.ApplicationNameLabel: "cache"}}} - if err := f.c.Create(f.ctx, other); err != nil { - t.Fatal(err) - } - stray, _ := store.SeedSecret(store.App{Namespace: ns, Release: "redis-cache", Kind: "Redis", Name: "cache", Block: testBlock()}, account.Account{Username: "default", Mapped: "default"}, ptr(store.OwnerReference(other)), metav1.Now(), "x") - if err := f.c.Create(f.ctx, stray); err != nil { - t.Fatal(err) - } - obj, err := f.r.List(f.ctx, &metainternal.ListOptions{}) if err != nil { t.Fatal(err) } - list := obj.(*corev1alpha1.CredentialList) - if len(list.Items) != 1 || list.Items[0].Name != "postgres-orders.web" { - t.Fatalf("list = %+v", list.Items) + items := obj.(*corev1alpha1.CredentialList).Items + if len(items) != 1 || items[0].Name != cred { + t.Errorf("list = %v, want only %s", items, cred) } } -func TestMintReplacesPasswordOnceAndRecords(t *testing.T) { - f := newFixture(t) - before := f.seed(t, "web") - - res, err := f.mint(t, "postgres-orders.web", before.ResourceVersion) +func TestListShowsAnAccountWithoutASecretAsNotIssued(t *testing.T) { + f := newFixture(t, template(cred, "web")) + obj, err := f.r.List(f.ctx, &metainternal.ListOptions{}) if err != nil { t.Fatal(err) } - if res.Status.Username != "web" || len(res.Status.Password) != 32 || res.Status.IssuedAt.Time != f.r.now() { - t.Fatalf("response = %+v", res.Status) + items := obj.(*corev1alpha1.CredentialList).Items + if len(items) != 1 || items[0].Status.Password.Origin != corev1alpha1.CredentialOriginNotIssued || items[0].ResourceVersion != "" { + t.Errorf("list = %+v", items) } - after := f.secret(t, before.Name) - if string(after.Data[account.PasswordKey]) == string(before.Data[account.PasswordKey]) { - t.Fatal("stored value unchanged") +} + +func TestMintWritesOneSecretUnderTheMirroredVersion(t *testing.T) { + f := newFixture(t) + tmpl, _ := f.seeded(t, cred, "web") + res, err := f.do(NewMintREST(f.r), cred, tmpl.Status.SecretResourceVersion, false) + if err != nil { + t.Fatal(err) } - if strings.Contains(string(after.Data[account.PasswordKey]), res.Status.Password) { - t.Fatal("the plaintext must not be stored") + if len(f.writes) != 1 || f.writes[0] != "update "+cred+account.SecretSuffix { + t.Errorf("writes = %v, want one update of the account Secret", f.writes) } - rec := account.ReadRecord(after) - if rec.Origin != corev1alpha1.CredentialOriginMinted || rec.By != "jane@example.org" || len(rec.History) != 2 || rec.History[0].Operation != corev1alpha1.CredentialOperationMint { - t.Fatalf("record = %+v", rec) + if len(res.Status.Password) != 32 || res.Status.Username != "web" { + t.Fatalf("answer = %+v", res.Status) + } + sec := f.secret(t, cred+account.SecretSuffix) + sum := sha256.Sum256([]byte(res.Status.Password)) + if string(sec.Data[account.PasswordKey]) != hex.EncodeToString(sum[:]) { + t.Error("the Secret does not hold the verifier of the minted password") + } + if sec.Labels["reconcile.fluxcd.io/watch"] != "Enabled" || sec.Labels[account.AccountLabel] != "true" { + t.Errorf("labels = %v", sec.Labels) + } + if ref := metav1.GetControllerOf(sec); ref == nil || ref.UID != tmpl.UID { + t.Errorf("controller = %v, want the template", ref) + } + rec := account.ReadRecord(sec) + if rec.Origin != corev1alpha1.CredentialOriginMinted || rec.By != "jane@example.org" || len(rec.History) != 2 { + t.Errorf("record = %+v, want Minted by the caller on top of the mirrored history", rec) } evs := f.events(t) - if len(evs) != 1 || evs[0].Reason != store.ReasonCredentialIssued || evs[0].InvolvedObject.Kind != "Postgres" || evs[0].InvolvedObject.Name != "orders" { - t.Fatalf("events = %+v", evs) + if len(evs) != 1 || evs[0].Reason != ReasonCredentialIssued { + t.Fatalf("events = %v", evs) } - if evs[0].Message != "password of user web issued by jane@example.org" || strings.Contains(evs[0].Message, res.Status.Password) { - t.Fatalf("event message = %q", evs[0].Message) + if strings.Contains(evs[0].Message, res.Status.Password) { + t.Error("the event carries the password") } +} - // The stale precondition is the one from before the mint: conflict, - // naming who issued in between, and nothing written. - _, err = f.mint(t, "postgres-orders.web", before.ResourceVersion) - if !apierrors.IsConflict(err) || !strings.Contains(err.Error(), "password issued by jane@example.org at 2026-10-02T09:14:07Z") { - t.Fatalf("expected conflict naming the issuer, got %v", err) - } - if string(f.secret(t, before.Name).Data[account.PasswordKey]) != string(after.Data[account.PasswordKey]) { - t.Fatal("a refused mint must not write") - } - // No precondition: always proceeds. - if _, err := f.mint(t, "postgres-orders.web", ""); err != nil { +func TestMintCreatesTheSecretOfAnAccountThatHasNone(t *testing.T) { + f := newFixture(t, template(cred, "web")) + res, err := f.do(NewMintREST(f.r), cred, "", false) + if err != nil { t.Fatal(err) } + if len(f.writes) != 1 || f.writes[0] != "create "+cred+account.SecretSuffix { + t.Errorf("writes = %v, want one create of the account Secret", f.writes) + } + if res.Status.Password == "" { + t.Error("no password") + } +} + +func TestMintConflictsWhenTheStatusLagsTheSecret(t *testing.T) { + f := newFixture(t) + tmpl, sec := f.seeded(t, cred, "web") + // Somebody minted after the status was mirrored. + sec.Data[account.PasswordKey] = []byte("other") + if err := f.c.Update(f.ctx, sec); err != nil { + t.Fatal(err) + } + before := f.secret(t, sec.Name) + f.writes = nil + res, err := f.do(NewMintREST(f.r), cred, "", false) + if !apierrors.IsConflict(err) || res != nil { + t.Fatalf("err = %v, answer = %v, want a conflict and no password", err, res) + } + if after := f.secret(t, sec.Name); string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { + t.Error("the Secret was overwritten") + } + _ = tmpl +} + +func TestMintConflictsOnAStalePrecondition(t *testing.T) { + f := newFixture(t) + f.seeded(t, cred, "web") + if _, err := f.do(NewMintREST(f.r), cred, "999999", false); !apierrors.IsConflict(err) { + t.Fatalf("err = %v, want a conflict", err) + } + if len(f.writes) != 0 { + t.Errorf("writes = %v, want none", f.writes) + } +} + +func TestMintRefusesAConflictingSecretAndAnUnknownFormat(t *testing.T) { + conflict := template(cred, "web") + conflict.Status.Conditions = []metav1.Condition{{Type: cozyv1alpha1.CredentialTemplateSecretConflict, Status: metav1.ConditionTrue, Reason: "ForeignSecret", LastTransitionTime: metav1.Now()}} + unknown := template(release+".api", "api") + unknown.Spec.Format.Name = "apr1" + f := newFixture(t, conflict, unknown) + for _, name := range []string{conflict.Name, unknown.Name} { + if _, err := f.do(NewMintREST(f.r), name, "", false); !apierrors.IsConflict(err) { + t.Errorf("mint %s: err = %v, want a conflict", name, err) + } + } + if len(f.writes) != 0 { + t.Errorf("writes = %v, want none", f.writes) + } +} + +func TestMintOnAMissingTemplateIsNotFound(t *testing.T) { + f := newFixture(t) + if _, err := f.do(NewMintREST(f.r), release+".nobody", "", false); !apierrors.IsNotFound(err) { + t.Fatalf("err = %v, want NotFound", err) + } } func TestRevokeReturnsNoPassword(t *testing.T) { f := newFixture(t) - before := f.seed(t, "web") - out, err := NewRevokeREST(f.r).Create(f.ctx, "postgres-orders.web", &corev1alpha1.CredentialRequest{}, nil, &metav1.CreateOptions{}) + f.seeded(t, cred, "web") + res, err := f.do(NewRevokeREST(f.r), cred, "", false) if err != nil { t.Fatal(err) } - res := out.(*corev1alpha1.CredentialRequest) - if res.Status.Password != "" || res.Status.Username != "web" { - t.Fatalf("response = %+v", res.Status) + if res.Status.Password != "" { + t.Error("a revoke returned a password") } - after := f.secret(t, before.Name) - if string(after.Data[account.PasswordKey]) == string(before.Data[account.PasswordKey]) { - t.Fatal("revoke must replace the stored value") + if rec := account.ReadRecord(f.secret(t, cred+account.SecretSuffix)); rec.Origin != corev1alpha1.CredentialOriginRevoked { + t.Errorf("origin = %s, want Revoked", rec.Origin) } - if rec := account.ReadRecord(after); rec.Origin != corev1alpha1.CredentialOriginRevoked { - t.Fatalf("record = %+v", rec) - } - if evs := f.events(t); len(evs) != 1 || evs[0].Reason != store.ReasonCredentialRevoked { - t.Fatalf("events = %+v", evs) - } -} - -func TestMintCreatesMissingDeclaredAccount(t *testing.T) { - f := newFixture(t) - f.declare = []account.Account{{Username: "Web", Mapped: "web"}} - - // Not declared: does not exist. - if _, err := f.mint(t, "postgres-orders.api", ""); !apierrors.IsNotFound(err) { - t.Fatalf("undeclared account: %v", err) - } - // A stale Secret at the name, from a deleted application, is replaced. - gone := &helmv2.HelmRelease{ObjectMeta: metav1.ObjectMeta{Name: "postgres-orders", Namespace: ns, UID: "old-uid"}} - stale, _ := store.SeedSecret(f.app(), account.Account{Username: "Web", Mapped: "web"}, ptr(store.OwnerReference(gone)), metav1.Now(), "x") - if err := f.c.Create(f.ctx, stale); err != nil { - t.Fatal(err) - } - if _, err := f.r.Get(f.ctx, "postgres-orders.web", &metav1.GetOptions{}); !apierrors.IsNotFound(err) { - t.Fatalf("a stale Secret must not be served: %v", err) - } - // A precondition on a missing Secret is ignored. - res, err := f.mint(t, "postgres-orders.web", "12345") - if err != nil { - t.Fatal(err) - } - if res.Status.Username != "Web" { - t.Fatalf("username must be the declared one: %+v", res.Status) - } - sec := f.secret(t, "postgres-orders.web.account") - if len(sec.OwnerReferences) != 1 || sec.OwnerReferences[0].UID != f.hr.UID || string(sec.Data[account.UsernameKey]) != "Web" { - t.Fatalf("created Secret = %+v", sec.ObjectMeta) - } - rec := account.ReadRecord(sec) - if rec.Origin != corev1alpha1.CredentialOriginMinted || len(rec.History) != 1 { - t.Fatalf("record = %+v", rec) - } -} - -func TestUnconvertedKindHasNoCredentials(t *testing.T) { - f := newFixture(t) - f.hr.Labels[appsv1alpha1.ApplicationKindLabel] = "Redis" - if err := f.c.Update(f.ctx, f.hr); err != nil { - t.Fatal(err) - } - f.seed(t, "web") - if _, err := f.r.Get(f.ctx, "postgres-orders.web", &metav1.GetOptions{}); !apierrors.IsNotFound(err) { - t.Fatalf("Get = %v", err) - } - if _, err := f.mint(t, "postgres-orders.web", ""); !apierrors.IsNotFound(err) { - t.Fatalf("mint = %v", err) - } -} - -func ptr[T any](v T) *T { return &v } - -func TestMintRefusesAnUndeclaredAccountWithASecret(t *testing.T) { - f := newFixture(t) - before := f.seed(t, "web") - // The user left the values and the prune of its Secret failed. - f.declare = nil - if _, err := f.mint(t, "postgres-orders.web", ""); !apierrors.IsNotFound(err) { - t.Fatalf("mint on an undeclared account: %v, want NotFound", err) - } - // A Secret holding another user than the one the name maps to. - f.declare = []account.Account{{Username: "Web", Mapped: "web"}} - if _, err := f.mint(t, "postgres-orders.web", ""); !apierrors.IsConflict(err) { - t.Fatalf("mint on a Secret of another user: %v, want Conflict", err) - } - if string(f.secret(t, before.Name).Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { - t.Fatal("a refused mint must not write") + if evs := f.events(t); len(evs) != 1 || evs[0].Reason != ReasonCredentialRevoked { + t.Errorf("events = %v", evs) } } func TestDryRunMintAndRevokeWriteNothing(t *testing.T) { f := newFixture(t) - before := f.seed(t, "web") - dry := &metav1.CreateOptions{DryRun: []string{metav1.DryRunAll}} + f.seeded(t, cred, "web") for _, op := range []*OperationREST{NewMintREST(f.r), NewRevokeREST(f.r)} { - out, err := op.Create(f.ctx, "postgres-orders.web", &corev1alpha1.CredentialRequest{}, nil, dry) + res, err := f.do(op, cred, "", true) if err != nil { t.Fatal(err) } - if res := out.(*corev1alpha1.CredentialRequest); res.Status.Password != "" || res.Status.Username != "web" { - t.Fatalf("dry-run response = %+v", res.Status) + if res.Status.Password != "" { + t.Error("a dry run returned a password") } } - after := f.secret(t, before.Name) - if after.ResourceVersion != before.ResourceVersion || string(after.Data[account.PasswordKey]) != string(before.Data[account.PasswordKey]) { - t.Fatal("a dry run changed the account Secret") - } - if evs := f.events(t); len(evs) != 0 { - t.Fatalf("a dry run recorded events: %+v", evs) - } -} - -// Two operations racing on one account cannot both land: the write carries -// the resourceVersion the operation read, so the slower one gets a conflict -// and no password. -func TestMintConflictsOnAConcurrentChange(t *testing.T) { - var raced bool - f := newFixtureWith(t, &interceptor.Funcs{ - Update: func(ctx context.Context, cl client.WithWatch, obj client.Object, opts ...client.UpdateOption) error { - if sec, ok := obj.(*corev1.Secret); ok && !raced { - raced = true - other := &corev1.Secret{} - if err := cl.Get(ctx, client.ObjectKeyFromObject(sec), other); err != nil { - return err - } - other.Annotations["concurrent"] = "write" - if err := cl.Update(ctx, other); err != nil { - return err - } - } - return cl.Update(ctx, obj, opts...) - }, - }) - f.seed(t, "web") - res, err := f.mint(t, "postgres-orders.web", "") - if !apierrors.IsConflict(err) || res != nil { - t.Fatalf("mint racing a concurrent write: res=%v err=%v, want a conflict and no password", res, err) + if len(f.writes) != 0 || len(f.events(t)) != 0 { + t.Errorf("writes = %v, events = %v, want none", f.writes, f.events(t)) } } @@ -407,8 +375,8 @@ func TestMintLogsNoPassword(t *testing.T) { }() f := newFixture(t) - f.seed(t, "web") - res, err := f.mint(t, "postgres-orders.web", "") + f.seeded(t, cred, "web") + res, err := f.do(NewMintREST(f.r), cred, "", false) if err != nil { t.Fatal(err) } diff --git a/pkg/registry/core/credential/subresources.go b/pkg/registry/core/credential/subresources.go index a4f9e2229..8f1158735 100644 --- a/pkg/registry/core/credential/subresources.go +++ b/pkg/registry/core/credential/subresources.go @@ -6,20 +6,20 @@ import ( "context" "fmt" + corev1 "k8s.io/api/core/v1" apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apiserver/pkg/endpoints/request" "k8s.io/apiserver/pkg/registry/rest" "k8s.io/apiserver/pkg/util/dryrun" - "sigs.k8s.io/controller-runtime/pkg/client" - appsv1alpha1 "github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1" + cozyv1alpha1 "github.com/cozystack/cozystack/api/v1alpha1" corev1alpha1 "github.com/cozystack/cozystack/pkg/apis/core/v1alpha1" "github.com/cozystack/cozystack/pkg/credentials/account" "github.com/cozystack/cozystack/pkg/credentials/format" - "github.com/cozystack/cozystack/pkg/credentials/store" ) var ( @@ -29,10 +29,12 @@ var ( ) // OperationREST is the mint or the revoke subresource of a Credential. Both -// take a CredentialRequest, replace the account's stored value with that of a -// fresh random password in one conditional write of the account Secret, and -// answer with a CredentialRequest. A mint returns the password, once; a -// revoke discards it, so nobody holds a working one until the next mint. +// take a CredentialRequest, build the whole account Secret from the template +// with the stored value of a fresh random password, and write it in one +// request: an update under the Secret version the template's status mirrors, +// or a create when the account has no Secret yet. The Secret is never read. A +// mint returns the password, once; a revoke discards it, so nobody holds a +// working one until the next mint. type OperationREST struct { parent *REST op corev1alpha1.CredentialOperation @@ -80,31 +82,28 @@ func (o *OperationREST) Create(ctx context.Context, name string, obj runtime.Obj if err != nil { return nil, err } - // Only a declared account is minted, whether or not its Secret exists: a - // Secret left behind by a removed user, or by values written around the - // API, does not make the account real. - acc, err := o.declared(ctx, res) - if err != nil { - return nil, err - } - if res.secret != nil && string(res.secret.Data[account.UsernameKey]) != acc.Username { + tmpl := res.tmpl + if meta.IsStatusConditionTrue(tmpl.Status.Conditions, cozyv1alpha1.CredentialTemplateSecretConflict) { return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf( - "credential %s holds user %q, the application declares %q; write the application to reseed it", name, res.secret.Data[account.UsernameKey], acc.Username)) + "credential %s has a Secret it did not create; a platform administrator has to remove it first", name)) + } + if !format.Known(tmpl.Spec.Format.Name) { + return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf( + "credential %s names the format %q, which is not built in", name, tmpl.Spec.Format.Name)) } var precondition string if req.Spec.Preconditions != nil { precondition = req.Spec.Preconditions.ResourceVersion } - - // The Secret's version is the Credential's, so a caller that read the - // credential and sends that version back learns, as a conflict, that - // somebody minted in between instead of silently killing the password that - // person just received. A missing Secret takes no precondition. - if res.secret != nil && precondition != "" && precondition != res.secret.ResourceVersion { - rec := account.ReadRecord(res.secret) + mirrored := tmpl.Status.SecretResourceVersion + // The Credential's version is the account Secret's, as the template's + // status mirrors it, so a caller that read the credential and sends that + // version back learns, as a conflict, that somebody minted in between + // instead of silently killing the password that person just received. + if precondition != "" && precondition != mirrored { return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf( - "credential %s changed: password %s by %s at %s", name, describe(rec.Origin), rec.By, rec.At.UTC().Format("2006-01-02T15:04:05Z"))) + "credential %s changed: password %s by %s", name, describe(corev1alpha1.CredentialOrigin(tmpl.Status.Password.Origin)), tmpl.Status.Password.By)) } if options != nil && dryrun.IsDryRun(options.DryRun) { @@ -112,7 +111,7 @@ func (o *OperationREST) Create(ctx context.Context, name string, obj runtime.Obj // that is never stored would not log in. return &corev1alpha1.CredentialRequest{ TypeMeta: metav1.TypeMeta{APIVersion: corev1alpha1.SchemeGroupVersion.String(), Kind: "CredentialRequest"}, - Status: corev1alpha1.CredentialRequestStatus{Username: acc.Username, IssuedAt: metav1.NewTime(r.now())}, + Status: corev1alpha1.CredentialRequestStatus{Username: tmpl.Spec.Username, IssuedAt: metav1.NewTime(r.now())}, }, nil } @@ -120,62 +119,41 @@ func (o *OperationREST) Create(ctx context.Context, name string, obj runtime.Obj if err != nil { return nil, apierrors.NewInternalError(err) } - stored, err := format.Encode(res.kind.Block.Format.Name, password) + stored, err := format.Encode(tmpl.Spec.Format.Name, password) if err != nil { return nil, apierrors.NewInternalError(err) } now := metav1.NewTime(r.now()) by := user.GetName() + sec := buildSecret(tmpl, stored, recordOf(tmpl).Apply(o.op, now, by)) - username := acc.Username - if res.secret != nil { - sec := res.secret.DeepCopy() - sec.Data[account.PasswordKey] = []byte(stored) - account.WriteRecord(sec, account.ReadRecord(sec).Apply(o.op, now, by)) - // The update carries the Secret's own resourceVersion, so two concurrent - // operations cannot both land: the second gets a conflict and no password. - if err := r.direct.Update(ctx, sec); err != nil { - if apierrors.IsConflict(err) { - return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf("credential %s changed while the request was in flight", name)) + // The status is all the API knows of the Secret. A status that lags the + // Secret, or a Secret written since the status was read, makes the write + // fail rather than overwrite it, and the caller gets a conflict and no + // password. + if mirrored == "" { + if err := r.direct.Create(ctx, sec); err != nil { + if apierrors.IsAlreadyExists(err) { + return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf("credential %s is being seeded; retry", name)) } return nil, err } } else { - // A declared account whose Secret is missing, as after a conversion - // that left the release out, gets a Secret with a new identity. - app := store.App{ - Namespace: ns, Release: res.hr.Name, - Group: res.hr.Labels[appsv1alpha1.ApplicationGroupLabel], - Kind: res.hr.Labels[appsv1alpha1.ApplicationKindLabel], - Name: res.hr.Labels[appsv1alpha1.ApplicationNameLabel], - Block: res.kind.Block, - } - if res.stale != nil { - if err := r.direct.Delete(ctx, res.stale, client.Preconditions{UID: &res.stale.UID}); err != nil && !apierrors.IsNotFound(err) { - return nil, err - } - } - owner := store.OwnerReference(res.hr) - sec, err := store.SeedSecret(app, acc, &owner, now, by) - if err != nil { - return nil, apierrors.NewInternalError(err) - } - sec.Data[account.PasswordKey] = []byte(stored) - account.WriteRecord(sec, account.Record{}.Apply(o.op, now, by)) - if err := r.direct.Create(ctx, sec); err != nil { - if apierrors.IsAlreadyExists(err) { - return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf("credential %s was created while the request was in flight", name)) + sec.ResourceVersion = mirrored + if err := r.direct.Update(ctx, sec); err != nil { + if apierrors.IsConflict(err) || apierrors.IsNotFound(err) { + return nil, apierrors.NewConflict(r.gvr.GroupResource(), name, fmt.Errorf("credential %s changed while the request was in flight; retry", name)) } return nil, err } } - reason := store.ReasonCredentialIssued + reason := ReasonCredentialIssued if o.op == corev1alpha1.CredentialOperationRevoke { - reason = store.ReasonCredentialRevoked + reason = ReasonCredentialRevoked password = "" } - store.Emit(ctx, r.direct, res.hr, reason, username, by) + Emit(ctx, r.direct, res.hr, reason, tmpl.Spec.Username, by) return &corev1alpha1.CredentialRequest{ TypeMeta: metav1.TypeMeta{ @@ -183,32 +161,54 @@ func (o *OperationREST) Create(ctx context.Context, name string, obj runtime.Obj Kind: "CredentialRequest", }, Status: corev1alpha1.CredentialRequestStatus{ - Username: username, + Username: tmpl.Spec.Username, Password: password, IssuedAt: now, }, }, nil } -// declared finds the declared account a Credential name maps to. A name no -// declared user maps to is NotFound; one two declared users map to is a -// Conflict, since the write that declared them should have been refused. -func (o *OperationREST) declared(ctx context.Context, res *resolved) (account.Account, error) { - r := o.parent - name := account.CredentialName(res.hr.Name, res.mapped) - if res.kind.Accounts == nil { - return account.Account{}, apierrors.NewNotFound(r.gvr.GroupResource(), name) +// recordOf returns the record the template's status mirrors, which the mint +// extends with its own operation. +func recordOf(tmpl *cozyv1alpha1.CredentialTemplate) account.Record { + st := tmpl.Status + rec := account.Record{Origin: corev1alpha1.CredentialOrigin(st.Password.Origin), By: st.Password.By} + if st.Password.At != nil { + rec.At = *st.Password.At } - accounts, err := res.kind.Accounts(ctx, res.hr) - if err != nil { - return account.Account{}, apierrors.NewConflict(r.gvr.GroupResource(), name, err) + for _, h := range st.History { + rec.History = append(rec.History, corev1alpha1.CredentialHistoryEntry{Operation: corev1alpha1.CredentialOperation(h.Operation), At: h.At, By: h.By}) } - for _, acc := range accounts { - if acc.Mapped == res.mapped { - return acc, nil - } + return rec +} + +// buildSecret builds the whole account Secret of a template, as the +// controller would seed it, with the given stored value and record. +func buildSecret(tmpl *cozyv1alpha1.CredentialTemplate, stored string, rec account.Record) *corev1.Secret { + controller := true + sec := &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: tmpl.Name + account.SecretSuffix, + Namespace: tmpl.Namespace, + Labels: account.Labels(tmpl.Spec.Secret.Labels), + Annotations: map[string]string{account.FormatAnnotation: tmpl.Spec.Format.Name}, + OwnerReferences: []metav1.OwnerReference{{ + APIVersion: cozyv1alpha1.GroupVersion.String(), + Kind: "CredentialTemplate", + Name: tmpl.Name, + UID: tmpl.UID, + Controller: &controller, + BlockOwnerDeletion: &controller, + }}, + }, + Type: corev1.SecretTypeBasicAuth, + Data: map[string][]byte{ + account.UsernameKey: []byte(tmpl.Spec.Username), + account.PasswordKey: []byte(stored), + }, } - return account.Account{}, apierrors.NewNotFound(r.gvr.GroupResource(), name) + account.WriteRecord(sec, rec) + return sec } func describe(origin corev1alpha1.CredentialOrigin) string {