3scale / APIcast

3scale API Gateway
Apache License 2.0
305 stars 171 forks source link

[THREESCALE-11019] - FAPI advance profile #1466

Closed tkan145 closed 2 months ago

tkan145 commented 4 months ago

What

This PR support https://issues.redhat.com/browse/THREESCALE-11019.

Verification steps:

mkdir certs && cd certs

Generate CA certificate

openssl genrsa -out rootCA.key 2048
openssl req -batch -new -x509 -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem

Request token

Now we have everything set up, we can use curl to authenticate with the client certificate to get the access token.

ACCESS_TOKEN=$(docker compose -p keycloak-env exec gateway curl -k -v -H "Content-Type: application/x-www-form-urlencoded" \
   -d 'grant_type=client_credentials' \
   -d 'client_id=mtls_client_demo' \
   --cert /var/run/secrets/apicast/client.crt \
   --key /var/run/secrets/apicast/client.key \
   --cacert /var/run/secrets/apicast/rootCA.pem \
   "https://keycloak:8443/realms/basic/protocol/openid-connect/token" | jq -r '.access_token')
"cnf": {
    "x5t#S256": "3hhTJwX93ZWWyuuKOzm1k4qo-MH0dfhDC7jgg8ZyR6U"
  },

Now we are ready to test

< HTTP/2 200
< server: openresty
< date: Fri, 14 Jun 2024 06:50:11 GMT
< content-type: application/json
< content-length: 1756
< access-control-allow-origin: *
< access-control-allow-credentials: true
< x-fapi-transaction-id: 83688fb6-0ca9-44d3-9e9f-3b31bcdefd8a
<
tkan145 commented 2 months ago

APIcast does not validate the client (downstream) certificate as regular validation, it only does validation specified in OAuth 2.0 Mutual-TLS Client Authentication and Certificate-Bound Access Tokens. Am I right? If so, shouldn't APIcast validate the certificate as well (it would require cacerts that signed the client cert)?

According to the spec, we only need to verify the thumbprint. And I think user can use TLS Client Certificate Validation if they wish to validate the certificate.

In the verification steps, there is an unknown "keycloak_mtls.json". Maybe leftovers of past tries? Good catch! I tried to automate dev-env and provide necessary information to make testing easier. I will submit another PR to update dev-env once this one is merged.

In the verification steps, make gateway command is missing. (which somewhat important :) ) Hehe fixed.

The verification steps tell that the created client should have enabled the "OAuth 2.0 Mutual TLS Certificate Bound Access Token". Is this really necessary? Maybe it is the setting that adds the cnf claim to the JWT?

Yes it is the settings that will calculate the certificate hash and adds cnf claim to the JWT.

There is no user generated in keycloak. That means that there is no need for users in keycloak and the client cert is enough as user ID? If so, any user with a certificate signed with the same cacert defined for keycloak can get the token?

I believe so, FAPI spec also pointed it out as below:

The use of [MTLS](https://tools.ietf.org/html/rfc8705) for client authentication and sender constraining
access tokens brings significant security benefits over the use of shared secrets. However in some 
deployments the certificates used for [MTLS](https://tools.ietf.org/html/rfc8705) are issued by a Certificate
Authority at an organization level rather than a client level. In such situations it may be common for an
organization with multiple clients to use the same certificates (or certificates with the same DN) across
clients. Implementers should be aware that such sharing means that a compromise of any one client, would
result in a compromise of all clients sharing the same key.

The verification steps miss the client configuration "allow regexp pattern"

Fixed

I wonder if this "OAuth 2.0 Mutual TLS Certificate Bound Access Token" use case deserves its own dev env. Mainly because if I want to reproduce this, I would need to come back to this PR and re-do the verification steps. If not dev-env, maybe developer oriented readme?? what do you think?

Agree

No need to get the APICast IP, just need to update the docker-compose.yml and add 8443 port to the gateway service:

:+1: