Spring Cloud configuration server using SSH key for Git and runs in Docker

I found a lot of questions and guides before finally putting it all together. I wanted to document it so that someone else could save many hours of frustration.

I am trying to get a private git repository on BitBucket to work with Spring Boot Config Server using deployment keys and run it in Docker. I have a lot of problems.

  • How to configure using application.yml files.

I cannot figure out where I should put the SSH information. All tutorials seem to be for https.

  1. How to provide a private key for configuration. For Dev, inline syntax in YML is a pain. For production, you must provide it through an environment variable, which is another syntax chorus.

I keep getting an error when the private key is invalid.

  1. How to make the Docker container trust the host key without this annoying "trust this guy" invitation.

There seem to be several ways to make this work, but only the one that worked for me.

+7
source share
3 answers

The first part is the configuration. You want to ignore the standard private key and use the one that is provided as an environment variable. (SSH_KEY). In addition, the git repository is EV (GIT_URL), but you can hard code it if you want.

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

2 . Dev , YAML. ( , , , , )

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-----

bash , , Docker, . :

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

, . , , - ssh. Docker. "bitbucket.org" , . ssh, , .

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

, , , SSH- YAML ( env), .

SSH, , , ( , , ): AWS Cloudformation, Kubernetes ConfigMaps ).

( ) ( ) Spring Config.

~/.ssh/config :

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

( GitHub Enterprise, , SSH, , : ; , github.com HostName, User)

URI GitHub, - :

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

git-config :

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

, . 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

, " "...

+2

, № 1 Google ( SO) SSH Git-, - , . , , .

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

, , . , . SSH spring.cloud.config.server.git.sshLocation , ( env, bootstrap.yml ..)

Good coding!

0
source

Source: https://habr.com/ru/post/1688428/


All Articles