Istio安全-证书管理

注:本章更新至1.8版本

插入现有CA证书

本节展示了管理员如何使用现有的根证书来授权istio证书,签发证书和密钥,不使用Istio自动生成的证书。

默认情况下,istio的CA会生成一个自签的根证书和密钥,并使用它们签发负载证书。istio的CA也可以使用管理员指定的证书和密钥,以及管理员指定的根证书来签发负载证书。

根CA是网格中所有负载信任的根证书。每个Istio CA会使用一个中间CA来签发密钥和证书,该CA由根CA签发。当一个网格中存在多个Istio CA时,会在CA之间建立起信任层级。

本节展示如何将这些证书和密钥插入Istio的CA。

插入现有证书和密钥

在istio的samples/certs目录下有一套证书,可以比较好地解释istio的证书交互原理。

  • root-cert.pem: root CA certificate.
  • ca-[cert|key].pem: Citadel intermediate certificate and corresponding private key.
  • cert-chain.pem: certificate trust chain.
  • workload-foo-[cert|key].pem: workload certificate and key for URI SAN spiffe://trust-domain-foo/ns/foo/sa/foo signed by ca-cert.key.
  • workload-bar-[cert|key].pem: workload certificate and key for URI SAN spiffe://trust-domain-bar/ns/bar/sa/bar signed by ca-cert.key.

假如希望使用现有的CA证书ca-cert.pemca-key.pem(root-cert.pem签发了ca-cert.pem,因此root-cert.pem作为所有负载的根证书)。由于ca-cert.pem不同于root-cert.pem,因此无法直接通过根证书进行校验,此时需要通过一个cert-chain.pem来指定信任链,包含负载到根CA的所有中间CAs。在上述例子中,仅包含了istio的CA签名证书,因此cert-chain.pemca-cert.pem相同。注意:如果 ca-cert.pemroot-cert.pem是相同的,那么 cert-chain.pem文件应该是空的。

上述证书例子中的证书链为:root-cert.pem-->cert-chain.pem(含ca-cert.pem)-->workload-foo-cert.pem/workload-bar-cert.pem,可以使用如下方式进行校验:

# openssl verify -CAfile <(cat ca-cert.pem root-cert.pem) workload-bar-cert.pem
workload-bar-cert.pem: OK

对于生产环境,最好在一台离线机器上执行如下步骤,确保将根密钥暴露给尽可能少的人

  1. 创建一个保存证书和密钥的目录

    $ mkdir -p certs
    $ pushd certs
  2. 生成根证书和密钥

    $ make -f ../tools/certs/Makefile.selfsigned.mk root-ca

    上述命令将生成如下文件:

    • root-cert.pem: 根证书
    • root-key.pem: 根密钥
    • root-ca.conf: openssl 使用该配置来生成根证书
    • root-cert.csr: 为根证书生成的CSR
  3. 生成中间证书和密钥

    $ make -f ../tools/certs/Makefile.selfsigned.mk cluster1-cacerts

    执行如上命令会生成一个名为cluster1的目录,包含如下文件,root-cert.pem签发了ca-cert.pem

    • ca-cert.pem: 中间证书
    • ca-key.pem: 中间密钥
    • cert-chain.pem: Istiod使用的证书链
    • root-cert.pem: 根证书
    • intermediate.conf: openssl 使用该配置来生成中间证书
    • cluster-ca.csr: 为中间证书生成的CSR

    可以将cluster1替换为任何字符串。例如make mycluster-certs将会生成名为mycluster的目录

    为了配置其他Istio CA,可以重复执行上述步骤来生成不同名称的证书目录

  4. 创建一个cacerts secret,包含如下输入文件ca-cert.pem, ca-key.pem, root-cert.pemcert-chain.pem。需要注意的是创建出来的secret的名称必须是cacerts,这样才能被istio正确挂载。

    $ kubectl create namespace istio-system
    $ kubectl create secret generic cacerts -n istio-system \
    --from-file=cluster1/ca-cert.pem \
    --from-file=cluster1/ca-key.pem \
    --from-file=cluster1/root-cert.pem \
    --from-file=cluster1/cert-chain.pem
  5. 返回Istio安装的顶层目录

    $ popd

