Get started with Gateway
Put the gateway in front of an API, let one service read invoices through it, and see a read allowed and a delete refused.
Available, with limitsAPI
In this walkthrough you put the gateway in front of an invoices API for the realm foobar. You give the gateway its
identity in the realm, write one policy that lets the billing-service client read invoices, and run the gateway
beside the API. At the end, billing-service reads an invoice through the gateway, and its attempt to delete one is
refused before it reaches the API.
Before you begin
Section titled “Before you begin”- A realm administrator’s session. You’re in a group that holds
foobar’sRealmOwnerprivilege, with your session in$CVYUH_SESSION: Call the API shows how to get it. Every admin call here goes tohttps://foobar.cvyuh.com/api/v0/am. This walkthrough uses the API only. - An HTTP API on a Linux host, listening at
http://127.0.0.1:8801and answeringGET /invoices/1. Any API will do: the gateway needs nothing from it. - On that host: Docker,
openssl,curlandjq, and the gateway’s container image, here$GATEWAY_IMAGE. - An audience for the API. Callers name it
https://api.example.com.
Give the gateway its identity
Section titled “Give the gateway its identity”-
Make the gateway’s key. On the API’s host, make a P-256 key pair, and read the public half’s coordinates for the realm:
Terminal window openssl ecparam -name prime256v1 -genkey -noout | openssl pkcs8 -topk8 -nocrypt -out gateway-key.pemb64url() { openssl base64 -A | tr '+/' '-_' | tr -d '='; }X=$(openssl ec -in gateway-key.pem -pubout -outform DER 2>/dev/null | tail -c 64 | head -c 32 | b64url)Y=$(openssl ec -in gateway-key.pem -pubout -outform DER 2>/dev/null | tail -c 32 | b64url)The private half stays on this host. The realm only ever holds the public half.
-
Activate the realm’s
floorclient. Every realm is born with this client for the gateway, inactive, holding the three scopes the gateway needs. Give it the public key and the two addresses the gateway reads the realm at:Terminal window curl -X PATCH https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client/floor \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "status": "active","jwks": { "keys": [ { "kty": "EC", "crv": "P-256", "kid": "gateway-1", "x": "'$X'", "y": "'$Y'" } ] },"allowedResources": ["https://foobar.cvyuh.com/policies/v1", "https://foobar.cvyuh.com/access/v1"],"defaultAudience": "https://foobar.cvyuh.com/policies/v1" }'The gateway signs in as
floorwith an assertion signed by its key, so it holds no shared secret. It reads the realm’s policies and revocations at/policies/v1, and asks for the decisions a token can’t settle at/access/v1.
Let one service read invoices
Section titled “Let one service read invoices”-
Register the calling service.
billing-servicegets tokens for the invoices API with a secret:Terminal window curl "https://foobar.cvyuh.com/api/v0/am/objects/v1/oauth2Client?id=billing-service" \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "status": "active","clientName": "Billing service","tokenEndpointAuthMethod": "client_secret_basic","clientSecret": "<a long random secret>","grantTypes": ["client_credentials"],"allowedResources": ["https://api.example.com"],"defaultAudience": "https://api.example.com" }' -
Write the policy. It lets
billing-serviceread anything under/invoices/, on whatever host the gateway forwards to:Terminal window curl -i "https://foobar.cvyuh.com/api/v0/am/objects/v1/policy?id=invoices-read" \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-d '{ "active": true,"resources": ["*://*:*/invoices/*"],"actionValues": { "GET": true },"subject": { "type": "JwtClaim", "claimName": "client_id", "claimValue": "billing-service" } }'The answer’s
ETagis the policy’s revision. -
Link it to the
defaultpolicy set and theurlresource type. Each link names the policy’s current revision, and each answer’sETagis the revision the next one names:Terminal window curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/invoices-read/relationships/policySet \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-H 'If-Match: "<rev>"' \-d '{ "targetId": "default" }'curl https://foobar.cvyuh.com/api/v0/am/objects/v1/policy/invoices-read/relationships/resourceType \-H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \-H "Content-Type: application/json" \-H 'If-Match: "<rev>"' \-d '{ "targetId": "url" }'
Run the gateway
Section titled “Run the gateway”-
Start the gateway beside the API. It listens on port 8080 and forwards what it allows to the API:
Terminal window docker run -d --name gateway --network host \-v "$PWD/gateway-key.pem:/etc/gateway/key.pem:ro" \-e ISSUER=https://foobar.cvyuh.com/oauth2 \-e JWT_AUDIENCE=https://api.example.com \-e CLIENT_ID=floor \-e CLIENT_KEY=/etc/gateway/key.pem \-e POLICY_SET=default \-e BACKEND=http://127.0.0.1:8801 \"$GATEWAY_IMAGE"Every setting is in Gateway configuration.
-
Give it a few seconds. Until its first policies and revocation list arrive, the gateway refuses everything.
See it decide
Section titled “See it decide”-
Call without a token. The gateway refuses, and says where to find out how to get one:
Terminal window curl -i http://127.0.0.1:8080/invoices/1HTTP/1.1 401 UnauthorizedWWW-Authenticate: Bearer resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"WWW-Authenticate: DPoP algs="ES256 PS256 RS256", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource" -
Get a token as
billing-service. It’s an access token forhttps://api.example.com, good for five minutes:Terminal window TOKEN=$(curl -s https://foobar.cvyuh.com/oauth2/token \-u "billing-service:<the secret>" \-d grant_type=client_credentials \-d resource=https://api.example.com | jq -r .access_token) -
Read an invoice. The policy allows it, so the gateway forwards it, and you get the API’s own answer:
Terminal window curl -i http://127.0.0.1:8080/invoices/1 -H "Authorization: Bearer $TOKEN"The API receives the request with
x-agent-id: billing-service, the verified caller, which no client can set for itself. -
Try to delete it. No policy allows
DELETE, so the gateway refuses it, and the API never sees it:Terminal window curl -i -X DELETE http://127.0.0.1:8080/invoices/1 -H "Authorization: Bearer $TOKEN"HTTP/1.1 403 ForbiddenWWW-Authenticate: Bearer error="insufficient_scope", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"The body gives the reason,
no_matching_policy.
Check it worked
Section titled “Check it worked”- Set
billing-service’sstatustoinactiveand get a new token: the realm refuses it. A token it already holds keeps working at the gateway until it expires: see A client you set inactive. - Change the policy’s
actionValuesto{ "GET": true, "DELETE": true }: within a minute, the gateway’s next policy poll, the delete reaches the API.
If it doesn’t work
Section titled “If it doesn’t work”| What you see | Why | What to do |
|---|---|---|
Every call with a token is 503 revocations_stale | The gateway can’t sign in to the realm, so it has no revocation list yet | Check that floor is active and holds the gateway’s public key, and that CLIENT_KEY is the matching private half |
The read is 403 no_policies_loaded | The policy isn’t linked to default, or isn’t linked to url | Make both links |
The read is 403 policies_excluded | The policy is linked to default but not yet to url, so the set is incomplete and allows nothing | Link it to url |
The read is 401 invalid_token | The token’s audience isn’t the gateway’s JWT_AUDIENCE, or it’s from another realm | Get the token from https://foobar.cvyuh.com/oauth2/token for the same audience the gateway names |
Every symptom, with its fix, is in Troubleshooting.