Skip to content

Commit 38e56ad

Browse files
logstonnschad
andauthored
Add basic auth configuration options to Nginx (#206)
This commit adds the ability to include an `auth_basic` and `auth_basic_user_file` directive in the Nginx configuration file. Co-authored-by: Niclas Schad <[email protected]> Signed-off-by: Paul Logston <[email protected]> Co-authored-by: Niclas Schad <[email protected]>
1 parent b454208 commit 38e56ad

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* [ENHANCEMENT] Define namespace in templates #184
1313
* [ENHANCEMENT] Use FQDN for memcached addresses #175
1414
* [ENHANCEMENT] Optionally generate endpoints for `X-Scope-OrgID` injection (multi-tenancy) #180
15+
* [ENHANCEMENT] Optionally configure Basic Auth within Nginx #205
1516
* [BUGFIX] Fix whitespace in runtime-config annotations, introduced in #209, fixed in #212
1617
* [BUGFIX] Correcting nginx config for auth orgs to right proxy_pass #192
1718

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ Kubernetes: `^1.19.0-0`
588588
| nginx.&ZeroWidthSpace;affinity | object | `{}` | |
589589
| nginx.&ZeroWidthSpace;annotations | object | `{}` | |
590590
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;auth_orgs | list | `[]` | (optional) List of [auth tenants](https://cortexmetrics.io/docs/guides/auth/) to set in the nginx config |
591+
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;basicAuthSecretName | string | `""` | (optional) Name of basic auth secret. In order to use this option, a secret with htpasswd formatted contents at the key ".htpasswd" must exist. For example: apiVersion: v1 kind: Secret metadata: name: my-secret namespace: <same as cortex installation> stringData: .htpasswd: | user1:$apr1$/woC1jnP$KAh0SsVn5qeSMjTtn0E9Q0 user2:$apr1$QdR8fNLT$vbCEEzDj7LyqCMyNpSoBh/ Please note that the use of basic auth will not identify organizations the way X-Scope-OrgID does. Thus, the use of basic auth alone will not prevent one tenant from viewing the metrics of another. To ensure tenants are scoped appropriately, explicitly set the `X-Scope-OrgID` header in the nginx config. Example setHeaders: X-Scope-Org-Id: $remote_user |
591592
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;client_max_body_size | string | `"1M"` | |
592593
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;dnsResolver | string | `"kube-dns.kube-system.svc.cluster.local"` | |
593594
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;setHeaders | object | `{}` | |

Diff for: templates/nginx/nginx-config.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ data:
4040
proxy_set_header {{ $key }} {{ $value }};
4141
{{- end }}
4242
43+
{{ if .Values.nginx.config.basicAuthSecretName -}}
44+
auth_basic "Restricted Content";
45+
auth_basic_user_file /etc/apache2/.htpasswd;
46+
{{- end }}
47+
4348
location = /healthz {
49+
# auth_basic off is not set here, even when a basic auth directive is
50+
# included in the server block, as Nginx's NGX_HTTP_REWRITE_PHASE
51+
# (point when this return statement is evaluated) comes before the
52+
# NGX_HTTP_ACCESS_PHASE (point when basic auth is evaluated). Thus,
53+
# this return statement returns a response before basic auth is
54+
# evaluated.
4455
return 200 'alive';
4556
}
4657

Diff for: templates/nginx/nginx-dep.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ spec:
5959
{{- end }}
6060
- name: config
6161
mountPath: /etc/nginx
62+
{{- if .Values.nginx.config.basicAuthSecretName }}
63+
- name: htpasswd
64+
mountPath: /etc/apache2
65+
readOnly: true
66+
{{- end }}
6267
ports:
6368
- name: http-metrics
6469
containerPort: {{ .Values.nginx.http_listen_port }}
@@ -92,6 +97,12 @@ spec:
9297
- name: config
9398
configMap:
9499
name: {{ template "cortex.fullname" . }}-nginx
100+
{{- if .Values.nginx.config.basicAuthSecretName }}
101+
- name: htpasswd
102+
secret:
103+
defaultMode: 420
104+
secretName: {{ .Values.nginx.config.basicAuthSecretName }}
105+
{{- end }}
95106
{{- if .Values.nginx.extraVolumes }}
96107
{{ toYaml .Values.nginx.extraVolumes | indent 8}}
97108
{{- end }}

Diff for: values.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,29 @@ nginx:
11651165
setHeaders: {}
11661166
# -- (optional) List of [auth tenants](https://cortexmetrics.io/docs/guides/auth/) to set in the nginx config
11671167
auth_orgs: []
1168+
# -- (optional) Name of basic auth secret.
1169+
# In order to use this option, a secret with htpasswd formatted contents at
1170+
# the key ".htpasswd" must exist. For example:
1171+
#
1172+
# apiVersion: v1
1173+
# kind: Secret
1174+
# metadata:
1175+
# name: my-secret
1176+
# namespace: <same as cortex installation>
1177+
# stringData:
1178+
# .htpasswd: |
1179+
# user1:$apr1$/woC1jnP$KAh0SsVn5qeSMjTtn0E9Q0
1180+
# user2:$apr1$QdR8fNLT$vbCEEzDj7LyqCMyNpSoBh/
1181+
#
1182+
# Please note that the use of basic auth will not identify organizations
1183+
# the way X-Scope-OrgID does. Thus, the use of basic auth alone will not
1184+
# prevent one tenant from viewing the metrics of another. To ensure tenants
1185+
# are scoped appropriately, explicitly set the `X-Scope-OrgID` header
1186+
# in the nginx config. Example
1187+
# setHeaders:
1188+
# X-Scope-Org-Id: $remote_user
1189+
basicAuthSecretName: ""
1190+
11681191
image:
11691192
repository: nginx
11701193
tag: 1.21

0 commit comments

Comments
 (0)