feat(api): add the credentials API with mint and revoke subresources (#4921)

## What this PR does

Tenants read application passwords from Secrets today, and a leaked
password can't be taken back. This PR adds the platform side of the user
secrets API design (cozystack/community#137, ADR 0002): a password comes
out once from a mint, the next mint replaces it, and a revoke kills it.
The engines are converted in the PRs stacked on top, starting with
Postgres in #4922.

How it works:

- A kind's chart renders one `CredentialTemplate`
(`cozystack.io/v1alpha1`) per account: the username, the stored format
(`scram-sha-256`, `mysql-native`, `sha256` or `plaintext`), labels for
the account Secret, and an optional `valuesPath` for an operator that
reads no Secret.
- cozystack-controller keeps the account Secret `<template>.account`. It
seeds it on a release the API marked for seeding, adopts an unowned
Secret with the same username, leaves a foreign one alone with a
`SecretConflict` condition, and mirrors the record (origin, time, actor,
history) into the template status. For a `valuesPath` template it
maintains an optional `valuesFrom` entry on the release.
- `core.cozystack.io/v1alpha1` serves `credentials` as a read-only view
of the templates, plus `mint` and `revoke` subresources. A mint checks
the caller's `resourceVersion` precondition against the mirrored status,
builds the whole Secret and writes it in one request, or answers 409. It
never reads the Secret back. One API request writes one object; the
best-effort Event is the only extra write.
- The Application API marks a new release for seeding, keeps the account
`valuesFrom` entries when it rewrites a release, and refuses to delete a
suspended release that still has accounts, since Flux would skip its
uninstall.
- The dashboard gets a Credentials tab on the application page. It lists
accounts with origin and history; an administrator can mint or revoke,
and a viewer sees no buttons.

The application charts are untouched here: a kind starts using this only
when its chart renders templates. The `CredentialFormat` webhooks that
ADR 0002 describes are not in this PR, so `format.name` accepts only the
four built-in names for now.

### Screenshots

<img width="1440" height="900" alt="01-admin-tab"
src="https://github.com/user-attachments/assets/8c48086c-8f6c-4908-82c1-40e9078afe57"
/>

<img width="1440" height="900" alt="02-mint-dialog"
src="https://github.com/user-attachments/assets/802b2963-3f57-4c0b-ab84-f74bad28c343"
/>

<img width="1440" height="900" alt="03-mint-result"
src="https://github.com/user-attachments/assets/0e9ae3cc-7a3c-4daf-9a09-d4abf53bf70b"
/>

<img width="1440" height="900" alt="04-viewer-tab"
src="https://github.com/user-attachments/assets/10e940f2-4ceb-469d-a0f9-6c9d20a62919"
/>

### Downstream repositories

- [ ] No downstream repository is affected by this change
- [ ] [cozystack/website](https://github.com/cozystack/website) -
follow-up: needed, cozystack/website#753 should document the
`Credential` mint/revoke flow and the dashboard tab
- [ ]
[cozystack/terraform-provider-cozystack](https://github.com/cozystack/terraform-provider-cozystack)
- follow-up: needed later, a way to mint from Terraform; nothing breaks
in this PR alone
- [ ]
[cozystack/ansible-cozystack](https://github.com/cozystack/ansible-cozystack)
- follow-up:
- [ ] [cozystack/ccp](https://github.com/cozystack/ccp) - follow-up:
- [ ] [cozystack/talm](https://github.com/cozystack/talm) - follow-up:
- [ ] [cozystack/cozyhr](https://github.com/cozystack/cozyhr) -
follow-up:
- [ ] [cozystack/cozy-proxy](https://github.com/cozystack/cozy-proxy) -
follow-up:
- [ ]
[cozystack/cozystack-telemetry-server](https://github.com/cozystack/cozystack-telemetry-server)
- follow-up:
- [ ]
[cozystack/external-apps-example](https://github.com/cozystack/external-apps-example)
- follow-up:
- [ ] [cozystack/examples](https://github.com/cozystack/examples) -
follow-up:
- [ ] [cozystack/community](https://github.com/cozystack/community) -
follow-up: none, the design is cozystack/community#137

### Release note

```release-note
feat(api): Add the credentials API. Applications that support it expose their accounts as `credentials.core.cozystack.io`; an administrator mints a password once with the `mint` subresource, and `revoke` invalidates it without issuing a new one. The dashboard shows a Credentials tab for such applications. No application uses it in this release on its own; see the per-engine changes.
```
This commit is contained in:
Timofei Larkin authored and GitHub committed 2026-11-26 12:37:20 +03:00
commit 94c79925f8
44 files changed
+4986 -127

No files matched your search

@@ -1,4 +1,5 @@
API rule violation: list_type_missing,github.com/cozystack/cozystack/pkg/apis/apps/v1alpha1,ApplicationStatus,Conditions
API rule violation: list_type_missing,github.com/cozystack/cozystack/pkg/apis/core/v1alpha1,CredentialStatus,History
API rule violation: list_type_missing,github.com/cozystack/cozystack/pkg/apis/core/v1alpha1,OptionSpec,Items
API rule violation: list_type_missing,github.com/cozystack/cozystack/pkg/apis/core/v1alpha1,TapPackage,Tags
API rule violation: list_type_missing,github.com/cozystack/cozystack/pkg/apis/core/v1alpha1,TapSpec,Packages
+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])
}