Skip to content

Commit d30438d

Browse files
authored
Merge pull request #43134 from shannonxtreme/secret-types-cleanup
Secret types cleanup
2 parents 9db9d35 + fe04fcb commit d30438d

File tree

1 file changed

+76
-82
lines changed
  • content/en/docs/concepts/configuration

1 file changed

+76
-82
lines changed

content/en/docs/concepts/configuration/secret.md

Lines changed: 76 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,16 @@ Kubernetes doesn't impose any constraints on the type name. However, if you
182182
are using one of the built-in types, you must meet all the requirements defined
183183
for that type.
184184

185-
If you are defining a type of secret that's for public use, follow the convention
186-
and structure the secret type to have your domain name before the name, separated
185+
If you are defining a type of Secret that's for public use, follow the convention
186+
and structure the Secret type to have your domain name before the name, separated
187187
by a `/`. For example: `cloud-hosting.example.net/cloud-api-credentials`.
188188

189-
### Opaque secrets
189+
### Opaque Secrets
190190

191-
`Opaque` is the default Secret type if omitted from a Secret configuration file.
192-
When you create a Secret using `kubectl`, you will use the `generic`
193-
subcommand to indicate an `Opaque` Secret type. For example, the following
194-
command creates an empty Secret of type `Opaque`.
191+
`Opaque` is the default Secret type if you don't explicitly specify a type in
192+
a Secret manifest. When you create a Secret using `kubectl`, you must use the
193+
`generic` subcommand to indicate an `Opaque` Secret type. For example, the
194+
following command creates an empty Secret of type `Opaque`:
195195

196196
```shell
197197
kubectl create secret generic empty-secret
@@ -208,50 +208,48 @@ empty-secret Opaque 0 2m6s
208208
The `DATA` column shows the number of data items stored in the Secret.
209209
In this case, `0` means you have created an empty Secret.
210210

211-
### Service account token Secrets
211+
### ServiceAccount token Secrets
212212

213213
A `kubernetes.io/service-account-token` type of Secret is used to store a
214214
token credential that identifies a
215-
{{< glossary_tooltip text="service account" term_id="service-account" >}}.
215+
{{< glossary_tooltip text="ServiceAccount" term_id="service-account" >}}. This
216+
is a legacy mechanism that provides long-lived ServiceAccount credentials to
217+
Pods.
218+
219+
In Kubernetes v1.22 and later, the recommended approach is to obtain a
220+
short-lived, automatically rotating ServiceAccount token by using the
221+
[`TokenRequest`](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
222+
API instead. You can get these short-lived tokens using the following methods:
223+
224+
* Call the `TokenRequest` API either directly or by using an API client like
225+
`kubectl`. For example, you can use the
226+
[`kubectl create token`](/docs/reference/generated/kubectl/kubectl-commands#-em-token-em-)
227+
command.
228+
* Request a mounted token in a
229+
[projected volume](/docs/reference/access-authn-authz/service-accounts-admin/#bound-service-account-token-volume)
230+
in your Pod manifest. Kubernetes creates the token and mounts it in the Pod.
231+
The token is automatically invalidated when the Pod that it's mounted in is
232+
deleted. For details, see
233+
[Launch a Pod using service account token projection](/docs/tasks/configure-pod-container/configure-service-account/#launch-a-pod-using-service-account-token-projection).
216234

217235
{{< note >}}
218-
Versions of Kubernetes before v1.22 automatically created credentials for
219-
accessing the Kubernetes API. This older mechanism was based on creating token
220-
Secrets that could then be mounted into running Pods.
221-
In more recent versions, including Kubernetes v{{< skew currentVersion >}}, API
222-
credentials are obtained directly by using the
223-
[TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
224-
API, and are mounted into Pods using a
225-
[projected volume](/docs/reference/access-authn-authz/service-accounts-admin/#bound-service-account-token-volume).
226-
The tokens obtained using this method have bounded lifetimes, and are
227-
automatically invalidated when the Pod they are mounted into is deleted.
228-
229-
You can still
230-
[manually create](/docs/tasks/configure-pod-container/configure-service-account/#manually-create-a-service-account-api-token)
231-
a service account token Secret; for example, if you need a token that never
232-
expires. However, using the
233-
[TokenRequest](/docs/reference/kubernetes-api/authentication-resources/token-request-v1/)
234-
subresource to obtain a token to access the API is recommended instead.
235-
You can use the
236-
[`kubectl create token`](/docs/reference/generated/kubectl/kubectl-commands#-em-token-em-)
237-
command to obtain a token from the `TokenRequest` API.
238-
{{< /note >}}
239-
240-
You should only create a service account token Secret object
236+
You should only create a ServiceAccount token Secret
241237
if you can't use the `TokenRequest` API to obtain a token,
242238
and the security exposure of persisting a non-expiring token credential
243-
in a readable API object is acceptable to you.
239+
in a readable API object is acceptable to you. For instructions, see
240+
[Manually create a long-lived API token for a ServiceAccount](/docs/tasks/configure-pod-container/configure-service-account/#manually-create-a-service-account-api-token).
241+
{{< /note >}}
244242

245243
When using this Secret type, you need to ensure that the
246244
`kubernetes.io/service-account.name` annotation is set to an existing
247-
service account name. If you are creating both the ServiceAccount and
245+
ServiceAccount name. If you are creating both the ServiceAccount and
248246
the Secret objects, you should create the ServiceAccount object first.
249247

250248
After the Secret is created, a Kubernetes {{< glossary_tooltip text="controller" term_id="controller" >}}
251249
fills in some other fields such as the `kubernetes.io/service-account.uid` annotation, and the
252250
`token` key in the `data` field, which is populated with an authentication token.
253251

254-
The following example configuration declares a service account token Secret:
252+
The following example configuration declares a ServiceAccount token Secret:
255253

256254
```yaml
257255
apiVersion: v1
@@ -268,33 +266,27 @@ data:
268266

