Search in sources :

Example 1 with CustomCertificateModel

use of com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel in project hub-alert by blackducksoftware.

the class CertificateActions method importCertificate.

private CertificateModel importCertificate(CertificateModel certificateModel) throws AlertException {
    CustomCertificateModel certificateToStore = convertToDatabaseModel(certificateModel);
    try {
        CustomCertificateModel storedCertificate = certificateAccessor.storeCertificate(certificateToStore);
        trustStoreService.importCertificate(storedCertificate);
        return convertDatabaseModelToRestModel(storedCertificate);
    } catch (AlertException importException) {
        deleteByAlias(certificateToStore);
        throw importException;
    }
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 2 with CustomCertificateModel

use of com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel in project hub-alert by blackducksoftware.

the class CertificateActions method validateCertificateFields.

private List<AlertFieldStatus> validateCertificateFields(CertificateModel certificateModel) {
    CustomCertificateModel convertedModel = convertToDatabaseModel(certificateModel);
    List<AlertFieldStatus> fieldErrors = new ArrayList<>();
    if (StringUtils.isBlank(certificateModel.getAlias())) {
        fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, "Alias cannot be empty."));
    } else {
        List<CustomCertificateModel> duplicateCertificates = certificateAccessor.getCertificates().stream().filter(certificate -> certificate.getAlias().equals(certificateModel.getAlias())).collect(Collectors.toList());
        if (duplicateCertificates.size() > 1) {
            fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
        } else if (duplicateCertificates.size() == 1) {
            boolean sameConfig = convertedModel.getNullableId() != null && duplicateCertificates.get(0).getNullableId().equals(convertedModel.getNullableId());
            if (!sameConfig) {
                fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_ALIAS, ERROR_DUPLICATE_ALIAS));
            }
        }
    }
    if (StringUtils.isBlank(certificateModel.getCertificateContent())) {
        fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, "Certificate content cannot be empty."));
    } else {
        try {
            trustStoreService.validateCertificateContent(convertedModel);
        } catch (AlertException ex) {
            logger.error(ex.getMessage(), ex);
            fieldErrors.add(AlertFieldStatus.error(CertificatesDescriptor.KEY_CERTIFICATE_CONTENT, String.format("Certificate content not valid: %s", ex.getMessage())));
        }
    }
    return fieldErrors;
}
Also used : AbstractResourceActions(com.synopsys.integration.alert.common.action.api.AbstractResourceActions) ValidationActionResponse(com.synopsys.integration.alert.common.action.ValidationActionResponse) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) ActionResponse(com.synopsys.integration.alert.common.action.ActionResponse) ValidationResponseModel(com.synopsys.integration.alert.common.rest.model.ValidationResponseModel) AlertTrustStoreManager(com.synopsys.integration.alert.component.certificates.AlertTrustStoreManager) IntegrationEscapeUtil(com.synopsys.integration.util.IntegrationEscapeUtil) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) AuthorizationManager(com.synopsys.integration.alert.common.security.authorization.AuthorizationManager) Logger(org.slf4j.Logger) ConfigContextEnum(com.synopsys.integration.alert.common.enumeration.ConfigContextEnum) CertificatesDescriptor(com.synopsys.integration.alert.component.certificates.CertificatesDescriptor) Collectors(java.util.stream.Collectors) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) CertificatesDescriptorKey(com.synopsys.integration.alert.component.certificates.CertificatesDescriptorKey) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Component(org.springframework.stereotype.Component) NumberUtils(org.apache.commons.lang3.math.NumberUtils) Optional(java.util.Optional) CustomCertificateAccessor(com.synopsys.integration.alert.common.persistence.accessor.CustomCertificateAccessor) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) ArrayList(java.util.ArrayList) AlertFieldStatus(com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 3 with CustomCertificateModel

use of com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel in project hub-alert by blackducksoftware.

the class DefaultCustomCertificateAccessor method createModel.

private CustomCertificateModel createModel(CustomCertificateEntity entity) {
    CustomCertificateModel customCertificateModel = new CustomCertificateModel(entity.getAlias(), entity.getCertificateContent(), DateUtils.formatDate(entity.getLastUpdated(), DateUtils.UTC_DATE_FORMAT_TO_MINUTE));
    customCertificateModel.setId(entity.getId());
    return customCertificateModel;
}
Also used : CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel)

