Search in sources :

Example 26 with AzureCredentialView

use of com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView in project cloudbreak by hortonworks.

the class AzureInteractiveLoginStatusCheckerTask method doCall.

@Override
protected Boolean doCall() {
    Response response = createPollingRequest();
    if (response.getStatusInfo().getFamily() == Family.SUCCESSFUL) {
        String tokenResponseString = response.readEntity(String.class);
        try {
            String refreshToken = new ObjectMapper().readTree(tokenResponseString).get("refresh_token").asText();
            LOGGER.info("Access token received");
            ExtendedCloudCredential extendedCloudCredential = armInteractiveLoginStatusCheckerContext.getExtendedCloudCredential();
            AzureCredentialView armCredentialView = new AzureCredentialView(extendedCloudCredential);
            try {
                String graphApiAccessToken = createResourceToken(refreshToken, armCredentialView.getTenantId(), GRAPH_WINDOWS);
                String managementApiToken = createResourceToken(refreshToken, armCredentialView.getTenantId(), MANAGEMENT_CORE_WINDOWS);
                subscriptionChecker.checkSubscription(armCredentialView.getSubscriptionId(), managementApiToken);
                tenantChecker.checkTenant(armCredentialView.getTenantId(), managementApiToken);
                String secretKey = UUID.randomUUID().toString();
                String appId = applicationCreator.createApplication(graphApiAccessToken, armCredentialView.getTenantId(), secretKey);
                sendStatusMessage(extendedCloudCredential, "Cloudbreak application created");
                ServicePrincipalInner sp = principalCreator.createServicePrincipal(graphApiAccessToken, appId, armCredentialView.getTenantId());
                String principalObjectId = sp.objectId();
                String notification = new StringBuilder("Principal created for application!").append(" Name: ").append(sp.displayName()).append(", AppId: ").append(sp.appId()).toString();
                sendStatusMessage(extendedCloudCredential, notification);
                String roleName = armCredentialView.getRoleName();
                String roleType = armCredentialView.getRoleType();
                String roleId = azureRoleManager.handleRoleOperations(managementApiToken, armCredentialView.getSubscriptionId(), roleName, roleType);
                azureRoleManager.assignRole(managementApiToken, armCredentialView.getSubscriptionId(), roleId, principalObjectId);
                sendStatusMessage(extendedCloudCredential, "Role assigned for principal");
                extendedCloudCredential.putParameter("accessKey", appId);
                extendedCloudCredential.putParameter("secretKey", secretKey);
                extendedCloudCredential.putParameter("spDisplayName", sp.displayName());
                armInteractiveLoginStatusCheckerContext.getCredentialNotifier().createCredential(getAuthenticatedContext().getCloudContext(), extendedCloudCredential);
            } catch (InteractiveLoginException | InteractiveLoginUnrecoverableException e) {
                LOGGER.error("Interactive login failed", e);
                sendErrorStatusMessage(extendedCloudCredential, e.getMessage());
            }
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
        return true;
    } else {
        LOGGER.info("Polling request failed this time, status code {}, response: {}", response.getStatus(), response.readEntity(String.class));
        return false;
    }
}
Also used : Response(javax.ws.rs.core.Response) ExtendedCloudCredential(com.sequenceiq.cloudbreak.cloud.model.ExtendedCloudCredential) AzureCredentialView(com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView) ServicePrincipalInner(com.microsoft.azure.management.graphrbac.implementation.ServicePrincipalInner) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

AzureCredentialView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureCredentialView)26 AzureStackView (com.sequenceiq.cloudbreak.cloud.azure.view.AzureStackView)19 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)19 HashMap (java.util.HashMap)19 InstanceAuthentication (com.sequenceiq.cloudbreak.cloud.model.InstanceAuthentication)18 Matchers.containsString (org.hamcrest.Matchers.containsString)18 CloudStack (com.sequenceiq.cloudbreak.cloud.model.CloudStack)17 Group (com.sequenceiq.cloudbreak.cloud.model.Group)17 Network (com.sequenceiq.cloudbreak.cloud.model.Network)17 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)17 Test (org.junit.Test)17 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)5 CloudException (com.microsoft.azure.CloudException)3 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 ActionWentFailException (com.sequenceiq.cloudbreak.service.Retry.ActionWentFailException)3 CloudError (com.microsoft.azure.CloudError)2 Deployment (com.microsoft.azure.management.resources.Deployment)2 CloudInstance (com.sequenceiq.cloudbreak.cloud.model.CloudInstance)2 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)2