269267
After creating the Secret, wait for Kubernetes to populate the `token` key in the `data` field.
270268

271-
See the [ServiceAccount](/docs/tasks/configure-pod-container/configure-service-account/)
272-
documentation for more information on how service accounts work.
269+
See the [ServiceAccount](/docs/concepts/security/service-accounts/)
270+
documentation for more information on how ServiceAccounts work.
273271
You can also check the `automountServiceAccountToken` field and the
274272
`serviceAccountName` field of the
275273
[`Pod`](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core)
276-
for information on referencing service account credentials from within Pods.
274+
for information on referencing ServiceAccount credentials from within Pods.
277275

278276
### Docker config Secrets
279277

280278
If you are creating a Secret to store credentials for accessing a container image registry,
281279
you must use one of the following `type` values for that Secret:
282280

283-
- `kubernetes.io/dockercfg`
284-
- `kubernetes.io/dockerconfigjson`
285-
286-
The `kubernetes.io/dockercfg` type is reserved to store a serialized
287-
`~/.dockercfg` which is the legacy format for configuring Docker command line.
288-
When using this Secret type, you have to ensure the Secret `data` field
289-
contains a `.dockercfg` key whose value is content of a `~/.dockercfg` file
290-
encoded in the base64 format.
291-
292-
The `kubernetes.io/dockerconfigjson` type is designed for storing a serialized
293-
JSON that follows the same format rules as the `~/.docker/config.json` file
294-
which is a new format for `~/.dockercfg`.
295-
When using this Secret type, the `data` field of the Secret object must
296-
contain a `.dockerconfigjson` key, in which the content for the
297-
`~/.docker/config.json` file is provided as a base64 encoded string.
281+
- `kubernetes.io/dockercfg`: store a serialized `~/.dockercfg` which is the
282+
legacy format for configuring Docker command line. The Secret
283+
`data` field contains a `.dockercfg` key whose value is the content of a
284+
base64 encoded `~/.dockercfg` file.
285+
- `kubernetes.io/dockerconfigjson`: store a serialized JSON that follows the
286+
same format rules as the `~/.docker/config.json` file, which is a new format
287+
for `~/.dockercfg`. The Secret `data` field must contain a
288+
`.dockerconfigjson` key for which the value is the content of a base64
289+
encoded `~/.docker/config.json` file.
298290

