+ * This helper class makes it possible to configure credentials to be used + * within a Spring context. + *
+ * + *| Property Tuples | + *Description | + *
|---|---|
| azure.credential.(name.)tenantId + * azure.credential.(name.)clientId + * azure.credential.(name.)clientSecret |
+ * the Azure Tenant ID + * the Client ID + * the Client Certificate + * |
+ *
| azure.credential.(name.)tenantId + * azure.credential.(name.)clientId + * azure.credential.(name.)clientCertificate |
+ * the Azure Tenant ID + * the Client ID + * the path to the PEM client certificate |
+ *
name of the credential. Note if
+ * name is entirely omitted it is taken to be the default
+ * credential. Note if the default credential is omitted it is configure to use
+ * AzureDefaultCredential which allows for the use a Managed Identity (if it is
+ * present).
+ *
+ * @author manfred.riem@microsoft.com
+ */
+public class SpringEnvironmentTokenBuilder {
+
+ /**
+ * Defines the AZURE_CREDENTIAL_PREFIX.
+ */
+ private static final String AZURE_CREDENTIAL_PREFIX = "azure.credential.";
+
+ /**
+ * Stores the named credentials.
+ */
+ private final HashMapname
+ *
+ * @param name
+ * @return
+ */
+ public SpringEnvironmentTokenBuilder namedCredential(String name) {
+ this.name = name;
+ return this;
+ }
+
+ /**
+ * Sets the builder to return the default credential.
+ */
+ public SpringEnvironmentTokenBuilder defaultCredential() {
+ return namedCredential("");
+ }
+
+ /**
+ * Builds an Azure TokenCredendial.
+ *
+ * @throws IllegalArgumentException if attempting to retrieve a named credential
+ * not defined in the environment.
+ */
+ public TokenCredential build() {
+ TokenCredential result = credentials.get(name);
+ if (result == null) {
+ throw new IllegalArgumentException(
+ "Attempting to retrieve Azure credential not configured in the environment. (name=" + name + ")");
+ } else {
+ return result;
+ }
+ }
+}
diff --git a/sdk/spring/azure-identity-spring-library/src/test/java/com/microsoft/azure/identity/spring/SpringEnvironmentTokenBuilderTest.java b/sdk/spring/azure-identity-spring-library/src/test/java/com/microsoft/azure/identity/spring/SpringEnvironmentTokenBuilderTest.java
new file mode 100644
index 000000000000..496385d04edf
--- /dev/null
+++ b/sdk/spring/azure-identity-spring-library/src/test/java/com/microsoft/azure/identity/spring/SpringEnvironmentTokenBuilderTest.java
@@ -0,0 +1,88 @@
+// Copyright (c) Microsoft Corporation. All rights reserved.
+// Licensed under the MIT License.
+package com.microsoft.azure.identity.spring;
+
+import com.azure.identity.ClientSecretCredential;
+import com.azure.identity.DefaultAzureCredential;
+import com.azure.identity.DefaultAzureCredentialBuilder;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import org.junit.jupiter.api.Test;
+import org.springframework.core.env.StandardEnvironment;
+
+/**
+ * The unit tests for the AzureIdentitySpringHelper class.
+ *
+ * @author manfred.riem@microsoft.com
+ */
+public class SpringEnvironmentTokenBuilderTest {
+
+ /**
+ * Test getDefaultCredential method.
+ */
+ @Test
+ public void testGetDefaultCredential() {
+ SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
+ assertNotNull(builder.build());
+ assertEquals(builder.build(), builder.defaultCredential().build());
+ }
+
+ /**
+ * Test populate method.
+ */
+ @Test
+ public void testPopulate() {
+ System.setProperty("azure.credential.names", "");
+ System.setProperty("azure.credential.tenantId", "tenantId");
+ System.setProperty("azure.credential.clientId", "clientId");
+ System.setProperty("azure.credential.clientSecret", "clientSecret");
+ StandardEnvironment environment = new StandardEnvironment();
+ SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
+ builder.fromEnvironment(environment);
+
+ assertNotNull(builder.build());
+ assertTrue(builder.build() instanceof ClientSecretCredential);
+ assertEquals(builder.build(), builder.defaultCredential().build());
+ }
+
+ /**
+ * Test populate method.
+ */
+ @Test
+ public void testPopulate2() {
+ System.setProperty("azure.credential.names", "myname");
+ System.setProperty("azure.credential.myname.tenantId", "tenantId");
+ System.setProperty("azure.credential.myname.clientId", "clientId");
+ System.setProperty("azure.credential.myname.clientSecret", "clientSecret");
+ StandardEnvironment environment = new StandardEnvironment();
+ SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
+ builder.fromEnvironment(environment);
+ assertNotNull(builder.namedCredential("myname").build());
+ assertTrue(builder.build() instanceof ClientSecretCredential);
+ assertNotEquals(builder.build(), builder.defaultCredential().build());
+ }
+
+ /**
+ * Test populate method.
+ */
+ @Test
+ public void testPopulate3() {
+ System.setProperty("azure.credential.names", "myname2");
+ System.setProperty("azure.credential.myname2.tenantId", "tenantId");
+ System.setProperty("azure.credential.myname2.clientSecret", "clientSecret");
+ StandardEnvironment environment = new StandardEnvironment();
+ SpringEnvironmentTokenBuilder builder = new SpringEnvironmentTokenBuilder();
+ try {
+ builder.fromEnvironment(environment);
+ fail();
+ } catch (Throwable t) {
+ assertEquals(IllegalStateException.class, t.getClass(),
+ "Unexpected exception class on missing configuration field.");
+ }
+ }
+}
diff --git a/sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-servicebus/pom.xml b/sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-servicebus/pom.xml
index d2f7d689574b..c9ad24eef608 100644
--- a/sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-servicebus/pom.xml
+++ b/sdk/spring/azure-spring-boot-samples/azure-spring-boot-sample-servicebus/pom.xml
@@ -31,7 +31,7 @@
com.azure.core.management SDK.
+ */
+ default com.azure.core.management.AzureEnvironment getCoreEnvironment() {
+ return com.azure.core.management.AzureEnvironment.knownEnvironments().stream()
+ .filter(coreEnvironment -> getEnvironment().managementEndpoint()
+ .equals(coreEnvironment.getManagementEndpoint()))
+ .findAny().get();
+ }
+
}
diff --git a/sdk/spring/azure-spring-cloud-context/src/main/java/com/azure/spring/cloud/context/core/api/ResourceManagerProvider.java b/sdk/spring/azure-spring-cloud-context/src/main/java/com/azure/spring/cloud/context/core/api/ResourceManagerProvider.java
deleted file mode 100644
index 9104b9c03ab4..000000000000
--- a/sdk/spring/azure-spring-cloud-context/src/main/java/com/azure/spring/cloud/context/core/api/ResourceManagerProvider.java
+++ /dev/null
@@ -1,48 +0,0 @@
-// Copyright (c) Microsoft Corporation. All rights reserved.
-// Licensed under the MIT License.
-
-package com.azure.spring.cloud.context.core.api;
-
-import com.microsoft.azure.management.eventhub.EventHub;
-import com.microsoft.azure.management.eventhub.EventHubConsumerGroup;
-import com.microsoft.azure.management.eventhub.EventHubNamespace;
-import com.microsoft.azure.management.redis.RedisCache;
-import com.microsoft.azure.management.resources.ResourceGroup;
-import com.microsoft.azure.management.servicebus.Queue;
-import com.microsoft.azure.management.servicebus.ServiceBusNamespace;
-import com.microsoft.azure.management.servicebus.ServiceBusSubscription;
-import com.microsoft.azure.management.servicebus.Topic;
-import com.microsoft.azure.management.storage.StorageAccount;
-import com.azure.spring.cloud.context.core.util.Tuple;
-import com.microsoft.azure.storage.CloudStorageAccount;
-import com.microsoft.azure.storage.queue.CloudQueue;
-
-/**
- * Interface to provide {@link ResourceManager}
- *
- * @author Warren Zhu
- */
-public interface ResourceManagerProvider {
-
- ResourceManager