Move accountant into world

This commit is contained in:
Marc Di Luzio 2020-07-24 20:06:06 +01:00
parent 1e4d642038
commit e840b3e47b
6 changed files with 18 additions and 17 deletions

View file

@ -1,28 +0,0 @@
package internal
// Accountant decribes something that stores accounts and account values
type Accountant interface {
// RegisterAccount will register a new account and return it's info
RegisterAccount(name string) (acc Account, err error)
// AssignData stores a custom account key value pair
AssignData(account string, key string, value string) error
// GetValue returns custom account data for a specific key
GetValue(account string, key string) (string, error)
// VerifySecret will verify whether the account secret matches with the
VerifySecret(account string, secret string) (bool, error)
// GetSecret gets the secret associated with an account
GetSecret(account string) (string, error)
}
// Account represents a registered user
type Account struct {
// Name simply describes the account and must be unique
Name string
// Data represents internal account data
Data map[string]string
}

View file

@ -1,63 +0,0 @@
package internal
import (
"testing"
"github.com/google/uuid"
)
func TestNewAccountant(t *testing.T) {
// Very basic verify here for now
accountant := NewSimpleAccountant()
if accountant == nil {
t.Error("Failed to create accountant")
}
}
func TestAccountant_RegisterAccount(t *testing.T) {
accountant := NewSimpleAccountant()
// Start by making two accounts
namea := uuid.New().String()
acca, err := accountant.RegisterAccount(namea)
if err != nil {
t.Error(err)
} else if acca.Name != namea {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", namea, acca.Name)
}
nameb := uuid.New().String()
accb, err := accountant.RegisterAccount(nameb)
if err != nil {
t.Error(err)
} else if accb.Name != nameb {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
}
// Verify another request gets rejected
_, err = accountant.RegisterAccount(namea)
if err == nil {
t.Error("Duplicate account name did not produce error")
}
}
func TestAccountant_AssignGetData(t *testing.T) {
accountant := NewSimpleAccountant()
name := uuid.New().String()
a, err := accountant.RegisterAccount(name)
if err != nil {
t.Error(err)
}
err = accountant.AssignData(a.Name, "key", "value")
if err != nil {
t.Error("Failed to set data for created account")
} else if id, err := accountant.GetValue(a.Name, "key"); err != nil {
t.Error("Failed to get data for account")
} else if id != "value" {
t.Error("Fetched data is incorrect for account")
}
}

View file

