在 Kubernetes (GKE) 上创建 cockroachdb 用户证书的最后一步

Final step in creating cockroachdb user certificates on Kubernetes (GKE)

我在创建用于 NodeJS pg 客户端的用户证书以访问 GKE 上的安全 CockroachDB 运行 的最后一步时遇到了问题。 (使用 root 用户客户端密钥和证书一切正常,但我不想使用 root 进行 pg 访问)。

容器的 /cockroach-certs 目录如下所示

ca.crt  client.root.crt  client.root.key

kubectl exec -it cockroachdb-client-secure -- ./cockroach  --certs-dir=/cockroach-certs cert list

演出

+-----------------------+------------------+-----------------+------------+--------------+-------+
|         Usage         | Certificate File |    Key File     |  Expires   |    Notes     | Error |
+-----------------------+------------------+-----------------+------------+--------------+-------+
| Certificate Authority | ca.crt           |                 | 2023/09/03 | num certs: 1 |       |
| Client                | client.root.crt  | client.root.key | 2023/09/03 | user: root   |       |
+-----------------------+------------------+-----------------+------------+--------------+-------+

我使用了 cockroach 的客户端-secure.yaml (https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/client-secure.yaml) - 用于为 root 设置 CSR 的同一个客户端 - 但将用户名更改为 xyz(已作为用户添加到数据库)。 这成功地为 xyz 生成了 CSR,然后我批准了它。

default.client.xyz                               8m        system:serviceaccount:default:cockroachdb   Approved,Issued

并创建了预期的秘密,我可以从中导出客户端密钥和证书。

default.client.xyz                Opaque                                2         9m

问题是证书列表没有显示 client.xyz.key 或 client.xyz.crt 并且它们不在 /cockroach-certs 目录中。如果我从 default.client.xyz 秘密中提取它们并手动将它们复制到那里,它们会显示在证书列表中但不会分配给特定用户。

cockroach docs使用"cockroach cert"创建用户,但没有显示使用kubernetes时的具体过程。所以我错过了最后一块拼图 - 为什么 client-secure.yaml 使用 -user=root 完成整个过程,但错过了使用 -user=xyz 的最后一步,以及我错过的步骤是什么?

.. 添加了更多信息 容器声称已经写入了证书文件,但实际上它们并不存在。

$ kubectl logs fidserver-csr  -c init-certs

+ /request-cert '-namespace=default' '-certs-dir=/cockroach-certs' '-type=client' '-user=fidserver' '-symlink-ca-from=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
2018/09/07 15:32:11 Looking up cert and key under secret default.client.fidserver
W0907 15:32:11.700733       1 client_config.go:529] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
2018/09/07 15:32:11 Writing cert and key to local files
wrote key file: /cockroach-certs/client.fidserver.key
wrote certificate file: /cockroach-certs/client.fidserver.crt
symlinked CA certificate file: /cockroach-certs/ca.crt -> /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

... 已更新以在不同的证书目录中重试 - 实际上没有文件被写入,尽管日志声称它们是。符号链接也没有发生。

+ /request-cert '-namespace=default' '-certs-dir=/cockroach-client-certs' '-type=client' '-user=fidserver' '-symlink-ca-from=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
2018/09/08 09:35:56 Looking up cert and key under secret default.client.fidserver
W0908 09:35:56.160525       1 client_config.go:529] Neither --kubeconfig nor --master was specified.  Using the inClusterConfig.  This might not work.
2018/09/08 09:35:56 Secret default.client.fidserver not found, sending CSR
Sending create request: default.client.fidserver for 
Request sent, waiting for approval. To approve, run 'kubectl certificate approve default.client.fidserver'
2018-09-08 09:36:26.718183601 +0000 UTC m=+30.564697651: waiting for 'kubectl certificate approve default.client.fidserver'
2018-09-08 09:36:56.718422397 +0000 UTC m=+60.564936446: waiting for 'kubectl certificate approve default.client.fidserver'
2018-09-08 09:37:26.718657743 +0000 UTC m=+90.565171864: waiting for 'kubectl certificate approve default.client.fidserver'
2018-09-08 09:37:56.718959817 +0000 UTC m=+120.565473905: waiting for 'kubectl certificate approve default.client.fidserver'
CSR approved, but no certificate in response. Waiting some more
request default.client.fidserver Approved at 2018-09-08 09:38:00 +0000 UTC
  reason:   KubectlApprove
  message:  This CSR was approved by kubectl certificate approve.
2018/09/08 09:38:00 Storing cert and key under secret default.client.fidserver
2018/09/08 09:38:01 Writing cert and key to local files
wrote key file: /cockroach-client-certs/client.fidserver.key
wrote certificate file: /cockroach-client-certs/client.fidserver.crt
symlinked CA certificate file: /cockroach-client-certs/ca.crt -> /var/run/secrets/kubernetes.io/serviceaccount/ca.crt

现已记录为蟑螂问题。 https://github.com/cockroachdb/cockroach/issues/29968

这是在 the github issue 中调试的。

request-cert init 容器获取的证书和密钥是用户通过 --user 参数请求的证书和密钥。

这意味着容器将只能访问一组客户端凭据。该卷也不会用其他证书更新,因为这仅在初始时间完成。

要为不同的用户申请证书,必须创建一个新的 pod,包括:

  • request-cert 初始化容器 --user=<desired user>
  • 容器在 /cockroach-certs
  • 中使用 <desired user> 的客户端证书