我要实现的目标:阻止所有到服务的流量,其中包含用于在与服务相同的 namespace 中处理该流量的代码。
原因:,这是将特定服务“锁定”到特定IP/CIDR的第一步
我有一个名为istio-ingressgateway的主要入口GW,可用于服务。

$ kubectl describe gw istio-ingressgateway -n istio-system
Name:         istio-ingressgateway
Namespace:    istio-system
Labels:       operator.istio.io/component=IngressGateways
              operator.istio.io/managed=Reconcile
              operator.istio.io/version=1.5.5
              release=istio
Annotations:  API Version:  networking.istio.io/v1beta1
Kind:         Gateway
Metadata:
  Creation Timestamp:  2020-08-28T15:45:10Z
  Generation:          1
  Resource Version:    95438963
  Self Link:           /apis/networking.istio.io/v1beta1/namespaces/istio-system/gateways/istio-ingressgateway
  UID:                 ae5dd2d0-44a3-4c2b-a7ba-4b29c26fa0b9
Spec:
  Selector:
    App:    istio-ingressgateway
    Istio:  ingressgateway
  Servers:
    Hosts:
      *
    Port:
      Name:      http
      Number:    80
      Protocol:  HTTP
Events:          <none>
我还拥有另一个“主要” GW,即支持TLS的K8s入口GW(我想将其包括在内,以便尽可能地明确)
k describe gw istio-autogenerated-k8s-ingress -n istio-system
Name:         istio-autogenerated-k8s-ingress
Namespace:    istio-system
Labels:       app=istio-ingressgateway
              istio=ingressgateway
              operator.istio.io/component=IngressGateways
              operator.istio.io/managed=Reconcile
              operator.istio.io/version=1.5.5
              release=istio
Annotations:  API Version:  networking.istio.io/v1beta1
Kind:         Gateway
Metadata:
  Creation Timestamp:  2020-08-28T15:45:56Z
  Generation:          2
  Resource Version:    95439499
  Self Link:           /apis/networking.istio.io/v1beta1/namespaces/istio-system/gateways/istio-autogenerated-k8s-ingress
  UID:                 edd46c17-9975-4089-95ff-a2414d40954a
Spec:
  Selector:
    Istio:  ingressgateway
  Servers:
    Hosts:
      *
    Port:
      Name:      http
      Number:    80
      Protocol:  HTTP
    Hosts:
      *
    Port:
      Name:      https-default
      Number:    443
      Protocol:  HTTPS
    Tls:
      Credential Name:     ingress-cert
      Mode:                SIMPLE
      Private Key:         sds
      Server Certificate:  sds
Events:                    <none>
我希望能够在 namespace x中创建另一个GW,并将授权策略附加到该GW。
如果我在istio-system命名空间中创建授权策略,那么它会随带RBAC: access denied一起返回,这很不错-但这适用于使用主GW的所有服务。
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: block-all
  namespace: istio-system
spec:
  selector:
    matchLabels:
      app: istio-ingressgateway
  action: DENY
  rules:
    - from:
        - source:
            ipBlocks: ["0.0.0.0/0"]
我目前所拥有的不起作用。任何指针将不胜感激。应用x时,以下所有内容均在kubectl apply -f files.yaml -n x命名空间下创建
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  annotations:
      app: x-ingress
  name: x-gw
  labels:
    app: x-ingress
spec:
  selector:
    istio: ingressgateway
  servers:
    - hosts:
        - x.y.com
      port:
        name: http
        number: 80
        protocol: HTTP
      tls:
        httpsRedirect: true
    - hosts:
        - x.y.com
      port:
        name: https
        number: 443
        protocol: HTTPS
      tls:
        mode: SIMPLE
        privateKey: sds
        serverCertificate: sds
        credentialName: ingress-cert
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: x
  labels:
    app: x
spec:
  hosts:
    - x.y.com
  gateways:
    - x-gw
  http:
    - route:
        - destination:
            host: x
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: x-ingress-policy
spec:
  selector:
    matchLabels:
      app: x-ingress
  action: DENY
  rules:
    - from:
        - source:
            ipBlocks: ["0.0.0.0/0"]
---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: x
  labels:
    app: x
spec:
  hosts:
    - x.y.com
  gateways:
    - x-gw
  http:
    - route:
        - destination:
            host: x

上面应该阻止所有到GW的流量,因为它在0.0.0.0/0的CIDR范围内匹配
我完全误解了GW/AuthorizationPolicies的概念,还是错过了什么?
编辑
我最终创建了另一个具有IP限制块的GW,因为AWS上的传统负载均衡器不支持IP转发。
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
metadata:
  namespace: istio-system
  name: istiocontrolplane
spec:
  profile: demo
  components:
    ingressGateways:
      - name: istio-ingressgateway
        enabled: true
      - name: admin-ingressgateway
        enabled: true
        label:
          istio: admin-ingressgateway
        k8s:
          serviceAnnotations:
            service.beta.kubernetes.io/aws-load-balancer-type: "nlb"
---
apiVersion: security.istio.io/v1beta1
kind: AuthorizationPolicy
metadata:
  name: deny-all-admin
  namespace: istio-system
spec:
  selector:
    matchLabels:
      istio: admin-ingressgateway
  action: ALLOW
  rules:
    - from:
        - source:
            ipBlocks: ["176.252.114.59/32"]