@ -34,7 +34,7 @@ func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*r
return nil, fmt.Errorf("empty account name")
}
if acc, err := s.accountant.RegisterAccount(req.Name); err != nil {
if acc, err := s.world.Accountant.RegisterAccount(req.Name); err != nil {
return nil, err
} else if _, err := s.SpawnRoverForAccount(req.Name); err != nil {
@ -57,13 +57,13 @@ func (s *Server) Register(ctx context.Context, req *roveapi.RegisterRequest) (*r
func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (response *roveapi.StatusResponse, err error) {
log.Printf("Handling status request: %s\n", req.Account.Name)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
} else if resp, err := s.accountant.GetValue(req.Account.Name, "rover"); err != nil {
} else if resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover"); err != nil {
return nil, err
} else if rover, err := s.world.GetRover(resp); err != nil {
@ -117,7 +117,7 @@ func (s *Server) Status(ctx context.Context, req *roveapi.StatusRequest) (respon
func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi.RadarResponse, error) {
log.Printf("Handling radar request: %s\n", req.Account.Name)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
@ -126,7 +126,7 @@ func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi
response := &roveapi.RadarResponse{}
resp, err := s.accountant.GetValue(req.Account.Name, "rover")
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
if err != nil {
return nil, err
@ -149,14 +149,14 @@ func (s *Server) Radar(ctx context.Context, req *roveapi.RadarRequest) (*roveapi
func (s *Server) Command(ctx context.Context, req *roveapi.CommandRequest) (*roveapi.CommandResponse, error) {
log.Printf("Handling command request: %s and %+v\n", req.Account.Name, req.Commands)
if valid, err := s.accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
if valid, err := s.world.Accountant.VerifySecret(req.Account.Name, req.Account.Secret); err != nil {
return nil, err
} else if !valid {
return nil, fmt.Errorf("Secret incorrect for account %s", req.Account.Name)
}
resp, err := s.accountant.GetValue(req.Account.Name, "rover")
resp, err := s.world.Accountant.GetValue(req.Account.Name, "rover")
if err != nil {
return nil, err
}

View file

@ -27,9 +27,6 @@ type Server struct {
// Internal state
world *rove.World
// Accountant
accountant Accountant
// gRPC server
netListener net.Listener
grpcServ *grpc.Server
@ -80,7 +77,6 @@ func NewServer(opts ...ServerOption) *Server {
persistence: EphemeralData,
schedule: cron.New(),
world: rove.NewWorld(32),
accountant: NewSimpleAccountant(),
}
// Apply all options
@ -188,7 +184,7 @@ func (s *Server) SaveWorld() error {
if s.persistence == PersistentData {
s.world.RLock()
defer s.world.RUnlock()
if err := persistence.SaveAll("world", s.world, "accounts", s.accountant); err != nil {
if err := persistence.SaveAll("world", s.world); err != nil {
return fmt.Errorf("failed to save out persistent data: %s", err)
}
}
@ -200,7 +196,7 @@ func (s *Server) LoadWorld() error {
if s.persistence == PersistentData {
s.world.Lock()
defer s.world.Unlock()
if err := persistence.LoadAll("world", &s.world, "accounts", &s.accountant); err != nil {
if err := persistence.LoadAll("world", &s.world); err != nil {
return err
}
}
@ -214,7 +210,7 @@ func (s *Server) SpawnRoverForAccount(account string) (string, error) {
return "", err
}
err = s.accountant.AssignData(account, "rover", inst)
err = s.world.Accountant.AssignData(account, "rover", inst)
if err != nil {
log.Printf("Failed to assign rover to account, %s", err)

View file

@ -1,90 +0,0 @@
package internal
import (
"fmt"
"time"
"github.com/google/uuid"
)
// SimpleAccountant manages a set of accounts
type SimpleAccountant struct {
Accounts map[string]Account
}
// NewSimpleAccountant creates a new accountant
func NewSimpleAccountant() Accountant {
return &SimpleAccountant{
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *SimpleAccountant) RegisterAccount(name string) (acc Account, err error) {
// Set up the account info
acc.Name = name
acc.Data = make(map[string]string)
// Verify this acount isn't already registered
for _, a := range a.Accounts {
if a.Name == acc.Name {
return Account{}, fmt.Errorf("account name already registered: %s", a.Name)
}
}
// Set the creation time
acc.Data["created"] = time.Now().String()
// Create a secret
acc.Data["secret"] = uuid.New().String()
// Simply add the account to the map
a.Accounts[acc.Name] = acc
return
}
// VerifySecret verifies if an account secret is correct
func (a *SimpleAccountant) VerifySecret(account string, secret string) (bool, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
return this.Data["secret"] == secret, nil
}
return false, fmt.Errorf("no account found for id: %s", account)
}
// GetSecret gets the internal secret
func (a *SimpleAccountant) GetSecret(account string) (string, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
return this.Data["secret"], nil
}
return "", fmt.Errorf("no account found for id: %s", account)
}
// AssignData assigns data to an account
func (a *SimpleAccountant) AssignData(account string, key string, value string) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
this.Data[key] = value
a.Accounts[account] = this
} else {
return fmt.Errorf("no account found for id: %s", account)
}
return nil
}
// GetValue gets the rover rover for the account
func (a *SimpleAccountant) GetValue(account string, key string) (string, error) {
// Find the account matching the ID
this, ok := a.Accounts[account]
if !ok {
return "", fmt.Errorf("no account found for id: %s", account)
}
return this.Data[key], nil
}