将 SSH 私钥存储在 GitLab 存储库变量中

2023-12-27

使用 GitLab CI,我想将生产代码推送到远程网络主机。

为了使用 SSH 连接,我将密钥对的私钥存储在我的 GitLab 存储库的变量中。我还将公钥复制到服务器的授权密钥中。这是我的(一部分).gitlab-ci.yml.

image: ubuntu

before_script:
  # Setup SSH credentials and known host
  - which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )
  - mkdir -p ~/.ssh
  - echo "$SSH_PRIVATE" | tr -d '\r' > ~/.ssh/id_rsa
  - chmod 700 ~/.ssh/id_rsa
  - eval "$(ssh-agent -s)"
  - ssh-add ~/.ssh/id_rsa
  - echo "$SSH_KNOWN_HOSTS"
  - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts

这个方法可行,但我质疑它的安全性。这样我的私钥安全吗?我还能如何实现我正在寻找的结果?

编辑:我特别质疑这种方法在生产环境中的安全性。


官方的例子是在gitlab-examples/ssh-私钥 https://gitlab.com/gitlab-examples/ssh-private-key/

Its .gitlab-ci.yml https://gitlab.com/gitlab-examples/ssh-private-key/-/blob/master/.gitlab-ci.yml uses a 自定义环境变量 https://docs.gitlab.com/ee/ci/variables/README.html#custom-environment-variables SSH_PRIVATE_KEY,如“如何使用 GitLab CI/CD 简化智能家居配置 / 准备服务器(和 GitLab)以进行 SSH 访问 https://about.gitlab.com/blog/2018/08/02/using-the-gitlab-ci-slash-cd-for-smart-home-configuration-management/#preparing-the-server-and-gitlab-for-ssh-access".

只要那样变量被屏蔽 https://docs.gitlab.com/ee/ci/variables/README.html#mask-a-custom-variable,这应该足够安全。


sneaky https://stackoverflow.com/users/8398149/sneaky建议在评论 https://stackoverflow.com/questions/64699458/storing-ssh-private-key-in-gitlab-repository-variables/64710518?noredirect=1#comment129147001_64710518创建预处理文件:

cat id_rsa | base64 -w0插入script-part你的工作在.gitlab-ci.yml:

mkdir -p ~/.ssh && echo "$SSH_PRIVATE_KEY" | \
  base64 -d > ${SSH_KEY_FILE} && chmod 0600 ${SSH_KEY_FILE}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将 SSH 私钥存储在 GitLab 存储库变量中 的相关文章

随机推荐