2021-07-30 17:46:38 +02:00
|
|
|
package server
|
|
|
|
|
2023-10-12 15:42:36 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2023-10-16 11:19:39 +02:00
|
|
|
"strings"
|
2023-10-12 15:42:36 +02:00
|
|
|
"time"
|
|
|
|
|
2023-10-19 21:14:05 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
|
2023-11-28 13:45:26 +01:00
|
|
|
nbpeer "github.com/netbirdio/netbird/management/server/peer"
|
2023-10-12 15:42:36 +02:00
|
|
|
"github.com/netbirdio/netbird/management/server/telemetry"
|
|
|
|
)
|
2023-08-18 19:23:11 +02:00
|
|
|
|
2021-07-30 17:46:38 +02:00
|
|
|
type Store interface {
|
2022-05-21 15:21:39 +02:00
|
|
|
GetAllAccounts() []*Account
|
2022-11-07 12:10:56 +01:00
|
|
|
GetAccount(accountID string) (*Account, error)
|
2023-11-28 14:23:38 +01:00
|
|
|
DeleteAccount(account *Account) error
|
2022-11-07 12:10:56 +01:00
|
|
|
GetAccountByUser(userID string) (*Account, error)
|
|
|
|
GetAccountByPeerPubKey(peerKey string) (*Account, error)
|
2023-02-03 10:33:28 +01:00
|
|
|
GetAccountByPeerID(peerID string) (*Account, error)
|
2023-03-16 15:57:44 +01:00
|
|
|
GetAccountBySetupKey(setupKey string) (*Account, error) // todo use key hash later
|
2022-03-01 15:22:18 +01:00
|
|
|
GetAccountByPrivateDomain(domain string) (*Account, error)
|
2023-03-16 15:57:44 +01:00
|
|
|
GetTokenIDByHashedToken(secret string) (string, error)
|
|
|
|
GetUserByTokenID(tokenID string) (*User, error)
|
2021-07-30 17:46:38 +02:00
|
|
|
SaveAccount(account *Account) error
|
2023-03-20 16:14:55 +01:00
|
|
|
DeleteHashedPAT2TokenIDIndex(hashedToken string) error
|
|
|
|
DeleteTokenID2UserIDIndex(tokenID string) error
|
2022-10-16 13:33:46 +02:00
|
|
|
GetInstallationID() string
|
2022-11-08 10:46:12 +01:00
|
|
|
SaveInstallationID(ID string) error
|
2022-11-07 17:52:23 +01:00
|
|
|
// AcquireAccountLock should attempt to acquire account lock and return a function that releases the lock
|
|
|
|
AcquireAccountLock(accountID string) func()
|
|
|
|
// AcquireGlobalLock should attempt to acquire a global lock and return a function that releases the lock
|
|
|
|
AcquireGlobalLock() func()
|
2023-11-28 13:45:26 +01:00
|
|
|
SavePeerStatus(accountID, peerID string, status nbpeer.PeerStatus) error
|
2024-02-20 09:59:56 +01:00
|
|
|
SavePeerLocation(accountID string, peer *nbpeer.Peer) error
|
2023-08-18 19:23:11 +02:00
|
|
|
SaveUserLastLogin(accountID, userID string, lastLogin time.Time) error
|
2022-11-08 10:46:12 +01:00
|
|
|
// Close should close the store persisting all unsaved data.
|
|
|
|
Close() error
|
2023-10-16 11:19:39 +02:00
|
|
|
// GetStoreEngine should return StoreEngine of the current store implementation.
|
2023-10-12 15:42:36 +02:00
|
|
|
// This is also a method of metrics.DataSource interface.
|
2023-10-16 11:19:39 +02:00
|
|
|
GetStoreEngine() StoreEngine
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
type StoreEngine string
|
2023-10-12 15:42:36 +02:00
|
|
|
|
|
|
|
const (
|
2023-10-16 11:19:39 +02:00
|
|
|
FileStoreEngine StoreEngine = "jsonfile"
|
|
|
|
SqliteStoreEngine StoreEngine = "sqlite"
|
2023-10-12 15:42:36 +02:00
|
|
|
)
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
func getStoreEngineFromEnv() StoreEngine {
|
|
|
|
// NETBIRD_STORE_ENGINE supposed to be used in tests. Otherwise rely on the config file.
|
|
|
|
kind, ok := os.LookupEnv("NETBIRD_STORE_ENGINE")
|
2023-10-12 15:42:36 +02:00
|
|
|
if !ok {
|
2023-10-16 11:19:39 +02:00
|
|
|
return FileStoreEngine
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
value := StoreEngine(strings.ToLower(kind))
|
2023-10-12 15:42:36 +02:00
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
if value == FileStoreEngine || value == SqliteStoreEngine {
|
2023-10-12 15:42:36 +02:00
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
return FileStoreEngine
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
func NewStore(kind StoreEngine, dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
2023-10-12 15:42:36 +02:00
|
|
|
if kind == "" {
|
|
|
|
// fallback to env. Normally this only should be used from tests
|
2023-10-16 11:19:39 +02:00
|
|
|
kind = getStoreEngineFromEnv()
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
|
|
|
switch kind {
|
2023-10-16 11:19:39 +02:00
|
|
|
case FileStoreEngine:
|
2023-10-19 21:14:05 +02:00
|
|
|
log.Info("using JSON file store engine")
|
2023-10-12 15:42:36 +02:00
|
|
|
return NewFileStore(dataDir, metrics)
|
2023-10-16 11:19:39 +02:00
|
|
|
case SqliteStoreEngine:
|
2023-10-19 21:14:05 +02:00
|
|
|
log.Info("using SQLite store engine")
|
2023-10-12 15:42:36 +02:00
|
|
|
return NewSqliteStore(dataDir, metrics)
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported kind of store %s", kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewStoreFromJson(dataDir string, metrics telemetry.AppMetrics) (Store, error) {
|
|
|
|
fstore, err := NewFileStore(dataDir, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-10-16 11:19:39 +02:00
|
|
|
kind := getStoreEngineFromEnv()
|
2023-10-12 15:42:36 +02:00
|
|
|
|
|
|
|
switch kind {
|
2023-10-16 11:19:39 +02:00
|
|
|
case FileStoreEngine:
|
2023-10-12 15:42:36 +02:00
|
|
|
return fstore, nil
|
2023-10-16 11:19:39 +02:00
|
|
|
case SqliteStoreEngine:
|
2023-10-12 15:42:36 +02:00
|
|
|
return NewSqliteStoreFromFileStore(fstore, dataDir, metrics)
|
|
|
|
default:
|
2023-10-16 11:19:39 +02:00
|
|
|
return nil, fmt.Errorf("unsupported store engine %s", kind)
|
2023-10-12 15:42:36 +02:00
|
|
|
}
|
2021-07-30 17:46:38 +02:00
|
|
|
}
|