feat(api): add the CredentialTemplate kind

An application write in the Cozystack API that kept an account list, its
Secrets and the HelmRelease in step needed a cross-replica lock and
still raced. The accepted design (cozystack/community#137) moves the
account list to the chart instead: a kind's chart renders one
cozystack.io CredentialTemplate per account, naming the user, the stored
format and the Secret's labels, and its status mirrors the account
Secret's record so the Credential view reads templates only.

Tenants cannot write a template, and username is immutable because the
account Secret's name derives from it. valuesPath starts with "_" so the
Application API hides it on read and refuses it on write.

Design: https://github.com/cozystack/community/blob/main/design-proposals/user-secrets-api/README.md

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
This commit is contained in:
Andrei Kvapil committed 2026-11-24 23:06:02 +01:00
1 parent 0cd89dc1c8
commit 9e7c684fe2
4 files changed
+543 -5

No files matched your search

+138
View File
@@ -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
// <valuesPath>.u<sha256 hex of the username>. 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 <name>.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{})
}
+183 -5
View File
@@ -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"
)
@@ -355,6 +355,184 @@ func (in *ComponentInstall) DeepCopy() *ComponentInstall {
return out
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *CredentialTemplate) DeepCopyInto(out *CredentialTemplate) {
*out = *in
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 CredentialTemplate.
func (in *CredentialTemplate) DeepCopy() *CredentialTemplate {
if in == nil {
return nil
}
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 *CredentialTemplateHistoryEntry) DeepCopyInto(out *CredentialTemplateHistoryEntry) {
*out = *in
in.At.DeepCopyInto(&out.At)
}
// 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(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 *CredentialTemplateList) DeepCopyInto(out *CredentialTemplateList) {
*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 CredentialTemplateList.
func (in *CredentialTemplateList) DeepCopy() *CredentialTemplateList {
if in == nil {
return nil
}
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
}
// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil.
func (in *DependencyStatus) DeepCopyInto(out *DependencyStatus) {
*out = *in
@@ -422,7 +600,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)
}
}
@@ -575,7 +753,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])
}
@@ -624,7 +802,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])
}
+1
View File
@@ -16,6 +16,7 @@ go.mod
go.sum
# In-repo packages the tool and its tests import.
api/v1alpha1/
internal/apigate/
pkg/apis/apps/v1alpha1/
pkg/apis/apps/validation/
@@ -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 <name>.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
<valuesPath>.u<sha256 hex of the username>. 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: {}