netbird/management/server/event.go

82 lines
2.1 KiB
Go
Raw Normal View History

package server
import (
"context"
"fmt"
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
"os"
"time"
2023-04-03 15:09:35 +02:00
log "github.com/sirupsen/logrus"
"github.com/netbirdio/netbird/management/server/activity"
"github.com/netbirdio/netbird/management/server/status"
)
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
func isEnabled() bool {
response := os.Getenv("NB_EVENT_ACTIVITY_LOG_ENABLED")
return response == "" || response == "true"
}
// GetEvents returns a list of activity events of an account
func (am *DefaultAccountManager) GetEvents(ctx context.Context, accountID, userID string) ([]*activity.Event, error) {
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
defer unlock()
account, err := am.Store.GetAccount(ctx, accountID)
if err != nil {
return nil, err
}
user, err := account.FindUser(userID)
if err != nil {
return nil, err
}
if !(user.HasAdminPower() || user.IsServiceUser) {
return nil, status.Errorf(status.PermissionDenied, "only users with admin power can view events")
}
events, err := am.eventStore.Get(ctx, accountID, 0, 10000, true)
if err != nil {
return nil, err
}
// this is a workaround for duplicate activity.UserJoined events that might occur when a user redeems invite.
// we will need to find a better way to handle this.
filtered := make([]*activity.Event, 0)
dups := make(map[string]struct{})
for _, event := range events {
if event.Activity == activity.UserJoined {
key := event.TargetID + event.InitiatorID + event.AccountID + fmt.Sprint(event.Activity)
_, duplicate := dups[key]
if duplicate {
continue
} else {
dups[key] = struct{}{}
}
}
filtered = append(filtered, event)
}
return filtered, nil
}
func (am *DefaultAccountManager) StoreEvent(ctx context.Context, initiatorID, targetID, accountID string, activityID activity.ActivityDescriber, meta map[string]any) {
MySQL Support (#2837) * Update store.go * Update sql_store.go * Update store.go * Update golang-test-linux.yml * Update store.go * Update go.mod * Update go.mod * Update go.sum * Update store.go * Update sql_store.go * TestContainer * Update go.sum * Update store.go * TestUtil Duplicate * dsn fix * go mod tidy * NETBIRD_STORE_ENGINE_MYSQL_DSN * Skip Test * Update test-infrastructure-files.yml * Update test-infrastructure-files.yml * MYSQL_ROOT_PASSWORD added * Update test-infrastructure-files.yml * Update store.go * Debug + Mysql JSON Query * swicth/case convert * Update store.go * Update store.go * Debug * MySQL Test Version Change * Root Test * Ignore other sql tests. * MySQL Connection Fix * enable other tests * The word "key" is a reserved word in MySQL. * Remove Debugs * Update sql_store.go * Added default null value for datetime. * Added default null value for datetime. * MySQL Hooks * MySQL Config File * remove default values * test timeout change * MySQL max lifetime change * WithConfigFile * disable other tests * Update mysql.cnf * Update golang-test-linux.yml * Delete sql_hooks.go * enable other tests * test timeout change * update packets * Fix the Inactivity Expiration problem * Update sql_store.go * Update mysql.cnf * Update sql_store.go * Update sql_store.go * timeout change * MySQL Connection LifeTime Change * TestContainers have been optimized. * Update store_ios.go * Update sql_store.go * timeout fix * fix migration (setup keys) * Update event.go * Add disable option for event activities. * Revert "Update event.go" * Update event.go * Fix Gorm Mysql Bug * update go-jose module * containerd module update * containerd downgrade * Revert commits * Revert "Revert commits" This reverts commit 62b3eac799825e0d3624904401fe67587ad8e780. * Revert "containerd downgrade" This reverts commit 4e46108915ea3b70f8a0234d4860c308a843c5a0. * Revert "containerd module update" This reverts commit e8cfa87d1688e0feeebf0c1ea0127578eba30bd3. * Revert "update go-jose module" This reverts commit 1fabdc760601e389589750daa6b2089148dd29fb.
2024-12-23 11:06:13 +01:00
if isEnabled() {
go func() {
_, err := am.eventStore.Save(ctx, &activity.Event{
Timestamp: time.Now().UTC(),
Activity: activityID,
InitiatorID: initiatorID,
TargetID: targetID,
AccountID: accountID,
Meta: meta,
})
if err != nil {
// todo add metric
log.WithContext(ctx).Errorf("received an error while storing an activity event, error: %s", err)
}
}()
}
}