Nginx Ingress Controller安全加密

在Kubernetes集群中,Nginx Ingress Controller是用来处理外部请求并将其路由到集群内服务的重要组件。如果您对业务数据在传输过程中有更高的安全要求,推荐您对Nginx Ingress Controller进行安全加密配置。本文将介绍关于Nginx Ingress Controller安全配置与校验的方法。

安全加密配置

在Nginx Ingress Controller中,可以通过配置ConfigMap中ssl-protocolsssl-ciphers字段来增强安全性。

名称

描述

实现字段

自定义TLS协议版本

指定允许使用的SSL协议。默认使用TLSv1.2、TLSv1.3。

ssl-protocols: "TLSv1.2 TLSv1.3"详细信息请参见ssl-protocol

自定义密码套件

设置要启用的密码列表。密码应采用OpenSSL库能够识别的格式进行指定。Nginx Ingress Controller会有一个默认的密码套件配置,默认的密码套件支持多种TLS客户端。如果您期望指定更安全的密码套件,可以在ConfigMap中指定ssl-ciphers来覆盖默认值。关于如何查看当前的密码套件配置,请参见查看配置密码套件

ssl-ciphers: "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384" (默认密码列表)。详细信息请参见ssl-ciphers

说明

密码套件的排序比较重要,因为它决定优先选择哪些算法。如无特殊需求,建议保持默认配置的密码套件即可。

前提条件

操作步骤

您可以通过控制台和kubectl两种方式进行操作。

控制台操作

  1. 登录容器服务管理控制台,在左侧导航栏选择集群

  2. 集群列表页面,单击目标集群名称,然后在左侧导航栏,选择配置管理 > 配置项

  3. 在配置项页面上方设置命名空间为kube-system,然后在名称搜索框中搜索nginx-configuration,然后单击nginx-configuration操作列的编辑

  4. 编辑页面,单击+添加,根据需求填写配置项名称,然后单击确定

kubectl操作

使用以下命令更新ConfigMap。

kubectl edit cm -n kube-system nginx-configuration
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-configuration
  namespace: kube-system
data:
  ssl-protocols: "TLSv1.2 TLSv1.3"  # 仅允许TLSv1.2 和TLSv1.3。
  ssl-ciphers: "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384"  # 默认的密码套件列表。

验证TLS版本和密码套件的安全性

  • 方法一:从testssl.sh站点下载testssl.sh工具,然后执行以下命令。

    ./testssl.sh <您的站点域名>

    预期输出:

    当检查到非健壮的加密套件或者使用了不安全的(较低的)TLS协议版本,则会用红色高亮显示作为提示。

     Testing protocols via sockets except NPN+ALPN
     SSLv2      not offered (OK)
     SSLv3      not offered (OK)
     TLS 1      not offered
     TLS 1.1    not offered
     TLS 1.2    offered (OK)
     TLS 1.3    offered (OK): final
     NPN/SPDY   not offered
     ALPN/HTTP2 h2, http/1.1 (offered)
     Testing cipher categories
     NULL ciphers (no encryption)                  not offered (OK)
     Anonymous NULL Ciphers (no authentication)    not offered (OK)
     Export ciphers (w/o ADH+NULL)                 not offered (OK)
     LOW: 64 Bit + DES, RC[2,4] (w/o export)       not offered (OK)
     Triple DES Ciphers / IDEA                     not offered
     Obsolete CBC ciphers (AES, ARIA etc.)         not offered
     Strong encryption (AEAD ciphers)              offered (OK)
     Testing robust (perfect) forward secrecy, (P)FS -- omitting Null Authentication/Encryption, 3DES, RC4
     PFS is offered (OK)          TLS_AES_256_GCM_SHA384 TLS_CHACHA20_POLY1305_SHA256 ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-CHACHA20-POLY1305
                                  TLS_AES_128_GCM_SHA256 ECDHE-RSA-AES128-GCM-SHA256
     Elliptic curves offered:     prime256v1 secp384r1 secp521r1 X25519 X448
     Finite field group:          ffdhe2048 ffdhe3072 ffdhe4096 ffdhe6144 ffdhe8192
     Testing server preferences
     Has server cipher order?     yes (OK) -- TLS 1.3 and below
     Negotiated protocol          TLSv1.3
     Negotiated cipher            TLS_AES_256_GCM_SHA384, 253 bit ECDH (X25519)
     Cipher order
        TLSv1.2:   ECDHE-RSA-AES128-GCM-SHA256 ECDHE-RSA-AES256-GCM-SHA384 ECDHE-RSA-CHACHA20-POLY1305
        TLSv1.3:   TLS_AES_256_GCM_SHA384 TLS_CHACHA20_POLY1305_SHA256 TLS_AES_128_GCM_SHA256
    ...
  • 方法二:将您的站点域名输入到在线网站中进行验证。

