/** * Creates an instance of AzureCliCredentials with the default Azure CLI configuration. * * @return an instance of AzureCliCredentials * @throws IOException if the Azure CLI token files are not accessible */ public static AzureCliCredentials create() throws IOException { return create( Paths.get(System.getProperty("user.home"), ".azure", "azureProfile.json").toFile(), Paths.get(System.getProperty("user.home"), ".azure", "accessTokens.json").toFile()); }
/** * Creates an instance of AzureCliCredentials with custom locations of the token files. * * @param azureProfile the azureProfile.json file created by Azure CLI * @param accessTokens the accessTokens.json file created by Azure CLI * @return an instance of AzureCliCredentials * @throws IOException if the Azure CLI token files are not accessible */ public static AzureCliCredentials create(File azureProfile, File accessTokens) throws IOException { AzureCliCredentials credentials = new AzureCliCredentials(); credentials.azureProfile = azureProfile; credentials.accessTokens = accessTokens; credentials.loadAccessTokens(); return credentials; }
@Override public synchronized String getToken(String resource) throws IOException { String token = subscriptions.get(defaultSubscriptionId()).credentialInstance().getToken(resource); if (token == null) { System.err.println("Please login in Azure CLI and press any key to continue after you've successfully logged in."); System.in.read(); loadAccessTokens(); token = subscriptions.get(defaultSubscriptionId()).credentialInstance().getToken(resource); } return token; } }
/** * @return the active directory application client id */ public String clientId() { return subscriptions.get(defaultSubscriptionId()).clientId(); }
private synchronized void loadAccessTokens() throws IOException { try { AzureCliSubscription.Wrapper wrapper = MAPPER.readValue(azureProfile, AzureCliSubscription.Wrapper.class); List<AzureCliToken> tokens = MAPPER.readValue(accessTokens, new TypeReference<List<AzureCliToken>>() { }); while (wrapper == null || tokens == null || tokens.isEmpty() || wrapper.subscriptions == null || wrapper.subscriptions.isEmpty()) { System.err.println("Please login in Azure CLI and press any key to continue after you've successfully logged in."); System.in.read(); wrapper = MAPPER.readValue(azureProfile, AzureCliSubscription.Wrapper.class); tokens = MAPPER.readValue(accessTokens, new TypeReference<List<AzureCliToken>>() { }); } for (AzureCliSubscription subscription : wrapper.subscriptions) { for (AzureCliToken token : tokens) { // Find match of user and tenant if (subscription.isServicePrincipal() == token.isServicePrincipal() && subscription.userName().equalsIgnoreCase(token.user()) && subscription.tenant().equalsIgnoreCase(token.tenant())) { subscriptions.put(subscription.id(), subscription.withToken(token)); if (subscription.isDefault()) { withDefaultSubscriptionId(subscription.id()); } } } } } catch (IOException e) { System.err.println(String.format("Cannot read files %s and %s. Are you logged in Azure CLI?", azureProfile.getAbsolutePath(), accessTokens.getAbsolutePath())); throw e; } }
/** * @return the Azure environment to authenticate with */ public AzureEnvironment environment() { return subscriptions.get(defaultSubscriptionId()).environment(); }
/** * Get Authenticated object using authentication file from Azure CLI 2.0 * * Note: The integrated Azure CLI in Azure Cloud Shell does not have the accessToken.json, * so we need to use MSI to authenticate in the Cloud Shell. * * @return Authenticated object if Azure CLI 2.0 is logged in correctly; otherwise return null. */ protected Authenticated getAuthObjFromAzureCli() { try { final Azure.Configurable azureConfigurable = azureConfigure(); final Authenticated auth; if (isInCloudShell()) { getLog().info(AUTH_WITH_MSI); auth = azureConfigurable.authenticate(new MSICredentials()); } else { getLog().info(AUTH_WITH_AZURE_CLI); auth = azureConfigurable.authenticate(AzureCliCredentials.create()); } return auth; } catch (Exception e) { getLog().debug(AZURE_CLI_AUTH_FAIL); getLog().debug(e); } return null; }
/** * @return the tenant or domain the containing the application */ @Override public String domain() { return subscriptions.get(defaultSubscriptionId()).tenant(); }
/** * Get Authenticated object using authentication file from Azure CLI 2.0 * * Note: The integrated Azure CLI in Azure Cloud Shell does not have the accessToken.json, * so we need to use MSI to authenticate in the Cloud Shell. * * @return Authenticated object if Azure CLI 2.0 is logged in correctly; otherwise return null. */ protected Authenticated getAuthObjFromAzureCli() { try { final Azure.Configurable azureConfigurable = azureConfigure(); final Authenticated auth; if (isInCloudShell()) { getLog().info(AUTH_WITH_MSI); auth = azureConfigurable.authenticate(new MSICredentials()); } else { getLog().info(AUTH_WITH_AZURE_CLI); auth = azureConfigurable.authenticate(AzureCliCredentials.create()); } return auth; } catch (Exception e) { getLog().debug(AZURE_CLI_AUTH_FAIL); getLog().debug(e); } return null; }