使用授权策略配置Istio Ingress Gateway以要求标头令牌

时间:2020-02-06 13:32:11

标签: istio

我将Istio Ingress Gateway配置为接受我的URL(使用https),例如 microservices.myexample.com grafana.myexample.com 等。

一切正常,但所有网址都是公开的。

因此,我被要求配置入口网关以保护 microservices.myexample.com (Grafana有一个登录页面)中的URL。仅当请求在标头中包含令牌时,才允许访问。

但是当我应用此yml文件时,所有URL均被阻止,并且它们需要包含 grafana.myexample.com 的标头:

apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
 name: ingress
 namespace: istio-system
spec:
 selector:
   matchLabels:
     app: istio-ingressgateway
 rules:
 - from: []
   to:
    - operation:
        #paths: ["/customers*"] # I also tried with paths. Every microservice has a path after microservices.myexample.com 
        hosts: ["microservices.myexample.com"]
   when:
    - key: request.headers[token]
      values: ["test123"]

2 个答案:

答案 0 :(得分:1)

我们做到了。

以防万一有人陷入同一问题。以下代码将应用于mynamespace中的所有服务。除了以 / actuator / health

结尾的网址之外,所有网址都需要令牌。
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: token-authorization
  namespace: mynamespace
spec:
  rules:
  - to:
    - operation:
        paths: ["*/actuator/health"]
  - to:
    - operation:
        paths: ["/*"]
    when:
    - key: request.headers[token]
      values: ["test123"]

答案 1 :(得分:0)

这不起作用。

这是因为在您的AuthorizationPolicy中,hosts下的operation:不支持HTTPS协议。

根据Istio documentation

可选。主机列表,与“ request.host”属性匹配。

如果未设置,则允许任何主机。必须仅与HTTP一起使用。

这是因为HTTPS流量中的主机标头已加密。关于此的更多信息是here

请求标头令牌也是如此。