Search in sources :

Example 1 with ManagedIdentityCredential

use of com.azure.identity.ManagedIdentityCredential 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)

Aggregations

AzureProfile (com.azure.core.management.profile.AzureProfile)1 ManagedIdentityCredential (com.azure.identity.ManagedIdentityCredential)1 ManagedIdentityCredentialBuilder (com.azure.identity.ManagedIdentityCredentialBuilder)1 AzureResourceManager (com.azure.resourcemanager.AzureResourceManager)1 Subscription (com.azure.resourcemanager.resources.models.Subscription)1