DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Trending

  • Detection and Mitigation of Lateral Movement in Cloud Networks
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Integrating Vault with Spring Cloud Config Server

Integrating Vault with Spring Cloud Config Server

This guide walks through the process of creating a central configuration management for microservices using Spring Cloud Config integrating with HashiCorp Vault.

By 
Marcos Barbero user avatar
Marcos Barbero
DZone Core CORE ·
Jun. 20, 18 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
20.1K Views

Join the DZone community and get the full member experience.

Join For Free

This guide walks through the process of creating a central configuration management for microservices using Spring Cloud Config integrating with HashiCorp Vault.

Introduction

Nowadays, software is commonly delivered as a service and doesn't matter the programming language that was chosen, it's always good to follow the twelve-factor app methodology.

The first factor is about the codebase, it starts saying:

One codebase tracked in revision control, many deploys

It means that the same codebase needs to be deployed in multiple environments without any change other than the configuration, which brings us to the externalized configuration.

If you are working (or going to work) with microservices in an elastic environment, you probably noticed the need for a central place for configuration management, and that's also one of the twelve-factors.

Prerequisite

  • JDK 1.8
  • Text editor or your favorite IDE
  • Maven 3.0+
  • Docker

Spring Cloud Configuration

Spring Cloud Config provides server and client-side support for externalized configuration in a distributed system and it has quite few backend storage support, this guide covers the following:

  • File System
  • Git
  • Vault

Setting the Configuration Server

The easiest way to set up a Config Server is reaching start.spring.io, search for Config Server in the dependencies search box and hit the "Generate Project" button, which will generate a zip file containing the Config Server project with all the necessary dependencies. Unzip the project, open the main class, and add the @EnableConfigServer on the class level.

You need to end up with something like this:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

The generated project has an application.properties file placed at src/main/resources, for personal preferences, I renamed it to application.yml.

Open the application.yml file and add the following configuration. As I'm running all the applications in the same host, it's important to change the server.port.

server:
  port: 8888

spring:
  application:
    name: configserver

Profiles and Auto of the Box Implementations

Spring Cloud Config Server uses profiles to provide multiple auto of the box backend storages implementations. Let's walk through some of them.

File System Backend

There's a native profile available where the "Config Server" searches for the properties/YAML files from the local classpath or file system.

spring:
  profiles:
    active: native
The default value of the searchLocations is identical to a local Spring Boot application ( [classpath:/, classpath:/config, file:./, file:./config]). This does not expose the application.properties from the server to all clients because any property sources present in the server are removed before being sent to the client.

You can point to any location using spring.cloud.config.server.native.searchLocations.

Remember to use the file: prefix for file resources (the default without a prefix is usually the classpath). As with any Spring Boot configuration, you can embed ${}-style environment placeholders, but remember that absolute paths in Windows require an extra / (for example, file:///${user.home}/config-repo ).

For this guide, I'll create a folder named config-repo in the ${user.home}. This new folder is created to store the configuration files for the native profile. Now add the following configuration to application.yml.

spring:
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo

Now create the file config-repo/configclient.yml. This file stores the configuration for our microservice (created later in this guide) named configclient, and add the following configuration.

client:
  pseudo:
    property: Property value loaded from File System

Start the application and hit the endpoint http://localhost:8888/configclient/default. The response will be:

{  
   "name":"configclient",
   "profiles":[  
      "default"
   ],
   "label":null,
   "version":null,
   "state":null,
   "propertySources":[  
      {  
         "name":"file:///Users/my-user/config-repo/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from File System"
         }
      }
   ]
}
The native profile is a good way to start with Config Server on the local environment, but I don't recommend anyone to go to production using a file system based storage.

Git Backend

There's also a git profile where you can point to an external git repository that contains all the configurations files for your microservices. To make it work, you can either add the git profile to the active profiles or totally remove the spring.profiles.active property. If you choose to keep this property, the last specified profile has priority and overrides all the previously defined.

Add the following config:

spring:
  profiles:
    active: native, git
  cloud:
    config:
      server:
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault

Here's the full configuration for reference:

spring:
  profiles:
    active: native, git
  application:
    name: configserver
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault

Now, if you hit the http://localhost:8888/configclient/default again, there will be the following result:

{  
   "name":"configclient",
   "profiles":[  
      "default"
   ],
   "label":null,
   "version":null,
   "state":null,
   "propertySources":[  
      {  
         "name":"file:///Users/my-user/config-repo/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from File System"
         }
      },
      {  
         "name":"https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault/configclient.yml",
         "source":{  
            "client.pseudo.property":"Property value loaded from Git"
         }
      }
   ]
}
If no profile is set, git is the default one.

Vault Backend

Vault is a tool for securely accessing secrets. A secret is anything with which you want to tightly control access, such as API keys, passwords, certificates, and other sensitive information. Vault provides a unified interface to any secret while providing tight access control and recording a detailed audit log.

There's also a vault profile that enables integration with Vault to securely store the application properties. In this part, we'll be using docker to create a Vault container.

Create a vault container:

docker run -d -p 8200:8200 --name vault -e 'VAULT_DEV_ROOT_TOKEN_ID=myroot' -e 'VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200' vault
It's specified the root token here. DO NOT do that in production.

Enter in the vault container and authenticate to vault:

docker exec -i -t vault sh
export VAULT_ADDR='http://localhost:8200'
vault auth myroot

Now it's time to write our secret property:

vault write secret/configclient client.pseudo.property="Property value loaded from Vault"

Now that we have everything in place, it's time to configure our Config Server to talk to Vault and add the following properties:

spring:
  profiles:
    active: vault
  cloud:
    config:
      server:
        vault:
          port: 8200
          host: 127.0.01

The complete configuration for reference:

spring:
  profiles:
    active: native, git, vault
  application:
    name: configserver
  cloud:
    config:
      server:
        native:
          searchLocations: file://${user.home}/config-repo
        git:
          uri: https://github.com/weekly-drafts/config-repo-spring-cloud-configserver-vault
        vault:
          port: 8200
          host: 127.0.01
You can also secure properties without vault using encryption.

Setting the Config Client

As before, the easiest way to create the config client is reaching start.spring.io and adding Config Client and Web dependencies.

Open the generated project and add a file named resources/bootstrap.yml with the following content:

spring:
  application:
    name: configclient
  cloud:
    config:
      token: myroot
      uri: http://localhost:8888

It's important to add exactly configclient as spring.application.name because that's the name used to bind to the external configuration.

Now open the main class and add a @Value to recover our pseudo property and return it to an endpoint, here's my main class for reference.

@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${client.pseudo.property}")
    private String pseudoProperty;

    @GetMapping("/property")
    public ResponseEntity<String> getProperty() {
        return ResponseEntity.ok(pseudoProperty);
    }
}

After starting the project, you'll be able to reach http://localhost:8080/property and the expected result is:

Property value loaded from Vault

Summary

Congratulations! You just created a central configuration management using Spring Cloud Config and secured all your secrets with HashiCorp Vault.

Footnote

  • The code used for this tutorial can be found on GitHub
  • Spring Cloud Config Docs
Spring Cloud Spring Framework File system

Published at DZone with permission of Marcos Barbero, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Component Tests for Spring Cloud Microservices
  • A Robust Distributed Payment Network With Enchanted Audit Functionality - Part 2: Spring Boot, Axon, and Implementation
  • 7 Microservices Best Practices for Developers

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!