Skip to content

Commit

Permalink
add nginx basic auth option (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalyos authored Dec 5, 2023
1 parent 906958c commit b628ea8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 1 deletion.
9 changes: 9 additions & 0 deletions charts/onechart/templates/basic-auth.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{ if .Values.ingress }}{{ if .Values.ingress.nginxBasicAuth }}
apiVersion: v1
kind: Secret
metadata:
name: {{ $resourceName }}-basic-auth
namespace: {{ .root.Release.Namespace }}
data:
auth: {{ htpasswd .Values.ingress.nginxBasicAuth.user .Values.ingress.nginxBasicAuth.password | b64enc }}
{{ end }}{{ end }}
7 changes: 6 additions & 1 deletion charts/onechart/templates/ingress.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@ metadata:
namespace: {{ .root.Release.Namespace }}
labels:
{{- include "helm-chart.labels" .root | nindent 4 }}
{{- if or (or .root.Values.gitSha .ingress.annotations) .root.Values.gitRepository }}
{{- if or (or (or .root.Values.gitSha .ingress.nginxBasicAuth) .ingress.annotations) .root.Values.gitRepository }}
annotations:
{{- if .ingress.nginxBasicAuth }}
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: {{ $resourceName }}-basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - basic'
{{- end }}
{{- if .ingress.annotations }}
{{- toYaml .ingress.annotations | nindent 4 }}
{{- end }}
Expand Down
29 changes: 29 additions & 0 deletions charts/onechart/tests/ingress_basic_auth_ing_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
suite: test nginx basic auth ingress
templates:
- ingress.yaml
tests:
- it: Should set nginx specific annotations
set:
ingress:
host: chart-example.local
nginxBasicAuth:
user: admin1
password: secret1
asserts:
- equal:
path: metadata.annotations
value:
nginx.ingress.kubernetes.io/auth-type: basic
nginx.ingress.kubernetes.io/auth-secret: basic-auth
nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - basic'
- hasDocuments:
count: 1
- it: Should NOT set nginx specific annotations
set:
ingress:
host: chart-example.local
asserts:
- notExists:
path: metadata.annotations
- hasDocuments:
count: 1
26 changes: 26 additions & 0 deletions charts/onechart/tests/ingress_basic_auth_secret_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
suite: test nginx basic auth secret
templates:
- basic-auth.yaml
tests:
- it: Should set Ingress host name
set:
ingress:
host: chart-example.local
nginxBasicAuth:
user: admin1
password: secret1
asserts:
- hasDocuments:
count: 1
- exists:
path: data.auth

- it: Shouldnt generate extra secret
set:
ingresses:
- host: chart-example.local
annotations:
kubernetes.io/ingress.class: nginx
asserts:
- hasDocuments:
count: 0
3 changes: 3 additions & 0 deletions charts/onechart/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ image:
# tlsEnabled: true
# annotations:
# cert-manager.io/cluster-issuer: letsencrypt-staging
# nginxBasicAuth:
# user: admin
# password: secret

# vars:
# MY_VAR: "value"
Expand Down

0 comments on commit b628ea8

Please sign in to comment.