Search in sources :

Example 16 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project azure-sdk-for-java by Azure.

the class DataLakeAnalyticsManagementTestBase method initializeClients.

@Override
protected void initializeClients(RestClient restClient, String defaultSubscription, String domain) {
    rgName = generateRandomResourceName("adlarg", 15);
    adlsName = generateRandomResourceName("adls", 15);
    jobAndCatalogAdlaName = generateRandomResourceName("secondadla", 15);
    environmentLocation = Region.US_EAST2;
    dataLakeAnalyticsAccountManagementClient = new DataLakeAnalyticsAccountManagementClientImpl(restClient).withSubscriptionId(defaultSubscription);
    // TODO: in the future this needs to be dynamic depending on the Azure environment
    // the tests are running in.
    String adlaSuffix = "azuredatalakeanalytics.net";
    addTextReplacementRule("https://(.*)." + adlaSuffix, this.mockUri());
    // Generate creds and a set of rest clients for catalog and job
    ApplicationTokenCredentials credentials = new AzureTestCredentials();
    if (IS_RECORD) {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        try {
            credentials = ApplicationTokenCredentials.fromFile(credFile);
        } catch (IOException e) {
            Assert.fail("Failed to read credentials from file: " + credFile + " with error: " + e.getMessage());
        }
    }
    if (IS_RECORD) {
        RestClient restClientWithTimeout = buildRestClient(new RestClient.Builder().withConnectionTimeout(5, TimeUnit.MINUTES).withBaseUrl("https://{accountName}.{adlaJobDnsSuffix}").withCredentials(credentials).withLogLevel(LogLevel.BODY_AND_HEADERS).withNetworkInterceptor(this.interceptor()), IS_MOCKED);
        dataLakeAnalyticsJobManagementClient = new DataLakeAnalyticsJobManagementClientImpl(restClientWithTimeout).withAdlaJobDnsSuffix(adlaSuffix);
        RestClient catalogRestClient = buildRestClient(new RestClient.Builder().withBaseUrl("https://{accountName}.{adlaCatalogDnsSuffix}").withCredentials(credentials).withLogLevel(LogLevel.BODY_AND_HEADERS).withNetworkInterceptor(this.interceptor()), IS_MOCKED);
        dataLakeAnalyticsCatalogManagementClient = new DataLakeAnalyticsCatalogManagementClientImpl(catalogRestClient).withAdlaCatalogDnsSuffix(adlaSuffix);
    } else {
        // for mocked clients, we can just use the basic rest client, since the DNS is replaced.
        dataLakeAnalyticsCatalogManagementClient = new DataLakeAnalyticsCatalogManagementClientImpl(restClient);
        dataLakeAnalyticsJobManagementClient = new DataLakeAnalyticsJobManagementClientImpl(restClient);
    }
    resourceManagementClient = ResourceManager.authenticate(restClient).withSubscription(defaultSubscription);
    dataLakeStoreAccountManagementClient = new DataLakeStoreAccountManagementClientImpl(restClient).withSubscriptionId(defaultSubscription);
    storageManagementClient = StorageManager.authenticate(restClient, defaultSubscription);
    // create the resource group, ADLS account and ADLA account for job and catalog use.
    resourceManagementClient.resourceGroups().define(rgName).withRegion(environmentLocation).create();
    DataLakeStoreAccount createParams = new DataLakeStoreAccount();
    createParams.withLocation(environmentLocation.name());
    dataLakeStoreAccountManagementClient.accounts().create(rgName, adlsName, createParams);
    List<DataLakeStoreAccountInfo> adlsAccts = new ArrayList<DataLakeStoreAccountInfo>();
    DataLakeStoreAccountInfo adlsInfo = new DataLakeStoreAccountInfo();
    adlsInfo.withName(adlsName);
    adlsAccts.add(adlsInfo);
    DataLakeAnalyticsAccount adlaCreateParams = new DataLakeAnalyticsAccount();
    adlaCreateParams.withLocation(environmentLocation.name());
    adlaCreateParams.withDataLakeStoreAccounts(adlsAccts);
    adlaCreateParams.withDefaultDataLakeStoreAccount(adlsName);
    dataLakeAnalyticsAccountManagementClient.accounts().create(rgName, jobAndCatalogAdlaName, adlaCreateParams);
}
Also used : DataLakeStoreAccount(com.microsoft.azure.management.datalake.store.models.DataLakeStoreAccount) DataLakeAnalyticsJobManagementClientImpl(com.microsoft.azure.management.datalake.analytics.implementation.DataLakeAnalyticsJobManagementClientImpl) RestClient(com.microsoft.rest.RestClient) ArrayList(java.util.ArrayList) DataLakeStoreAccountManagementClientImpl(com.microsoft.azure.management.datalake.store.implementation.DataLakeStoreAccountManagementClientImpl) IOException(java.io.IOException) DataLakeAnalyticsAccountManagementClientImpl(com.microsoft.azure.management.datalake.analytics.implementation.DataLakeAnalyticsAccountManagementClientImpl) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) DataLakeAnalyticsCatalogManagementClientImpl(com.microsoft.azure.management.datalake.analytics.implementation.DataLakeAnalyticsCatalogManagementClientImpl) File(java.io.File) AzureTestCredentials(com.microsoft.azure.management.resources.core.AzureTestCredentials)

