Istio 请求身份验证 - 使用结果 404

问题描述

这是我的请求身份验证,

apiVersion: security.istio.io/v1beta1
kind: RequestAuthentication
Metadata:
  name:prod-authenticator
  namespace: prod
spec:
  selector:
    matchLabels:
      istio: ingressgateway
  jwtRules:
  - issuer: https://securetoken.google.com/<project-id>
    jwksUri: https://www.googleapis.com/service_accounts/v1/jwk/securetoken@system.gserviceaccount.com

我的授权政策是这样的,

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
Metadata:
 name:prod-authorized-api
 namespace: prod
spec:
 action: ALLOW
 rules:
 - from:
   - source:
        requestPrincipals: ["*"]

 - to:
    - operation:
        paths: ["/user/ping"]

以下帮助我排除了没有有效令牌的健康路径 (/user/ping)。

我的虚拟服务是

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
Metadata:
  name: user-svc-vs
  namespace: prod
spec:
  hosts:
  - gateway.xxxx.com
  gateways:
  - istio-system/prod-gateway
  http:
  - match:
    - uri:
        prefix: /pop
    route:
    - destination:
        host:user-svc.prod.svc.cluster.local 

但是当检查时我可以看到只有健康 api 得到 200 休息所有都得到 404,当检查浏览器时我看到了

从源“https://”访问“https://”的 XMLHttpRequest 已被 CORS 策略阻止:对预检请求的响应未通过访问控制检查:无“访问控制” -Allow-Origin' 标头存在于请求的资源上。

在虚拟服务中,我尝试添加“corsPolicy”但没有任何效果

示例虚拟服务尝试的示例是

apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
Metadata:
  name: ratings-route
spec:
  hosts:
  - ratings.prod.svc.cluster.local
  http:
  - route:
    - destination:
        host: ratings.prod.svc.cluster.local
        subset: v1
    corsPolicy:
      allowOrigins:
      - exact: https://example.com
      allowMethods:
      - POST
      - GET
      allowCredentials: false
      allowHeaders:
      - X-Foo-Bar
      maxAge: "24h"

解决方法

CORS 预检请求是浏览器自动发出的 HTTP OPTIONS 请求,请参阅 this site for details

因此,在您的 OPTIONS 中允许 AuthorizationPolicy 调用以允许飞行前请求。

- to:
    - operation:
        methods: ["OPTIONS"]