Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b9cc9e7
Cache both key and value for keyVault. (#637)
rujche May 12, 2020
d7b3694
Remove unnecessary locks. (#637)
rujche May 12, 2020
a2603a8
Just keep uniformed property name in propertyNames. (#637)
rujche May 12, 2020
0000471
Fix test failure in KeyVaultOperationUnitTest. (#637)
rujche May 12, 2020
871a5e6
Fix 2 bugs reported by findbugs-maven-plugin. (#637)
rujche May 12, 2020
15bc3c8
Remove reference of io.jsonwebtoken.lang.Collections. (#637)
rujche May 12, 2020
8e03416
Need refresh KeyVaultItems if propertyNames == null. (#637)
rujche May 12, 2020
64ff4bb
1. Remove unnecessary 'synchronized'. 2. Use volatile long instead of…
rujche May 12, 2020
c3c26f5
secretKey and propertyName mean different things. (#637)
rujche May 12, 2020
0646fbc
Property should be distinct. (#637)
rujche May 12, 2020
9986e00
Make KeyVaultPropertySource extend PropertySource to avoid get all pr…
rujche May 13, 2020
e2814dc
Rix error. (#637)
rujche May 13, 2020
46073e6
Fix gradle build error. (#637)
rujche May 14, 2020
53c0e9d
Add some test property in application.properties. (#637)
rujche May 14, 2020
70094b0
Fix mvn build failure. (#637)
rujche May 14, 2020
161fa85
Add another test case: keyVaultValueContainSpel. (#637)
rujche May 14, 2020
af2d3f4
Make the sample simple: Just keep the basic function. (#637)
rujche May 15, 2020
fbb0904
No logic change, just format code. (#637)
rujche May 15, 2020
29dd696
No logic change, just make code easier to read. (#637)
rujche May 15, 2020
54a46e5
No logic change, just make code easier to read. (#637)
rujche May 15, 2020
d79803f
Add test: test SpEL in key vault. (#637)
rujche May 15, 2020
c5806a3
No logic change, just make code easier to read. (#637)
rujche May 15, 2020
9680199
Remove useless dependency. (#637)
rujche May 15, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,23 @@

package sample.keyvault;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SampleApplication implements CommandLineRunner {
private static final Logger LOGGER = LoggerFactory.getLogger(SampleApplication.class);

@Value("${yourSecretPropertyName}")
private String mySecretProperty;
private String yourSecretPropertyName;

public static void main(String[] args) {
SpringApplication.run(SampleApplication.class, args);
}

public void run(String... varl) throws Exception {
LOGGER.info("property yourSecretPropertyName in Azure Key Vault: {}", mySecretProperty);

System.out.println("property yourSecretPropertyName in Azure Key Vault: " + mySecretProperty);
public void run(String[] args) throws Exception {
System.out.println("property yourSecretPropertyName value is: " + yourSecretPropertyName);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,21 @@
*/
@Slf4j
public class KeyVaultIT {

private static ClientSecretAccess access;
private static Vault vault;
private static String resourceGroupName;
private static RestTemplate restTemplate;
private static final String prefix = "test-keyvault";
private static final String VM_USER_NAME = "deploy";
private static final String VM_USER_PASSWORD = "12NewPAwX0rd!";
private static final String KEY_VAULT_VALUE = "value";
private static final String KEY_VAULT_SECRET_NAME = "key-vault-secret-name";
private static final String KEY_VAULT_SECRET_VALUE = "key-vault-secret-value";
private static final String APP_PROPERTY_NAME = "app.property.name";
private static final String APP_PROPERTY_VALUE = "app.property.value";
private static final String APP_PROPERTY_NAME_WITH_SPEL_IN_VALUE = "app.property.name.with.spel.in.value";
private static final String KEY_VAULT_SECRET_NAME_WITH_SPEL_IN_VALUE = "key-vault-secret-name-with-spel-in-value";
private static final String AZURE_COSMOSDB_KEY = "azure-cosmosdb-key";
private static final String TEST_KEY_VAULT_JAR_FILE_NAME = "app.jar";
private static final int DEFAULT_MAX_RETRY_TIMES = 3;
private static String TEST_KEYVAULT_APP_JAR_PATH;
Expand All @@ -74,8 +80,12 @@ public static void createKeyVault() throws IOException {
resourceGroupName = SdkContext.randomResourceName(ConstantsHelper.TEST_RESOURCE_GROUP_NAME_PREFIX, 30);
final KeyVaultTool tool = new KeyVaultTool(access);
vault = tool.createVaultInNewGroup(resourceGroupName, prefix);
vault.secrets().define("key").withValue(KEY_VAULT_VALUE).create();
vault.secrets().define("azure-cosmosdb-key").withValue(KEY_VAULT_VALUE).create();
vault.secrets().define(KEY_VAULT_SECRET_NAME).withValue(KEY_VAULT_SECRET_VALUE).create();
vault.secrets()
.define(KEY_VAULT_SECRET_NAME_WITH_SPEL_IN_VALUE)
.withValue(String.format("${%s}", APP_PROPERTY_NAME))
.create();
vault.secrets().define(AZURE_COSMOSDB_KEY).withValue(KEY_VAULT_SECRET_VALUE).create();
restTemplate = new RestTemplate();

TEST_KEYVAULT_APP_JAR_PATH = new File(System.getProperty("keyvault.app.jar.path")).getCanonicalPath();
Expand All @@ -84,7 +94,7 @@ public static void createKeyVault() throws IOException {
log.info("keyvault.app.zip.path={}", TEST_KEYVAULT_APP_ZIP_PATH);
log.info("--------------------->resources provision over");
}

@AfterClass
public static void deleteResourceGroup() {
final ResourceGroupTool tool = new ResourceGroupTool(access);
Expand All @@ -109,7 +119,7 @@ public void keyVaultAsPropertySource() {
.getSource().getClass() + "\n");
}

assertEquals(KEY_VAULT_VALUE, app.getProperty("key"));
assertEquals(KEY_VAULT_SECRET_VALUE, app.getProperty(KEY_VAULT_SECRET_NAME));
app.close();
log.info("--------------------->test over");
}
Expand All @@ -123,10 +133,52 @@ public void keyVaultAsPropertySourceWithSpecificKeys() {
app.property("azure.keyvault.client-id", access.clientId());
app.property("azure.keyvault.client-key", access.clientSecret());
app.property("azure.keyvault.tenant-id", access.tenant());
app.property("azure.keyvault.secret.keys", "key , azure-cosmosdb-key");
app.property(
"azure.keyvault.secret.keys",
String.join(",",
KEY_VAULT_SECRET_NAME,
AZURE_COSMOSDB_KEY
)
);

app.start();
assertEquals(KEY_VAULT_VALUE, app.getProperty("key"));
assertEquals(KEY_VAULT_SECRET_VALUE, app.getProperty(KEY_VAULT_SECRET_NAME));
app.close();
log.info("--------------------->test over");
}
}

@Test
public void keyVaultAsPropertySourceWithSpELInValue() {
try (AppRunner app = new AppRunner(DumbApp.class)) {
app.property("azure.keyvault.enabled", "true");
app.property("azure.keyvault.uri", vault.vaultUri());
app.property("azure.keyvault.client-id", access.clientId());
app.property("azure.keyvault.client-key", access.clientSecret());
app.property("azure.keyvault.tenant-id", access.tenant());
app.property(
"azure.keyvault.secret.keys",
String.join(",",
KEY_VAULT_SECRET_NAME,
AZURE_COSMOSDB_KEY,
KEY_VAULT_SECRET_NAME_WITH_SPEL_IN_VALUE
)
);
app.property(APP_PROPERTY_NAME, APP_PROPERTY_VALUE);
app.property(
APP_PROPERTY_NAME_WITH_SPEL_IN_VALUE,
String.format("${%s}", KEY_VAULT_SECRET_NAME)
);

app.start();
assertEquals(
KEY_VAULT_SECRET_VALUE,
app.getProperty(APP_PROPERTY_NAME_WITH_SPEL_IN_VALUE)
);
assertEquals(
APP_PROPERTY_VALUE,
app.getProperty(KEY_VAULT_SECRET_NAME_WITH_SPEL_IN_VALUE)
);
app.close();
log.info("--------------------->test over");
}
Expand Down Expand Up @@ -174,7 +226,7 @@ public void keyVaultWithAppServiceMSI() {
final ResponseEntity<String> response = curlWithRetry(resourceUrl, 3, 120_000, String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(KEY_VAULT_VALUE, response.getBody());
assertEquals(KEY_VAULT_SECRET_VALUE, response.getBody());
log.info("--------------------->test app service with MSI over");
}

Expand Down Expand Up @@ -206,13 +258,12 @@ public void keyVaultWithVirtualMachineMSI() throws Exception {
final List<String> commands = new ArrayList<>();
commands.add(String.format("cd /home/%s", VM_USER_NAME));
commands.add(
String.
format("nohup java -jar -Xdebug " +
String.format("nohup java -jar -Xdebug " +
"-Xrunjdwp:server=y,transport=dt_socket,address=4000,suspend=n " +
"-Dazure.keyvault.uri=%s %s &" +
" >/log.txt 2>&1"
, vault.vaultUri(),
TEST_KEY_VAULT_JAR_FILE_NAME));
" >/log.txt 2>&1",
vault.vaultUri(),
TEST_KEY_VAULT_JAR_FILE_NAME));
vmTool.runCommandOnVM(vm, commands);

final ResponseEntity<String> response = curlWithRetry(
Expand All @@ -222,15 +273,17 @@ public void keyVaultWithVirtualMachineMSI() throws Exception {
String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());
assertEquals(KEY_VAULT_VALUE, response.getBody());
assertEquals(KEY_VAULT_SECRET_VALUE, response.getBody());
log.info("key vault value is: {}", response.getBody());
log.info("--------------------->test virtual machine with MSI over");
}

private static <T> ResponseEntity<T> curlWithRetry(String resourceUrl,
final int retryTimes,
int sleepMills,
Class<T> clazz) {
private static <T> ResponseEntity<T> curlWithRetry(
String resourceUrl,
final int retryTimes,
int sleepMills,
Class<T> clazz
) {
HttpStatus httpStatus = HttpStatus.BAD_REQUEST;
ResponseEntity<T> response = ResponseEntity.of(Optional.empty());
int rt = retryTimes;
Expand All @@ -252,5 +305,6 @@ private static <T> ResponseEntity<T> curlWithRetry(String resourceUrl,
}

@SpringBootApplication
public static class DumbApp {}
public static class DumbApp {
}
}
Loading