-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add slack notifications for new vulnerabilities
Whenever a new vulnerability is reported, a message will be posted to slack. If no slack webhook uri is present, the this is a noop.
- Loading branch information
1 parent
7bf69c2
commit d04129d
Showing
3 changed files
with
82 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/no/digipost/github/monitoring/SlackClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package no.digipost.github.monitoring | ||
|
||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import java.net.URI | ||
import java.net.http.HttpClient | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse | ||
|
||
class SlackClient(private val webhookUrl: String) { | ||
|
||
private val logger: Logger = LoggerFactory.getLogger("no.digipost.github.monitoring.SlackClient") | ||
private val client: HttpClient = HttpClient.newBuilder().build() | ||
|
||
fun sendToSlack(vulnerability: Vulnerability) { | ||
val request = slackRequest("Ny sårbarhet: ${toSlackInformation(vulnerability)}") | ||
val response = client.send(request, HttpResponse.BodyHandlers.ofString()) | ||
|
||
if (response.statusCode() != 200) { | ||
logger.warn("Failed to report new vulnerability to slack. Status code ${response.statusCode()}, body: ${response.body()}") | ||
} | ||
} | ||
|
||
private fun toSlackInformation(vulnerability: Vulnerability): String { | ||
return "*${vulnerability.severity} (${vulnerability.score})* " + | ||
"<https://nvd.nist.gov/vuln/detail/${vulnerability.CVE}|${vulnerability.CVE}>, " + | ||
"package name: ${vulnerability.packageName}" | ||
} | ||
|
||
private fun slackRequest(message: String): HttpRequest { | ||
return HttpRequest | ||
.newBuilder() | ||
.uri(URI.create(webhookUrl)) | ||
.POST(HttpRequest.BodyPublishers.ofString("{ \"text\": \"$message\"}")) | ||
.header("Content-Type", "application/json") | ||
.build() | ||
} | ||
} |