旋转密码时与 documentdb 的命令行连接

Command line connection to documentdb when rotating password

我正在使用 documentdb,密码通过 aws secret manager 自动轮换。我想编写一种通过命令行连接到数据库的快速方法。由于密码经常更改,这意味着有一个命令将从 aws secerts 加载密码并将其传递给 mongo 连接字符串。我想出了这个可怕的班轮来连接 mongo:

mongo --ssl --host *my_host*:27017 --sslCAFile rds-combined-ca-bundle.pem --username admin --password `aws secretsmanager get-secret-value --secret-id documentDB_login | jq .SecretString | jq fromjson | jq .password`

我已经 运行 aws configure 并且 secretmanager 正确 return 我的密码。

我发誓这起初有效,但现在失败了,说当它试图连接到我的 mongo 实例时身份验证失败。如果我回显上面丑陋的一个衬里,以便我可以看到 aws secretmanager 调用的结果,然后将回显响应复制并粘贴到我的命令行中,它会正确连接,所以不确定为什么功能等效的命令没有。

如何编写脚本以通过命令行连接到 documentdb?有没有更简洁的方法(最好是不需要 yum install of jp 的方法)来做到这一点?

我不能确定为什么你的命令以前有效但现在无效,但看起来它没有去除双引号。 AWS cli 也可以为您进行 JSON 解析,但不幸的是,它无法解析 secret 本身中嵌套的 JSON。为此,您仍然需要 jq。但是,将 CLI JSON 解析与 jq 结合起来,您可以稍微简化它(尽管它不会更短):

mongo --ssl --host *my_host*:27017 --sslCAFile rds-combined-ca-bundle.pem --username admin --password `aws secretsmanager get-secret-value --secret-id documentDB_login --query 'SecretString' --output text | jq -r .password`