Skip to content

Commit 5afbada

Browse files
committed
add: add register origins endpoint
1 parent 81d1b21 commit 5afbada

17 files changed

Lines changed: 1226 additions & 7 deletions

File tree

api/notificationservice/notificationservice_docs.go

Lines changed: 119 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,7 @@ const docTemplatenotificationservice = `{
874874
"KeycloakAuth": []
875875
}
876876
],
877-
"description": "Create a new notification",
877+
"description": "Create a new notification. It will always be stored by the notification service and it will possibly also trigger actions like sending mails, depending on the cofigured rules.",
878878
"consumes": [
879879
"application/json"
880880
],
@@ -942,9 +942,105 @@ const docTemplatenotificationservice = `{
942942
}
943943
}
944944
}
945+
},
946+
"/origins/{serviceID}": {
947+
"put": {
948+
"security": [
949+
{
950+
"KeycloakAuth": []
951+
}
952+
],
953+
"description": "Registers a set of origins in the given service. Replaces origins of this service if they already existed. The origins can be ulitized to set trigger conditions for actions.",
954+
"consumes": [
955+
"application/json"
956+
],
957+
"produces": [
958+
"application/json"
959+
],
960+
"tags": [
961+
"origin"
962+
],
963+
"summary": "Register Origins",
964+
"parameters": [
965+
{
966+
"type": "string",
967+
"description": "serviceID of the calling service, needs to be unique among all services registering origins",
968+
"name": "serviceID",
969+
"in": "path",
970+
"required": true
971+
},
972+
{
973+
"description": "origins provided by the calling service",
974+
"name": "origins",
975+
"in": "body",
976+
"required": true,
977+
"schema": {
978+
"type": "array",
979+
"items": {
980+
"$ref": "#/definitions/models.Origin"
981+
}
982+
}
983+
}
984+
],
985+
"responses": {
986+
"204": {
987+
"description": "origins registered",
988+
"headers": {
989+
"api-version": {
990+
"type": "string",
991+
"description": "API version"
992+
}
993+
}
994+
},
995+
"400": {
996+
"description": "Bad Request",
997+
"schema": {
998+
"$ref": "#/definitions/errorResponses.ErrorResponse"
999+
},
1000+
"headers": {
1001+
"api-version": {
1002+
"type": "string",
1003+
"description": "API version"
1004+
}
1005+
}
1006+
},
1007+
"500": {
1008+
"description": "Internal Server Error",
1009+
"schema": {
1010+
"$ref": "#/definitions/errorResponses.ErrorResponse"
1011+
},
1012+
"headers": {
1013+
"api-version": {
1014+
"type": "string",
1015+
"description": "API version"
1016+
}
1017+
}
1018+
}
1019+
}
1020+
}
9451021
}
9461022
},
9471023
"definitions": {
1024+
"errorResponses.ErrorResponse": {
1025+
"type": "object",
1026+
"properties": {
1027+
"details": {
1028+
"type": "string"
1029+
},
1030+
"errors": {
1031+
"type": "object",
1032+
"additionalProperties": {
1033+
"type": "string"
1034+
}
1035+
},
1036+
"title": {
1037+
"type": "string"
1038+
},
1039+
"type": {
1040+
"type": "string"
1041+
}
1042+
}
1043+
},
9481044
"filter.CompareOperator": {
9491045
"type": "string",
9501046
"enum": [
@@ -1274,6 +1370,28 @@ const docTemplatenotificationservice = `{
12741370
}
12751371
}
12761372
},
1373+
"models.Origin": {
1374+
"type": "object",
1375+
"required": [
1376+
"class",
1377+
"name"
1378+
],
1379+
"properties": {
1380+
"class": {
1381+
"description": "unique identifier",
1382+
"type": "string"
1383+
},
1384+
"name": {
1385+
"description": "human readable name representation",
1386+
"type": "string"
1387+
},
1388+
"serviceID": {
1389+
"description": "service in which this origin is defined",
1390+
"type": "string",
1391+
"readOnly": true
1392+
}
1393+
}
1394+
},
12771395
"paging.Request": {
12781396
"type": "object",
12791397
"properties": {

api/notificationservice/notificationservice_swagger.yaml

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
basePath: /api/notification-service
22
definitions:
3+
errorResponses.ErrorResponse:
4+
properties:
5+
details:
6+
type: string
7+
errors:
8+
additionalProperties:
9+
type: string
10+
type: object
11+
title:
12+
type: string
13+
type:
14+
type: string
15+
type: object
316
filter.CompareOperator:
417
enum:
518
- beginsWith
@@ -250,6 +263,22 @@ definitions:
250263
- timestamp
251264
- title
252265
type: object
266+
models.Origin:
267+
properties:
268+
class:
269+
description: unique identifier
270+
type: string
271+
name:
272+
description: human readable name representation
273+
type: string
274+
serviceID:
275+
description: service in which this origin is defined
276+
readOnly: true
277+
type: string
278+
required:
279+
- class
280+
- name
281+
type: object
253282
paging.Request:
254283
properties:
255284
index:
@@ -905,7 +934,9 @@ paths:
905934
post:
906935
consumes:
907936
- application/json
908-
description: Create a new notification
937+
description: Create a new notification. It will always be stored by the notification
938+
service and it will possibly also trigger actions like sending mails, depending
939+
on the cofigured rules.
909940
parameters:
910941
- description: notification to add
911942
in: body
@@ -975,6 +1006,58 @@ paths:
9751006
summary: Notification filter options
9761007
tags:
9771008
- notification
1009+
/origins/{serviceID}:
1010+
put:
1011+
consumes:
1012+
- application/json
1013+
description: Registers a set of origins in the given service. Replaces origins
1014+
of this service if they already existed. The origins can be ulitized to set
1015+
trigger conditions for actions.
1016+
parameters:
1017+
- description: serviceID of the calling service, needs to be unique among all
1018+
services registering origins
1019+
in: path
1020+
name: serviceID
1021+
required: true
1022+
type: string
1023+
- description: origins provided by the calling service
1024+
in: body
1025+
name: origins
1026+
required: true
1027+
schema:
1028+
items:
1029+
$ref: '#/definitions/models.Origin'
1030+
type: array
1031+
produces:
1032+
- application/json
1033+
responses:
1034+
"204":
1035+
description: origins registered
1036+
headers:
1037+
api-version:
1038+
description: API version
1039+
type: string
1040+
"400":
1041+
description: Bad Request
1042+
headers:
1043+
api-version:
1044+
description: API version
1045+
type: string
1046+
schema:
1047+
$ref: '#/definitions/errorResponses.ErrorResponse'
1048+
"500":
1049+
description: Internal Server Error
1050+
headers:
1051+
api-version:
1052+
description: API version
1053+
type: string
1054+
schema:
1055+
$ref: '#/definitions/errorResponses.ErrorResponse'
1056+
security:
1057+
- KeycloakAuth: []
1058+
summary: Register Origins
1059+
tags:
1060+
- origin
9781061
securityDefinitions:
9791062
KeycloakAuth:
9801063
authorizationUrl: '{{.KeycloakAuthUrl}}/realms/{{.KeycloakRealm}}/protocol/openid-connect/auth'

cmd/notification-service/main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ import (
3333
"github.com/greenbone/opensight-notification-service/pkg/config/secretfiles"
3434
"github.com/greenbone/opensight-notification-service/pkg/repository"
3535
"github.com/greenbone/opensight-notification-service/pkg/repository/notificationrepository"
36+
"github.com/greenbone/opensight-notification-service/pkg/repository/originrepository"
3637
"github.com/greenbone/opensight-notification-service/pkg/services/healthservice"
3738
"github.com/greenbone/opensight-notification-service/pkg/services/notificationservice"
39+
"github.com/greenbone/opensight-notification-service/pkg/services/originservice"
3840
"github.com/greenbone/opensight-notification-service/pkg/web"
3941
"github.com/greenbone/opensight-notification-service/pkg/web/healthcontroller"
4042
"github.com/greenbone/opensight-notification-service/pkg/web/notificationcontroller"
43+
"github.com/greenbone/opensight-notification-service/pkg/web/origincontroller"
4144
)
4245

4346
func main() {
@@ -102,6 +105,10 @@ func run(config config.Config) error {
102105
if err != nil {
103106
return fmt.Errorf("error creating Notification Repository: %w", err)
104107
}
108+
originsRepository, err := originrepository.NewOriginRepository(pgClient)
109+
if err != nil {
110+
return err
111+
}
105112

106113
// Encrypt
107114
manager := security.NewEncryptManager()
@@ -118,6 +125,7 @@ func run(config config.Config) error {
118125
mailChannelService := notificationchannelservice.NewMailChannelService(notificationChannelService, notificationChannelRepository, mailService, config.ChannelLimit.EMailLimit)
119126
mattermostChannelService := notificationchannelservice.NewMattermostChannelService(notificationChannelService, config.ChannelLimit.MattermostLimit)
120127
teamsChannelService := notificationchannelservice.NewTeamsChannelService(notificationChannelService, config.ChannelLimit.TeamsLimit)
128+
originService := originservice.NewOriginService(originsRepository)
121129
healthService := healthservice.NewHealthService(pgClient)
122130

123131
// scheduler
@@ -150,6 +158,7 @@ func run(config config.Config) error {
150158
mailcontroller.AddCheckMailServerController(notificationServiceRouter, mailChannelService, authMiddleware, registry)
151159
mattermostcontroller.NewMattermostController(notificationServiceRouter, notificationChannelService, mattermostChannelService, authMiddleware, registry)
152160
teamsController.AddTeamsController(notificationServiceRouter, notificationChannelRepository, teamsChannelService, authMiddleware, registry)
161+
origincontroller.NewOriginController(notificationServiceRouter, originService, authMiddleware)
153162

154163
// health router
155164
rootRouter := router.Group("/")

docker-compose.service.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ services:
2121
ports:
2222
- 8085:8085
2323
networks:
24-
- notification-service-net
24+
- opensight-notification-net
2525
depends_on:
2626
postgres:
2727
condition: service_healthy

pkg/entities/origin.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// SPDX-FileCopyrightText: 2026 Greenbone AG <https://greenbone.net>
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-or-later
4+
5+
package entities
6+
7+
type Origin struct {
8+
Name string
9+
Class string
10+
ServiceID string // read-only
11+
}

pkg/models/origin.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-FileCopyrightText: 2025 Greenbone AG <https://greenbone.net>
2+
//
3+
// SPDX-License-Identifier: AGPL-3.0-or-later
4+
5+
package models
6+
7+
import "github.com/greenbone/opensight-notification-service/pkg/entities"
8+
9+
// Origin of an event/notification.
10+
type Origin struct {
11+
Name string `json:"name" binding:"required"` // human readable name representation
12+
Class string `json:"class" binding:"required"` // unique identifier
13+
ServiceID string `json:"serviceID" readonly:"true"` // service in which this origin is defined
14+
}
15+
16+
// ToEntity transforms the rest model to the entity for use in the service
17+
func (o Origin) ToEntity() entities.Origin {
18+
return entities.Origin(o)
19+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE notification_service.origins (
2+
"name" TEXT NOT NULL,
3+
"class" TEXT NOT NULL CONSTRAINT origins_class_unique UNIQUE,
4+
"service_id" TEXT NOT NULL
5+
);
6+
7+
CREATE INDEX idx_origins_service_id ON notification_service.origins(service_id);

0 commit comments

Comments
 (0)