-
Notifications
You must be signed in to change notification settings - Fork 9
/
component.go
93 lines (76 loc) · 3.08 KB
/
component.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package events
import (
"context"
"github.com/cloudtrust/common-service/database"
errorhandler "github.com/cloudtrust/common-service/errors"
api "github.com/cloudtrust/keycloak-bridge/api/events"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
app "github.com/cloudtrust/keycloak-bridge/internal/keycloakb"
)
// Component is the interface of the events component.
type Component interface {
GetActions(ctx context.Context) ([]api.ActionRepresentation, error)
GetEvents(context.Context, map[string]string) (api.AuditEventsRepresentation, error)
GetEventsSummary(context.Context) (api.EventSummaryRepresentation, error)
GetUserEvents(context.Context, map[string]string) (api.AuditEventsRepresentation, error)
}
type component struct {
db app.EventsDBModule
eventDBModule database.EventsDBModule
logger app.Logger
}
// NewComponent returns a component
func NewComponent(db app.EventsDBModule, eventDBModule database.EventsDBModule, logger app.Logger) Component {
return &component{
db: db,
eventDBModule: eventDBModule,
logger: logger,
}
}
func (ec *component) reportEvent(ctx context.Context, apiCall string, values ...string) {
errEvent := ec.eventDBModule.ReportEvent(ctx, apiCall, "back-office", values...)
if errEvent != nil {
//store in the logs also the event that failed to be stored in the DB
app.LogUnrecordedEvent(ctx, ec.logger, apiCall, errEvent.Error(), values...)
}
}
// Get actions
func (ec *component) GetActions(ctx context.Context) ([]api.ActionRepresentation, error) {
var apiActions = []api.ActionRepresentation{}
for _, action := range actions {
var name = action.Name
var scope = string(action.Scope)
apiActions = append(apiActions, api.ActionRepresentation{
Name: &name,
Scope: &scope,
})
}
return apiActions, nil
}
// Get events according to optional parameters
func (ec *component) GetEvents(ctx context.Context, params map[string]string) (api.AuditEventsRepresentation, error) {
var empty [0]api.AuditRepresentation
var res api.AuditEventsRepresentation
var err error
res.Events = empty[:]
res.Count, err = ec.db.GetEventsCount(ctx, params)
if err == nil && res.Count > 0 {
res.Events, err = ec.db.GetEvents(ctx, params)
}
return res, err
}
// Get all possible values for origin and ctEventType
func (ec *component) GetEventsSummary(ctx context.Context) (api.EventSummaryRepresentation, error) {
return ec.db.GetEventsSummary(ctx)
}
// Get all events related to a given realm and a given user
func (ec *component) GetUserEvents(ctx context.Context, params map[string]string) (api.AuditEventsRepresentation, error) {
if val, ok := params[prmPathRealm]; !ok || len(val) == 0 {
return api.AuditEventsRepresentation{}, errorhandler.CreateMissingParameterError(msg.Realm)
}
if val, ok := params[prmPathUserID]; !ok || len(val) == 0 {
return api.AuditEventsRepresentation{}, errorhandler.CreateMissingParameterError(msg.UserID)
}
ec.reportEvent(ctx, "GET_ACTIVITY", database.CtEventRealmName, params[prmPathRealm], database.CtEventUserID, params[prmPathUserID])
return ec.GetEvents(ctx, params)
}