zhangly1 on X: Claude on another computer on the LAN needed magpie at https://. Settings → Network and sharing → HTTPS (magpie https on) serves other computers TLS on the gateway's own port, with a certificate magpie's CA (the mTLS one, in ~/.config/magpie/mtls) signs for this computer's names and addresses; this computer keeps plain http://. The switch is read per connection, so no restart. - Certificate row: Import… takes the user's own PEM certificate (with chain) and unencrypted key, in any files and order; a pair that can't be served (no key, no cert, a stranger key, a passphrase, run out, nothing PEM) is refused with the reason in the page's language and leaves nothing behind. Remove goes back to magpie's CA. --tls-cert still comes first. - magpie's CA row: SHA-256 shown, Save puts magpie-ca.crt in Downloads; the gateway serves it without a key at /magpie-ca.crt to any address the share lets in. Rows under it give curl -kO, macOS, Windows, Linux and NODE_EXTRA_CA_CERTS steps with Copy. - A page open from another computer can't turn HTTPS on or off under itself (it would lose its own address). - magpie https status|on|off|import|remove|ca --out; the serve banner names magpie's CA or the imported file instead of "(mTLS)". - docs/reference.md and gateway-routing.md say all of it, and that Windows' curl.exe needs --ssl-no-revoke for any private CA. Run: - gofmt -l clean; go vet linux -tags nogui ./internal/..., GOOS=windows and GOOS=darwin -tags nogui vet of the touched packages (WSL, mote refused ssh all evening). - WSL: go test -tags nogui gateway -run HTTPS|MTLS|ClientCert|CAFile|TLS|LAN ok; gui -run HTTPS|MTLS|Keep ok; settings, settingsapi, mtls ok; cli -run Help|HTTPS|TLS|Serve|Banner|Flag|Unknown ok. The whole cli package hit its 40m timeout in TestEveryAgentFieldNameWritesNothing (#5234, #4247, not this change). - GUI (WSL Playwright, chromium + webkit): https-settings (en, zh, 390px, strings in all four languages) ok, plus mtls-settings, allow-ips, browser-access, cors-origins, gui-de/ja/zh-tw, i18n-keys, i18n-en-no-cjk, lan-*, public-url*, api-key-theme, connect-address-kept, gateway-connect-keys, gateway-caller-keys, side-gateway-lan, menu-scroll. Reds at load ~130: api-key-theme (#5044), connect-address-kept (#5972), gateway-caller-keys (#2572), gui-ja (#5943), lan-docker (#4692), all red the same at base10bef5fe0run beside it; allow-ips and gateway-connect-keys went red once under that load and pass 18/18 at concurrency 1, base and this change side by side. - Real: a sandbox HOME gateway built from this tree on WSL (MAGPIE_ADDR=0.0.0.0:3593, a gateway key, magpie https on, magpie serve). From the Windows host: curl -k /magpie-ca.crt 200 without a key; Node fetch with NODE_EXTRA_CA_CERTS=magpie-ca.crt 200 on /v1/models, 401 without the key, SELF_SIGNED_CERT_IN_CHAIN without the CA; plain http:// 400; curl.exe --cacert needs --ssl-no-revoke (schannel), then 200. Cert SANs: localhost, the hostname, .local, 127.0.0.1, ::1 and every address. magpie https import of an openssl CA's leaf on the running gateway: Node trusting that CA 200, magpie's CA alone refused. remove + off on the running gateway: plain http:// from Windows 200. This computer's plain http:// 200 throughout. - At the pushed tree (on origin/mainee818e8bc), WSL: gofmt -l clean; go vet -tags nogui linux, GOOS=windows, GOOS=darwin nogui of gateway, gui, cli ok; go test -run 'HTTPS|CAFile|MTLS|LAN' gateway, gui ok. GUI https-settings, mtls-settings, i18n-keys, gui-de/ja/zh-tw, allow-ips, lan-* (both engines): 147/155; reds are gui-ja (#5943), lan-docker (#4692), allow-ips webkit under load, and gui-zh-tw chromium under load (2 of 3 green alone, filed #5983). Broken by hand: - TLSOn without HTTPSOn: TestHTTPSOtherComputer red. - lanGuard without the caFileOpen exemption: TestHTTPSCAFile red. - mtls config without GetCertificate = settingsCert: all three serving tests red. - app.js without renderHTTPS(s): https-settings red in both engines. Not tried: the macOS, Windows and Linux system trust commands on a real store (they change the box's trust); Claude Desktop itself; a darwin GUI run (mote unreachable); the full suite before the tag (started after it).
241 lines
6.8 KiB
Go
241 lines
6.8 KiB
Go
package cli
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/agent"
|
|
)
|
|
|
|
// helpHome points the home and every config, cache and data folder into
|
|
// one new folder, and gives it.
|
|
func helpHome(t *testing.T) string {
|
|
t.Helper()
|
|
root := t.TempDir()
|
|
for k, v := range map[string]string{
|
|
"HOME": root, "USERPROFILE": root,
|
|
"XDG_CONFIG_HOME": filepath.Join(root, ".config"),
|
|
"XDG_CACHE_HOME": filepath.Join(root, ".cache"),
|
|
"XDG_DATA_HOME": filepath.Join(root, ".local", "share"),
|
|
"XDG_STATE_HOME": filepath.Join(root, ".local", "state"),
|
|
"APPDATA": filepath.Join(root, "AppData", "Roaming"),
|
|
"LOCALAPPDATA": filepath.Join(root, "AppData", "Local"),
|
|
"SNOW_CONFIG_DIR": "",
|
|
"DSH_HOME": "",
|
|
} {
|
|
t.Setenv(k, v)
|
|
}
|
|
return root
|
|
}
|
|
|
|
// tree is every file and folder under root, with each file's bytes and
|
|
// time.
|
|
func tree(t *testing.T, root string) map[string]string {
|
|
t.Helper()
|
|
out := map[string]string{}
|
|
filepath.WalkDir(root, func(p string, d fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
rel, _ := filepath.Rel(root, p)
|
|
if d.IsDir() {
|
|
out[rel] = "dir"
|
|
return nil
|
|
}
|
|
b, _ := os.ReadFile(p)
|
|
info, _ := d.Info()
|
|
out[rel] = string(b) + "@" + info.ModTime().String()
|
|
return nil
|
|
})
|
|
return out
|
|
}
|
|
|
|
// runOut runs magpie with args and gives what it printed; a run that
|
|
// doesn't come back in time (a gateway started, a window waited on) fails
|
|
// the test.
|
|
func runOut(t *testing.T, args ...string) (string, error) {
|
|
t.Helper()
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
old := os.Stdout
|
|
os.Stdout = w
|
|
var out strings.Builder
|
|
read := make(chan struct{})
|
|
go func() {
|
|
b, _ := io.ReadAll(r)
|
|
out.Write(b)
|
|
close(read)
|
|
}()
|
|
done := make(chan error, 1)
|
|
go func() { done <- run(args) }()
|
|
select {
|
|
case err = <-done:
|
|
case <-time.After(20 * time.Second):
|
|
os.Stdout = old
|
|
t.Fatalf("magpie %s didn't come back: it ran", strings.Join(args, " "))
|
|
}
|
|
w.Close()
|
|
os.Stdout = old
|
|
<-read
|
|
r.Close()
|
|
return out.String(), err
|
|
}
|
|
|
|
// runCommands are the commands run's switch dispatches on, read from
|
|
// main.go, so a command added there is covered without a list to keep.
|
|
func runCommands(t *testing.T) []string {
|
|
t.Helper()
|
|
f, err := parser.ParseFile(token.NewFileSet(), "main.go", nil, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var cmds []string
|
|
for _, d := range f.Decls {
|
|
fn, ok := d.(*ast.FuncDecl)
|
|
if !ok || fn.Name.Name != "run" {
|
|
continue
|
|
}
|
|
ast.Inspect(fn.Body, func(n ast.Node) bool {
|
|
sw, ok := n.(*ast.SwitchStmt)
|
|
if !ok {
|
|
return true
|
|
}
|
|
if ix, ok := sw.Tag.(*ast.IndexExpr); !ok || types(ix) != "args[0]" {
|
|
return true
|
|
}
|
|
for _, s := range sw.Body.List {
|
|
for _, e := range s.(*ast.CaseClause).List {
|
|
if lit, ok := e.(*ast.BasicLit); ok {
|
|
v, _ := strconv.Unquote(lit.Value)
|
|
cmds = append(cmds, v)
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
})
|
|
}
|
|
if len(cmds) < 40 {
|
|
t.Fatalf("read %d commands from run's switch: %v", len(cmds), cmds)
|
|
}
|
|
return append(cmds, "healthcheck", "heap", "cpu", "goroutines", "mtls", "https")
|
|
}
|
|
|
|
func types(ix *ast.IndexExpr) string {
|
|
id, ok := ix.X.(*ast.Ident)
|
|
lit, ok2 := ix.Index.(*ast.BasicLit)
|
|
if !ok || !ok2 {
|
|
return ""
|
|
}
|
|
return id.Name + "[" + lit.Value + "]"
|
|
}
|
|
|
|
// --help, -h or help after any command, and every agent, in any place,
|
|
// prints its usage and does nothing else: no file under the home is
|
|
// written and nothing is started. `magpie snow --help` set Snow's model to
|
|
// "--help" and took magpie out of it (#1457), and `magpie serve --help`
|
|
// ran a gateway.
|
|
func TestHelpOnEveryCommandDoesNothing(t *testing.T) {
|
|
root := helpHome(t)
|
|
cmds := runCommands(t)
|
|
for _, a := range agent.All() {
|
|
cmds = append(cmds, a.ID)
|
|
}
|
|
before := tree(t, root)
|
|
for _, c := range cmds {
|
|
t.Run(c, func(t *testing.T) {
|
|
for _, args := range [][]string{
|
|
{c, "--help"}, {c, "-h"}, {c, "help"},
|
|
{c, "x", "--help"}, {c, "x", "y", "-h"}, {c, "--help", "x"},
|
|
} {
|
|
out, err := runOut(t, args...)
|
|
if err != nil {
|
|
t.Errorf("magpie %s: %v", strings.Join(args, " "), err)
|
|
}
|
|
if !strings.Contains(out, "magpie") && !strings.Contains(out, "usage") {
|
|
t.Errorf("magpie %s printed no usage: %q", strings.Join(args, " "), out)
|
|
}
|
|
if got := tree(t, root); !reflect.DeepEqual(got, before) {
|
|
t.Fatalf("magpie %s wrote under the home:\nbefore %v\nafter %v", strings.Join(args, " "), before, got)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// The reporter's case (#1457, blackrion): Snow on magpie's profile, then
|
|
// `magpie snow --help` and its other forms. Each prints Snow's usage, a
|
|
// flag no agent form takes is refused, and Snow's files stay as they were.
|
|
func TestAgentHelpLeavesSnowWired(t *testing.T) {
|
|
groupsHome(t)
|
|
t.Setenv("SNOW_CONFIG_DIR", "")
|
|
dir := filepath.Join(os.Getenv("HOME"), ".snow")
|
|
os.MkdirAll(filepath.Join(dir, "profiles"), 0o755)
|
|
own := `{
|
|
"snowcfg": {
|
|
"baseUrl": "https://idealab.example.com/v1",
|
|
"apiKey": "team-key",
|
|
"requestMethod": "chat",
|
|
"advancedModel": "glm-5",
|
|
"basicModel": "glm-5"
|
|
}
|
|
}`
|
|
for f, s := range map[string]string{
|
|
"profiles/default.json": own,
|
|
"config.json": own,
|
|
"active-profile.json": "{\n \"activeProfile\": \"default\"\n}",
|
|
} {
|
|
if err := os.WriteFile(filepath.Join(dir, f), []byte(s), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := runOut(t, "snow", "a/m"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
before := tree(t, dir)
|
|
if _, ok := before[filepath.Join("profiles", "magpie.json")]; !ok {
|
|
t.Fatalf("not wired: %v", before)
|
|
}
|
|
for _, args := range [][]string{
|
|
{"snow", "--help"}, {"snow", "-h"}, {"snow", "help"}, {"snow", "model", "--help"}, {"snow-cli", "--help"},
|
|
} {
|
|
out, err := runOut(t, args...)
|
|
if err != nil || !strings.Contains(out, "magpie snow <model>") {
|
|
t.Errorf("magpie %s: %v %q", strings.Join(args, " "), err, out)
|
|
}
|
|
if got := tree(t, dir); !reflect.DeepEqual(got, before) {
|
|
t.Fatalf("magpie %s changed Snow's files:\nbefore %v\nafter %v", strings.Join(args, " "), before, got)
|
|
}
|
|
}
|
|
for _, args := range [][]string{
|
|
{"snow", "--verbose"}, {"snow", "model", "--x"}, {"snow", "-"}, {"snow", "effort", "--high"},
|
|
} {
|
|
_, err := runOut(t, args...)
|
|
if err == nil || !strings.Contains(err.Error(), "nothing was changed") {
|
|
t.Errorf("magpie %s: %v", strings.Join(args, " "), err)
|
|
}
|
|
if got := tree(t, dir); !reflect.DeepEqual(got, before) {
|
|
t.Fatalf("magpie %s changed Snow's files:\nbefore %v\nafter %v", strings.Join(args, " "), before, got)
|
|
}
|
|
}
|
|
// a model no profile of the user's is on, typed without a flag, is
|
|
// refused by Snow itself the same way
|
|
if _, err := runOut(t, "snow", "nonsense"); err == nil {
|
|
t.Error("magpie snow nonsense taken")
|
|
}
|
|
if got := tree(t, dir); !reflect.DeepEqual(got, before) {
|
|
t.Fatalf("magpie snow nonsense changed Snow's files:\nbefore %v\nafter %v", before, got)
|
|
}
|
|
}
|