# Authentication and Authorization with APISIX and OIDC based Identity provider (Keycloak) Follow the steps to enable authentication and authorization of OPEA services using APISIX api gateway and Identity provider like keycloak ## Prerequisites 1. Make sure ChatQnA service is running and accessible locally 2. Run keycloak, setup a realm with OIDC based authentication and add users with passwords for each user. Steps to start keycloak from official keycloak helm chart ```sh # Prerequisite: Create a PersistentVolume of 9Gi for keycloak-postgress with RWO access (to persist updated keycloak configuration) # Below is a reference to create PersistentVolume which will be used by keycloak-postgress kubectl apply -f - < export accessUrl=http://$NODE_IP:$NODE_PORT/ ```
Apisix helm chart provides configs to change the service type to other options like LoadBalancer (apisix.service.type) and externalTrafficPolicy to 'local'(apisix.service.externalTrafficPolicy). These can be added in values_apisix_gw.yaml

While accessing the published APIs, the HTTP Authorization header of the request should contain the Access token provided by Identity provider as 'Bearer <Access Token>'.

The access token, refresh token, userinfo, user roles and OIDC scopes assigned to user can be obtained by invoking OIDC auth endpoint through UI or token endpoint through curl and providing user credentials.

Below steps can be followed to get access token from keycloak and access the APISIX published ChatQnA API through curl ```sh # Get access token for specified user from keycloak export USER= export PASSWORD= export KEYCLOAK_ADDR= export KEYCLOAK_REALM= export KEYCLOAK_CLIENT_ID= export KEYCLOAK_CLIENT_SECRET= #Invoke Keycloak's OIDC token endpoint to get access token, refresh token and expirt times. (Only Access token is used in the example below) export TOKEN=$(curl -X POST http://${KEYCLOAK_ADDR}/realms/${KEYCLOAK_REALM}/protocol/openid-connect/token -H 'Content-Type: application/x-www-form-urlencoded' -d "grant_type=password&client_id=${KEYCLOAK_CLIENT_ID}&client_secret=${KEYCLOAK_CLIENT_SECRET}&username=${USER}&password=${PASSWORD}" | jq -r .access_token) # follow instructions above to fetch the NODE_IP and NODE_PORT export accessUrl="http://$NODE_IP:$NODE_PORT/chatqna-oidc" # try without token. Shall get response: "Authorization required 401 error" curl -X POST $accessUrl -d '{"text":"What is the revenue of Nike in 2023?","parameters":{"max_new_tokens":17, "do_sample": true}}' -sS -H 'Content-Type: application/json' -w " %{http_code}\n" # try with token. Shall get the correct response from ChatQnA with http code 200 curl -X POST $accessUrl -d '{"text":"What is the revenue of Nike in 2023?","parameters":{"max_new_tokens":17, "do_sample": true}}' -sS -H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' -w " %{http_code}\n" ``` ## Uninstall ```sh # Uninstall apisix helm uninstall auth-apisix-crds --namespace auth-apisix helm uninstall auth-apisix --namespace auth-apisix ``` The crds installed by apisix won't be deleted by helm uninstall. Need to manually delete those crds
All APISIX specific crds can be obtained by 'kubectl get crds | grep apisix'
Each crd can be manually deleted by 'kubectl delete crd/\'