2021-07-30 17:46:38 +02:00
|
|
|
package server
|
2021-07-18 20:51:09 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
|
|
|
"github.com/wiretrustee/wiretrustee/util"
|
|
|
|
)
|
|
|
|
|
|
|
|
// storeFileName Store file name. Stored in the datadir
|
|
|
|
const storeFileName = "store.json"
|
|
|
|
|
2021-07-18 21:00:32 +02:00
|
|
|
// FileStore represents an account storage backed by a file persisted to disk
|
2021-07-18 20:51:09 +02:00
|
|
|
type FileStore struct {
|
|
|
|
Accounts map[string]*Account
|
|
|
|
SetupKeyId2AccountId map[string]string `json:"-"`
|
2021-07-22 10:28:00 +02:00
|
|
|
PeerKeyId2AccountId map[string]string `json:"-"`
|
2021-07-18 20:51:09 +02:00
|
|
|
|
|
|
|
// mutex to synchronise Store read/write operations
|
|
|
|
mux sync.Mutex `json:"-"`
|
|
|
|
storeFile string `json:"-"`
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
type StoredAccount struct {
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:51:09 +02:00
|
|
|
// NewStore restores a store from the file located in the datadir
|
|
|
|
func NewStore(dataDir string) (*FileStore, error) {
|
|
|
|
return restore(filepath.Join(dataDir, storeFileName))
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore restores the state of the store from the file.
|
|
|
|
// Creates a new empty store file if doesn't exist
|
|
|
|
func restore(file string) (*FileStore, error) {
|
|
|
|
|
|
|
|
if _, err := os.Stat(file); os.IsNotExist(err) {
|
|
|
|
// create a new FileStore if previously didn't exist (e.g. first run)
|
|
|
|
s := &FileStore{
|
2021-08-12 12:49:10 +02:00
|
|
|
Accounts: make(map[string]*Account),
|
|
|
|
mux: sync.Mutex{},
|
|
|
|
SetupKeyId2AccountId: make(map[string]string),
|
|
|
|
PeerKeyId2AccountId: make(map[string]string),
|
|
|
|
storeFile: file,
|
2021-07-18 20:51:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = s.persist(file)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := util.ReadJson(file, &FileStore{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
store := read.(*FileStore)
|
|
|
|
store.storeFile = file
|
|
|
|
store.SetupKeyId2AccountId = make(map[string]string)
|
2021-07-30 17:46:38 +02:00
|
|
|
store.PeerKeyId2AccountId = make(map[string]string)
|
2021-07-18 20:51:09 +02:00
|
|
|
for accountId, account := range store.Accounts {
|
|
|
|
for setupKeyId := range account.SetupKeys {
|
|
|
|
store.SetupKeyId2AccountId[strings.ToLower(setupKeyId)] = accountId
|
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
for _, peer := range account.Peers {
|
2021-08-15 16:56:26 +02:00
|
|
|
store.PeerKeyId2AccountId[peer.Key] = accountId
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-18 20:51:09 +02:00
|
|
|
|
|
|
|
return store, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// persist persists account data to a file
|
|
|
|
// It is recommended to call it with locking FileStore.mux
|
|
|
|
func (s *FileStore) persist(file string) error {
|
|
|
|
return util.WriteJson(file, s)
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
// GetPeer returns a peer from a Store
|
|
|
|
func (s *FileStore) GetPeer(peerKey string) (*Peer, error) {
|
2021-07-22 10:28:00 +02:00
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
accountId, accountIdFound := s.PeerKeyId2AccountId[peerKey]
|
2021-07-18 20:51:09 +02:00
|
|
|
if !accountIdFound {
|
2021-08-15 16:56:26 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "peer not found")
|
2021-07-18 20:51:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
account, err := s.GetAccount(accountId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-07-18 20:51:09 +02:00
|
|
|
}
|
|
|
|
|
2021-08-01 19:06:01 +02:00
|
|
|
if peer, ok := account.Peers[peerKey]; ok {
|
|
|
|
return peer, nil
|
2021-07-18 20:51:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "peer not found")
|
2021-07-18 20:51:09 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
// SaveAccount updates an existing account or adds a new one
|
|
|
|
func (s *FileStore) SaveAccount(account *Account) error {
|
2021-07-18 20:51:09 +02:00
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
// todo will override, handle existing keys
|
|
|
|
s.Accounts[account.Id] = account
|
|
|
|
|
|
|
|
// todo check that account.Id and keyId are not exist already
|
|
|
|
// because if keyId exists for other accounts this can be bad
|
|
|
|
for keyId := range account.SetupKeys {
|
|
|
|
s.SetupKeyId2AccountId[strings.ToLower(keyId)] = account.Id
|
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
for _, peer := range account.Peers {
|
|
|
|
s.PeerKeyId2AccountId[peer.Key] = account.Id
|
|
|
|
}
|
|
|
|
|
2021-07-18 20:51:09 +02:00
|
|
|
err := s.persist(s.storeFile)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
func (s *FileStore) GetAccountBySetupKey(setupKey string) (*Account, error) {
|
2021-07-22 10:28:00 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
accountId, accountIdFound := s.SetupKeyId2AccountId[strings.ToLower(setupKey)]
|
2021-07-22 10:28:00 +02:00
|
|
|
if !accountIdFound {
|
2021-07-30 17:46:38 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "provided setup key doesn't exists")
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
account, err := s.GetAccount(accountId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileStore) GetAccount(accountId string) (*Account, error) {
|
|
|
|
|
2021-07-22 10:28:00 +02:00
|
|
|
account, accountFound := s.Accounts[accountId]
|
|
|
|
if !accountFound {
|
2021-08-12 12:49:10 +02:00
|
|
|
return nil, status.Errorf(codes.NotFound, "account not found")
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
|
|
|
|
return account, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *FileStore) GetPeerAccount(peerKey string) (*Account, error) {
|
|
|
|
s.mux.Lock()
|
|
|
|
defer s.mux.Unlock()
|
|
|
|
|
|
|
|
accountId, accountIdFound := s.PeerKeyId2AccountId[peerKey]
|
|
|
|
if !accountIdFound {
|
|
|
|
return nil, status.Errorf(codes.NotFound, "Provided peer key doesn't exists %s", peerKey)
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
return s.GetAccount(accountId)
|
2021-07-22 10:28:00 +02:00
|
|
|
}
|