Spring cloud &引用;“机密id丢失”;使用Spring cloud Vault连接到Vault时出错
我正在尝试使用基于角色的身份验证(SpringBoot项目)连接到SpringVault 根据文档,我应该只能使用approle(拉模式)连接到SpringVault。但是,我在应用程序启动时发现secrect id缺少异常 当我通过secret id时,我也能够连接,并且属性/值得到正确的自动连接 是否有任何方法可以使用“令牌+角色/角色id”和spring在运行时使用上述信息自动为我生成机密id来连接vaultSpring cloud &引用;“机密id丢失”;使用Spring cloud Vault连接到Vault时出错,spring-cloud,hashicorp-vault,spring-vault,spring-cloud-vault-config,Spring Cloud,Hashicorp Vault,Spring Vault,Spring Cloud Vault Config,我正在尝试使用基于角色的身份验证(SpringBoot项目)连接到SpringVault 根据文档,我应该只能使用approle(拉模式)连接到SpringVault。但是,我在应用程序启动时发现secrect id缺少异常 当我通过secret id时,我也能够连接,并且属性/值得到正确的自动连接 是否有任何方法可以使用“令牌+角色/角色id”和spring在运行时使用上述信息自动为我生成机密id来连接vault spring.cloud.vault: scheme: http
spring.cloud.vault:
scheme: http
host: <host url>
port: 80
token : <token>
generic.application-name: vault/abc/pqr/test
generic.backend: <some value>
generic.default-context: vault/abc/pqr/test
token: <security token>
authentication: approle
app-role:
role-id: <role-id>
spring.cloud.vault:
方案:http
主持人:
港口:80
代币:
generic.application-name:vault/abc/pqr/test
generic.backend:
generic.default-context:vault/abc/pqr/test
代币:
认证:批准
应用程序角色:
角色id:
POM:
org.springframework.cloud
SpringCloudVault启动程序配置
1.0.0.1构建快照
如果需要其他信息,请告诉我
更新
@mp911de,我按照您的建议进行了尝试,但是spring cloud vault正在选择bootstrap.yml中设置的属性,而“onApplicationEvent”中没有一个属性集,因此解决方案不起作用。我尝试通过“System.setProperty”方法设置属性,但该事件不起作用
然而,若我在main-before-run方法中设置属性,那个么它将按预期工作。但我需要先加载application.properties(需要从中选择一些配置),因此不想在那里编写逻辑
我的方法有什么问题吗
@Component public class LoadVaultProperties implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
private RestTemplate restTemplate = new RestTemplate();
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
try {
String roleId = getRoleIdForRole(event); //helper method
String secretId = getSecretIdForRoleId(event); //helper method
Properties properties = new Properties();
properties.put("spring.cloud.vault.app-role.secret-id", secretId);
properties.put("spring.cloud.vault.app-role.role-id", roleId);
event.getEnvironment().getPropertySources().addFirst(new PropertiesPropertySource(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME, properties));
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
}
@组件公共类LoadVault属性实现ApplicationListener{
私有RestTemplate RestTemplate=新RestTemplate();
@凌驾
ApplicationEvent上的公共无效(ApplicationEnvironmentPreparedEvent事件){
试一试{
字符串roleId=getRoleIdForRole(事件);//helper方法
字符串secretId=getsecretidforoleid(事件);//helper方法
属性=新属性();
properties.put(“spring.cloud.vault.app role.secret id”,secretId);
properties.put(“spring.cloud.vault.app role.role id”,roleId);
event.getEnvironment().getPropertySources().addFirst(新属性PropertySource(
PropertySourceBootstrapConfiguration.BOOTSTRAP_PROPERTY_SOURCE_NAME,properties));
}捕获(例外情况除外){
抛出新的非法状态异常(ex);
}
}
Spring Vault的AppRole身份验证支持两种模式,但不支持拉模式:
secret\u id
角色id
进行身份验证而无需机密id
。此模式要求通过在角色创建时设置bind\u secret\u id=false
来创建角色,而无需机密id
机密id
。Spring Vault不会获取包装的机密id
,但我认为这是一个不错的增强
更新:在应用程序启动之前设置系统属性:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
System.setProperty("spring.cloud.vault.app-role.role-id", "…");
System.setProperty("spring.cloud.vault.app-role.secret-id", "…");
SpringApplication.run(MyApplication.class, args);
}
参考资料:
secret\u id
角色id
进行身份验证而无需机密id
。此模式要求通过在角色创建时设置bind\u secret\u id=false
来创建角色,而无需机密id
机密id
。Spring Vault不会获取包装的机密id
,但我认为这是一个不错的增强
更新:在应用程序启动之前设置系统属性:
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
System.setProperty("spring.cloud.vault.app-role.role-id", "…");
System.setProperty("spring.cloud.vault.app-role.secret-id", "…");
SpringApplication.run(MyApplication.class, args);
}
参考资料:
SpringApplication
之前,Spring boot会自动拾取该id,而不会丢失secrect id。您可以将其存储为系统属性或创建属性源并在调用run(…)
之前,请在SpringApplication
中注册它。我尝试了您建议的解决方案,但该方法也面临问题。我已更新了我的问题,并提供了更多详细信息。感谢onApplicationEvent(…)
为时已晚,因为此时所有属性都已绑定。在我们的示例中,令牌和角色将驻留在spring cloud config server中(现在它位于本地,但这是计划),因此,在加载上述配置之前无法获取机密id。感谢@mp911de的回复。在这种情况下,我需要在什么时候获取代码中的机密id,并将其设置为应用程序属性,以便在启动SpringApplication
之前,Spring boot自动获取该id,而不会丢失secrect id在调用run(…)
之前,您可以将其存储为系统属性或创建属性源,并将其注册到SpringApplication
。我尝试了您建议的解决方案,但也面临该方法的问题。我已更新了我的问题,并提供了更多详细信息。感谢onApplicationEvent(…)
为时已晚,因为此时所有属性都已绑定。在我们的示例中,令牌和角色将驻留在spring cloud config server中(现在它位于本地,但这是计划),因此在加载所述配置之前无法获取机密id。