# Use Secrets in a cluster

Keep Kubernetes Secrets in step with {{product}} Secrets through the External Secrets add-on, with no key stored in the cluster.

Replace _my-cluster_ with your cluster's name or id, _DB_URL_ with a secret of yours and _shop_
with a namespace.

The `external-secrets` add-on runs the External Secrets Operator. Each **store** in its options
becomes a ClusterSecretStore. An `ExternalSecret` in the cluster names a secret; the operator
reads it from Deutsche Cloud Secrets, writes a Kubernetes Secret and refreshes it.

- The cluster holds no Deutsche Cloud credential. The operator proves itself with its
  service-account token, and Deutsche Cloud answers only for the secrets the store lists.
- A store lists secret names (`DB_URL`) or name prefixes (`PAYMENTS_*`); never every secret.
- With `environment`, a name means that environment's secret, else the shared one. Without it,
  shared secrets only.
- With `namespaces`, only ExternalSecrets in those namespaces may use the store.
- Every read is in the secret's access log, with the cluster as the reader, and in the audit log
  as `secrets:GetSecretValue`.

## Prerequisites

- Permission for `eks:CreateAddon` on the cluster. In the console, also `eks:DescribeCluster` to
  open the cluster.
- `secrets:GetSecretValue` on every secret a store names, or on every secret (`secret/*`) for a
  `PREFIX_*` entry: a prefix also reaches secrets created later.

## Install the add-on

**Console**
1. Open **Kubernetes**, open the cluster, then choose **Add-ons**.
2. Choose **Add an add-on**, then **External Secrets**.
3. Name the store, list its secrets, and choose an environment and namespaces if you want.
4. Choose **Install**.

**CLI**
```sh
dcloud eks addons create my-cluster --name external-secrets --config 'stores=[{"name":"ews","secrets":["DB_URL","PAYMENTS_*"],"environment":"prod","namespaces":["shop"]}]'
```

**API**
```http
POST /eks/api/clusters/my-cluster/addons
{ "name": "external-secrets", "config": { "stores": [{ "name": "ews", "secrets": ["DB_URL", "PAYMENTS_*"], "environment": "prod", "namespaces": ["shop"] }] } }
```

**Terraform**
```hcl
resource "dcloud_eks_addon" "external_secrets" {
  cluster_id = dcloud_eks_cluster.main.id
  name       = "external-secrets"
  config = jsonencode({
    stores = [{ name = "ews", secrets = ["DB_URL", "PAYMENTS_*"], environment = "prod" }]
  })
}
```

## Sync a secret into a namespace

```yaml
apiVersion: external-secrets.io/v1
kind: ExternalSecret
metadata:
  name: db
  namespace: shop
spec:
  refreshInterval: 1h
  secretStoreRef: { kind: ClusterSecretStore, name: ews }
  target: { name: db }
  data:
    - secretKey: url
      remoteRef: { key: DB_URL }
```

`kubectl get secret db -n shop` shows the Secret once the operator has read it. A secret whose
value is JSON can be split into keys with `dataFrom: [{ extract: { key: NAME } }]`.

Keep `refreshInterval` at an hour or more unless a value changes often: each refresh is a read in
the secret's access log. A store answers at most 600 reads a minute per cluster.

## Change or remove

Changing the stores (**Change** in the console, `dcloud eks addons update`) needs
`secrets:GetSecretValue` on what the new stores name. Removing the add-on deletes the operator and
the stores; the Kubernetes Secrets it wrote stay until you delete them.
