2021-08-19 21:12:21 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-07-03 11:33:02 +02:00
|
|
|
"context"
|
2024-10-28 17:52:23 +01:00
|
|
|
"crypto/sha256"
|
|
|
|
b64 "encoding/base64"
|
2021-08-20 22:33:43 +02:00
|
|
|
"hash/fnv"
|
2024-11-11 17:46:10 +01:00
|
|
|
"slices"
|
2021-08-20 22:33:43 +02:00
|
|
|
"strconv"
|
2021-08-19 21:12:21 +02:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-11-05 10:24:50 +01:00
|
|
|
"unicode/utf8"
|
2023-04-03 15:09:35 +02:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-11-15 17:00:06 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-04-03 15:09:35 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/activity"
|
|
|
|
"github.com/netbirdio/netbird/management/server/status"
|
2021-08-19 21:12:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SetupKeyReusable is a multi-use key (can be used for multiple machines)
|
|
|
|
SetupKeyReusable SetupKeyType = "reusable"
|
|
|
|
// SetupKeyOneOff is a single use key (can be used only once)
|
|
|
|
SetupKeyOneOff SetupKeyType = "one-off"
|
|
|
|
|
|
|
|
// DefaultSetupKeyDuration = 1 month
|
|
|
|
DefaultSetupKeyDuration = 24 * 30 * time.Hour
|
|
|
|
// DefaultSetupKeyName is a default name of the default setup key
|
|
|
|
DefaultSetupKeyName = "Default key"
|
2022-12-05 13:09:59 +01:00
|
|
|
// SetupKeyUnlimitedUsage indicates an unlimited usage of a setup key
|
|
|
|
SetupKeyUnlimitedUsage = 0
|
|
|
|
)
|
2022-09-11 23:16:40 +02:00
|
|
|
|
2022-12-05 13:09:59 +01:00
|
|
|
const (
|
2022-09-11 23:16:40 +02:00
|
|
|
// UpdateSetupKeyName indicates a setup key name update operation
|
|
|
|
UpdateSetupKeyName SetupKeyUpdateOperationType = iota
|
|
|
|
// UpdateSetupKeyRevoked indicates a setup key revoked filed update operation
|
|
|
|
UpdateSetupKeyRevoked
|
|
|
|
// UpdateSetupKeyAutoGroups indicates a setup key auto-assign groups update operation
|
|
|
|
UpdateSetupKeyAutoGroups
|
|
|
|
// UpdateSetupKeyExpiresAt indicates a setup key expiration time update operation
|
|
|
|
UpdateSetupKeyExpiresAt
|
2021-08-19 21:12:21 +02:00
|
|
|
)
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
// SetupKeyUpdateOperationType operation type
|
|
|
|
type SetupKeyUpdateOperationType int
|
|
|
|
|
|
|
|
func (t SetupKeyUpdateOperationType) String() string {
|
|
|
|
switch t {
|
|
|
|
case UpdateSetupKeyName:
|
|
|
|
return "UpdateSetupKeyName"
|
|
|
|
case UpdateSetupKeyRevoked:
|
|
|
|
return "UpdateSetupKeyRevoked"
|
|
|
|
case UpdateSetupKeyAutoGroups:
|
|
|
|
return "UpdateSetupKeyAutoGroups"
|
|
|
|
case UpdateSetupKeyExpiresAt:
|
|
|
|
return "UpdateSetupKeyExpiresAt"
|
|
|
|
default:
|
|
|
|
return "InvalidOperation"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetupKeyUpdateOperation operation object with type and values to be applied
|
|
|
|
type SetupKeyUpdateOperation struct {
|
|
|
|
Type SetupKeyUpdateOperationType
|
|
|
|
Values []string
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:21 +02:00
|
|
|
// SetupKeyType is the type of setup key
|
|
|
|
type SetupKeyType string
|
|
|
|
|
|
|
|
// SetupKey represents a pre-authorized key used to register machines (peers)
|
|
|
|
type SetupKey struct {
|
2023-10-12 15:42:36 +02:00
|
|
|
Id string
|
|
|
|
// AccountID is a reference to Account that this object belongs
|
|
|
|
AccountID string `json:"-" gorm:"index"`
|
2021-08-19 21:12:21 +02:00
|
|
|
Key string
|
2024-10-28 17:52:23 +01:00
|
|
|
KeySecret string
|
2021-08-19 21:12:21 +02:00
|
|
|
Name string
|
|
|
|
Type SetupKeyType
|
|
|
|
CreatedAt time.Time
|
|
|
|
ExpiresAt time.Time
|
2023-10-12 15:42:36 +02:00
|
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime:false"`
|
2021-08-19 21:12:21 +02:00
|
|
|
// Revoked indicates whether the key was revoked or not (we don't remove them for tracking purposes)
|
|
|
|
Revoked bool
|
|
|
|
// UsedTimes indicates how many times the key was used
|
|
|
|
UsedTimes int
|
2021-08-22 11:29:25 +02:00
|
|
|
// LastUsed last time the key was used for peer registration
|
|
|
|
LastUsed time.Time
|
2022-09-11 23:16:40 +02:00
|
|
|
// AutoGroups is a list of Group IDs that are auto assigned to a Peer when it uses this key to register
|
2023-10-12 15:42:36 +02:00
|
|
|
AutoGroups []string `gorm:"serializer:json"`
|
2022-12-05 13:09:59 +01:00
|
|
|
// UsageLimit indicates the number of times this key can be used to enroll a machine.
|
|
|
|
// The value of 0 indicates the unlimited usage.
|
|
|
|
UsageLimit int
|
2023-09-04 11:37:39 +02:00
|
|
|
// Ephemeral indicate if the peers will be ephemeral or not
|
|
|
|
Ephemeral bool
|
2021-08-19 21:12:21 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
// Copy copies SetupKey to a new object
|
2021-08-20 22:33:43 +02:00
|
|
|
func (key *SetupKey) Copy() *SetupKey {
|
2023-08-22 17:56:39 +02:00
|
|
|
autoGroups := make([]string, len(key.AutoGroups))
|
|
|
|
copy(autoGroups, key.AutoGroups)
|
2022-09-11 23:16:40 +02:00
|
|
|
if key.UpdatedAt.IsZero() {
|
|
|
|
key.UpdatedAt = key.CreatedAt
|
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
return &SetupKey{
|
2022-09-11 23:16:40 +02:00
|
|
|
Id: key.Id,
|
2023-10-12 15:42:36 +02:00
|
|
|
AccountID: key.AccountID,
|
2022-09-11 23:16:40 +02:00
|
|
|
Key: key.Key,
|
2024-10-28 17:52:23 +01:00
|
|
|
KeySecret: key.KeySecret,
|
2022-09-11 23:16:40 +02:00
|
|
|
Name: key.Name,
|
|
|
|
Type: key.Type,
|
|
|
|
CreatedAt: key.CreatedAt,
|
|
|
|
ExpiresAt: key.ExpiresAt,
|
|
|
|
UpdatedAt: key.UpdatedAt,
|
|
|
|
Revoked: key.Revoked,
|
|
|
|
UsedTimes: key.UsedTimes,
|
|
|
|
LastUsed: key.LastUsed,
|
2022-09-13 17:19:03 +02:00
|
|
|
AutoGroups: autoGroups,
|
2022-12-05 13:09:59 +01:00
|
|
|
UsageLimit: key.UsageLimit,
|
2023-09-04 11:37:39 +02:00
|
|
|
Ephemeral: key.Ephemeral,
|
2021-08-20 22:33:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
// EventMeta returns activity event meta related to the setup key
|
|
|
|
func (key *SetupKey) EventMeta() map[string]any {
|
2024-10-28 17:52:23 +01:00
|
|
|
return map[string]any{"name": key.Name, "type": key.Type, "key": key.KeySecret}
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
// hiddenKey returns the Key value hidden with "*" and a 5 character prefix.
|
2022-11-05 10:24:50 +01:00
|
|
|
// E.g., "831F6*******************************"
|
2024-10-28 17:52:23 +01:00
|
|
|
func hiddenKey(key string, length int) string {
|
|
|
|
prefix := key[0:5]
|
|
|
|
if length > utf8.RuneCountInString(key) {
|
|
|
|
length = utf8.RuneCountInString(key) - len(prefix)
|
|
|
|
}
|
|
|
|
return prefix + strings.Repeat("*", length)
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
// IncrementUsage makes a copy of a key, increments the UsedTimes by 1 and sets LastUsed to now
|
2021-08-22 11:29:25 +02:00
|
|
|
func (key *SetupKey) IncrementUsage() *SetupKey {
|
|
|
|
c := key.Copy()
|
2023-11-27 16:40:02 +01:00
|
|
|
c.UsedTimes++
|
2023-04-03 15:09:35 +02:00
|
|
|
c.LastUsed = time.Now().UTC()
|
2021-08-22 11:29:25 +02:00
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2021-08-19 21:12:21 +02:00
|
|
|
// IsValid is true if the key was not revoked, is not expired and used not more than it was supposed to
|
|
|
|
func (key *SetupKey) IsValid() bool {
|
2021-08-25 14:16:17 +02:00
|
|
|
return !key.IsRevoked() && !key.IsExpired() && !key.IsOverUsed()
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRevoked if key was revoked
|
|
|
|
func (key *SetupKey) IsRevoked() bool {
|
|
|
|
return key.Revoked
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsExpired if key was expired
|
|
|
|
func (key *SetupKey) IsExpired() bool {
|
2024-10-28 17:52:23 +01:00
|
|
|
if key.ExpiresAt.IsZero() {
|
|
|
|
return false
|
|
|
|
}
|
2023-04-10 10:54:23 +02:00
|
|
|
return time.Now().After(key.ExpiresAt)
|
2021-08-25 14:16:17 +02:00
|
|
|
}
|
|
|
|
|
2022-12-05 13:09:59 +01:00
|
|
|
// IsOverUsed if the key was used too many times. SetupKey.UsageLimit == 0 indicates the unlimited usage.
|
2021-08-25 14:16:17 +02:00
|
|
|
func (key *SetupKey) IsOverUsed() bool {
|
2022-12-05 13:09:59 +01:00
|
|
|
limit := key.UsageLimit
|
|
|
|
if key.Type == SetupKeyOneOff {
|
|
|
|
limit = 1
|
|
|
|
}
|
|
|
|
return limit > 0 && key.UsedTimes >= limit
|
2021-08-19 21:12:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GenerateSetupKey generates a new setup key
|
2022-12-05 13:09:59 +01:00
|
|
|
func GenerateSetupKey(name string, t SetupKeyType, validFor time.Duration, autoGroups []string,
|
2024-10-28 17:52:23 +01:00
|
|
|
usageLimit int, ephemeral bool) (*SetupKey, string) {
|
2021-08-20 22:33:43 +02:00
|
|
|
key := strings.ToUpper(uuid.New().String())
|
2022-12-05 13:09:59 +01:00
|
|
|
limit := usageLimit
|
|
|
|
if t == SetupKeyOneOff {
|
|
|
|
limit = 1
|
|
|
|
}
|
2024-10-28 17:52:23 +01:00
|
|
|
|
|
|
|
expiresAt := time.Time{}
|
|
|
|
if validFor != 0 {
|
|
|
|
expiresAt = time.Now().UTC().Add(validFor)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashedKey := sha256.Sum256([]byte(key))
|
|
|
|
encodedHashedKey := b64.StdEncoding.EncodeToString(hashedKey[:])
|
|
|
|
|
2021-08-19 21:12:21 +02:00
|
|
|
return &SetupKey{
|
2022-09-11 23:16:40 +02:00
|
|
|
Id: strconv.Itoa(int(Hash(key))),
|
2024-10-28 17:52:23 +01:00
|
|
|
Key: encodedHashedKey,
|
|
|
|
KeySecret: hiddenKey(key, 4),
|
2022-09-11 23:16:40 +02:00
|
|
|
Name: name,
|
|
|
|
Type: t,
|
2023-04-03 15:09:35 +02:00
|
|
|
CreatedAt: time.Now().UTC(),
|
2024-10-28 17:52:23 +01:00
|
|
|
ExpiresAt: expiresAt,
|
2023-04-03 15:09:35 +02:00
|
|
|
UpdatedAt: time.Now().UTC(),
|
2022-09-11 23:16:40 +02:00
|
|
|
Revoked: false,
|
|
|
|
UsedTimes: 0,
|
|
|
|
AutoGroups: autoGroups,
|
2022-12-05 13:09:59 +01:00
|
|
|
UsageLimit: limit,
|
2023-09-04 11:37:39 +02:00
|
|
|
Ephemeral: ephemeral,
|
2024-10-28 17:52:23 +01:00
|
|
|
}, key
|
2021-08-19 21:12:21 +02:00
|
|
|
}
|
2021-08-20 15:01:57 +02:00
|
|
|
|
2022-12-05 13:09:59 +01:00
|
|
|
// GenerateDefaultSetupKey generates a default reusable setup key with an unlimited usage and 30 days expiration
|
2024-10-28 17:52:23 +01:00
|
|
|
func GenerateDefaultSetupKey() (*SetupKey, string) {
|
2022-12-05 13:09:59 +01:00
|
|
|
return GenerateSetupKey(DefaultSetupKeyName, SetupKeyReusable, DefaultSetupKeyDuration, []string{},
|
2023-09-04 11:37:39 +02:00
|
|
|
SetupKeyUnlimitedUsage, false)
|
2021-08-20 15:01:57 +02:00
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
|
|
|
func Hash(s string) uint32 {
|
|
|
|
h := fnv.New32a()
|
|
|
|
_, err := h.Write([]byte(s))
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
return h.Sum32()
|
|
|
|
}
|
2022-09-11 23:16:40 +02:00
|
|
|
|
|
|
|
// CreateSetupKey generates a new setup key with a given name, type, list of groups IDs to auto-assign to peers registered with this key,
|
|
|
|
// and adds it to the specified account. A list of autoGroups IDs can be empty.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) CreateSetupKey(ctx context.Context, accountID string, keyName string, keyType SetupKeyType,
|
2023-09-04 11:37:39 +02:00
|
|
|
expiresIn time.Duration, autoGroups []string, usageLimit int, userID string, ephemeral bool) (*SetupKey, error) {
|
2024-07-31 14:53:32 +02:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
2022-11-07 17:52:23 +01:00
|
|
|
defer unlock()
|
2022-09-11 23:16:40 +02:00
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
var setupKey *SetupKey
|
|
|
|
var plainKey string
|
|
|
|
var eventsToStore []func()
|
2023-01-02 15:11:32 +01:00
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
if err = validateSetupKeyAutoGroups(ctx, transaction, accountID, autoGroups); err != nil {
|
|
|
|
return err
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
2024-11-11 17:46:10 +01:00
|
|
|
|
|
|
|
setupKey, plainKey = GenerateSetupKey(keyName, keyType, expiresIn, autoGroups, usageLimit, ephemeral)
|
|
|
|
setupKey.AccountID = accountID
|
|
|
|
|
|
|
|
events := am.prepareSetupKeyEvents(ctx, transaction, accountID, userID, autoGroups, nil, setupKey)
|
|
|
|
eventsToStore = append(eventsToStore, events...)
|
|
|
|
|
|
|
|
return transaction.SaveSetupKey(ctx, LockingStrengthUpdate, setupKey)
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
am.StoreEvent(ctx, userID, setupKey.Id, accountID, activity.SetupKeyCreated, setupKey.EventMeta())
|
|
|
|
for _, storeEvent := range eventsToStore {
|
|
|
|
storeEvent()
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
// for the creation return the plain key to the caller
|
|
|
|
setupKey.Key = plainKey
|
|
|
|
|
2022-09-11 23:16:40 +02:00
|
|
|
return setupKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SaveSetupKey saves the provided SetupKey to the database overriding the existing one.
|
|
|
|
// Due to the unique nature of a SetupKey certain properties must not be overwritten
|
|
|
|
// (e.g. the key itself, creation date, ID, etc).
|
2024-11-15 17:00:06 +01:00
|
|
|
// These properties are overwritten: AutoGroups, Revoked (only from false to true), and the UpdatedAt. The rest is copied from the existing key.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) SaveSetupKey(ctx context.Context, accountID string, keyToSave *SetupKey, userID string) (*SetupKey, error) {
|
2022-09-11 23:16:40 +02:00
|
|
|
if keyToSave == nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, status.Errorf(status.InvalidArgument, "provided setup key to update is nil")
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
unlock := am.Store.AcquireWriteLockByUID(ctx, accountID)
|
|
|
|
defer unlock()
|
|
|
|
|
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
2024-08-12 12:48:05 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
var oldKey *SetupKey
|
|
|
|
var newKey *SetupKey
|
|
|
|
var eventsToStore []func()
|
|
|
|
|
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
if err = validateSetupKeyAutoGroups(ctx, transaction, accountID, keyToSave.AutoGroups); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-11 23:16:40 +02:00
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
oldKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyToSave.Id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-11 23:16:40 +02:00
|
|
|
|
2024-11-15 17:00:06 +01:00
|
|
|
if oldKey.Revoked && !keyToSave.Revoked {
|
|
|
|
return status.Errorf(status.InvalidArgument, "can't un-revoke a revoked setup key")
|
|
|
|
}
|
|
|
|
|
|
|
|
// only auto groups, revoked status (from false to true) can be updated
|
2024-11-11 17:46:10 +01:00
|
|
|
newKey = oldKey.Copy()
|
|
|
|
newKey.AutoGroups = keyToSave.AutoGroups
|
|
|
|
newKey.Revoked = keyToSave.Revoked
|
|
|
|
newKey.UpdatedAt = time.Now().UTC()
|
|
|
|
|
|
|
|
addedGroups := difference(newKey.AutoGroups, oldKey.AutoGroups)
|
|
|
|
removedGroups := difference(oldKey.AutoGroups, newKey.AutoGroups)
|
|
|
|
|
|
|
|
events := am.prepareSetupKeyEvents(ctx, transaction, accountID, userID, addedGroups, removedGroups, oldKey)
|
|
|
|
eventsToStore = append(eventsToStore, events...)
|
|
|
|
|
|
|
|
return transaction.SaveSetupKey(ctx, LockingStrengthUpdate, newKey)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2022-09-11 23:16:40 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-01-02 15:11:32 +01:00
|
|
|
if !oldKey.Revoked && newKey.Revoked {
|
2024-07-03 11:33:02 +02:00
|
|
|
am.StoreEvent(ctx, userID, newKey.Id, accountID, activity.SetupKeyRevoked, newKey.EventMeta())
|
2023-01-02 15:11:32 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
for _, storeEvent := range eventsToStore {
|
|
|
|
storeEvent()
|
|
|
|
}
|
2023-01-02 15:11:32 +01:00
|
|
|
|
2023-10-04 15:08:50 +02:00
|
|
|
return newKey, nil
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListSetupKeys returns a list of all setup keys of the account
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) ListSetupKeys(ctx context.Context, accountID, userID string) ([]*SetupKey, error) {
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
2024-03-27 16:11:45 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
return am.Store.GetAccountSetupKeys(ctx, LockingStrengthShare, accountID)
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSetupKey looks up a SetupKey by KeyID, returns NotFound error if not found.
|
2024-07-03 11:33:02 +02:00
|
|
|
func (am *DefaultAccountManager) GetSetupKey(ctx context.Context, accountID, userID, keyID string) (*SetupKey, error) {
|
2024-09-27 16:10:50 +02:00
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
2022-09-11 23:16:40 +02:00
|
|
|
if err != nil {
|
2022-11-11 20:36:45 +01:00
|
|
|
return nil, err
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return nil, status.NewUserNotPartOfAccountError()
|
|
|
|
}
|
|
|
|
|
|
|
|
if user.IsRegularUser() {
|
|
|
|
return nil, status.NewAdminPermissionError()
|
2024-03-27 16:11:45 +01:00
|
|
|
}
|
|
|
|
|
2024-11-22 10:15:51 +01:00
|
|
|
setupKey, err := am.Store.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyID)
|
2024-09-27 16:10:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// the UpdatedAt field was introduced later, so there might be that some keys have a Zero value (e.g, null in the store file)
|
2024-09-27 16:10:50 +02:00
|
|
|
if setupKey.UpdatedAt.IsZero() {
|
|
|
|
setupKey.UpdatedAt = setupKey.CreatedAt
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
|
|
|
|
2024-10-28 17:52:23 +01:00
|
|
|
return setupKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSetupKey removes the setup key from the account
|
|
|
|
func (am *DefaultAccountManager) DeleteSetupKey(ctx context.Context, accountID, userID, keyID string) error {
|
|
|
|
user, err := am.Store.GetUserByUserID(ctx, LockingStrengthShare, userID)
|
|
|
|
if err != nil {
|
2024-11-11 17:46:10 +01:00
|
|
|
return err
|
2022-11-05 10:24:50 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.AccountID != accountID {
|
|
|
|
return status.NewUserNotPartOfAccountError()
|
2024-10-28 17:52:23 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
if user.IsRegularUser() {
|
|
|
|
return status.NewAdminPermissionError()
|
2024-10-28 17:52:23 +01:00
|
|
|
}
|
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
var deletedSetupKey *SetupKey
|
|
|
|
|
|
|
|
err = am.Store.ExecuteInTransaction(ctx, func(transaction Store) error {
|
|
|
|
deletedSetupKey, err = transaction.GetSetupKeyByID(ctx, LockingStrengthShare, accountID, keyID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return transaction.DeleteSetupKey(ctx, LockingStrengthUpdate, accountID, keyID)
|
|
|
|
})
|
2024-10-28 17:52:23 +01:00
|
|
|
if err != nil {
|
2024-11-11 17:46:10 +01:00
|
|
|
return err
|
2024-10-28 17:52:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
am.StoreEvent(ctx, userID, keyID, accountID, activity.SetupKeyDeleted, deletedSetupKey.EventMeta())
|
|
|
|
|
|
|
|
return nil
|
2022-09-11 23:16:40 +02:00
|
|
|
}
|
2024-08-12 12:48:05 +02:00
|
|
|
|
2024-11-11 17:46:10 +01:00
|
|
|
func validateSetupKeyAutoGroups(ctx context.Context, transaction Store, accountID string, autoGroupIDs []string) error {
|
|
|
|
groups, err := transaction.GetGroupsByIDs(ctx, LockingStrengthShare, accountID, autoGroupIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, groupID := range autoGroupIDs {
|
|
|
|
group, ok := groups[groupID]
|
2024-08-12 12:48:05 +02:00
|
|
|
if !ok {
|
2024-11-11 17:46:10 +01:00
|
|
|
return status.Errorf(status.NotFound, "group not found: %s", groupID)
|
2024-08-12 12:48:05 +02:00
|
|
|
}
|
2024-11-11 17:46:10 +01:00
|
|
|
|
|
|
|
if group.IsGroupAll() {
|
|
|
|
return status.Errorf(status.InvalidArgument, "can't add 'All' group to the setup key")
|
2024-08-12 12:48:05 +02:00
|
|
|
}
|
|
|
|
}
|
2024-11-11 17:46:10 +01:00
|
|
|
|
2024-08-12 12:48:05 +02:00
|
|
|
return nil
|
|
|
|
}
|
2024-11-11 17:46:10 +01:00
|
|
|
|
|
|
|
// prepareSetupKeyEvents prepares a list of event functions to be stored.
|
|
|
|
func (am *DefaultAccountManager) prepareSetupKeyEvents(ctx context.Context, transaction Store, accountID, userID string, addedGroups, removedGroups []string, key *SetupKey) []func() {
|
|
|
|
var eventsToStore []func()
|
|
|
|
|
|
|
|
modifiedGroups := slices.Concat(addedGroups, removedGroups)
|
|
|
|
groups, err := transaction.GetGroupsByIDs(ctx, LockingStrengthShare, accountID, modifiedGroups)
|
|
|
|
if err != nil {
|
2024-11-15 18:09:32 +01:00
|
|
|
log.WithContext(ctx).Debugf("failed to get groups for setup key events: %v", err)
|
2024-11-11 17:46:10 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range removedGroups {
|
|
|
|
group, ok := groups[g]
|
|
|
|
if !ok {
|
2024-11-15 18:09:32 +01:00
|
|
|
log.WithContext(ctx).Debugf("skipped adding group: %s GroupRemovedFromSetupKey activity: group not found", g)
|
2024-11-11 17:46:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsToStore = append(eventsToStore, func() {
|
|
|
|
meta := map[string]any{"group": group.Name, "group_id": group.ID, "setupkey": key.Name}
|
|
|
|
am.StoreEvent(ctx, userID, key.Id, accountID, activity.GroupRemovedFromSetupKey, meta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, g := range addedGroups {
|
|
|
|
group, ok := groups[g]
|
|
|
|
if !ok {
|
2024-11-15 18:09:32 +01:00
|
|
|
log.WithContext(ctx).Debugf("skipped adding group: %s GroupAddedToSetupKey activity: group not found", g)
|
2024-11-11 17:46:10 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
eventsToStore = append(eventsToStore, func() {
|
|
|
|
meta := map[string]any{"group": group.Name, "group_id": group.ID, "setupkey": key.Name}
|
|
|
|
am.StoreEvent(ctx, userID, key.Id, accountID, activity.GroupAddedToSetupKey, meta)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return eventsToStore
|
|
|
|
}
|