Java Code Examples for com.microsoft.azure.credentials.ApplicationTokenCredentials#fromFile()

The following examples show how to use com.microsoft.azure.credentials.ApplicationTokenCredentials#fromFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AzureFileSystemBehaviorITCase.java    From flink with Apache License 2.0 6 votes vote down vote up
private static boolean isHttpsTrafficOnly() throws IOException {
	if (StringUtils.isNullOrWhitespaceOnly(RESOURCE_GROUP) || StringUtils.isNullOrWhitespaceOnly(TOKEN_CREDENTIALS_FILE)) {
		// default to https only, as some fields are missing
		return true;
	}

	Assume.assumeTrue("Azure storage account not configured, skipping test...", !StringUtils.isNullOrWhitespaceOnly(ACCOUNT));

	AzureTokenCredentials credentials = ApplicationTokenCredentials.fromFile(new File(TOKEN_CREDENTIALS_FILE));
	Azure azure =
		StringUtils.isNullOrWhitespaceOnly(SUBSCRIPTION_ID) ?
			Azure.authenticate(credentials).withDefaultSubscription() :
			Azure.authenticate(credentials).withSubscription(SUBSCRIPTION_ID);

	return azure.storageAccounts().getByResourceGroup(RESOURCE_GROUP, ACCOUNT).inner().enableHttpsTrafficOnly();
}
 
Example 2
Source File: ManageSqlFailoverGroups.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 3
Source File: ManageSqlDatabasesAcrossDifferentDataCenters.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {

        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
   } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 4
Source File: ManageSqlFirewallRules.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 5
Source File: ManageSqlWithRecoveredOrRestoredDatabase.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {

        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 6
Source File: ManageSqlVirtualNetworkRules.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 7
Source File: ManageSqlImportExportDatabase.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {

        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));

        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 8
Source File: GettingSqlServerMetrics.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 9
Source File: ManageSqlServerDnsAliases.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 10
Source File: ManageSqlServerKeysWithAzureKeyVaultKey.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure, credentials.clientId());
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 11
Source File: ManageSqlDatabaseInElasticPool.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
   } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 12
Source File: KeyTests.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
private Vault createVault() throws Exception {
    String vaultName = SdkContext.randomResourceName("vault", 20);

    ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(new File(System.getenv("AZURE_AUTH_LOCATION")));

    Vault vault = keyVaultManager.vaults().define(vaultName)
            .withRegion(Region.US_WEST)
            .withNewResourceGroup(RG_NAME)
            .defineAccessPolicy()
                .forServicePrincipal(credentials.clientId())
                .allowKeyAllPermissions()
                .attach()
            .create();

    Assert.assertNotNull(vault);

    SdkContext.sleep(10000);

    return vault;
}
 
Example 13
Source File: ManageSqlServerSecurityAlertPolicy.java    From azure-libraries-for-java with MIT License 6 votes vote down vote up
/**
 * Main entry point.
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));


        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        RestClient restClient = new RestClient.Builder()
            .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withReadTimeout(150, TimeUnit.SECONDS)
            .withLogLevel(LogLevel.BODY)
            .withCredentials(credentials).build();
        Azure azure = Azure.authenticate(restClient, credentials.domain(), credentials.defaultSubscriptionId()).withDefaultSubscription();

        // Print selected subscription
        System.out.println("Selected subscription: " + azure.subscriptionId());

        runSample(azure);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 14
Source File: ManageServicePrincipalCredentials.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
/**
 * Main entry point.
 *
 * @param args the parameters
 */
