Jed Arndt
Jed Arndt

Reputation: 21

How do I connect to Bitbucket using SSH from a spring-config-server application?

I have created a simple Spring cloud config server application and corresponding 'client' application. The config server reads config files from a git repo and the client app pulls them from the config server. It works with config files stored in a local git repo. I now want to connect to a remote Bitbucket server.

I have seen a few examples using username and password hardcoded, but I don't want to hardcode these. I'd like to use SSH keys.

I have found this example of using ssh keys in the docs.

I have a public key added to my repo in Bitbucket and i have the following application.yml file:

server:
  port: 8888
spring:
  cloud:
    config:
      server:
        git:
          uri: git@mybitbucketserver/ubp/config-server.git
          cloneOnStart: true
          ignoreLocalSshSettings: true
          hostKey: publicKeyLabel
          hostKeyAlgorithm: ssh-rsa
          privateKey: "-----BEGIN RSA PRIVATE KEY-----MIIEoAIBAAKmJyC-----END RSA PRIVATE KEY-----"

Where publicKeyLabel is the name of the label added to the repo on bitbucket. When I run this config server application i get the following error:

org.eclipse.jgit.api.errors.InvalidRemoteException: Invalid remote: origin

With a nested error of:

Caused by: org.eclipse.jgit.errors.NoRemoteRepositoryException: git@mybitbucketserver/ubp/config-server.git: not found.

I have double checked url. The public key attached to the repo has worked with the private key in my code in connecting from Openshift so i know it works (i have remove some of the key for this example).

Any suggestions of what i can try? Seen so few examples of people using SSH for this, everyone seems to hardcode the PW :|

If there is a better way of doing this I am all ears? My end goal would ideally be not to have the private key etc in code as well but I was hoping to get this working first then figure out where I can store the PK.

Upvotes: 1

Views: 2619

Answers (1)

mOchi
mOchi

Reputation: 166

Please check and see below. I hope these help you isolate and achieve your objectives of the setup 1 at a time:

  1. Let's make your current setup work first and establish connection between your config server and bitbucket before we move on to loading the SSH key externally (not hard coded).

    • privateKey is a multiline value. Hence, it has to be registered as multiline in YAML. As you saw in the example, it was using "|" which indicate a block-style indicator. You can also read more here for more details.
    • This shouldn't be an issue, but just in case, consider writing the git URI explicitly with SSH protocol such as: ssh://git@mybitbucketserver/ubp/config-server.git
    • If (and only if) after that change you come across another error e.g. "not a valid private key", you may refer on this later.
  2. Avoiding writing the privateKey in the YAML. These are some suggestions I can share:

    • 1 way is to register the key on the local SSH e.g. /root/.ssh/id_rsa and then set ignoreLocalSshSettings: false
    • Another way is something like this: privateKey: ${git.repository.key}. We'll have to define git.repository.key externally in options we can follow from here.

Upvotes: 3

Related Questions