第 10 章 安全性
10.1. 为 Knative 服务配置 JSON Web 令牌身份验证 复制链接链接已复制到粘贴板!
OpenShift Serverless 当前没有用户定义的授权功能。要为部署添加用户定义的授权,您必须将 OpenShift Serverless 与 Red Hat OpenShift Service Mesh 集成,然后为 Knative 服务配置 JSON Web Token(JWT)身份验证和 sidecar 注入。
您可以使用 Service Mesh 2.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token(JWT)身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象成员的应用程序命名空间中创建身份验证请求和策略。您还必须为该服务启用 sidecar 注入。
在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-serving 和 knative-serving-ingress。
如果需要对这些命名空间中的 pod 进行 sidecar 注入,请参阅 OpenShift Serverless 文档中的原生将 Service Mesh 与 OpenShift Serverless 集成。
先决条件
- 您已在集群中安装了 OpenShift Serverless Operator、Knative Serving 和 Red Hat OpenShift Service Mesh。
-
安装 OpenShift CLI(
oc)。 - 您已创建了一个项目,或者具有适当的角色和权限访问项目,以便在 OpenShift Container Platform 中创建应用程序和其他工作负载。
流程
在您的服务中添加
sidecar.istio.io/inject="true"注解:服务示例
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: <service_name> spec: template: metadata: annotations: sidecar.istio.io/inject: "true"1 sidecar.istio.io/rewriteAppHTTPProbers: "true"2 ...应用
Service资源:$ oc apply -f <filename>在
ServiceMeshMemberRoll对象的每个无服务器应用程序命名空间中创建一个RequestAuthentication资源:apiVersion: security.istio.io/v1beta1 kind: RequestAuthentication metadata: name: jwt-example namespace: <namespace> spec: jwtRules: - issuer: testing@secure.istio.io jwksUri: https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/jwks.json应用
RequestAuthentication资源:$ oc apply -f <filename>通过创建以下
AuthorizationPolicy资源,允许从ServiceMeshMemberRoll对象中的每个无服务器应用程序命名空间的系统 pod 访问RequestAuthenticaton资源:apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: allowlist-by-paths namespace: <namespace> spec: action: ALLOW rules: - to: - operation: paths: - /metrics1 - /healthz2 应用
AuthorizationPolicy资源:$ oc apply -f <filename>对于作为
ServiceMeshMemberRoll对象中成员的每个无服务器应用程序命名空间,请创建以下AuthorizationPolicy资源:apiVersion: security.istio.io/v1beta1 kind: AuthorizationPolicy metadata: name: require-jwt namespace: <namespace> spec: action: ALLOW rules: - from: - source: requestPrincipals: ["testing@secure.istio.io/testing@secure.istio.io"]应用
AuthorizationPolicy资源:$ oc apply -f <filename>
验证
如果您尝试使用
curl请求来获取 Knative 服务 URL,则会被拒绝:示例命令
$ curl http://hello-example-1-default.apps.mycluster.example.com/输出示例
RBAC: access denied使用有效 JWT 验证请求。
获取有效的 JWT 令牌:
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.8/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -使用
curl请求标头中的有效令牌访问该服务:$ curl -H "Authorization: Bearer $TOKEN" http://hello-example-1-default.apps.example.com现在允许请求:
输出示例
Hello OpenShift!
您可以使用 Service Mesh 1.x 和 OpenShift Serverless 在 Knative 服务中使用 JSON Web Token(JWT)身份验证。要做到这一点,您必须在作为 ServiceMeshMemberRoll 对象的成员的应用程序命名空间中创建策略。您还必须为该服务启用 sidecar 注入。
在启用了 Kourier 时,不支持在系统命名空间中向 pod 添加 sidecar 注入,如 knative-serving 和 knative-serving-ingress。
如果需要对这些命名空间中的 pod 进行 sidecar 注入,请参阅 OpenShift Serverless 文档中的原生将 Service Mesh 与 OpenShift Serverless 集成。
先决条件
- 您已在集群中安装了 OpenShift Serverless Operator、Knative Serving 和 Red Hat OpenShift Service Mesh。
-
安装 OpenShift CLI(
oc)。 - 您已创建了一个项目,或者具有适当的角色和权限访问项目,以便在 OpenShift Container Platform 中创建应用程序和其他工作负载。
流程
在您的服务中添加
sidecar.istio.io/inject="true"注解:服务示例
apiVersion: serving.knative.dev/v1 kind: Service metadata: name: <service_name> spec: template: metadata: annotations: sidecar.istio.io/inject: "true"1 sidecar.istio.io/rewriteAppHTTPProbers: "true"2 ...应用
Service资源:$ oc apply -f <filename>在作为
ServiceMeshMemberRoll对象的成员的无服务器应用程序命名空间中创建策略,该策略只允许具有有效 JSON Web Tokens(JWT)的请求:重要路径
/metrics和/healthz必须包含在excludePaths中,因为它们是从knative-serving命名空间中的系统 pod 访问的。apiVersion: authentication.istio.io/v1alpha1 kind: Policy metadata: name: default namespace: <namespace> spec: origins: - jwt: issuer: testing@secure.istio.io jwksUri: "https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/jwks.json" triggerRules: - excludedPaths: - prefix: /metrics1 - prefix: /healthz2 principalBinding: USE_ORIGIN应用
Policy资源:$ oc apply -f <filename>
验证
如果您尝试使用
curl请求来获取 Knative 服务 URL,则会被拒绝:$ curl http://hello-example-default.apps.mycluster.example.com/输出示例
Origin authentication failed.使用有效 JWT 验证请求。
获取有效的 JWT 令牌:
$ TOKEN=$(curl https://raw.githubusercontent.com/istio/istio/release-1.6/security/tools/jwt/samples/demo.jwt -s) && echo "$TOKEN" | cut -d '.' -f2 - | base64 --decode -使用
curl请求标头中的有效令牌访问该服务:$ curl http://hello-example-default.apps.mycluster.example.com/ -H "Authorization: Bearer $TOKEN"现在允许请求:
输出示例
Hello OpenShift!