mirror of
https://github.com/cozystack/cozystack.git
synced 2026-12-04 14:12:07 +09:00
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:
44 files changed
+4986
-127
No files matched your search
@@ -0,0 +1,73 @@
|
||||
# Kubernetes Audit Log
|
||||
|
||||
Cozystack attributes every issuance and revocation of a managed application password to the kube-apiserver audit log. A mint is a `create` on the `mint` subresource of a `core.cozystack.io` Credential, which kube-apiserver audits before it proxies the request to the Cozystack API, recording the caller, the time, the account in `objectRef` and the subresource that tells a mint from a revoke. kube-apiserver records neither the body nor the response of a request it proxies to an aggregated API, at any audit level, so the password shown once in the mint response cannot reach the log. The Cozystack API itself runs without audit flags, and it must stay that way or get a policy that keeps `credentials` below `RequestResponse`, because it is the one server that could write the password down.
|
||||
|
||||
The `monitoring-agents` package tails `/var/log/audit/kube/*.log` on every node and ships it to the VictoriaLogs instance in the `tenant-root` tenant, tagged `audit.*`, where it is kept for a month by default. Only `tenant-root` can read that store. Every other tenant sees the last ten operations on each of its accounts in the Credential's `status.history`, and each mint and revoke also records a Kubernetes Event on the application, with reason `CredentialIssued` or `CredentialRevoked`, naming the account and the caller.
|
||||
|
||||
## Distributions that ship no audit policy
|
||||
|
||||
Talos Linux configures kube-apiserver with an audit policy and writes the log under `/var/log/audit/kube/` out of the box, which is where the default fluent-bit input reads it. A cluster built on a distribution that gives kube-apiserver no audit policy records nothing, so attribution is lost silently. Give it the policy below, or merge these rules into the policy it has, and point the log at the directory fluent-bit tails.
|
||||
|
||||
```yaml
|
||||
apiVersion: audit.k8s.io/v1
|
||||
kind: Policy
|
||||
omitStages:
|
||||
- RequestReceived
|
||||
rules:
|
||||
# Who issued or revoked which account, and when. Metadata is all there is to
|
||||
# record for a proxied request, and all there must ever be for this resource.
|
||||
- level: Metadata
|
||||
resources:
|
||||
- group: core.cozystack.io
|
||||
resources:
|
||||
- credentials
|
||||
- credentials/mint
|
||||
- credentials/revoke
|
||||
# Account Secrets and the tenantsecrets view carry stored password forms;
|
||||
# Metadata keeps their data out of the log.
|
||||
- level: Metadata
|
||||
resources:
|
||||
- group: ""
|
||||
resources:
|
||||
- secrets
|
||||
- group: core.cozystack.io
|
||||
resources:
|
||||
- tenantsecrets
|
||||
- level: Metadata
|
||||
```
|
||||
|
||||
```
|
||||
--audit-policy-file=/etc/kubernetes/audit-policy.yaml
|
||||
--audit-log-path=/var/log/audit/kube/kube-apiserver.log
|
||||
--audit-log-maxage=30
|
||||
--audit-log-maxbackup=10
|
||||
--audit-log-maxsize=100
|
||||
```
|
||||
|
||||
The log path has to be a file under the node's `/var/log/audit/kube/` directory, because the fluent-bit DaemonSet mounts the node's `/var/log` and tails that glob. A distribution that writes the log elsewhere needs `fluent-bit.config.inputs` in the `monitoring-agents` values changed to match.
|
||||
|
||||
## Finding an issuance
|
||||
|
||||
In VictoriaLogs, the stream fields of the audit output include `verb` and `user_username`, and the record keeps the whole audit event, so a mint is found by its subresource:
|
||||
|
||||
```
|
||||
objectRef.subresource:mint AND objectRef.resource:credentials
|
||||
```
|
||||
|
||||
The matching record names the Credential in `objectRef.name`, the caller in `user_username`, the impersonated user where Kubernetes impersonation was used, the source address, the response code and the authorization decision. The user is a person only when the dashboard signs in through OIDC; without it, the dashboard acts as the tenant ServiceAccount.
|
||||
|
||||
## Shipping the log to a SIEM
|
||||
|
||||
The fluent-bit outputs of `monitoring-agents` are one string, so replacing `config.outputs` to add a destination drops the VictoriaLogs outputs with it. Add an output under `fluent-bit.config.extraOutputs` instead, which is appended after them. The audit records carry the tag `audit.*`:
|
||||
|
||||
```yaml
|
||||
fluent-bit:
|
||||
config:
|
||||
extraOutputs: |
|
||||
[OUTPUT]
|
||||
Name forward
|
||||
Match audit.*
|
||||
Host siem.example.org
|
||||
Port 24224
|
||||
tls on
|
||||
```
|
||||
Reference in new issue
Block a user