feat: add secure settings foundation
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SystemState string
|
||||
|
||||
const (
|
||||
StateUninitialized SystemState = "uninitialized"
|
||||
StateCredentialsSaved SystemState = "credentials_saved"
|
||||
StateDingTalkVerified SystemState = "dingtalk_verified"
|
||||
StateWaitingAdmin SystemState = "waiting_first_admin"
|
||||
StateActive SystemState = "active"
|
||||
StateCredentialError SystemState = "credential_error"
|
||||
)
|
||||
|
||||
type Snapshot struct {
|
||||
ClientID string
|
||||
SecretConfigured bool
|
||||
CredentialsVerified bool
|
||||
AdministratorCount int
|
||||
}
|
||||
|
||||
func DetermineState(s Snapshot) SystemState {
|
||||
id := strings.TrimSpace(s.ClientID) != ""
|
||||
if !id && !s.SecretConfigured {
|
||||
return StateUninitialized
|
||||
}
|
||||
if !id || !s.SecretConfigured {
|
||||
return StateCredentialError
|
||||
}
|
||||
if !s.CredentialsVerified {
|
||||
return StateCredentialsSaved
|
||||
}
|
||||
if s.AdministratorCount < 1 {
|
||||
return StateWaitingAdmin
|
||||
}
|
||||
return StateActive
|
||||
}
|
||||
|
||||
type Command struct {
|
||||
Name string
|
||||
Args []string
|
||||
}
|
||||
|
||||
func ParseCommand(raw string) (Command, bool) {
|
||||
fields := strings.Fields(strings.TrimSpace(raw))
|
||||
if len(fields) == 0 {
|
||||
return Command{}, false
|
||||
}
|
||||
name := strings.ToLower(fields[0])
|
||||
switch name {
|
||||
case "/setting", "/settings", "/设置":
|
||||
default:
|
||||
return Command{}, false
|
||||
}
|
||||
return Command{Name: name, Args: fields[1:]}, true
|
||||
}
|
||||
|
||||
type NotableLink struct{ BaseID, SheetID, ViewID string }
|
||||
|
||||
func ParseNotableLink(raw string) (NotableLink, error) {
|
||||
if len(raw) > 4096 {
|
||||
return NotableLink{}, errors.New("AI Table link is too long")
|
||||
}
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil || u.Scheme != "https" {
|
||||
return NotableLink{}, errors.New("invalid AI Table link")
|
||||
}
|
||||
host := strings.ToLower(u.Hostname())
|
||||
if host != "alidocs.dingtalk.com" && host != "docs.dingtalk.com" {
|
||||
return NotableLink{}, errors.New("untrusted AI Table host")
|
||||
}
|
||||
base := path.Base(strings.TrimRight(u.Path, "/"))
|
||||
q := u.Query()
|
||||
result := NotableLink{BaseID: base, SheetID: q.Get("sheetId"), ViewID: q.Get("viewId")}
|
||||
if result.BaseID == "" || result.BaseID == "." || result.SheetID == "" {
|
||||
return NotableLink{}, errors.New("AI Table link is missing base or sheet")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
type Runtime interface {
|
||||
Start(context.Context, string) error
|
||||
Stop(context.Context) error
|
||||
}
|
||||
type Switcher struct {
|
||||
runtime Runtime
|
||||
active string
|
||||
}
|
||||
|
||||
func NewSwitcher(r Runtime, active string) *Switcher { return &Switcher{runtime: r, active: active} }
|
||||
func (s *Switcher) Switch(ctx context.Context, candidate string) error {
|
||||
if s == nil || s.runtime == nil {
|
||||
return errors.New("runtime is required")
|
||||
}
|
||||
candidate = strings.TrimSpace(candidate)
|
||||
if candidate == "" {
|
||||
return errors.New("candidate is required")
|
||||
}
|
||||
old := s.active
|
||||
if err := s.runtime.Stop(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.runtime.Start(ctx, candidate); err != nil {
|
||||
if old != "" {
|
||||
_ = s.runtime.Start(ctx, old)
|
||||
}
|
||||
return err
|
||||
}
|
||||
s.active = candidate
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
package settings
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDetermineState(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
in Snapshot
|
||||
want SystemState
|
||||
}{
|
||||
{"empty", Snapshot{}, StateUninitialized},
|
||||
{"partial", Snapshot{ClientID: "id"}, StateCredentialError},
|
||||
{"saved", Snapshot{ClientID: "id", SecretConfigured: true}, StateCredentialsSaved},
|
||||
{"verified", Snapshot{ClientID: "id", SecretConfigured: true, CredentialsVerified: true}, StateWaitingAdmin},
|
||||
{"active", Snapshot{ClientID: "id", SecretConfigured: true, CredentialsVerified: true, AdministratorCount: 1}, StateActive},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
if got := DetermineState(tc.in); got != tc.want {
|
||||
t.Fatalf("got %s want %s", got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseCommandStrict(t *testing.T) {
|
||||
yes := []string{"/setting", "/settings", "/设置", "/setting 123456", "/setting language ja", "/setting support", "/setting question https://alidocs.dingtalk.com/i/nodes/abc"}
|
||||
for _, raw := range yes {
|
||||
if c, ok := ParseCommand(raw); !ok || c.Name == "" {
|
||||
t.Fatalf("must match %q", raw)
|
||||
}
|
||||
}
|
||||
no := []string{"如何设置机器人", "abc /setting", "请解释/setting", "/settiing"}
|
||||
for _, raw := range no {
|
||||
if _, ok := ParseCommand(raw); ok {
|
||||
t.Fatalf("must not match %q", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseNotableLink(t *testing.T) {
|
||||
got, err := ParseNotableLink("https://alidocs.dingtalk.com/i/nodes/base123?sheetId=sheet456&viewId=view789&recordId=row")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.BaseID != "base123" || got.SheetID != "sheet456" || got.ViewID != "view789" {
|
||||
t.Fatalf("got %+v", got)
|
||||
}
|
||||
if _, err = ParseNotableLink("http://evil.example/x?sheetId=s"); err == nil {
|
||||
t.Fatal("expected rejection")
|
||||
}
|
||||
}
|
||||
|
||||
type fakeRuntime struct {
|
||||
active string
|
||||
starts []string
|
||||
fail string
|
||||
}
|
||||
|
||||
func (f *fakeRuntime) Start(_ context.Context, id string) error {
|
||||
f.starts = append(f.starts, id)
|
||||
if id == f.fail {
|
||||
return errors.New("boom")
|
||||
}
|
||||
f.active = id
|
||||
return nil
|
||||
}
|
||||
func (f *fakeRuntime) Stop(_ context.Context) error { f.active = ""; return nil }
|
||||
func TestSwitcherRollsBack(t *testing.T) {
|
||||
f := &fakeRuntime{active: "old", fail: "new"}
|
||||
s := NewSwitcher(f, "old")
|
||||
if err := s.Switch(context.Background(), "new"); err == nil {
|
||||
t.Fatal("expected error")
|
||||
}
|
||||
if f.active != "old" {
|
||||
t.Fatalf("active=%q", f.active)
|
||||
}
|
||||
if len(f.starts) != 2 || f.starts[0] != "new" || f.starts[1] != "old" {
|
||||
t.Fatalf("starts=%v", f.starts)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user