299291
Below is an example for a `kubernetes.io/dockercfg` type of Secret:
300292

@@ -314,13 +306,13 @@ If you do not want to perform the base64 encoding, you can choose to use the
314306
`stringData` field instead.
315307
{{< /note >}}
316308

317-
When you create these types of Secrets using a manifest, the API
309+
When you create Docker config Secrets using a manifest, the API
318310
server checks whether the expected key exists in the `data` field, and
319311
it verifies if the value provided can be parsed as a valid JSON. The API
320312
server doesn't validate if the JSON actually is a Docker config file.
321313

322-
When you do not have a Docker config file, or you want to use `kubectl`
323-
to create a Secret for accessing a container registry, you can do:
314+
You can also use `kubectl` to create a Secret for accessing a container
315+
registry, such as when you don't have a Docker configuration file:
324316

325317
```shell
326318
kubectl create secret docker-registry secret-tiger-docker \
@@ -330,15 +322,16 @@ kubectl create secret docker-registry secret-tiger-docker \
330322
--docker-server=my-registry.example:5000
331323
```
332324

333-
That command creates a Secret of type `kubernetes.io/dockerconfigjson`.
334-
If you dump the `.data.dockerconfigjson` field from that new Secret and then
335-
decode it from base64:
325+
This command creates a Secret of type `kubernetes.io/dockerconfigjson`.
326+
327+
Retrieve the `.data.dockerconfigjson` field from that new Secret and decode the
328+
data:
336329

337330
```shell
338331
kubectl get secret secret-tiger-docker -o jsonpath='{.data.*}' | base64 -d
339332
```
340333

