From d581b22e15b84b122a60bdac4a12ace409cdce92 Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 18 Nov 2026 14:27:17 +0000 Subject: [PATCH] cmd/tailscale/cli: export advertised Services without serve config get-config left them out of --all, and for --service wrote {}, which set-config read as the legacy format. Write them with "endpoints": {}. Updates #18381 Signed-off-by: Kristoffer Dalby --- cmd/tailscale/cli/serve_config_files_test.go | 54 +++++++++++ cmd/tailscale/cli/serve_config_mixed_test.go | 89 +++++++++++++++++++ .../cli/serve_config_roundtrip_test.go | 40 +++++++++ cmd/tailscale/cli/serve_v2.go | 31 ++++--- 4 files changed, 202 insertions(+), 12 deletions(-) create mode 100644 cmd/tailscale/cli/serve_config_mixed_test.go diff --git a/cmd/tailscale/cli/serve_config_files_test.go b/cmd/tailscale/cli/serve_config_files_test.go index dbbd5556a..048f1d6a3 100644 --- a/cmd/tailscale/cli/serve_config_files_test.go +++ b/cmd/tailscale/cli/serve_config_files_test.go @@ -53,6 +53,60 @@ func TestServeConfigExistingFiles(t *testing.T) { file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://DB.Example.com:5432"}}}}`, means: []string{"--service=svc:foo --tcp=5432 tcp://DB.Example.com:5432"}, }, + + // Advertisement. + { + name: "advertised_false", + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false}}}`, + means: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "drain svc:foo", + }, + }, + { + name: "advertised_true_is_default", + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": true}}}`, + means: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"}, + export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`, + }, + { + name: "no_endpoints", + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`, + means: []string{"advertise svc:foo"}, + }, + { + name: "no_endpoints_not_advertised", + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}, "advertised": false}}}`, + export: `{"version": "0.0.1"}`, + }, + { + name: "no_services", + file: `{"version": "0.0.1", "services": {}}`, + export: `{"version": "0.0.1"}`, + }, + { + name: "version_only", + file: `{"version": "0.0.1"}`, + }, + + // One Service. + { + name: "service", + flag: "--service=svc:foo", + file: `{"version": "0.0.1", "endpoints": {"tcp:443": "https://localhost:8443"}}`, + means: []string{"--service=svc:foo --https=443 https://localhost:8443"}, + }, + { + name: "service_no_endpoints", + flag: "--service=svc:foo", + file: `{"version": "0.0.1", "endpoints": {}}`, + means: []string{"advertise svc:foo"}, + }, + { + name: "service_no_endpoints_not_advertised", + flag: "--service=svc:foo", + file: `{"version": "0.0.1", "endpoints": {}, "advertised": false}`, + }, }) } diff --git a/cmd/tailscale/cli/serve_config_mixed_test.go b/cmd/tailscale/cli/serve_config_mixed_test.go new file mode 100644 index 000000000..c726e5247 --- /dev/null +++ b/cmd/tailscale/cli/serve_config_mixed_test.go @@ -0,0 +1,89 @@ +// Copyright (c) Tailscale Inc & contributors +// SPDX-License-Identifier: BSD-3-Clause + +package cli + +import ( + "strings" + "testing" +) + +// TestServeConfigMixed runs sequences that mix tailscale serve commands and +// config files, and checks each lands on the state the commands in want +// build, in state and in every read-out. A step is one of: +// +// "" tailscale serve +// "set-config " set-config of that file +// "set-config $FROM" set-config of what get-config writes on the from node +// "set-config --all $FROM_STATUS" set-config --all of what serve status --json writes on the from node +func TestServeConfigMixed(t *testing.T) { + tests := []struct { + name string + from []string // tailscale serve commands that build the from node + steps []string + want []string // tailscale serve commands that build the same state + }{ + // One node's export applied over another's state. See + // TestServeConfigRoundTrip for --all. + { + name: "export_of_advertised_service_without_config", + from: []string{"advertise svc:foo"}, + steps: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "set-config --service=svc:foo $FROM", + }, + want: []string{"advertise svc:foo"}, + }, + + // set-config reads serve status --json as the legacy format. It + // carries what the config file format cannot, but has no advertised + // state: every service it names comes back advertised. + { + name: "status_json_loses_drain", + from: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "drain svc:foo", + }, + steps: []string{"set-config --all $FROM_STATUS"}, + want: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"}, + }, + { + name: "status_json_loses_advertised_without_config", + from: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "advertise svc:bar", + }, + steps: []string{"set-config --all $FROM_STATUS"}, + want: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + dir := t.TempDir() + from := buildState(t, dir, tt.from) + got := newServiceHostClient() + for _, step := range tt.steps { + verb, rest, _ := strings.Cut(step, " ") + if verb != "set-config" { + runServeCmd(t, got, cmd(step)...) + continue + } + flag, file, _ := strings.Cut(rest, " ") + switch file { + case "$FROM": + file = runServeCmd(t, from, "get-config", flag) + case "$FROM_STATUS": + // set-config warns that serve status --json is deprecated + // as input; the warning is pinned in TestRunServeSetConfig. + if _, stderr, err := serveRun(got, "set-config", flag, writeTmpServeConfig(t, runServeCmd(t, from, "status", "--json"))); err != nil { + t.Fatalf("step %q: %v\n%s", step, err, stderr) + } + continue + } + setConfig(t, got, flag, file) + } + assertSameState(t, buildState(t, dir, tt.want), got) + }) + } +} diff --git a/cmd/tailscale/cli/serve_config_roundtrip_test.go b/cmd/tailscale/cli/serve_config_roundtrip_test.go index 18e1903a6..7c7d028a8 100644 --- a/cmd/tailscale/cli/serve_config_roundtrip_test.go +++ b/cmd/tailscale/cli/serve_config_roundtrip_test.go @@ -22,6 +22,19 @@ import ( "tailscale.com/tailcfg" ) +// The tests in this file, serve_config_files_test.go and +// serve_config_mixed_test.go treat a node's Services (its ipn.ServeConfig +// Services plus which of them it advertises) as the state, and check that +// every way in and out of it agrees: +// +// tailscale serve ... -> state <- set-config +// state -> get-config (the file) +// state -> serve status --json +// +// Each row of TestServeConfigRoundTrip is one state spelled three ways side by +// side: the tailscale serve commands that build it, the file get-config writes +// for it, and the state itself. + // TestServeConfigRoundTrip checks, for each row, that the commands build the // state; that the file builds it with set-config, on a fresh node and over one // serving something else; that get-config writes the file back whichever way @@ -70,6 +83,33 @@ func TestServeConfigRoundTrip(t *testing.T) { }}, advertised: []string{"svc:foo"}, }, + + // Advertisement. + { + name: "drained", + cli: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "drain svc:foo", + }, + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false}}}`, + services: services{"svc:foo": {TCP: tcp{5432: {TCPForward: "localhost:5432"}}}}, + }, + { + name: "advertised_without_config", + cli: []string{"advertise svc:foo"}, + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`, + advertised: []string{"svc:foo"}, + }, + { + // Turning off a Service's last port leaves it advertised. + name: "last_port_turned_off", + cli: []string{ + "--service=svc:foo --tcp=5432 tcp://localhost:5432", + "--service=svc:foo --tcp=5432 off", + }, + file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`, + advertised: []string{"svc:foo"}, + }, } for _, tt := range tests { diff --git a/cmd/tailscale/cli/serve_v2.go b/cmd/tailscale/cli/serve_v2.go index 04a47c4a8..7dc288c73 100644 --- a/cmd/tailscale/cli/serve_v2.go +++ b/cmd/tailscale/cli/serve_v2.go @@ -686,7 +686,8 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er magicDNSSuffix := st.CurrentTailnet.MagicDNSSuffix handleService := func(svcName tailcfg.ServiceName, serviceConfig *ipn.ServiceConfig) (*conffile.ServiceDetailsFile, error) { - var sdf conffile.ServiceDetailsFile + // "endpoints" is required, so a Service without any writes {}. + sdf := conffile.ServiceDetailsFile{Endpoints: map[*tailcfg.ProtoPortRange]*conffile.Target{}} // Leave unset for true case since that's the default. if !advertised.Contains(svcName.String()) { sdf.Advertised.Set(false) @@ -793,6 +794,12 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er } else if e.allServices { var scf conffile.ServicesConfigFile scf.Version = "0.0.1" + // An advertised Service without serve config is still state to write. + for _, svc := range prefs.AdvertiseServices { + if _, ok := sc.Services[tailcfg.ServiceName(svc)]; !ok { + mak.Set(&sc.Services, tailcfg.ServiceName(svc), new(ipn.ServiceConfig)) + } + } for svcName, serviceConfig := range sc.Services { sdf, err := handleService(svcName, serviceConfig) if err != nil { @@ -807,17 +814,17 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er } else if forSingleService { serviceConfig, ok := sc.Services[e.service] if !ok { - j = []byte("{}") - } else { - sdf, err := handleService(e.service, serviceConfig) - if err != nil { - return err - } - sdf.Version = "0.0.1" - j, err = json.MarshalIndent(sdf, "", " ") - if err != nil { - return err - } + // No serve config is still a state the file can say. + serviceConfig = new(ipn.ServiceConfig) + } + sdf, err := handleService(e.service, serviceConfig) + if err != nil { + return err + } + sdf.Version = "0.0.1" + j, err = json.MarshalIndent(sdf, "", " ") + if err != nil { + return err } } else { return errors.New("must specify either --service=svc: or --all")