JWT 令牌
此任务向您展示如何设置 Istio 授权策略以根据 JSON Web 令牌 (JWT) 强制执行访问。 Istio 授权策略支持字符串类型和字符串列表类型的 JWT 声明。
开始之前
在开始此任务之前,请执行以下操作
完成 Istio 最终用户身份验证任务。
阅读 Istio 授权概念。
使用 Istio 安装指南 安装 Istio。
部署两个工作负载:
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
工作负载接受由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.24/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
工作负载的请求都具有有效的 JWT,并且requestPrincipal
设置为testing@secure.istio.io/testing@secure.istio.io
。 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
获取一个JWT,其中
iss
和sub
键的值相同,为testing@secure.istio.io
。这将导致Istio生成属性requestPrincipal
,其值为testing@secure.istio.io/testing@secure.istio.io
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.24/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
获取一个JWT,它将
groups
声明设置为一个字符串列表:group1
和group2
$ TOKEN_GROUP=$(curl https://raw.githubusercontent.com/istio/istio/release-1.24/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"}
验证一个包含
groups
声明中包含group1
的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_GROUP" -w "%{http_code}\n" 200
验证一个没有
groups
声明的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" 403
清理
删除命名空间foo
$ kubectl delete namespace foo