mirror of
https://github.com/netbirdio/netbird.git
synced 2026-04-19 16:56:39 +00:00
Compare commits
4 Commits
dependabot
...
fix-peer-r
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6022686c37 | ||
|
|
d23e942493 | ||
|
|
8fdd4ae3a2 | ||
|
|
ad3d7888cf |
@@ -29,42 +29,42 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type AccountManager interface {
|
type AccountManager interface {
|
||||||
GetOrCreateAccountByUser(userId, domain string) (*Account, error)
|
GetOrCreateAccountByUser(userID, domain string) (*Account, error)
|
||||||
GetAccountByUser(userId string) (*Account, error)
|
GetAccountByUser(userID string) (*Account, error)
|
||||||
AddSetupKey(
|
AddSetupKey(
|
||||||
accountId string,
|
accountID string,
|
||||||
keyName string,
|
keyName string,
|
||||||
keyType SetupKeyType,
|
keyType SetupKeyType,
|
||||||
expiresIn *util.Duration,
|
expiresIn *util.Duration,
|
||||||
) (*SetupKey, error)
|
) (*SetupKey, error)
|
||||||
RevokeSetupKey(accountId string, keyId string) (*SetupKey, error)
|
RevokeSetupKey(accountID string, keyID string) (*SetupKey, error)
|
||||||
RenameSetupKey(accountId string, keyId string, newName string) (*SetupKey, error)
|
RenameSetupKey(accountID string, keyID string, newName string) (*SetupKey, error)
|
||||||
GetAccountById(accountId string) (*Account, error)
|
GetAccountById(accountID string) (*Account, error)
|
||||||
GetAccountByUserOrAccountId(userId, accountId, domain string) (*Account, error)
|
GetAccountByUserOrAccountId(userID, accountID, domain string) (*Account, error)
|
||||||
GetAccountWithAuthorizationClaims(claims jwtclaims.AuthorizationClaims) (*Account, error)
|
GetAccountWithAuthorizationClaims(claims jwtclaims.AuthorizationClaims) (*Account, error)
|
||||||
IsUserAdmin(claims jwtclaims.AuthorizationClaims) (bool, error)
|
IsUserAdmin(claims jwtclaims.AuthorizationClaims) (bool, error)
|
||||||
AccountExists(accountId string) (*bool, error)
|
AccountExists(accountID string) (*bool, error)
|
||||||
AddAccount(accountId, userId, domain string) (*Account, error)
|
AddAccount(accountID, userID, domain string) (*Account, error)
|
||||||
GetPeer(peerKey string) (*Peer, error)
|
GetPeer(peerKey string) (*Peer, error)
|
||||||
MarkPeerConnected(peerKey string, connected bool) error
|
MarkPeerConnected(peerKey string, connected bool) error
|
||||||
RenamePeer(accountId string, peerKey string, newName string) (*Peer, error)
|
RenamePeer(accountID string, peerKey string, newName string) (*Peer, error)
|
||||||
DeletePeer(accountId string, peerKey string) (*Peer, error)
|
DeletePeer(accountID string, peerKey string) (*Peer, error)
|
||||||
GetPeerByIP(accountId string, peerIP string) (*Peer, error)
|
GetPeerByIP(accountID string, peerIP string) (*Peer, error)
|
||||||
GetNetworkMap(peerKey string) (*NetworkMap, error)
|
GetNetworkMap(peerKey string) (*NetworkMap, error)
|
||||||
AddPeer(setupKey string, userId string, peer *Peer) (*Peer, error)
|
AddPeer(setupKey string, userID string, peer *Peer) (*Peer, error)
|
||||||
UpdatePeerMeta(peerKey string, meta PeerSystemMeta) error
|
UpdatePeerMeta(peerKey string, meta PeerSystemMeta) error
|
||||||
GetUsersFromAccount(accountId string) ([]*UserInfo, error)
|
GetUsersFromAccount(accountID string) ([]*UserInfo, error)
|
||||||
GetGroup(accountId, groupID string) (*Group, error)
|
GetGroup(accountID, groupID string) (*Group, error)
|
||||||
SaveGroup(accountId string, group *Group) error
|
SaveGroup(accountID string, group *Group) error
|
||||||
DeleteGroup(accountId, groupID string) error
|
DeleteGroup(accountID, groupID string) error
|
||||||
ListGroups(accountId string) ([]*Group, error)
|
ListGroups(accountID string) ([]*Group, error)
|
||||||
GroupAddPeer(accountId, groupID, peerKey string) error
|
GroupAddPeer(accountID, groupID, peerKey string) error
|
||||||
GroupDeletePeer(accountId, groupID, peerKey string) error
|
GroupDeletePeer(accountID, groupID, peerKey string) error
|
||||||
GroupListPeers(accountId, groupID string) ([]*Peer, error)
|
GroupListPeers(accountID, groupID string) ([]*Peer, error)
|
||||||
GetRule(accountId, ruleID string) (*Rule, error)
|
GetRule(accountID, ruleID string) (*Rule, error)
|
||||||
SaveRule(accountID string, rule *Rule) error
|
SaveRule(accountID string, rule *Rule) error
|
||||||
DeleteRule(accountId, ruleID string) error
|
DeleteRule(accountID, ruleID string) error
|
||||||
ListRules(accountId string) ([]*Rule, error)
|
ListRules(accountID string) ([]*Rule, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type DefaultAccountManager struct {
|
type DefaultAccountManager struct {
|
||||||
@@ -101,9 +101,9 @@ type UserInfo struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewAccount creates a new Account with a generated ID and generated default setup keys
|
// NewAccount creates a new Account with a generated ID and generated default setup keys
|
||||||
func NewAccount(userId, domain string) *Account {
|
func NewAccount(userID, domain string) *Account {
|
||||||
accountId := xid.New().String()
|
accountID := xid.New().String()
|
||||||
return newAccountWithId(accountId, userId, domain)
|
return newAccountWithId(accountID, userID, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *Account) Copy() *Account {
|
func (a *Account) Copy() *Account {
|
||||||
@@ -220,7 +220,7 @@ func (am *DefaultAccountManager) warmupIDPCache() error {
|
|||||||
|
|
||||||
// AddSetupKey generates a new setup key with a given name and type, and adds it to the specified account
|
// AddSetupKey generates a new setup key with a given name and type, and adds it to the specified account
|
||||||
func (am *DefaultAccountManager) AddSetupKey(
|
func (am *DefaultAccountManager) AddSetupKey(
|
||||||
accountId string,
|
accountID string,
|
||||||
keyName string,
|
keyName string,
|
||||||
keyType SetupKeyType,
|
keyType SetupKeyType,
|
||||||
expiresIn *util.Duration,
|
expiresIn *util.Duration,
|
||||||
@@ -233,7 +233,7 @@ func (am *DefaultAccountManager) AddSetupKey(
|
|||||||
keyDuration = expiresIn.Duration
|
keyDuration = expiresIn.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
account, err := am.Store.GetAccount(accountId)
|
account, err := am.Store.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "account not found")
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
||||||
}
|
}
|
||||||
@@ -250,18 +250,18 @@ func (am *DefaultAccountManager) AddSetupKey(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// RevokeSetupKey marks SetupKey as revoked - becomes not valid anymore
|
// RevokeSetupKey marks SetupKey as revoked - becomes not valid anymore
|
||||||
func (am *DefaultAccountManager) RevokeSetupKey(accountId string, keyId string) (*SetupKey, error) {
|
func (am *DefaultAccountManager) RevokeSetupKey(accountID string, keyID string) (*SetupKey, error) {
|
||||||
am.mux.Lock()
|
am.mux.Lock()
|
||||||
defer am.mux.Unlock()
|
defer am.mux.Unlock()
|
||||||
|
|
||||||
account, err := am.Store.GetAccount(accountId)
|
account, err := am.Store.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "account not found")
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
setupKey := getAccountSetupKeyById(account, keyId)
|
setupKey := getAccountSetupKeyById(account, keyID)
|
||||||
if setupKey == nil {
|
if setupKey == nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyID)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyCopy := setupKey.Copy()
|
keyCopy := setupKey.Copy()
|
||||||
@@ -277,21 +277,21 @@ func (am *DefaultAccountManager) RevokeSetupKey(accountId string, keyId string)
|
|||||||
|
|
||||||
// RenameSetupKey renames existing setup key of the specified account.
|
// RenameSetupKey renames existing setup key of the specified account.
|
||||||
func (am *DefaultAccountManager) RenameSetupKey(
|
func (am *DefaultAccountManager) RenameSetupKey(
|
||||||
accountId string,
|
accountID string,
|
||||||
keyId string,
|
keyID string,
|
||||||
newName string,
|
newName string,
|
||||||
) (*SetupKey, error) {
|
) (*SetupKey, error) {
|
||||||
am.mux.Lock()
|
am.mux.Lock()
|
||||||
defer am.mux.Unlock()
|
defer am.mux.Unlock()
|
||||||
|
|
||||||
account, err := am.Store.GetAccount(accountId)
|
account, err := am.Store.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "account not found")
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
setupKey := getAccountSetupKeyById(account, keyId)
|
setupKey := getAccountSetupKeyById(account, keyID)
|
||||||
if setupKey == nil {
|
if setupKey == nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyID)
|
||||||
}
|
}
|
||||||
|
|
||||||
keyCopy := setupKey.Copy()
|
keyCopy := setupKey.Copy()
|
||||||
@@ -306,11 +306,11 @@ func (am *DefaultAccountManager) RenameSetupKey(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetAccountById returns an existing account using its ID or error (NotFound) if doesn't exist
|
// GetAccountById returns an existing account using its ID or error (NotFound) if doesn't exist
|
||||||
func (am *DefaultAccountManager) GetAccountById(accountId string) (*Account, error) {
|
func (am *DefaultAccountManager) GetAccountById(accountID string) (*Account, error) {
|
||||||
am.mux.Lock()
|
am.mux.Lock()
|
||||||
defer am.mux.Unlock()
|
defer am.mux.Unlock()
|
||||||
|
|
||||||
account, err := am.Store.GetAccount(accountId)
|
account, err := am.Store.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "account not found")
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
||||||
}
|
}
|
||||||
@@ -321,16 +321,16 @@ func (am *DefaultAccountManager) GetAccountById(accountId string) (*Account, err
|
|||||||
// GetAccountByUserOrAccountId look for an account by user or account Id, if no account is provided and
|
// GetAccountByUserOrAccountId look for an account by user or account Id, if no account is provided and
|
||||||
// user id doesn't have an account associated with it, one account is created
|
// user id doesn't have an account associated with it, one account is created
|
||||||
func (am *DefaultAccountManager) GetAccountByUserOrAccountId(
|
func (am *DefaultAccountManager) GetAccountByUserOrAccountId(
|
||||||
userId, accountId, domain string,
|
userID, accountID, domain string,
|
||||||
) (*Account, error) {
|
) (*Account, error) {
|
||||||
if accountId != "" {
|
if accountID != "" {
|
||||||
return am.GetAccountById(accountId)
|
return am.GetAccountById(accountID)
|
||||||
} else if userId != "" {
|
} else if userID != "" {
|
||||||
account, err := am.GetOrCreateAccountByUser(userId, domain)
|
account, err := am.GetOrCreateAccountByUser(userID, domain)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.NotFound, "account not found using user id: %s", userId)
|
return nil, status.Errorf(codes.NotFound, "account not found using user id: %s", userID)
|
||||||
}
|
}
|
||||||
err = am.updateIDPMetadata(userId, account.Id)
|
err = am.updateIDPMetadata(userID, account.Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -345,9 +345,9 @@ func isNil(i idp.Manager) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// updateIDPMetadata update user's app metadata in idp manager
|
// updateIDPMetadata update user's app metadata in idp manager
|
||||||
func (am *DefaultAccountManager) updateIDPMetadata(userId, accountID string) error {
|
func (am *DefaultAccountManager) updateIDPMetadata(userID, accountID string) error {
|
||||||
if !isNil(am.idpManager) {
|
if !isNil(am.idpManager) {
|
||||||
err := am.idpManager.UpdateUserAppMetadata(userId, idp.AppMetadata{WTAccountId: accountID})
|
err := am.idpManager.UpdateUserAppMetadata(userID, idp.AppMetadata{WTAccountId: accountID})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return status.Errorf(
|
return status.Errorf(
|
||||||
codes.Internal,
|
codes.Internal,
|
||||||
@@ -524,6 +524,7 @@ func (am *DefaultAccountManager) handleNewUserAccount(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
account = NewAccount(claims.UserId, lowerDomain)
|
account = NewAccount(claims.UserId, lowerDomain)
|
||||||
|
am.addAllGroup(account)
|
||||||
account.Users[claims.UserId] = NewAdminUser(claims.UserId)
|
account.Users[claims.UserId] = NewAdminUser(claims.UserId)
|
||||||
err = am.updateAccountDomainAttributes(account, claims, true)
|
err = am.updateAccountDomainAttributes(account, claims, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -602,12 +603,12 @@ func (am *DefaultAccountManager) GetAccountWithAuthorizationClaims(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AccountExists checks whether account exists (returns true) or not (returns false)
|
// AccountExists checks whether account exists (returns true) or not (returns false)
|
||||||
func (am *DefaultAccountManager) AccountExists(accountId string) (*bool, error) {
|
func (am *DefaultAccountManager) AccountExists(accountID string) (*bool, error) {
|
||||||
am.mux.Lock()
|
am.mux.Lock()
|
||||||
defer am.mux.Unlock()
|
defer am.mux.Unlock()
|
||||||
|
|
||||||
var res bool
|
var res bool
|
||||||
_, err := am.Store.GetAccount(accountId)
|
_, err := am.Store.GetAccount(accountID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
||||||
res = false
|
res = false
|
||||||
@@ -621,16 +622,16 @@ func (am *DefaultAccountManager) AccountExists(accountId string) (*bool, error)
|
|||||||
return &res, nil
|
return &res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddAccount generates a new Account with a provided accountId and userId, saves to the Store
|
// AddAccount generates a new Account with a provided accountID and userID, saves to the Store
|
||||||
func (am *DefaultAccountManager) AddAccount(accountId, userId, domain string) (*Account, error) {
|
func (am *DefaultAccountManager) AddAccount(accountID, userID, domain string) (*Account, error) {
|
||||||
am.mux.Lock()
|
am.mux.Lock()
|
||||||
defer am.mux.Unlock()
|
defer am.mux.Unlock()
|
||||||
|
|
||||||
return am.createAccount(accountId, userId, domain)
|
return am.createAccountWithID(accountID, userID, domain)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (am *DefaultAccountManager) createAccount(accountId, userId, domain string) (*Account, error) {
|
func (am *DefaultAccountManager) createAccountWithID(accountID, userID, domain string) (*Account, error) {
|
||||||
account := newAccountWithId(accountId, userId, domain)
|
account := newAccountWithId(accountID, userID, domain)
|
||||||
|
|
||||||
am.addAllGroup(account)
|
am.addAllGroup(account)
|
||||||
|
|
||||||
@@ -665,7 +666,7 @@ func (am *DefaultAccountManager) addAllGroup(account *Account) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// newAccountWithId creates a new Account with a default SetupKey (doesn't store in a Store) and provided id
|
// newAccountWithId creates a new Account with a default SetupKey (doesn't store in a Store) and provided id
|
||||||
func newAccountWithId(accountId, userId, domain string) *Account {
|
func newAccountWithId(accountID, userID, domain string) *Account {
|
||||||
log.Debugf("creating new account")
|
log.Debugf("creating new account")
|
||||||
|
|
||||||
setupKeys := make(map[string]*SetupKey)
|
setupKeys := make(map[string]*SetupKey)
|
||||||
@@ -677,22 +678,22 @@ func newAccountWithId(accountId, userId, domain string) *Account {
|
|||||||
peers := make(map[string]*Peer)
|
peers := make(map[string]*Peer)
|
||||||
users := make(map[string]*User)
|
users := make(map[string]*User)
|
||||||
|
|
||||||
log.Debugf("created new account %s with setup key %s", accountId, defaultKey.Key)
|
log.Debugf("created new account %s with setup key %s", accountID, defaultKey.Key)
|
||||||
|
|
||||||
return &Account{
|
return &Account{
|
||||||
Id: accountId,
|
Id: accountID,
|
||||||
SetupKeys: setupKeys,
|
SetupKeys: setupKeys,
|
||||||
Network: network,
|
Network: network,
|
||||||
Peers: peers,
|
Peers: peers,
|
||||||
Users: users,
|
Users: users,
|
||||||
CreatedBy: userId,
|
CreatedBy: userID,
|
||||||
Domain: domain,
|
Domain: domain,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAccountSetupKeyById(acc *Account, keyId string) *SetupKey {
|
func getAccountSetupKeyById(acc *Account, keyID string) *SetupKey {
|
||||||
for _, k := range acc.SetupKeys {
|
for _, k := range acc.SetupKeys {
|
||||||
if keyId == k.Id {
|
if keyID == k.Id {
|
||||||
return k
|
return k
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user