kubectl patch svc istio-ingressgateway -n istio-system -p '{"spec":{"externalTrafficPolicy":"Local"}}'
然后,我在要锁定的工作负载中使用了该网关。

最佳答案

据我所知,您应该以3种方式使用AuthorizationPolicy

入口网关上的


  • 命名空间
  • 上的
  • 关于特定服务

  • 我已经尝试过像注释一样使它在带有注释的特定网关上工作,但是我无法使其工作。
    例如
    以下授权策略拒绝对 namespace x中的工作负载的所有请求。
    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
     name: deny-all
     namespace: x
    spec:
      {}
    
    以下授权策略拒绝入口网关上的所有请求。
    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: deny-all
      namespace: istio-system
    spec:
      selector:
        matchLabels:
          app: istio-ingressgateway
    
    以下授权策略拒绝x namespace 中对httpbin的所有请求。
    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: deny-service-x
      namespace: x
    spec:
      selector:
        matchLabels:
          app: httpbin
    

    假设您拒绝x namespace 上的所有请求,而只允许获取httpbin服务的请求。
    然后,您将使用此AuthorizationPolicy拒绝所有请求
    apiVersion: security.istio.io/v1beta1
    kind: AuthorizationPolicy
    metadata:
      name: deny-all
      namespace: x
    spec:
      {}
    
    并且此AuthorizationPolicy仅允许获取请求。
    apiVersion: "security.istio.io/v1beta1"
    kind: "AuthorizationPolicy"
    metadata:
      name: "x-viewer"
      namespace: x
    spec:
      selector:
        matchLabels:
          app: httpbin
      rules:
      - to:
        - operation:
            methods: ["GET"]
    

    还有一个主要问题,就是ipBlocks。有与此相关的github issue
    正如@incfly在这里提到的


    我已经尝试从istio documentation尝试运行此示例,但是即使我更改了externalTrafficPolicy,它也对我不起作用。然后,在istio上方讨论envoyfilter的变通方法。
    答案由@ hleal18 here提供。
    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: httpbin
      namespace: foo
    spec:
      workloadSelector:
        labels:
          app: httpbin
      configPatches:
        - applyTo: HTTP_FILTER
          match:
            context: SIDECAR_INBOUND
            listener:
              filterChain:
                filter:
                  name: "envoy.http_connection_manager"
                  subFilter:
                    name: "envoy.router"
          patch:
            operation: INSERT_BEFORE
            value:
              name: envoy.filters.http.rbac
              config:
                rules:
                  action: ALLOW
                  policies:
                    "ip-premissions":
                      permissions:
                        - any: true
                      principals:
                        - remote_ip:
                            address_prefix: xxx.xxx.xx.xx
                            prefix_len: 32
    

    我已经在我的测试群集上尝试了以上envoy筛选器,据我所知它正在工作。
    看看我执行的以下步骤。
    1.我已经更改了externalTrafficPolicy
    kubectl patch svc istio-ingressgateway -n istio-system -p '{"spec":{"externalTrafficPolicy":"Local"}}'
    
    2.我创建了启用istio-injection的命名空间x并在此处部署了httpbin。
    kubectl create namespace x
    kubectl label namespace x istio-injection=enabled
    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.7/samples/httpbin/httpbin.yaml -n x
    kubectl apply -f https://github.com/istio/istio/blob/master/samples/httpbin/httpbin-gateway.yaml -n x
    
    3.我已经创建了envoyfilter
    apiVersion: networking.istio.io/v1alpha3
    kind: EnvoyFilter
    metadata:
      name: httpbin
      namespace: x
    spec:
      workloadSelector:
        labels:
          app: httpbin
      configPatches:
        - applyTo: HTTP_FILTER
          match:
            context: SIDECAR_INBOUND
            listener:
              filterChain:
                filter:
                  name: "envoy.http_connection_manager"
                  subFilter:
                    name: "envoy.router"
          patch:
            operation: INSERT_BEFORE
            value:
              name: envoy.filters.http.rbac
              config:
                rules:
                  action: ALLOW
                  policies:
                    "ip-premissions":
                      permissions:
                        - any: true
                      principals:
                        - remote_ip:
                            address_prefix: xx.xx.xx.xx
                            prefix_len: 32
    
    address_prefix CLIENT_IP ,我已经使用了一些命令来获取它。
    export INGRESS_HOST=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.status.loadBalancer.ingress[0].ip}')
    export INGRESS_PORT=$(kubectl -n istio-system get service istio-ingressgateway -o jsonpath='{.spec.ports[?(@.name=="http2")].port}')
    curl "$INGRESS_HOST":"$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"
    CLIENT_IP=$(curl "$INGRESS_HOST":"$INGRESS_PORT"/ip -s | grep "origin" | cut -d'"' -f 4) && echo "$CLIENT_IP"
    
    4.我已经用curl和我的浏览器对其进行了测试。
    curl "$INGRESS_HOST":"$INGRESS_PORT"/headers -s -o /dev/null -w "%{http_code}\n"
    200
    
    kubernetes - Istio授权策略不适用于子网关-LMLPHP

    如果您还有其他问题,请告诉我,我可能会为您提供帮助。

    关于kubernetes - Istio授权策略不适用于子网关,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63647973/

    10-10 04:30