istio-ingress网关安全-文件挂载

生成服务器证书和私钥

创建一个根证书和私钥以为您的服务所用的证书签名:

$ openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -subj '/O=example Inc./CN=example.com' -keyout example.com.key -out example.com.crt

httpbin.example.com 创建一个证书和私钥:

$ openssl req -out httpbin.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin.example.com.key -subj "/CN=httpbin.example.com/O=httpbin organization" $ openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin.example.com.csr -out httpbin.example.com.crt 

基于文件挂载的方式配置 TLS ingress 网关

本节中,您将配置一个使用 443 端口的 ingress 网关,以处理 HTTPS 流量。 首先使用证书和私钥创建一个 secret。该 secret 将被挂载为 /etc/istio/ingressgateway-certs 路径下的一个文件。 然后您可以创建一个网关定义,它将配置一个运行于端口 443 的服务。

1、创建一个 Kubernetes secret 以保存服务器的证书和私钥。使用 kubectl 在命名空间 istio-system 下创建 secret istio-ingressgateway-certs。Istio 网关将会自动加载该 secret。

该 secret 必须在 istio-system 命名空间下,且名为 istio-ingressgateway-certs,以与此任务中使用的 Istio 默认 ingress 网关的配置保持一致。
$ kubectl create -n istio-system secret tls istio-ingressgateway-certs --key httpbin.example.com.key --cert httpbin.example.com.crt
secret "istio-ingressgateway-certs" created

请注意,默认情况下,istio-system 命名空间下的所有 pod 都能挂载这个 secret 并访问该私钥。您可以将 ingress 网关部署到一个单独的命名空间中,并在那创建 secret,这样就只有这个 ingress 网关 pod 才能挂载它。

验证 tls.crttls.key 是否都已经挂载到 ingress 网关 pod 中:

$ kubectl exec -it -n istio-system $(kubectl -n istio-system get pods -l istio=ingressgateway -o jsonpath='{.items[0].metadata.name}') -- ls -al /etc/istio/ingressgateway-certs

2、为 443 端口定义 Gateway 并设置 server

证书和私钥必须位于 /etc/istio/ingressgateway-certs,否则网关将无法加载它们。
 kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: SIMPLE
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
      privateKey: /etc/istio/ingressgateway-certs/tls.key
    hosts:
    - "httpbin.example.com"
EOF

3、配置路由以让流量从 Gateway 进入。定义与控制 Ingress 流量任务中相同的 VirtualService

kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: httpbin
spec:
  hosts:
  - "httpbin.example.com"
  gateways:
  - httpbin-gateway
  http:
  - match:
    - uri:
        prefix: /status
    - uri:
        prefix: /delay
    route:
    - destination:
        port:
          number: 8000
        host: httpbin
EOF

4、使用 curl 发送一个 https 请求到 SECURE_INGRESS_PORT 以通过 HTTPS 访问 httpbin 服务。

--resolve 标志让 curl 在通过 TLS 访问网关 IP 时支持 SNIhttpbin.example.com--cacert 选项则让 curl 使用您创建的证书来验证服务器。

-HHost:httpbin.example.com 标志也包含了,但只有当 SECURE_INGRESS_PORT 与实际网关端口(443)不同(例如,您通过映射的 NodePort 来访问服务)时才真正需要。

通过发送请求到 /status/418 URL 路径,您可以很好地看到您的 httpbin 服务确实已被访问。 httpbin 服务将返回 418 I’m a Teapot 代码。

curl -v -HHost:httpbin.example.com --resolve httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST --cacert example.com.crt https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418
...
Server certificate:
  subject: CN=httpbin.example.com; O=httpbin organization
  start date: Oct 27 19:32:48 2019 GMT
  expire date: Oct 26 19:32:48 2020 GMT
  common name: httpbin.example.com (matched)
  issuer: O=example Inc.; CN=example.com
  SSL certificate verify ok.
SSL certificate verify ok.
...
HTTP/2 418
...
-=[ teapot ]=-

   _...._
 .'  _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
  |       ;/
  \_     _/
    `"""`
