Search in sources :

Example 11 with CloudCredentialStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testForFailedStatusBecauseMissingCompute.

/**
 * Testing the GcpContext checking mechanism. If the inner created
 * GcpContext does not contains a valid Compute instance then a FAILED
 * status should come back.
 */
@Test
public void testForFailedStatusBecauseMissingCompute() {
    final AuthenticatedContext authContext = createAuthContext();
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn("some service id");
    when(context.getCompute()).thenReturn(null);
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.FAILED, status.getStatus());
}
Also used : AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Example 12 with CloudCredentialStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testForNullExceptionOnVerifyPermissionCheck.

/**
 * Test that if some exception terminates the credential check operation,
 * or the checked credential is not valid. If that so, than the expected
 * CredentialStatus should be FAILED.
 *
 * @throws IOException this exception could thrown by the credential
 *                     checker method.
 */
@Test
public void testForNullExceptionOnVerifyPermissionCheck() throws IOException {
    final AuthenticatedContext authContext = createAuthContext();
    final String expectionReasonMessage = "exception message";
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn("some service id");
    when(context.getCompute().regions().list(anyString()).executeUsingHead()).thenThrow(new BadRequestException(expectionReasonMessage));
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.FAILED, status.getStatus());
    Assert.assertTrue("Not the specified exception has come with the status", status.getException() instanceof BadRequestException);
    Assert.assertEquals("Not the expected exception message has come with the status", expectionReasonMessage, status.getException().getMessage());
}
Also used : BadRequestException(javax.ws.rs.BadRequestException) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Matchers.anyString(org.mockito.Matchers.anyString) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Example 13 with CloudCredentialStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus in project cloudbreak by hortonworks.

the class GcpCredentialConnectorTest method testPassingVerifyPermissionCheck.

/**
 * Test that if the credential authentication has passed, the returned
 * CredentialStatus is VERIFIED.
 *
 * @throws IOException this exception could thrown by the credential
 *                     checker method.
 */
@Test
public void testPassingVerifyPermissionCheck() throws IOException {
    final AuthenticatedContext authContext = createAuthContext();
    when(contextBuilder.contextInit(authContext.getCloudContext(), authContext, null, null, false)).thenReturn(context);
    when(context.getProjectId()).thenReturn("some id");
    when(context.getServiceAccountId()).thenReturn("some service id");
    final CloudCredentialStatus status = underTest.verify(authContext);
    Assert.assertNotNull("The returned CloudCredentialStatus instance is null!", status);
    Assert.assertEquals("Invalid credential status has specified!", CredentialStatus.VERIFIED, status.getStatus());
}
Also used : AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) Test(org.junit.Test)

Example 14 with CloudCredentialStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus in project cloudbreak by hortonworks.

the class GcpCredentialConnector method verify.

@Override
public CloudCredentialStatus verify(@Nonnull AuthenticatedContext authenticatedContext) {
    Objects.requireNonNull(authenticatedContext);
    LOGGER.info("Verify credential: {}", authenticatedContext.getCloudCredential());
    GcpContext gcpContext = gcpContextBuilder.contextInit(authenticatedContext.getCloudContext(), authenticatedContext, null, null, false);
    try {
        checkGcpContextValidity(gcpContext);
        preCheckOfGooglePermission(gcpContext);
    } catch (TokenResponseException te) {
        return createFailedCloudCredentialStatusWithExc(te, authenticatedContext, getErrDescriptionFromTokenResponse(te));
    } catch (Exception e) {
        return createFailedCloudCredentialStatusWithExc(e, authenticatedContext, Optional.empty());
    }
    return new CloudCredentialStatus(authenticatedContext.getCloudCredential(), CredentialStatus.VERIFIED);
}
Also used : GcpContext(com.sequenceiq.cloudbreak.cloud.gcp.context.GcpContext) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) IOException(java.io.IOException) InvalidGcpContextException(com.sequenceiq.cloudbreak.cloud.gcp.context.InvalidGcpContextException) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)

Example 15 with CloudCredentialStatus

use of com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus in project cloudbreak by hortonworks.

the class CreateCredentialHandler method accept.

@Override
public void accept(Event<CreateCredentialRequest> credentialRequestEvent) {
    LOGGER.info("Received event: {}", credentialRequestEvent);
    CreateCredentialRequest request = credentialRequestEvent.getData();
    CloudContext cloudContext = request.getCloudContext();
    try {
        CloudConnector connector = cloudPlatformConnectors.get(cloudContext.getPlatformVariant());
        AuthenticatedContext ac = connector.authentication().authenticate(cloudContext, request.getCloudCredential());
        CloudCredentialStatus credentialStatus = connector.credentials().create(ac);
        if (CredentialStatus.FAILED == credentialStatus.getStatus()) {
            if (credentialStatus.getException() != null) {
                throw new CloudConnectorException(credentialStatus.getException());
            }
            throw new CloudConnectorException(credentialStatus.getStatusReason());
        }
        CreateCredentialResult result = new CreateCredentialResult(request);
        request.getResult().onNext(result);
        eventBus.notify(result.selector(), new Event<>(credentialRequestEvent.getHeaders(), result));
        LOGGER.info("Creating credential successfully finished for {}", cloudContext);
    } catch (RuntimeException e) {
        CreateCredentialResult failure = new CreateCredentialResult(e, request);
        request.getResult().onNext(failure);
        eventBus.notify(failure.selector(), new Event<>(credentialRequestEvent.getHeaders(), failure));
    }
}
Also used : CreateCredentialRequest(com.sequenceiq.cloudbreak.cloud.event.resource.CreateCredentialRequest) CloudConnector(com.sequenceiq.cloudbreak.cloud.CloudConnector) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) CloudContext(com.sequenceiq.cloudbreak.cloud.context.CloudContext) Event(reactor.bus.Event) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) CloudCredentialStatus(com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus) CreateCredentialResult(com.sequenceiq.cloudbreak.cloud.event.resource.CreateCredentialResult)

Aggregations

CloudCredentialStatus (com.sequenceiq.cloudbreak.cloud.model.CloudCredentialStatus)16 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)9 Test (org.junit.Test)7 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)3 AmazonClientException (com.amazonaws.AmazonClientException)2 AwsCredentialView (com.sequenceiq.cloudbreak.cloud.aws.view.AwsCredentialView)2 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)2 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 CloudResourceStatus (com.sequenceiq.cloudbreak.cloud.model.CloudResourceStatus)2 ExtendedCloudCredential (com.sequenceiq.cloudbreak.cloud.model.ExtendedCloudCredential)2 Event (reactor.bus.Event)2 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)1 DescribeRegionsRequest (com.amazonaws.services.ec2.model.DescribeRegionsRequest)1 TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)1 AzureClient (com.sequenceiq.cloudbreak.cloud.azure.client.AzureClient)1 CloudContext (com.sequenceiq.cloudbreak.cloud.context.CloudContext)1 CredentialVerificationException (com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationException)1 CredentialVerificationRequest (com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationRequest)1 CredentialVerificationResult (com.sequenceiq.cloudbreak.cloud.event.credential.CredentialVerificationResult)1