2021-07-30 17:46:38 +02:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2021-08-01 19:06:01 +02:00
|
|
|
"github.com/google/uuid"
|
|
|
|
log "github.com/sirupsen/logrus"
|
2021-07-30 17:46:38 +02:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
"net"
|
|
|
|
"sync"
|
2021-08-20 22:33:43 +02:00
|
|
|
"time"
|
2021-07-30 17:46:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type AccountManager struct {
|
|
|
|
Store Store
|
|
|
|
// mutex to synchronise account operations (e.g. generating Peer IP address inside the Network)
|
2021-09-07 18:36:46 +02:00
|
|
|
mux sync.Mutex
|
|
|
|
peersUpdateManager *PeersUpdateManager
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account represents a unique account of the system
|
|
|
|
type Account struct {
|
|
|
|
Id string
|
|
|
|
SetupKeys map[string]*SetupKey
|
|
|
|
Network *Network
|
2021-08-01 19:06:01 +02:00
|
|
|
Peers map[string]*Peer
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager creates a new AccountManager with a provided Store
|
2021-09-07 18:36:46 +02:00
|
|
|
func NewManager(store Store, peersUpdateManager *PeersUpdateManager) *AccountManager {
|
2021-07-30 17:46:38 +02:00
|
|
|
return &AccountManager{
|
2021-09-07 18:36:46 +02:00
|
|
|
Store: store,
|
|
|
|
mux: sync.Mutex{},
|
|
|
|
peersUpdateManager: peersUpdateManager,
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 22:33:43 +02:00
|
|
|
//AddSetupKey generates a new setup key with a given name and type, and adds it to the specified account
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) AddSetupKey(accountId string, keyName string, keyType SetupKeyType, expiresIn time.Duration) (*SetupKey, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := GenerateSetupKey(keyName, keyType, expiresIn)
|
|
|
|
account.SetupKeys[setupKey.Key] = setupKey
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return setupKey, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//RevokeSetupKey marks SetupKey as revoked - becomes not valid anymore
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) RevokeSetupKey(accountId string, keyId string) (*SetupKey, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := getAccountSetupKeyById(account, keyId)
|
|
|
|
if setupKey == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyCopy := setupKey.Copy()
|
|
|
|
keyCopy.Revoked = true
|
|
|
|
account.SetupKeys[keyCopy.Key] = keyCopy
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyCopy, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//RenameSetupKey renames existing setup key of the specified account.
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) RenameSetupKey(accountId string, keyId string, newName string) (*SetupKey, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-20 22:33:43 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
setupKey := getAccountSetupKeyById(account, keyId)
|
|
|
|
if setupKey == nil {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "unknown setupKey %s", keyId)
|
|
|
|
}
|
|
|
|
|
|
|
|
keyCopy := setupKey.Copy()
|
|
|
|
keyCopy.Name = newName
|
|
|
|
account.SetupKeys[keyCopy.Key] = keyCopy
|
2021-09-07 18:36:46 +02:00
|
|
|
err = am.Store.SaveAccount(account)
|
2021-08-20 22:33:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed adding account key")
|
|
|
|
}
|
|
|
|
|
|
|
|
return keyCopy, nil
|
|
|
|
}
|
|
|
|
|
2021-08-12 12:49:10 +02:00
|
|
|
//GetAccount returns an existing account or error (NotFound) if doesn't exist
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) GetAccount(accountId string) (*Account, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
2021-08-20 15:44:18 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
2021-08-12 12:49:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetOrCreateAccount returns an existing account or creates a new one if doesn't exist
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) GetOrCreateAccount(accountId string) (*Account, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
_, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
2021-09-07 18:36:46 +02:00
|
|
|
return am.createAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
} else {
|
|
|
|
// other error
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
account, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed retrieving account")
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//AccountExists checks whether account exists (returns true) or not (returns false)
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) AccountExists(accountId string) (*bool, error) {
|
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
var res bool
|
2021-09-07 18:36:46 +02:00
|
|
|
_, err := am.Store.GetAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
if s, ok := status.FromError(err); ok && s.Code() == codes.NotFound {
|
|
|
|
res = false
|
|
|
|
return &res, nil
|
|
|
|
} else {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res = true
|
|
|
|
return &res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAccount generates a new Account with a provided accountId and saves to the Store
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) AddAccount(accountId string) (*Account, error) {
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
am.mux.Lock()
|
|
|
|
defer am.mux.Unlock()
|
2021-08-12 12:49:10 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
return am.createAccount(accountId)
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
func (am *AccountManager) createAccount(accountId string) (*Account, error) {
|
2021-08-12 12:49:10 +02:00
|
|
|
account, _ := newAccountWithId(accountId)
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
err := am.Store.SaveAccount(account)
|
2021-08-12 12:49:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, "failed creating account")
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newAccountWithId creates a new Account with a default SetupKey (doesn't store in a Store) and provided id
|
|
|
|
func newAccountWithId(accountId string) (*Account, *SetupKey) {
|
2021-08-01 19:06:01 +02:00
|
|
|
|
|
|
|
log.Debugf("creating new account")
|
|
|
|
|
|
|
|
setupKeys := make(map[string]*SetupKey)
|
2021-09-07 18:36:46 +02:00
|
|
|
defaultKey := GenerateDefaultSetupKey()
|
|
|
|
oneOffKey := GenerateSetupKey("One-off key", SetupKeyOneOff, DefaultSetupKeyDuration)
|
|
|
|
setupKeys[defaultKey.Key] = defaultKey
|
|
|
|
setupKeys[oneOffKey.Key] = oneOffKey
|
2021-08-12 12:49:10 +02:00
|
|
|
network := &Network{
|
|
|
|
Id: uuid.New().String(),
|
|
|
|
Net: net.IPNet{IP: net.ParseIP("100.64.0.0"), Mask: net.IPMask{255, 192, 0, 0}},
|
|
|
|
Dns: ""}
|
2021-08-01 19:06:01 +02:00
|
|
|
peers := make(map[string]*Peer)
|
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
log.Debugf("created new account %s with setup key %s", accountId, defaultKey.Key)
|
2021-08-01 19:06:01 +02:00
|
|
|
|
2021-09-07 18:36:46 +02:00
|
|
|
return &Account{Id: accountId, SetupKeys: setupKeys, Network: network, Peers: peers}, defaultKey
|
2021-08-01 19:06:01 +02:00
|
|
|
}
|
2021-08-12 12:49:10 +02:00
|
|
|
|
|
|
|
// newAccount creates a new Account with a default SetupKey (doesn't store in a Store)
|
|
|
|
func newAccount() (*Account, *SetupKey) {
|
|
|
|
accountId := uuid.New().String()
|
|
|
|
return newAccountWithId(accountId)
|
|
|
|
}
|
2021-08-20 22:33:43 +02:00
|
|
|
|
|
|
|
func getAccountSetupKeyById(acc *Account, keyId string) *SetupKey {
|
|
|
|
for _, k := range acc.SetupKeys {
|
|
|
|
if keyId == k.Id {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAccountSetupKeyByKey(acc *Account, key string) *SetupKey {
|
|
|
|
for _, k := range acc.SetupKeys {
|
|
|
|
if key == k.Key {
|
|
|
|
return k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|