Skip to content

Commit

Permalink
[github #4555][devops #1761663] [Bug fixes] Azure CLI authentication …
Browse files Browse the repository at this point in the history
…does not show subscriptions for all tenants
  • Loading branch information
wangmingliang-ms committed Aug 25, 2020
1 parent 851c008 commit 9e9a00b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.applicationinsights.v2015_05_01.implementation.InsightsManager;
import com.microsoft.azure.management.appplatform.v2019_05_01_preview.implementation.AppPlatformManager;
import com.microsoft.azure.management.resources.Subscription;
import com.microsoft.azure.management.resources.Tenant;
import com.microsoft.azuretools.adauth.PromptBehavior;
import com.microsoft.azuretools.adauth.StringUtils;
import com.microsoft.azuretools.authmanage.AdAuthManagerBuilder;
import com.microsoft.azuretools.authmanage.AzureManagerFactory;
import com.microsoft.azuretools.authmanage.BaseADAuthManager;
import com.microsoft.azuretools.authmanage.CommonSettings;
import com.microsoft.azuretools.authmanage.Environment;
import com.microsoft.azuretools.authmanage.RefreshableTokenCredentials;
import com.microsoft.azuretools.authmanage.SubscriptionManager;
import com.microsoft.azuretools.authmanage.SubscriptionManagerPersist;
import com.microsoft.azuretools.authmanage.models.AuthMethodDetails;
import com.microsoft.azuretools.telemetry.TelemetryInterceptor;
import com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces;
import com.microsoft.azuretools.utils.Pair;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import rx.Observable;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import static com.microsoft.azuretools.Constants.FILE_NAME_SUBSCRIPTIONS_DETAILS_AT;
import static org.apache.commons.lang3.StringUtils.isBlank;
Expand Down Expand Up @@ -90,7 +80,6 @@ public BaseADAuthManager getInstance() {
}
}

private static final Logger LOGGER = Logger.getLogger(AccessTokenAzureManager.class.getName());
private final SubscriptionManager subscriptionManager;
private final BaseADAuthManager delegateADAuthManager;

Expand Down Expand Up @@ -123,7 +112,7 @@ public Azure getAzure(String sid) throws IOException {
return sidToAzureMap.get(sid);
}
String tid = subscriptionManager.getSubscriptionTenant(sid);
Azure azure = authTid(tid).withSubscription(sid);
Azure azure = authTenant(tid).withSubscription(sid);
// TODO: remove this call after Azure SDK properly implements handling of unregistered provider namespaces
AzureRegisterProviderNamespaces.registerAzureNamespaces(azure);
sidToAzureMap.put(sid, azure);
Expand All @@ -146,85 +135,14 @@ public InsightsManager getInsightsManager(String sid) {
});
}

@Override
public List<Subscription> getSubscriptions() throws IOException {
List<Subscription> sl = new LinkedList<Subscription>();
// could be multi tenant - return all subscriptions for the current account
List<Tenant> tl = getTenants(delegateADAuthManager.getCommonTenantId());
for (Tenant t : tl) {
sl.addAll(getSubscriptions(t.tenantId()));
}
return sl;
}

@Override
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() throws IOException {
List<Pair<Subscription, Tenant>> stl = new LinkedList<>();
for (Tenant t : getTenants(delegateADAuthManager.getCommonTenantId())) {
String tid = t.tenantId();
for (Subscription s : getSubscriptions(tid)) {
stl.add(new Pair<Subscription, Tenant>(s, t));
}
}
return stl;
}

@Override
public Settings getSettings() {
return settings;
}

public List<Subscription> getSubscriptions(String tid) throws IOException {
List<Subscription> sl = authTid(tid).subscriptions().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());

return Observable.empty();
})
.toList()
.toBlocking()
.singleOrDefault(Collections.emptyList());

return sl;
}

public List<Tenant> getTenants(String tid) throws IOException {
List<Tenant> tl = authTid(tid).tenants().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());

return Observable.empty();
})
.toList()
.toBlocking()
.singleOrDefault(Collections.emptyList());

return tl;
}

// public static Azure.Authenticated auth(String accessToken) throws Exception {
// return Azure.configure().authenticate(getTokenCredentials(accessToken));
// }

// private static TokenCredentials getTokenCredentials(String token) throws Exception {
// return null;
// }

