Search in sources :

Example 91 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class AlertTrustStoreManager method importCertificate.

public synchronized void importCertificate(CustomCertificateModel customCertificate) throws AlertException {
    logger.debug("Importing certificate into trust store.");
    validateCustomCertificateHasValues(customCertificate);
    File trustStoreFile = getAndValidateTrustStoreFile();
    KeyStore trustStore = getAsKeyStore(trustStoreFile, getTrustStorePassword(), getTrustStoreType());
    try {
        Certificate cert = getAsJavaCertificate(customCertificate);
        trustStore.setCertificateEntry(customCertificate.getAlias(), cert);
        try (OutputStream stream = new BufferedOutputStream(new FileOutputStream(trustStoreFile))) {
            trustStore.store(stream, getTrustStorePassword());
        }
    } catch (IOException | CertificateException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new AlertException("There was a problem storing the certificate.", e);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) FileOutputStream(java.io.FileOutputStream) CertificateException(java.security.cert.CertificateException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) File(java.io.File) KeyStore(java.security.KeyStore) BufferedOutputStream(java.io.BufferedOutputStream) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Certificate(java.security.cert.Certificate)

Example 92 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class MsTeamsChannelTest method sendMessageTestIT.

@Test
@Tag(TestTags.DEFAULT_INTEGRATION)
@Tag(TestTags.CUSTOM_EXTERNAL_CONNECTION)
public void sendMessageTestIT() {
    ChannelRestConnectionFactory connectionFactory = createConnectionFactory();
    MarkupEncoderUtil markupEncoderUtil = new MarkupEncoderUtil();
    MSTeamsChannelMessageConverter messageConverter = new MSTeamsChannelMessageConverter(new MSTeamsChannelMessageFormatter(markupEncoderUtil));
    MSTeamsChannelMessageSender messageSender = new MSTeamsChannelMessageSender(ChannelKeys.MS_TEAMS, connectionFactory);
    MSTeamsChannel msTeamsChannel = new MSTeamsChannel(messageConverter, messageSender);
    MSTeamsJobDetailsModel msTeamsJobDetailsModel = new MSTeamsJobDetailsModel(UUID.randomUUID(), properties.getProperty(TestPropertyKey.TEST_MSTEAMS_WEBHOOK));
    MessageResult messageResult = null;
    try {
        messageResult = msTeamsChannel.distributeMessages(msTeamsJobDetailsModel, TEST_MESSAGE_HOLDER, "jobName");
    } catch (AlertException e) {
        Assertions.fail("Failed to distribute simple channel message due to an exception", e);
    }
    Assertions.assertFalse(messageResult.hasErrors(), "The message result had errors");
    Assertions.assertFalse(messageResult.hasWarnings(), "The message result had warnings");
}
Also used : ChannelRestConnectionFactory(com.synopsys.integration.alert.api.channel.rest.ChannelRestConnectionFactory) MarkupEncoderUtil(com.synopsys.integration.alert.common.util.MarkupEncoderUtil) MSTeamsJobDetailsModel(com.synopsys.integration.alert.common.persistence.model.job.details.MSTeamsJobDetailsModel) MessageResult(com.synopsys.integration.alert.common.message.model.MessageResult) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Example 93 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class ConfigurationOverridesStartupComponent method checkAndResetDefaultAdminPassword.

private void checkAndResetDefaultAdminPassword() throws AlertException {
    boolean disable = isEnvironmentVariableActivated(ENV_VAR_ADMIN_USER_PASSWORD_RESET);
    if (disable) {
        UserModel userModel = userAccessor.getUser(UserAccessor.DEFAULT_ADMIN_USER_ID).orElseThrow(() -> new AlertException("The default admin user was not found."));
        logger.info("Resetting the password for the user '{}'.", userModel.getName());
        UserModel newModel = UserModel.existingUser(userModel.getId(), userModel.getName(), DEFAULT_ADMIN_PASSWORD, userModel.getEmailAddress(), userModel.getAuthenticationType(), userModel.getRoles(), userModel.isEnabled());
        userAccessor.updateUser(newModel, true);
    }
}
Also used : UserModel(com.synopsys.integration.alert.common.persistence.model.UserModel) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException)

Example 94 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class BlackDuckSSOConfigRetrieverTest method retrieveExceptionTest.

@Test
public void retrieveExceptionTest() throws IntegrationException {
    HttpUrl baseUrl = new HttpUrl("https://a-blackduck-server");
    ApiDiscovery apiDiscovery = new ApiDiscovery(baseUrl);
    BlackDuckApiClient blackDuckApiClient = Mockito.mock(BlackDuckApiClient.class);
    Mockito.when(blackDuckApiClient.getResponse(Mockito.any(BlackDuckRequest.class))).thenThrow(new AlertException());
    BlackDuckSSOConfigRetriever ssoConfigRetriever = new BlackDuckSSOConfigRetriever(apiDiscovery, blackDuckApiClient);
    try {
        ssoConfigRetriever.retrieve();
        fail(String.format("Expected an %s to be thrown", AlertException.class.getSimpleName()));
    } catch (AlertException e) {
    // Pass
    }
}
Also used : ApiDiscovery(com.synopsys.integration.blackduck.api.generated.discovery.ApiDiscovery) BlackDuckApiClient(com.synopsys.integration.blackduck.service.BlackDuckApiClient) BlackDuckRequest(com.synopsys.integration.blackduck.service.request.BlackDuckRequest) HttpUrl(com.synopsys.integration.rest.HttpUrl) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Test(org.junit.jupiter.api.Test)