Example 4 with CustomCertificateModel

use of com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel in project hub-alert by blackducksoftware.

the class DefaultCustomCertificateAccessorTest method storeCertificateIdDoesNotExistTest.

@Test
public void storeCertificateIdDoesNotExistTest() {
    CustomCertificateRepository customCertificateRepository = new MockCustomCertificateRepository();
    DefaultCustomCertificateAccessor customCertificateAccessor = new DefaultCustomCertificateAccessor(customCertificateRepository);
    try {
        CustomCertificateModel certificateModel = new CustomCertificateModel(9L, alias, content, testDate.toString());
        customCertificateAccessor.storeCertificate(certificateModel);
        fail();
    } catch (AlertConfigurationException e) {
        assertNotNull(e);
    }
}
Also used : CustomCertificateRepository(com.synopsys.integration.alert.database.certificates.CustomCertificateRepository) MockCustomCertificateRepository(com.synopsys.integration.alert.database.api.mock.MockCustomCertificateRepository) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) MockCustomCertificateRepository(com.synopsys.integration.alert.database.api.mock.MockCustomCertificateRepository) AlertConfigurationException(com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException) Test(org.junit.jupiter.api.Test)

Example 5 with CustomCertificateModel

use of com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel in project hub-alert by blackducksoftware.

the class DefaultCustomCertificateAccessorTest method getCertificatesTest.

@Test
public void getCertificatesTest() {
    CustomCertificateRepository customCertificateRepository = new MockCustomCertificateRepository(alias, content, testDate);
    DefaultCustomCertificateAccessor customCertificateAccessor = new DefaultCustomCertificateAccessor(customCertificateRepository);
    List<CustomCertificateModel> customCertificateModelList = customCertificateAccessor.getCertificates();
    assertEquals(1, customCertificateModelList.size());
    CustomCertificateModel customCertificateModel = customCertificateModelList.get(0);
    testCustomCertificateModel(expectedCustomCertificateModel, customCertificateModel);
}
Also used : CustomCertificateRepository(com.synopsys.integration.alert.database.certificates.CustomCertificateRepository) MockCustomCertificateRepository(com.synopsys.integration.alert.database.api.mock.MockCustomCertificateRepository) CustomCertificateModel(com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel) MockCustomCertificateRepository(com.synopsys.integration.alert.database.api.mock.MockCustomCertificateRepository) Test(org.junit.jupiter.api.Test)

Aggregations

CustomCertificateModel (com.synopsys.integration.alert.common.persistence.model.CustomCertificateModel)11 MockCustomCertificateRepository (com.synopsys.integration.alert.database.api.mock.MockCustomCertificateRepository)6 CustomCertificateRepository (com.synopsys.integration.alert.database.certificates.CustomCertificateRepository)6 Test (org.junit.jupiter.api.Test)6 AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)4 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)3 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)3 AlertConfigurationException (com.synopsys.integration.alert.api.common.model.exception.AlertConfigurationException)1 AbstractResourceActions (com.synopsys.integration.alert.common.action.api.AbstractResourceActions)1 AlertFieldStatus (com.synopsys.integration.alert.common.descriptor.config.field.errors.AlertFieldStatus)1 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)1 CustomCertificateAccessor (com.synopsys.integration.alert.common.persistence.accessor.CustomCertificateAccessor)1 ValidationResponseModel (com.synopsys.integration.alert.common.rest.model.ValidationResponseModel)1 AuthorizationManager (com.synopsys.integration.alert.common.security.authorization.AuthorizationManager)1 AlertTrustStoreManager (com.synopsys.integration.alert.component.certificates.AlertTrustStoreManager)1 CertificatesDescriptor (com.synopsys.integration.alert.component.certificates.CertificatesDescriptor)1 CertificatesDescriptorKey (com.synopsys.integration.alert.component.certificates.CertificatesDescriptorKey)1 IntegrationEscapeUtil (com.synopsys.integration.util.IntegrationEscapeUtil)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1