Ingress: Basic Auth
We can secure our ingres setup by adding basic auth.
Create Basic Auth Secret
First we need to create basic auth file using htpasswd
. It will ask for a password and confirmation password, then write the credential to file auth
.
Then we create a secret
to store the basic auth credential. This command below will read the auth
file and create a secret named ingress-auth
.
We can see and validate the secret we just create using this command below.
That will output a yaml format for our ingress-auth
secret.
Configure Ingress
Now with the secret ready we need to configure our ingress. Add this annotations below.
nginx.ingress.kubernetes.io/auth-type
: type of authentication, either basic
or digest
.
nginx.ingress.kubernetes.io/auth-secret
: the name of secret object that contains username and password that we just created. This annotations also support secret reference in other namespace "namespace/secret_name"
.
nginx.ingress.kubernetes.io/auth-realm
: message to display with an appropriate context why the authentication is required.
Lets apply the changes and test using curl
. First test without any username password.
We got error 401 Authorization Required
for any request without basic auth credentials. We can also see the realm message in the response header.
Next lets test with added basic auth using -u admin
. The curl command will ask for password, put the correct password and we should see the ok response.
You can try what happened if you put wrong password.
References
Last updated