部署Istio

使用demo profile,Istio会从挂载的secret文件中读取证书

$ istioctl install --set profile=demo

在下面的例子中,istio的CA证书(ca-cert.pem)与根证书(root-cert.pem)不同,因此负载无法通过根证书验证工作负载证书,需要使用一个cert-chain.pem来指定信任的证书链,该证书链包含负载和根CA之间的所有中间CA,在此例子中,它包含了istio的CA签名证书,因此cert-chain.pemca-cert.pem相同的。注意,如果ca-cert.pemroot-cert.pem相同,那么ca-chain.pem文件应该是空的。

配置示例services

  1. 部署httpbin和sleep示例services

    $ kubectl create ns foo
    $ kubectl apply -f <(istioctl kube-inject -f samples/httpbin/httpbin.yaml) -n foo
    $ kubectl apply -f <(istioctl kube-inject -f samples/sleep/sleep.yaml) -n foo
  2. 部署一个策略,使得foo命名空间的负载仅接受mutual TLS流量

    $ kubectl apply -n foo -f - <<EOF
    apiVersion: "security.istio.io/v1beta1"
    kind: "PeerAuthentication"
    metadata:
    name: "default"
    spec:
    mtls:
    mode: STRICT
    EOF

校验证书

本节中会校验插入到CA中的证书是否签发了负载证书。

  1. sleep 20s,等待mTLS策略下httpbin的证书链生效。由于CA证书是自签的,因此openssl命令会返回verify error:num=19:self signed certificate in certificate chain错误。

    $ sleep 20; kubectl exec "$(kubectl get pod -l app=sleep -n foo -o jsonpath={.items..metadata.name})" -c istio-proxy -n foo -- openssl s_client -showcerts -connect httpbin.foo:8000 > httpbin-proxy-cert.txt
  2. 解析证书链中的证书

    $ sed -n '/-----BEGIN CERTIFICATE-----/{:start /-----END CERTIFICATE-----/!{N;b start};/.*/p}' httpbin-proxy-cert.txt > certs.pem
    $ awk 'BEGIN {counter=0;} /BEGIN CERT/{counter++} { print > "proxy-cert-" counter ".pem"}' < certs.pem
  3. 校验根证书与管理员指定的证书相同

    $ openssl x509 -in samples/certs/root-cert.pem -text -noout > /tmp/root-cert.crt.txt
    $ openssl x509 -in ./proxy-cert-3.pem -text -noout > /tmp/pod-root-cert.crt.txt
    $ diff -s /tmp/root-cert.crt.txt /tmp/pod-root-cert.crt.txt
    Files /tmp/root-cert.crt.txt and /tmp/pod-root-cert.crt.txt are identical
  4. 校验CA证书与管理员指定的相同

    $ openssl x509 -in samples/certs/ca-cert.pem -text -noout > /tmp/ca-cert.crt.txt
    $ openssl x509 -in ./proxy-cert-2.pem -text -noout > /tmp/pod-cert-chain-ca.crt.txt
    $ diff -s /tmp/ca-cert.crt.txt /tmp/pod-cert-chain-ca.crt.txt
    Files /tmp/ca-cert.crt.txt and /tmp/pod-cert-chain-ca.crt.txt are identical
  5. 校验从根证书到负载证书的证书链。下面使用中间证书和根证书组成的证书链来校验其签发了负载证书

    $ openssl verify -CAfile <(cat samples/certs/ca-cert.pem samples/certs/root-cert.pem) ./proxy-cert-1.pem
    ./proxy-cert-1.pem: OK

卸载

卸载证书cacertfoo以及istio-system命名空间

$ kubectl delete secret cacerts -n istio-system
$ kubectl delete ns foo istio-system

Istio的DNS证书管理