private Azure.Authenticated authTid(String tid) throws IOException {
return Azure.configure()
.withInterceptor(new TelemetryInterceptor())
.withUserAgent(CommonSettings.USER_AGENT)
.authenticate(new RefreshableTokenCredentials(this, tid));
}

private AppPlatformManager authSpringCloud(String sid, String tid) {
return buildAzureManager(AppPlatformManager.configure())
.authenticate(new RefreshableTokenCredentials(this, tid), sid);
}

private InsightsManager authApplicationInsights(String sid, String tid) {
return buildAzureManager(InsightsManager.configure())
.authenticate(new RefreshableTokenCredentials(this, tid), sid);
@Override
protected String getTenantId() {
return delegateADAuthManager.getCommonTenantId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.applicationinsights.v2015_05_01.implementation.InsightsManager;
import com.microsoft.azure.management.appplatform.v2019_05_01_preview.implementation.AppPlatformManager;
import com.microsoft.azure.management.resources.Subscription;
import com.microsoft.azure.management.resources.Tenant;
import com.microsoft.azuretools.adauth.PromptBehavior;
import com.microsoft.azuretools.authmanage.AuthMethod;
import com.microsoft.azuretools.authmanage.AzureManagerFactory;
Expand All @@ -44,20 +42,18 @@
import com.microsoft.azuretools.azurecommons.helpers.Nullable;
import com.microsoft.azuretools.telemetry.TelemetryInterceptor;
import com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces;
import com.microsoft.azuretools.utils.Pair;
import com.microsoft.rest.credentials.ServiceClientCredentials;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Logger;

import static com.microsoft.azuretools.Constants.FILE_NAME_SUBSCRIPTIONS_DETAILS_AZ;
import static com.microsoft.azuretools.authmanage.Environment.ENVIRONMENT_LIST;

public class AzureCliAzureManager extends AzureManagerBase {
private static final Logger LOGGER = Logger.getLogger(AzureCliAzureManager.class.getName());

private static final String FAILED_TO_AUTH_WITH_AZURE_CLI = "Failed to auth with Azure CLI";
private static final String UNABLE_TO_GET_AZURE_CLI_CREDENTIALS = "Unable to get Azure CLI credentials, " +
"please ensure you have installed Azure CLI and signed in.";
Expand Down Expand Up @@ -124,26 +120,8 @@ public InsightsManager getInsightsManager(String sid) {
}

@Override
public List<Subscription> getSubscriptions() {
return isSignedIn() ? authenticated.subscriptions().list() : Collections.EMPTY_LIST;
}

@Override
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() {
if (!isSignedIn()) {
return Collections.EMPTY_LIST;
}
final Tenant subscriptionTenant = authenticated.tenants().list().stream()
.filter(tenant -> StringUtils.equals(tenant.tenantId(), authenticated.tenantId()))
.findFirst().orElse(null);
if (subscriptionTenant == null) {
return Collections.EMPTY_LIST;
}
final List<Pair<Subscription, Tenant>> result = new ArrayList<>();
for (Subscription subscription : getSubscriptions()) {
result.add(new Pair<>(subscription, subscriptionTenant));
}
return result;
protected String getTenantId() {
return authenticated.tenantId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,19 @@
import com.microsoft.azure.management.resources.Tenant;
import com.microsoft.azuretools.authmanage.CommonSettings;
import com.microsoft.azuretools.authmanage.Environment;
import com.microsoft.azuretools.authmanage.RefreshableTokenCredentials;
import com.microsoft.azuretools.telemetry.TelemetryInterceptor;
import com.microsoft.azuretools.utils.Pair;
import org.apache.commons.lang3.StringUtils;
import rx.Observable;

import java.io.IOException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import static com.microsoft.azuretools.authmanage.Environment.*;

Expand All @@ -54,6 +60,7 @@ public abstract class AzureManagerBase implements AzureManager {
protected Map<String, Azure> sidToAzureMap = new ConcurrentHashMap<>();
protected Map<String, AppPlatformManager> sidToAzureSpringCloudManagerMap = new ConcurrentHashMap<>();
protected Map<String, InsightsManager> sidToInsightsManagerMap = new ConcurrentHashMap<>();
private static final Logger LOGGER = Logger.getLogger(AzureManagerBase.class.getName());

@Override
public String getPortalUrl() {
Expand Down Expand Up @@ -96,4 +103,68 @@ protected <T extends AzureConfigurable<T>> T buildAzureManager(AzureConfigurable
return configurable.withInterceptor(new TelemetryInterceptor())
.withUserAgent(CommonSettings.USER_AGENT);
}

protected abstract String getTenantId() throws IOException;

@Override
public List<Subscription> getSubscriptions() throws IOException {
List<Subscription> sl = new LinkedList<Subscription>();
// could be multi tenant - return all subscriptions for the current account
List<Tenant> tl = getTenants(getTenantId());
for (Tenant t : tl) {
sl.addAll(getSubscriptions(t.tenantId()));
}
return sl;
}

@Override
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() throws IOException {
List<Pair<Subscription, Tenant>> stl = new LinkedList<>();
for (Tenant t : getTenants(getTenantId())) {
String tid = t.tenantId();
for (Subscription s : getSubscriptions(tid)) {
stl.add(new Pair<>(s, t));
}
}
return stl;
}

private List<Subscription> getSubscriptions(String tid) {
return authTenant(tid).subscriptions().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());
return Observable.empty();
})
.toList()
.toBlocking()
.singleOrDefault(Collections.emptyList());
}

private List<Tenant> getTenants(String tid) {
return authTenant(tid).tenants().listAsync()
.onErrorResumeNext(err -> {
LOGGER.warning(err.getMessage());
return Observable.empty();
})
.toList()
.toBlocking()
.singleOrDefault(Collections.emptyList());
}

protected Azure.Authenticated authTenant(String tenantId) {
return Azure.configure()
.withInterceptor(new TelemetryInterceptor())
.withUserAgent(CommonSettings.USER_AGENT)
.authenticate(new RefreshableTokenCredentials(this, tenantId));
}

protected AppPlatformManager authSpringCloud(String subscriptionId, String tenantId) {
return buildAzureManager(AppPlatformManager.configure())
.authenticate(new RefreshableTokenCredentials(this, tenantId), subscriptionId);
}

protected InsightsManager authApplicationInsights(String subscriptionId, String tenantId) {
return buildAzureManager(InsightsManager.configure())
.authenticate(new RefreshableTokenCredentials(this, tenantId), subscriptionId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.authentication.KeyVaultCredentials;
import com.microsoft.azure.management.Azure;
import com.microsoft.azure.management.resources.Subscription;
import com.microsoft.azure.management.resources.Tenant;
import com.microsoft.azuretools.adauth.PromptBehavior;
import com.microsoft.azuretools.authmanage.AzureManagerFactory;
import com.microsoft.azuretools.authmanage.CommonSettings;
Expand All @@ -44,16 +42,13 @@
import com.microsoft.azuretools.authmanage.models.AuthMethodDetails;
import com.microsoft.azuretools.telemetry.TelemetryInterceptor;
import com.microsoft.azuretools.utils.AzureRegisterProviderNamespaces;
import com.microsoft.azuretools.utils.Pair;
import com.microsoft.rest.credentials.ServiceClientCredentials;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import static com.microsoft.azuretools.Constants.FILE_NAME_SUBSCRIPTIONS_DETAILS_SP;
Expand Down Expand Up @@ -154,21 +149,8 @@ public InsightsManager getInsightsManager(String sid) throws IOException {
}

@Override
public List<Subscription> getSubscriptions() throws IOException {
List<Subscription> sl = auth().subscriptions().list();
return sl;
}

@Override
public List<Pair<Subscription, Tenant>> getSubscriptionsWithTenant() throws IOException {
List<Pair<Subscription, Tenant>> stl = new LinkedList<>();
for (Tenant t : getTenants()) {
//String tid = t.tenantId();
for (Subscription s : getSubscriptions()) {
stl.add(new Pair<>(s, t));
}
}
return stl;
protected String getTenantId() throws IOException {
return auth().tenantId();
}

@Override
Expand All @@ -187,11 +169,6 @@ public void drop() throws IOException {
subscriptionManager.cleanSubscriptions();
}

public List<Tenant> getTenants() throws IOException {
List<Tenant> tl = auth().tenants().list();
return tl;
}

@Override
public KeyVaultClient getKeyVaultClient(String tid) {
ServiceClientCredentials creds = new KeyVaultCredentials() {
Expand Down

0 comments on commit 9e9a00b

Please sign in to comment.