From 8cd121cd8dcc2dd1bb4e8b82b28a93ff9890bfb6 Mon Sep 17 00:00:00 2001 From: Shili Chen Date: Wed, 2 Nov 2022 18:25:46 +0800 Subject: [PATCH] Cherry-pick the spring change from https://github.com/Azure/azure-sdk-for-java/commit/94e61ff66d2345f3594f3293b39f68be04f1c8b6 --- ...bstractAzureServiceConfigurationTests.java | 82 ++++++++++--------- .../autoconfigure/FakeCredentialInTest.java | 29 +++++++ .../UserPrincipalMicrosoftGraphTests.java | 2 +- ...reCloudFoundryServiceApplicationTests.java | 4 +- .../test/resources/cloudfoundry/vcap2.json | 3 +- .../test/resources/cloudfoundry/vcap3.json | 3 +- .../storage/FakeCredentialInTest.java | 15 ++++ .../AzureBlobClientBuilderFactoryTests.java | 4 +- 8 files changed, 94 insertions(+), 48 deletions(-) create mode 100644 sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/FakeCredentialInTest.java create mode 100644 sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/FakeCredentialInTest.java diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/AbstractAzureServiceConfigurationTests.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/AbstractAzureServiceConfigurationTests.java index 15f1fb26d225..aa1a83763f2b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/AbstractAzureServiceConfigurationTests.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/AbstractAzureServiceConfigurationTests.java @@ -20,6 +20,10 @@ import org.springframework.boot.test.context.assertj.AssertableApplicationContext; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import static com.azure.spring.cloud.autoconfigure.FakeCredentialInTest.CLIENT_ID_PLACEHOLDER; +import static com.azure.spring.cloud.autoconfigure.FakeCredentialInTest.PASSWORD_PLACEHOLDER; +import static com.azure.spring.cloud.autoconfigure.FakeCredentialInTest.SECRET; +import static com.azure.spring.cloud.autoconfigure.FakeCredentialInTest.USERNAME_PLACEHOLDER; import static com.azure.spring.cloud.core.implementation.util.ReflectionUtils.getField; import static org.assertj.core.api.Assertions.assertThat; @@ -37,55 +41,55 @@ public abstract class AbstractAzureServiceConfigurationTests { - assertSovereignCloudsSetInCredential(context, ClientSecretCredential.class); - }); + .withPropertyValues( + getPropertyPrefix() + ".profile.cloud-type=AZURE_US_GOVERNMENT", + getPropertyPrefix() + ".profile.tenant-id=fake-tenant-id", + getPropertyPrefix() + ".credential." + CLIENT_ID_PLACEHOLDER + "=fakeClientIdPlaceholder", + getPropertyPrefix() + ".credential.client-" + SECRET + "=fake-client-secret" + ) + .withConfiguration(AutoConfigurations.of( + AzureTokenCredentialAutoConfiguration.class, + AzureGlobalPropertiesAutoConfiguration.class + )) + .run(context -> { + assertSovereignCloudsSetInCredential(context, ClientSecretCredential.class); + }); } @Test protected void usGovCloudShouldWorkWithClientCertificateCredential() { getMinimalContextRunner() - .withPropertyValues( - getPropertyPrefix() + ".profile.cloud-type=AZURE_US_GOVERNMENT", - getPropertyPrefix() + ".profile.tenant-id=fake-tenant-id", - getPropertyPrefix() + ".credential.client-id=fake-client-id", - getPropertyPrefix() + ".credential.client-certificate-path=fake-client-cert-path" - ) - .withConfiguration(AutoConfigurations.of( - AzureTokenCredentialAutoConfiguration.class, - AzureGlobalPropertiesAutoConfiguration.class - )) - .run(context -> { - assertSovereignCloudsSetInCredential(context, ClientCertificateCredential.class); - }); + .withPropertyValues( + getPropertyPrefix() + ".profile.cloud-type=AZURE_US_GOVERNMENT", + getPropertyPrefix() + ".profile.tenant-id=fake-tenant-id", + getPropertyPrefix() + ".credential." + CLIENT_ID_PLACEHOLDER + "=fakeClientIdPlaceholder", + getPropertyPrefix() + ".credential.client-certificate-path=fake-client-cert-path" + ) + .withConfiguration(AutoConfigurations.of( + AzureTokenCredentialAutoConfiguration.class, + AzureGlobalPropertiesAutoConfiguration.class + )) + .run(context -> { + assertSovereignCloudsSetInCredential(context, ClientCertificateCredential.class); + }); } @Test protected void usGovCloudShouldWorkWithUsernamePasswordCredential() { getMinimalContextRunner() - .withPropertyValues( - getPropertyPrefix() + ".profile.cloud-type=AZURE_US_GOVERNMENT", - getPropertyPrefix() + ".credential.client-id=fake-client-id", - getPropertyPrefix() + ".credential.username=123", - getPropertyPrefix() + ".credential.password=123" - ) - .withConfiguration(AutoConfigurations.of( - AzureTokenCredentialAutoConfiguration.class, - AzureGlobalPropertiesAutoConfiguration.class - )) - .run(context -> { - assertSovereignCloudsSetInCredential(context, UsernamePasswordCredential.class); - }); + .withPropertyValues( + getPropertyPrefix() + ".profile.cloud-type=AZURE_US_GOVERNMENT", + getPropertyPrefix() + ".credential." + CLIENT_ID_PLACEHOLDER + "=fakeClientIdPlaceholder", + getPropertyPrefix() + ".credential." + USERNAME_PLACEHOLDER + "=fakeNamePlaceholder", + getPropertyPrefix() + ".credential." + PASSWORD_PLACEHOLDER + "=fakePasswordPlaceholder" + ) + .withConfiguration(AutoConfigurations.of( + AzureTokenCredentialAutoConfiguration.class, + AzureGlobalPropertiesAutoConfiguration.class + )) + .run(context -> { + assertSovereignCloudsSetInCredential(context, UsernamePasswordCredential.class); + }); } private void assertSovereignCloudsSetInCredential(AssertableApplicationContext context, Class credentialType) { diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/FakeCredentialInTest.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/FakeCredentialInTest.java new file mode 100644 index 000000000000..d8c0c4a082bb --- /dev/null +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/FakeCredentialInTest.java @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.autoconfigure; + +/** + * Fake credential shared in Tests + */ +public class FakeCredentialInTest { + /** + * Username placeholder + */ + public static final String USERNAME_PLACEHOLDER = "username"; + + /** + * Password placeholder + */ + public static final String PASSWORD_PLACEHOLDER = "password"; + + /** + * Client ID placeholder + */ + public static final String CLIENT_ID_PLACEHOLDER = "client-id"; + + /** + * Secret placeholder + */ + public static final String SECRET = "secret"; +} diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/aad/implementation/graph/UserPrincipalMicrosoftGraphTests.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/aad/implementation/graph/UserPrincipalMicrosoftGraphTests.java index 13a4c916d527..d49e86657011 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/aad/implementation/graph/UserPrincipalMicrosoftGraphTests.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/aad/implementation/graph/UserPrincipalMicrosoftGraphTests.java @@ -81,7 +81,7 @@ void setup() { properties.getProfile().getEnvironment().setMicrosoftGraphEndpoint(MOCK_MICROSOFT_GRAPH_ENDPOINT); endpoints = new AadAuthorizationServerEndpoints(properties.getProfile().getEnvironment().getActiveDirectoryEndpoint(), properties.getProfile().getTenantId()); clientId = "client"; - clientSecret = "pass"; + clientSecret = "fakeCredentialPlaceholder"; } @Test diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cloudfoundry/environment/AzureCloudFoundryServiceApplicationTests.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cloudfoundry/environment/AzureCloudFoundryServiceApplicationTests.java index 933eb1c0667b..a3468fdc3331 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cloudfoundry/environment/AzureCloudFoundryServiceApplicationTests.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/cloudfoundry/environment/AzureCloudFoundryServiceApplicationTests.java @@ -91,7 +91,7 @@ public void testVcapSingleServiceWithNulls() throws IOException { assertEquals("dbs/ZFxCAA==/", config.getCredentials().get("documentdb_database_link")); assertEquals("https://hostname:443/", config.getCredentials().get("documentdb_host_endpoint")); assertEquals( - "3becR7JFnWamMvGwWYWWTV4WpeNhN8tOzJ74yjAxPKDpx65q2lYz60jt8WXU6HrIKrAIwhs0Hglf0123456789==", + "fakeCredentialPlaceholder", config.getCredentials().get("documentdb_master_key")); } catch (IOException e) { LOG.error("Error reading json file", e); @@ -127,7 +127,7 @@ public void testVcapUserProvidedService() throws IOException { assertEquals("dbs/ZFxCAA==/", config.getCredentials().get("documentdb_database_link")); assertEquals("https://hostname:443/", config.getCredentials().get("documentdb_host_endpoint")); assertEquals( - "3becR7JFnWamMvGwWYWWTV4WpeNhN8tOzJ74yjAxPKDpx65q2lYz60jt8WXU6HrIKrAIwhs0Hglf0123456789==", + "fakeCredentialPlaceholder", config.getCredentials().get("documentdb_master_key")); } catch (IOException e) { LOG.error("Error reading json file", e); diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap2.json b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap2.json index 454f12c3c4bc..fbc837979445 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap2.json +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap2.json @@ -5,7 +5,7 @@ "documentdb_database_id": "docdb123mj", "documentdb_database_link": "dbs/ZFxCAA==/", "documentdb_host_endpoint": "https://hostname:443/", - "documentdb_master_key": "3becR7JFnWamMvGwWYWWTV4WpeNhN8tOzJ74yjAxPKDpx65q2lYz60jt8WXU6HrIKrAIwhs0Hglf0123456789==" + "documentdb_master_key": "fakeCredentialPlaceholder" }, "label": "azure-documentdb", "name": "mydocumentdb", @@ -17,4 +17,3 @@ } ] } - \ No newline at end of file diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap3.json b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap3.json index 563d4e3da258..96fb0dc5b546 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap3.json +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/resources/cloudfoundry/vcap3.json @@ -7,7 +7,7 @@ "documentdb_database_id": "docdb123mj", "documentdb_database_link": "dbs/ZFxCAA==/", "documentdb_host_endpoint": "https://hostname:443/", - "documentdb_master_key": "3becR7JFnWamMvGwWYWWTV4WpeNhN8tOzJ74yjAxPKDpx65q2lYz60jt8WXU6HrIKrAIwhs0Hglf0123456789==" + "documentdb_master_key": "fakeCredentialPlaceholder" }, "label": "user-provided", "name": "mydocumentdb", @@ -17,4 +17,3 @@ } ] } - \ No newline at end of file diff --git a/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/FakeCredentialInTest.java b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/FakeCredentialInTest.java new file mode 100644 index 000000000000..b83fdaf5064b --- /dev/null +++ b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/FakeCredentialInTest.java @@ -0,0 +1,15 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.cloud.service.implementation.storage; + +/** + * Fake credential list. + */ +public final class FakeCredentialInTest { + /** + * Fake customer provided key credential. + */ + public static final String FAKE_CUSTOMER_PROVIDED_KEY = + "JdppJP5eH1w/CQ0cx4RGYWoC7NmQ0nmDbYR2PYWSDTXojV9bI1ck0Eh0sUIg8xj4KYj7tv+ZPLICu3BgLt6mMz=="; +} diff --git a/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/blob/AzureBlobClientBuilderFactoryTests.java b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/blob/AzureBlobClientBuilderFactoryTests.java index b12b358b7b4c..7c123824f75e 100644 --- a/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/blob/AzureBlobClientBuilderFactoryTests.java +++ b/sdk/spring/spring-cloud-azure-service/src/test/java/com/azure/spring/cloud/service/implementation/storage/blob/AzureBlobClientBuilderFactoryTests.java @@ -19,6 +19,7 @@ import java.util.List; +import static com.azure.spring.cloud.service.implementation.storage.FakeCredentialInTest.FAKE_CUSTOMER_PROVIDED_KEY; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; @@ -34,7 +35,6 @@ class AzureBlobClientBuilderFactoryTests AzureBlobClientBuilderFactoryTests.BlobServiceClientBuilderFactoryExt> { private static final String ENDPOINT = "https://abc.blob.core.windows.net/"; - private static final String CUSTOMER_PROVIDED_KEY = "JdppJP5eH1w/CQ0cx4RGYWoC7NmQ0nmDbYR2PYWSDTXojV9bI1ck0Eh0sUIg8xj4KYj7tv+ZPLICu3BgLt6mMz=="; private static final String CONNECTION_STRING = "BlobEndpoint=https://test.blob.core.windows.net/;" + "QueueEndpoint=https://test.queue.core.windows.net/;FileEndpoint=https://test.file.core.windows.net/;" + "TableEndpoint=https://test.table.core.windows.net/;SharedAccessSignature=sv=2020-08-04" @@ -92,7 +92,7 @@ protected void buildClient(BlobServiceClientBuilder builder) { protected void verifyServicePropertiesConfigured() { AzureStorageBlobTestProperties properties = new AzureStorageBlobTestProperties(); properties.setEndpoint(ENDPOINT); - properties.setCustomerProvidedKey(CUSTOMER_PROVIDED_KEY); + properties.setCustomerProvidedKey(FAKE_CUSTOMER_PROVIDED_KEY); properties.setEncryptionScope("test-scope"); properties.setServiceVersion(BlobServiceVersion.V2019_07_07);