Skip to content

Commit e7f9560

Browse files
committed
Support authorization for NATS
Allow users to enable authorization for NATS to prevent unauthenticated access from function or other components. Signed-off-by: Han Verstraete (OpenFaaS Ltd) <[email protected]>
1 parent 0daa5d9 commit e7f9560

8 files changed

+99
-0
lines changed

chart/openfaas/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,9 @@ yaml) |
562562
| `jetstreamQueueWorker.maxWaiting` | Configure the max waiting pulls for the queue-worker JetStream consumer. The value should be at least max_inflight * queue_worker.replicas. Note that this value can not be updated once the consumer is created. | `512` |
563563
| `jetstreamQueueWorker.logs.debug` | Log debug messages | `false` |
564564
| `jetstreamQueueWorker.logs.format` | Set the log format, supports `console` or `json` | `console` |
565+
| `nats.authorization.enabled` | Enable authorization for NATS | `false` |
566+
| `nats.authorization.generateToken` | Generate the NATS authorization token on first installation. | `true` |
567+
| `nats.authorization.tokenSecret` | Secret resource that will be used to read the NATS authorization secret | `nats-token` |
565568
| `nats.channel` | The name of the NATS Streaming channel or NATS JetStream stream to use for asynchronous function invocations | `faas-request` |
566569
| `nats.external.clusterName` | The name of the externally-managed NATS Streaming server | `""` |
567570
| `nats.external.enabled` | Whether to use an externally-managed NATS Streaming server | `false` |

chart/openfaas/templates/event-worker-dep.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ spec:
3333
secret:
3434
secretName: {{.Values.eventSubscription.endpointSecret}}
3535
{{- end }}
36+
{{- if .Values.nats.authorization.enabled }}
37+
- name: nats-token
38+
secret:
39+
secretName: nats-token
40+
{{- end }}
3641
containers:
3742
- name: event-worker
3843
resources:
@@ -45,6 +50,9 @@ spec:
4550
{{- if .Values.eventSubscription.endpointSecret }}
4651
- "-webhook-secret-file=/var/secrets/webhook-secret/webhook-secret"
4752
{{- end}}
53+
{{- if .Values.nats.authorization.enabled }}
54+
- -nats-token-file=/var/secrets/nats_token/token
55+
{{- end }}
4856
env:
4957
{{- if .Values.nats.external.enabled }}
5058
- name: nats_host
@@ -80,6 +88,11 @@ spec:
8088
readOnly: true
8189
mountPath: "/var/secrets/webhook-secret"
8290
{{- end }}
91+
{{- if .Values.nats.authorization.enabled }}
92+
- name: nats-token
93+
readOnly: true
94+
mountPath: "/var/secrets/nats_token"
95+
{{- end }}
8396
{{- with .Values.nodeSelector }}
8497
nodeSelector:
8598
{{ toYaml . | indent 8 }}

chart/openfaas/templates/gateway-dep.yaml

+32
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
{{- fail "enabling 'operator.create' is only supported for OpenFaaS Pro" }}
77
{{- end }}
88

9+
{{- $jetstreamEnabled := and .Values.async (eq .Values.queueMode "jetstream") }}
10+
{{- $natsRequired := or $jetstreamEnabled .Values.eventSubscription.metering.enabled .Values.eventSubscription.auditing.enabled }}
11+
912
apiVersion: apps/v1
1013
kind: Deployment
1114
metadata:
@@ -41,6 +44,11 @@ spec:
4144
secret:
4245
secretName: issuer-key
4346
{{- end}}
47+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
48+
- name: nats-token
49+
secret:
50+
secretName: nats-token
51+
{{- end }}
4452
# - name: faas-auth
4553
# projected:
4654
# sources:
@@ -76,6 +84,9 @@ spec:
7684
{{- if .Values.openfaasPro }}
7785
- "-license-file=/var/secrets/license/license"
7886
{{- end }}
87+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
88+
- -nats-token-file=/var/secrets/nats_token/token
89+
{{- end }}
7990
{{- if .Values.securityContext }}
8091
securityContext:
8192
{{- toYaml .Values.securityContext | nindent 10 }}
@@ -218,6 +229,11 @@ spec:
218229
readOnly: true
219230
mountPath: "/var/secrets/license"
220231
{{- end }}
232+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
233+
- name: nats-token
234+
readOnly: true
235+
mountPath: "/var/secrets/nats_token"
236+
{{- end }}
221237
ports:
222238
- name: http
223239
containerPort: 8080
@@ -235,6 +251,9 @@ spec:
235251
- ./faas-netes
236252
- -operator=true
237253
- "-license-file=/var/secrets/license/license"
254+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
255+
- -nats-token-file=/var/secrets/nats_token/token
256+
{{- end }}
238257
env:
239258
- name: reconcile_workers
240259
value: {{ .Values.operator.reconcileWorkers | quote }}
@@ -345,6 +364,11 @@ spec:
345364
readOnly: true
346365
mountPath: "/var/secrets"
347366
{{- end }}
367+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
368+
- name: nats-token
369+
readOnly: true
370+
mountPath: "/var/secrets/nats_token"
371+
{{- end }}
348372

