Java Code Examples for com.microsoft.azure.management.Azure#Authenticated

The following examples show how to use com.microsoft.azure.management.Azure#Authenticated . 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: ManageScaleSetUserAssignedMSIFromServicePrincipal.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 {
        //=============================================================
        // Authenticate

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

        Azure.Authenticated authenticated = Azure.configure()
                .withLogLevel(LogLevel.BODY_AND_HEADERS)
                .authenticate(credFile);

        runSample(authenticated);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 2
Source File: ManageUsersGroupsAndRoles.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.BODY)
                .authenticate(credentials);

        runSample(authenticated, credentials.defaultSubscriptionId());
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 3
Source File: ManageServicePrincipal.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"));

        Azure.Authenticated authenticated = Azure.configure()
                .withLogLevel(LogLevel.BODY)
                .authenticate(credFile);
        String defaultSubscriptionId = authenticated.subscriptions().list().get(0).subscriptionId();
        runSample(authenticated, defaultSubscriptionId);
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
}
 
Example 4
Source File: ManageServicePrincipal.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
/**
 * Main function which runs the actual sample.
 * @param authenticated instance of Authenticated
 * @param defaultSubscriptionId default subscription id
 * @return true if sample runs successfully
 */
public static boolean runSample(Azure.Authenticated authenticated, String defaultSubscriptionId) {
    ActiveDirectoryApplication activeDirectoryApplication = null;

    try {
        String authFileName = "myAuthFile.azureauth";
        String authFilePath = Paths.get(getBasePath(), authFileName).toString();

        activeDirectoryApplication =
                createActiveDirectoryApplication(authenticated);

        ServicePrincipal servicePrincipal =
                createServicePrincipalWithRoleForApplicationAndExportToFile(
                        authenticated,
                        activeDirectoryApplication,
                        BuiltInRole.CONTRIBUTOR,
                        defaultSubscriptionId,
                        authFilePath);

        SdkContext.sleep(15000);

        useAuthFile(authFilePath);

        manageApplication(authenticated, activeDirectoryApplication);

        manageServicePrincipal(authenticated, servicePrincipal);

        return true;
    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        if (activeDirectoryApplication != null) {
            // this will delete Service Principal as well
            authenticated.activeDirectoryApplications().deleteById(activeDirectoryApplication.id());
        }
    }

    return false;
}
 
Example 5
Source File: ManageServicePrincipal.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static ActiveDirectoryApplication createActiveDirectoryApplication(Azure.Authenticated authenticated) throws Exception {
    String name = SdkContext.randomResourceName("adapp-sample", 20);
    //create a self-sighed certificate
    String domainName = name + ".com";
    // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
    String certPassword = "StrongPass!12";
    Certificate certificate = Certificate.createSelfSigned(domainName, certPassword);

    // create Active Directory application
    ActiveDirectoryApplication activeDirectoryApplication = authenticated.activeDirectoryApplications()
            .define(name)
                .withSignOnUrl("https://github.com/Azure/azure-sdk-for-java/" + name)
                // password credentials definition
                .definePasswordCredential("password")
                    // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
                    .withPasswordValue("P@ssw0rd")
                    .withDuration(Duration.standardDays(700))
                    .attach()
                // certificate credentials definition
                .defineCertificateCredential("cert")
                    .withAsymmetricX509Certificate()
                    .withPublicKey(Files.readAllBytes(Paths.get(certificate.getCerPath())))
                    .withDuration(Duration.standardDays(100))
                    .attach()
                .create();
    System.out.println(activeDirectoryApplication.id() + " - " + activeDirectoryApplication.applicationId());
    return activeDirectoryApplication;
}
 
Example 6
Source File: ManageServicePrincipal.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static ServicePrincipal createServicePrincipalWithRoleForApplicationAndExportToFile(
        Azure.Authenticated authenticated,
        ActiveDirectoryApplication activeDirectoryApplication,
        BuiltInRole role,
        String subscriptionId,
        String authFilePath) throws Exception {

    String name = SdkContext.randomResourceName("sp-sample", 20);
    //create a self-sighed certificate
    String domainName = name + ".com";
    // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
    String certPassword = "StrongPass!12";
    Certificate certificate = Certificate.createSelfSigned(domainName, certPassword);

    // create  a Service Principal and assign it to a subscription with the role Contributor
    return authenticated.servicePrincipals()
            .define("name")
                .withExistingApplication(activeDirectoryApplication)
                // password credentials definition
                .definePasswordCredential("ServicePrincipalAzureSample")
                // [SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Serves as an example, not for deployment. Please change when using this in your code.")]
                    .withPasswordValue("StrongPass!12")
                    .attach()
                // certificate credentials definition
                .defineCertificateCredential("spcert")
                    .withAsymmetricX509Certificate()
                    .withPublicKey(Files.readAllBytes(Paths.get(certificate.getCerPath())))
                    .withDuration(Duration.standardDays(7))
                    // export the credentials to the file
                    .withAuthFileToExport(new FileOutputStream(authFilePath))
                    .withPrivateKeyFile(certificate.getPfxPath())
                    .withPrivateKeyPassword(certPassword)
                    .attach()
            .withNewRoleInSubscription(role, subscriptionId)
            .create();
}
 
Example 7
Source File: ManageServicePrincipal.java    From azure-libraries-for-java with MIT License 5 votes vote down vote up
private static void manageApplication(Azure.Authenticated authenticated, ActiveDirectoryApplication activeDirectoryApplication) {
    activeDirectoryApplication.update()
            // add another password credentials
            .definePasswordCredential("password-1")
            .withPasswordValue("P@ssw0rd-1")
            .withDuration(Duration.standardDays(700))
            .attach()
            // add a reply url
            .withReplyUrl("http://localhost:8080")
            .apply();
}
 
Example 8
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 9
Source File: ManageServicePrincipal.java    From azure-libraries-for-java with MIT License 4 votes vote down vote up
private static void manageServicePrincipal(Azure.Authenticated authenticated, ServicePrincipal servicePrincipal) {
    servicePrincipal.update()
            .withoutCredential("ServicePrincipalAzureSample")
            .withoutRole(servicePrincipal.roleAssignments().iterator().next())
            .apply();
}