Something went wrong. Try again.
small gleam coding and persistent agent daemon with a detachable cli and a canvas ui forked from okami.mom/albedo
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165package webui
import ( "context" "encoding/json" "errors" "fmt" "reflect" "slices" "strings" "testing")
// parseServeRoot decides whether --tailnet-https may take tailscale's https// root, so a misread entry would replace someone else's config or publish// albedo through Funnel. e2e/web_test.py drives the flag end to end.func TestParseServeRoot(t *testing.T) { const ours = `"albedo.example.ts.net:443":{"Handlers":{"/":{"Proxy":"http://127.0.0.1:4317"}}}` for _, test := range []struct { name string status string want serveRoot }{ {name: "nothing published", status: `{}`, want: serveRoot{}}, {name: "already ours", status: `{"TCP":{"443":{"HTTPS":true}},"Web":{` + ours + `}}`, want: serveRoot{ours: "albedo.example.ts.net:443"}}, { name: "ours beside an unknown key", status: `{"Web":{` + ours + `},"SomethingNew":{}}`, want: serveRoot{busy: []string{"SomethingNew", "Web albedo.example.ts.net:443"}}, }, { name: "ours opened to the internet", status: `{"Web":{` + ours + `},"AllowFunnel":{"albedo.example.ts.net:443":true}}`, want: serveRoot{busy: []string{"AllowFunnel albedo.example.ts.net:443", "Web albedo.example.ts.net:443"}}, }, { name: "a port that merely starts the same", status: `{"Web":{"albedo.example.ts.net:443":{"Handlers":{"/":{"Proxy":"http://127.0.0.1:43170"}}}}}`, want: serveRoot{busy: []string{"Web albedo.example.ts.net:443"}}, }, { name: "another port", status: `{"TCP":{"10000":{}},"Web":{"albedo.example.ts.net:8443":{"Handlers":{"/":{"Proxy":"http://127.0.0.1:4317"}}}}}`, want: serveRoot{busy: []string{"TCP 10000", "Web albedo.example.ts.net:8443"}}, }, } { t.Run(test.name, func(t *testing.T) { root, err := parseServeRoot([]byte(test.status), Loopback(4317).String()) if err != nil { t.Fatal(err) } if !reflect.DeepEqual(root, test.want) { t.Errorf("root = %#v, want %#v", root, test.want) } }) } if _, err := parseServeRoot([]byte("No serve config\n"), Loopback(4317).String()); err == nil { t.Error("a text status parsed") }}
// ServeHTTPS must refuse before any serve command when the control server// offers no certificate for the machine (Headscale reports CertDomains null),// and a wildcard covers exactly one label.func TestServeHTTPSNeedsACertificate(t *testing.T) { for _, test := range []struct { name, dnsName, domains string want bool }{ {"exact domain", "Albedo.Example.ts.net.", `["albedo.example.ts.net"]`, true}, {"wildcard domain", "albedo.example.ts.net.", `["*.example.ts.net"]`, true}, {"wildcard covers one label", "a.b.example.ts.net.", `["*.example.ts.net"]`, false}, {"another name", "albedo.example.ts.net.", `["other.example.ts.net"]`, false}, {"no certificates", "albedo.example.ts.net.", `null`, false}, } { t.Run(test.name, func(t *testing.T) { tailnet, ok := parseTailnet([]byte(`{"BackendState":"Running","Self":{"DNSName":"` + test.dnsName + `"},"CertDomains":` + test.domains + `}`)) if !ok { t.Fatal("not a tailnet") } if _, err := tailnet.ServeHTTPS(4317); (err == nil) != test.want { t.Errorf("ServeHTTPS() error = %v, want allowed %t", err, test.want) } }) }}
// fakeTailscaled keeps a serve config the way tailscaled does, answering the// four commands Claim and Release run.type fakeTailscaled struct { serve map[string]any // flaky loses the status read right after a serve command, as when // tailscaled restarts under it. flaky bool lost bool calls []string}
func (f *fakeTailscaled) run(_ context.Context, args ...string) (string, error) { f.calls = append(f.calls, strings.Join(args, " ")) switch { case slices.Equal(args, []string{"serve", "status", "--json"}): if f.lost { f.lost = false return "", errors.New("tailscaled went away") } encoded, _ := json.Marshal(f.serve) return string(encoded), nil case len(args) == 4 && slices.Equal(args[:3], []string{"serve", "--bg", "--https=443"}): f.serve = map[string]any{"TCP": map[string]any{"443": map[string]any{"HTTPS": true}}, "Web": map[string]any{"albedo.example.ts.net:443": map[string]any{"Handlers": map[string]any{"/": map[string]any{"Proxy": args[3]}}}}} f.lost, f.flaky = f.flaky, false return "", nil case slices.Equal(args, []string{"serve", "--https=443", "--set-path", "/", "off"}): f.serve = map[string]any{} return "", nil } return "", fmt.Errorf("unexpected tailscale %v", args)}
func withTailscaled(t *testing.T, fake *fakeTailscaled) { t.Helper() previous := tailscale tailscale = fake.run t.Cleanup(func() { tailscale = previous })}
// --tailnet-https publishes the root through tailscale serve and --stop takes// it back; a root Funnel opens to the internet is refused untouched; and a// handler created but not confirmed is removed before the refusal.func TestServeClaimsAndReleasesTheRootOnlyWhenItsOwn(t *testing.T) { serve := NewServe(4317) fake := &fakeTailscaled{serve: map[string]any{}} withTailscaled(t, fake) origin, err := serve.Claim(t.Context()) if err != nil || origin.String() != "https://albedo.example.ts.net" { t.Fatalf("claim: %v %v", origin, err) } proxy := fake.serve["Web"].(map[string]any)["albedo.example.ts.net:443"].(map[string]any)["Handlers"].(map[string]any)["/"].(map[string]any)["Proxy"] if proxy != "http://127.0.0.1:4317" { t.Fatalf("published %v", proxy) } if err := serve.Release(t.Context()); err != nil || len(fake.serve) != 0 { t.Fatalf("release left %v: %v", fake.serve, err) }
funnel := map[string]any{"Web": map[string]any{"albedo.example.ts.net:443": map[string]any{"Handlers": map[string]any{"/": map[string]any{"Proxy": "http://127.0.0.1:4317"}}}}, "AllowFunnel": map[string]any{"albedo.example.ts.net:443": true}} fake = &fakeTailscaled{serve: funnel} withTailscaled(t, fake) if _, err := serve.Claim(t.Context()); err == nil || !strings.Contains(err.Error(), "AllowFunnel") { t.Fatalf("a Funnel root was claimed: %v", err) } if len(fake.calls) != 1 { t.Fatalf("a refused claim changed tailscale: %v", fake.calls) }
fake = &fakeTailscaled{serve: map[string]any{}, flaky: true} withTailscaled(t, fake) if _, err := serve.Claim(t.Context()); err == nil || !strings.Contains(err.Error(), "tailscaled went away") { t.Fatalf("an unconfirmed handler was kept: %v", err) } if len(fake.serve) != 0 { t.Fatalf("the unconfirmed handler stayed: %v", fake.serve) }}