简体   繁体   English

使用用于Git的SSH密钥并在Docker中运行的Spring Cloud Config Server

[英]Spring Cloud Config Server using SSH key for Git and running in Docker

I found many questions and tutorials before finally putting this all together. 在最终将所有内容组合在一起之前,我发现了许多问题和教程。 Wanted to document it so somebody else can save many hours of frustration. 想要记录下来,这样其他人可以节省很多时间的挫败感。

I am trying to get a private git repository on BitBucket to work with Spring Boot Config Server using deploy keys and have it run in Docker. 我正在尝试在BitBucket上获得一个私有git存储库,以使用部署密钥与Spring Boot Config Server一起使用,并使其在Docker中运行。 I am running into many issues. 我遇到很多问题。

  1. How to actually configure using the application.yml files. 如何使用application.yml文件进行实际配置。

I cant seem to figure out where I should put the SSH info. 我似乎无法弄清楚应该在哪里放置SSH信息。 All tutorials seem to be for https. 所有教程似乎都是针对https的。

  1. How to provide the private key to the configuration. 如何为配置提供私钥。 For Dev the syntax for inline in YML is a pain. 对于开发人员来说,YML中内联的语法很麻烦。 For production, you have to provide it via an environment variable, which is another syntax chore. 对于生产,您必须通过环境变量来提供它,这是另一个语法琐事。

I keep getting an error that the private key is invalid. 我不断收到错误消息,指出私钥无效。

  1. How to get the Docker container to trust the host key without that pesky "do you trust this guy" prompt. 如何使Docker容器信任主机密钥,而不会显示讨厌的“您是否信任此人”提示。

There seems to be several ways to make this work, but only one that worked for me. 似乎有几种方法可以使这项工作奏效,但只有一种对我有用。

First piece is the configuration. 首先是配置。 You want to ignore the standard private key and use one provided as an environment variable. 您想忽略标准私钥,而使用提供的私钥作为环境变量。 (SSH_KEY). (SSH_KEY)。 Also, the git repo is an EV (GIT_URL) but you can hardcode if you want. 另外,git repo是EV(GIT_URL),但您可以根据需要进行硬编码。

spring:
  cloud:
    config:
      server:
        git:
          uri:  ${GIT_URL}
          ignore-local-ssh-settings: true
          private-key: ${SSH_KEY}

Part 2 is tricky. 第2部分比较棘手。 For Dev, you want the key inline, so you need to use a pipe to prefix the block in YAML. 对于Dev,您需要内联键,因此需要使用管道在YAML中为该块添加前缀。 (Note this key is throw away as in I just generated it and have now thrown it away) (请注意,此密钥已被丢弃,就像我刚刚生成它并已将其丢弃一样)

