mirror of
https://github.com/netbirdio/netbird.git
synced 2026-10-06 05:29:07 +02:00
Add group events
This commit is contained in:
@@ -63,7 +63,7 @@ type AccountManager interface {
|
|||||||
UpdatePeerSSHKey(peerKey string, sshKey string) error
|
UpdatePeerSSHKey(peerKey string, sshKey string) error
|
||||||
GetUsersFromAccount(accountID, userID string) ([]*UserInfo, error)
|
GetUsersFromAccount(accountID, userID string) ([]*UserInfo, error)
|
||||||
GetGroup(accountId, groupID string) (*Group, error)
|
GetGroup(accountId, groupID string) (*Group, error)
|
||||||
SaveGroup(accountId string, group *Group) error
|
SaveGroup(accountID, userID string, group *Group) error
|
||||||
UpdateGroup(accountID string, groupID string, operations []GroupUpdateOperation) (*Group, error)
|
UpdateGroup(accountID string, groupID string, operations []GroupUpdateOperation) (*Group, error)
|
||||||
DeleteGroup(accountId, groupID string) error
|
DeleteGroup(accountId, groupID string) error
|
||||||
ListGroups(accountId string) ([]*Group, error)
|
ListGroups(accountId string) ([]*Group, error)
|
||||||
|
|||||||
@@ -29,6 +29,10 @@ const (
|
|||||||
SetupKeyRevoked
|
SetupKeyRevoked
|
||||||
// SetupKeyOverused indicates that setup key usage exhausted
|
// SetupKeyOverused indicates that setup key usage exhausted
|
||||||
SetupKeyOverused
|
SetupKeyOverused
|
||||||
|
// GroupCreated indicates that a user created a group
|
||||||
|
GroupCreated
|
||||||
|
// GroupUpdated indicates that a user updated a group
|
||||||
|
GroupUpdated
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -58,6 +62,10 @@ const (
|
|||||||
SetupKeyRevokedMessage string = "Setup key revoked"
|
SetupKeyRevokedMessage string = "Setup key revoked"
|
||||||
// SetupKeyOverusedMessage is a human-readable text message of the SetupKeyOverused activity
|
// SetupKeyOverusedMessage is a human-readable text message of the SetupKeyOverused activity
|
||||||
SetupKeyOverusedMessage string = "Setup key overused"
|
SetupKeyOverusedMessage string = "Setup key overused"
|
||||||
|
// GroupCreatedMessage is a human-readable text message of the GroupCreated activity
|
||||||
|
GroupCreatedMessage string = "Group created"
|
||||||
|
// GroupUpdatedMessage is a human-readable text message of the GroupUpdated activity
|
||||||
|
GroupUpdatedMessage string = "Group updated"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Activity that triggered an Event
|
// Activity that triggered an Event
|
||||||
@@ -92,6 +100,10 @@ func (a Activity) Message() string {
|
|||||||
return SetupKeyRevokedMessage
|
return SetupKeyRevokedMessage
|
||||||
case SetupKeyOverused:
|
case SetupKeyOverused:
|
||||||
return SetupKeyOverusedMessage
|
return SetupKeyOverusedMessage
|
||||||
|
case GroupCreated:
|
||||||
|
return GroupCreatedMessage
|
||||||
|
case GroupUpdated:
|
||||||
|
return GroupUpdatedMessage
|
||||||
default:
|
default:
|
||||||
return "UNKNOWN_ACTIVITY"
|
return "UNKNOWN_ACTIVITY"
|
||||||
}
|
}
|
||||||
@@ -126,6 +138,10 @@ func (a Activity) StringCode() string {
|
|||||||
return "setupkey.overuse"
|
return "setupkey.overuse"
|
||||||
case SetupKeyUpdated:
|
case SetupKeyUpdated:
|
||||||
return "setupkey.update"
|
return "setupkey.update"
|
||||||
|
case GroupCreated:
|
||||||
|
return "group.add"
|
||||||
|
case GroupUpdated:
|
||||||
|
return "group.update"
|
||||||
default:
|
default:
|
||||||
return "UNKNOWN_ACTIVITY"
|
return "UNKNOWN_ACTIVITY"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package server
|
package server
|
||||||
|
|
||||||
import "github.com/netbirdio/netbird/management/server/status"
|
import (
|
||||||
|
"github.com/netbirdio/netbird/management/server/activity"
|
||||||
|
"github.com/netbirdio/netbird/management/server/status"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// Group of the peers for ACL
|
// Group of the peers for ACL
|
||||||
type Group struct {
|
type Group struct {
|
||||||
@@ -34,6 +38,11 @@ type GroupUpdateOperation struct {
|
|||||||
Values []string
|
Values []string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// EventMeta returns activity event meta related to the group
|
||||||
|
func (g *Group) EventMeta() map[string]any {
|
||||||
|
return map[string]any{"name": g.Name}
|
||||||
|
}
|
||||||
|
|
||||||
func (g *Group) Copy() *Group {
|
func (g *Group) Copy() *Group {
|
||||||
return &Group{
|
return &Group{
|
||||||
ID: g.ID,
|
ID: g.ID,
|
||||||
@@ -62,7 +71,7 @@ func (am *DefaultAccountManager) GetGroup(accountID, groupID string) (*Group, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
// SaveGroup object of the peers
|
// SaveGroup object of the peers
|
||||||
func (am *DefaultAccountManager) SaveGroup(accountID string, group *Group) error {
|
func (am *DefaultAccountManager) SaveGroup(accountID, userID string, group *Group) error {
|
||||||
|
|
||||||
unlock := am.Store.AcquireAccountLock(accountID)
|
unlock := am.Store.AcquireAccountLock(accountID)
|
||||||
defer unlock()
|
defer unlock()
|
||||||
@@ -71,7 +80,7 @@ func (am *DefaultAccountManager) SaveGroup(accountID string, group *Group) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
_, exists := account.Groups[group.ID]
|
||||||
account.Groups[group.ID] = group
|
account.Groups[group.ID] = group
|
||||||
|
|
||||||
account.Network.IncSerial()
|
account.Network.IncSerial()
|
||||||
@@ -79,6 +88,20 @@ func (am *DefaultAccountManager) SaveGroup(accountID string, group *Group) error
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !exists {
|
||||||
|
_, err = am.eventStore.Save(&activity.Event{
|
||||||
|
Timestamp: time.Now(),
|
||||||
|
Activity: activity.GroupCreated,
|
||||||
|
InitiatorID: userID,
|
||||||
|
TargetID: group.ID,
|
||||||
|
AccountID: accountID,
|
||||||
|
Meta: group.EventMeta(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return am.updateAccountPeers(account)
|
return am.updateAccountPeers(account)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -522,6 +522,7 @@ components:
|
|||||||
enum: [ "user.peer.delete", "user.join", "user.invite", "user.peer.add",
|
enum: [ "user.peer.delete", "user.join", "user.invite", "user.peer.add",
|
||||||
"setupkey.peer.add", "setupkey.add", "setupkey.update", "setupkey.revoke", "setupkey.overuse",
|
"setupkey.peer.add", "setupkey.add", "setupkey.update", "setupkey.revoke", "setupkey.overuse",
|
||||||
"rule.add", "rule.delete", "rule.update",
|
"rule.add", "rule.delete", "rule.update",
|
||||||
|
"group.add", "group.update",
|
||||||
"account.create",
|
"account.create",
|
||||||
]
|
]
|
||||||
initiator_id:
|
initiator_id:
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ const (
|
|||||||
// Defines values for EventActivityCode.
|
// Defines values for EventActivityCode.
|
||||||
const (
|
const (
|
||||||
EventActivityCodeAccountCreate EventActivityCode = "account.create"
|
EventActivityCodeAccountCreate EventActivityCode = "account.create"
|
||||||
|
EventActivityCodeGroupAdd EventActivityCode = "group.add"
|
||||||
|
EventActivityCodeGroupUpdate EventActivityCode = "group.update"
|
||||||
EventActivityCodeRuleAdd EventActivityCode = "rule.add"
|
EventActivityCodeRuleAdd EventActivityCode = "rule.add"
|
||||||
EventActivityCodeRuleDelete EventActivityCode = "rule.delete"
|
EventActivityCodeRuleDelete EventActivityCode = "rule.delete"
|
||||||
EventActivityCodeRuleUpdate EventActivityCode = "rule.update"
|
EventActivityCodeRuleUpdate EventActivityCode = "rule.update"
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ func (h *Groups) GetAllGroupsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// UpdateGroupHandler handles update to a group identified by a given ID
|
// UpdateGroupHandler handles update to a group identified by a given ID
|
||||||
func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
||||||
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.WriteError(err, w)
|
util.WriteError(err, w)
|
||||||
return
|
return
|
||||||
@@ -102,7 +102,7 @@ func (h *Groups) UpdateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
Peers: peerIPsToKeys(account, req.Peers),
|
Peers: peerIPsToKeys(account, req.Peers),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.accountManager.SaveGroup(account.Id, &group); err != nil {
|
if err := h.accountManager.SaveGroup(account.Id, user.Id, &group); err != nil {
|
||||||
log.Errorf("failed updating group %s under account %s %v", groupID, account.Id, err)
|
log.Errorf("failed updating group %s under account %s %v", groupID, account.Id, err)
|
||||||
util.WriteError(err, w)
|
util.WriteError(err, w)
|
||||||
return
|
return
|
||||||
@@ -219,7 +219,7 @@ func (h *Groups) PatchGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
// CreateGroupHandler handles group creation request
|
// CreateGroupHandler handles group creation request
|
||||||
func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
claims := h.jwtExtractor.ExtractClaimsFromRequestContext(r, h.authAudience)
|
||||||
account, _, err := h.accountManager.GetAccountFromToken(claims)
|
account, user, err := h.accountManager.GetAccountFromToken(claims)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.WriteError(err, w)
|
util.WriteError(err, w)
|
||||||
return
|
return
|
||||||
@@ -243,7 +243,7 @@ func (h *Groups) CreateGroupHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
Peers: peerIPsToKeys(account, req.Peers),
|
Peers: peerIPsToKeys(account, req.Peers),
|
||||||
}
|
}
|
||||||
|
|
||||||
err = h.accountManager.SaveGroup(account.Id, &group)
|
err = h.accountManager.SaveGroup(account.Id, user.Id, &group)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.WriteError(err, w)
|
util.WriteError(err, w)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ func (key *SetupKey) Copy() *SetupKey {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// EventMeta returns activity event meta related to the peer
|
// EventMeta returns activity event meta related to the setup key
|
||||||
func (key *SetupKey) EventMeta() map[string]any {
|
func (key *SetupKey) EventMeta() map[string]any {
|
||||||
return map[string]any{"name": key.Name, "type": key.Type, "key": key.HiddenCopy(1).Key}
|
return map[string]any{"name": key.Name, "type": key.Type, "key": key.HiddenCopy(1).Key}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user