You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/secrets.md
+56-32Lines changed: 56 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,46 +2,77 @@
2
2
3
3
This page shows how to use secrets within your functions for API tokens, passwords and similar.
4
4
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.
6
6
7
-
## Creating the secret
7
+
## Design
8
8
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:
10
27
11
28
```txt
12
29
R^YqzKzSJw51K9zPpQ3R3N
13
30
```
14
31
15
-
Now we need to define the secret in the cluster.
32
+
Now we can import the secret into the cluster.
16
33
17
-
### Define a secret in Kubernetes
34
+
####Define a secret in Kubernetes
18
35
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.
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`.
30
47
31
-
### Define a secret in Docker Swarm
48
+
You can skip creating a file and use input directly from the command-line like this:
32
49
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.
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:
36
61
37
62
```sh
38
63
docker secret create secret-api-key \
39
64
~/secrets/secret_api_key.txt
40
65
```
41
66
42
-
## Use the secret in your function
67
+
or:
43
68
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`.
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`.
45
76
46
77
_Note_: prior to version `0.8.2` secrets were mounted to `/run/secrets`. The example functions demonstrate a smooth upgrade implementation.
This example comes from the [`ApiKeyProtected`](https://github.com/openfaas/faas/tree/master/sample-functions/ApiKeyProtected-Secrets) sample function.
64
95
65
-
## Deploy a function with secrets
96
+
###Deploy a function with secrets
66
97
67
-
Now, update your stack file to include the secret:
98
+
Create a `stack.yaml` file in the current directory:
68
99
69
100
```yaml
70
101
provider:
71
102
name: faas
72
-
gateway: http://localhost:8080
73
103
74
104
functions:
75
105
protectedapi:
76
-
lang: Dockerfile
106
+
lang: dockerfile
77
107
skip_build: true
78
108
image: functions/api-key-protected:latest
79
109
secrets:
80
110
- secret-api-key
81
111
```
82
112
83
-
and then deploy `faas-cli deploy -f ./stack.yaml`
113
+
Now deploy the function with: `faas-cli deploy`
84
114
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.
0 commit comments