349373
{{- else }}
350374
- name: faas-netes
@@ -367,6 +391,9 @@ spec:
367391
{{- if or .Values.openfaasPro .Values.oem }}
368392
- "-license-file=/var/secrets/license/license"
369393
{{- end }}
394+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
395+
- -nats-token-file=/var/secrets/nats_token/token
396+
{{- end }}
370397
env:
371398
- name: port
372399
value: "8081"
@@ -455,6 +482,11 @@ spec:
455482
readOnly: true
456483
mountPath: "/var/secrets"
457484
{{- end }}
485+
{{- if and $natsRequired .Values.nats.authorization.enabled }}
486+
- name: nats-token
487+
readOnly: true
488+
mountPath: "/var/secrets/nats_token"
489+
{{- end }}
458490
- mountPath: /tmp
459491
name: faas-netes-temp-volume
460492
ports:

chart/openfaas/templates/jetstream-queueworker-dep.yaml

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ spec:
2929
secret:
3030
secretName: openfaas-license
3131
{{- end }}
32+
{{- if .Values.nats.authorization.enabled }}
33+
- name: nats-token
34+
secret:
35+
secretName: nats-token
36+
{{- end }}
3237
containers:
3338
- name: queue-worker
3439
resources:
@@ -49,6 +54,9 @@ spec:
4954
{{- if .Values.jetstreamQueueWorker.pprof }}
5055
- "-pprof=true"
5156
{{- end }}
57+
{{- if .Values.nats.authorization.enabled }}
58+
- -nats-token-file=/var/secrets/nats_token/token
59+
{{- end }}
5260
env:
5361
{{- if .Values.nats.external.enabled }}
5462
- name: nats_host
@@ -111,6 +119,11 @@ spec:
111119
readOnly: true
112120
mountPath: "/var/secrets/license"
113121
{{- end }}
122+
{{- if .Values.nats.authorization.enabled }}
123+
- name: nats-token
124+
readOnly: true
125+
mountPath: "/var/secrets/nats_token"
126+
{{- end }}
114127
{{- with .Values.nodeSelector }}
115128
nodeSelector:
116129
{{ toYaml . | indent 8 }}

chart/openfaas/templates/nats-dep.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ spec:
4141
args:
4242
{{- if eq .Values.queueMode "jetstream" }}
4343
- --js
44+
{{- if .Values.nats.authorization.enabled }}
45+
- --auth=$(AUTH_TOKEN)
46+
{{- end }}
4447
{{- else}}
4548
- --store
4649
- memory
@@ -51,6 +54,14 @@ spec:
5154
securityContext:
5255
{{- toYaml .Values.securityContext | nindent 10 }}
5356
{{- end }}
57+
env:
58+
{{- if and (eq .Values.queueMode "jetstream") .Values.nats.authorization.enabled }}
59+
- name: AUTH_TOKEN
60+
valueFrom:
61+
secretKeyRef:
62+
name: nats-token
63+
key: token
64+
{{- end }}
5465
volumeMounts:
5566
- mountPath: /tmp/nats
5667
name: nats-data
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{{- if and .Values.nats.authorization.enabled .Values.nats.authorization.generateToken }}
2+
apiVersion: v1
3+
kind: Secret
4+
metadata:
5+
name: nats-token
6+
namespace: {{ .Release.namespace | quote }}
7+
labels:
8+
app: {{ template "openfaas.name" . }}
9+
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
10+
component: queue-worker
11+
heritage: {{ .Release.Service }}
12+
release: {{ .Release.Name }}
13+
annotations:
14+
"helm.sh/hook": "pre-install"
15+
data:
16+
# kubectl -n openfaas get secret nats-token -o jsonpath="{.data.token}" | base64 --decode
17+
token: {{ randAlphaNum 12 | b64enc | quote }}
18+
{{- end }}

chart/openfaas/values-pro.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ queueMode: jetstream
7979
# See https://github.com/openfaas/openfaas-pro/blob/master/jetstream.md
8080
nats:
8181
streamReplication: 1
82+
authorization:
83+
enabled: true
8284

8385
# For timeouts longer than 60 seconds see:
8486
# https://docs.openfaas.com/tutorials/expanded-timeouts/

chart/openfaas/values.yaml

+7
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,13 @@ nats:
381381
port: ""
382382
# The version of NATS Core used with OpenFaaS Pro and JetStream
383383
image: nats:2.10.18
384+
authorization:
385+
enabled: false
386+
# Generate the authorization token for NATS on first install.
387+
# Set to false if you are creating the token manually.
388+
generateToken: true
389+
# Secret resource that will be used to read the NATS authorization secret.
390+
tokenSecret: nats-token
384391
resources:
385392
requests:
386393
memory: "120Mi"

0 commit comments

Comments
 (0)