Skip to content

Commit 2310b00

Browse files
LucasRoesleralexellis
authored andcommitted
Add a very simple overview of secrets
**What** - Add new Reference section - Add a document on using generic secrets to the Reference section Signed-off-by: Lucas Roesler <[email protected]>
1 parent cdb6cdb commit 2310b00

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

docs/reference/secrets.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Using secrets
2+
3+
This page shows how to use secrets within your functions for API tokens, passwords and similar.
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.
6+
7+
_Note_: The examples in the following section require `faas-cli` version `>=0.5.1`
8+
9+
## Creating the secret
10+
11+
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
12+
13+
```txt
14+
R^YqzKzSJw51K9zPpQ3R3N
15+
```
16+
17+
Now we need to define the secret in the cluster.
18+
19+
### Define a secret in Kubernetes
20+
21+
In Kubernetes we can leverage the [secrets api](https://kubernetes.io/docs/concepts/configuration/secret/) to safely store our secret values
22+
23+
From the commandline use
24+
25+
```sh
26+
kubectl create secret generic secret_api_key --from-file=secret_api_key=~/secrets/secret_api_key.txt
27+
```
28+
29+
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+
31+
### Define a secret in Docker Swarm
32+
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.
34+
35+
From the command line use
36+
37+
```sh
38+
docker secret create secret_api_key ~/secrets/secret_api_key.txt
39+
```
40+
41+
## Use the secret in your function
42+
43+
This is the simplest part, update your stack file to include the secret:
44+
45+
```yaml
46+
provider:
47+
name: faas
48+
gateway: http://localhost:8080
49+
50+
functions:
51+
protectedapi:
52+
lang: Dockerfile
53+
skip_build: true
54+
image: functions/api-key-protected:latest
55+
secrets:
56+
- secret_api_key
57+
```
58+
59+
and then deploy `faas-cli deploy -f ./stack.yaml`

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ pages:
117117
- First Python Function: ./tutorials/first-python-function.md
118118
- Workshop: ./tutorials/workshop.md
119119
- Featured: ./tutorials/featured.md
120+
- Reference:
121+
- Secrets: ./reference/secrets.md
120122
- Design & Architecture:
121123
- Gateway: ./architecture/gateway.md
122124
- Watchdog: ./architecture/watchdog.md

0 commit comments

Comments
 (0)