Skip to content

Commit

Permalink
Add basic auth configuration options to Nginx (#206)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
logston and nschad authored Sep 3, 2021
1 parent b454208 commit 38e56ad
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* [ENHANCEMENT] Define namespace in templates #184
* [ENHANCEMENT] Use FQDN for memcached addresses #175
* [ENHANCEMENT] Optionally generate endpoints for `X-Scope-OrgID` injection (multi-tenancy) #180
* [ENHANCEMENT] Optionally configure Basic Auth within Nginx #205
* [BUGFIX] Fix whitespace in runtime-config annotations, introduced in #209, fixed in #212
* [BUGFIX] Correcting nginx config for auth orgs to right proxy_pass #192

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,7 @@ Kubernetes: `^1.19.0-0`
| nginx.&ZeroWidthSpace;affinity | object | `{}` | |
| nginx.&ZeroWidthSpace;annotations | object | `{}` | |
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;auth_orgs | list | `[]` | (optional) List of [auth tenants](https://cortexmetrics.io/docs/guides/auth/) to set in the nginx config |
| 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 |
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;client_max_body_size | string | `"1M"` | |
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;dnsResolver | string | `"kube-dns.kube-system.svc.cluster.local"` | |
| nginx.&ZeroWidthSpace;config.&ZeroWidthSpace;setHeaders | object | `{}` | |
Expand Down
11 changes: 11 additions & 0 deletions templates/nginx/nginx-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ data:
proxy_set_header {{ $key }} {{ $value }};
{{- end }}
{{ if .Values.nginx.config.basicAuthSecretName -}}
auth_basic "Restricted Content";
auth_basic_user_file /etc/apache2/.htpasswd;
{{- end }}
location = /healthz {
# auth_basic off is not set here, even when a basic auth directive is
# included in the server block, as Nginx's NGX_HTTP_REWRITE_PHASE
# (point when this return statement is evaluated) comes before the
# NGX_HTTP_ACCESS_PHASE (point when basic auth is evaluated). Thus,
# this return statement returns a response before basic auth is
# evaluated.
return 200 'alive';
}
Expand Down
11 changes: 11 additions & 0 deletions templates/nginx/nginx-dep.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ spec:
{{- end }}
- name: config
mountPath: /etc/nginx
{{- if .Values.nginx.config.basicAuthSecretName }}
- name: htpasswd
mountPath: /etc/apache2
readOnly: true
{{- end }}
ports:
- name: http-metrics
containerPort: {{ .Values.nginx.http_listen_port }}
Expand Down Expand Up @@ -92,6 +97,12 @@ spec:
- name: config
configMap:
name: {{ template "cortex.fullname" . }}-nginx
{{- if .Values.nginx.config.basicAuthSecretName }}
- name: htpasswd
secret:
defaultMode: 420
secretName: {{ .Values.nginx.config.basicAuthSecretName }}
{{- end }}
{{- if .Values.nginx.extraVolumes }}
{{ toYaml .Values.nginx.extraVolumes | indent 8}}
{{- end }}
Expand Down
23 changes: 23 additions & 0 deletions values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,29 @@ nginx:
setHeaders: {}
# -- (optional) List of [auth tenants](https://cortexmetrics.io/docs/guides/auth/) to set in the nginx config
auth_orgs: []
# -- (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
basicAuthSecretName: ""

image:
repository: nginx
tag: 1.21
Expand Down

0 comments on commit 38e56ad

Please sign in to comment.