|
| 1 | +package org.owasp.wrongsecrets.challenges.docker; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import java.util.Optional; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.http.HttpEntity; |
| 10 | +import org.springframework.http.HttpHeaders; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.web.client.RestTemplate; |
| 14 | + |
| 15 | +/** Service for sending Slack notifications when challenges are completed. */ |
| 16 | +@Service |
| 17 | +public class SlackNotificationService { |
| 18 | + |
| 19 | + private static final Logger logger = LoggerFactory.getLogger(SlackNotificationService.class); |
| 20 | + |
| 21 | + private final RestTemplate restTemplate; |
| 22 | + private final ObjectMapper objectMapper; |
| 23 | + private final Optional<Challenge59> challenge59; |
| 24 | + |
| 25 | + public SlackNotificationService( |
| 26 | + RestTemplate restTemplate, |
| 27 | + ObjectMapper objectMapper, |
| 28 | + @Autowired(required = false) Challenge59 challenge59) { |
| 29 | + this.restTemplate = restTemplate; |
| 30 | + this.objectMapper = objectMapper; |
| 31 | + this.challenge59 = Optional.ofNullable(challenge59); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Sends a Slack notification when a challenge is completed. |
| 36 | + * |
| 37 | + * @param challengeName The name of the completed challenge |
| 38 | + * @param userName Optional username of the person who completed the challenge |
| 39 | + */ |
| 40 | + public void notifyChallengeCompletion(String challengeName, String userName) { |
| 41 | + if (!isSlackConfigured()) { |
| 42 | + logger.debug("Slack not configured, skipping notification for challenge: {}", challengeName); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + String message = buildCompletionMessage(challengeName, userName); |
| 48 | + SlackMessage slackMessage = new SlackMessage(message); |
| 49 | + |
| 50 | + HttpHeaders headers = new HttpHeaders(); |
| 51 | + headers.setContentType(MediaType.APPLICATION_JSON); |
| 52 | + |
| 53 | + HttpEntity<SlackMessage> request = new HttpEntity<>(slackMessage, headers); |
| 54 | + |
| 55 | + String webhookUrl = challenge59.get().getSlackWebhookUrl(); |
| 56 | + restTemplate.postForEntity(webhookUrl, request, String.class); |
| 57 | + logger.info( |
| 58 | + "Successfully sent Slack notification for challenge completion: {}", challengeName); |
| 59 | + |
| 60 | + } catch (Exception e) { |
| 61 | + logger.warn("Failed to send Slack notification for challenge: {}", challengeName, e); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private boolean isSlackConfigured() { |
| 66 | + return challenge59.isPresent() |
| 67 | + && challenge59.get().getSlackWebhookUrl() != null |
| 68 | + && !challenge59.get().getSlackWebhookUrl().trim().isEmpty() |
| 69 | + && !challenge59.get().getSlackWebhookUrl().equals("not_set") |
| 70 | + && challenge59.get().getSlackWebhookUrl().startsWith("https://hooks.slack.com"); |
| 71 | + } |
| 72 | + |
| 73 | + private String buildCompletionMessage(String challengeName, String userName) { |
| 74 | + String userPart = (userName != null && !userName.trim().isEmpty()) ? " by " + userName : ""; |
| 75 | + |
| 76 | + return String.format( |
| 77 | + "🎉 Challenge %s completed%s! Another secret vulnerability discovered in WrongSecrets.", |
| 78 | + challengeName, userPart); |
| 79 | + } |
| 80 | + |
| 81 | + /** Simple record for Slack message payload. */ |
| 82 | + public static class SlackMessage { |
| 83 | + @JsonProperty("text") |
| 84 | + private final String text; |
| 85 | + |
| 86 | + public SlackMessage(String text) { |
| 87 | + this.text = text; |
| 88 | + } |
| 89 | + |
| 90 | + public String getText() { |
| 91 | + return text; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments