feat(bucket)!: issue tenant user keys through platform credentials

Tenants read every user's S3 keys in plain text from the per-user
credentials Secret, and the only way to rotate them was to delete the
BucketAccess and force two Helm upgrades. The chart now renders a
CredentialTemplate per tenant user that names the BucketAccess, in the
plaintext format, instead of rendering the BucketAccess itself, so
cozystack-controller owns the grant, a mint re-creates it and a revoke
deletes it; the keys are shown once, by the mint. The per-user Secret of
a tenant user keeps only the endpoint and bucket name. It is rendered as
data rather than stringData: Helm's three-way merge removes a live key
only when the previous manifest named it, and a stringData key never
shows up in data, so a key the chart stops emitting would otherwise
stay.

A tenant user's BucketAccess keeps the name the chart gave it, which
the conversion hands over by name; a user name that is not a valid
object name never had one and takes the account name instead. A user
whose account name is longer than 63 characters is refused at render,
as credentials-migrate refuses it.

A user marked platform, which backupstrategy-controller now sets on the
backup bucket's system user, keeps its chart-rendered BucketAccess and
its keys in a Secret no tenant role selects or grants, so default
backups keep reading it; its keys are never minted. Clearing the flag
drops that BucketAccess, which revokes those keys, and the user becomes
a normal account whose keys are minted. values.yaml documents both.

BREAKING CHANGE: clients that read accessKey and secretKey from
<bucket>-<user>-credentials must mint the user's credential instead;
every mint rotates both keys.

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 0f97ae304a
commit d2bd291a10
14 files changed
+360 -9

No files matched your search

