Skip to content

Commit ad25aba

Browse files
authored
Merge pull request #35383 from shannonxtreme/manage-secrets-file-note
Improve Managing Secrets using a Configuration File
2 parents 23c8345 + 4c897e1 commit ad25aba

File tree

2 files changed

+57
-68
lines changed

2 files changed

+57
-68
lines changed

content/en/docs/tasks/configmap-secret/managing-secret-using-config-file.md

Lines changed: 55 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -13,65 +13,70 @@ description: Creating Secret objects using resource configuration file.
1313

1414
<!-- steps -->
1515

16-
## Create the Config file
16+
## Create the Secret {#create-the-config-file}
1717

18-
You can create a Secret in a file first, in JSON or YAML format, and then
19-
create that object. The
18+
You can define the `Secret` object in a manifest first, in JSON or YAML format,
19+
and then create that object. The
2020
[Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core)
2121
resource contains two maps: `data` and `stringData`.
2222
The `data` field is used to store arbitrary data, encoded using base64. The
2323
`stringData` field is provided for convenience, and it allows you to provide
24-
Secret data as unencoded strings.
24+
the same data as unencoded strings.
2525
The keys of `data` and `stringData` must consist of alphanumeric characters,
2626
`-`, `_` or `.`.
2727

28-
For example, to store two strings in a Secret using the `data` field, convert
29-
the strings to base64 as follows:
28+
The following example stores two strings in a Secret using the `data` field.
3029

31-
```shell
32-
echo -n 'admin' | base64
33-
```
30+
1. Convert the strings to base64:
3431

35-
The output is similar to:
32+
```shell
33+
echo -n 'admin' | base64
34+
echo -n '1f2d1e2e67df' | base64
35+
```
3636

37-
```
38-
YWRtaW4=
39-
```
37+
{{< note >}}
38+
The serialized JSON and YAML values of Secret data are encoded as base64 strings. Newlines are not valid within these strings and must be omitted. When using the `base64` utility on Darwin/macOS, users should avoid using the `-b` option to split long lines. Conversely, Linux users *should* add the option `-w 0` to `base64` commands or the pipeline `base64 | tr -d '\n'` if the `-w` option is not available.
39+
{{< /note >}}
4040

41-
```shell
42-
echo -n '1f2d1e2e67df' | base64
43-
```
41+
The output is similar to:
4442

45-
The output is similar to:
43+
```
44+
YWRtaW4=
45+
MWYyZDFlMmU2N2Rm
46+
```
4647

47-
```
48-
MWYyZDFlMmU2N2Rm
49-
```
48+
1. Create the manifest:
5049

51-
Write a Secret config file that looks like this:
50+
```yaml
51+
apiVersion: v1
52+
kind: Secret
53+
metadata:
54+
name: mysecret
55+
type: Opaque
56+
data:
57+
username: YWRtaW4=
58+
password: MWYyZDFlMmU2N2Rm
59+
```
5260
53-
```yaml
54-
apiVersion: v1
55-
kind: Secret
56-
metadata:
57-
name: mysecret
58-
type: Opaque
59-
data:
60-
username: YWRtaW4=
61-
password: MWYyZDFlMmU2N2Rm
62-
```
61+
Note that the name of a Secret object must be a valid
62+
[DNS subdomain name](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names).
63+
64+
1. Create the Secret using [`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply):
65+
66+
```shell
67+
kubectl apply -f ./secret.yaml
68+
```
69+
70+
The output is similar to:
71+
72+
```
73+
secret/mysecret created
74+
```
6375
64-
Note that the name of a Secret object must be a valid
65-
[DNS subdomain name](/docs/concepts/overview/working-with-objects/names#dns-subdomain-names).
76+
To verify that the Secret was created and to decode the Secret data, refer to
77+
[Managing Secrets using kubectl](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#verify-the-secret).
6678
67-
{{< note >}}
68-
The serialized JSON and YAML values of Secret data are encoded as base64
69-
strings. Newlines are not valid within these strings and must be omitted. When
70-
using the `base64` utility on Darwin/macOS, users should avoid using the `-b`
71-
option to split long lines. Conversely, Linux users *should* add the option
72-
`-w 0` to `base64` commands or the pipeline `base64 | tr -d '\n'` if the `-w`
73-
option is not available.
74-
{{< /note >}}
79+
### Specify unencoded data when creating a Secret
7580
7681
For certain scenarios, you may wish to use the `stringData` field instead. This
7782
field allows you to put a non-base64 encoded string directly into the Secret,
@@ -103,25 +108,10 @@ stringData:
103108
username: <user>
104109
password: <password>
105110
```
111+
When you retrieve the Secret data, the command returns the encoded values,
112+
and not the plaintext values you provided in `stringData`.
106113

107-
## Create the Secret object
108-
109-
Now create the Secret using [`kubectl apply`](/docs/reference/generated/kubectl/kubectl-commands#apply):
110-
111-
```shell
112-
kubectl apply -f ./secret.yaml
113-
```
114-
115-
The output is similar to:
116-
117-
```
118-
secret/mysecret created
119-
```
120-
121-
## Check the Secret
122-
123-
The `stringData` field is a write-only convenience field. It is never output when
124-
retrieving Secrets. For example, if you run the following command:
114+
For example, if you run the following command:
125115

126116
```shell
127117
kubectl get secret mysecret -o yaml
@@ -143,14 +133,11 @@ metadata:
143133
type: Opaque
144134
```
145135

146-
The commands `kubectl get` and `kubectl describe` avoid showing the contents of a `Secret` by
147-
default. This is to protect the `Secret` from being exposed accidentally to an onlooker,
148-
or from being stored in a terminal log.
149-
To check the actual content of the encoded data, please refer to
150-
[decoding secret](/docs/tasks/configmap-secret/managing-secret-using-kubectl/#decoding-secret).
136+
### Specifying both `data` and `stringData`
137+
138+
If you specify a field in both `data` and `stringData`, the value from `stringData` is used.
151139

152-
If a field, such as `username`, is specified in both `data` and `stringData`,
153-
the value from `stringData` is used. For example, the following Secret definition:
140+
For example, if you define the following Secret:
154141

155142
```yaml
156143
apiVersion: v1
@@ -164,7 +151,7 @@ stringData:
164151
username: administrator
165152
```
166153

167-
Results in the following Secret:
154+
The `Secret` object is created as follows:
168155

169156
```yaml
170157
apiVersion: v1
@@ -180,7 +167,7 @@ metadata:
180167
type: Opaque
181168
```
182169

183-
Where `YWRtaW5pc3RyYXRvcg==` decodes to `administrator`.
170+
`YWRtaW5pc3RyYXRvcg==` decodes to `administrator`.
184171

185172
## Clean Up
186173

content/en/docs/tasks/configmap-secret/managing-secret-using-kubectl.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ The commands `kubectl get` and `kubectl describe` avoid showing the contents
113113
of a `Secret` by default. This is to protect the `Secret` from being exposed
114114
accidentally, or from being stored in a terminal log.
115115

116+
To check the actual content of the encoded data, refer to [Decoding the Secret](#decoding-secret).
117+
116118
## Decoding the Secret {#decoding-secret}
117119

118120
To view the contents of the Secret you created, run the following command:

0 commit comments

Comments
 (0)