341-
then the output is equivalent to this JSON document (which is also a valid
334+
The output is equivalent to the following JSON document (which is also a valid
342335
Docker configuration file):
343336

344337
```json
@@ -370,9 +363,9 @@ Secret must contain one of the following two keys:
370363
- `username`: the user name for authentication
371364
- `password`: the password or token for authentication
372365

373-
Both values for the above two keys are base64 encoded strings. You can, of
374-
course, provide the clear text content using the `stringData` for Secret
375-
creation.
366+
Both values for the above two keys are base64 encoded strings. You can
367+
alternatively provide the clear text content using the `stringData` field in the
368+
Secret manifest.
376369

377370
The following manifest is an example of a basic authentication Secret:
378371

@@ -394,7 +387,7 @@ people to understand the purpose of your Secret, and sets a convention for what
394387
to expect.
395388
The Kubernetes API verifies that the required keys are set for a Secret of this type.
396389

397-
### SSH authentication secrets
390+
### SSH authentication Secrets
398391

399392
The builtin type `kubernetes.io/ssh-auth` is provided for storing data used in
400393
SSH authentication. When using this Secret type, you will have to specify a
@@ -416,32 +409,34 @@ data:
416409
MIIEpQIBAAKCAQEAulqb/Y ...
417410
```
418411

419-
The SSH authentication Secret type is provided only for user's convenience.
420-
You could instead create an `Opaque` type Secret for credentials used for SSH authentication.
412+
The SSH authentication Secret type is provided only for convenience.
413+
You can create an `Opaque` type for credentials used for SSH authentication.
421414
However, using the defined and public Secret type (`kubernetes.io/ssh-auth`) helps other
422415
people to understand the purpose of your Secret, and sets a convention for what key names
423416
to expect.
424-
and the API server does verify if the required keys are provided in a Secret configuration.
417+
The Kubernetes API verifies that the required keys are set for a Secret of this type.
425418

426419
{{< caution >}}
427420
SSH private keys do not establish trusted communication between an SSH client and
428421
host server on their own. A secondary means of establishing trust is needed to
429422
mitigate "man in the middle" attacks, such as a `known_hosts` file added to a ConfigMap.
430423
{{< /caution >}}
431424

432-
### TLS secrets
425+
### TLS Secrets
433426

434-
Kubernetes provides a builtin Secret type `kubernetes.io/tls` for storing
427+
The `kubernetes.io/tls` Secret type is for storing
435428
a certificate and its associated key that are typically used for TLS.
436429

437-
One common use for TLS secrets is to configure encryption in transit for
430+
One common use for TLS Secrets is to configure encryption in transit for
438431
an [Ingress](/docs/concepts/services-networking/ingress/), but you can also use it
439432
with other resources or directly in your workload.
440433
When using this type of Secret, the `tls.key` and the `tls.crt` key must be provided
441434
in the `data` (or `stringData`) field of the Secret configuration, although the API
442435
server doesn't actually validate the values for each key.
443436

444-
As an alternative to using `stringData`, you can use the `data` field to provide the base64 encoded certificate and private key. Refer to [Constraints on Secret names and data](#restriction-names-data) for more on this.
437+
As an alternative to using `stringData`, you can use the `data` field to provide
438+
the base64 encoded certificate and private key. For details, see
439+
[Constraints on Secret names and data](#restriction-names-data).
445440

446441
The following YAML contains an example config for a TLS Secret:
447442

@@ -461,13 +456,13 @@ stringData:
461456
MIIEpgIBAAKCAQEA7yn3bRHQ5FHMQ ...
462457
```
463458

464-
The TLS Secret type is provided for user's convenience. You can create an `Opaque`
465-
for credentials used for TLS server and/or client. However, using the builtin Secret
466-
type helps ensure the consistency of Secret format in your project; the API server
467-
does verify if the required keys are provided in a Secret configuration.
459+
The TLS Secret type is provided only for convenience.
460+
You can create an `Opaque` type for credentials used for TLS authentication.
461+
However, using the defined and public Secret type (`kubernetes.io/ssh-auth`)
462+
helps ensure the consistency of Secret format in your project. The API server
463+
verifies if the required keys are set for a Secret of this type.
468464

469-
When creating a TLS Secret using `kubectl`, you can use the `tls` subcommand
470-
as shown in the following example:
465+
To create a TLS Secret using `kubectl`, use the `tls` subcommand:
471466

472467
```shell
473468
kubectl create secret tls my-tls-secret \
@@ -480,8 +475,7 @@ and must match the given private key for `--key`.
480475

481476
### Bootstrap token Secrets
482477

483-
A bootstrap token Secret can be created by explicitly specifying the Secret
484-
`type` to `bootstrap.kubernetes.io/token`. This type of Secret is designed for
478+
The `bootstrap.kubernetes.io/token` Secret type is for
485479
tokens used during the node bootstrap process. It stores tokens used to sign
486480
well-known ConfigMaps.
487481

@@ -508,7 +502,7 @@ data:
508502
usage-bootstrap-signing: dHJ1ZQ==
509503
```
510504

511-
A bootstrap type Secret has the following keys specified under `data`:
505+
A bootstrap token Secret has the following keys specified under `data`:
512506

513507
- `token-id`: A random 6 character string as the token identifier. Required.
514508
- `token-secret`: A random 16 character string as the actual token secret. Required.
@@ -521,8 +515,8 @@ A bootstrap type Secret has the following keys specified under `data`:
521515
- `auth-extra-groups`: A comma-separated list of group names that will be
522516
authenticated as in addition to the `system:bootstrappers` group.
523517

524-
The above YAML may look confusing because the values are all in base64 encoded
525-
strings. In fact, you can create an identical Secret using the following YAML:
518+
You can alternatively provide the values in the `stringData` field of the Secret
519+
without base64 encoding them:
526520

527521
```yaml
528522
apiVersion: v1

0 commit comments

Comments
 (0)