This is an automated email from the ASF dual-hosted git repository. acosentino pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/camel.git
commit e7e95444381b09f30b2dec9fe7b3e94669232911 Author: Andrea Cosentino <[email protected]> AuthorDate: Fri Apr 1 13:20:50 2022 +0200 CAMEL-17686 - Support ability to load properties from Vault/Secrets cloud services - Azure Key Vault --- .../key/vault/KeyVaultPropertiesFunction.java | 3 +- .../operations/KeyVaultPropertiesSourceTestIT.java | 137 +++++++++++++++++++++ 2 files changed, 139 insertions(+), 1 deletion(-) diff --git a/components/camel-azure/camel-azure-key-vault/src/main/java/org/apache/camel/component/azure/key/vault/KeyVaultPropertiesFunction.java b/components/camel-azure/camel-azure-key-vault/src/main/java/org/apache/camel/component/azure/key/vault/KeyVaultPropertiesFunction.java index ea3e105..e1d8c91 100644 --- a/components/camel-azure/camel-azure-key-vault/src/main/java/org/apache/camel/component/azure/key/vault/KeyVaultPropertiesFunction.java +++ b/components/camel-azure/camel-azure-key-vault/src/main/java/org/apache/camel/component/azure/key/vault/KeyVaultPropertiesFunction.java @@ -16,6 +16,7 @@ */ package org.apache.camel.component.azure.key.vault; +import com.azure.core.exception.ResourceNotFoundException; import com.azure.identity.ClientSecretCredential; import com.azure.identity.ClientSecretCredentialBuilder; import com.azure.security.keyvault.secrets.SecretClient; @@ -170,7 +171,7 @@ public class KeyVaultPropertiesFunction extends ServiceSupport implements Proper if (ObjectHelper.isEmpty(returnValue)) { returnValue = defaultValue; } - } catch (Exception ex) { + } catch (ResourceNotFoundException ex) { if (ObjectHelper.isNotEmpty(defaultValue)) { returnValue = defaultValue; } else { diff --git a/components/camel-azure/camel-azure-key-vault/src/test/java/org/apache/camel/component/azure/key/vault/integration/operations/KeyVaultPropertiesSourceTestIT.java b/components/camel-azure/camel-azure-key-vault/src/test/java/org/apache/camel/component/azure/key/vault/integration/operations/KeyVaultPropertiesSourceTestIT.java new file mode 100644 index 0000000..819f15a --- /dev/null +++ b/components/camel-azure/camel-azure-key-vault/src/test/java/org/apache/camel/component/azure/key/vault/integration/operations/KeyVaultPropertiesSourceTestIT.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 + * + * http://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.apache.camel.component.azure.key.vault.integration.operations; + +import org.apache.camel.FailedToCreateRouteException; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class KeyVaultPropertiesSourceTestIT extends CamelTestSupport { + + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_VAULT_NAME", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_ID", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_SECRET", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_TENANT_ID", matches = ".*") + @Test + public void testFunction() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start").setBody(simple("{{azure:hello}}")).to("mock:bar"); + } + }); + context.start(); + + getMockEndpoint("mock:bar").expectedBodiesReceived("test"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_VAULT_NAME", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_ID", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_SECRET", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_TENANT_ID", matches = ".*") + @Test + public void testFunctionWithDefault() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start").setBody(simple("{{azure:hello:admin}}")).to("mock:bar"); + } + }); + context.start(); + + getMockEndpoint("mock:bar").expectedBodiesReceived("test"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_VAULT_NAME", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_ID", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_SECRET", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_TENANT_ID", matches = ".*") + @Test + public void testFunctionWithMultiValues() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:start").setBody(simple("{{azure:database/username}}")).to("mock:bar"); + } + }); + context.start(); + + getMockEndpoint("mock:bar").expectedBodiesReceived("admin"); + + template.sendBody("direct:start", "Hello World"); + + assertMockEndpointsSatisfied(); + } + + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_VAULT_NAME", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_ID", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_SECRET", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_TENANT_ID", matches = ".*") + @Test + public void testFunctionWithMultiValuesAndDefault() throws Exception { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:username").setBody(simple("{{azure:dbsample/username:oscerd}}")).to("mock:bar"); + from("direct:password").setBody(simple("{{azure:dbsample/password:password}}")).to("mock:bar"); + } + }); + context.start(); + + getMockEndpoint("mock:bar").expectedBodiesReceived("oscerd", "password"); + + template.sendBody("direct:username", "Hello World"); + template.sendBody("direct:password", "Hello World"); + assertMockEndpointsSatisfied(); + } + + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_VAULT_NAME", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_ID", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_CLIENT_SECRET", matches = ".*") + @EnabledIfEnvironmentVariable(named = "CAMEL_VAULT_AZURE_TENANT_ID", matches = ".*") + @Test + public void testComplexCustomPropertiesNoDefaultValueFunction() { + Exception exception = assertThrows(FailedToCreateRouteException.class, () -> { + context.addRoutes(new RouteBuilder() { + @Override + public void configure() { + from("direct:username").setBody(simple("{{azure:postgresql/additional1}}")).to("mock:bar"); + from("direct:password").setBody(simple("{{azure:postgresql/additional2}}")).to("mock:bar"); + } + }); + context.start(); + + getMockEndpoint("mock:bar").expectedBodiesReceived("admin", "secret"); + + template.sendBody("direct:username", "Hello World"); + template.sendBody("direct:password", "Hello World"); + assertMockEndpointsSatisfied(); + }); + } +}
