Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module github.com/greenbone/opensight-notification-service

go 1.26.0

replace github.com/greenbone/opensight-golang-libraries => ../opensight-golang-libraries

require (
github.com/gin-contrib/cors v1.7.6
github.com/gin-gonic/gin v1.11.0
Expand Down Expand Up @@ -51,6 +53,8 @@ require (
github.com/go-resty/resty/v2 v2.17.2 // indirect
github.com/goccy/go-json v0.10.5 // indirect
github.com/goccy/go-yaml v1.19.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jonboulle/clockwork v0.5.0 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pM
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gabriel-vasile/mimetype v1.4.13 h1:46nXokslUBsAJE/wMsp5gtO500a4F3Nkz9Ufpk2AcUM=
Expand Down Expand Up @@ -114,8 +116,12 @@ github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/greenbone/keycloak-client-golang v0.2.3 h1:gtTTE844ub9iwOADwPJqbrF/edmEulO2sl2rBamSOSA=
github.com/greenbone/keycloak-client-golang v0.2.3/go.mod h1:Ws0Dc9j7g3OFupux3kHDTlTd+xelygEvvRa144/+Wf8=
github.com/greenbone/opensight-golang-libraries v1.31.1 h1:H2M7TMd2tvUFr4WFC0dYGBFrhZE/EuzygYzyiVIUFho=
github.com/greenbone/opensight-golang-libraries v1.31.1/go.mod h1:69rfVIgQmz8CFpkUIB/+qexwPNa0ENRCgq9OoTlQzbI=
github.com/hashicorp/go-cleanhttp v0.5.2 h1:035FKYIWjmULyFRBKPs8TBQoi0x6d9G4xc9neXJWAZQ=
github.com/hashicorp/go-cleanhttp v0.5.2/go.mod h1:kO/YDlP8L1346E6Sodw+PrpBSV4/SoxCXGY6BqNFT48=
github.com/hashicorp/go-hclog v1.6.3 h1:Qr2kF+eVWjTiYmU7Y31tYlP1h0q/X3Nl3tPGdaB11/k=
github.com/hashicorp/go-hclog v1.6.3/go.mod h1:W4Qnvbt70Wk/zYJryRzDRU/4r0kIg0PVHBcfoyhpF5M=
github.com/hashicorp/go-retryablehttp v0.7.8 h1:ylXZWnqa7Lhqpk0L1P1LzDtGcCR0rPVUrx/c8Unxc48=
github.com/hashicorp/go-retryablehttp v0.7.8/go.mod h1:rjiScheydd+CxvumBsIrFKlx3iS0jrZ7LvzFGFmuKbw=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
Expand Down
22 changes: 12 additions & 10 deletions pkg/models/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@

package models

import "github.com/greenbone/opensight-golang-libraries/pkg/notifications"

Check failure on line 7 in pkg/models/notification.go

View workflow job for this annotation

GitHub Actions / Unit tests

github.com/greenbone/opensight-golang-libraries@v1.31.1: replacement directory ../opensight-golang-libraries does not exist

Check failure on line 7 in pkg/models/notification.go

View workflow job for this annotation

GitHub Actions / Run govulncheck

could not import github.com/greenbone/opensight-golang-libraries/pkg/notifications (invalid package name: "")

Check failure on line 7 in pkg/models/notification.go

View workflow job for this annotation

GitHub Actions / Run govulncheck

github.com/greenbone/opensight-golang-libraries@v1.31.1: replacement directory ../opensight-golang-libraries does not exist

// Notification is sent by a backend service. It will always be stored by the
// notification service and it will possibly also trigger actions like sending mails,
// depending on the cofigured rules.
// depending on the configured rules.
type Notification struct {
Id string `json:"id" readonly:"true"`
Origin string `json:"origin" binding:"required"` // name of the origin, e.g. `SBOM - React`
OriginClass string `json:"originClass"` // unique identifier for the class of origins, e.g. `/vi/SBOM`, for now optional for backwards compatibility, will be required in future
OriginResourceID string `json:"originResourceID,omitempty"` // together with class it can be used to provide a link to the origin, e.g. `<id of react sbom object>`
Timestamp string `json:"timestamp" binding:"required" format:"date-time"`
Title string `json:"title" binding:"required"`
Detail string `json:"detail" binding:"required"`
Level string `json:"level" binding:"required" enums:"info,warning,error,urgent"`
CustomFields map[string]any `json:"customFields,omitempty"` // can contain arbitrary structured information about the event
Id string `json:"id" readonly:"true"`
Origin string `json:"origin" binding:"required"` // name of the origin, e.g. `SBOM - React`
OriginClass string `json:"originClass"` // unique identifier for the class of origins, e.g. `/vi/SBOM`, for now optional for backwards compatibility, will be required in future
OriginResourceID string `json:"originResourceID,omitempty"` // together with class it can be used to provide a link to the origin, e.g. `<id of react sbom object>`
Timestamp string `json:"timestamp" binding:"required" format:"date-time"`
Title string `json:"title" binding:"required"`
Detail string `json:"detail" binding:"required"`
Level notifications.Level `json:"level" binding:"required" enums:"info,warning,error,urgent"`
CustomFields map[string]any `json:"customFields,omitempty"` // can contain arbitrary structured information about the event
}
16 changes: 14 additions & 2 deletions pkg/models/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ package models

import (
"fmt"
"slices"
"strings"

"github.com/greenbone/opensight-golang-libraries/pkg/notifications"
"github.com/greenbone/opensight-notification-service/pkg/entities"
"github.com/greenbone/opensight-notification-service/pkg/translation"
"github.com/greenbone/opensight-notification-service/pkg/validation"
)
Expand All @@ -24,10 +27,17 @@ type Rule struct {
Errors ValidationErrors `json:"errors,omitempty" readonly:"true"` // populated if the rule is invalid, this can be useful to highlight rules which need action from the user.
}

// RuleOptions Represents a list of all options required for the creation of a Rule
type RuleOptions struct {
Origins []entities.Origin `json:"origins"`
Comment thread
mgoetzegb marked this conversation as resolved.
Outdated
Levels []notifications.Level `json:"levels"`
Channels []NotificationChannel `json:"channels"`
Comment thread
mgoetzegb marked this conversation as resolved.
Outdated
}

// Trigger condition, fulfilled if both one of `origins` and `levels` match the ones from the incoming event.
type Trigger struct {
Origins []OriginReference `json:"origins" validate:"required"`
Levels []string `json:"levels" validate:"required"`
Origins []OriginReference `json:"origins" validate:"required"`
Levels []notifications.Level `json:"levels" validate:"required"`
}

// Action determines to which channel the event is forwarded.
Expand Down Expand Up @@ -78,6 +88,8 @@ func (r *Rule) Validate() ValidationErrors {
for i, level := range r.Trigger.Levels {
if level == "" {
errs[fmt.Sprintf("trigger.levels[%d]", i)] = translation.LevelIsRequired
} else if !slices.Contains(notifications.AllowedLevels, level) {
errs[fmt.Sprintf("trigger.levels[%d]", i)] = translation.InvalidLevel
}
}
}
Expand Down
19 changes: 10 additions & 9 deletions pkg/repository/notificationrepository/notification_db_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package notificationrepository
import (
"encoding/json"

"github.com/greenbone/opensight-golang-libraries/pkg/notifications"
"github.com/greenbone/opensight-notification-service/pkg/helper"
"github.com/greenbone/opensight-notification-service/pkg/models"
"github.com/greenbone/opensight-notification-service/pkg/services/notificationservice/dtos"
Expand All @@ -19,15 +20,15 @@ const (
)

type notificationRow struct {
Id string `db:"id"`
Origin string `db:"origin"`
OriginClass string `db:"origin_class"`
OriginResourceID *string `db:"origin_resource_id"`
Timestamp string `db:"timestamp"`
Title string `db:"title"`
Detail string `db:"detail"`
Level string `db:"level"`
CustomFields []byte `db:"custom_fields"`
Id string `db:"id"`
Origin string `db:"origin"`
OriginClass string `db:"origin_class"`
OriginResourceID *string `db:"origin_resource_id"`
Timestamp string `db:"timestamp"`
Title string `db:"title"`
Detail string `db:"detail"`
Level notifications.Level `db:"level"`
CustomFields []byte `db:"custom_fields"`
}

func notificationFieldMapping() map[string]string {
Expand Down
Loading
Loading