Example 17 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzureRemoteCleanup method setUp.

@Before
public void setUp() throws Exception {
    try {
        this.credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
        this.azureClient = Azure.configure().authenticate(this.credentials).withSubscription(this.subscriptionId);
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) IOException(java.io.IOException) Before(org.junit.Before)

Example 18 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class TestAzureLongRunningEnumeration method setUp.

@Override
@Before
public void setUp() throws Exception {
    for (int i = 0; i < numOfVMsToTest; i++) {
        String azureName = generateName(azureVMNamePrefix);
        azureVMNames.add(azureName);
        nicSpecs.add(initializeNicSpecs(azureName, false, true, false));
    }
    try {
        /*
             * Init Class-specific (shared between test runs) vars.
             *
             * NOTE: Ultimately this should go to @BeforeClass, BUT BasicReusableHostTestCase.HOST
             * is not accessible.
             */
        if (computeHost == null) {
            PhotonModelServices.startServices(this.host);
            PhotonModelTaskServices.startServices(this.host);
            PhotonModelAdaptersRegistryAdapters.startServices(this.host);
            AzureAdaptersTestUtils.startServicesSynchronouslyAzure(this.host);
            this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
            this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
            // TODO: VSYM-992 - improve test/fix arbitrary timeout
            this.host.setTimeoutSeconds(this.timeoutSeconds);
            // Create a resource pool where the VMs will be housed
            ResourcePoolState resourcePool = createDefaultResourcePool(this.host);
            AuthCredentialsServiceState authCredentials = createDefaultAuthCredentials(this.host, this.clientID, this.clientKey, this.subscriptionId, this.tenantId);
            endpointState = createDefaultEndpointState(this.host, authCredentials.documentSelfLink);
            // create a compute host for the Azure
            computeHost = createDefaultComputeHost(this.host, resourcePool.documentSelfLink, endpointState);
            endpointState.computeHostLink = computeHost.documentSelfLink;
        }
        this.host.waitForServiceAvailable(PhotonModelServices.LINKS);
        this.host.waitForServiceAvailable(PhotonModelTaskServices.LINKS);
        this.nodeStatsUri = UriUtils.buildUri(this.host.getUri(), ServiceUriPaths.CORE_MANAGEMENT);
        this.maxMemoryInMb = this.host.getState().systemInfo.maxMemoryByteCount / BYTES_TO_MB;
        internalTagResourcesMap.put(NetworkState.class, NETWORK_TAG_TYPE_VALUE);
        internalTagResourcesMap.put(SubnetState.class, SUBNET_TAG_TYPE_VALUE);
        internalTagResourcesMap.put(NetworkInterfaceState.class, NETWORK_INTERFACE_TAG_TYPE_VALUE);
        if (!this.isMock) {
            ApplicationTokenCredentials credentials = new ApplicationTokenCredentials(this.clientID, this.tenantId, this.clientKey, AzureEnvironment.AZURE);
            this.computeManagementClient = new ComputeManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
            this.resourceManagementClient = new ResourceManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
            this.storageManagementClient = new StorageManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
            this.networkManagementClient = new NetworkManagementClientImpl(credentials).withSubscriptionId(this.subscriptionId);
        }
    } catch (Throwable e) {
        throw new Exception(e);
    }
}
Also used : NetworkManagementClientImpl(com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl) ResourcePoolState(com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState) AuthCredentialsServiceState(com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState) StorageManagementClientImpl(com.microsoft.azure.management.storage.implementation.StorageManagementClientImpl) ComputeManagementClientImpl(com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials) AzureTestUtil.randomString(com.vmware.photon.controller.model.adapters.azure.instance.AzureTestUtil.randomString) ResourceManagementClientImpl(com.microsoft.azure.management.resources.implementation.ResourceManagementClientImpl) Before(org.junit.Before)

Example 19 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project photon-model by vmware.

the class AzureUtils method getAzureConfig.

/**
 * Configures authentication credential for Azure.
 */
public static ApplicationTokenCredentials getAzureConfig(AuthCredentialsServiceState parentAuth) {
    String clientId = parentAuth.privateKeyId;
    String clientKey = EncryptionUtils.decrypt(parentAuth.privateKey);
    String tenantId = parentAuth.customProperties.get(AzureConstants.AZURE_TENANT_ID);
    AzureEnvironment azureEnvironment = AzureEnvironment.AZURE;
    if (AzureUtils.isAzureClientMock()) {
        azureEnvironment.endpoints().put(AzureEnvironment.Endpoint.ACTIVE_DIRECTORY.toString(), AzureUtils.getAzureBaseUri());
    }
    return new ApplicationTokenCredentials(clientId, tenantId, clientKey, azureEnvironment);
}
Also used : AzureEnvironment(com.microsoft.azure.AzureEnvironment) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials)