public static void main(String[] args) {
    try {
        final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
        ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credFile);
        Azure.Authenticated authenticated = Azure.configure()
                .withLogLevel(LogLevel.BASIC)
                .authenticate(credentials);

        runSample(authenticated, credentials.defaultSubscriptionId(), credentials.environment());
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 15
Source File: AzureAccount.java    From clouditor with Apache License 2.0 5 votes vote down vote up
private static AzureTokenCredentials defaultCredentialProviderChain() throws IOException {
  // check if the default credentials-file exists
  var credentialsFile = new File(defaultAuthFile());

  if (credentialsFile.exists()) {
    LOGGER.info("Using default credentials file {}", credentialsFile);
    return ApplicationTokenCredentials.fromFile(credentialsFile);
  } else {
    // otherwise, use default locations
    LOGGER.info("Did not find default credentials. Trying to use AzureCLI credentials instead.");
    return AzureCliCredentials.create();
  }
}
 
Example 16
Source File: ApplicationGatewayTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Test
@Ignore("Need client id for key vault usage")
public void canCreateApplicationGatewayWithSecret() throws Exception {
    String appGatewayName = SdkContext.randomResourceName("agwaf", 15);
    String appPublicIp = SdkContext.randomResourceName("pip", 15);
    String identityName = SdkContext.randomResourceName("id", 10);

    PublicIPAddress pip = networkManager.publicIPAddresses()
            .define(appPublicIp)
            .withRegion(Region.US_EAST)
            .withNewResourceGroup(RG_NAME)
            .withSku(PublicIPSkuType.STANDARD)
            .withStaticIP()
            .create();

    ApplicationTokenCredentials credentials = ApplicationTokenCredentials
            .fromFile(new File(System.getenv("AZURE_AUTH_LOCATION")));
    
    Identity identity = msiManager.identities()
           .define(identityName)
           .withRegion(Region.US_EAST)
           .withExistingResourceGroup(RG_NAME)
           .create();

    Assert.assertNotNull(identity.name());
    Assert.assertNotNull(identity.principalId());

    Secret secret1 = createKeyVaultSecret(credentials.clientId(), identity.principalId());
    Secret secret2 = createKeyVaultSecret(credentials.clientId(), identity.principalId());

    ManagedServiceIdentity serviceIdentity = createManagedServiceIdentityFromIdentity(identity);

    ApplicationGateway appGateway = networkManager.applicationGateways().define(appGatewayName)
            .withRegion(Region.US_EAST)
            .withExistingResourceGroup(RG_NAME)
            // Request routing rules
            .defineRequestRoutingRule("rule1")
            .fromPublicFrontend()
            .fromFrontendHttpsPort(443)
            .withSslCertificate("ssl1")
            .toBackendHttpPort(8080)
            .toBackendIPAddress("11.1.1.1")
            .toBackendIPAddress("11.1.1.2")
            .attach()
            .withIdentity(serviceIdentity)
            .defineSslCertificate("ssl1")
                    .withKeyVaultSecretId(secret1.id())
                    .attach()
            .withExistingPublicIPAddress(pip)
            .withTier(ApplicationGatewayTier.WAF_V2)
            .withSize(ApplicationGatewaySkuName.WAF_V2)
            .withAutoScale(2, 5)
            .withWebApplicationFirewall(true, ApplicationGatewayFirewallMode.PREVENTION)
            .create();

    Assert.assertEquals(secret1.id(), appGateway.sslCertificates().get("ssl1").keyVaultSecretId());
    Assert.assertEquals(secret1.id(), appGateway.requestRoutingRules().get("rule1").sslCertificate().keyVaultSecretId());

    appGateway = appGateway.update()
            .defineSslCertificate("ssl2")
                .withKeyVaultSecretId(secret2.id())
                .attach()
            .apply();

    Assert.assertEquals(secret2.id(), appGateway.sslCertificates().get("ssl2").keyVaultSecretId());
}
 
Example 17
Source File: SecretTests.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
@Ignore("Mock framework doesn't support data plane")
public void canCRUDSecret() throws Exception {
    String vaultName = SdkContext.randomResourceName("vault", 20);
    String secretName = SdkContext.randomResourceName("secret", 20);

    ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(new File(System.getenv("AZURE_AUTH_LOCATION")));

    Vault vault = keyVaultManager.vaults().define(vaultName)
            .withRegion(Region.US_WEST)
            .withNewResourceGroup(RG_NAME)
            .defineAccessPolicy()
                .forServicePrincipal(credentials.clientId())
                .allowSecretAllPermissions()
                .attach()
            .create();

    Assert.assertNotNull(vault);

    SdkContext.sleep(10000);

    Secret secret = vault.secrets().define(secretName)
            .withValue("Some secret value")
            .create();

    Assert.assertNotNull(secret);
    Assert.assertNotNull(secret.id());
    Assert.assertEquals("Some secret value", secret.value());

    secret = secret.update()
            .withValue("Some updated value")
            .apply();

    Assert.assertEquals("Some updated value", secret.value());

    List<Secret> versions = secret.listVersions();

    int count = 2;
    for (Secret version : versions) {
        if ("Some secret value".equals(version.value())) {
            count --;
        }
        if ("Some updated value".equals(version.value())) {
            count --;
        }
    }
    Assert.assertEquals(0, count);

}
 
Example 18
Source File: Azure.java    From azure-libraries-for-java with MIT License 3 votes vote down vote up
/**
 * Authenticates API access using a properties file containing the required credentials.
 * @param credentialsFile the file containing the credentials in the standard Java properties file format,
 * with the following keys:<p>
 * <code>
    *   subscription= #subscription ID<br>
    *   tenant= #tenant ID<br>
    *   client= #client id<br>
    *   key= #client key<br>
    *   managementURI= #management URI<br>
    *   baseURL= #base URL<br>
    *   authURL= #authentication URL<br>
 *</code>
 * @return authenticated Azure client
 * @throws IOException exception thrown from file access
 */
public static Authenticated authenticate(File credentialsFile) throws IOException {
    ApplicationTokenCredentials credentials = ApplicationTokenCredentials.fromFile(credentialsFile);
    return new AuthenticatedImpl(new RestClient.Builder()
            .withBaseUrl(credentials.environment(), AzureEnvironment.Endpoint.RESOURCE_MANAGER)
            .withCredentials(credentials)
            .withSerializerAdapter(new AzureJacksonAdapter())
            .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
            .withInterceptor(new ProviderRegistrationInterceptor(credentials))
            .withInterceptor(new ResourceManagerThrottlingInterceptor())
            .build(), credentials.domain()).withDefaultSubscription(credentials.defaultSubscriptionId());
}
 
Example 19
Source File: CbDelegatedTokenCredentials.java    From cloudbreak with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance of the DelegatedTokenCredentials from an auth file.
 *
 * @param authFile The credentials based on the file
 * @param redirectUrl the URL to redirect to after authentication in Active Directory
 * @return a new delegated token credentials
 * @throws IOException exception thrown from file access errors.
 */
public static com.microsoft.azure.credentials.DelegatedTokenCredentials fromFile(File authFile, String redirectUrl) throws IOException {
    return new com.microsoft.azure.credentials.DelegatedTokenCredentials(ApplicationTokenCredentials.fromFile(authFile), redirectUrl);
}
 
Example 20
Source File: CbDelegatedTokenCredentials.java    From cloudbreak with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new instance of the DelegatedTokenCredentials from an auth file,
 * with a pre-acquired oauth2 authorization code.
 *
 * @param authFile The credentials based on the file
 * @param redirectUrl the URL to redirect to after authentication in Active Directory
 * @param authorizationCode the oauth2 authorization code
 * @return a new delegated token credentials
 * @throws IOException exception thrown from file access errors.
 */
public static com.microsoft.azure.credentials.DelegatedTokenCredentials fromFile(File authFile, String redirectUrl, String authorizationCode)
        throws IOException {
    return new com.microsoft.azure.credentials.DelegatedTokenCredentials(ApplicationTokenCredentials.fromFile(authFile), redirectUrl, authorizationCode);
}