Skip to content

design-proposal(user-secrets-api): seed accounts from chart-rendered templates - #137

Merged
Andrei Kvapil (kvaps) merged 3 commits into
mainfrom
design/user-secrets-credential-templates
Nov 21, 2026
Merged

Andrei Kvapil (kvaps) merged 3 commits into
mainfrom
design/user-secrets-credential-templates

Conversation

@kvaps

Copy link
Copy Markdown
Member

Follows my review of cozystack/cozystack#4921: cozystack/cozystack#4921 (review)

An application write in the credentials API wrote the HelmRelease and every account Secret in one request, so it needed a Lease lock, and each review round found another race in it. This moves the account list to the chart: it renders a CredentialTemplate per account, a controller in cozystack-controller seeds the Secret, and mint and revoke update that one Secret rendered from the template. The ApplicationDefinition credentials block goes away.

One correction to the review comment: CredentialTemplate lives in cozystack.io/v1alpha1, not in core.cozystack.io. That group-version is served whole by the aggregated API, so a CRD can't go there.

The ClickHouse part (a second users_xml storage instead of valuesFrom) comes from reading the ClickHouse and operator sources and still needs a check on a cluster.

Decision 0001 is in #133, so this one is 0002.

Before review

  • If this revises a merged design proposal: it adds a decision record under that proposal's decisions/ directory, or says below why none is needed.

Adds decision record 0002 with its design in credential-templates.md, and marks the three README sections it supersedes.

DCO

  • Commits are signed off (git commit --signoff).

…templates

An application write in the Cozystack API kept several objects in step,
which needed a cross-replica lock and kept producing races in review.
The chart now renders a CredentialTemplate per account, a controller
seeds its Secret, and mint and revoke update that one Secret rendered
from the template, so an API request writes at most one object.

Records the change as decision 0002 with its design, and marks the
proposal sections it supersedes.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Important

  • Trigger review

This repository does not receive automatic reviews because it has fewer than 10 stars.

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: cb683a85-8be8-4fc8-8fc8-0e37b4225531
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@kvaps
Andrei Kvapil (kvaps) marked this pull request as ready for review November 19, 2026 19:20

@lllamnyp Timofei Larkin (lllamnyp) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the direction. Taking the account list out of the request path and letting a controller seed the Secrets makes the account lifecycle eventually consistent instead of something every write has to win a race for. Four things I'd like changed before this lands.

1. Edit the proposal, and let 0002 carry the history. design-proposals/README.md (Proposal Drift) says a merged proposal is edited as the design evolves, and a decision record keeps the reasoning and is never edited. This PR does the reverse: the README stays as it was behind "superseded" blocks, and the current design lives in credential-templates.md, whose opening paragraphs list what it replaces. A reader of the README then gets a document that is mostly wrong, with the real design somewhere else. Please fold credential-templates.md into §1, §3 and §7 of the README, drop the superseded blocks, and move the "what this replaces" narrative into 0002, linking to the README at aee08fd for the previous text.

2. Describe the account Secret, don't template it. The credentials block described an account in abstract terms: a format, the labels the operator needs, and fixed username and password keys. CredentialTemplate skips that level and templates the Secret's data directly. I'd keep the abstract level in the template:

spec:
  username: web
  format:
    name: scram-sha-256
  secret:
    labels: {cnpg.io/reload: "true"}

3. Keep a format webhook. The template functions cover the built-in hashes and what sprig can compose. A hash outside that set, apr1 or RabbitMQ's salted SHA-256 for example, would need a function compiled into the API and the controller, which is a release, or a template that stores the plaintext. With an abstract format it stays a registration: a cluster-scoped object that only platform administrators can write names a format and carries the webhook's client config, and a template refers to it by format.name like any built-in. The rules from the #74 review carry over: the webhook gets the format name, the username and the plaintext and returns the stored value; any failure fails closed with nothing written; it is platform-declared because the plaintext crosses to it; and the migration CLI, which has only the built-in functions, does not serve it. Here the controller renders too, so it calls the webhook when it seeds, as well as the API at mint. Registering formats cluster-wide also keeps a template harmless if templates are ever created by something other than a chart, since a template can only name a format, not point the API at an endpoint.

The one case this shape doesn't cover is ClickHouse as written here, a per-user XML file with the hash inline. A format that emits that XML would be a template under another name. How would you wire ClickHouse with an abstract format: valuesFrom as before, or a sidecar that composes users.d from the account Secrets?

4. Put the record in the template's status, and have Credential read only templates. I'm fine with the record staying in the Secret's annotations, written together with the value in the mint's one update, since that is what keeps the record from drifting from the value. But the controller should mirror it into the CredentialTemplate status: origin, at, by, history, and the Secret resourceVersion it mirrored. Credential then reads only templates. Its resourceVersion is the mirrored Secret version, and a mint builds the whole Secret from the template and writes it under that version without reading the Secret first, so a lagging status gives a retryable 409 and never a silent overwrite. That takes Secret reads out of the credentials code path entirely. cozystack-api keeps its Secret read access for tenantsecrets, so this narrows the code that can leak a Secret, not the API's permissions.

…proposal

The design lived in a separate file behind "superseded" notes, so the
proposal read as mostly wrong. Sections 1, 2, 3 and 7 now describe the
CredentialTemplate itself, and decision 0002 carries what changed and
why.

The template now describes the account Secret abstractly, with a
format name instead of templated data, so the API runs no chart
templates over a password. Formats outside the built-in set are
registered cluster-wide as webhooks, the controller mirrors the record
into the template's status so the credentials code reads no Secret,
and only releases marked for seeding get seeded Secrets, so an
unconverted release keeps the passwords its clients use.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>
…l templates

The example still showed the ApplicationDefinition credentials block
and the API seeding and pruning Secrets. It now shows what each chart
renders, what the controller seeds and mirrors, and what the API
writes on a mint, including a registered format for the nginx kind.

Also fixes the README places that still described the view as built
from Secrets.

Assisted-by: LLM
Signed-off-by: Andrei Kvapil <kvapss@gmail.com>

@lllamnyp Timofei Larkin (lllamnyp) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All four points are addressed in 9a2e0ef and 186281a. Approving.

@kvaps
Andrei Kvapil (kvaps) merged commit f0ab838 into main Nov 21, 2026
2 checks passed
Aleksei Sviridkin (lexfrei) added a commit to cozystack/cozystack that referenced this pull request Nov 26, 2026
…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.
```
fl0pp5 pushed a commit to fl0pp5/cozystack that referenced this pull request Nov 26, 2026
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants