Search in sources :

Example 11 with AzureProfile

use of com.azure.core.management.profile.AzureProfile in project azure-credentials-plugin by jenkinsci.

the class AzureResourceManagerRetriever method getAzureResourceManager.

private static AzureResourceManager getAzureResourceManager(AzureBaseCredentials azureCredentials, @CheckForNull String subscriptionId) {
    AzureProfile profile = new AzureProfile(azureCredentials.getAzureEnvironment());
    TokenCredential tokenCredential = AzureCredentials.getTokenCredential(azureCredentials);
    AzureResourceManager.Authenticated builder = AzureResourceManager.configure().withHttpClient(HttpClientRetriever.get()).authenticate(tokenCredential, profile);
    if (Util.fixEmpty(subscriptionId) == null) {
        return builder.withDefaultSubscription();
    }
    return builder.withSubscription(subscriptionId);
}
Also used : AzureProfile(com.azure.core.management.profile.AzureProfile) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) TokenCredential(com.azure.core.credential.TokenCredential)

Example 12 with AzureProfile

use of com.azure.core.management.profile.AzureProfile in project azure-credentials-plugin by jenkinsci.

the class AzureImdsCredentials method validate.

public boolean validate() throws AzureCredentials.ValidationException {
    try {
        final String credentialSubscriptionId = getSubscriptionId();
        AzureProfile profile = new AzureProfile(AzureEnvUtil.resolveAzureEnv(getAzureEnvName()));
        ManagedIdentityCredential credential = new ManagedIdentityCredentialBuilder().build();
        AzureResourceManager azure = AzureResourceManager.configure().withHttpClient(HttpClientRetriever.get()).authenticate(credential, profile).withSubscription(credentialSubscriptionId);
        PagedIterable<Subscription> subscriptions = azure.subscriptions().list();
        if (subscriptionId != null) {
            for (Subscription subscription : subscriptions) {
                if (subscription.subscriptionId().equalsIgnoreCase(credentialSubscriptionId)) {
                    return true;
                }
            }
        } else {
            return true;
        }
    } catch (Exception e) {
        throw new AzureCredentials.ValidationException(Messages.Azure_CantValidate() + ": " + e.getMessage(), e);
    }
    throw new AzureCredentials.ValidationException(Messages.Azure_Invalid_SubscriptionId());
}
Also used : AzureProfile(com.azure.core.management.profile.AzureProfile) ManagedIdentityCredential(com.azure.identity.ManagedIdentityCredential) AzureResourceManager(com.azure.resourcemanager.AzureResourceManager) Subscription(com.azure.resourcemanager.resources.models.Subscription) ManagedIdentityCredentialBuilder(com.azure.identity.ManagedIdentityCredentialBuilder)

Example 13 with AzureProfile

use of com.azure.core.management.profile.AzureProfile in project azure-credentials-plugin by jenkinsci.

the class IntegrationTestBase method getAzureClient.

protected static AzureResourceManager getAzureClient() {
    final TokenCredential credential = new ClientSecretCredentialBuilder().clientId(testEnv.clientId).clientSecret(testEnv.clientSecret).tenantId(testEnv.tenantId).build();
    AzureProfile profile = new AzureProfile(AzureEnvironment.AZURE);
    return AzureResourceManager.authenticate(credential, profile).withSubscription(testEnv.subscriptionId);
}
Also used : ClientSecretCredentialBuilder(com.azure.identity.ClientSecretCredentialBuilder) AzureProfile(com.azure.core.management.profile.AzureProfile) TokenCredential(com.azure.core.credential.TokenCredential)

Example 14 with AzureProfile

use of com.azure.core.management.profile.AzureProfile in project azure-maven-plugins by microsoft.

the class AzureService method getResourceManager.

