Move KeyVaultPropertyInitializer to KeyVaultPropertyEnvironmentPostProcessor (#164)

See gh-162
This commit is contained in:
Eddú Meléndez Gonzales 2017-09-07 00:54:06 -05:00 коммит произвёл ZhijunZhao
Родитель b57b6da96f
Коммит 241554f40a
4 изменённых файлов: 69 добавлений и 71 удалений

Просмотреть файл

@ -0,0 +1,67 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for
* license information.
*/
package com.microsoft.azure.keyvault.spring.boot;
import com.microsoft.azure.keyvault.KeyVaultClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.util.Assert;
public class KeyVaultPropertyEnvironmentPostProcessor implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
if (environment.getProperty(Constants.AZURE_CLIENTID) == null) {
// User doesn't want to enable Key Vault property initializer.
return;
}
final String clientId = getProperty(environment, Constants.AZURE_CLIENTID);
final String clientKey = getProperty(environment, Constants.AZURE_CLIENTKEY);
final String vaultUri = getProperty(environment, Constants.AZURE_KEYVAULT_VAULT_URI);
boolean enabled = true;
if (environment.getProperty(Constants.AZURE_KEYVAULT_ENABLED) != null) {
enabled = Boolean.parseBoolean(environment.getProperty(Constants.AZURE_KEYVAULT_ENABLED));
}
long timeAcquiringTimeoutInSeconds = 60;
if (environment.getProperty(Constants.AZURE_TOKEN_ACQUIRE_TIMEOUT_IN_SECONDS) != null) {
timeAcquiringTimeoutInSeconds = Long.parseLong(
environment.getProperty(Constants.AZURE_TOKEN_ACQUIRE_TIMEOUT_IN_SECONDS));
}
if (enabled) {
final KeyVaultClient kvClient = new KeyVaultClient(
new AzureKeyVaultCredential(clientId, clientKey, timeAcquiringTimeoutInSeconds));
try {
final MutablePropertySources sources = environment.getPropertySources();
sources.addFirst(new KeyVaultPropertySource(new KeyVaultOperation(kvClient, vaultUri)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String getProperty(ConfigurableEnvironment env, String propertyName) {
Assert.notNull(env, "env must not be null!");
Assert.notNull(propertyName, "propertyName must not be null!");
final String property = env.getProperty(propertyName);
if (property == null || property.isEmpty()) {
throw new IllegalArgumentException("property " + propertyName + " must not be null!");
}
return property;
}
}

Просмотреть файл

@ -1,69 +0,0 @@
/**
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for
* license information.
*/
package com.microsoft.azure.keyvault.spring.boot;
import com.microsoft.azure.keyvault.KeyVaultClient;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.MutablePropertySources;
import org.springframework.util.Assert;
public class KeyVaultPropertyInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext ctx) {
final ConfigurableEnvironment env = ctx.getEnvironment();
if (env.getProperty(Constants.AZURE_CLIENTID) == null) {
// User doesn't want to enable Key Vault property initializer.
return;
}
final String clientId = getProperty(env, Constants.AZURE_CLIENTID);
final String clientKey = getProperty(env, Constants.AZURE_CLIENTKEY);
final String vaultUri = getProperty(env, Constants.AZURE_KEYVAULT_VAULT_URI);
boolean enabled = true;
if (env.getProperty(Constants.AZURE_KEYVAULT_ENABLED) != null) {
enabled = Boolean.parseBoolean(env.getProperty(Constants.AZURE_KEYVAULT_ENABLED));
}
long timeAcquringTimeoutInSeconds = 60;
if (env.getProperty(Constants.AZURE_TOKEN_ACQUIRE_TIMEOUT_IN_SECONDS) != null) {
timeAcquringTimeoutInSeconds = Long.parseLong(
env.getProperty(Constants.AZURE_TOKEN_ACQUIRE_TIMEOUT_IN_SECONDS));
}
if (enabled) {
final KeyVaultClient kvClient = new KeyVaultClient(
new AzureKeyVaultCredential(clientId, clientKey, timeAcquringTimeoutInSeconds));
try {
final MutablePropertySources sources = env.getPropertySources();
sources.addFirst(new KeyVaultPropertySource(new KeyVaultOperation(kvClient, vaultUri)));
} catch (Exception e) {
e.printStackTrace();
}
}
}
private String getProperty(ConfigurableEnvironment env, String propertyName) {
Assert.notNull(env, "env must not be null!");
Assert.notNull(propertyName, "propertyName must not be null!");
final String property = env.getProperty(propertyName);
if (property == null || property.isEmpty()) {
throw new IllegalArgumentException("property " + propertyName + " must not be null!");
}
return property;
}
}

Просмотреть файл

@ -1 +1,2 @@
org.springframework.context.ApplicationContextInitializer=com.microsoft.azure.keyvault.spring.boot.KeyVaultPropertyInitializer
org.springframework.boot.env.EnvironmentPostProcessor=\
com.microsoft.azure.keyvault.spring.boot.KeyVaultPropertyEnvironmentPostProcessor

Просмотреть файл

@ -20,7 +20,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.assertFalse;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(initializers = KeyVaultPropertyInitializer.class)
@TestPropertySource(locations = "classpath:application.properties")
public class InitializerTest {