-
-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[plugin-azure-key-vault] Add ability to read property values from Azu…
…re KeyVault
- Loading branch information
Showing
15 changed files
with
505 additions
and
222 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...e-resource-manager/src/main/java/org/vividus/azure/client/AzureResourceManagerClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.azure.client; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
|
||
import com.azure.core.credential.TokenCredential; | ||
import com.azure.core.http.ContentType; | ||
import com.azure.core.http.HttpHeaderName; | ||
import com.azure.core.http.HttpMethod; | ||
import com.azure.core.http.HttpPipeline; | ||
import com.azure.core.http.HttpRequest; | ||
import com.azure.core.http.HttpResponse; | ||
import com.azure.core.management.profile.AzureProfile; | ||
import com.azure.resourcemanager.resources.fluentcore.utils.HttpPipelineProvider; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.vividus.softassert.ISoftAssert; | ||
|
||
import io.netty.handler.codec.http.HttpResponseStatus; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class AzureResourceManagerClient | ||
{ | ||
private static final String EXECUTION_FAILED = "Azure REST API HTTP request execution is failed: "; | ||
|
||
private final HttpPipeline httpPipeline; | ||
private final AzureProfile azureProfile; | ||
|
||
public AzureResourceManagerClient(AzureProfile azureProfile, TokenCredential tokenCredential) | ||
{ | ||
this.azureProfile = azureProfile; | ||
this.httpPipeline = HttpPipelineProvider.buildHttpPipeline(tokenCredential, azureProfile); | ||
} | ||
|
||
/** | ||
* Executes an HTTP request to the specified Azure resource using the provided parameters. | ||
* This method constructs the URL for the request, sends the HTTP request, and handles the response | ||
* or errors using the provided Consumer and ISoftAssert. | ||
* | ||
* @param method The HTTP method to be used for the request (e.g., GET, POST, PUT, DELETE). | ||
* @param azureResourceIdentifier This is a VIVIDUS-only term. It's used to specify Azure resource uniquely. From | ||
* the technical perspective it's a part of Azure resource REST API URL path. For | ||
* example, if the full Azure resource URL is | ||
* <br> | ||
* <code>https://management.azure.com/subscriptions/ | ||
* 00000000-0000-0000-0000-000000000000/resourceGroups/sample-resource-group/ | ||
* providers/Microsoft.KeyVault/vaults/sample-vault?api-version=2021-10-01</code>, | ||
* <br> | ||
* then resource identifier will be | ||
* <br> | ||
* <code>resourceGroups/sample-resource-group/providers/Microsoft.KeyVault/ | ||
* vaults/sample-vault</code>. | ||
* @param apiVersion Azure resource provider API version. Note API versions may vary depending on | ||
* the resource type. | ||
* @param azureResourceBody The Azure resource configuration in JSON format. | ||
* @param responseBodyConsumer The Consumer that handles the response body if the request is successful. | ||
* @param softAssert An implementation of ISoftAssert used for recording assertions on failure. | ||
* This allows for error recording without stopping the execution flow. | ||
*/ | ||
public void executeHttpRequest(HttpMethod method, String azureResourceIdentifier, String apiVersion, | ||
Optional<String> azureResourceBody, Consumer<String> responseBodyConsumer, | ||
ISoftAssert softAssert) | ||
{ | ||
String url = String.format("%ssubscriptions/%s%s?api-version=%s", | ||
azureProfile.getEnvironment().getResourceManagerEndpoint(), azureProfile.getSubscriptionId(), | ||
StringUtils.prependIfMissing(azureResourceIdentifier, "/"), apiVersion); | ||
executeHttpRequest(method, url, azureResourceBody, responseBodyConsumer, softAssert::recordFailedAssertion); | ||
} | ||
|
||
/** | ||
* Executes an HTTP request to the specified URL with the given parameters, handles errors | ||
* using a soft assertion mechanism. Can be used for scenarios where need to continue execution | ||
* even if the HTTP request fails, but still record the error. | ||
* | ||
* @param method The HTTP method to be used for the request (e.g., GET, POST, PUT, DELETE). | ||
* @param azureResourceUrl It's used to specify Azure resource uniquely. For example: | ||
* <ul> | ||
* <li><code>https://management.azure.com/subscriptions/ | ||
* 00000000-0000-0000-0000-000000000000/resourceGroups/sample-resource-group/ | ||
* providers/Microsoft.KeyVault/vaults/sample-vault?api-version=2021-10-01</code> | ||
* <br><i>or</i> | ||
* <br> | ||
* <li><code>https://api.loganalytics.io/v1/workspaces/00000000-0000-0000-0000-000000000000/ | ||
* query?query=Syslog</code> | ||
* </ul> | ||
* @param azureResourceBody The Azure resource configuration in JSON format. | ||
* @param responseBodyConsumer The Consumer that handles the response body if the request is successful. | ||
* @param softAssert An implementation of ISoftAssert used for recording assertions on failure. | ||
* This allows for error recording without stopping the execution flow. | ||
*/ | ||
public void executeHttpRequest(HttpMethod method, String azureResourceUrl, Optional<String> azureResourceBody, | ||
Consumer<String> responseBodyConsumer, ISoftAssert softAssert) | ||
{ | ||
executeHttpRequest(method, azureResourceUrl, azureResourceBody, responseBodyConsumer, | ||
softAssert::recordFailedAssertion); | ||
} | ||
|
||
/** | ||
* Executes an HTTP request to the specified URL with the given parameters. | ||
* This version of the method throws a RuntimeException on any error. | ||
* | ||
* @param method The HTTP method to be used for the request (e.g., GET, POST, PUT, DELETE). | ||
* @param azureResourceUrl It's used to specify Azure resource uniquely. For example: | ||
* <ul> | ||
* <li><code>https://management.azure.com/subscriptions/ | ||
* 00000000-0000-0000-0000-000000000000/resourceGroups/sample-resource-group/ | ||
* providers/Microsoft.KeyVault/vaults/sample-vault?api-version=2021-10-01</code> | ||
* <br><i>or</i> | ||
* <br> | ||
* <li><code>https://api.loganalytics.io/v1/workspaces/00000000-0000-0000-0000-000000000000/ | ||
* query?query=Syslog</code> | ||
* </ul> | ||
* @param azureResourceBody The Azure resource configuration in JSON format. | ||
* @param responseBodyConsumer The Consumer that handles the response body if the request is successful. | ||
*/ | ||
public void executeHttpRequest(HttpMethod method, String azureResourceUrl, Optional<String> azureResourceBody, | ||
Consumer<String> responseBodyConsumer) | ||
{ | ||
executeHttpRequest(method, azureResourceUrl, azureResourceBody, responseBodyConsumer, | ||
error -> { throw new RuntimeException(error); }); | ||
} | ||
|
||
private void executeHttpRequest(HttpMethod method, String azureResourceUrl, Optional<String> azureResourceBody, | ||
Consumer<String> responseBodyConsumer, Consumer<String> errorCallback) | ||
{ | ||
HttpRequest httpRequest = new HttpRequest(method, azureResourceUrl); | ||
azureResourceBody.ifPresent(requestBody -> { | ||
httpRequest.setBody(requestBody); | ||
httpRequest.setHeader(HttpHeaderName.CONTENT_TYPE, ContentType.APPLICATION_JSON); | ||
}); | ||
|
||
try (HttpResponse httpResponse = httpPipeline.send(httpRequest).block()) | ||
{ | ||
Optional.ofNullable(httpResponse).map(HttpResponse::getBodyAsString).map(Mono::block).ifPresentOrElse( | ||
responseBody -> { | ||
if (httpResponse.getStatusCode() == HttpResponseStatus.OK.code()) | ||
{ | ||
responseBodyConsumer.accept(responseBody); | ||
} | ||
else | ||
{ | ||
errorCallback.accept(EXECUTION_FAILED + ": " + responseBody); | ||
} | ||
}, () -> errorCallback.accept(EXECUTION_FAILED + " with empty body and status code: " | ||
+ httpResponse.getStatusCode() | ||
) | ||
); | ||
} | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...nager/src/main/java/org/vividus/azure/configuration/AzureKeyVaultPropertiesProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.azure.configuration; | ||
|
||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
|
||
import com.azure.core.http.HttpMethod; | ||
import com.azure.core.management.AzureEnvironment; | ||
import com.azure.core.management.profile.AzureProfile; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
import org.vividus.azure.client.AzureResourceManagerClient; | ||
import org.vividus.azure.identity.CredentialFactory; | ||
import org.vividus.configuration.AbstractPropertiesProcessor; | ||
import org.vividus.util.json.JsonPathUtils; | ||
|
||
public class AzureKeyVaultPropertiesProcessor extends AbstractPropertiesProcessor | ||
{ | ||
private static final String KEY_VAULT_PROCESSOR_ENABLED_PROPERTY = "azure.key-vault.processor.enabled"; | ||
private static final String ENVIRONMENT_PROPERTY = "azure.environment"; | ||
private static final String KEY_VAULT_NAME_PROPERTY = "azure.key-vault.name"; | ||
private static final String KEY_VAULT_API_VERSION_PROPERTY = "azure.key-vault.api-version"; | ||
|
||
private static final Map<String, AzureEnvironment> ENVIRONMENT_MAP = Map.of( | ||
"AZURE", AzureEnvironment.AZURE, | ||
"AZURE_CHINA", AzureEnvironment.AZURE_CHINA, | ||
"AZURE_GERMANY", AzureEnvironment.AZURE_GERMANY, | ||
"AZURE_US_GOVERNMENT", AzureEnvironment.AZURE_US_GOVERNMENT | ||
); | ||
|
||
private AzureEnvironment azureEnvironment; | ||
private boolean processorEnabled; | ||
private String keyVaultName; | ||
private String apiVersion; | ||
private AzureResourceManagerClient client; | ||
|
||
public AzureKeyVaultPropertiesProcessor() | ||
{ | ||
super("AZURE_KEY_VAULT"); | ||
} | ||
|
||
@Override | ||
public Properties processProperties(Properties properties) | ||
{ | ||
this.processorEnabled = Boolean.parseBoolean(properties.getProperty(KEY_VAULT_PROCESSOR_ENABLED_PROPERTY)); | ||
this.azureEnvironment = ENVIRONMENT_MAP.get(properties.getProperty(ENVIRONMENT_PROPERTY)); | ||
this.keyVaultName = properties.getProperty(KEY_VAULT_NAME_PROPERTY); | ||
this.apiVersion = properties.getProperty(KEY_VAULT_API_VERSION_PROPERTY); | ||
return super.processProperties(properties); | ||
} | ||
|
||
@Override | ||
protected String processValue(String propertyName, String partOfPropertyValueToProcess) | ||
{ | ||
return processorEnabled ? getKeyVaultSecretValue(partOfPropertyValueToProcess) | ||
: String.format("AZURE_KEY_VAULT(%s)", partOfPropertyValueToProcess); | ||
} | ||
|
||
private String getKeyVaultSecretValue(String secretName) | ||
{ | ||
if (client == null) | ||
{ | ||
Validate.notEmpty(keyVaultName, "Provide value into property `%s`", KEY_VAULT_NAME_PROPERTY); | ||
this.client = new AzureResourceManagerClient(new AzureProfile(azureEnvironment), | ||
CredentialFactory.createTokenCredential()); | ||
} | ||
String[] value = new String[1]; | ||
client.executeHttpRequest(HttpMethod.GET, | ||
String.format("https://%s%s/secrets/%s?api-version=%s", keyVaultName, | ||
azureEnvironment.getKeyVaultDnsSuffix(), secretName, apiVersion), Optional.empty(), | ||
response -> value[0] = JsonPathUtils.getData(response, "$.value")); | ||
return value[0]; | ||
} | ||
} |
Oops, something went wrong.