简体   繁体   English

如何将标头转发到 Kubernetes (Istio) 中的不同服务

[英]How do I forward headers to different services in Kubernetes (Istio)

I have a sample application (web-app, backend-1, backend-2) deployed on minikube all under a JWT policy, and they all have proper destination rules, Istio sidecar and MTLS enabled in order to secure the east-west traffic.我在 minikube 上部署了一个示例应用程序(web-app、backend-1、backend-2),它们都在 JWT 策略下,它们都有适当的目的地规则,启用了 Istio sidecar 和 MTLS 以保护东西向流量。

apiVersion: authentication.istio.io/v1alpha1
kind: Policy
metadata:
  name: oidc
spec:
  targets:
  - name: web-app
  - name: backend-1
  - name: backend-2
  peers:
  - mtls: {}
  origins:
  - jwt:
      issuer: "http://myurl/auth/realms/test"
      jwksUri: "http://myurl/auth/realms/test/protocol/openid-connect/certs"
  principalBinding: USE_ORIGIN

When I run the following command I receive a 401 unauthorized response when requesting the data from the backend, which is due to $TOKEN not being forwarded to backend-1 and backend-2 headers during the http request.当我运行以下命令时,我在从后端请求数据时收到 401 未经授权的响应,这是由于 $TOKEN 在 http 请求期间未转发到 backend-1 和 backend-2 标头。

$> curl http://minikubeip/api "Authorization: Bearer $TOKEN"

Is there a way to forward http headers to backend-1 and backend-2 using native kubernetes/istio?有没有办法使用原生 kubernetes/istio 将 http 标头转发到 backend-1 和 backend-2? Am I forced to make application code changes to accomplish this?我是否必须更改应用程序代码才能完成此操作?

Edit: This is the error I get after applying my oidc policy.编辑:这是应用我的 oidc 策略后得到的错误。 When I curl web-app with the auth token I get当我使用身份验证令牌卷曲网络应用程序时,我得到

{"errors":[{"code":"APP_ERROR_CODE","message":"401 Unauthorized"} {"errors":[{"code":"APP_ERROR_CODE","message":"401 Unauthorized"}

Note that when I curl backend-1 or backend-2 with the same auth-token I get the appropriate data.请注意,当我使用相同的身份验证令牌 curl backend-1 或 backend-2 时,我会获得适当的数据。 Also, there is no other destination rule/policy applied to these services currently, policy enforcement is on, and my istio version is 1.1.15.此外,目前没有其他目标规则/策略应用于这些服务,策略实施已开启,我的 istio 版本是 1.1.15。 This is the policy I am applying:这是我正在申请的政策:

apiVersion: authentication.istio.io/v1alpha1
kind: Policy
metadata:
  name: default
  namespace: default
spec:
  # peers:
  # - mtls: {}
  origins:
  - jwt:
      issuer: "http://10.148.199.140:8080/auth/realms/test"
      jwksUri: "http://10.148.199.140:8080/auth/realms/test/protocol/openid-connect/certs"
  principalBinding: USE_ORIGIN

should the token be propagated to backend-1 and backend-2 without any other changes?令牌是否应该传播到 backend-1 和 backend-2 而没有任何其他更改?

Yes, policy should transfer token to both backend-1 and backend-2是的,策略应该将令牌转移到 backend-1 和 backend-2

There is a github issue , where users had same issue like You有一个github 问题,用户和你有同样的问题

A few informations from there:来自那里的一些信息:

The JWT is verified by an Envoy filter, so you'll have to check the Envoy logs. JWT 由 Envoy 过滤器验证,因此您必须检查 Envoy 日志。 For the code, see https://github.com/istio/proxy/tree/master/src/envoy/http/jwt_auth代码见https://github.com/istio/proxy/tree/master/src/envoy/http/jwt_auth

Pilot retrieves the JWKS to be used by the filter (it is inlined into the Envoy config), you can find the code for that in pilot/pkg/security Pilot 检索过滤器要使用的 JWKS(它被内联到 Envoy 配置中),您可以在 Pilot/pkg/security 中找到该代码

And another problem with that in stackoverflow stackoverflow 中的另一个问题

where accepted answer is:接受的答案是:

The problem was resolved with two options: 1. Replace Service Name and port by external server ip and external port (for issuer and jwksUri) 2. Disable the usage of mTLS and its policy (Known issue: https://github.com/istio/istio/issues/10062 ).该问题通过两个选项解决:1. 将服务名称和端口替换为外部服务器 ip 和外部端口(对于 issuer 和 jwksUri) 2. 禁用 mTLS 及其策略的使用(已知问题: https : //github.com/ istio/istio/issues/10062 )。

From istio documentation来自 istio 文档

For each service, Istio applies the narrowest matching policy.对于每个服务,Istio 应用最窄匹配策略。 The order is: service-specific > namespace-wide > mesh-wide.顺序是:服务特定 > 命名空间范围 > 网格范围。 If more than one service-specific policy matches a service, Istio selects one of them at random.如果有多个特定于服务的策略与一个服务匹配,Istio 会随机选择其中一个。 Operators must avoid such conflicts when configuring their policies.运营商在配置策略时必须避免此类冲突。

To enforce uniqueness for mesh-wide and namespace-wide policies, Istio accepts only one authentication policy per mesh and one authentication policy per namespace.为了强制网格范围和命名空间范围策略的唯一性,Istio 只接受每个网格一个身份验证策略和每个命名空间一个身份验证策略。 Istio also requires mesh-wide and namespace-wide policies to have the specific name default. Istio 还要求网格范围和命名空间范围的策略具有特定的名称默认值。

If a service has no matching policies, both transport authentication and origin authentication are disabled.如果服务没有匹配的策略,则传输身份验证和源身份验证都将被禁用。

Istio supports header propagation. Istio 支持标头传播。 Probably didn't support when this thread was created.创建此线程时可能不支持。

You can allow the original header to be forwarded by using forwardOriginalToken : true in JWTRules or forward a valid JWT payload using outputPayloadToHeader in JWTRules .您可以通过在JWTRules使用forwardOriginalToken : true 来允许转发原始标头,或者在JWTRules使用outputPayloadToHeader转发有效的 JWT 负载。

Reference: ISTIO JWTRule documentation参考: ISTIO JWTRule 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM