From c7ea1b106f62d3f55148adc9d7abef7401d5fc19 Mon Sep 17 00:00:00 2001 From: James Jesudason Date: Thu, 17 Dec 2020 15:36:50 +0000 Subject: [PATCH 1/3] Add command-line option to add an account This is needed when the serial-vault is used on-premise with enableUserAuth turned off. --- manage/account.go | 1 + manage/account_test.go | 11 ++++++++++- manage/accountadd.go | 43 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 manage/accountadd.go diff --git a/manage/account.go b/manage/account.go index 979800e9..da688b8e 100644 --- a/manage/account.go +++ b/manage/account.go @@ -22,4 +22,5 @@ package manage // AccountCommand is the main command for account management type AccountCommand struct { Cache AccountCacheCommand `command:"cache" alias:"c" description:"Cache the account assertions from the store in the database"` + Add AccountAddCommand `command:"add" alias:"a" description:"Add a new account"` } diff --git a/manage/account_test.go b/manage/account_test.go index 967618ef..48969db9 100644 --- a/manage/account_test.go +++ b/manage/account_test.go @@ -46,7 +46,16 @@ func (s *AccountSuite) TestAccount(c *check.C) { ErrorMessage: "Unknown command `invalid'. You should use the cache command"}, { Args: []string{"serial-vault-admin", "account", "cache"}, - ErrorMessage: ""}, + ErrorMessage: "", + }, + { + Args: []string{"serial-vault-admin", "account", "add", "acc123"}, + ErrorMessage: "", + }, + { + Args: []string{"serial-vault-admin", "account", "add", "acc123", "-r"}, + ErrorMessage: "", + }, } for _, t := range tests { diff --git a/manage/accountadd.go b/manage/accountadd.go new file mode 100644 index 00000000..b2982d2c --- /dev/null +++ b/manage/accountadd.go @@ -0,0 +1,43 @@ +package manage + +import ( + "fmt" + "github.com/CanonicalLtd/serial-vault/datastore" +) + +// AccountAddCommand handles adding a new account for the serial-vault-admin command +type AccountAddCommand struct { + ResellerAPI bool `short:"r" long:"reseller" description:"Enable the reseller API"` +} + +// Execute the adding of an account +func (cmd AccountAddCommand) Execute(args []string) error { + err := checkAccountIDArg(args, "Add") + if err != nil { + return err + } + + // Open the database and create the account + openDatabase() + account := datastore.Account{ + AuthorityID: args[0], + ResellerAPI: cmd.ResellerAPI, + } + if err := datastore.Environ.DB.CreateAccount(account); err != nil { + return fmt.Errorf("error creating the account: %v", err) + } + + fmt.Printf("Account '%s' created successfully\n", account.AuthorityID) + return nil +} + +func checkAccountIDArg(args []string, action string) error { + switch len(args) { + case 0: + return fmt.Errorf("%s account expects an 'account ID' argument", action) + case 1: + return nil + default: + return fmt.Errorf("%s account expects a single 'account ID' argument", action) + } +} From 7d6f53c69e03ea1c35dd23201e1fdffd0106df25 Mon Sep 17 00:00:00 2001 From: James Jesudason Date: Thu, 17 Dec 2020 15:44:00 +0000 Subject: [PATCH 2/3] Fix to test error message --- manage/account_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manage/account_test.go b/manage/account_test.go index 48969db9..0d903840 100644 --- a/manage/account_test.go +++ b/manage/account_test.go @@ -40,7 +40,7 @@ func (s *AccountSuite) TestAccount(c *check.C) { tests := []manTest{ { Args: []string{"serial-vault-admin", "account"}, - ErrorMessage: "Please specify the cache command"}, + ErrorMessage: "Please specify one command of: add or cache"}, { Args: []string{"serial-vault-admin", "account", "invalid"}, ErrorMessage: "Unknown command `invalid'. You should use the cache command"}, From 14b03e1cd0d47233bb75d4100631be27a6b35a4e Mon Sep 17 00:00:00 2001 From: James Jesudason Date: Fri, 18 Dec 2020 14:24:41 +0000 Subject: [PATCH 3/3] Update error message on test --- manage/account_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manage/account_test.go b/manage/account_test.go index 0d903840..284ab7de 100644 --- a/manage/account_test.go +++ b/manage/account_test.go @@ -43,7 +43,7 @@ func (s *AccountSuite) TestAccount(c *check.C) { ErrorMessage: "Please specify one command of: add or cache"}, { Args: []string{"serial-vault-admin", "account", "invalid"}, - ErrorMessage: "Unknown command `invalid'. You should use the cache command"}, + ErrorMessage: "Unknown command `invalid'. Please specify one command of: add or cache"}, { Args: []string{"serial-vault-admin", "account", "cache"}, ErrorMessage: "",