Compare commits

..

4 Commits

Author SHA1 Message Date
braginini
6022686c37 Fix codacy issues 2022-06-08 00:20:59 +02:00
braginini
d23e942493 Fix codacy issues 2022-06-08 00:14:53 +02:00
braginini
8fdd4ae3a2 Remove create account 2022-06-08 00:02:53 +02:00
braginini
ad3d7888cf Fix new account registration 2022-06-07 23:56:40 +02:00
3 changed files with 67 additions and 62 deletions

1
go.mod
View File

@@ -89,6 +89,7 @@ require (
github.com/srwiley/rasterx v0.0.0-20200120212402-85cb7272f5e9 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
github.com/yuin/goldmark v1.4.1 // indirect
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf // indirect
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 // indirect
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect

4
go.sum
View File

@@ -130,6 +130,8 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/eko/gocache/v2 v2.3.1 h1:8MMkfqGJ0KIA9OXT0rXevcEIrU16oghrGDiIDJDFCa0=
github.com/eko/gocache/v2 v2.3.1/go.mod h1:l2z8OmpZHL0CpuzDJtxm267eF3mZW1NqUsMj+sKrbUs=
github.com/eko/gocache/v3 v3.0.0 h1:Mfa3Nj6GdrXiBXz+JsvESffxO8BGUYVQ2heiAhEhH1U=
github.com/eko/gocache/v3 v3.0.0/go.mod h1:2//8SJUJBp0/MKvuPd6mhZuWpL3qTic4N0ssUEaflCk=
github.com/elazarl/goproxy v0.0.0-20170405201442-c4fc26588b6e/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/elazarl/goproxy v0.0.0-20180725130230-947c36da3153/go.mod h1:/Zj4wYkgs4iZTTu3o/KG3Itv/qCCa8VVMlb3i9OVuzc=
github.com/emicklei/go-restful v0.0.0-20170410110728-ff4f55a20633/go.mod h1:otzb+WCGbkyDHkqmQmT5YD2WR4BBwUdeQoFo8l/7tVs=
@@ -658,6 +660,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf h1:oXVg4h2qJDd9htKxb5SCpFBHLipW6hXmL3qpUixS2jw=
golang.org/x/exp v0.0.0-20220518171630-0b5c67f07fdf/go.mod h1:yh0Ynu2b5ZUe3MQfp2nM0ecK7wsgouWTDN0FNeJuIys=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.0.0-20200430140353-33d19683fad8 h1:6WW6V3x1P/jokJBpRQYUJnMHRP6isStQwCozxnU7XQw=

View File

@@ -29,42 +29,42 @@ const (
)
type AccountManager interface {
GetOrCreateAccountByUser(userId, domain string) (*Account, error)
GetAccountByUser(userId string) (*Account, error)
GetOrCreateAccountByUser(userID, domain string) (*Account, error)
GetAccountByUser(userID string) (*Account, error)
AddSetupKey(
accountId string,
accountID string,
keyName string,
keyType SetupKeyType,
expiresIn *util.Duration,
) (*SetupKey, error)
RevokeSetupKey(accountId string, keyId string) (*SetupKey, error)
RenameSetupKey(accountId string, keyId string, newName string) (*SetupKey, error)
GetAccountById(accountId string) (*Account, error)
GetAccountByUserOrAccountId(userId, accountId, domain string) (*Account, error)
RevokeSetupKey(accountID string, keyID string) (*SetupKey, error)
RenameSetupKey(accountID string, keyID string, newName string) (*SetupKey, error)
GetAccountById(accountID string) (*Account, error)
GetAccountByUserOrAccountId(userID, accountID, domain string) (*Account, error)
GetAccountWithAuthorizationClaims(claims jwtclaims.AuthorizationClaims) (*Account, error)
IsUserAdmin(claims jwtclaims.AuthorizationClaims) (bool, error)
AccountExists(accountId string) (*bool, error)
AddAccount(accountId, userId, domain string) (*Account, error)
AccountExists(accountID string) (*bool, error)
AddAccount(accountID, userID, domain string) (*Account, error)
GetPeer(peerKey string) (*Peer, error)
MarkPeerConnected(peerKey string, connected bool) error
RenamePeer(accountId string, peerKey string, newName string) (*Peer, error)
DeletePeer(accountId string, peerKey string) (*Peer, error)
GetPeerByIP(accountId string, peerIP string) (*Peer, error)
RenamePeer(accountID string, peerKey string, newName string) (*Peer, error)
DeletePeer(accountID string, peerKey string) (*Peer, error)
GetPeerByIP(accountID string, peerIP string) (*Peer, 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
GetUsersFromAccount(accountId string) ([]*UserInfo, error)
GetGroup(accountId, groupID string) (*Group, error)
SaveGroup(accountId string, group *Group) error
DeleteGroup(accountId, groupID string) error
ListGroups(accountId string) ([]*Group, error)
GroupAddPeer(accountId, groupID, peerKey string) error
GroupDeletePeer(accountId, groupID, peerKey string) error
GroupListPeers(accountId, groupID string) ([]*Peer, error)
GetRule(accountId, ruleID string) (*Rule, error)
GetUsersFromAccount(accountID string) ([]*UserInfo, error)
GetGroup(accountID, groupID string) (*Group, error)
SaveGroup(accountID string, group *Group) error
DeleteGroup(accountID, groupID string) error
ListGroups(accountID string) ([]*Group, error)
GroupAddPeer(accountID, groupID, peerKey string) error
GroupDeletePeer(accountID, groupID, peerKey string) error
GroupListPeers(accountID, groupID string) ([]*Peer, error)
GetRule(accountID, ruleID string) (*Rule, error)
SaveRule(accountID string, rule *Rule) error
DeleteRule(accountId, ruleID string) error
ListRules(accountId string) ([]*Rule, error)
DeleteRule(accountID, ruleID string) error
ListRules(accountID string) ([]*Rule, error)
}
type DefaultAccountManager struct {
@@ -101,9 +101,9 @@ type UserInfo struct {
}
// NewAccount creates a new Account with a generated ID and generated default setup keys
func NewAccount(userId, domain string) *Account {
accountId := xid.New().String()
return newAccountWithId(accountId, userId, domain)
func NewAccount(userID, domain string) *Account {
accountID := xid.New().String()
return newAccountWithId(accountID, userID, domain)
}
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
func (am *DefaultAccountManager) AddSetupKey(
accountId string,
accountID string,
keyName string,
keyType SetupKeyType,
expiresIn *util.Duration,
@@ -233,7 +233,7 @@ func (am *DefaultAccountManager) AddSetupKey(
keyDuration = expiresIn.Duration
}
account, err := am.Store.GetAccount(accountId)
account, err := am.Store.GetAccount(accountID)
if err != nil {
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
func (am *DefaultAccountManager) RevokeSetupKey(accountId string, keyId string) (*SetupKey, error) {
func (am *DefaultAccountManager) RevokeSetupKey(accountID string, keyID string) (*SetupKey, error) {
am.mux.Lock()
defer am.mux.Unlock()
account, err := am.Store.GetAccount(accountId)
account, err := am.Store.GetAccount(accountID)
if err != nil {
return nil, status.Errorf(codes.NotFound, "account not found")
}
setupKey := getAccountSetupKeyById(account, keyId)
setupKey := getAccountSetupKeyById(account, keyID)
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()
@@ -277,21 +277,21 @@ func (am *DefaultAccountManager) RevokeSetupKey(accountId string, keyId string)
// RenameSetupKey renames existing setup key of the specified account.
func (am *DefaultAccountManager) RenameSetupKey(
accountId string,
keyId string,
accountID string,
keyID string,
newName string,
) (*SetupKey, error) {
am.mux.Lock()
defer am.mux.Unlock()
account, err := am.Store.GetAccount(accountId)
account, err := am.Store.GetAccount(accountID)
if err != nil {
return nil, status.Errorf(codes.NotFound, "account not found")
}
setupKey := getAccountSetupKeyById(account, keyId)
setupKey := getAccountSetupKeyById(account, keyID)
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()
@@ -306,11 +306,11 @@ func (am *DefaultAccountManager) RenameSetupKey(
}
// 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()
defer am.mux.Unlock()
account, err := am.Store.GetAccount(accountId)
account, err := am.Store.GetAccount(accountID)
if err != nil {
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
// user id doesn't have an account associated with it, one account is created
func (am *DefaultAccountManager) GetAccountByUserOrAccountId(
userId, accountId, domain string,
userID, accountID, domain string,
) (*Account, error) {
if accountId != "" {
return am.GetAccountById(accountId)
} else if userId != "" {
account, err := am.GetOrCreateAccountByUser(userId, domain)
if accountID != "" {
return am.GetAccountById(accountID)
} else if userID != "" {
account, err := am.GetOrCreateAccountByUser(userID, domain)
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 {
return nil, err
}
@@ -345,9 +345,9 @@ func isNil(i idp.Manager) bool {
}
// 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) {
err := am.idpManager.UpdateUserAppMetadata(userId, idp.AppMetadata{WTAccountId: accountID})
err := am.idpManager.UpdateUserAppMetadata(userID, idp.AppMetadata{WTAccountId: accountID})
if err != nil {
return status.Errorf(
codes.Internal,
@@ -603,12 +603,12 @@ func (am *DefaultAccountManager) GetAccountWithAuthorizationClaims(
}
// 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()
defer am.mux.Unlock()
var res bool
_, err := am.Store.GetAccount(accountId)
_, err := am.Store.GetAccount(accountID)
if err != nil {
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
res = false
@@ -622,16 +622,16 @@ func (am *DefaultAccountManager) AccountExists(accountId string) (*bool, error)
return &res, nil
}
// 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) {
// 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) {
am.mux.Lock()
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) {
account := newAccountWithId(accountId, userId, domain)
func (am *DefaultAccountManager) createAccountWithID(accountID, userID, domain string) (*Account, error) {
account := newAccountWithId(accountID, userID, domain)
am.addAllGroup(account)
@@ -666,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
func newAccountWithId(accountId, userId, domain string) *Account {
func newAccountWithId(accountID, userID, domain string) *Account {
log.Debugf("creating new account")
setupKeys := make(map[string]*SetupKey)
@@ -678,22 +678,22 @@ func newAccountWithId(accountId, userId, domain string) *Account {
peers := make(map[string]*Peer)
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{
Id: accountId,
Id: accountID,
SetupKeys: setupKeys,
Network: network,
Peers: peers,
Users: users,
CreatedBy: userId,
CreatedBy: userID,
Domain: domain,
}
}
func getAccountSetupKeyById(acc *Account, keyId string) *SetupKey {
func getAccountSetupKeyById(acc *Account, keyID string) *SetupKey {
for _, k := range acc.SetupKeys {
if keyId == k.Id {
if keyID == k.Id {
return k
}
}