Skip to content

Instantly share code, notes, and snippets.

@mp911de
Created December 2, 2016 21:24
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mp911de/17f550ffecdc9e8f22061bfdf896bbb4 to your computer and use it in GitHub Desktop.
Save mp911de/17f550ffecdc9e8f22061bfdf896bbb4 to your computer and use it in GitHub Desktop.
Using Spring Cloud Vault Config to get a token for Spring Cloud Vault Consul Config
# Bootstrap Configuration: META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=example.infrastructure.VaultForConsulBootstrapConfiguration
/*
* Copyright 2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package example.infrastructure;
import java.util.Collections;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.cloud.vault.config.consul.VaultConsulProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.vault.core.VaultOperations;
import org.springframework.vault.core.env.VaultPropertySource;
/**
* @author Mark Paluch
*/
@Configuration
@AutoConfigureOrder(1)
public class VaultForConsulBootstrapConfiguration implements ApplicationContextAware,
InitializingBean {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void afterPropertiesSet() {
ConfigurableEnvironment ce = (ConfigurableEnvironment) applicationContext
.getEnvironment();
if (ce.getPropertySources().contains("consul-token")) {
return;
}
VaultOperations vaultOperations = applicationContext
.getBean(VaultOperations.class);
VaultConsulProperties consulProperties = applicationContext
.getBean(VaultConsulProperties.class);
VaultPropertySource vaultPropertySource = new VaultPropertySource(
vaultOperations, String.format("%s/creds/%s",
consulProperties.getBackend(), consulProperties.getRole()));
MapPropertySource mps = new MapPropertySource("consul-token",
Collections.singletonMap("spring.cloud.consul.token",
vaultPropertySource.getProperty("token")));
ce.getPropertySources().addFirst(mps);
}
}
@krisiye
Copy link

krisiye commented Mar 8, 2021

@mp911de - This issue is different in that you will only see this once you enable the vault secret backend for consul with spring-cloud/spring-cloud-vault#580. It's about making the consul token (from vault based on a Role) available for the consul data loader to use.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment