为Elastic Beanstalk上的github专用回购访问设置SSH密钥

我的Node.JS项目包含对github上托pipe的私有NPM repos的引用。 这在当地工作正常,但我很努力使这个工作在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设置一个可用的SSHconfiguration,而不必在源代码控制中存储密钥等。

显然,EB实例没有所需的SSH密钥来访问我的私人github回购。 如果我使用内联username:password@github.com HTTPS风格的git URL,它可以正常工作。 它也可以使用github提供的oauth token方法 (本质上是一个user:pass)。 但我不希望有任何凭据检查源代码控制,所以我试图从github克隆到我的EB实例上通过SSH工作。

我已经尝试了一百万种方法,包括根据这个博客文章 npm preinstall脚本,它曾经工作,直到npm2,其中一个更改进行预安装后,运行树build立,和PR来解决这个问题仍悬而未决。

我已经尝试了一个.ebextensions命令configuration,试图调用git config将git@github.com上的一个insteadof放到一个HTTPS URL中,该环境variables来自一个OAUTH标记(本身很棘手,因为envvariables没有设置为这一次在启动周期中,缺less$ HOME会让git config感到困惑)。

我也尝试过使用.ebextensions在我的EB实例上设置SSH的各种不同方法,包括从所提到的博客文章的评论中提供的这个解决scheme 。 这基本上是我卡在现在的地方。

  • 我已经成功地创build了一个密钥对,在我的githubconfiguration文件中进行设置,并validation私钥可以从我的本地客户端使用,以克隆我的回购
  • 我已经把我的私钥和一个sshconfiguration文件放在一个专用的S3存储桶中
  • 我已经创build了一个.ebextensions filesconfiguration,将这两个文件从我的S3存储桶复制到/tmp/.ssh/ ,根据这个例子
  • 我创build了一个debuggingcommands .ebextensionsconfiguration,其中列出了/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包含我的私钥,经validation可以在本地使用。

但是,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去查找sshconfiguration文件的path – 它可能是在提出链接解决scheme的时候,但可能在后来的AMI中发生了变化:使用的环境当EB启动似乎有点有限; 命令以用户nodejs运行,但/ tmp似乎用nodejs目录,即使$ HOME没有设置在任何地方。

我怎样才能让git拿起我的SSHconfiguration,然后使用我的SSH密钥? 我怎样才能找出git在哪里查找SSHconfiguration文件? 通常它在〜/ .ssh中,但是由于$ HOME没有被设置,所以…这应该很容易,但是正在逼迫我。

经过了一整天的努力,最终在这个问题上碰到了一个我之前错过的非常类似的问题,结果发现,把ssh密钥放在EB上的正确位置是在/root/.ssh而不是 /tmp/.ssh而不是 /home/ec2-user/.ssh

我最后的configuration(假设有一个私人的SSH密钥位于<my-bucket>/github-eb-key的S3存储区中,相应的公共密钥是通过有权访问repo的github用户注册的)configuration为64bit Amazon Linux 2016.09 v3.3.0 running Node.js的AMI以及.ebextensions/01_ssh_setup.config的以下.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 
Interesting Posts