Spring Boot Config Server with vault backend

ローカル環境でvaultサーバを起動

昨日、試したので、
tomotaka.hatenablog.com

Spring Boot Cloud Config Server

コードは、gitにアップ
github.com

すごく簡単です。application.ymlの設定にvaultを追加するだけ(gitは、無効に)

server:
  port: 8888
spring:
  cloud:
    config:
      server:
#       git:
#         uri: file:${HOME}/config-sample
        vault:
          host: 127.0.0.1
          port: 8200
          scheme: http
          backend: secret
          defaultKey: application
  profiles:
    active: vault

vaultに必要なデータを書いておきます。
以下のようにコマンドからwriteできます。

vault write secret/application foo=bar baz=bam
vault write secret/app-config foo=bar

vaultにデータを追加するのもhttpを使用してできます。
それもSpring Bootで試してみました。
github.com
これも、とても簡単にできました。(productionで使う場合は、さらに考慮が必要ですが)

@SpringBootApplication
public class SpringVaultSampleApplication {
    private static Logger logger = LoggerFactory.getLogger(SpringVaultSampleApplication.class);

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

    @Bean
    CommandLineRunner commandLineRunner(VaultTemplate vaultTemplate) {
        return x -> {
            vaultTemplate.write("secret/hello", new Hello("world"));
            VaultResponseSupport<Hello> hello = vaultTemplate.read("secret/hello", Hello.class);
            logger.info("vault value is [{}]", hello.getData().getVault());
            VaultResponse response = vaultTemplate.read("secret/hello");
            logger.info("vault json response is [{}]", response.getData());
        };
    }

    public static class Hello {
        String vault;

        public Hello(@JsonProperty("value") String value) {
            this.vault = value;
        }

        public String getVault() {
            return vault;
        }
    }

}

curlコマンドで、vaultの値を取得。ここでのyour-tokenは、サーバを起動した時にコンソールに表示される値を使用

curl -X "GET" "http://localhost:8888/app-config/default" -H "X-Config-Token: your-token"

出力される値に、applicationと、上記curlコマンドで指定したcpp-configの2つが取れるのは、application.ymlのdefaultKey: applicationと指定しているからのようです。
f:id:tomoTaka:20171224085742p:plain

helloのデータを取得する場合

curl -X "GET" "http://localhost:8888/hello/default" -H "X-Config-Token: your-token"

f:id:tomoTaka:20171224091136p:plain