private-key: |
                    -----BEGIN RSA PRIVATE KEY-----
                    MIIEpAIBAAKCAQEAszmCR06LVHk/kNYV6LoYgEfHlK4rp75sCsRJ7rdAbWNED+yB
                    bneOm5gue0LGIhT7iTP9D7aN6bKVHv1SBconCA7Pa2NMA9epcMT5ecJc8ndpZOFn
                    iqM77jmMMPvj8EIC06w5oK5zoYwpGotYQFHllf8M+20HtW2fZdPYAYwLcVdmc5tI
                    vLoS+10qw5D3X9zrwk2Cbt37Iqnz1cHOQq+g7sxgVgt18aIKKeg0JslaGqSlWMoT
                    ICUMHj89E4BMHj8ND8otSXHL+VhN+ghd7w1MpckxLWBsNs1+G1FuiJEVAtRq/j+8
                    SOilxgifvI1LqpZ5kO01XFlmkcuN4NMT03qpcwIDAQABAoIBAB5oQGk2sz7mv1kk
                    aV0tzaBeDUd1cWSpUw1UljKRFrY4ZEDLYH5MfH57iE9TWehIZRC3KFU1JMikitZS
                    JktjK9IbKSfQFgKE4XOHh8gXqMteZRw/feCwpydYzic1ZUvK903QZ4qSbn3XGNYv
                    FA79lhUny50Qt4EZkzSkh35js0FMSR9VmyXENxN6IgXUZyoaNAATr44Vkd488BY2
                    7PvdOniemo8/8p4Ij0Aq9Q7rOtm77ZXjyFRX5mDTi2ndSllMEhVcWXHSii+ukbvF
                    117Ns+8M7VWroNfRzI+Ilm/Xz/ePOLlNoYcY0h5+QM9vMPTX9Cpl5WofgOMK1sKd
                    mSdI4ukCgYEA12kcu0aDyIrEPHcyaT9izSFply0Uon2QKS9EQn6cr83vaEGViamh
                    f5q1coYouGnsLfbgKolEMKsYtbmJvInPFDCdc2x0Fmc207Wp1OECsN+HwElEXkrs
                    uPDpGQgs5odjN5Grue9837920oG3UBBdVDAKly2dTOcvoWW+88seFSUCgYEA1P7f
                    p78HDMQ8zTy5+3Rd4+lmJjPsY618XxSQ80j8Elrhi/DyTMA0XGc5c3cKRPmSj+JD
                    GN34WQbw7JO2mKM7YJs+tkSBeTKce8F3cZQy1jy3LNHCtfXylOxmxOFKynV5h2b/
                    jno+pGdmAPK5yvnGASd2eujtzt+AL07XiD2LnLcCgYEAsFRz131WfP/SuShdlLf1
                    WbODKuQVIxojuwLdHo1kF6k805v0G/dGoxzycOgPRz41vj57q3Yn4qr8FC3n6PTq
                    FT3idUyPDpO41r67Ye469KxWBHo1Q/aTJqTWOs5tatvixOcyqoa3MrUZQCI8+4YZ
                    z8Nvt+b3/66zV6vhDtHzMx0CgYAvWW2M0+mUS/ecRHivzqGkrdkYewh87C8uz9qd
                    SsdGqU9kla63oy7Ar+3Unkz5ImYTeGAkIgw4dlOOtBOugPMNOdXKHRaPQ9IHrO2J
                    oUFf4OVzoDnhy4ge1SLPd6nxsgXPNPVwzfopABdr9Ima9sWusgAjuK5NA+ByI9vE
                    HLJxpwKBgQCTM938cdx457ag1hS6EaEKyqljS1/B8ozptB4cy3h0hzw0crNmW84/
                    1Lt9MJmeR4FrWitQkkVLZL3SrYzrP2i+uDd4wVVD5epvnGP/Bk6g05/eB9LgDRx/
                    EeBgS282jUBkXZ6WpzqHCcku3Avs3ajzsC1WaEYx0tCiBxSkiJlaLQ==
                    -----END RSA PRIVATE KEY-----

On the production front, you need to use a bash variable at the command prompt to store your key before you pass it to the Docker command that runs your container. 在生产方面,您需要在命令提示符处使用bash变量来存储密钥,然后再将其传递给运行容器的Docker命令。 Example: 例:

$ pem=$( cat path_to_key )
$ docker run -e "SSH_KEY=$pem" configserver

At this point you should have the application taken care of. 此时,您应该已经处理了该应用程序。 Now all you need is to get past the ssh host not trusted problem. 现在,您需要做的就是克服ssh主机不受信任的问题。 For this, add these lines in your Dockerfile. 为此,请将这些行添加到Dockerfile中。 Replace "bitbucket.org" with whatever host you want. 将“ bitbucket.org”替换为所需的任何主机。 These commands create the ssh config directory, fix the permissions, and then create and populate the knownhosts file. 这些命令创建ssh config目录,修复权限,然后创建并填充knownhosts文件。

RUN mkdir -p /root/.ssh
RUN chmod 700 /root/.ssh
RUN ssh-keyscan bitbucket.org > /root/.ssh/known_hosts

I wanted to add a further twist on this, that would hopefully remove the need to mess around with SSH keys in the YAML file (or in env variables), which is usually A Bad Idea. 我想对此进行进一步的修改,希望可以消除在YAML文件(或环境变量)中使用SSH密钥的麻烦,这通常是一个坏主意。

This revolves around the SSH Config file, so if the app does not have access to it, or it cannot be modified, this won't work (but I cannot think of any real-world situation in which this would apply, including Cloud deployments: either AWS Cloudformation templates, or Kubernetes ConfigMaps would provide useful workarounds). 这围绕SSH Config文件进行,因此,如果该应用程序无权访问它,或者无法对其进行修改,则将无法正常工作(但我无法想到这种情况适用的任何实际情况,包括Cloud部署:AWS Cloudformation模板或Kubernetes ConfigMaps将提供有用的解决方法。

The issue revolves (for the most part) around the (rather inexplicable) limitation of not being able to specify a private key file in the Spring Config application properties. 该问题(大部分情况)围绕(无法解释的)局限性,即不能在Spring Config应用程序属性中指定私钥文件。

In your ~/.ssh/config file, you can add the following: ~/.ssh/config文件中,可以添加以下内容:

Host git-config
    HostName github.myserver.example.com
    User someone
    IdentityFile /path/to/private_key

(I need to connect to a private GitHub Enterprise server and the user associated with the SSH key is not the same as the application server is being run under: this works just fine; if that's not the case, simply use github.com for the HostName , and omit the User ) (我需要连接到私有GitHub Enterprise服务器,并且与SSH密钥关联的用户与运行该应用程序服务器的用户不同:这很好用;如果不是这种情况,只需将github.com用于HostName ,并省略User

Then, instead of using the actual GitHub URI, something like: 然后,代替实际的GitHub URI,如下所示:

git@github.myserver.example.com:my-team/config-properties-demo.git

you replace git-config for the host: 您将git-config替换为主机:

spring:
  cloud:
    config:
      server:
        git:
          uri: git@git-config:my-team/config-properties-demo.git
          strictHostKeyChecking: false

It is indeed a bit cumbersome, but relatively easy to automate. 这确实有点麻烦,但相对容易实现自动化。 A much preferable option would be for Spring Config to add another option that points to the private key material: 一个更好的选择是让Spring Config添加另一个指向私钥材料的选项:

spring:
  cloud:
    config:
      server:
        git:
          uri: git@github.myserver.example.com:my-team/config-properties-demo.git
          user: someone
          private_key_file: /path/to/private_key
          strictHostKeyChecking: false

I guess this is one for the "enhancement requests" section... 我想这是“增强请求”部分的内容...

Pardon the necro, but this is the #1 result on Google (from SO) when searching for how to do SSH authentication with Git repos when the config server is deployed to an environment with an ephemeral file system - and I believe I have found a way to do just that. 请原谅死灵,但这是在将配置服务器部署到具有临时文件系统的环境中搜索如何使用Git存储库进行SSH身份验证时,在Google上排名第一的结果(来自SO)-我相信我已经找到了一个做到这一点的方法。 Below is a gist of what I am currently doing to make that happen for my client. 以下是我目前为客户实现此目标的要点。

https://gist.github.com/hanserya/43b00162741fa3022481301db60e8acd https://gist.github.com/hanserya/43b00162741fa3022481301db60e8acd

It is definitely an ugly duckling, but is functional and should serve as solid footing for anyone that needs it. 它绝对是丑小鸭,但功能强大,应该为需要它的任何人提供坚实的立足点。 With this implementation, you'll be able to mount a volume to a container running the config server. 通过此实现,您将能够将卷挂载到运行配置服务器的容器。 Then, just configure the environment to use the volume as the SSH directory with the spring.cloud.config.server.git.sshLocation configuration key via whatever medium works best for you (env variables, bootstrap.yml, etc...) 然后,只需通过最适合您的媒介(env变量,bootstrap.yml等),将环境配置为使用spring.cloud.config.server.git.sshLocation配置密钥将卷用作SSH目录。

Happy Coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Cloud 配置服务器使用 SSH 密钥访问 GIT 存储库提供身份验证是必需的,但尚未注册 CredentialsProvider - Spring cloud config server accessing GIT repository using SSH key giving Authentication is required but no CredentialsProvider has been registered Spring-cloud-config Git SSH - Spring-cloud-config Git SSH spring cloud config server git for local file - spring cloud config server git for local file Spring Cloud,配置服务器无法启动,如何为git配置uri - Spring cloud, config server can not start, how to config uri for git Spring Boot Cloud 配置、GitLab、Docker 和 SSH 密钥设置 - Spring Boot Cloud Configuration, GitLab, Docker and SSH Key Set Up 使用VSTS git作为config-repo的Spring Cloud配置 - Spring cloud config using VSTS git as config-repo 无法使用 spring-cloud-config-server 从本地 git 存储库读取配置 - Unable to read configurations from local git repository using spring-cloud-config-server 无法使用Spring Cloud Config从git服务器获取信息 - Can't get information from git server using spring cloud config 无法将 Spring Cloud Config Server 与远程 Git 集成 - Can't integrate Spring Cloud Config Server with remote Git 将 spring 云配置服务器连接到本地 git 存储库失败 - connecting spring cloud config server to local git repo failed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM