# Secret spec and handler API

URL: /docs/develop/services/secrets/spec

Every field of Secret, then the methods a handler calls on ctx.secret.

This page lists the spec fields of Secret, each with its type, its limits and its description. The [Platform page](/docs/platform/services/secrets.md) explains the concept, and the `metadata` of every kind is [the same](/docs/develop/common-definitions.md#objectmeta).

## Secret [#secret]

```yaml
apiVersion: funcd.io/v1alpha1
kind: Secret
metadata:
  name: app-secret
  namespace: default
  resourceGroup: rg1
spec:
  data:
    API_KEY: czNjcjN0
    SHARED: ZnJvbS1zZWNyZXQ=
```

| apiVersion | kind |
| --- | --- |
| `funcd.io/v1alpha1` | `Secret` |

### `Secret.spec` [#secretspec]

| Field | Type and limits | Description |
| --- | --- | --- |
| `data` | map of string to string | The Secret's values by key, each in base64. Each key is an environment-variable name: a letter or an underscore, then letters, digits and underscores; a key that starts with FUNCD\_ never reaches a workload. Each value must be UTF-8 text with no NUL byte: a Function or a Lake that binds a Secret holding another value does not start. |
| `owner` | object, [`Secret.spec.owner`](#secretspecowner) | The one Function or Job that may write the Secret's keys through ctx.secret, in its namespace, named with its kind, besides the writers that a RolesAssignment names, such as the Secret Data Writer role. The owner writes only while it binds the Secret. With no owner, only those writers can write it. An apply of the Secret replaces its data, keys a workload wrote included. |
| `type` | string, one of `Opaque` | funcd does not read it. |

### `Secret.spec.owner` [#secretspecowner]

| Field | Type and limits | Description |
| --- | --- | --- |
| `kind` | string, required, one of `Function`, `Lake`, `Job` | The workload's kind. A Function, a Lake and a Job may share a name, so the kind says which one it is; each field names the kinds it accepts. |
| `name` | string, required, a DNS-1123 label | The workload's name, in the object's namespace. |

## Handler API [#handler-api]

The methods a handler calls on its context's clients, in each language funcd ships a shim for.

### `ctx.secret` [#ctxsecret]

#### TypeScript [#typescript]

The Secrets that the function binds. get and list read their keys and values at each call, with no cache, so a changed Secret shows at the next read; a key that starts with FUNCD\_ is never read: get returns undefined for it, and list leaves it out. put and del change one key of a bound Secret that the function may write, as the Secret's owner or through a writer role such as Secret Data Writer.

| Method | What it does |
| --- | --- |
| `get(key: string): Promise<string \| undefined>` | The key's value, or undefined when no bound Secret holds the key, the key starts with FUNCD\_, or a bound Secret does not exist. When two Secrets hold the key, the later one in spec.bindings.secret wins. Fails with the HTTP status 403 when a Policy forbids the function to read a bound Secret. |
| `list(): Promise<string[]>` | The keys that the bound Secrets hold, sorted, except a key that starts with FUNCD\_. Fails with the HTTP status 403 when a Policy forbids the function to read a bound Secret, and 404 when a bound Secret does not exist. |
| `put(binding: string, key: string, value: string): Promise<void>` | Writes key in Secret `binding`, which the function binds and may write: its owner, or a principal a writer role names. Every function that binds it reads the value at its next read. Fails with the HTTP status 403 when the function does not bind the Secret or may not write it, 404 when the Secret does not exist, 400 when the key or the value breaks a Secret's rules, 413 when the request passes 1 MiB, and 409 when the Secret changed under each of 5 attempts to write it. |
| `del(binding: string, key: string): Promise<void>` | Removes key from Secret `binding`, which the function binds and may write, as for put; a key that the Secret lacks is no error. Fails with the HTTP status 403 when the function does not bind the Secret or may not write it, 404 when the Secret does not exist, and 409 when the Secret changed under each of 5 attempts to write it. |

Each method here that calls funcd can also fail with the status 500 and the problem type `urn:funcd:problem:internal`, when funcd meets an error that it did not expect; funcd logs its cause.

When funcd refuses a call, the method fails with an error that holds funcd's status and problem type. This table gives them for each method, with when each happens and what to do.

| Method | Status | Problem type | When | What to do |
| --- | --- | --- | --- | --- |
| `get` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `get` | 403 | `urn:funcd:problem:forbidden` | A Policy forbids the Function to read a Secret that it binds. | Change or remove the Policy that forbids secret::read on the Secret. |
| `list` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `list` | 403 | `urn:funcd:problem:forbidden` | A Policy forbids the Function to read a Secret that it binds. | Change or remove the Policy that forbids secret::read on the Secret. |
| `list` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `list` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `put` | 400 | `urn:funcd:problem:invalid` | The body of the write is not &#123;"value": "&lt;text&gt;"&#125;, as when the value is not a string. | Write the value as a string. |
| `put` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `put` | 400 | `urn:funcd:problem:invalid` | The key is not an environment-variable name: a letter or an underscore, then letters, digits and underscores. | Use such a key. |
| `put` | 403 | `urn:funcd:problem:forbidden` | The Function's spec.bindings.secret does not list the Secret. | Add the Secret to spec.bindings.secret, or write a Secret that the Function binds. |
| `put` | 403 | `urn:funcd:problem:forbidden` | The Function does not own the Secret, no RolesAssignment makes it a writer, or a Policy forbids the write. | Write from the Secret's owner, or grant the Function secret::write with a RolesAssignment, such as the Secret Data Writer role. |
| `put` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `put` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `put` | 409 | `urn:funcd:problem:conflict` | The Secret changed under each attempt to write it, as when other writes of it run at the same time. | Retry the write. |
| `put` | 413 | `urn:funcd:problem:payload-too-large` | The request's body is larger than the local API takes for the call. | Send a smaller body. |
| `del` | 403 | `urn:funcd:problem:forbidden` | The Function's spec.bindings.secret does not list the Secret. | Add the Secret to spec.bindings.secret, or write a Secret that the Function binds. |
| `del` | 403 | `urn:funcd:problem:forbidden` | The Function does not own the Secret, no RolesAssignment makes it a writer, or a Policy forbids the write. | Write from the Secret's owner, or grant the Function secret::write with a RolesAssignment, such as the Secret Data Writer role. |
| `del` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `del` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `del` | 409 | `urn:funcd:problem:conflict` | The Secret changed under each attempt to write it, as when other writes of it run at the same time. | Retry the write. |

#### Python [#python]

The Secrets that the function binds. ``get`` and ``list`` read their keys and values at each call, with no cache, so a changed Secret shows at the next read; a key that starts with ``FUNCD_`` is never read: ``get`` returns ``None`` for it, and ``list`` leaves it out. ``put`` and ``delete`` change one key of a bound Secret that the function may write, as the Secret's owner or through a writer role such as Secret Data Writer.

| Method | What it does |
| --- | --- |
| `get(key: str) -> str \| None` | The key's value, or ``None`` when no bound Secret holds the key, the key starts with ``FUNCD_``, or a bound Secret does not exist. When two Secrets hold the key, the later one in ``spec.bindings.secret`` wins. Fails with the HTTP status 403 when a Policy forbids the function to read a bound Secret. |
| `list() -> list[str]` | The keys that the bound Secrets hold, sorted, except a key that starts with ``FUNCD_``. Fails with the HTTP status 403 when a Policy forbids the function to read a bound Secret, and 404 when a bound Secret does not exist. |
| `put(binding: str, key: str, value: str) -> None` | Writes key in Secret ``binding``, which the function binds and may write: its owner, or a principal a writer role names. Every function that binds it reads the value at its next read. Fails with the HTTP status 403 when the function does not bind the Secret or may not write it, 404 when the Secret does not exist, 400 when the key or the value breaks a Secret's rules, 413 when the request passes 1 MiB, and 409 when the Secret changed under each of 5 attempts to write it. |
| `delete(binding: str, key: str) -> None` | Removes key from Secret ``binding``, which the function binds and may write, as for ``put``; a key that the Secret lacks is no error. Fails with the HTTP status 403 when the function does not bind the Secret or may not write it, 404 when the Secret does not exist, and 409 when the Secret changed under each of 5 attempts to write it. |

Each method here that calls funcd can also fail with the status 500 and the problem type `urn:funcd:problem:internal`, when funcd meets an error that it did not expect; funcd logs its cause.

When funcd refuses a call, the method fails with an error that holds funcd's status and problem type. This table gives them for each method, with when each happens and what to do.

| Method | Status | Problem type | When | What to do |
| --- | --- | --- | --- | --- |
| `get` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `get` | 403 | `urn:funcd:problem:forbidden` | A Policy forbids the Function to read a Secret that it binds. | Change or remove the Policy that forbids secret::read on the Secret. |
| `list` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `list` | 403 | `urn:funcd:problem:forbidden` | A Policy forbids the Function to read a Secret that it binds. | Change or remove the Policy that forbids secret::read on the Secret. |
| `list` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `list` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `put` | 400 | `urn:funcd:problem:invalid` | The body of the write is not &#123;"value": "&lt;text&gt;"&#125;, as when the value is not a string. | Write the value as a string. |
| `put` | 400 | `urn:funcd:problem:invalid` | A Secret's value is not UTF-8 text, or it holds a NUL byte. | Store the value as UTF-8 text with no NUL byte, such as in base64. |
| `put` | 400 | `urn:funcd:problem:invalid` | The key is not an environment-variable name: a letter or an underscore, then letters, digits and underscores. | Use such a key. |
| `put` | 403 | `urn:funcd:problem:forbidden` | The Function's spec.bindings.secret does not list the Secret. | Add the Secret to spec.bindings.secret, or write a Secret that the Function binds. |
| `put` | 403 | `urn:funcd:problem:forbidden` | The Function does not own the Secret, no RolesAssignment makes it a writer, or a Policy forbids the write. | Write from the Secret's owner, or grant the Function secret::write with a RolesAssignment, such as the Secret Data Writer role. |
| `put` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `put` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `put` | 409 | `urn:funcd:problem:conflict` | The Secret changed under each attempt to write it, as when other writes of it run at the same time. | Retry the write. |
| `put` | 413 | `urn:funcd:problem:payload-too-large` | The request's body is larger than the local API takes for the call. | Send a smaller body. |
| `delete` | 403 | `urn:funcd:problem:forbidden` | The Function's spec.bindings.secret does not list the Secret. | Add the Secret to spec.bindings.secret, or write a Secret that the Function binds. |
| `delete` | 403 | `urn:funcd:problem:forbidden` | The Function does not own the Secret, no RolesAssignment makes it a writer, or a Policy forbids the write. | Write from the Secret's owner, or grant the Function secret::write with a RolesAssignment, such as the Secret Data Writer role. |
| `delete` | 404 | `urn:funcd:problem:not-found` | The calling Function no longer exists, so it binds no Secret. | Call from a Function that exists; a deleted Function's worker stops. |
| `delete` | 404 | `urn:funcd:problem:not-found` | A Secret that the Function binds does not exist. | Create the Secret, or remove it from the Function's spec.bindings.secret. |
| `delete` | 409 | `urn:funcd:problem:conflict` | The Secret changed under each attempt to write it, as when other writes of it run at the same time. | Retry the write. |

## HTTP API [#http-api]

The table lists each described operation of these kinds, with its method and path. The operation's page in the API reference gives its parameters, its body, its authentication and every answer, with when it happens and what to do.

| Method | Path | Operation |
| --- | --- | --- |
| `GET` | `/apis/funcd.io/v1alpha1/namespaces/{namespace}/secrets` | [List the Secrets of a namespace](/docs/api/services/secret/list.md) |
| `POST` | `/apis/funcd.io/v1alpha1/namespaces/{namespace}/secrets` | [Create a Secret](/docs/api/services/secret/create.md) |
| `GET` | `/apis/funcd.io/v1alpha1/namespaces/{namespace}/secrets/{name}` | [Read a Secret](/docs/api/services/secret/get.md) |
| `PUT` | `/apis/funcd.io/v1alpha1/namespaces/{namespace}/secrets/{name}` | [Replace a Secret](/docs/api/services/secret/replace.md) |
| `DELETE` | `/apis/funcd.io/v1alpha1/namespaces/{namespace}/secrets/{name}` | [Delete a Secret](/docs/api/services/secret/delete.md) |
