Spring cloud config and Git SSH configuration

In this post, we are going to configure spring cloud config server using SSH configuration with git.

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system.

Spring Cloud Config Server pulls configuration for remote clients from various sources (git repository, any JDBC compatible database, Subversion, Hashicorp Vault, Credhub, and local filesystems). In this post, we will be using a private Git repository hosted on GitLab.

Spring Cloud Config Server

We will start by creating a simple Spring Boot project from start.spring.io with spring-cloud-config-server dependency.

pom.xml file should look like below:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.demo.cloudserver</groupId>
<artifactId>config-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>config-service</name>
<description>Spring cloud config services</description>

<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR9</spring-cloud.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>

And need to use @EnableConfigServer annotation.

@SpringBootApplication
@EnableConfigServer
public class ConfigServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServiceApplication.class, args);
    }

}

GitLab SSH configuration

SSH keys allow you to establish a secure connection between your computer and GitLab without supplying your username or password each time.

In this post, I used the PuTTY Key generator to generate a new SSH key pair.

  • Generate a public/private key pair
  • Add key passphrase
  • Export the private key under OpenSSH format (Conversions -> Export OpenSSH key) and copy the contents of the file.

Adding an SSH key to your GitLab account

  1. Navigate to https://gitlab.com or your local GitLab instance URL and sign in.
  2. Select your avatar in the upper right corner, and click Settings
  3. Click SSH Keys.
  4. Paste the public key that you copied into the Key text box, includes a descriptive name in the Title text box and an (optional) expiry date for the key under “Expires at” section.

See the full documentation on GitLab. Bitbucket and Github also have an SSH key process.

Now add configuration parameters in the application.yml file:

  • ignore-local-ssh-settings: true. If true, use property-based instead of file-based SSH config. Must be set at as spring.cloud.config.server.git.ignoreLocalSshSettingsnot inside a repository definition.
  • passphrase: Passphrase to unlocking your ssh private key
  • private-key: Valid SSH private key. Must be set if ignoreLocalSshSettings is true and Git URI is SSH format.

And we’re done, hope this story has helped you understand how to configure the Spring Cloud config server using SSH config with git.

Related Posts