2023-01-02 15:11:32 +01:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2023-01-02 15:11:32 +01:00
|
|
|
"fmt"
|
2023-02-03 21:47:20 +01:00
|
|
|
"net/http"
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
"github.com/netbirdio/netbird/management/server"
|
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/api"
|
|
|
|
"github.com/netbirdio/netbird/management/server/http/util"
|
|
|
|
"github.com/netbirdio/netbird/management/server/jwtclaims"
|
|
|
|
)
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// EventsHandler HTTP handler
|
|
|
|
type EventsHandler struct {
|
2023-02-03 21:47:20 +01:00
|
|
|
accountManager server.AccountManager
|
|
|
|
claimsExtractor *jwtclaims.ClaimsExtractor
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// NewEventsHandler creates a new EventsHandler HTTP handler
|
|
|
|
func NewEventsHandler(accountManager server.AccountManager, authCfg AuthCfg) *EventsHandler {
|
|
|
|
return &EventsHandler{
|
2023-01-02 15:11:32 +01:00
|
|
|
accountManager: accountManager,
|
2023-02-03 21:47:20 +01:00
|
|
|
claimsExtractor: jwtclaims.NewClaimsExtractor(
|
|
|
|
jwtclaims.WithAudience(authCfg.Audience),
|
|
|
|
jwtclaims.WithUserIDClaim(authCfg.UserIDClaim),
|
|
|
|
),
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 15:01:24 +01:00
|
|
|
// GetAllEvents list of the given account
|
|
|
|
func (h *EventsHandler) GetAllEvents(w http.ResponseWriter, r *http.Request) {
|
2023-02-03 21:47:20 +01:00
|
|
|
claims := h.claimsExtractor.FromRequestContext(r)
|
2024-09-27 16:10:50 +02:00
|
|
|
accountID, userID, err := h.accountManager.GetAccountIDFromToken(r.Context(), claims)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(r.Context()).Error(err)
|
2023-01-02 15:11:32 +01:00
|
|
|
http.Redirect(w, r, "/", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
accountEvents, err := h.accountManager.GetEvents(r.Context(), accountID, userID)
|
2023-01-02 15:11:32 +01:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-01-02 15:11:32 +01:00
|
|
|
return
|
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
events := make([]*api.Event, len(accountEvents))
|
|
|
|
for i, e := range accountEvents {
|
|
|
|
events[i] = toEventResponse(e)
|
|
|
|
}
|
|
|
|
|
2024-09-27 16:10:50 +02:00
|
|
|
err = h.fillEventsWithUserInfo(r.Context(), events, accountID, userID)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteError(r.Context(), err, w)
|
2023-09-19 18:08:40 +02:00
|
|
|
return
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
util.WriteJSONObject(r.Context(), w, events)
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-07-03 11:33:02 +02:00
|
|
|
func (h *EventsHandler) fillEventsWithUserInfo(ctx context.Context, events []*api.Event, accountId, userId string) error {
|
2023-09-23 10:47:49 +02:00
|
|
|
// build email, name maps based on users
|
2024-07-03 11:33:02 +02:00
|
|
|
userInfos, err := h.accountManager.GetUsersFromAccount(ctx, accountId, userId)
|
2023-09-19 18:08:40 +02:00
|
|
|
if err != nil {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Errorf("failed to get users from account: %s", err)
|
2023-09-19 18:08:40 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
emails := make(map[string]string)
|
2023-09-23 10:47:49 +02:00
|
|
|
names := make(map[string]string)
|
2023-09-19 18:08:40 +02:00
|
|
|
for _, ui := range userInfos {
|
|
|
|
emails[ui.ID] = ui.Email
|
2023-09-23 10:47:49 +02:00
|
|
|
names[ui.ID] = ui.Name
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
for _, event := range events {
|
2023-09-23 10:47:49 +02:00
|
|
|
// fill initiator
|
2023-09-19 18:08:40 +02:00
|
|
|
if event.InitiatorEmail == "" {
|
|
|
|
event.InitiatorEmail, ok = emails[event.InitiatorId]
|
|
|
|
if !ok {
|
2024-07-03 11:33:02 +02:00
|
|
|
log.WithContext(ctx).Warnf("failed to resolve email for initiator: %s", event.InitiatorId)
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
}
|
2023-09-23 10:47:49 +02:00
|
|
|
|
|
|
|
if event.InitiatorName == "" {
|
|
|
|
// here to allowed to be empty because in the first release we did not store the name
|
|
|
|
event.InitiatorName = names[event.InitiatorId]
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill target meta
|
|
|
|
email, ok := emails[event.TargetId]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
event.Meta["email"] = email
|
|
|
|
|
|
|
|
username, ok := names[event.TargetId]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
event.Meta["username"] = username
|
2023-09-19 18:08:40 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
func toEventResponse(event *activity.Event) *api.Event {
|
|
|
|
meta := make(map[string]string)
|
|
|
|
if event.Meta != nil {
|
|
|
|
for s, a := range event.Meta {
|
|
|
|
meta[s] = fmt.Sprintf("%v", a)
|
|
|
|
}
|
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
e := &api.Event{
|
|
|
|
Id: fmt.Sprint(event.ID),
|
|
|
|
InitiatorId: event.InitiatorID,
|
2023-09-23 10:47:49 +02:00
|
|
|
InitiatorName: event.InitiatorName,
|
2023-09-19 18:08:40 +02:00
|
|
|
InitiatorEmail: event.InitiatorEmail,
|
|
|
|
Activity: event.Activity.Message(),
|
|
|
|
ActivityCode: api.EventActivityCode(event.Activity.StringCode()),
|
|
|
|
TargetId: event.TargetID,
|
|
|
|
Timestamp: event.Timestamp,
|
|
|
|
Meta: meta,
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
2023-09-19 18:08:40 +02:00
|
|
|
return e
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|