Skip to content

Commit aaa0d43

Browse files
committed
Clarify use of secrets / env-vars
Signed-off-by: Alex Ellis (VMware) <[email protected]>
1 parent a2987de commit aaa0d43

File tree

1 file changed

+56
-32
lines changed

1 file changed

+56
-32
lines changed

docs/reference/secrets.md

Lines changed: 56 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,77 @@
22

33
This page shows how to use secrets within your functions for API tokens, passwords and similar.
44

5-
Using secrets is a two step process. First we need to define the secret in your cluster and then you need to 'use' the secret to your function. You can find a simple example function [ApiKeyProtected in the OpenFaaS repo](https://github.com/openfaas/faas/tree/master/sample-functions/ApiKeyProtected-Secrets). When we deploy this function we provide a secret key that it uses to authenticate requests.
5+
Using secrets is a two step process. First you need to define a new secret in your cluster and then you need to 'use' the secret to your function by adding it the deployment request or stack YAML file.
66

7-
## Creating the secret
7+
## Design
88

9-
It is generally easiest to read your secret values from files. For our examples we have created a simple text file `~/secrets/secret_api_key.txt` that looks like
9+
* Secrets can be specified via API, CLI or YAML file
10+
* You can use one to many secrets in a function
11+
* Secrets must exist in the cluster at deployment time
12+
* Secrets need to be created with `kubectl` or `docker secret create`, but [in the near future](https://github.com/openfaas/faas/issues/807) an API will exist to create, list, delete and update secrets.
13+
14+
### A note on environmental variables
15+
16+
The OpenFaaS contributors believe that enviromental variables should be reserved for non-confidential data only. You can read how to use environmental variables in the YAML reference page.
17+
18+
All secrets are made available in the container file-system and should be read from the following location: `/var/openfaas/secrets/<secret-name>`. Both Kubernetes and Swarm have excellent stores for secrets. In the sample below we show how to create and consume a secret in a function.
19+
20+
## Sample
21+
22+
We have built a sample function that can be deployed alongside a secret (an API key) to validate incoming requests. It is available in the [openfaas/faas](https://github.com/openfaas/faas/) repo: [ApiKeyProtected](https://github.com/openfaas/faas/tree/master/sample-functions/ApiKeyProtected-Secrets). Only requests presenting a valid API key value will be validated.
23+
24+
### Creating a file for the secret
25+
26+
Create a text file named `secret-api-key.txt` and add the following value:
1027

1128
```txt
1229
R^YqzKzSJw51K9zPpQ3R3N
1330
```
1431

15-
Now we need to define the secret in the cluster.
32+
Now we can import the secret into the cluster.
1633

17-
### Define a secret in Kubernetes
34+
#### Define a secret in Kubernetes
1835

19-
In Kubernetes we can leverage the [secrets api](https://kubernetes.io/docs/concepts/configuration/secret/) to safely store our secret values
36+
In Kubernetes we can leverage the [built-in secret store](https://kubernetes.io/docs/concepts/configuration/secret/) to securely store secrets for functions.
2037

21-
From the commandline use
38+
Type in:
2239

2340
```sh
2441
kubectl create secret generic secret-api-key \
25-
--from-file=secret-api-key=~/secrets/secret_api_key.txt \
42+
--from-file=secret-api-key=secret-api-key.txt \
2643
--namespace openfaas-fn
2744
```
2845

2946
Here we have explicitly named the key of the secret value so that when it is mounted into the function container, it will be named exactly `secret-api-key` instead of `secret_api_key.txt`.
3047

31-
### Define a secret in Docker Swarm
48+
You can skip creating a file and use input directly from the command-line like this:
3249

33-
For sensitive value we can leverage the [Docker Swarm Secrets](https://docs.docker.com/engine/swarm/secrets/) feature to safely store our secret values.
50+
```sh
51+
kubectl create secret generic secret-api-key \
52+
--from-literal secret-api-key="R^YqzKzSJw51K9zPpQ3R3N" \
53+
--namespace openfaas-fn
54+
```
3455

35-
From the command line use
56+
#### Define a secret in Docker Swarm
57+
58+
Docker has a built-in [secrets store](https://docs.docker.com/engine/swarm/secrets/) just like Kubernetes which can be used to securely store secrets for our functions.
59+
60+
Type in:
3661

3762
```sh
3863
docker secret create secret-api-key \
3964
~/secrets/secret_api_key.txt
4065
```
4166

42-
## Use the secret in your function
67+
or:
4368

44-
Secrets are mounted as files to `/var/openfaas/secrets` inside your function. Using secrets is as simple as adding code to read the value from `/var/openfaas/secrets/secret-api-key`.
69+
```sh
70+
echo "R^YqzKzSJw51K9zPpQ3R3N" | docker secret create secret-api-key -
71+
```
72+
73+
### Use the secret in your function
74+
75+
OpenFaaS secrets are mounted as files to `/var/openfaas/secrets` inside your function's filesystem. To use a secret, just read the file from the secrets location using the name of the secret for the filename such as: `/var/openfaas/secrets/secret-api-key`.
4576

4677
_Note_: prior to version `0.8.2` secrets were mounted to `/run/secrets`. The example functions demonstrate a smooth upgrade implementation.
4778

@@ -62,46 +93,39 @@ func getAPISecret(secretName string) (secretBytes []byte, err error) {
6293

6394
This example comes from the [`ApiKeyProtected`](https://github.com/openfaas/faas/tree/master/sample-functions/ApiKeyProtected-Secrets) sample function.
6495

65-
## Deploy a function with secrets
96+
### Deploy a function with secrets
6697

67-
Now, update your stack file to include the secret:
98+
Create a `stack.yaml` file in the current directory:
6899

69100
```yaml
70101
provider:
71102
name: faas
72-
gateway: http://localhost:8080
73103

74104
functions:
75105
protectedapi:
76-
lang: Dockerfile
106+
lang: dockerfile
77107
skip_build: true
78108
image: functions/api-key-protected:latest
79109
secrets:
80110
- secret-api-key
81111
```
82112
83-
and then deploy `faas-cli deploy -f ./stack.yaml`
113+
Now deploy the function with: `faas-cli deploy`
84114

85-
Once the deploy is done you can test the function using the cli. The function is very simple, it reads the secret value that is mounted into the container for you and then returns a success or failure message based on if your header matches that secret value. For example,
115+
Once the deploy is done you can test the function using the `faas-cli` or `curl`. The function reads the secret value that was mounted into the container by OpenFaaS and then returns a success or failure message based on if your header matches that secret value. The same code runs exactly the same without modifications on both Kubernetes and Docker Swarm.
86116

87-
```sh
88-
faas-cli invoke protectedapi -H "X-Api-Key=R^YqzKzSJw51K9zPpQ3R3N"
89-
```
90-
91-
Resulting in
117+
Let's see how that works:
92118

93-
```txt
119+
```sh
120+
echo | faas-cli invoke protectedapi -H "X-Api-Key=R^YqzKzSJw51K9zPpQ3R3N"
94121
Unlocked the function!
95122
```
96123

97-
When you use the wrong api key,
124+
Now let's use an incorrect value for the api-key:
98125

99126
```sh
100-
faas-cli invoke protectedapi -H "X-Api-Key=thisiswrong"
101-
```
102-
103-
You get
104-
105-
```txt
127+
echo | faas-cli invoke protectedapi -H "X-Api-Key=thisiswrong"
106128
Access denied!
107129
```
130+
131+
You can also use multiple secrets for the same function or across multiple functions.

0 commit comments

Comments
 (0)