Skip to content

Commit

Permalink
Added notification for propose new vulnerability (#2833)
Browse files Browse the repository at this point in the history
* Added notification for propose new vulnerability
  • Loading branch information
Cheshiriks authored Nov 2, 2023
1 parent 075fe77 commit 2d70218
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.saveourtool.save.backend.controllers.vulnerability.event

import com.saveourtool.save.backend.service.NotificationService
import com.saveourtool.save.backend.service.UserDetailsService
import com.saveourtool.save.domain.Role
import com.saveourtool.save.entities.Notification
import com.saveourtool.save.entities.cosv.VulnerabilityMetadata
import com.saveourtool.save.entities.cosv.evententities.VulnerabilityMetadataEvent
import com.saveourtool.save.entities.vulnerability.VulnerabilityStatus
import org.springframework.context.event.EventListener
import org.springframework.stereotype.Component

/**
* A vulnerability metadata listener for sending notifications.
*/
@Component
class VulnerabilityMetadataListener(
private val userDetailsService: UserDetailsService,
private val notificationService: NotificationService,
) {
/**
* @param vulnerabilityEvent new VulnerabilityMetadataEvent
*/
@EventListener
fun createVulnerabilityMetadata(vulnerabilityEvent: VulnerabilityMetadataEvent) {
if (vulnerabilityEvent.vulnerabilityMetadata.status == VulnerabilityStatus.PENDING_REVIEW) {
val recipients = userDetailsService.findByRole(Role.SUPER_ADMIN.asSpringSecurityRole())
val notifications = recipients.map {
Notification(
message = messageNewVulnerabilityMetadata(vulnerabilityEvent.vulnerabilityMetadata),
user = it,
)
}
notificationService.saveAll(notifications)
}
}

companion object {
/**
* @param vulnerability
* @return message
*/
fun messageNewVulnerabilityMetadata(vulnerability: VulnerabilityMetadata) = """
New vulnerability: ${vulnerability.identifier} is waiting for approve.
""".trimIndent()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.saveourtool.save.entities.cosv.evententities

import com.saveourtool.save.entities.cosv.VulnerabilityMetadata

/**
* @property vulnerabilityMetadata
**/
data class VulnerabilityMetadataEvent(
var vulnerabilityMetadata: VulnerabilityMetadata
)
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.saveourtool.save.entities.cosv.CosvFile
import com.saveourtool.save.entities.cosv.VulnerabilityMetadata
import com.saveourtool.save.entities.cosv.VulnerabilityMetadataDto
import com.saveourtool.save.entities.cosv.VulnerabilityMetadataDto.Companion.SUMMARY_LENGTH
import com.saveourtool.save.entities.cosv.evententities.VulnerabilityMetadataEvent
import com.saveourtool.save.entities.vulnerability.VulnerabilityLanguage
import com.saveourtool.save.entities.vulnerability.VulnerabilityStatus
import com.saveourtool.save.utils.ELLIPSIS
Expand All @@ -22,6 +23,7 @@ import com.saveourtool.save.utils.warn

import com.saveourtool.osv4k.Severity
import com.saveourtool.osv4k.SeverityType
import org.springframework.context.ApplicationEventPublisher
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional
Expand All @@ -36,6 +38,7 @@ import kotlinx.datetime.toJavaLocalDateTime
class VulnerabilityMetadataService(
private val vulnerabilityMetadataRepository: VulnerabilityMetadataRepository,
private val cosvFileS3KeyManager: CosvFileS3KeyManager,
private val applicationEventPublisher: ApplicationEventPublisher,
) {
/**
* @param cosvFile
Expand All @@ -60,7 +63,9 @@ class VulnerabilityMetadataService(
existedMetadata.updateBy(cosv, cosvFile, isAutoApprove)
}
?: run {
cosv.toNewMetadata(user, organization, cosvFile, isAutoApprove)
val newMetadata = cosv.toNewMetadata(user, organization, cosvFile, isAutoApprove)
applicationEventPublisher.publishEvent(VulnerabilityMetadataEvent(newMetadata))
newMetadata
}

return vulnerabilityMetadataRepository.save(metadata)
Expand Down

0 comments on commit 2d70218

Please sign in to comment.