Remove account IDs in favor of just account names

These were a "security" feature but pre-emptive and just add complications when we can implement secrets later
This commit is contained in:
Marc Di Luzio 2020-06-10 18:56:24 +01:00
parent b3b369f608
commit 7749854eb7
8 changed files with 23 additions and 53 deletions

View file

@ -13,9 +13,6 @@ type Account struct {
// Name simply describes the account and must be unique
Name string `json:"name"`
// Id represents a unique ID per account and is set one registered
Id uuid.UUID `json:"id"`
// Rover represents the rover that this account owns
Rover uuid.UUID `json:"rover"`
}
@ -26,40 +23,37 @@ type accountantData struct {
// Accountant manages a set of accounts
type Accountant struct {
Accounts map[uuid.UUID]Account `json:"accounts"`
Accounts map[string]Account `json:"accounts"`
}
// NewAccountant creates a new accountant
func NewAccountant() *Accountant {
return &Accountant{
Accounts: make(map[uuid.UUID]Account),
Accounts: make(map[string]Account),
}
}
// RegisterAccount adds an account to the set of internal accounts
func (a *Accountant) RegisterAccount(name string) (acc Account, err error) {
// Set the account ID to a new UUID
acc.Id = uuid.New()
// Set the account name
acc.Name = name
// 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")
} else if a.Id == acc.Id {
return Account{}, fmt.Errorf("Account ID already registered")
}
}
// Simply add the account to the map
a.Accounts[acc.Id] = acc
a.Accounts[acc.Name] = acc
return
}
// AssignRover assigns rover ownership of an rover to an account
func (a *Accountant) AssignRover(account, rover uuid.UUID) error {
func (a *Accountant) AssignRover(account string, rover uuid.UUID) error {
// Find the account matching the ID
if this, ok := a.Accounts[account]; ok {
@ -73,7 +67,7 @@ func (a *Accountant) AssignRover(account, rover uuid.UUID) error {
}
// GetRover gets the rover rover for the account
func (a *Accountant) GetRover(account uuid.UUID) (uuid.UUID, error) {
func (a *Accountant) GetRover(account string) (uuid.UUID, error) {
// Find the account matching the ID
if this, ok := a.Accounts[account]; !ok {
return uuid.UUID{}, fmt.Errorf("no account found for id: %s", account)

View file

@ -36,11 +36,6 @@ func TestAccountant_RegisterAccount(t *testing.T) {
t.Errorf("Missmatched account name after register, expected: %s, actual: %s", nameb, acca.Name)
}
// Verify our accounts have differing IDs
if acca.Id == accb.Id {
t.Error("Duplicate account IDs fo separate accounts")
}
// Verify another request gets rejected
_, err = accountant.RegisterAccount(namea)
if err == nil {
@ -62,12 +57,12 @@ func TestAccountant_AssignGetRover(t *testing.T) {
inst := uuid.New()
err = accountant.AssignRover(a.Id, inst)
err = accountant.AssignRover(a.Name, inst)
if err != nil {
t.Error("Failed to set rover for created account")
} else if accountant.Accounts[a.Id].Rover != inst {
} else if accountant.Accounts[a.Name].Rover != inst {
t.Error("Rover for assigned account is incorrect")
} else if id, err := accountant.GetRover(a.Id); err != nil {
} else if id, err := accountant.GetRover(a.Name); err != nil {
t.Error("Failed to get rover for account")
} else if id != inst {
t.Error("Fetched rover is incorrect for account")