本节展示如何使用 Chiron提供和管理DNS证书,Chiron是一个与istiod相连的轻量型组件,它使用kubernetes的CA API签发证书,无需管理私钥。有如下优势:

  • 与isitod不同,这种方式无需维护签发的私钥,增强了安全性
  • 简化了将根证书分发到TLS客户端。客户端不再需要等待istiod生成并分发其CA证书

首先使用istioctl安装istio,并配置DNS证书,当istiod启动后会读取该配置:

$ cat <<EOF > ./istio.yaml
apiVersion: install.istio.io/v1alpha1
kind: IstioOperator
spec:
meshConfig:
certificates:
- secretName: dns.example1-service-account
dnsNames: [example1.istio-system.svc, example1.istio-system]
- secretName: dns.example2-service-account
dnsNames: [example2.istio-system.svc, example2.istio-system]
EOF
$ istioctl install -f ./istio.yaml

可以看到在istio-system下生成了两个secret:

# kubectl get secret -n istio-system |grep dns
dns.example1-service-account istio.io/dns-key-and-cert 3 81s
dns.example2-service-account istio.io/dns-key-and-cert 3 81s

DNS证书的提供和管理

Istio根据用户的配置为DNS证书提供了DNS名字和secret名称。DNS证书由kubernetes CA签发,并根据配置保存到secret中。istio也管理着DNS证书的生命周期,包括证书滚动和重新生成。

配置DNS证书

可以在 istioctl install 命令中使用IstioControlPlane用户资源对istio进行配置。dnsNames字段用于设定证书中的DNS名称,secretName字段指定保存证书和密钥的kubernetes secret的名称。

检查提供的DNS证书

在配置istio生成DNS证书并保存到secret后,需要校验提供的证书是否能够正确运行。

为了校验istio前面例子中生成的dns.example1-service-account的DNS证书,以及校验该证书是否包含配置的DNS名称,需要获取kubernetes的secret,解析并对其解码,查看其具体内容:

$ kubectl get secret dns.example1-service-account -n istio-system -o jsonpath="{.data['cert-chain\.pem']}" | base64 --decode | openssl x509 -in /dev/stdin -text -noout

输出的文本包括:

X509v3 Subject Alternative Name:
DNS:example1.istio-system.svc, DNS:example1.istio-system

重新生成DNS证书

istio可以在DNS证书被错删的情况下重新生成证书。

  1. 删除前面保存的DNS证书

    $ kubectl delete secret dns.example1-service-account -n istio-system
  2. 校验istio重新生成了删除的DNS证书,且证书包含配置的DNS名称。需要从kubernetes获取secret,解析并对其解码,获取其内容:

    $sleep 10; kubectl get secret dns.example1-service-account -n istio-system -o jsonpath="{.data['cert-chain\.pem']}" | base64 --decode | openssl x509 -in /dev/stdin -text -noout

输出包括

X509v3 Subject Alternative Name:
DNS:example1.istio-system.svc, DNS:example1.istio-system

卸载

$ kubectl delete ns istio-system

