diff --git a/api/v1alpha1/credentialtemplate_types.go b/api/v1alpha1/credentialtemplate_types.go index e23709965..b29e9d479 100644 --- a/api/v1alpha1/credentialtemplate_types.go +++ b/api/v1alpha1/credentialtemplate_types.go @@ -9,6 +9,7 @@ import ( // 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. +// +kubebuilder:validation:XValidation:rule="!has(self.bucketAccess) || self.format.name == 'plaintext'",message="a template with bucketAccess stores its keys in the plaintext format" type CredentialTemplateSpec struct { // Username is the account name as the engine knows it. // +kubebuilder:validation:MinLength=1 @@ -30,6 +31,29 @@ type CredentialTemplateSpec struct { // +optional // +kubebuilder:validation:Pattern=`^_[A-Za-z0-9_]*$` ValuesPath string `json:"valuesPath,omitempty"` + + // BucketAccess makes the account engine-issued: it names the BucketAccess + // whose COSI grant issues the account's keys, which the platform cannot + // choose. cozystack-controller creates and owns it, re-creates it on a + // mint, and deletes it on a revoke. The keys are stored as they are + // issued, so the format is plaintext. + // +optional + BucketAccess *CredentialTemplateBucketAccess `json:"bucketAccess,omitempty"` +} + +// CredentialTemplateBucketAccess is the BucketAccess an engine-issued +// template stands for. Its credentials Secret carries the BucketAccess's own name. +// +kubebuilder:validation:XValidation:rule="self.name == oldSelf.name",message="bucketAccess.name is immutable" +type CredentialTemplateBucketAccess struct { + // Name of the BucketAccess and of the Secret the COSI sidecar writes. + // +kubebuilder:validation:MinLength=1 + Name string `json:"name"` + // BucketClaimName is the BucketClaim the access is granted on. + // +kubebuilder:validation:MinLength=1 + BucketClaimName string `json:"bucketClaimName"` + // BucketAccessClassName is the class that sets the access policy. + // +kubebuilder:validation:MinLength=1 + BucketAccessClassName string `json:"bucketAccessClassName"` } // CredentialTemplateFormat names a stored form of a password. @@ -103,6 +127,10 @@ const ( CredentialTemplateSecretConflict = "SecretConflict" // CredentialTemplateUnknownFormat: the format names no built-in format. CredentialTemplateUnknownFormat = "UnknownFormat" + // CredentialTemplateBucketAccessConflict: a BucketAccess at the name an + // engine-issued template names that the controller did not create or + // adopt; it is left alone. + CredentialTemplateBucketAccessConflict = "BucketAccessConflict" ) // +kubebuilder:object:root=true diff --git a/api/v1alpha1/zz_generated.deepcopy.go b/api/v1alpha1/zz_generated.deepcopy.go index dfb0c233d..5ae6a6462 100644 --- a/api/v1alpha1/zz_generated.deepcopy.go +++ b/api/v1alpha1/zz_generated.deepcopy.go @@ -382,6 +382,21 @@ func (in *CredentialTemplate) DeepCopyObject() runtime.Object { return nil } +// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. +func (in *CredentialTemplateBucketAccess) DeepCopyInto(out *CredentialTemplateBucketAccess) { + *out = *in +} + +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateBucketAccess. +func (in *CredentialTemplateBucketAccess) DeepCopy() *CredentialTemplateBucketAccess { + if in == nil { + return nil + } + out := new(CredentialTemplateBucketAccess) + in.DeepCopyInto(out) + return out +} + // 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 @@ -491,6 +506,11 @@ func (in *CredentialTemplateSpec) DeepCopyInto(out *CredentialTemplateSpec) { *out = *in out.Format = in.Format in.Secret.DeepCopyInto(&out.Secret) + if in.BucketAccess != nil { + in, out := &in.BucketAccess, &out.BucketAccess + *out = new(CredentialTemplateBucketAccess) + **out = **in + } } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new CredentialTemplateSpec. diff --git a/internal/controller/credentialtemplate_crd_test.go b/internal/controller/credentialtemplate_crd_test.go new file mode 100644 index 000000000..9d029f90b --- /dev/null +++ b/internal/controller/credentialtemplate_crd_test.go @@ -0,0 +1,76 @@ +// SPDX-License-Identifier: Apache-2.0 + +package controller + +import ( + "context" + "os" + "testing" + + apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" + schemacel "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/cel" + "k8s.io/apimachinery/pkg/util/validation/field" + celconfig "k8s.io/apiserver/pkg/apis/cel" + "sigs.k8s.io/yaml" +) + +// credentialTemplateCRD is the generated CRD cozystack-controller ships. +// Reading it rather than restating the rule catches a marker that did not +// generate. +const credentialTemplateCRD = "../../packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml" + +// An engine-issued template, one that names a BucketAccess, stores the keys +// as they are issued, so its format must be plaintext; a template without a +// BucketAccess may use any format. +func TestCredentialTemplateCRDTiesBucketAccessToPlaintext(t *testing.T) { + raw, err := os.ReadFile(credentialTemplateCRD) + if err != nil { + t.Fatal(err) + } + var crd apiextensionsv1.CustomResourceDefinition + if err := yaml.Unmarshal(raw, &crd); err != nil { + t.Fatal(err) + } + var spec *apiextensionsv1.JSONSchemaProps + for i := range crd.Spec.Versions { + if v := crd.Spec.Versions[i]; v.Name == "v1alpha1" && v.Schema != nil && v.Schema.OpenAPIV3Schema != nil { + if p, ok := v.Schema.OpenAPIV3Schema.Properties["spec"]; ok { + spec = &p + } + } + } + if spec == nil || len(spec.XValidations) == 0 { + t.Fatal("v1alpha1 spec schema with x-kubernetes-validations not found in the CRD") + } + var internal apiextensions.JSONSchemaProps + if err := apiextensionsv1.Convert_v1_JSONSchemaProps_To_apiextensions_JSONSchemaProps(spec, &internal, nil); err != nil { + t.Fatal(err) + } + structural, err := schema.NewStructural(&internal) + if err != nil { + t.Fatal(err) + } + validator := schemacel.NewValidator(structural, true, celconfig.PerCallLimit) + + access := map[string]any{"name": "bucket-photos-web", "bucketClaimName": "bucket-photos", "bucketAccessClassName": "seaweedfs"} + for _, tc := range []struct { + name string + spec map[string]any + accept bool + }{ + {"bucketAccess with plaintext", map[string]any{"username": "web", "format": map[string]any{"name": "plaintext"}, "bucketAccess": access}, true}, + {"bucketAccess with another format", map[string]any{"username": "web", "format": map[string]any{"name": "scram-sha-256"}, "bucketAccess": access}, false}, + {"bucketAccess with a format named cosi", map[string]any{"username": "web", "format": map[string]any{"name": "cosi"}, "bucketAccess": access}, false}, + {"plaintext without bucketAccess", map[string]any{"username": "web", "format": map[string]any{"name": "plaintext"}}, true}, + {"another format without bucketAccess", map[string]any{"username": "web", "format": map[string]any{"name": "sha256"}}, true}, + } { + t.Run(tc.name, func(t *testing.T) { + errs, _ := validator.Validate(context.TODO(), field.NewPath("spec"), structural, tc.spec, nil, celconfig.RuntimeCELCostBudget) + if accepted := len(errs) == 0; accepted != tc.accept { + t.Errorf("accepted = %v, want %v: %v", accepted, tc.accept, errs) + } + }) + } +} diff --git a/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml b/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml index b56c24c73..e662b8fbf 100644 --- a/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml +++ b/packages/system/cozystack-controller/definitions/cozystack.io_credentialtemplates.yaml @@ -59,6 +59,37 @@ spec: Secret that stores its password. A kind's chart renders one per account; a tenant cannot write it. properties: + bucketAccess: + description: |- + BucketAccess makes the account engine-issued: it names the BucketAccess + whose COSI grant issues the account's keys, which the platform cannot + choose. cozystack-controller creates and owns it, re-creates it on a + mint, and deletes it on a revoke. The keys are stored as they are + issued, so the format is plaintext. + properties: + bucketAccessClassName: + description: BucketAccessClassName is the class that sets the + access policy. + minLength: 1 + type: string + bucketClaimName: + description: BucketClaimName is the BucketClaim the access is + granted on. + minLength: 1 + type: string + name: + description: Name of the BucketAccess and of the Secret the COSI + sidecar writes. + minLength: 1 + type: string + required: + - bucketAccessClassName + - bucketClaimName + - name + type: object + x-kubernetes-validations: + - message: bucketAccess.name is immutable + rule: self.name == oldSelf.name format: description: Format is how a password is stored in the account Secret. properties: @@ -106,6 +137,10 @@ spec: - format - username type: object + x-kubernetes-validations: + - message: a template with bucketAccess stores its keys in the plaintext + format + rule: '!has(self.bucketAccess) || self.format.name == ''plaintext''' status: description: |- CredentialTemplateStatus mirrors the record the account Secret keeps, so the diff --git a/pkg/apis/core/v1alpha1/credential_types.go b/pkg/apis/core/v1alpha1/credential_types.go index 26a0b31ce..af2525f51 100644 --- a/pkg/apis/core/v1alpha1/credential_types.go +++ b/pkg/apis/core/v1alpha1/credential_types.go @@ -136,6 +136,11 @@ type CredentialRequestStatus struct { // shown once. // +optional Password string `json:"password,omitempty"` + // AccessKeyID is the access key that goes with Password, for an account + // whose engine issues a key pair (a template with bucketAccess); present + // in a mint's answer only and shown once. + // +optional + AccessKeyID string `json:"accessKeyID,omitempty"` // IssuedAt is when the operation took effect in the record. IssuedAt metav1.Time `json:"issuedAt,omitempty"` } diff --git a/pkg/generated/openapi/zz_generated.openapi.go b/pkg/generated/openapi/zz_generated.openapi.go index 511e8bf8b..6de2f7f33 100644 --- a/pkg/generated/openapi/zz_generated.openapi.go +++ b/pkg/generated/openapi/zz_generated.openapi.go @@ -635,6 +635,13 @@ func schema_pkg_apis_core_v1alpha1_CredentialRequestStatus(ref common.ReferenceC Format: "", }, }, + "accessKeyID": { + SchemaProps: spec.SchemaProps{ + Description: "AccessKeyID is the access key that goes with Password, for an account whose engine issues a key pair (a template with bucketAccess); present in a mint's answer only and shown once.", + Type: []string{"string"}, + Format: "", + }, + }, "issuedAt": { SchemaProps: spec.SchemaProps{ Description: "IssuedAt is when the operation took effect in the record.",