feat(api): describe engine-issued keys on CredentialTemplate

A Bucket user's keys come from the COSI driver, which accepts no
caller-chosen secret, so the platform cannot choose them the way it
chooses a password. The optional bucketAccess field lets a template name
the BucketAccess whose grant issues the keys, which makes the account
engine-issued whatever its format. The keys are stored as they are
issued, so a CEL rule requires the plaintext format on such a template;
a format name of its own would say who issues the secret, which
bucketAccess already says, and would stop format meaning only how a
password is stored. A mint can return the access key that goes with the
secret key through one optional field on CredentialRequestStatus.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil committed 2026-12-02 20:38:26 +01:00
1 parent 94807d93f3
commit 08cb839283
6 files changed
+171

No files matched your search

+28
View File
@@ -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
+20
View File
@@ -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.
@@ -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)
}
})
}
}
@@ -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
@@ -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"`
}
@@ -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.",