请求认证
请求认证
RequestAuthentication 定义了工作负载支持哪些请求身份验证方法。如果请求包含无效的身份验证信息,它将根据配置的身份验证规则拒绝请求。不包含任何身份验证凭据的请求将被接受,但不会有任何经过身份验证的身份。要仅限于访问经过身份验证的请求,这应与授权规则一起使用。示例
- 要求对具有标签
app:httpbin
的所有工作负载的所有请求使用 JWT
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: httpbin
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
jwtRules:
- issuer: "issuer-foo"
jwksUri: https://example.com/.well-known/jwks.json
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: httpbin
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
rules:
- from:
- source:
requestPrincipals: ["*"]
- 根命名空间(默认情况下为“istio-system”)中的策略适用于网格中所有命名空间中的工作负载。以下策略使所有工作负载仅接受包含有效 JWT 令牌的请求。
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: req-authn-for-all
namespace: istio-system
spec:
jwtRules:
- issuer: "issuer-foo"
jwksUri: https://example.com/.well-known/jwks.json
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: require-jwt-for-all
namespace: istio-system
spec:
rules:
- from:
- source:
requestPrincipals: ["*"]
- 下一个示例显示如何为不同的
host
设置不同的 JWT 要求。RequestAuthentication
声明它可以接受由issuer-foo
或issuer-bar
发出的 JWT(公钥集根据 OpenID Connect 规范隐式设置)。
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: httpbin
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
jwtRules:
- issuer: "issuer-foo"
- issuer: "issuer-bar"
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: httpbin
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
rules:
- from:
- source:
requestPrincipals: ["issuer-foo/*"]
to:
- operation:
hosts: ["example.com"]
- from:
- source:
requestPrincipals: ["issuer-bar/*"]
to:
- operation:
hosts: ["another-host.com"]
- 您可以微调授权策略以针对每个路径设置不同的要求。例如,要对所有路径(除了 /healthz)要求 JWT,可以使用相同的
RequestAuthentication
,但授权策略可以是
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: httpbin
namespace: foo
spec:
selector:
matchLabels:
app: httpbin
rules:
- from:
- source:
requestPrincipals: ["*"]
- to:
- operation:
paths: ["/healthz"]
[实验性] 现在支持基于派生 元数据 的路由。前缀“@”用于表示与请求中的标头而不是内部元数据匹配。当前,此功能仅支持以下元数据
request.auth.claims.{claim-name}[.{nested-claim}]*
,这些是从已验证的 JWT 令牌中提取的。使用.
或[]
作为嵌套声明名称的分隔符。例如:request.auth.claims.sub
、request.auth.claims.name.givenName
和request.auth.claims[foo.com/name]
。有关更多信息,请参阅 基于 JWT 声明的路由。
仅在网关中支持使用与 JWT 声明元数据匹配的功能。以下示例显示了
- RequestAuthentication 用于解码和验证 JWT。这还使
@request.auth.claims
可用于 VirtualService 中。 - AuthorizationPolicy 用于检查请求中有效的主体。这使得 JWT 成为请求的必备条件。
- VirtualService 用于根据“sub”声明路由请求。
apiVersion: security.istio.io/v1
kind: RequestAuthentication
metadata:
name: jwt-on-ingress
namespace: istio-system
spec:
selector:
matchLabels:
app: istio-ingressgateway
jwtRules:
- issuer: "example.com"
jwksUri: https://example.com/.well-known/jwks.json
---
apiVersion: security.istio.io/v1
kind: AuthorizationPolicy
metadata:
name: require-jwt
namespace: istio-system
spec:
selector:
matchLabels:
app: istio-ingressgateway
rules:
- from:
- source:
requestPrincipals: ["*"]
---
apiVersion: networking.istio.io/v1
kind: VirtualService
metadata:
name: route-jwt
spec:
hosts:
- foo.prod.svc.cluster.local
gateways:
- istio-ingressgateway
http:
- name: "v2"
match:
- headers:
"@request.auth.claims.sub":
exact: "dev"
route:
- destination:
host: foo.prod.svc.cluster.local
subset: v2
- name: "default"
route:
- destination:
host: foo.prod.svc.cluster.local
subset: v1
JWTRule
用于身份验证的 JSON Web 令牌 (JWT) 令牌格式,如 RFC 7519 中所定义。有关如何在整个身份验证流程中使用此格式,请参阅 OAuth 2.0 和 OIDC 1.0。
示例
由 https://example.com
发行的 JWT 的规范,其中受众声明必须是 bookstore_android.apps.example.com
或 bookstore_web.apps.example.com
。令牌应显示在 Authorization
标头中(默认)。JSON Web 密钥集 (JWKS) 将按照 OpenID Connect 协议进行发现。
issuer: https://example.com
audiences:
- bookstore_android.apps.example.com
bookstore_web.apps.example.com
此示例指定了非默认位置(x-goog-iap-jwt-assertion
标头)中的令牌。它还定义了显式获取 JWKS 的 URI。
issuer: https://example.com
jwksUri: https://example.com/.secret/jwks.json
fromHeaders:
- "x-goog-iap-jwt-assertion"
JWTHeader
此消息指定提取 JWT 令牌的标头位置。
ClaimToHeader
此消息指定将声明复制到标头的详细信息。