查看配置密码套件

执行以下命令可以查看当前配置密码套件:

kubectl -n kube-system exec deploy/nginx-ingress-controller -- nginx -T  |grep ssl_ciphers

预期输出:

Defaulted container "nginx-ingress-controller" out of: nginx-ingress-controller, init-sysctl (init)
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
        ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
nginx: configuration file /etc/nginx/nginx.conf test is successful

FAQ

是否支持国密算法?

ACK Nginx Ingress Controller采用社区提供的Nginx Ingress Controller版本,当前不支持国密算法。

Nginx Ingress支持的加密套件算法有哪些?

Nginx采用OpenSSL来进行加解密。您可以通过nginx -V命令查看Nginx以及对应的OpenSSL版本,也可以通过openssl ciphers命令查看可支持的密码套件。执行kubectl exec -itn kube-system <nginx-ingress-pod-name> bash命令可进入容器。

  • 进入Nginx Ingress Controller容器执行nginx -V命令。

    nginx-ingress-controller-5c455d7d9f-nr7dd:/etc/nginx$ nginx -V

    预期输出。

    nginx version: nginx/1.21.6
    built by gcc 12.2.1 20220924 (Alpine 12.2.1_git20220924-r10)
    built with OpenSSL 3.1.3 19 Sep 2023 (running with OpenSSL 3.1.5 30 Jan 2024)
    TLS SNI support enabled
    configure arguments: --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --modules-path=/etc/nginx/modules --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-compat --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_geoip_module --with-http_gzip_static_module --with-http_sub_module --with-http_v2_module --with-stream --with-stream_ssl_module --with-stream_realip_module --with-stream_ssl_preread_module --with-threads --with-http_secure_link_module --with-http_gunzip_module --with-file-aio --without-mail_pop3_module --without-mail_smtp_module --without-mail_imap_module --without-http_uwsgi_module --without-http_scgi_module --with-cc-opt='-g -O3 -flto -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wno-deprecated-declarations -fno-strict-aliasing -D_FORTIFY_SOURCE=2 --param=ssp-buffer-size=4 -DTCP_FASTOPEN=23 -fPIC -I/root/.hunter/_Base/d45d77d/aab92d8/3b7ee27/Install/include -Wno-cast-function-type -m64 -mtune=generic' --with-ld-opt='-flto -fPIE -fPIC -pie -Wl,-z,relro -Wl,-z,now -L/root/.hunter/_Base/d45d77d/aab92d8/3b7ee27/Install/lib' --user=www-data --group=www-data --add-module=/tmp/build/ngx_devel_kit-0.3.2 --add-module=/tmp/build/set-misc-nginx-module-0.33 --add-module=/tmp/build/headers-more-nginx-module-0.34 --add-module=/tmp/build/ngx_http_substitutions_filter_module-b8a71eacc7f986ba091282ab8b1bbbc6ae1807e0 --add-module=/tmp/build/lua-nginx-module-0.10.25 --add-module=/tmp/build/stream-lua-nginx-module-0.0.13 --add-module=/tmp/build/lua-upstream-nginx-module-8aa93ead98ba2060d4efd594ae33a35d153589bf --add-dynamic-module=/tmp/build/nginx-http-auth-digest-1.0.0 --add-dynamic-module=/tmp/build/nginx-opentracing-0.19.0/opentracing --add-dynamic-module=/tmp/build/ModSecurity-nginx-1.0.3 --add-dynamic-module=/tmp/build/ngx_http_geoip2_module-a26c6beed77e81553686852dceb6c7fdacc5970d --add-dynamic-module=/tmp/build/ngx_brotli
  • 进入Nginx Ingress Controller容器执行openssl ciphers命令。

    nginx-ingress-controller-5c455d7d9f-nr7dd:/etc/nginx$ openssl ciphers

    预期输出:

    TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:TLS_AES_128_GCM_SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:RSA-PSK-AES256-GCM-SHA384:DHE-PSK-AES256-GCM-SHA384:RSA-PSK-CHACHA20-POLY1305:DHE-PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:AES256-GCM-SHA384:PSK-AES256-GCM-SHA384:PSK-CHACHA20-POLY1305:RSA-PSK-AES128-GCM-SHA256:DHE-PSK-AES128-GCM-SHA256:AES128-GCM-SHA256:PSK-AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:ECDHE-PSK-AES256-CBC-SHA384:ECDHE-PSK-AES256-CBC-SHA:SRP-RSA-AES-256-CBC-SHA:SRP-AES-256-CBC-SHA:RSA-PSK-AES256-CBC-SHA384:DHE-PSK-AES256-CBC-SHA384:RSA-PSK-AES256-CBC-SHA:DHE-PSK-AES256-CBC-SHA:AES256-SHA:PSK-AES256-CBC-SHA384:PSK-AES256-CBC-SHA:ECDHE-PSK-AES128-CBC-SHA256:ECDHE-PSK-AES128-CBC-SHA:SRP-RSA-AES-128-CBC-SHA:SRP-AES-128-CBC-SHA:RSA-PSK-AES128-CBC-SHA256:DHE-PSK-AES128-CBC-SHA256:RSA-PSK-AES128-CBC-SHA:DHE-PSK-AES128-CBC-SHA:AES128-SHA:PSK-AES128-CBC-SHA256:PSK-AES128-CBC-SHA
  • 进入Nginx Ingress Controller容器执行openssl ciphers -tls1_2 -v命令。显示仅支持TLS 1.2的密码套件。

    nginx-ingress-controller-5c455d7d9f-nr7dd:/etc/nginx$ openssl ciphers -tls1_2 -v

    预期输出。

    TLS_AES_256_GCM_SHA384         TLSv1.3 Kx=any      Au=any   Enc=AESGCM(256)            Mac=AEAD
    TLS_CHACHA20_POLY1305_SHA256   TLSv1.3 Kx=any      Au=any   Enc=CHACHA20/POLY1305(256) Mac=AEAD
    TLS_AES_128_GCM_SHA256         TLSv1.3 Kx=any      Au=any   Enc=AESGCM(128)            Mac=AEAD
    ECDHE-ECDSA-AES256-GCM-SHA384  TLSv1.2 Kx=ECDH     Au=ECDSA Enc=AESGCM(256)            Mac=AEAD
    ECDHE-RSA-AES256-GCM-SHA384    TLSv1.2 Kx=ECDH     Au=RSA   Enc=AESGCM(256)            Mac=AEAD
    DHE-RSA-AES256-GCM-SHA384      TLSv1.2 Kx=DH       Au=RSA   Enc=AESGCM(256)            Mac=AEAD
    ECDHE-ECDSA-CHACHA20-POLY1305  TLSv1.2 Kx=ECDH     Au=ECDSA Enc=CHACHA20/POLY1305(256) Mac=AEAD
    ECDHE-RSA-CHACHA20-POLY1305    TLSv1.2 Kx=ECDH     Au=RSA   Enc=CHACHA20/POLY1305(256) Mac=AEAD
    ...

如何通过IANA/RFC标准名称获取对应的OpenSSL名称?

密码套件的命名遵循两种规则:OpenSSL的命名比较简洁,而IANA/RFC的命名则更为详细。例如,OpenSSL中的AES128-SHA对应着IANA/RFC中的TLS_RSA_WITH_AES_128_CBC_SHA

如果您已知一个IANA的密码套件名称,需要获取其OpenSSL名称,可通过以下两种方式:

  • 通过OpenSSL站点获取对应的OpenSSL的名称。

  • 执行openssl ciphers -convert <已知IANA的密码套件名称>命令获取对应的转换名称。

    nginx-ingress-controller-5c455d7d9f-nr7dd:/etc/nginx$ openssl ciphers -convert TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
    OpenSSL cipher name: ECDHE-RSA-AES128-GCM-SHA256