Also pass db-password secret to spring backend

Signed-off-by: Friedrich Greiner <greinerfriedrich@gmail.com>
This commit is contained in:
friedrich greiner 2021-03-14 16:13:24 +01:00
parent decd790f19
commit 0fa30766c2
3 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,30 @@
package com.company.project.configuration;
import java.io.IOException;
import java.nio.charset.Charset;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.util.StreamUtils;
/**
* Read property from docker secret file.
*/
public class DockerSecretsProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
Resource resource = new FileSystemResource("/run/secrets/db-password");
if (resource.exists() && System.getProperty("MYSQL_PASSWORD") == null) {
try {
String dbPassword = StreamUtils.copyToString(resource.getInputStream(), Charset.defaultCharset());
System.setProperty("MYSQL_PASSWORD", dbPassword);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}

View File

@ -0,0 +1 @@
org.springframework.boot.env.EnvironmentPostProcessor=com.company.project.configuration.DockerSecretsProcessor

View File

@ -3,6 +3,8 @@ services:
backend:
build: backend
restart: always
secrets:
- db-password
environment:
MYSQL_HOST: db
networks: