Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cmd/agentbbs/provision.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func provisionUser(st store.Store, args []string) {
pubkeyFile := fs.String("pubkey-file", "", "read the SSH public key from this file")
kind := fs.String("kind", string(auth.Member), "account kind: member | agent")
fs.Parse(args)
accountKind, ok := parseProvisionKind(*kind)
if !ok {
fail(`invalid --kind: must be "member" or "agent"`)
}

// Normalize with the same rules the hub uses for self-service joins, so
// store-provisioned handles are indistinguishable from join@ ones.
Expand Down Expand Up @@ -67,7 +71,7 @@ func provisionUser(st store.Store, args []string) {
fail(fmt.Sprintf("this key already belongs to member %q (fp %s)", existing.Name, fp))
}

u, err := st.EnsureUser(handle, *kind, fp)
u, err := st.EnsureUser(handle, string(accountKind), fp)
if err != nil {
if errors.Is(err, store.ErrKeyMismatch) {
fail(fmt.Sprintf("handle %q is already registered with a different key", handle))
Expand All @@ -84,6 +88,17 @@ func provisionUser(st store.Store, args []string) {
})
}

func parseProvisionKind(value string) (auth.Kind, bool) {
switch auth.Kind(strings.ToLower(strings.TrimSpace(value))) {
case auth.Member:
return auth.Member, true
case auth.Agent:
return auth.Agent, true
default:
return "", false
}
}

func fail(msg string) {
fmt.Fprintln(os.Stderr, "provision-user: "+msg)
os.Exit(1)
Expand Down
28 changes: 28 additions & 0 deletions cmd/agentbbs/provision_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"testing"

"github.com/profullstack/agentbbs/internal/auth"
)

func TestParseProvisionKind(t *testing.T) {
tests := []struct {
input string
want auth.Kind
ok bool
}{
{input: "member", want: auth.Member, ok: true},
{input: " Agent ", want: auth.Agent, ok: true},
{input: "guest", ok: false},
{input: "admin", ok: false},
{input: "", ok: false},
}

for _, test := range tests {
got, ok := parseProvisionKind(test.input)
if got != test.want || ok != test.ok {
t.Errorf("parseProvisionKind(%q) = (%q, %t), want (%q, %t)", test.input, got, ok, test.want, test.ok)
}
}
}
Loading