为 Elastic Beanstalk 上的 github 私有存储库访问设置 SSH 密钥

Setting up SSH keys for github private repo access on Elastic Beanstalk

我的 Node.JS 项目包含对托管在 github 上的私有 NPM 存储库的引用。这在本地运行良好,但我正在努力让它在 Elastic Beanstalk 上运行。

dependencies: {
  ...
  "express": "^4.12.4",
  "jsonwebtoken": "^5.0.5",
  "my-private-module": "git@github.com:<my-user>/<my-repo>.git#<my-version>",
  ...
}

-

我需要的是能够在我的 Elastic Beanstalk 实例上为 git 设置一个有效的 SSH 配置,而不必在源代码管理中存储密钥等。

显然,EB 实例没有访问我的私有 github 存储库所需的 SSH 密钥。如果我使用内联 username:password@github.com 的 HTTPS 样式 git URL,它工作正常。它还可以使用 github 提供的 oauth token method(本质上是 user:pass)。但我不希望将任何凭据签入源代码管理,因此我尝试从 github 克隆以通过 SSH 在我的 EB 实例上工作。

我已经尝试了一百万种方法,包括 npm preinstall 根据 this blog post 编写的脚本,这些脚本在 npm2 之前一直有效,其中在构建树后对 运行 进行了预安装,并且修复该问题的 PR 仍在等待中。

我尝试了一个 .ebextensions 命令配置,试图调用 git configinsteadof on git@github.com 放入 HTTPS URL 带有来自环境变量的 OAUTH 令牌(这本身就很棘手,因为此时在启动周期中未设置环境变量,并且缺少 $HOME 使得 git 配置混乱)。

我还尝试了使用 .ebextensions 在我的 EB 实例上设置 SSH 的各种不同方法,包括 this solution from the comments on the mentioned blog post。这基本上就是我现在被困的地方。

/tmp/.ssh/config 包含:

Host github.com
    IdentityFile /tmp/.ssh/deploy_key
    IdentitiesOnly yes
    UserKnownHostsFile=/dev/null
    StrictHostKeyChecking no

/tmp/.ssh/deploy_key 包含我的私钥,已验证可在本地使用。

但是,git仍然会报错:

npm ERR! Command failed: git clone --template=/tmp/.npm/_git-remotes/_templates --mirror ssh://git@github.com/[.....]
npm ERR! Cloning into bare repository '/tmp/.npm/_git-remotes/git-ssh-git-github-com-[...]
npm ERR! Host key verification failed.
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

我现在运行没主意了。我最好的猜测是 /tmp/.ssh 不是 git 寻找 ssh 配置文件的路径 - 它可能是在提出链接解决方案时但可能在以后更改 AMI:s等。EB启动时使用的环境好像有点受限;命令是 运行 作为用户 nodejs 但是 /tmp 似乎被用作主目录,即使没有在任何地方设置 $HOME。

如何让 git 获取我的 SSH 配置,然后使用我的 SSH 密钥?我如何找出 git 在哪里寻找 SSH 配置文件?通常它在 ~/.ssh 中,但由于未设置 $HOME,嗯...这应该很容易,但让我抓狂。

经过一整天的努力并最终在 this answer 上绊倒了一个我之前错过的非常相似的问题,结果证明放置 ssh 密钥的正确位置以便被 git 在 EB 中 /root/.sshnot /tmp/.sshnot /home/ec2-user/.ssh.

我的最终配置(假设在 <my-bucket>/github-eb-key 的 S3 存储桶中有一个 SSH 私钥,并且相应的 public 密钥已注册到 github 用户,该用户有权访问回购),使用配置为 64bit Amazon Linux 2016.09 v3.3.0 running Node.js 的 AMI,并在 .ebextensions/01_ssh_setup.config 中使用以下内容:

Resources: 
  AWSEBAutoScalingGroup: 
    Metadata: 
      ? "AWS::CloudFormation::Authentication"
      : 
        S3Auth: 
          buckets: 
            - <my-bucket>
          roleName: 
            ? "Fn::GetOptionSetting"
            : 
              DefaultValue: aws-elasticbeanstalk-ec2-role
              Namespace: "aws:asg:launchconfiguration"
              OptionName: IamInstanceProfile
          type: s3
files: 
  /root/.ssh/github-eb-key: 
    authentication: S3Auth
    mode: "000600"
    owner: root
    group: root
    source: "https://s3-eu-west-1.amazonaws.com/<my-bucket>/github-eb-key"
  /root/.ssh/config: 
    mode: "000600"
    owner: root
    group: root
    content: |
      Host github.com
        IdentityFile /root/.ssh/github-eb-key
        IdentitiesOnly yes
        UserKnownHostsFile=/dev/null
        StrictHostKeyChecking no