使用 Key Vault 中的密钥连接到 Cosmos
Connect to Cosmos using key from Key Vault
我有一个 Spring 启动应用程序需要使用 CosmosDB。我的目标是从 Key Vault 加载 CosmosDB 连接密钥并使用它连接到 CosmosDB。
我已将密钥作为秘密存放在 Key Vault 中,但似乎存在排序问题,因为 Cosmos bean 是在 Key Vault 之前创建的。我能够成功连接到 Key Vault 并且在此之前已经收到了几个密钥,如果我对连接密钥进行硬编码,我也能够连接到 Cosmos。
是否可以从 Key Vault 加载密钥并使用它来创建 Cosmos bean?
我尝试了以下方法,但我收到 Cosmos 的连接错误(由于未设置密钥)- 可能是因为它在 Key Vault 之前加载。是否有可靠的方法连接到 Cosmos 或任何适用于 Spring 引导的适当示例?
我正在使用的依赖项:
azure-cosmosdb-spring-boot-starter (from com.microsoft.azure)
azure-identity (from com.azure)
azure-security-keyvault-secrets (from com.azure)
CosmosConfiguration.java class:
@Slf4j
@Configuration
@Profile("!local")
public class CosmosConfiguration extends AbstractCosmosConfiguration {
@Value("${cosmosPrimaryKey}")
private String key;
@Override
public CosmosClient cosmosClient(CosmosDBConfig config) {
return CosmosClient
.builder()
.endpoint(config.getUri())
.cosmosKeyCredential(new CosmosKeyCredential(key))
.consistencyLevel(consistencyLevel.STRONG)
.build()
}
}
application.properties(仅相关部分):
azure.keyvault.enabled=true
azure.keyvault.uri=https://mykeyvault.azure.net
azure.keyvault.secrets-keys=cosmosPrimaryKey
cosmosdb.keyname=cosmosPrimaryKey
azure.cosmosdb.uri=https://mycosmos.documents.azure.com:443
azure.cosmodb.repositories.enabled=true
spring.main.allow-bean-definition-overriding=true
我对你的情况的想法是在创建 'CosmosClient' 时添加判断。这是我的代码。
@Autowired
private CosmosProperties properties;
public CosmosClientBuilder cosmosClientBuilder() {
DirectConnectionConfig directConnectionConfig = DirectConnectionConfig.getDefaultConfig();
String uri = properties.getUri();
if(true) {
String temp = getConnectUriFromKeyvault();
properties.setUri(temp);
}
return new CosmosClientBuilder()
.endpoint(properties.getUri())
.key(properties.getKey())
.directMode(directConnectionConfig);
}
public String getConnectUriFromKeyvault() {
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl("https://vauxxxxen.vault.azure.net/")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
KeyVaultSecret secret = secretClient.getSecret("cosmosdbScanWithwrongkey");
return secret.getValue();
}
CosmosProperties 实体:
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "cosmos")
public class CosmosProperties {
private String uri;
private String key;
private String secondaryKey;
private boolean queryMetricsEnabled;
//get set function
//...
}
application.properties:
cosmos.uri=https://txxxb.documents.azure.com:443/
cosmos.key=gdvBggxxxxxWA==
cosmos.secondaryKey=wDcxxxfinXg==
dynamic.collection.name=spel-property-collection
# Populate query metrics
cosmos.queryMetricsEnabled=true
我按照这个 doc 获取密钥保管库机密。
我有一个 Spring 启动应用程序需要使用 CosmosDB。我的目标是从 Key Vault 加载 CosmosDB 连接密钥并使用它连接到 CosmosDB。 我已将密钥作为秘密存放在 Key Vault 中,但似乎存在排序问题,因为 Cosmos bean 是在 Key Vault 之前创建的。我能够成功连接到 Key Vault 并且在此之前已经收到了几个密钥,如果我对连接密钥进行硬编码,我也能够连接到 Cosmos。 是否可以从 Key Vault 加载密钥并使用它来创建 Cosmos bean?
我尝试了以下方法,但我收到 Cosmos 的连接错误(由于未设置密钥)- 可能是因为它在 Key Vault 之前加载。是否有可靠的方法连接到 Cosmos 或任何适用于 Spring 引导的适当示例?
我正在使用的依赖项:
azure-cosmosdb-spring-boot-starter (from com.microsoft.azure)
azure-identity (from com.azure)
azure-security-keyvault-secrets (from com.azure)
CosmosConfiguration.java class:
@Slf4j
@Configuration
@Profile("!local")
public class CosmosConfiguration extends AbstractCosmosConfiguration {
@Value("${cosmosPrimaryKey}")
private String key;
@Override
public CosmosClient cosmosClient(CosmosDBConfig config) {
return CosmosClient
.builder()
.endpoint(config.getUri())
.cosmosKeyCredential(new CosmosKeyCredential(key))
.consistencyLevel(consistencyLevel.STRONG)
.build()
}
}
application.properties(仅相关部分):
azure.keyvault.enabled=true
azure.keyvault.uri=https://mykeyvault.azure.net
azure.keyvault.secrets-keys=cosmosPrimaryKey
cosmosdb.keyname=cosmosPrimaryKey
azure.cosmosdb.uri=https://mycosmos.documents.azure.com:443
azure.cosmodb.repositories.enabled=true
spring.main.allow-bean-definition-overriding=true
我对你的情况的想法是在创建 'CosmosClient' 时添加判断。这是我的代码。
@Autowired
private CosmosProperties properties;
public CosmosClientBuilder cosmosClientBuilder() {
DirectConnectionConfig directConnectionConfig = DirectConnectionConfig.getDefaultConfig();
String uri = properties.getUri();
if(true) {
String temp = getConnectUriFromKeyvault();
properties.setUri(temp);
}
return new CosmosClientBuilder()
.endpoint(properties.getUri())
.key(properties.getKey())
.directMode(directConnectionConfig);
}
public String getConnectUriFromKeyvault() {
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl("https://vauxxxxen.vault.azure.net/")
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
KeyVaultSecret secret = secretClient.getSecret("cosmosdbScanWithwrongkey");
return secret.getValue();
}
CosmosProperties 实体:
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "cosmos")
public class CosmosProperties {
private String uri;
private String key;
private String secondaryKey;
private boolean queryMetricsEnabled;
//get set function
//...
}
application.properties:
cosmos.uri=https://txxxb.documents.azure.com:443/
cosmos.key=gdvBggxxxxxWA==
cosmos.secondaryKey=wDcxxxfinXg==
dynamic.collection.name=spel-property-collection
# Populate query metrics
cosmos.queryMetricsEnabled=true
我按照这个 doc 获取密钥保管库机密。