Skip to content

Commit 9024d4b

Browse files
committed
Update Postgresql example to use OpenFaaS secret for db password
Signed-off-by: Han Verstraete (OpenFaaS Ltd) <han@openfaas.com>
1 parent 65066f9 commit 9024d4b

1 file changed

Lines changed: 35 additions & 6 deletions

File tree

docs/languages/python/index.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ functions:
242242
+ ADDITIONAL_PACKAGE: "libpq-dev gcc python3-dev"
243243
```
244244

245-
### Example with Postgresql
245+
### Example with PostgreSQL
246246

247247
stack.yml
248248

@@ -258,22 +258,34 @@ functions:
258258
image: pgfn:latest
259259
build_options:
260260
- libpq
261+
environment:
262+
db_host: "postgresql.default.svc.cluster.local"
263+
secrets:
264+
- db-password
261265
```
262266
263-
Alternatively you can specify `ADDITIONAL_PACKAGE` in the `build_args` section for the function.
267+
The `build_options: libpq` shorthand installs the packages needed to compile `psycopg2`. If you need more control over which packages are installed, you can use `build_args` instead:
264268

265269
```yaml
266270
build_args:
267271
ADDITIONAL_PACKAGE: "libpq-dev gcc python3-dev"
268272
```
269273

274+
The database host is set as an environment variable so it can be changed per deployment without rebuilding the image. The database password is stored as an [OpenFaaS secret](/reference/secrets/) to keep it out of environment variables and the function image.
275+
276+
Create the secret before deploying the function:
277+
278+
```bash
279+
faas-cli secret create db-password --from-literal='passwd'
280+
```
281+
270282
requirements.txt
271283

272284
```
273285
psycopg2==2.9.3
274286
```
275287
276-
Create a database and table:
288+
Create a database and table to use with the example:
277289
278290
```sql
279291
CREATE DATABASE main;
@@ -284,20 +296,33 @@ CREATE TABLE users (
284296
name TEXT,
285297
);
286298
287-
-- Insert the original Postgresql author's name into the test table:
299+
-- Insert the original PostgreSQL author's name into the test table:
288300
289301
INSERT INTO users (name) VALUES ('Michael Stonebraker');
290302
```
291303

292304
handler.py:
293305

306+
The handler reads the database password from the mounted secret and the host from the `db_host` environment variable set in `stack.yaml`. It opens a connection, queries the `users` table, and returns the results.
307+
294308
```python
309+
import os
295310
import psycopg2
296311

297312
def handle(event, context):
298313

299314
try:
300-
conn = psycopg2.connect("dbname='main' user='postgres' port=5432 host='192.168.1.35' password='passwd'")
315+
password = read_secret('db-password')
316+
317+
# Connect using the host from the db_host env var
318+
# and the password from the mounted secret.
319+
conn = psycopg2.connect(
320+
dbname='main',
321+
user='postgres',
322+
port=5432,
323+
host=os.getenv('db_host'),
324+
password=password
325+
)
301326
except Exception as e:
302327
print("DB error {}".format(e))
303328
return {
@@ -313,9 +338,13 @@ def handle(event, context):
313338
"statusCode": 200,
314339
"body": rows
315340
}
341+
342+
def read_secret(name):
343+
with open("/var/openfaas/secrets/" + name, "r") as f:
344+
return f.read().strip()
316345
```
317346

318-
Always read the secret from an OpenFaaS secret at `/var/openfaas/secrets/secret-name`. The use of environment variables is an anti-pattern and will be visible via the OpenFaaS API.
347+
Always read secrets from an OpenFaaS secret at `/var/openfaas/secrets/secret-name`. The use of environment variables for sensitive values is an anti-pattern — they are visible via the OpenFaaS API.
319348

320349
### Authenticate a function
321350

0 commit comments

Comments
 (0)