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;
}
}
Aggregations