Hi all,
I’m trying to enabe user authentication in istio 1.6.3 using a keycloak server.
Testing my framework with the demo token from the documentation, it works. But with the Keycloak token the authorization fails.
Here the step I followed
RequestAuthentication with demo token - File h-test-ingress.yaml
apiVersion: "security.istio.io/v1beta1"
kind: "RequestAuthentication"
metadata:
name: "h-ingress-jwt"
namespace: istio-system
spec:
selector:
matchLabels:
istio: ingressgateway
jwtRules:
- issuer: "testing@secure.istio.io"
jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json"
---
Apply the file an test without token, wrong token and correct token
export TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s)
sysop@hdev:~/software/hproject$ export TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s)
sysop@hdev:~/software/hproject$ echo -e "\n\nWITHOUT token"
WITHOUT token
sysop@hdev:~/software/hproject$ curl --insecure https://www.h.net -s -o /dev/null -w "%{http_code}\n"
200
sysop@hdev:~/software/hproject$ echo -e "\n\nwith INVALID token"
with INVALID token
sysop@hdev:~/software/hproject$ curl --insecure --header "Authorization: Bearer pippo" https://www.h.net -s -o /dev/null -w "%{http_code}\n"
401
sysop@hdev:~/software/hproject$ echo -e "\n\nWITH token"
WITH token
sysop@hdev:~/software/hproject$ curl --insecure --header "Authorization: Bearer $TOKEN" https://www.h.net -s -o /dev/null -w "%{http_code}\n"
200
sysop@hdev:~/software/hproject
The access with token gives correctly 200
RequestAuthentication with keycloak token - File h-keycloak2-ingress.yaml
apiVersion: "security.istio.io/v1beta1"
kind: "RequestAuthentication"
metadata:
name: "h-ingress-jwt"
namespace: istio-system
spec:
selector:
matchLabels:
istio: ingressgateway
jwtRules:
- issuer: "http://192.168.202.21:8000/auth/realms/istio"
jwksUri: "http://192.168.202.21:8000/auth/realms/istio/protocol/openid-connect/certs"
---
Apply the file
Execute a login to Keycloak with user test (verify login works) and get the access token
sysop@hdev:~/software/hproject$ curl --insecure -L -X POST ‘https://192.168.202.21:8443/auth/realms/istio/protocol/openid-connect/token’
-H ‘Content-Type: application/x-www-form-urlencoded’
–data-urlencode ‘client_id=cistio’
–data-urlencode ‘grant_type=password’
–data-urlencode ‘client_secret=d74ae23c-4b7b-4f88-8876-4a0456e6f832’
–data-urlencode ‘scope=openid’
–data-urlencode ‘username=test’
–data-urlencode ‘password=aaaaaaaa’ | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 204 100 72 100 132 529 970 --:–:-- --:–:-- --:–:-- 1500
{
“error”: “invalid_grant”,
“error_description”: “Invalid user credentials”
}
sysop@hdev:~/software/hproject$ curl --insecure -L -X POST ‘https://192.168.202.21:8443/auth/realms/istio/protocol/openid-connect/token’
-H ‘Content-Type: application/x-www-form-urlencoded’
–data-urlencode ‘client_id=cistio’
–data-urlencode ‘grant_type=password’
–data-urlencode ‘client_secret=d74ae23c-4b7b-4f88-8876-4a0456e6f832’
–data-urlencode ‘scope=openid’
–data-urlencode ‘username=test’
–data-urlencode ‘password=test’ | jq
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 3332 100 3204 100 128 24646 984 --:–:-- --:–:-- --:–:-- 25630
{
“access_token”: “eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJtS3hqTHFWVnFTSThZRzJWUFZBZEhmQkljN24zZ0NXS1UyMTZGSTU1NGN3In0.eyJleHAiOjE1OTQxMzc0MDksImlhdCI6MTU5NDEzNzEwOSwianRpIjoiNTdiMjY5OTAtNWQzYi00YzRlLThhYTYtMGQ1ZjdiZWFhOTA4IiwiaXNzIjoiaHR0cHM6Ly8xOTIuMTY4LjIwMi4yMTo4NDQzL2F1dGgvcmVhbG1zL2lzdGlvIiwiYXVkIjoiYWNjb3VudCIsInN1YiI6IjdmNzdiOWI0LThjNzgtNDM4MS04NTIzLTAwZDc5MTgzNzZhZiIsInR5cCI6IkJlYXJlciI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Y3IiOiIxIiwiZW1haWxfdmVyaWZpZWQiOmZhbHNlLCJuYW1lIjoidGVzdCB0ZXN0IiwicHJlZmVycmVkX3VzZXJuYW1lIjoidGVzdCIsImdpdmVuX25hbWUiOiJ0ZXN0IiwiZmFtaWx5X25hbWUiOiJ0ZXN0IiwiZW1haWwiOiJ0ZXN0QGgubmV0In0.iySr1nUyvuKp5vS11lWgqkXWZFjrpwbOxQR2vApMy8oq4AZ9vexraSXuS4OJFt7pmWsCvGJbcMo1dByV3d9ZslswCeorMTCEQu9Cy1TvmVM6zvWWR-r-MtGcK3RJ12fKRgYF0tfK_JTVgYHBu_bbhiXup3NellBtprF8Sh8KIsKcnVRpZPqihOoBIJrK3tUY4ErKk5WFV-evprKMAi_yaKGXgMhrOtpwrY9iygcSDfRmTVIqJJ0OkqQo9OA8H209_rV8PpL5zwz55LUlNkfqDIbS-c–Vt2lZlJ5zd4QJGLqyX5l_TNfmJp2JZMvBz3fnxnWosz5L5AIIXUkdbtRew”,
“not-before-policy”: 0,
“session_state”: “ddcf6d03-8347-496c-abc1-c73f22aabb20”,
“scope”: “openid profile email”
}
sysop@hdev:~/software/hproject$
Set the token with the last generated access token
sysop@hdev:~/software/hproject$ export TOKEN="eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJtS3hqTHFWVnFTSThZRzJWUFZBZEhmQkljN24zZ0NXS1UyMTZGSTU1NGN3In0.eyJleHAiOjE1OTQxMzc0MDksImlhdCI6MTU5NDEzNzEwOSwianRpIjoiNTdiMjY5OTAtNWQzYi00YzRlLThhYTYtMGQ1ZjdiZWFhOTA4IiwiaXNzIjoiaHR0cHM6Ly8xOTIuMTY4LjIwMi4yMTo4NDQzL2F1dGgvcmVhbG1zL2lzdGlvIiwiYXVkIjoiYWNjb3VudCIsInN1YiI6IjdmNzdiOWI0LThjNzgtNDM4MS04NTIzLTAwZDc5MTgzNzZhZiIsInR5cCI6IkJ
...
BaLnlk_gOFfOTSmJTkfzVvTTmeagOAwYt8BVC9ePV26DYEaR0qcz1vTaHpN3BwDh0rqpsyHDHn9T7WDw8fxuJ8MbHcnUsGP5Y6PaKXQV4rpAqzu6wDC7_mbtxvCGolJ3uwzYsoenDWfhjCnRdumO8vSXnJA39jRPrbCHQonXB_WCOLlcr5PWO4d0yVd8437d0--eBA"
sysop@hdev:~/software/hproject$
Test the access
sysop@hdev:~/software/hproject$ echo -e "\n\nWITHOUT token"
WITHOUT token
sysop@hdev:~/software/hproject$ curl --insecure https://www.h.net -s -o /dev/null -w "%{http_code}\n"
200
sysop@hdev:~/software/hproject$ echo -e "\n\nwith INVALID token"
with INVALID token
sysop@hdev:~/software/hproject$ curl --insecure --header "Authorization: Bearer pippo" https://www.h.net -s -o /dev/null -w "%{http_code}\n"
401
sysop@hdev:~/software/hproject$ echo -e "\n\nWITH token"
WITH token
sysop@hdev:~/software/hproject$ curl --insecure --header "Authorization: Bearer $TOKEN" https://www.h.net -s -o /dev/null -w "%{http_code}\n"
401
sysop@hdev:~/software/hproject$
The token is rejected and I receive a 401 code.
My framework:
- 4 KVM virtual machines with Ubuntu 20.04
- K3S kubernetes distribution (1 master, 3 workers)
- istio 1.6.3 (upgraded from 1.6.1 upgraded from 1.6.0)
- keykloak server installed in the same cluster using the helm chart codecentric/keycloak setting values to expose the service using loadbalancer and port 8000 for http and 8443 for https
- a keycloak realm called istio, a client called cistio and a user called test
- the service at https://www.h.net is an httpbin application
- the https certificates are self-signed
What I’m doing wrong?
There is any working example of RequestAuthorization using keycloak?