【问题标题】:istio JWT authentication for single service behind ingress gatewayIstio JWT 身份验证,用于入口网关后面的单个服务
【发布时间】:2023-03-22 23:05:02
【问题描述】:

我在 AKS (v1.16.13) 上运行了 2 个服务,并部署了以下 istio (v1.7.3) 配置。第一个是我调用 OIDC 流程并获取 JWT 令牌的 UI,第二个是需要有效 JWT 令牌的后端服务。

apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
  name: myapp-gateway
  namespace: "istio-system"
spec:
  selector:
    istio: ingressgateway
  servers:
  - hosts:
    - myapp.com
    port:
      name: http-myapp
      number: 80
      protocol: HTTP
    tls:
      httpsRedirect: true
  - hosts:
    - myapp.com
    port:
      name: https-myapp
      number: 443
      protocol: HTTPS
    tls:
      credentialName: myapp-credential
      mode: SIMPLE
---
apiVersion: networking.istio.io/v1beta1
kind: VirtualService
metadata:
  name: myapp
  namespace: myapp
spec:
  gateways:
  - istio-system/myapp-gateway
  hosts:
  - myapp.com
  http:
  - match:
    - uri:
        prefix: /ui
    route:
    - destination:
        host: myapp-ui.myapp.svc.cluster.local
        port:
          number: 4200
  - match:
    - uri:
        prefix: /backend/
    rewrite:
      uri: /
    route:
    - destination:
        host: myapp-service-backend.myapp.svc.cluster.local
        port:
          number: 8080
---
apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: myapp-jwt-backend
  namespace: myapp
spec:
  jwtRules:
  - issuer: https://oktapreview.com
  selector:
    matchLabels:
      app: myapp-service-backend

使用该配置,如果我调用 myapp.com/backend,我希望得到 401,但事实并非如此。请求身份验证不会启动。

经过进一步研究 (https://discuss.istio.io/t/cannot-use-jwt-policy-with-an-externalname-virtualservice-target/2794/3),我发现我不能在 VirtualService 上应用 RequestAuthentication,而只能在网关上应用,这对我来说很奇怪但没关系。我已将 RequestAuthentication 更改为以下内容,但在调用后端后仍然没有任何变化:

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
metadata:
  name: myapp-jwt-backend
  namespace: istio-system
spec:
  jwtRules:
  - issuer: https://oktapreview.com
  selector:
    matchLabels:
      istio: myapp-gateway

您知道如何为我的用例设置 istio 吗?假设 RequestAuthentication 可以在网关上工作,我需要 2 个网关吗? 1个用于UI,第二个用于后端?看起来有点过头了。

【问题讨论】:

  • 您可以将 requestauth 应用于 istio 服务网格中的服务(网关身份验证不是必需的)。如果传入请求包含无效令牌,则会发生错误。如果不存在身份验证令牌,默认情况下会将请求传递给后端。要更改该行为,请同时应用 AuthorizationPolicy。
  • 非常感谢。我的错,我应该更详细地阅读文档:istio.io/latest/docs/tasks/security/authorization/authz-jwt。添加 AuthorizationPolicy 后,它现在可以工作了。您要发布答案以获得奖励吗?
  • 我很高兴它有帮助。

标签: kubernetes jwt istio


【解决方案1】:

感谢 sachin 的评论并再次阅读文档让我意识到我需要在 RequestAuthentication 之上的 AuthorizationPolicy:

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:    
  name: myapp-require-jwt-backend
spec:
  action: ALLOW
  rules:
  - from:
    - source:
        requestPrincipals:
        - https://xxx/*
  selector:
    matchLabels:
      app: myapp-service-backend

请求身份验证只是确保在提供 JWT 令牌时,它必须是有效的。如果没有token,就直接通过请求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-01
    • 2021-08-13
    • 2020-02-05
    • 2019-06-06
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多