@Cacheable(cacheName = "resource/{}/manager", key = "$subscriptionId")
default ResourceManager getResourceManager(String subscriptionId) {
    // make sure it is signed in.
    final IAccount account = az(IAzureAccount.class).account();
    final AzureConfiguration config = Azure.az().config();
    final String userAgent = config.getUserAgent();
    final HttpLogDetailLevel logDetailLevel = config.getLogLevel() == null ? HttpLogDetailLevel.NONE : HttpLogDetailLevel.valueOf(config.getLogLevel());
    final AzureProfile azureProfile = new AzureProfile(account.getEnvironment());
    final Providers providers = ResourceManager.configure().withHttpClient(getDefaultHttpClient()).withPolicy(getUserAgentPolicy(userAgent)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).withSubscription(subscriptionId).providers();
    return ResourceManager.configure().withHttpClient(getDefaultHttpClient()).withLogLevel(logDetailLevel).withPolicy(// set user agent with policy
    getUserAgentPolicy(userAgent)).withPolicy(// add policy to auto register resource providers
    new ProviderRegistrationPolicy(providers)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).withSubscription(subscriptionId);
}
Also used : IAccount(com.microsoft.azure.toolkit.lib.account.IAccount) IAzureAccount(com.microsoft.azure.toolkit.lib.account.IAzureAccount) HttpLogDetailLevel(com.azure.core.http.policy.HttpLogDetailLevel) ProviderRegistrationPolicy(com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy) AzureProfile(com.azure.core.management.profile.AzureProfile) Providers(com.azure.resourcemanager.resources.models.Providers) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable)

Example 15 with AzureProfile

use of com.azure.core.management.profile.AzureProfile in project azure-maven-plugins by microsoft.

the class AzureSpringCloud method getClient.

@Cacheable(cacheName = "asc/{}/client", key = "$subscriptionId")
@AzureOperation(name = "springcloud.get_client.subscription", params = "subscriptionId", type = AzureOperation.Type.SERVICE)
protected SpringServices getClient(final String subscriptionId) {
    final Account account = Azure.az(AzureAccount.class).account();
    final AzureConfiguration config = Azure.az().config();
    final String userAgent = config.getUserAgent();
    final HttpLogDetailLevel logLevel = Optional.ofNullable(config.getLogLevel()).map(HttpLogDetailLevel::valueOf).orElse(HttpLogDetailLevel.NONE);
    final AzureProfile azureProfile = new AzureProfile(null, subscriptionId, account.getEnvironment());
    return AppPlatformManager.configure().withHttpClient(AzureService.getDefaultHttpClient()).withLogLevel(logLevel).withPolicy(// set user agent with policy
    getUserAgentPolicy(userAgent)).authenticate(account.getTokenCredential(subscriptionId), azureProfile).springServices();
}
Also used : Account(com.microsoft.azure.toolkit.lib.auth.Account) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) AzureConfiguration(com.microsoft.azure.toolkit.lib.AzureConfiguration) HttpLogDetailLevel(com.azure.core.http.policy.HttpLogDetailLevel) AzureProfile(com.azure.core.management.profile.AzureProfile) AzureAccount(com.microsoft.azure.toolkit.lib.auth.AzureAccount) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

AzureProfile (com.azure.core.management.profile.AzureProfile)17 HttpLogDetailLevel (com.azure.core.http.policy.HttpLogDetailLevel)9 TokenCredential (com.azure.core.credential.TokenCredential)8 AzureConfiguration (com.microsoft.azure.toolkit.lib.AzureConfiguration)8 Account (com.microsoft.azure.toolkit.lib.auth.Account)8 AzureAccount (com.microsoft.azure.toolkit.lib.auth.AzureAccount)8 Cacheable (com.microsoft.azure.toolkit.lib.common.cache.Cacheable)8 AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)3 HttpLogOptions (com.azure.core.http.policy.HttpLogOptions)2 ProviderRegistrationPolicy (com.azure.resourcemanager.resources.fluentcore.policy.ProviderRegistrationPolicy)2 Providers (com.azure.resourcemanager.resources.models.Providers)2 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)2 ClientSecretCredentialBuilder (com.azure.identity.ClientSecretCredentialBuilder)1 ManagedIdentityCredential (com.azure.identity.ManagedIdentityCredential)1 ManagedIdentityCredentialBuilder (com.azure.identity.ManagedIdentityCredentialBuilder)1 ComputeManager (com.azure.resourcemanager.compute.ComputeManager)1 RelayManager (com.azure.resourcemanager.relay.RelayManager)1 ResourceManager (com.azure.resourcemanager.resources.ResourceManager)1 Subscription (com.azure.resourcemanager.resources.models.Subscription)1 StorageManager (com.azure.resourcemanager.storage.StorageManager)1