This is a small PoC for understanding the working and deploying Keycloak and OAuth2-proxy in local k8s environment.
Setting up minikube
-
In /etc/hosts set a sample domain name using the minikube ip. You can get it by using the below command
echo $(minikube ip) -
In /etc/hosts, we will then be setting up the domains for our Keycloak, Oauth and Application
<paste you minikube ip here> domain_name.com <paste you minikube ip here> keycloak.domain_name.com <paste you minikube ip here> auth.domain_name.comFor this demo we will be using
domain_nameasratanboddu -
Ensure that you have the
nginx-ingressaddon enabled. You can use below command to verifyminikube addons listIf you dont have it enabled, you can use the below command to enable it.
minikube addons enable ingress
User authentication and authorization tool
- Deploying Keycloak
- Configuring Keycloak
-
Navigate to the
keycloakfolder and set the necessary environment variables for Keycloak. More information on variable herea. KEYCLOAK_USER : default User
b. KEYCLOAK_PASSWORD : default password
c. PROXY_ADDRESS_FORWARDING: enabling proxy address forwarding
d. DB_VENDOR : specifying the db vendor. Can be one of the following:
- `h2` for the embedded H2 database - `postgres` for the Postgres database - `mysql ` for the MySql database - `mariadb` for the MariaDB database - `oracle` for the Oracle database - `mssql` for the Microsoft SQL Server databasee. DB_ADDR : hostname of the DB being used
f. DB_PORT : port of the database (optional, default is DB vendor default port)
g. DB_DATABASE : name of the database to use (optional, default is keycloak)
h. DB_USER : user to use to authenticate to the database (optional, default is '')
i. DB_PASSWORD : user's password to use to authenticate to the database (optional, default is '')
-
After successfully updating all the necessary values, we can deploy all the yamls using the below command
kubectl apply -f keycloak/.
-
In the Left-Side menu item "Client Scopes", click "Create"
-
Create a new client scope called
apiwith the default settings -
Click on the
Mapperstab, create a new mapper calledgroupswith below settings and Save. -
Click
Add Builtinand addusername,email,profile. -
Add this newly created scope to your existing Client at
Clients -> Your_Client_Name -> Client ScopesSelect the newly created sccopeapiat the left box and clickAdd selectedforDefault Client ScopesNOTE: You client should haveValid Redirect URIset tohttp://<your_domain_name>/oauth2/callback
We have now completed deploying and configuring Keycloak, which will now send the correct tokens to OAuth2-proxy.
-
Navigate to the
oauth2-proxyfolder and set the necessary vallues in theoauth_configmap.yaml-
For the
login_url,redeem_url,validate_url, replace the domain name to yourdomain_name -
Set the
client_idby navigating toClients (Left Navbar)-> Your Client -> Client ID -
Set the
client_secretby navigating toClients (Left Navbar)-> Your Client -> Credentials Tab -> Secret -
Set the
cookie_secret: To generate a strong cookie secret usepython -c 'import os,base64; print(base64.urlsafe_b64encode(os.urandom(16)).decode())' -
Set the
cookie_refresh: To refresh the cookie after this duration -
Set the
keycloak_groupif you want to authenticate on the basis of groups. Only users having this group will be able to access the application -
For
cookie_domainsandwhitelist_domains, replace the domain name with yourdomain_nameFor more details on the remaining values you can refer here
-
-
Navigate to the
oauth2-proxyfolder and set the necessary vallues in theoauth_configmap2.yaml- Here, we need tp update the Keycloak URL for Logout in the
error.htmlsection
- Here, we need tp update the Keycloak URL for Logout in the
-
After successfully updating all the necessary values, we can deploy all the yamls using the below command
kubectl apply -f oauth2-proxy/.
This application is a sample flask applicatioin which can be deployed to demonstrate the authentication for apis.
-
Navigate to the
flask-application/k8s_mamifestfolder- Here in the
ingress.yaml, we will need to add the following annotations to make the servic secure.
nginx.ingress.kubernetes.io/auth-signin: http://auth.<domain_name>.com/oauth2/start?rd=https%3A%2F%2F$host$request_uri nginx.ingress.kubernetes.io/auth-url: http://auth.<domain_name>.com/oauth2/authHere, replace the
domain_namewith your domain name. - Here in the
-
After successfully updating all the necessary values, we can deploy all the yamls using the below command
kubectl apply -f flask-application/k8s_manifest/.