Example 95 with AlertException

use of com.synopsys.integration.alert.api.common.model.exception.AlertException in project hub-alert by blackducksoftware.

the class EmailMessagingService method sendEmailMessage.

public void sendEmailMessage(Properties javamailProperties, String smtpFrom, String smtpHost, int smtpPort, boolean smtpAuth, String smtpUsername, String smtpPassword, EmailTarget emailTarget) throws AlertException {
    try {
        String templateName = StringUtils.trimToEmpty(emailTarget.getTemplateName());
        Set<String> emailAddresses = emailTarget.getEmailAddresses().stream().map(String::trim).filter(StringUtils::isNotBlank).collect(Collectors.toSet());
        if (emailAddresses.isEmpty() || StringUtils.isBlank(templateName)) {
            // Nothing to send
            logger.debug("No non-blank email addresses were provided");
            return;
        }
        Map<String, Object> model = emailTarget.getModel();
        Session session = Session.getInstance(javamailProperties);
        TemplateLoader templateLoader = freemarkerTemplatingService.createClassTemplateLoader("/templates/email");
        Configuration templateDirectory = freemarkerTemplatingService.createFreemarkerConfig(templateLoader);
        Template emailTemplate = templateDirectory.getTemplate(templateName);
        String html = freemarkerTemplatingService.resolveTemplate(model, emailTemplate);
        MimeMultipartBuilder mimeMultipartBuilder = new MimeMultipartBuilder();
        mimeMultipartBuilder.addHtmlContent(html);
        mimeMultipartBuilder.addTextContent(Jsoup.parse(html).text());
        mimeMultipartBuilder.addEmbeddedImages(emailTarget.getContentIdsToFilePaths());
        List<String> attachmentFilePaths = emailTarget.getAttachmentFilePaths();
        if (!attachmentFilePaths.isEmpty()) {
            mimeMultipartBuilder.addAttachments(attachmentFilePaths);
        }
        MimeMultipart mimeMultipart = mimeMultipartBuilder.build();
        String subjectLine = (String) model.get(EmailPropertyKeys.TEMPLATE_KEY_SUBJECT_LINE.getPropertyKey());
        if (StringUtils.isBlank(subjectLine)) {
            subjectLine = "Default Subject Line - please define one";
        }
        Template subjectLineTemplate = new Template(EMAIL_SUBJECT_LINE_TEMPLATE, subjectLine, templateDirectory);
        String resolvedSubjectLine = freemarkerTemplatingService.resolveTemplate(model, subjectLineTemplate);
        List<Message> messages = createMessages(emailAddresses, resolvedSubjectLine, session, mimeMultipart, smtpFrom);
        sendMessages(smtpAuth, smtpHost, smtpPort, smtpUsername, smtpPassword, session, messages);
    } catch (MessagingException | IOException | IntegrationException ex) {
        String errorMessage = "Could not send the email. " + ex.getMessage();
        throw new AlertException(errorMessage, ex);
    }
}
Also used : Configuration(freemarker.template.Configuration) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) IntegrationException(com.synopsys.integration.exception.IntegrationException) MessagingException(javax.mail.MessagingException) TemplateLoader(freemarker.cache.TemplateLoader) IOException(java.io.IOException) Template(freemarker.template.Template) MimeMultipart(javax.mail.internet.MimeMultipart) AlertException(com.synopsys.integration.alert.api.common.model.exception.AlertException) Session(javax.mail.Session)

Aggregations

AlertException (com.synopsys.integration.alert.api.common.model.exception.AlertException)101 Test (org.junit.jupiter.api.Test)35 MessageResult (com.synopsys.integration.alert.common.message.model.MessageResult)22 ActionResponse (com.synopsys.integration.alert.common.action.ActionResponse)18 List (java.util.List)18 FieldModel (com.synopsys.integration.alert.common.rest.model.FieldModel)17 ValidationActionResponse (com.synopsys.integration.alert.common.action.ValidationActionResponse)16 ConfigurationFieldModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationFieldModel)16 Optional (java.util.Optional)13 IssueTrackerModelHolder (com.synopsys.integration.alert.api.channel.issue.model.IssueTrackerModelHolder)12 ConfigurationModel (com.synopsys.integration.alert.common.persistence.model.ConfigurationModel)12 DistributionJobModel (com.synopsys.integration.alert.common.persistence.model.job.DistributionJobModel)11 HashMap (java.util.HashMap)11 IntegrationException (com.synopsys.integration.exception.IntegrationException)10 ArrayList (java.util.ArrayList)9 ConfigContextEnum (com.synopsys.integration.alert.common.enumeration.ConfigContextEnum)8 DistributionJobDetailsModel (com.synopsys.integration.alert.common.persistence.model.job.details.DistributionJobDetailsModel)8 DescriptorKey (com.synopsys.integration.alert.descriptor.api.model.DescriptorKey)8 UUID (java.util.UUID)8 IssueOperation (com.synopsys.integration.alert.common.channel.issuetracker.enumeration.IssueOperation)7