JWT 令牌
此任务向您展示如何设置 Istio 授权策略以强制基于 JSON Web Token (JWT) 的访问。 Istio 授权策略支持字符串类型、字符串列表类型和空格分隔字符串类型的 JWT 声明。
开始之前
在开始这个任务之前,请先完成以下操作:
完成 Istio 最终用户身份验证任务。
阅读 Istio 授权概念。
参照 Istio 安装指南安装 Istio。
部署两个工作负载(workload):
httpbin和curl。将它们部署在同一个命名空间中,例如
foo。每个工作负载都在前面运行一个 Envoy 代理。您可以使用以下命令来部署它们:$ kubectl create ns foo $ kubectl apply -f <(istioctl kube-inject -f @samples/httpbin/httpbin.yaml@) -n foo $ kubectl apply -f <(istioctl kube-inject -f @samples/curl/curl.yaml@) -n foo使用下面的命令验证
curl能够正常访问httpbin服务:$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl http://httpbin.foo:8000/ip -sS -o /dev/null -w "%{http_code}\n" 200
允许包含有效 JWT 和 列表类型声明的请求
以下命令为
foo命名空间下的httpbin工作负载创建一个名为jwt-example的身份验证策略。这个策略使得httpbin工作负载接收 Issuer 为testing@secure.istio.io的 JWT 令牌:$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: RequestAuthentication metadata: name: "jwt-example" namespace: foo spec: selector: matchLabels: app: httpbin jwtRules: - issuer: "testing@secure.istio.io" jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/jwks.json" EOF验证使用无效 JWT 的请求被拒绝:
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer invalidToken" -w "%{http_code}\n" 401验证没有 JWT 令牌的请求被允许,因为以上策略不包含授权策略:
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n" 200以下命令为
foo命名空间下的httpbin工作负载创建一个名为require-jwt的授权策略。这个策略要求所有发往httpbin服务的请求都要包含一个将requestPrincipal设置为testing@secure.istio.io/testing@secure.istio.io的有效 JWT。Istio 使用/连接 JWT 令牌的iss和sub以组成requestPrincipal字段。$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: foo spec: selector: matchLabels: app: httpbin action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] EOF获取
iss和sub都为testing@secure.istio.io的 JWT。这会让 Istio 生成的requestPrincipal属性值为testing@secure.istio.io/testing@secure.istio.io:$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode {"exp":4685989700,"foo":"bar","iat":1532389700,"iss":"testing@secure.istio.io","sub":"testing@secure.istio.io"}验证使用有效 JWT 的请求被允许:
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n" 200验证没有 JWT 的请求被拒绝:
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -w "%{http_code}\n" 403以下命令更新
require-jwt授权策略,使其同时要求 JWT 包含一个名为groups值为group1的声明:$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: foo spec: selector: matchLabels: app: httpbin action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] when: - key: request.auth.claims[groups] values: ["group1"] EOF获取
groups声明列表为group1和group2的 JWT:$ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/groups-scope.jwt -s) && echo "$TOKEN_GROUP" | cut -d '.' -f2 - | base64 --decode - {"exp":3537391104,"groups":["group1","group2"],"iat":1537391104,"iss":"testing@secure.istio.io","scope":["scope1","scope2"],"sub":"testing@secure.istio.io"}验证包含 JWT 且 JWT 中包含名为
groups值为group1声明的请求被允许:$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN_GROUP" -w "%{http_code}\n" 200验证包含 JWT,但 JWT 不包含
groups声明的请求被拒绝:$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN" -w "%{http_code}\n" 403
允许具有有效 JWT 和空格分隔声明的请求
一些 JWT 声明将多个值编码为单个空格分隔的字符串,而不是 JSON 数组。
OAuth2 scope 声明是此模式的一个众所周知的示例,在
RFC 6749 中定义为空格分隔的列表。
Istio 始终将 scope 和 permission 声明视为以空格分隔,
将它们拆分为授权策略可以匹配的单独值。对于使用相同编码的其他自定义声明,
请使用 JWTRule 中的 spaceDelimitedClaims 字段来显式选择它们。
如果没有此字段,自定义声明值(如 "admin editor")将作为单个精确字符串进行匹配。
{"iss": "testing@secure.istio.io", "roles": "admin editor"}下载 JWT 生成脚本和签名密钥:
$ wget --no-verbose https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/gen-jwt.py $ wget --no-verbose https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/key.pem以下命令更新
jwt-example请求身份验证策略以声明roles声明以空格分隔:$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: RequestAuthentication metadata: name: "jwt-example" namespace: foo spec: selector: matchLabels: app: httpbin jwtRules: - issuer: "testing@secure.istio.io" jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.32/security/tools/jwt/samples/jwks.json" spaceDelimitedClaims: ["roles"] EOF以下命令更新
require-jwt授权策略,要求roles声明包含值admin:$ kubectl apply -f - <<EOF apiVersion: security.istio.io/v1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: foo spec: selector: matchLabels: app: httpbin action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"] when: - key: request.auth.claims[roles] values: ["admin"] EOF获取一个 JWT,其
roles声明设置为空格分隔的字符串"admin editor":$ TOKEN_ROLES=$(python3 ./gen-jwt.py ./key.pem --claims "roles:admin editor")验证是否允许使用该 JWT 的请求,因为空格分隔的
roles声明中存在admin:$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN_ROLES" -w "%{http_code}\n" 200获取一个
roles声明不包含admin的 JWT:$ TOKEN_NO_ADMIN=$(python3 ./gen-jwt.py ./key.pem --claims "roles:editor")验证请求是否被拒绝:
$ kubectl exec "$(kubectl get pod -l app=curl -n foo -o jsonpath={.items..metadata.name})" -c curl -n foo -- curl "http://httpbin.foo:8000/headers" -sS -o /dev/null -H "Authorization: Bearer $TOKEN_NO_ADMIN" -w "%{http_code}\n" 403
清理
删除 foo 命名空间:
$ kubectl delete namespace foo