RequestAuthentication
RequestAuthentication
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。
- 根據 “sub” 聲明來路由請求的 VirtualService。
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 Token (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 Key Set (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
此訊息指定將聲明複製到標頭的詳細資訊。