mirror of
https://github.com/profullstack/agentbbs.git
synced 2026-08-13 22:37:28 +00:00
fix: validate provisioned account kind (#112)
This commit is contained in:
parent
bf777a1475
commit
03a2ce3f8b
2 changed files with 44 additions and 1 deletions
|
|
@ -34,6 +34,10 @@ func provisionUser(st store.Store, args []string) {
|
||||||
pubkeyFile := fs.String("pubkey-file", "", "read the SSH public key from this file")
|
pubkeyFile := fs.String("pubkey-file", "", "read the SSH public key from this file")
|
||||||
kind := fs.String("kind", string(auth.Member), "account kind: member | agent")
|
kind := fs.String("kind", string(auth.Member), "account kind: member | agent")
|
||||||
fs.Parse(args)
|
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
|
// Normalize with the same rules the hub uses for self-service joins, so
|
||||||
// store-provisioned handles are indistinguishable from join@ ones.
|
// store-provisioned handles are indistinguishable from join@ ones.
|
||||||
|
|
@ -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))
|
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 err != nil {
|
||||||
if errors.Is(err, store.ErrKeyMismatch) {
|
if errors.Is(err, store.ErrKeyMismatch) {
|
||||||
fail(fmt.Sprintf("handle %q is already registered with a different key", handle))
|
fail(fmt.Sprintf("handle %q is already registered with a different key", handle))
|
||||||
|
|
@ -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) {
|
func fail(msg string) {
|
||||||
fmt.Fprintln(os.Stderr, "provision-user: "+msg)
|
fmt.Fprintln(os.Stderr, "provision-user: "+msg)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
||||||
28
cmd/agentbbs/provision_test.go
Normal file
28
cmd/agentbbs/provision_test.go
Normal file
|
|
@ -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…
Add table
Add a link
Reference in a new issue