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.com
For this demo we will be using
domain_name
asratanboddu
-
Ensure that you have the
nginx-ingress
addon enabled. You can use below command to verifyminikube addons list
If 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
keycloak
folder 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 database
e. 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
api
with the default settings -
Click on the
Mappers
tab, create a new mapper calledgroups
with below settings and Save. -
Click
Add Builtin
and addusername
,email
,profile
. -
Add this newly created scope to your existing Client at
Clients -> Your_Client_Name -> Client Scopes
Select the newly created sccopeapi
at the left box and clickAdd selected
forDefault Client Scopes
NOTE: You client should haveValid Redirect URI
set 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-proxy
folder 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_id
by navigating toClients (Left Navbar)-> Your Client -> Client ID
-
Set the
client_secret
by 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_group
if you want to authenticate on the basis of groups. Only users having this group will be able to access the application -
For
cookie_domains
andwhitelist_domains
, replace the domain name with yourdomain_name
For more details on the remaining values you can refer here
-
-
Navigate to the
oauth2-proxy
folder and set the necessary vallues in theoauth_configmap2.yaml
- Here, we need tp update the Keycloak URL for Logout in the
error.html
section
- 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_mamifest
folder- 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/auth
Here, replace the
domain_name
with 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/.