Managing secrets on your development machine can be a challenge. This is evident in the high number of secrets that are regularly found publicly available in GitHub. I recently found that installing a containerized version of Spring Cloud Config Server on my workstation has made managing those secrets without including them in my source control alongside the code a little easier.
Spring Cloud Config Server is designed to store application configuration for distributed systems in a centralized location. For local development this means the configuration is not part of your application. This is good as it means your config is not checked in next to your code and especially good if your code repo is publicly available.
The easiest way to get Config Server running on your local machine for development is to use a Docker Image of Config Server and Docker Toolbox to run the docker image. While there are a number of ways you can get this running, I put together what I believe are the most straight forward steps to get you up and running.
If you are developing .NET applications like me, you will want to utilize SteelToe Configuration to interface with Config Server. SteelToe Configuration will merge the Config Server settings into your IConfiguration implementation.
Install Docker Toolbox https://docs.docker.com/toolbox/toolbox_install_windows/
Install Docker Compose https://docs.docker.com/compose/
Create folders for configuration
c:\data\configserver
c:\data\configserver\config
In virtual box manager there will be a VM labeled 'default'. Go into settings and add a shared folder for c:\data
with folder name of data
.
volumes:
- /data/configserver/config:/config
/data/
= the vm shared folder label in statement above
Create docker compose in configserver folder
c:\data\configserver
docker-compose.yml
version: "3.3"
services:
configserver:
image: hyness/spring-cloud-config-server
ports:
- "8888:8888"
volumes:
- /data/configserver/config:/config
environment:
- SPRING_PROFILES_ACTIVE=native
Create example config file in the config folder. The file name will determine the app and environment the config file will be served up by. In this example the 'demo' app and 'development' environment.
---
Features:
Wowfeature: true
Launch the docker Quickstart Terminal (shortcut on desktop)
192.168.99.100
In the Terminal window change directory to c:\data\configserver
cd c:/data/configserver
Load the container with docker compose
docker-compose up
Assuming your Docker IP address is the same go to the following URL
{"name":"demo","profiles":["development"],"label":null,"version":null,"state":null,"propertySources":[{"name":"file:config/demo-development.yml","source":{"Features.Wowfeature":true}}]}
Once you have this set up, you can load up the docker container with docker-compose up -d
which will run in disconnected mode in the background. Any changes you make to the config files will automatically get picked up by config server
To configure your .NET Core project to use Config server
Add Nuget Steeltoe.Extensions.Configuration.ConfigServerCore
Register the configuration
configApp.AddJsonFile("appsettings.json", optional: true);
configApp.AddJsonFile(
$"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json",
optional: true);
configApp.AddConfigServer(new ConfigServerClientSettings() { Environment = environmentName, TokenRenewRate = 3000 });
In your appsettings.development.json
config file add the config below:
"spring": {
"application": {
"name": "demo"
},
"cloud": {
"config": {
"uri": "http://192.168.99.100:8888",
"validate_certifates": false
}
}
}
I use the appsettings.secret.json
to store the config server location and add appsettings.secret.json
to .gitignore
so that it does not end up in my repo.
Please, leave a comment below if you found this helpful or you have questions about any of the steps.