Skip to content
GatewayMarkdown

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.

  • A realm administrator’s session. You’re in a group that holds foobar’s RealmOwner privilege, with your session in $CVYUH_SESSION: Call the API shows how to get it. Every admin call here goes to https://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:8801 and answering GET /invoices/1. Any API will do: the gateway needs nothing from it.
  • On that host: Docker, openssl, curl and jq, and the gateway’s container image, here $GATEWAY_IMAGE.
  • An audience for the API. Callers name it https://api.example.com.
  1. 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.pem
    b64url() { 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.

  2. Activate the realm’s floor client. 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 floor with 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.

  1. Register the calling service. billing-service gets 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" }'
  2. Write the policy. It lets billing-service read 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 ETag is the policy’s revision.

  3. Link it to the default policy set and the url resource type. Each link names the policy’s current revision, and each answer’s ETag is 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" }'
  1. 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.

  2. Give it a few seconds. Until its first policies and revocation list arrive, the gateway refuses everything.

  1. 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/1
    HTTP/1.1 401 Unauthorized
    WWW-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"
  2. Get a token as billing-service. It’s an access token for https://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)
  3. 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.

  4. 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 Forbidden
    WWW-Authenticate: Bearer error="insufficient_scope", resource_metadata="https://api.example.com/.well-known/oauth-protected-resource"

    The body gives the reason, no_matching_policy.

  • Set billing-service’s status to inactive and 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 actionValues to { "GET": true, "DELETE": true }: within a minute, the gateway’s next policy poll, the delete reaches the API.
What you seeWhyWhat to do
Every call with a token is 503 revocations_staleThe gateway can’t sign in to the realm, so it has no revocation list yetCheck 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_loadedThe policy isn’t linked to default, or isn’t linked to urlMake both links
The read is 403 policies_excludedThe policy is linked to default but not yet to url, so the set is incomplete and allows nothingLink it to url
The read is 401 invalid_tokenThe token’s audience isn’t the gateway’s JWT_AUDIENCE, or it’s from another realmGet the token from https://foobar.cvyuh.com/oauth2/token for the same audience the gateway names

Every symptom, with its fix, is in Troubleshooting.