Skip to content
Workload and AI agent identity · Chapter 4 of 20 Day 3: The cluster as an OIDC issuer – the specimen card at the entrance

Day 3: The cluster as an OIDC issuer – the specimen card at the entrance

Goal: verify a ServiceAccount token without calling the API server, watch a forgery fail that check, and reconfigure the cluster so that services outside it can verify its tokens – without otherwise opening up the API server · Duration: about 100 min · Prerequisite: Day 2 completed

Where we start

# on id1
kubectl get --raw /.well-known/openid-configuration

Expected:

{"issuer":"https://kubernetes.default.svc.cluster.local","jwks_uri":"https://172.16.0.126:6443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}

In addition, the pod besucher is running in the namespace default, and since Day 2 /etc/rancher/k3s/config.yaml contains service-account-extend-token-expiration=false.

Introduction

On Day 2 the gatekeeper called the badge office to check a badge – a TokenReview. That works as long as the gatekeeper sits in the same building and has a line to the badge office. A service outside the cluster has neither: it would need access to the API server and a permission of its own to have tokens reviewed.

There is a second way, and almost everything still to come in this course is built on it: the badge office puts up a specimen card of its seals – its public keys. Anyone who wants to check a badge compares the seal with the specimen card themselves. No call, no line, no permission needed. This is how cloud providers verify the tokens of Kubernetes clusters, this is how Keycloak will verify your cluster on Day 4, and this is how OpenID Connect works in the first place.

Today you read the specimen card, use it to check genuine and forged badges, and then hang it outside – so that anyone can read it, but nobody else gets into the building without a badge.

Today you will learn:

  • what a cluster’s JSON Web Key Set (JWKS) contains and how kid links a token to a key,
  • how to verify a token offline with step crypto jwt verify – signature, issuer, audience, time,
  • how to forge a token and see where the forgery fails,
  • why a service outside the cluster is not allowed to read the specimen card at first,
  • how to switch the cluster’s issuer to a reachable address without breaking old tokens,
  • how to allow anonymous access to exactly two paths and to nothing else.

Background

The picture for today: the specimen card at the entrance

Anyone in a museum who has to tell genuine tickets from forged ones gets a specimen card: this is what the seal looks like, this is the watermark. The badge office of your office building does the same.

  • The seal is the signature. The badge office presses it on with a stamp that only it owns – the cluster’s private key.
  • The specimen card is the JWKS, the list of public keys. With it anyone can check whether a seal is genuine, but nobody can produce one.
  • The stamp’s number is the kid. If the badge office has several stamps, the number on the badge says which specimen card to compare against.
  • The address of the badge office (iss) is written on every badge. The notice must hang at this address, otherwise a stranger does not know where to find the specimen card.
  • The notice at reception – the discovery document – says where the specimen card hangs (jwks_uri).
  • So far the notice hung inside, behind the gate. Today you hang it on the outside wall.

The terms

Term Meaning in the picture
JWKS JSON Web Key Set – list of public keys in JSON format specimen card of the seals
JWK a single key in this format one specimen card
kid Key ID – identifier of a key, written in the header of every token number of the stamp
iss Issuer – name and address of the issuer address of the badge office
Discovery document /.well-known/openid-configuration, names issuer and jwks_uri notice at reception
Offline verification verifying a token with the JWKS yourself, without asking the issuer comparing the seal with the specimen card
system:anonymous user name for requests without a badge, if anonymous access is allowed passer-by on the street

Remember: Whoever has the specimen card can check every seal but produce none. That is why the specimen card may be public – the stamp never.

What an offline verification checks – and what it does not

A service that verifies a token with the JWKS checks four things: the signature matches a key from the JWKS, iss is the expected issuer, aud contains its own name, and the time lies between nbf and exp. One thing it cannot check: whether the pod the token is bound to still exists. Only the API server knows that. With offline verification a token is therefore valid until it expires, even if the pod was deleted long ago. The short lifetimes from Day 2 are therefore not a nice extra but the actual safeguard.

Drill 1: Reading the specimen card

Where you are: on id1.

What you have afterwards: your cluster’s JWKS as a file and the connection between the kid in the token and the kid in the key.

Step 1 – create a working folder.

# on id1
mkdir -p ~/kurs/03

Step 2 – fetch the JWKS. The address is in the discovery document under jwks_uri. As an administrator you fetch it via kubectl:

# on id1
kubectl get --raw /openid/v1/jwks > ~/kurs/03/jwks.json
# on id1
jq . ~/kurs/03/jwks.json

Expected (the value n is shortened):

{
  "keys": [
    {
      "use": "sig",
      "kty": "RSA",
      "kid": "IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM",
      "alg": "RS256",
      "n": "7k92o9kuFYIoBJKPn7tYQwASjUQAw0p0k3zIexKT044pqeHCCLOpLLktoIndhas_3lFoVQdJ…",
      "e": "AQAB"
    }
  ]
}
Field Value Meaning
use sig the key is used to verify signatures
kty RSA key type
kid IgxV… identifier; the same value is in the header of every token signed with it
alg RS256 RSA signature with SHA-256
n, e long number, AQAB the public RSA key itself: modulus and exponent (65537)

What you see: A single key. The private counterpart lives on id1 under /var/lib/rancher/k3s/server/tls/service.key and never leaves the server. Compare the kid with the header of a token from Day 2 – it is identical.

Remember: The kid in the token points to a specimen card in the JWKS. Without a matching kid there is nothing to compare against.

Save your progress? Sign up for free

Drill 2: Verifying without a call

Where you are: on id1.

What you have afterwards: a genuine token that passes offline verification, three tokens that fail for different reasons – and a forgery you built yourself.

Step 1 – order a fresh badge for the warehouse door.

# on id1
kubectl create token kasse -n laden --audience lager --duration 10m > ~/kurs/03/lager.jwt

Step 2 – verify offline.

# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks.json --iss https://kubernetes.default.svc.cluster.local --aud lager < ~/kurs/03/lager.jwt
Option Value Meaning
verify verify instead of only display – this time without --insecure
--jwks ~/kurs/03/jwks.json the specimen card; step looks in it for the key with the kid from the token
--iss https://kubernetes.default.svc.cluster.local this must be the issuer’s name
--aud lager this value must be in aud
< …/lager.jwt the token as input

Expected (excerpt – header and payload as with inspect, signature omitted):

{
  "header": {
    "alg": "RS256",
    "kid": "IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM"
  },
  "payload": {
    "aud": [
      "lager"
    ],
    "exp": 1796116976,
    "iat": 1796116376,
    "iss": "https://kubernetes.default.svc.cluster.local",
    "jti": "3b52be6f-504a-46e0-8dff-07684b3a47bd",
    "kubernetes.io": {
      "namespace": "laden",
      "serviceaccount": {
        "name": "kasse",
        "uid": "043ce935-af06-4d95-86fa-64e67fff68e6"
      }
    },
    "nbf": 1796116376,
    "sub": "system:serviceaccount:laden:kasse"
  },

What you see: step only prints the token if all checks pass, and exits with exit code 0. No contact with the API server – only the file jwks.json.

Step 3 – wrong door. The same token, but you verify like the API server, that is, with its audience:

# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks.json --iss https://kubernetes.default.svc.cluster.local --aud https://kubernetes.default.svc.cluster.local < ~/kurs/03/lager.jwt

Expected:

validation failed: invalid audience claim (aud)

Step 4 – expired. The till’s first badge from Day 2, Drill 4:

# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks.json --iss https://kubernetes.default.svc.cluster.local --aud lager < kasse-v2-a.jwt

Expected (your time will differ):

validation failed: token is expired by 38m45.219s (exp)

What you see: Unlike the API server, step grants no minute of tolerance; instead it reports exactly how long the token has been expired.

Step 5 – prepare a forgery. Now you play the attacker. They know the format, the kid and the issuer – all of it is in every token and in the public JWKS. They want to pose as the ServiceAccount admin in the namespace kube-system. First, the payload they want to sign:

# on id1
nano ~/kurs/03/faelschung.json
~/kurs/03/faelschung.json new file
{
  "kubernetes.io": {
    "namespace": "kube-system",
    "serviceaccount": {
      "name": "admin"
    }
  }
}

Step 6 – make your own stamp. The attacker creates an RSA key pair and gives it the cluster’s kid:

# on id1
step crypto jwk create ~/kurs/03/faelscher.pub.json ~/kurs/03/faelscher.json --kty RSA --size 2048 --kid IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM --no-password --insecure
Option Value Meaning
jwk create <pub> <priv> two file names public and private key in JWK format
--kty RSA --size 2048 RSA with 2048 bits, like the cluster
--kid the cluster’s kid the forged stamp gets the number of the real one
--no-password --insecure store the private key unencrypted – only for this exercise

Expected:

Your public key has been saved in /home/ubuntu/kurs/03/faelscher.pub.json.
Your private key has been saved in /home/ubuntu/kurs/03/faelscher.json.

Step 7 – sign the forgery. exp should be ten minutes in the future. Calculate the point in time:

# on id1
date -d "+10 min" +%s

Expected (your number will differ):

1796117084

Put your number after --exp:

# on id1
step crypto jwt sign --key ~/kurs/03/faelscher.json --iss https://kubernetes.default.svc.cluster.local --aud lager --sub system:serviceaccount:kube-system:admin --exp 1796117084 < ~/kurs/03/faelschung.json > ~/kurs/03/gefaelscht.jwt
Option Value Meaning
--key the forger’s private key used for signing; the kid comes from the key file
--iss, --aud, --sub values as in the genuine token issuer, audience, subject
--exp seconds since 1970 expiry time
< faelschung.json additional fields for the payload; without this input step waits for text from the keyboard

The command prints nothing.

Step 8 – look at the forgery.

# on id1
step crypto jwt inspect --insecure < ~/kurs/03/gefaelscht.jwt

Expected (signature omitted):

{
  "header": {
    "alg": "RS256",
    "kid": "IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM",
    "typ": "JWT"
  },
  "payload": {
    "aud": "lager",
    "exp": 1796117084,
    "iat": 1796116491,
    "iss": "https://kubernetes.default.svc.cluster.local",
    "jti": "aacdd0c482fb9bbbd61f583abd417c3adec1ff8a5e7bf87d8b2cd31c5d9e2007",
    "kubernetes.io": {
      "namespace": "kube-system",
      "serviceaccount": {
        "name": "admin"
      }
    },
    "nbf": 1796116491,
    "sub": "system:serviceaccount:kube-system:admin"
  },

What you see: To the naked eye a flawless badge: correct kid, correct issuer, valid time. That is exactly why the switch for inspect is called --insecure.

Step 9 – verify the forgery.

# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks.json --iss https://kubernetes.default.svc.cluster.local --aud lager < ~/kurs/03/gefaelscht.jwt

Expected:

validation failed: invalid signature

Step 10 – and the API server? Build a TokenReview as on Day 2:

# on id1
jq -n --rawfile t ~/kurs/03/gefaelscht.jwt '{apiVersion: "authentication.k8s.io/v1", kind: "TokenReview", spec: {token: ($t | rtrimstr("\n")), audiences: ["lager"]}}' > ~/kurs/03/review-faelschung.json
# on id1
kubectl create -f ~/kurs/03/review-faelschung.json -o jsonpath="{.status}"

Expected:

{"error":"[invalid bearer token, go-jose/go-jose: error in cryptographic primitive]","user":{}}

What you see: Neither verifier is fooled by the genuine kid. They use the kid to look up the public key and recompute the signature – and it only matches the cluster’s stamp.

Remember: Anyone can read a token, and anyone can build one. It is only genuine if the signature matches the issuer’s public key – and that is exactly the check a service must never skip.

Save your progress? Sign up for free

Drill 3: The notice hangs inside

Where you are: on id2, then on id1.

What you have afterwards: proof that a service outside the cluster cannot read the specimen card today, and the rule that determines this.

id2 now plays an outside service, for example an identity provider that is supposed to verify your cluster’s tokens. It has no badge from the cluster.

Step 1 – a quick look without a badge.

# on id2
curl -sk https://172.16.0.126:6443/openid/v1/jwks

-k means: do not verify the server’s certificate. As a normal user id2 does not have the cluster CA yet; for this first look that is enough. In Drill 4 you fetch the CA and drop -k.

Expected:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}

What you see: 401. k3s does not allow anonymous requests – not even for the specimen card.

Step 2 – who may read the specimen card? Back on id1. Kubernetes ships a rule for this:

# on id1
kubectl get clusterrolebinding system:service-account-issuer-discovery -o yaml

Expected (excerpt):

roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: system:service-account-issuer-discovery
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: Group
  name: system:serviceaccounts
# on id1
kubectl get clusterrole system:service-account-issuer-discovery -o yaml

Expected (excerpt):

rules:
- nonResourceURLs:
  - /.well-known/openid-configuration
  - /.well-known/openid-configuration/
  - /openid/v1/jwks
  - /openid/v1/jwks/
  verbs:
  - get

What you see: The role only allows get on four paths – discovery document and JWKS, each with and without a trailing slash. It is bound to the group system:serviceaccounts: every pod in the cluster may read the notice, a stranger may not. So two things are missing: the stranger must be let in as an anonymous visitor at all, and the role must apply to them as well.

Remember: So far the specimen card hangs behind the gate. Anyone who comes without a badge is turned away before anyone asks what they want to read.

Save your progress? Sign up for free

Drill 4: Hanging the notice outside

Where you are: on id1, in between on your machine and on id2.

What you have afterwards: a cluster that announces itself as issuer under a reachable address and shows the discovery document and JWKS to everyone – and nothing else.

Three changes are needed. First, the issuer needs an address under which a stranger can find the notice; https://kubernetes.default.svc.cluster.local only exists inside the cluster. You use the API server’s address, https://172.16.0.126:6443. Second, the API server must let anonymous visitors in, but only on the two paths. Third, the group of anonymous users needs the role from Drill 3.

Step 1 – anonymous visitors only at two doors. Newer Kubernetes versions – 1.37 in the lab – can restrict anonymous access to individual paths, in a configuration file for authentication:

# on id1
sudo nano /etc/rancher/k3s/authn.yaml
/etc/rancher/k3s/authn.yaml new file
apiVersion: apiserver.config.k8s.io/v1
kind: AuthenticationConfiguration
anonymous:
  enabled: true
  conditions:
    - path: /.well-known/openid-configuration
    - path: /openid/v1/jwks
Field Meaning
kind: AuthenticationConfiguration configuration of the API server’s authentication
anonymous.enabled: true requests without a badge are let in as system:anonymous …
conditions … but only on these paths; everywhere else it stays at 401

Step 2 – switch the issuer and include the file.

# on id1
sudo nano /etc/rancher/k3s/config.yaml
/etc/rancher/k3s/config.yaml changes in green
disable:
  - traefik
kube-apiserver-arg:
  - service-account-extend-token-expiration=false
  - service-account-issuer=https://172.16.0.126:6443
  - service-account-issuer=https://kubernetes.default.svc.cluster.local
  - authentication-config=/etc/rancher/k3s/authn.yaml
Entry Meaning
first service-account-issuer the new name; from now on all tokens are issued with it, and jwks_uri is derived from it
second service-account-issuer the old name; tokens with this iss are still accepted, but no new ones are issued
authentication-config path to the file from Step 1

The order is crucial: the first entry issues, all further ones are only accepted. Without the second entry every token issued before the switch would become invalid – which at that moment means almost every pod badge in the cluster.

Step 3 – restart.

# on id1
sudo systemctl restart k3s
# on id1
kubectl wait --for=condition=Ready node/id1 --timeout=120s

Expected:

node/id1 condition met

Step 4 – the new notice.

# on id1
kubectl get --raw /.well-known/openid-configuration

Expected:

{"issuer":"https://172.16.0.126:6443","jwks_uri":"https://172.16.0.126:6443/openid/v1/jwks","response_types_supported":["id_token"],"subject_types_supported":["public"],"id_token_signing_alg_values_supported":["RS256"]}

What you see: issuer is now a real address, and jwks_uri lies beneath it.

Step 5 – what happens at the outside wall? On id2, again with the quick look:

# on id2
curl -sk https://172.16.0.126:6443/version

Expected:

{
  "kind": "Status",
  "apiVersion": "v1",
  "metadata": {},
  "status": "Failure",
  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
}

/version is not in the conditions – here it stays at 401. On the notice’s path, by contrast, the API server in the author’s lab reported this before Step 6:

"message": "forbidden: User \"system:anonymous\" cannot get path \"/.well-known/openid-configuration\"",
"code": 403

403 instead of 401: the passer-by is now let in and recognized as system:anonymous, but is not allowed to do anything yet.

Step 6 – the role for passers-by.

# on id1
nano ~/kurs/03/aushang-rbac.yaml
~/kurs/03/aushang-rbac.yaml new file
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: aushang-fuer-alle
subjects:
  - kind: Group
    name: system:unauthenticated
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: ClusterRole
  name: system:service-account-issuer-discovery
  apiGroup: rbac.authorization.k8s.io

system:unauthenticated is the group every anonymous visitor ends up in. The role is the same one from Drill 3 – only get on the four paths.

# on id1
kubectl apply -f ~/kurs/03/aushang-rbac.yaml

Expected:

clusterrolebinding.rbac.authorization.k8s.io/aushang-fuer-alle created

Step 7 – bring the cluster CA to id2. For id2 to verify the API server without -k, it needs the CA certificate. It has been on id1 as cluster-ca.crt since Day 1. scp -3 copies from one VM to the other via your machine:

# on your machine
scp -3 id1:cluster-ca.crt id2:
  • -3 routes the data through your machine instead of id1 having to log in to id2 itself.
  • id1:cluster-ca.crt is the source, id2: the target – the home folder on id2.

The command prints nothing.

Step 8 – read the notice from outside.

# on id2
curl -s --cacert cluster-ca.crt https://172.16.0.126:6443/.well-known/openid-configuration -o oidc.json
# on id2
jq . oidc.json

Expected:

{
  "issuer": "https://172.16.0.126:6443",
  "jwks_uri": "https://172.16.0.126:6443/openid/v1/jwks",
  "response_types_supported": [
    "id_token"
  ],
  "subject_types_supported": [
    "public"
  ],
  "id_token_signing_alg_values_supported": [
    "RS256"
  ]
}

Step 9 – fetch the specimen card from outside.

# on id2
curl -s --cacert cluster-ca.crt https://172.16.0.126:6443/openid/v1/jwks -o jwks.json
# on id2
jq ".keys[] | {kid, kty, alg, use}" jwks.json

.keys[] iterates over every key, {kid, kty, alg, use} shows only these four fields.

Expected:

{
  "kid": "IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM",
  "kty": "RSA",
  "alg": "RS256",
  "use": "sig"
}

What you see: Without a badge, with verified TLS: discovery document and JWKS are readable from outside. These are exactly the two addresses Keycloak will query on Day 4.

Remember: First put the new issuer in front, keep the old one behind it. Then open only the two paths of the notice to passers-by – via authentication and via RBAC.

If it does not work

If k3s does not come up after the restart, the reason is in the log: sudo journalctl -u k3s --since "-5 min" --no-pager. Older Kubernetes versions do not know the field anonymous in the AuthenticationConfiguration yet; check the version with kubectl version. If id2 still gets 401 instead of 403, the file was not loaded – check the path after authentication-config.

Save your progress? Sign up for free

Drill 5: Old and new badges

Where you are: on id1.

What you have afterwards: proof that badges with the old issuer remain valid, that new ones carry the new name and can be verified with the publicly fetched JWKS.

Step 1 – a badge from before the switch. The visitor badge from Day 2, Drill 5:

# on id1
step crypto jwt inspect --insecure < besucher-neu.jwt

Expected (excerpt – look for iss and exp):

    "exp": 1796117112,
    "iss": "https://kubernetes.default.svc.cluster.local",

It still carries the old name. exp is 09:25:12 UTC; check with date -u +%T that it is not that late yet – otherwise order a new visitor as on Day 2.

Step 2 – does the API server still accept it?

# on id1
TOKEN=$(cat besucher-neu.jwt)
# on id1
curl -s --cacert cluster-ca.crt -X POST -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" -d '{"apiVersion":"authentication.k8s.io/v1","kind":"SelfSubjectReview"}' https://172.16.0.126:6443/apis/authentication.k8s.io/v1/selfsubjectreviews -o wer.json
# on id1
jq -r ".status.userInfo.username" wer.json

Expected:

system:serviceaccount:default:default

What you see: Accepted – thanks to the second service-account-issuer entry.

Step 3 – a new badge.

# on id1
kubectl create token kasse -n laden --audience lager --duration 10m > ~/kurs/03/lager-neu.jwt
# on id1
step crypto jwt inspect --insecure < ~/kurs/03/lager-neu.jwt

Expected (excerpt – look for kid, aud and iss):

    "kid": "IgxVgJZWT78AbhR-GQDTcPv2lHU2X_x6hHv1u_4m7zM"
    "aud": [
      "lager"
    "iss": "https://172.16.0.126:6443",

What you see: New issuer, but the same stamp (kid). The name has changed, not the key.

Step 4 – verify like a stranger. This time you fetch the JWKS the way id2 does – without a badge, via the public address:

# on id1
curl -s --cacert cluster-ca.crt https://172.16.0.126:6443/openid/v1/jwks -o ~/kurs/03/jwks-oeffentlich.json
# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks-oeffentlich.json --iss https://172.16.0.126:6443 --aud lager < ~/kurs/03/lager-neu.jwt

Expected (excerpt – look for iss and sub):

    "iss": "https://172.16.0.126:6443",
    "sub": "system:serviceaccount:laden:kasse"

What you see: The verification passes. An outside service needs only three things for it: the issuer’s public address, the expected audience and trust in the TLS certificate.

Remember: An issuer can move without changing its stamp. During the move the API server accepts both names – an outside verifier, however, only the one you give it.

Save your progress? Sign up for free

Diagnosis task

A colleague has built a service that verifies tokens offline with the public JWKS, just as you did in Drill 5. Since the switch it rejects some of the tokens, but not others. They show you one of the rejected ones:

# on id1
step crypto jwt verify --jwks ~/kurs/03/jwks-oeffentlich.json --iss https://172.16.0.126:6443 --aud lager < ~/kurs/03/lager.jwt

Expected:

validation failed: invalid issuer claim (iss)

What is the cause, and what do you do?

Solution path:

  1. The signature is not the problem – the message explicitly names iss.
  2. step crypto jwt inspect --insecure < ~/kurs/03/lager.jwt shows "iss": "https://kubernetes.default.svc.cluster.local": the token was issued in Drill 2, before the switch.
  3. The API server accepts both names, your colleague’s service only the new one. The rejected tokens are the old ones, the accepted ones the new ones.
  4. Solution: Change nothing in the service – just wait. Since Day 2 pod tokens are valid for at most one hour plus about one minute; the kubelet renews projected tokens anyway. After that time every valid badge carries the new name. Then the second service-account-issuer can also be removed from config.yaml. If you cannot wait, let the service accept both names temporarily – but never permanently.

Challenge

You have switched on anonymous access. That is an intervention in the API server’s security. Prove that a passer-by can read nothing apart from the two notice paths – not the list of namespaces, not even the version. Write down the commands before you read the solution.

Solution: From id2, without a badge:

# on id2
curl -sk https://172.16.0.126:6443/api/v1/namespaces

Expected (excerpt):

  "message": "Unauthorized",
  "reason": "Unauthorized",
  "code": 401
# on id2
curl -sk https://172.16.0.126:6443/version

Expected: also 401 Unauthorized.

Not 403 but 401: on these paths the passer-by is not even let in as system:anonymous, because they are not in the conditions. Even if someone later accidentally gives the group system:unauthenticated more rights, they only take effect on the two paths. That is the advantage of the path conditions over the old switch --anonymous-auth=true, which let passers-by in everywhere.

Save your progress? Sign up for free

Checkpoint

# on id2
curl -s --cacert cluster-ca.crt https://172.16.0.126:6443/.well-known/openid-configuration

Expected: the discovery document with "issuer":"https://172.16.0.126:6443".

# on id2
curl -sk https://172.16.0.126:6443/version

Expected: 401 Unauthorized.

# on id1
kubectl get clusterrolebinding aushang-fuer-alle

Expected: a line with ClusterRole/system:service-account-issuer-discovery.

Test yourself

  1. Why may the JWKS be public, but the private key never?
  2. A token has the correct kid, the correct issuer and has not expired. Is that enough?
  3. What can an offline verification with the JWKS not determine that a TokenReview can?
  4. Why are there two service-account-issuer entries in config.yaml, and why does their order matter?
  5. A passer-by gets 403 on one path and 401 on another. What does the difference tell you?
  6. When may you remove the old issuer from the configuration?

Answers:

  1. With the public key you can only verify signatures, not create them. Whoever has the private key can issue tokens for any ServiceAccount.
  2. No. Anyone can build a token with these values (Drill 2). What matters is that the signature matches the public key from the issuer’s JWKS – and that aud names your own service.
  3. Whether the pod or ServiceAccount the token is bound to still exists. Offline a token is valid until exp, even if its pod was deleted long ago.
  4. The first entry is the new name that tokens are issued with. Further entries are only accepted. If the old name were in front, tokens would still be issued with the internal name, which a stranger cannot verify.
  5. 403: the path is open to anonymous users, but RBAC forbids access. 401: on this path anonymous users are not even let in.
  6. When no valid token with the old iss can exist any more – that is, after the longest lifetime of a token in the cluster, since Day 2 one hour plus tolerance.

Interview note

“How can a service outside the cluster verify ServiceAccount tokens?” – Good answer: the API server is an OIDC issuer. Under /.well-known/openid-configuration and /openid/v1/jwks it publishes the issuer and the public keys. The service verifies signature, iss, aud and time offline. For this, --service-account-issuer must be an HTTPS address reachable from outside, and both paths must be readable without authentication. If they dig deeper: when changing the issuer, keep the old one as a second entry; the pod binding cannot be checked offline, hence short lifetimes.

Enterprise note

Clouds usually do not publish the discovery document and JWKS directly from the API server, but as static files in a public bucket or behind a CDN – the API server then stays completely private. You can do the same self-hosted: fetch the documents regularly and put them on a web server with a public certificate, and set --service-account-issuer to its address. What matters then is a process for key rotation: put the new key into the published JWKS first, and only then sign with it. And: the issuer’s certificate must be trusted by the verifiers – nobody outside knows a cluster’s own CA.

Summary

  • The JWKS contains the cluster’s public keys; the kid in the token points to the matching one.
  • step crypto jwt verify --jwks … --iss … --aud … verifies a token offline: signature, issuer, audience, time.
  • A forgery with the correct kid and its own key fails on the signature – with step as with the API server.
  • Offline you cannot check whether a token’s pod still exists.
  • The issuer is now called https://172.16.0.126:6443; the old name is still accepted for a transition period.
  • An AuthenticationConfiguration with anonymous.conditions and a binding to system:unauthenticated open only the discovery document and JWKS to passers-by.

Commands at a glance

Command What for
kubectl get --raw /openid/v1/jwks the cluster’s JWKS as administrator
step crypto jwt verify --jwks <datei> --iss <iss> --aud <aud> < token verify a token offline
step crypto jwk create <pub> <priv> --kty RSA --kid <kid> --no-password --insecure key pair in JWK format
step crypto jwt sign --key <priv> --iss … --aud … --sub … --exp … < inhalt.json sign a token
date -d "+10 min" +%s point in time in seconds since 1970
kubectl get clusterrole system:service-account-issuer-discovery -o yaml who may read the notice
AuthenticationConfiguration with anonymous.conditions restrict anonymous access to individual paths
kube-apiserver-arg: [service-account-issuer=…] (twice) change the issuer, keep accepting the old one
scp -3 id1:<datei> id2: copy a file between two VMs via your own machine
curl -s --cacert cluster-ca.crt https://<api>/openid/v1/jwks fetch the JWKS from outside

Contents

More