10

我的 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 令牌方法(本质上是一个 user:pass)。但是我不希望将任何凭据签入源代码控制,因此我试图从 github 克隆以通过 SSH 在我的 EB 实例上工作。

我已经尝试了上百万种方法,包括npm preinstall根据这篇博客文章编写的脚本,这些脚本在 npm2 之前一直有效,在 npm2 中更改了 preinstall 以在构建树后运行,并且修复该问题的 PR 仍然悬而未决。

我尝试了一个.ebextensions命令配置,它试图调用git configinsteadofgit@github.com 上的一个 HTTPS URL 放入一个带有来自环境变量的 OAUTH 令牌的 HTTPS URL(本身很棘手,因为此时在启动时没有设置 env 变量循环,并且缺少 $HOME 会使 git config 混淆)。

我还尝试了各种不同的方法来.ebextensions在我的 EB 实例上设置 SSH,包括来自上述博客文章的评论中的这个解决方案。这基本上是我现在卡住的地方。

  • 我已经成功创建了一个密钥对,在我的 github 配置文件上进行了设置,并验证了我的本地客户端可以使用私钥来克隆我的 repo
  • 我已将我的私钥和 ssh 配置文件放在私有 S3 存储桶上
  • 根据此示例,我创建了一个.ebextensions files配置,将这两个文件从我的 S3 存储桶复制到/tmp/.ssh/
  • 我创建了一个调试commands .ebextensions配置,其中列出了 /tmp/.ssh 并显示文件已成功从 S3 下载:

/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 没有设置,嗯......这应该很容易,但让我发疯。

4

1 回答 1

16

经过一整天的努力,终于在我之前错过的一个非常相似的问题的答案中跌跌撞撞,事实证明,放置 ssh 密钥以便在 EB 上被 git 拾取的正确位置是 in /root/.sshnot /tmp/.sshnot /home/ec2-user/.ssh

我的最终配置(假设有一个私有 SSH 密钥位于 S3 存储桶中<my-bucket>/github-eb-key,并且相应的公钥已向有权访问存储库的 github 用户注册),使用配置为 的 AMI 64bit Amazon Linux 2016.09 v3.3.0 running Node.js,并在 中使用以下内容.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
于 2017-04-19T21:37:47.523 回答