in components-starter/camel-azure-key-vault-starter/src/main/java/org/apache/camel/component/azure/key/vault/springboot/SpringBootAzureKeyVaultPropertiesParser.java [44:115]
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
SecretClient client;
ConfigurableEnvironment environment = event.getEnvironment();
if (Boolean.parseBoolean(environment.getProperty("camel.component.azure-key-vault.early-resolve-properties"))) {
String vaultName = environment.getProperty("camel.vault.azure.vaultName");
String clientId = environment.getProperty("camel.vault.azure.clientId");
String clientSecret = environment.getProperty("camel.vault.azure.clientSecret");
String tenantId = environment.getProperty("camel.vault.azure.tenantId");
boolean azureIdentityEnabled = Boolean.parseBoolean(System.getenv("camel.vault.azure.azureIdentityEnabled"));
if (ObjectHelper.isNotEmpty(vaultName) && ObjectHelper.isNotEmpty(clientId) && ObjectHelper.isNotEmpty(clientSecret)
&& ObjectHelper.isNotEmpty(tenantId) && !azureIdentityEnabled) {
String keyVaultUri = "https://" + vaultName + ".vault.azure.net";
// Credential
ClientSecretCredential credential = new ClientSecretCredentialBuilder()
.tenantId(tenantId)
.clientId(clientId)
.clientSecret(clientSecret)
.build();
// Build Client
client = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(credential)
.buildClient();
} else if (ObjectHelper.isNotEmpty(vaultName) && azureIdentityEnabled) {
String keyVaultUri = "https://" + vaultName + ".vault.azure.net";
// Credential
TokenCredential credential = new DefaultAzureCredentialBuilder().build();
// Build Client
client = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(credential)
.buildClient();
} else {
throw new RuntimeCamelException(
"Using the Azure Key Vault Properties Function requires setting Azure credentials as application properties or environment variables or enable the Azure Identity Authentication mechanism");
}
KeyVaultPropertiesFunction keyVaultPropertiesFunction = new KeyVaultPropertiesFunction(client);
final Properties props = new Properties();
for (PropertySource mutablePropertySources : event.getEnvironment().getPropertySources()) {
if (mutablePropertySources instanceof MapPropertySource mapPropertySource) {
mapPropertySource.getSource().forEach((key, value) -> {
String stringValue = null;
if ((value instanceof OriginTrackedValue originTrackedValue &&
originTrackedValue.getValue() instanceof String v)) {
stringValue = v;
} else if (value instanceof String v) {
stringValue = v;
}
if (stringValue != null &&
stringValue.startsWith("{{azure:") &&
stringValue.endsWith("}}")) {
LOG.debug("decrypting and overriding property {}", key);
try {
String element = keyVaultPropertiesFunction.apply(stringValue
.replace("{{azure:", "")
.replace("}}", ""));
props.put(key, element);
} catch (Exception e) {
// Log and do nothing
LOG.debug("failed to parse property {}. This exception is ignored.", key, e);
}
}
});
}
}
environment.getPropertySources().addFirst(new PropertiesPropertySource("overridden-camel-azure-key-vault-properties", props));
}
}