+2
View File
@@ -41,6 +41,8 @@ type ConfigSpec struct {
}
type User struct {
// Marks a platform account, such as the `system` user of the backup bucket, which backupstrategy-controller sets. Its keys stay in the `<bucket>-<user>-credentials` Secret, which no tenant can read, and are never minted. Clearing it drops the chart-rendered BucketAccess, which revokes those keys, and the user becomes a normal account whose keys are minted through its Credential. Leave it unset for your own users.
Platform bool `json:"platform,omitempty"`
// Whether the user has read-only access.
Readonly bool `json:"readonly,omitempty"`
}
+1
View File
@@ -13,5 +13,6 @@
| `publicRead` | Serves the bucket's objects to anyone without credentials at `<endpoint>/<bucket>/<key>`. Anonymous callers can read objects but cannot list the bucket or write to it. | `bool` | `false` |
| `users` | Users configuration map. | `map[string]object` | `{}` |
| `users[name].readonly` | Whether the user has read-only access. | `bool` | `false` |
| `users[name].platform` | Marks a platform account, such as the `system` user of the backup bucket, which backupstrategy-controller sets. Its keys stay in the `<bucket>-<user>-credentials` Secret, which no tenant can read, and are never minted. Clearing it drops the chart-rendered BucketAccess, which revokes those keys, and the user becomes a normal account whose keys are minted through its Credential. Leave it unset for your own users. | `bool` | `false` |
| `schedulingClass` | Name of a SchedulingClass CR (cluster-scoped, group cozystack.io) used in place of the tenant's for this application's pods. A pod gets its class when it is created, so running pods keep theirs until they are recreated. Must be the tenant's own class or one listed in the tenant's `allowedSchedulingClasses`. Setting any other value is refused, and the pods of an application whose class is not, or no longer, in that list get the tenant's class. Empty means inherit from the tenant. | `string` | `""` |
@@ -0,0 +1,52 @@
{{/*
Account naming, which must match pkg/credentials/account.MapName: the
credential of a user is <release>.<account>, where <account> is the user
name lowercased with every character outside [a-z0-9-] replaced by "-".
*/}}
{{- define "bucket.accountName" -}}
{{- regexReplaceAll "[^a-z0-9-]" (lower .) "-" -}}
{{- end -}}
{{/*
The BucketAccess of a tenant user, which also names the Secret the COSI
sidecar writes for it: <release>-<user>, as the chart named it before the
conversion, so that platform migration 71 hands the existing one, and its
keys, to the template; or <release>-<account> when <release>-<user> is not
a valid object name, since no BucketAccess was ever created under it.
*/}}
{{- define "bucket.accessName" -}}
{{- $root := index . 0 -}}
{{- $user := index . 1 -}}
{{- $legacy := printf "%s-%s" $root.Release.Name $user -}}
{{- if and (le (len $legacy) 253) (regexMatch "^[a-z0-9]([-a-z0-9]*[a-z0-9])?(\\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*$" $legacy) -}}
{{- $legacy -}}
{{- else -}}
{{- printf "%s-%s" $root.Release.Name (include "bucket.accountName" $user) -}}
{{- end -}}
{{- end -}}
{{/*
The BucketAccessClass a user's grant comes from: the storage pool's, and
its -readonly variant for a read-only user.
*/}}
{{- define "bucket.accessClass" -}}
{{- $root := index . 0 -}}
{{- $user := index . 1 -}}
{{- $root.Values._namespace.seaweedfs }}{{- if $root.Values.storagePool }}-{{ $root.Values.storagePool }}{{- end }}{{- if $user.readonly }}-readonly{{- end }}
{{- end -}}
{{- define "bucket.accountsGuard" -}}
{{- $seen := dict -}}
{{- range $user, $u := .Values.users -}}
{{- if not (default false $u.platform) -}}
{{- $mapped := include "bucket.accountName" $user -}}
{{- if or (gt (len $mapped) 63) (not (regexMatch "^[a-z0-9]([-a-z0-9]*[a-z0-9])?$" $mapped)) -}}
{{- fail (printf "user %q maps to the account name %q, which must be at most 63 characters and start and end with a letter or a digit; rename the user" $user $mapped) -}}
{{- end -}}
{{- with index $seen $mapped -}}
{{- fail (printf "users %q and %q both map to the account name %q; rename one of them" . $user $mapped) -}}
{{- end -}}
{{- $_ := set $seen $mapped $user -}}
{{- end -}}
{{- end -}}
{{- end -}}
@@ -44,18 +44,25 @@ spec:
bucketClassName: {{ $seaweedfs }}{{- if $pool }}-{{ $pool }}{{- end }}{{- if .Values.locking }}-lock{{- if eq (toString .Values.lockingDefaultRetention) "false" }}-noretention{{- end }}{{- end }}
protocols:
- s3
{{- /*
Only a platform user's BucketAccess is rendered here. A tenant user's is
created by cozystack-controller from its CredentialTemplate
(credentials.yaml), so that a mint can re-create it and a revoke delete it.
*/}}
{{- range $name, $user := .Values.users }}
{{- if default false $user.platform }}
---
apiVersion: objectstorage.k8s.io/v1alpha1
kind: BucketAccess
metadata:
name: {{ $.Release.Name }}-{{ $name }}
spec:
bucketAccessClassName: {{ $seaweedfs }}{{- if $pool }}-{{ $pool }}{{- end }}{{- if $user.readonly }}-readonly{{- end }}
bucketAccessClassName: {{ include "bucket.accessClass" (list $ $user) }}
bucketClaimName: {{ $.Release.Name }}
credentialsSecretName: {{ $.Release.Name }}-{{ $name }}
protocol: s3
{{- end }}
{{- end }}
{{- if .Values.publicRead }}
{{- if hasKey .Values.users "public-read" }}
{{- fail "publicRead creates the BucketAccess <bucket>-public-read, which the user \"public-read\" already owns: rename that user" }}
@@ -0,0 +1,29 @@
{{- /*
One CredentialTemplate per tenant user: the account the Cozystack
credentials API mints and revokes through. The COSI driver issues a user's
keys, so the template names the BucketAccess whose grant issues them, and
cozystack-controller creates that BucketAccess, copies its keys into the
account Secret <release>.<account>.account, re-creates it on a mint and
deletes it on a revoke. The keys are stored as the driver issues them, so
the format is plaintext. The chart renders no BucketAccess and no key for
these users. A platform user keeps its chart-rendered BucketAccess in
bucketclaim.yaml.
*/ -}}
{{- include "bucket.accountsGuard" $ }}
{{- range $user, $u := .Values.users }}
{{- if not (default false $u.platform) }}
---
apiVersion: cozystack.io/v1alpha1
kind: CredentialTemplate
metadata:
name: {{ printf "%s.%s" $.Release.Name (include "bucket.accountName" $user) | quote }}
spec:
username: {{ $user | quote }}
format:
name: plaintext
bucketAccess:
name: {{ include "bucket.accessName" (list $ $user) | quote }}
bucketClaimName: {{ $.Release.Name | quote }}
bucketAccessClassName: {{ include "bucket.accessClass" (list $ $u) | quote }}
{{- end }}
{{- end }}
@@ -0,0 +1,143 @@
suite: bucket credential templates
templates:
- templates/credentials.yaml
- templates/bucketclaim.yaml
release:
name: bucket-photos
namespace: tenant-foo
set:
_namespace:
seaweedfs: tenant-root
tests:
- it: renders one engine-issued template per tenant user, naming the BucketAccess the controller creates
template: templates/credentials.yaml
set:
users:
web: {}
asserts:
- hasDocuments:
count: 1
- isKind:
of: CredentialTemplate
- equal:
path: metadata.name
value: bucket-photos.web
- equal:
path: spec
value:
username: web
format:
name: plaintext
bucketAccess:
name: bucket-photos-web
bucketClaimName: bucket-photos
bucketAccessClassName: tenant-root
- it: takes the read-only class of the storage pool for a read-only user
template: templates/credentials.yaml
set:
storagePool: fast
users:
reader:
readonly: true
asserts:
- equal:
path: spec.bucketAccess.bucketAccessClassName
value: tenant-root-fast-readonly
- it: renders no BucketAccess for a tenant user
template: templates/bucketclaim.yaml
set:
users:
web: {}
asserts:
- hasDocuments:
count: 1
- isKind:
of: BucketClaim
- it: keeps a platform user's BucketAccess in the chart and gives it no template
set:
users:
system:
platform: true
asserts:
- hasDocuments:
count: 0
template: templates/credentials.yaml
- hasDocuments:
count: 2
template: templates/bucketclaim.yaml
- equal:
path: spec
value:
bucketAccessClassName: tenant-root
bucketClaimName: bucket-photos
credentialsSecretName: bucket-photos-system
protocol: s3
template: templates/bucketclaim.yaml
documentSelector:
path: kind
value: BucketAccess
# The pre-conversion chart named a user's BucketAccess <release>-<user>,
# and migration 71 hands that one over by name, so a user name that is a
# valid object name keeps it. Any other one never had a BucketAccess and
# takes the account name.
- it: names the BucketAccess as the pre-conversion chart did when that name is valid
template: templates/credentials.yaml
set:
users:
archive.v2: {}
asserts:
- equal:
path: metadata.name
value: bucket-photos.archive-v2
- equal:
path: spec.bucketAccess.name
value: bucket-photos-archive.v2
- it: names the BucketAccess after the account when the user name is not a valid object name
template: templates/credentials.yaml
set:
users:
Web_Uploads: {}
asserts:
- equal:
path: metadata.name
value: bucket-photos.web-uploads
- equal:
path: spec.bucketAccess.name
value: bucket-photos-web-uploads
- it: refuses a user whose account name is not a valid name
template: templates/credentials.yaml
set:
users:
"-web": {}
asserts:
- failedTemplate:
errorPattern: start and end with a letter or a digit
- it: refuses a user whose account name is longer than a DNS label
template: templates/credentials.yaml
set:
users:
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: {}
asserts:
- failedTemplate:
errorPattern: at most 63 characters
- it: refuses two users that map to one account
template: templates/credentials.yaml
set:
users:
Web: {}
web: {}
asserts:
- failedTemplate:
errorPattern: both map to the account name "web"
@@ -61,16 +61,18 @@ tests:
path: spec.bucketAccessClassName
value: tenant-root-public
- it: keeps the users' BucketAccesses next to the public one
- it: renders only the platform users' BucketAccesses next to the public one
set:
publicRead: true
users:
app: {}
reader:
readonly: true
system:
platform: true
asserts:
- hasDocuments:
count: 4
count: 3
- it: refuses a user that would own the public BucketAccess name
set:
+4
View File
@@ -37,6 +37,10 @@
"additionalProperties": {
"type": "object",
"properties": {
"platform": {
"description": "Marks a platform account, such as the `system` user of the backup bucket, which backupstrategy-controller sets. Its keys stay in the `\u003cbucket\u003e-\u003cuser\u003e-credentials` Secret, which no tenant can read, and are never minted. Clearing it drops the chart-rendered BucketAccess, which revokes those keys, and the user becomes a normal account whose keys are minted through its Credential. Leave it unset for your own users.",
"type": "boolean"
},
"readonly": {
"description": "Whether the user has read-only access.",
"type": "boolean"
+1
View File
@@ -16,6 +16,7 @@ publicRead: false
## @typedef {struct} User - Bucket user configuration.
## @field {bool} [readonly] - Whether the user has read-only access.
## @field {bool} [platform] - Marks a platform account, such as the `system` user of the backup bucket, which backupstrategy-controller sets. Its keys stay in the `<bucket>-<user>-credentials` Secret, which no tenant can read, and are never minted. Clearing it drops the chart-rendered BucketAccess, which revokes those keys, and the user becomes a normal account whose keys are minted through its Credential. Leave it unset for your own users.
## @param {map[string]User} users - Users configuration map.
users: {}
@@ -6,6 +6,11 @@ metadata:
namespace: {{ .Values.backupStorage.namespace }}
spec:
users:
# A platform user: its keys stay in the bucket-system Secret this
# controller reads (backupStorage.systemSecretName), which no tenant
# role selects, and it gets no CredentialTemplate whose mint would
# rotate them under every default backup.
system:
readonly: false
platform: true
{{- end }}
@@ -42,6 +42,14 @@ tests:
path: metadata.namespace
value: tenant-root
template: templates/bucket.yaml
# The system user is a platform account: its keys stay in the Secret
# this controller reads, and no tenant credential can rotate them.
- equal:
path: spec.users.system
value:
readonly: false
platform: true
template: templates/bucket.yaml
- hasDocuments:
count: 0
template: templates/strategy-cnpg-default.yaml
+1 -1
View File
@@ -8,7 +8,7 @@ spec:
plural: buckets
singular: bucket
openAPISchema: |-
{"title":"Chart Values","type":"object","properties":{"locking":{"description":"Provisions bucket from the `-lock` BucketClass (with object lock enabled).","type":"boolean","default":false},"lockingDefaultRetention":{"description":"With `locking`, keeps the default retention of the `-lock` BucketClass (COMPLIANCE, 365 days). Set to `false` to provision from the `-lock-noretention` BucketClass instead: Object Lock is enabled with no default retention, and retention is then set on the bucket or per object through the S3 API. Read at creation only.","type":"boolean","default":true},"storagePool":{"description":"Selects a specific BucketClass by storage pool name.","type":"string","default":"","x-cozystack-options":{"source":"storagepool"}},"ui":{"description":"Deploys the web UI (s3manager) with its Service and its Ingress or HTTPRoute at `<bucket>.<tenant host>`. Set to `false` to run the bucket without them.","type":"boolean","default":true},"publicRead":{"description":"Serves the bucket's objects to anyone without credentials at `<endpoint>/<bucket>/<key>`. Anonymous callers can read objects but cannot list the bucket or write to it.","type":"boolean","default":false},"users":{"description":"Users configuration map.","type":"object","default":{},"additionalProperties":{"type":"object","properties":{"readonly":{"description":"Whether the user has read-only access.","type":"boolean"}}}},"schedulingClass":{"description":"Name of a SchedulingClass CR (cluster-scoped, group cozystack.io) used in place of the tenant's for this application's pods. A pod gets its class when it is created, so running pods keep theirs until they are recreated. Must be the tenant's own class or one listed in the tenant's `allowedSchedulingClasses`. Setting any other value is refused, and the pods of an application whose class is not, or no longer, in that list get the tenant's class. Empty means inherit from the tenant.","type":"string","default":"","maxLength":63}}}
{"title":"Chart Values","type":"object","properties":{"locking":{"description":"Provisions bucket from the `-lock` BucketClass (with object lock enabled).","type":"boolean","default":false},"lockingDefaultRetention":{"description":"With `locking`, keeps the default retention of the `-lock` BucketClass (COMPLIANCE, 365 days). Set to `false` to provision from the `-lock-noretention` BucketClass instead: Object Lock is enabled with no default retention, and retention is then set on the bucket or per object through the S3 API. Read at creation only.","type":"boolean","default":true},"storagePool":{"description":"Selects a specific BucketClass by storage pool name.","type":"string","default":"","x-cozystack-options":{"source":"storagepool"}},"ui":{"description":"Deploys the web UI (s3manager) with its Service and its Ingress or HTTPRoute at `<bucket>.<tenant host>`. Set to `false` to run the bucket without them.","type":"boolean","default":true},"publicRead":{"description":"Serves the bucket's objects to anyone without credentials at `<endpoint>/<bucket>/<key>`. Anonymous callers can read objects but cannot list the bucket or write to it.","type":"boolean","default":false},"users":{"description":"Users configuration map.","type":"object","default":{},"additionalProperties":{"type":"object","properties":{"platform":{"description":"Marks a platform account, such as the `system` user of the backup bucket, which backupstrategy-controller sets. Its keys stay in the `<bucket>-<user>-credentials` Secret, which no tenant can read, and are never minted. Clearing it drops the chart-rendered BucketAccess, which revokes those keys, and the user becomes a normal account whose keys are minted through its Credential. Leave it unset for your own users.","type":"boolean"},"readonly":{"description":"Whether the user has read-only access.","type":"boolean"}}}},"schedulingClass":{"description":"Name of a SchedulingClass CR (cluster-scoped, group cozystack.io) used in place of the tenant's for this application's pods. A pod gets its class when it is created, so running pods keep theirs until they are recreated. Must be the tenant's own class or one listed in the tenant's `allowedSchedulingClasses`. Setting any other value is refused, and the pods of an application whose class is not, or no longer, in that list get the tenant's class. Empty means inherit from the tenant.","type":"string","default":"","maxLength":63}}}
release:
prefix: bucket-
labels:
@@ -36,23 +36,46 @@
to every bucket needs a controller that owns them and is tracked
separately.
*/}}
{{- /*
What the Secret holds depends on who consumes the user. A tenant user's keys
live in the account Secret of its CredentialTemplate and are shown only by a
mint, so its Secret here carries the endpoint and bucket name, which are not
secret. A platform user (users.<name>.platform) has no template: its Secret
keeps the keys for the platform component that reads it, and is labelled so
the tenant Secret view and the dashboard leave it out.
*/}}
{{- range $name, $user := .Values.users }}
{{- $secretName := printf "%s-%s" $.Values.bucketName $name }}
{{- $existingSecret := lookup "v1" "Secret" $.Release.Namespace $secretName }}
{{- if $existingSecret }}
{{- $bucketInfo := fromJson (b64dec (index $existingSecret.data "BucketInfo")) }}
{{- $platform := default false $user.platform }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ $secretName }}-credentials
labels:
{{- if $platform }}
internal.cozystack.io/tenantresource: "false"
{{- else }}
apps.cozystack.io/user-secret: "true"
{{- end }}
type: Opaque
stringData:
accessKey: {{ index $bucketInfo.spec.secretS3 "accessKeyID" | quote }}
secretKey: {{ index $bucketInfo.spec.secretS3 "accessSecretKey" | quote }}
endpoint: {{ index $bucketInfo.spec.secretS3 "endpoint" | trimPrefix "https://" | trimPrefix "http://" | quote }}
bucketName: {{ index $bucketInfo.spec "bucketName" | quote }}
{{- /*
data rather than stringData: Helm's three-way merge removes a live key only
when the previous manifest named it, and a stringData key never appears in
data, so a key dropped from stringData stays in the Secret. Rendered as
data, a key this chart stops emitting is removed on the upgrade that drops
it. The keys the stringData chart left on tenant users are stripped by
platform migration 71.
*/}}
data:
{{- if $platform }}
accessKey: {{ index $bucketInfo.spec.secretS3 "accessKeyID" | b64enc | quote }}
secretKey: {{ index $bucketInfo.spec.secretS3 "accessSecretKey" | b64enc | quote }}
{{- end }}
endpoint: {{ index $bucketInfo.spec.secretS3 "endpoint" | trimPrefix "https://" | trimPrefix "http://" | b64enc | quote }}
bucketName: {{ index $bucketInfo.spec "bucketName" | b64enc | quote }}
{{- end }}
{{- end }}
@@ -78,3 +78,77 @@ tests:
- equal:
path: metadata.name
value: bucket-cozy-backups-ui
# With the COSI Secrets present (kubernetesProvider), what the per-user
# Secret holds depends on who consumes the user: a tenant user's keys live
# in its account Secret and are shown only by a mint, a platform user keeps
# them here, hidden from tenants.
- it: "a tenant user's Secret carries the endpoint and bucket name, no keys"
templates:
- templates/user-credentials.yaml
set:
<<: *base
users:
web: {}
kubernetesProvider: &cosiSecrets
scheme:
"v1/Secret":
gvr:
version: "v1"
resource: "secrets"
namespaced: true
objects:
- kind: Secret
apiVersion: v1
metadata:
name: bucket-cozy-backups-web
namespace: tenant-root
data:
BucketInfo: eyJtZXRhZGF0YSI6eyJuYW1lIjoiYmMtdWlkIn0sInNwZWMiOnsiYnVja2V0TmFtZSI6ImJ1Y2tldC1jbGFpbS11aWQiLCJzZWNyZXRTMyI6eyJlbmRwb2ludCI6Imh0dHBzOi8vczMuZXhhbXBsZS5vcmciLCJhY2Nlc3NLZXlJRCI6IkFLSUFURVNUIiwiYWNjZXNzU2VjcmV0S2V5IjoidGVzdC1zZWNyZXQifX19
- kind: Secret
apiVersion: v1
metadata:
name: bucket-cozy-backups-system
namespace: tenant-root
data:
BucketInfo: eyJtZXRhZGF0YSI6eyJuYW1lIjoiYmMtdWlkIn0sInNwZWMiOnsiYnVja2V0TmFtZSI6ImJ1Y2tldC1jbGFpbS11aWQiLCJzZWNyZXRTMyI6eyJlbmRwb2ludCI6Imh0dHBzOi8vczMuZXhhbXBsZS5vcmciLCJhY2Nlc3NLZXlJRCI6IkFLSUFURVNUIiwiYWNjZXNzU2VjcmV0S2V5IjoidGVzdC1zZWNyZXQifX19
asserts:
- hasDocuments:
count: 1
- equal:
path: metadata.labels
value:
apps.cozystack.io/user-secret: "true"
- equal:
path: data
value:
endpoint: czMuZXhhbXBsZS5vcmc=
bucketName: YnVja2V0LWNsYWltLXVpZA==
# Rendered as data, so a key the chart stops emitting is removed by
# Helm's three-way merge on the upgrade that drops it.
- notExists:
path: stringData
- it: "a platform user's Secret keeps the keys and is hidden from tenants"
templates:
- templates/user-credentials.yaml
set:
<<: *base
users:
system:
platform: true
kubernetesProvider: *cosiSecrets
asserts:
- hasDocuments:
count: 1
- equal:
path: metadata.labels
value:
internal.cozystack.io/tenantresource: "false"
- equal:
path: data
value:
accessKey: QUtJQVRFU1Q=
secretKey: dGVzdC1zZWNyZXQ=
endpoint: czMuZXhhbXBsZS5vcmc=
bucketName: YnVja2V0LWNsYWltLXVpZA==