Example 20 with ApplicationTokenCredentials

use of com.microsoft.azure.credentials.ApplicationTokenCredentials in project azure-gradle-plugins by lenala.

the class AzureAuthHelper method getAppTokenCredentials.

/**
 * Get ApplicationTokenCredentials from authentication settings in gradle.properties.
 *
 * @return ApplicationTokenCredentials object if configuration is correct; otherwise return null.
 */
private ApplicationTokenCredentials getAppTokenCredentials() {
    final String clientId = config.getAuthenticationSetting(CLIENT_ID);
    if (StringUtils.isEmpty(clientId)) {
        logger.quiet(CLIENT_ID_NOT_CONFIG);
        return null;
    }
    final String tenantId = config.getAuthenticationSetting(TENANT_ID);
    if (StringUtils.isEmpty(tenantId)) {
        logger.quiet(TENANT_ID_NOT_CONFIG);
        return null;
    }
    final String environment = config.getAuthenticationSetting(ENVIRONMENT);
    final AzureEnvironment azureEnvironment = getAzureEnvironment(environment);
    logger.quiet("Azure Management Endpoint: " + azureEnvironment.managementEndpoint());
    final String key = config.getAuthenticationSetting(KEY);
    if (!StringUtils.isEmpty(key)) {
        logger.quiet(USE_KEY_TO_AUTH);
        return new ApplicationTokenCredentials(clientId, tenantId, key, azureEnvironment);
    } else {
        logger.quiet(KEY_NOT_CONFIG);
    }
    final String certificate = config.getAuthenticationSetting(CERTIFICATE);
    if (StringUtils.isEmpty(certificate)) {
        logger.quiet(CERTIFICATE_FILE_NOT_CONFIG);
        return null;
    }
    final String certificatePassword = config.getAuthenticationSetting(CERTIFICATE_PASSWORD);
    try {
        byte[] cert;
        cert = Files.readAllBytes(Paths.get(certificate, new String[0]));
        logger.quiet(USE_CERTIFICATE_TO_AUTH + certificate);
        return new ApplicationTokenCredentials(clientId, tenantId, cert, certificatePassword, azureEnvironment);
    } catch (Exception e) {
        logger.quiet(CERTIFICATE_FILE_READ_FAIL + certificate);
    }
    return null;
}
Also used : AzureEnvironment(com.microsoft.azure.AzureEnvironment) ApplicationTokenCredentials(com.microsoft.azure.credentials.ApplicationTokenCredentials)

Aggregations

ApplicationTokenCredentials (com.microsoft.azure.credentials.ApplicationTokenCredentials)27 File (java.io.File)9 Azure (com.microsoft.azure.management.Azure)8 RestClient (com.microsoft.rest.RestClient)7 Before (org.junit.Before)7 AzureResponseBuilder (com.microsoft.azure.AzureResponseBuilder)6 AzureJacksonAdapter (com.microsoft.azure.serializer.AzureJacksonAdapter)6 ResourcePoolState (com.vmware.photon.controller.model.resources.ResourcePoolService.ResourcePoolState)5 AuthCredentialsServiceState (com.vmware.xenon.services.common.AuthCredentialsService.AuthCredentialsServiceState)5 AzureEnvironment (com.microsoft.azure.AzureEnvironment)4 ComputeManagementClientImpl (com.microsoft.azure.management.compute.implementation.ComputeManagementClientImpl)4 IOException (java.io.IOException)4 NetworkManagementClientImpl (com.microsoft.azure.management.network.implementation.NetworkManagementClientImpl)3 ResourceManagementClientImpl (com.microsoft.azure.management.resources.implementation.ResourceManagementClientImpl)3 Parameters (org.testng.annotations.Parameters)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Authenticated (com.microsoft.azure.management.Azure.Authenticated)2 DataLakeStoreAccountManagementClientImpl (com.microsoft.azure.management.datalake.store.implementation.DataLakeStoreAccountManagementClientImpl)2 DataLakeStoreAccount (com.microsoft.azure.management.datalake.store.models.DataLakeStoreAccount)2 NetworkRequest (com.sequenceiq.cloudbreak.api.model.NetworkRequest)2