I am working on a Java CucumberTest automation project. I am storing the usernames, passwords and other secrets in a json file.
{
"adminUsername":"adminuser",
"adminUserPassword":"Password123",
"host-url":"http://example.com/api/user",
"apiKey":"ak9202zmiwiwkwm",
}
The secrets are stored in Azure keyvalut. Currently I am manually copying the values from Azure KeyVault and updated the JSON file. During the execution of tests, the values are read from the json file. However it's not recommended to keep secrets in the code What I want to do is:
- When the test run started (Cucumber BeforeAll hook), load the json file
- Loop through the json file through and read each key
- for each key, get the secret value from AzureKeyVault and update the json file with the value for that key.
I am able to read the value of the secret from AzureKeyVault with this code:
String keyVaultName = System.getenv("KEY_VAULT_NAME");
String keyVaultUri = "https://" + keyVaultName + ".vault.azure.net";
SecretClient secretClient = new SecretClientBuilder()
.vaultUrl(keyVaultUri)
.credential(new DefaultAzureCredentialBuilder().build())
.buildClient();
KeyVaultSecret retrievedSecret = secretClient.getSecret("adminUserPassword");
System.out.println(retrievedSecret.getName()+"="+retrievedSecret.getValue());
I would like update the JSON file contents with the secret value that I just read from KeyVault in memory. And I want to do that for each key in the json file. I don't want to write to physical file. And later I would like to Deserialize the json to an object using ObjectMapper. I already defined a class matching with the JSON file structure.