网关定义传播需要时间,因此您可能会得到以下报错: Failed to connect to httpbin.example.com port <your secure port>: Connection refused。请稍后重新执行 curl 命令。

curl 的输出中寻找 Server certificate 部分,尤其是找到与 common name 匹配的行:common name: httpbin.example.com (matched)。 输出中的 SSL certificate verify ok 这一行表示服务端的证书验证成功。 如果一切顺利,您还应该看到返回的状态 418,以及精美的茶壶图。

配置双向 TLS ingress 网关

1、创建一个 Kubernetes Secret 以保存服务端将用来验证它的客户端的 CA 证书。使用 kubectl 在命名空间 istio-system 中创建 secret istio-ingressgateway-ca-certs。Istio 网关将会自动加载该 secret。

该 secret 必须在 istio-system 命名空间下,且名为 istio-ingressgateway-ca-certs,以与此任务中使用的 Istio 默认 ingress 网关的配置保持一致。
kubectl create -n istio-system secret generic istio-ingressgateway-ca-certs --from-file=example.com.crt
secret "istio-ingressgateway-ca-certs" created

2、重新定义之前的 Gateway,修改 TLS 模式为 MUTUAL,并指定 caCertificates

证书必须位于 /etc/istio/ingressgateway-ca-certs,否则网关将无法加载它们。 证书的(短)文件名必须与您创建 secret 的名称相同,在本例中为 example.com.crt。
kubectl apply -f - <<EOF
apiVersion: networking.istio.io/v1alpha3
kind: Gateway
metadata:
  name: httpbin-gateway
spec:
  selector:
    istio: ingressgateway # use istio default ingress gateway
  servers:
  - port:
      number: 443
      name: https
      protocol: HTTPS
    tls:
      mode: MUTUAL
      serverCertificate: /etc/istio/ingressgateway-certs/tls.crt
      privateKey: /etc/istio/ingressgateway-certs/tls.key
      caCertificates: /etc/istio/ingressgateway-ca-certs/example.com.crt
    hosts:
    - "httpbin.example.com"
EOF

3、像上一节中一样通过 HTTPS 访问 httpbin 服务:

curl -HHost:httpbin.example.com --resolve httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST --cacert example.com.crt https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418
curl: (35) error:14094410:SSL routines:SSL3_READ_BYTES:sslv3 alert handshake failure
网关定义传播需要时间,因此您可能会仍然得到 418 状态码。请稍后重新执行 curl 命令。

这次您将得到一个报错,因为服务端拒绝接受未认证的请求。您需要传递 curl 客户端证书和私钥以将请求签名。

4、为 httpbin.example.com 服务创建客户端证书。您可以使用 httpbin-client.example.com URI 来指定客户端,或使用其它 URI。

openssl req -out httpbin-client.example.com.csr -newkey rsa:2048 -nodes -keyout httpbin-client.example.com.key -subj "/CN=httpbin-client.example.com/O=httpbin's client organization"
openssl x509 -req -days 365 -CA example.com.crt -CAkey example.com.key -set_serial 0 -in httpbin-client.example.com.csr -out httpbin-client.example.com.crt

5、重新用 curl 发送之前的请求,这次通过参数传递客户端证书(添加 --cert 选项)和您的私钥(--key 选项):

curl -HHost:httpbin.example.com --resolve httpbin.example.com:$SECURE_INGRESS_PORT:$INGRESS_HOST --cacert example.com.crt --cert httpbin-client.example.com.crt --key httpbin-client.example.com.key https://httpbin.example.com:$SECURE_INGRESS_PORT/status/418
-=[ teapot ]=-

   _...._
 .'  _ _ `.
| ."` ^ `". _,
\_;`"---"`|//
  |       ;/
  \_     _/
    `"""`

这次服务器成功执行了客户端身份验证,您再次收到了漂亮的茶壶图。

参考:

https://preliminary.istio.io/latest/zh/docs/tasks/traffic-management/ingress/secure-ingress-mount/

原文地址:https://www.cnblogs.com/fat-girl-spring/p/15194914.html