【问题标题】:kubernetes helm chartKubernetes 掌舵图
【发布时间】:2019-03-15 08:20:46
【问题描述】:

我正在尝试使用 helm chart 阻止用户代理名称 wordpress 用于 nginx 入口。为此,我在 values.yml 文件中给出了键值,并在 configmap.yml 中调用了该键,如下所示。

 - values.yml file
   configmap:
     block_user_agents: "^.*wordpress.*$"

 - configmap.yml file
   data:
     block-user-agents: "{{ .Values.configmap.block_user_agents }}"

 - command to check
   curl -A "wordpress-blah" http://my_minikube_ip:32144(serviceport)

 - output

 <html>
 <head><title>404 Not Found</title></head>
 <body>
 <center><h1>404 Not Found</h1></center>
 <hr><center>nginx/1.15.5</center>
 </body>
 </html>

使用 helm install 成功部署 helm 图表后,我尝试使用 curl 命令对其进行测试,以检查其是否阻止用户代理。我找不到 404,因为我需要 403。任何人都可以在这里帮助我,正则表达式可以在这里工作吗?我错过了什么吗?

【问题讨论】:

    标签: kubernetes kubernetes-helm


    【解决方案1】:

    这是examplenginx.conf中如何屏蔽用户代理:

    ### make sure your 'if' statement is in the server block.
    ### case sensitive http user agent blocking  ###
    if ($http_user_agent ~ (Catall Spider|AcoiRobot) ) {
        return 403;
    }
    ### case insensitive http user agent blocking  ###
    if ($http_user_agent ~* (foo|bar) ) {
        return 403;
    }
    

    您可以使用以下example 在入口控制器的nginx.conf 中添加一些配置部分。它向 Nginx 配置添加了一个自定义标头,该标头仅适用于该特定 Ingress:

    ---
    apiVersion: extensions/v1beta1
    kind: Ingress
    metadata:
      name: nginx-configuration-snippet
      annotations:
        nginx.ingress.kubernetes.io/configuration-snippet: |
          more_set_headers "Request-Id: $req_id";
    spec:
      rules:
      - host: custom.configuration.com
        http:
          paths:
          - backend:
              serviceName: http-svc
              servicePort: 80
            path: /
    

    如果您需要向入口控制器添加一些全局设置,请参阅以下examples

    ---
    apiVersion: v1
    data:
      proxy-set-headers: "ingress-nginx/custom-headers"
    kind: ConfigMap
    metadata:
      name: nginx-configuration
      namespace: ingress-nginx
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    
    ---
    apiVersion: v1
    data:
      X-Different-Name: "true"
      X-Request-Start: t=${msec}
      X-Using-Nginx-Controller: "true"
    kind: ConfigMap
    metadata:
      name: custom-headers
      namespace: ingress-nginx
    
    ---
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: nginx-configuration
      namespace: ingress-nginx
      labels:
        app.kubernetes.io/name: ingress-nginx
        app.kubernetes.io/part-of: ingress-nginx
    data:
      proxy-connect-timeout: "10"
      proxy-read-timeout: "120"
      proxy-send-timeout: "120"
    

    使用前面的示例创建一个 YAML 文件并将其应用到您的集群:

    kubectl apply -f nginx-ingress-config.yaml
    

    您可以使用以下命令检查设置是否应用于入口控制器中的 nginx.conf:

    # Replace name of the ingress controller with the real name of the pod in the right namespace
    $ kubectl exec nginx-ingress-controller-6bdddddb-6dmnw -n kube-system cat /etc/nginx/nginx.conf
    
    # You can find the real name of your ingress controller and namespace using the next command
    $ kubectl get pods --all-namespaces | grep nginx-ingress-controller
    

    如果没有关于图表的信息,很难猜测应该设置哪个参数。

    如果您使用 helm 存储库中的图表,您可以通过运行来获取它的内容

    $ helm fetch <chart/name>
    

    之后,您将在当前目录中获得一个压缩的图表文件。 您可能需要通过读取图表模板目录中的模板文件来为您的代码 sn-p 找到正确的值。

    如果您使用的是您编写的图表,您可以使用stable/nginx-ingress 图表作为参考。它有很多配置选项。

    更新:

    从 0.20.0 版开始,新功能是introduced

    #2997 提供阻止 IP、用户代理和引用者的可能性 全球

    参数的用法在Map section of the manual中解释。

    如果您需要使用示例,可​​以在test-case 中找到。

    It("should block User-Agents defined in the ConfigMap", func() {
        err := f.UpdateNginxConfigMapData("block-user-agents", "~*chrome\\/68\\.0\\.3440\\.106\\ safari\\/537\\.36,AlphaBot")
        Expect(err).NotTo(HaveOccurred())
    

    ...

        // Should be blocked
        resp, _, errs := gorequest.New().
            Get(f.IngressController.HTTPURL).
            Set("Host", host).
            Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36").
            End()
        Expect(errs).To(BeNil())
        Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
    
        resp, _, errs = gorequest.New().
            Get(f.IngressController.HTTPURL).
            Set("Host", host).
            Set("User-Agent", "AlphaBot").
            End()
        Expect(errs).To(BeNil())
        Expect(resp.StatusCode).Should(Equal(http.StatusForbidden))
    
        // Shouldn't be blocked
        resp, _, errs = gorequest.New().
            Get(f.IngressController.HTTPURL).
            Set("Host", host).
            Set("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 11_4_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/11.0 Mobile/15E148 Safari/604.1").
            End()
        Expect(errs).To(BeNil())
        Expect(resp.StatusCode).Should(Equal(http.StatusOK))
    })
    

    【讨论】:

    • 你好 VAS。谢谢你的评论。以前,我使用 if 条件在 helm 图表中的 configmap.yaml 中阻止用户代理,如下所示。 if ($http_user_agent ~* "^.*wordpress.*$") { 返回 403;当我确实 curl 返回输出为 403 但在最近的 0.20.0 nginx ingresss 版本中发布了新注释时,它起作用了,因此我们可以删除该 if 条件并简单地添加,如下所示。 data: block-user-agents: "^.*wordpress.*$" 但是在部署 helm chart 并且如果做 curl 我需要得到 403 但得到 404 ......如何返回 403。
    猜你喜欢
    • 2017-04-08
    • 2020-05-19
    • 2021-06-13
    • 2019-03-05
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    相关资源
    最近更新 更多