Istio安全-证书管理(实操一)的更多相关文章

  1. Istio的流量管理(实操一)(istio 系列三)

    Istio的流量管理(实操一)(istio 系列三) 使用官方的Bookinfo应用进行测试.涵盖官方文档Traffic Management章节中的请求路由,故障注入,流量迁移,TCP流量迁移,请求 ...

  2. Istio的流量管理(实操二)(istio 系列四)

    Istio的流量管理(实操二)(istio 系列四) 涵盖官方文档Traffic Management章节中的inrgess部分. 目录 Istio的流量管理(实操二)(istio 系列四) Ingr ...

  3. Istio的流量管理(实操三)

    Istio的流量管理(实操三) 涵盖官方文档Traffic Management章节中的egress部分.其中有一小部分问题(已在下文标注)待官方解决. 目录 Istio的流量管理(实操三) 访问外部 ...

  4. Istio安全-授权(实操三)

    Istio安全-授权 目录 Istio安全-授权 授权HTTP流量 为使用HTTP流量的负载配置访问控制 卸载 授权TCP流量 部署 配置TCP负载的访问控制 卸载 使用JWT进行授权 部署 使用有效 ...

  5. ABP入门系列(1)——学习Abp框架之实操演练

    作为.Net工地搬砖长工一名,一直致力于挖坑(Bug)填坑(Debug),但技术却不见长进.也曾热情于新技术的学习,憧憬过成为技术大拿.从前端到后端,从bootstrap到javascript,从py ...

  6. 号外号外:9月13号《Speed-BI云平台案例实操--十分钟做报表》开讲了

    引言:如何快速分析纷繁复杂的数据?如何快速做出老板满意的报表?如何快速将Speed-BI云平台运用到实际场景中?         本课程将通过各行各业案例背景,将Speed-BI云平台运用到实际场景中 ...

  7. Mysql MHA(GTID)配置(实操)

    实现环境 centos6.7 MYSQL5.6.36 主:192.168.1.191 从1:192.168.1.145 从2:192.168.1.146 监测:放在从2上 192.168.1.146 ...

  8. Selenium之unittest测试框架详谈及实操

    申明:本文是基于python3.x及selenium3.x. unittest,也可以称为PyUnit,可以用来创建全面的测试套件,可以用于单元自动化测试(模块).功能自动化测试(UI)等等. 官方文 ...

  9. unittest测试框架详谈及实操(二)

    类级别的setUp()方法与tearDown()方法 在实操(一)的例子中,通过setUp()方法为每个测试方法都创建了一个Chrome实例,并且在每个测试方法执行结束后要关闭实例.是不是觉得有个多余 ...

随机推荐

  1. React 17 发布候选版本, 没有添加新功能

    React 17 发布候选版本, 没有添加新功能 React v17.0 Release Candidate: No New Features https://reactjs.org/blog/202 ...

  2. Serverless & FaaS

    Serverless & FaaS Function as a Service 通过 Functions(一个事件驱动型无服务器计算平台,还可以解决复杂的业务流程问题)更加高效地进行开发; 在 ...

  3. how to watch vuex state update

    how to watch vuex state update watch https://vuex.vuejs.org/api/#watch https://vuex.vuejs.org/guide/ ...

  4. Win10安装VSCode并配置Python环境 完整版超详细简单【原创】

    我们分为三个步骤进行: 一.下载VSCode 二.配置Python环境 三.测试Python 一.下载VSCode 1.打开国内镜像vscode下载地址,即可自动下载:https://vscode.c ...

  5. Go 去找个对象吧

    前言 我的读者中应该大部分都是 Java 从业者,不知道写 Java 这些年是否真的有找到对象? 没找到也没关系,总不能在一棵树上吊死,我们也可以来 Go 这边看看,说不定会有新发现. 开个玩笑,本文 ...

  6. es命令测试

    1.新建索引并赋值 :put/索引名/文档名/id //文档名后面会逐渐取消 相当表 PUT /test1/type1/1{ "nmae":"hb", &quo ...

  7. python进阶(4)文件操作

    文件操作 文件操作主要包括对文件内容的读写操作,这些操作是通过文件对象实现的,通过文件对象可以读写文本文件和二进制文件 open(file, mode='r', buffering=-1, encod ...

  8. idea添加汉化包之后出现的一些问题 解决方案

    先把原链接放在这:https://jingyan.baidu.com/article/fb48e8bef2bcb66e622e14d2.html 关掉idea之后,一定要记住是在压缩软件中打开,而不是 ...

  9. 搭载华为麒麟9000的Mate X2:秒售罄,一机难求

    本文首发 | 公众号:lunvey 昨日10点,搭载了麒麟9000芯片的华为Mate X2正式开售,定价17999,对于手机来说,价格实在是高昂. 虽然价格高昂,但是一分钱一分货,对于想抢先体验的网友 ...

  10. Redis-第十章节-链表

    目录 数组和链表 链表 对比 总结 1.数组和链表 数组: 数组会在内存中开辟一块连续的空间存储数据,这种存储方式有利也有弊端.当获取数据的时候,直接通过下标值就可以获取到对应的元素,时间复杂度为O( ...