Search in sources :

Example 6 with Credential

use of com.sequenceiq.environment.credential.domain.Credential in project cloudbreak by hortonworks.

the class CredentialService method getCredentialAndValidateUpdate.

private Credential getCredentialAndValidateUpdate(Credential credential, String accountId, CredentialType type) {
    Credential original = repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type).orElseThrow(notFound(NOT_FOUND_FORMAT_MESS_NAME, credential.getName()));
    ValidationResult validationResult = credentialValidator.validateCredentialUpdate(original, credential, type);
    if (validationResult.hasError()) {
        throw new BadRequestException(validationResult.getFormattedErrors());
    }
    return original;
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) BadRequestException(javax.ws.rs.BadRequestException) ValidationResult(com.sequenceiq.cloudbreak.validation.ValidationResult)

Example 7 with Credential

use of com.sequenceiq.environment.credential.domain.Credential in project cloudbreak by hortonworks.

the class CredentialService method initCodeGrantFlow.

public String initCodeGrantFlow(String accountId, String name) {
    Credential original = repository.findByNameAndAccountId(name, accountId, getEnabledPlatforms(), ENVIRONMENT).orElseThrow(notFound(NOT_FOUND_FORMAT_MESS_NAME, name));
    String originalAttributes = original.getAttributes();
    if (getAzureCodeGrantFlowAttributes(original) == null) {
        throw new UnsupportedOperationException("This operation is only allowed on Authorization Code Grant flow based credentails.");
    }
    Credential updated = credentialAdapter.initCodeGrantFlow(original, accountId);
    updated = repository.save(updated);
    secretService.delete(originalAttributes);
    return getCodeGrantFlowAppLoginUrl(updated);
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential)

Example 8 with Credential

use of com.sequenceiq.environment.credential.domain.Credential in project cloudbreak by hortonworks.

the class CredentialService method create.

public Credential create(Credential credential, @Nonnull String accountId, @Nonnull String creatorUserCrn, CredentialType type) {
    repository.findByNameAndAccountId(credential.getName(), accountId, getEnabledPlatforms(), type).map(Credential::getName).ifPresent(name -> {
        throw new BadRequestException("Credential already exists with name: " + name);
    });
    LOGGER.debug("Validating credential for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn);
    credentialValidator.validateCredentialCloudPlatform(credential.getCloudPlatform(), creatorUserCrn, type);
    LOGGER.debug("Validating credential parameters for cloudPlatform {} and creator {}.", credential.getCloudPlatform(), creatorUserCrn);
    credentialValidator.validateParameters(Platform.platform(credential.getCloudPlatform()), new Json(credential.getAttributes()));
    String credentialCrn = createCRN(accountId);
    credential.setResourceCrn(credentialCrn);
    credential.setCreator(creatorUserCrn);
    credential.setAccountId(accountId);
    Credential verifiedCredential = credentialAdapter.verify(credential, accountId, Boolean.TRUE).getCredential();
    if (verifiedCredential.getVerificationStatusText() != null) {
        throw new BadRequestException(verifiedCredential.getVerificationStatusText());
    }
    try {
        Credential createdCredential = transactionService.required(() -> {
            Credential created = repository.save(verifiedCredential);
            ownerAssignmentService.assignResourceOwnerRoleIfEntitled(creatorUserCrn, credentialCrn, accountId);
            return created;
        });
        sendCredentialNotification(createdCredential, ResourceEvent.CREDENTIAL_CREATED);
        return createdCredential;
    } catch (TransactionService.TransactionExecutionException e) {
        LOGGER.error("Error happened during credential creation: ", e);
        throw new InternalServerErrorException(e);
    }
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential) TransactionService(com.sequenceiq.cloudbreak.common.service.TransactionService) BadRequestException(javax.ws.rs.BadRequestException) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) Json(com.sequenceiq.cloudbreak.common.json.Json)

Example 9 with Credential

use of com.sequenceiq.environment.credential.domain.Credential in project cloudbreak by hortonworks.

the class EnvironmentModificationServiceTest method changeCredentialByEnvironmentCrn.

@Test
void changeCredentialByEnvironmentCrn() {
    String credentialName = "credentialName";
    final Credential value = new Credential();
    EnvironmentChangeCredentialDto environmentChangeDto = EnvironmentChangeCredentialDto.EnvironmentChangeCredentialDtoBuilder.anEnvironmentChangeCredentialDto().withCredentialName(credentialName).build();
    when(environmentService.findByResourceCrnAndAccountIdAndArchivedIsFalse(eq(CRN), eq(ACCOUNT_ID))).thenReturn(Optional.of(new Environment()));
    when(credentialService.getByNameForAccountId(eq(credentialName), eq(ACCOUNT_ID), eq(ENVIRONMENT))).thenReturn(value);
    environmentModificationServiceUnderTest.changeCredentialByEnvironmentCrn(ACCOUNT_ID, CRN, environmentChangeDto);
    ArgumentCaptor<Environment> environmentArgumentCaptor = ArgumentCaptor.forClass(Environment.class);
    verify(environmentService).save(environmentArgumentCaptor.capture());
    assertEquals(value, environmentArgumentCaptor.getValue().getCredential());
}
Also used : EnvironmentChangeCredentialDto(com.sequenceiq.environment.environment.dto.EnvironmentChangeCredentialDto) Credential(com.sequenceiq.environment.credential.domain.Credential) Environment(com.sequenceiq.environment.environment.domain.Environment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 10 with Credential

use of com.sequenceiq.environment.credential.domain.Credential in project cloudbreak by hortonworks.

the class EnvironmentTestData method newCredential.

private static Credential newCredential() {
    Credential credential = new Credential();
    credential.setResourceCrn(CREDENTIAL_CRN);
    return credential;
}
Also used : Credential(com.sequenceiq.environment.credential.domain.Credential)

Aggregations

Credential (com.sequenceiq.environment.credential.domain.Credential)102 Test (org.junit.jupiter.api.Test)49 Environment (com.sequenceiq.environment.environment.domain.Environment)27 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)23 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)13 EnvironmentDto (com.sequenceiq.environment.environment.dto.EnvironmentDto)10 ValidationResultBuilder (com.sequenceiq.cloudbreak.validation.ValidationResult.ValidationResultBuilder)9 EnvironmentAuthentication (com.sequenceiq.environment.environment.domain.EnvironmentAuthentication)9 EnvironmentCreationDto (com.sequenceiq.environment.environment.dto.EnvironmentCreationDto)9 BadRequestException (javax.ws.rs.BadRequestException)9 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 CheckPermissionByAccount (com.sequenceiq.authorization.annotation.CheckPermissionByAccount)8 ParametersDto (com.sequenceiq.environment.parameter.dto.ParametersDto)8 Set (java.util.Set)8 CloudConnector (com.sequenceiq.cloudbreak.cloud.CloudConnector)7 ValidationResult (com.sequenceiq.cloudbreak.validation.ValidationResult)7 AwsNetwork (com.sequenceiq.environment.network.dao.domain.AwsNetwork)7 Map (java.util.Map)7 ExtendedPollingResult (com.sequenceiq.cloudbreak.polling.ExtendedPollingResult)6 BaseNetwork (com.sequenceiq.environment.network.dao.domain.BaseNetwork)6