mirror of
https://github.com/tailscale/tailscale.git
synced 2026-12-06 23:36:34 +09:00
ipn/conffile,cmd/tailscale/cli: add object form for Service endpoints
A string endpoint's scheme names both listener and back-end, so it cannot
say HTTPS in front of plain HTTP, the CLI's default: get-config wrote
"http://...", which set-config applied as plaintext HTTP.
{"front": "https", "backend": "http://..."} names the listener; get-config
writes it only where the string implies another listener. No version bump:
older clients fail at parse on an object instead of misapplying it.
front/backend follow the draft in #20116.
RELNOTE: tailscale serve config files can put HTTPS in front of a plain-HTTP back-end, which get-config wrote as plaintext HTTP
Fixes #18381
Signed-off-by: Kristoffer Dalby <kristoffer@tailscale.com>
This commit is contained in:
1 parent
0d3d93e040
commit
27bd01a67c
7 files changed
+1777
-6
No files matched your search
@@ -0,0 +1,532 @@
|
||||
// Copyright (c) Tailscale Inc & contributors
|
||||
// SPDX-License-Identifier: BSD-3-Clause
|
||||
|
||||
package cli
|
||||
|
||||
import "testing"
|
||||
|
||||
// TestServeConfigExamples spells each row the three ways a user meets it: the
|
||||
// tailscale serve commands, the file get-config --all writes for them, and
|
||||
// serve status --json. Both the commands and that file applied with
|
||||
// set-config --all on a fresh node must read out as the row says, so what
|
||||
// set-config reads is what get-config writes. A file that round-trips to
|
||||
// itself can still name the wrong listener; serve status --json shows the
|
||||
// one actually served.
|
||||
func TestServeConfigExamples(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
cli []string // tailscale serve ...
|
||||
config string // tailscale serve get-config --all
|
||||
status string // tailscale serve status --json
|
||||
}{
|
||||
// A string endpoint's scheme names both the listener and the back-end.
|
||||
{
|
||||
name: "http_to_http",
|
||||
cli: []string{"--service=svc:awesome --http=80 127.0.0.1:30123"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:80": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"80": {
|
||||
"HTTP": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:80": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "https_to_https",
|
||||
cli: []string{"--service=svc:awesome --https=443 https://127.0.0.1:8443"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": "https://127.0.0.1:8443"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "https://127.0.0.1:8443"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
// The case from #18381: HTTPS in front of plain HTTP, which no
|
||||
// string says, so the endpoint is an object naming the listener.
|
||||
name: "https_to_http",
|
||||
cli: []string{"--service=svc:awesome --https=443 127.0.0.1:30123"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": {
|
||||
"front": "https",
|
||||
"backend": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
// The string older clients wrote for the row above keeps the
|
||||
// meaning it always had: plaintext HTTP on port 443.
|
||||
name: "http_on_443",
|
||||
cli: []string{"--service=svc:awesome --http=443 127.0.0.1:30123"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTP": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "http_to_https",
|
||||
cli: []string{"--service=svc:awesome --http=80 https://127.0.0.1:8443"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:80": {
|
||||
"front": "http",
|
||||
"backend": "https://127.0.0.1:8443"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"80": {
|
||||
"HTTP": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:80": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "https://127.0.0.1:8443"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
// file:// is served over HTTPS.
|
||||
name: "https_to_file",
|
||||
cli: []string{"--service=svc:awesome --https=443 $DIR/index.html"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": "file://$DIR/index.html"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Path": "$DIR/index.html"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
// tailscaled speaks plain HTTP to a Unix socket, so the scheme
|
||||
// names only the listener.
|
||||
name: "https_to_unix_socket",
|
||||
cli: []string{"--service=svc:awesome --https=443 unix:/run/app.sock"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": "https://unix:/run/app.sock"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "unix:/run/app.sock"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "tcp",
|
||||
cli: []string{"--service=svc:awesome --tcp=5432 tcp://127.0.0.1:5432"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:5432": "tcp://127.0.0.1:5432"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"5432": {
|
||||
"TCPForward": "127.0.0.1:5432"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp",
|
||||
cli: []string{"--service=svc:awesome --tls-terminated-tcp=443 tcp://127.0.0.1:9481"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": "tls-terminated-tcp://127.0.0.1:9481"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"TCPForward": "127.0.0.1:9481",
|
||||
"TerminateTLS": "awesome.test.ts.net"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "tun",
|
||||
cli: []string{"--service=svc:awesome --tun"},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"*": "TUN"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"Tun": true
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
// serve status --json does not show advertisement, so only the
|
||||
// file says the Service is drained.
|
||||
name: "drained",
|
||||
cli: []string{
|
||||
"--service=svc:awesome --tcp=5432 tcp://127.0.0.1:5432",
|
||||
"drain svc:awesome",
|
||||
},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:5432": "tcp://127.0.0.1:5432"
|
||||
},
|
||||
"advertised": false
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"5432": {
|
||||
"TCPForward": "127.0.0.1:5432"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
{
|
||||
name: "two_services",
|
||||
cli: []string{
|
||||
"--service=svc:awesome --https=443 127.0.0.1:30123",
|
||||
"--service=svc:db --tcp=5432 tcp://127.0.0.1:5432",
|
||||
"drain svc:db",
|
||||
},
|
||||
config: `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": {
|
||||
"front": "https",
|
||||
"backend": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
},
|
||||
"svc:db": {
|
||||
"endpoints": {
|
||||
"tcp:5432": "tcp://127.0.0.1:5432"
|
||||
},
|
||||
"advertised": false
|
||||
}
|
||||
}
|
||||
}`,
|
||||
status: `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"svc:db": {
|
||||
"TCP": {
|
||||
"5432": {
|
||||
"TCPForward": "127.0.0.1:5432"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
skipOnWindows(t, tt.cli...)
|
||||
dir := serveTestDir(t)
|
||||
config, status := expandDir(tt.config, dir), expandDir(tt.status, dir)
|
||||
|
||||
// cli -> get-config == config, status == status
|
||||
cliNode := buildState(t, dir, tt.cli)
|
||||
checkJSON(t, "get-config --all after the commands", config, runServeCmd(t, cliNode, "get-config", "--all"))
|
||||
checkJSON(t, "serve status --json after the commands", status, runServeCmd(t, cliNode, "status", "--json"))
|
||||
|
||||
// set-config(config) -> get-config == config, status == status
|
||||
fileNode := newServiceHostClient()
|
||||
setConfig(t, fileNode, "--all", config)
|
||||
checkJSON(t, "get-config --all after set-config --all of config", config, runServeCmd(t, fileNode, "get-config", "--all"))
|
||||
checkJSON(t, "serve status --json after set-config --all of config", status, runServeCmd(t, fileNode, "status", "--json"))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeConfigIssue18381 replays the steps of #18381 on one node: a
|
||||
// Service exported with get-config and imported with set-config after a reset
|
||||
// comes back serving HTTPS, as it was, not plaintext HTTP on port 443.
|
||||
func TestServeConfigIssue18381(t *testing.T) {
|
||||
const servingHTTPS = `
|
||||
{
|
||||
"Services": {
|
||||
"svc:awesome": {
|
||||
"TCP": {
|
||||
"443": {
|
||||
"HTTPS": true
|
||||
}
|
||||
},
|
||||
"Web": {
|
||||
"awesome.test.ts.net:443": {
|
||||
"Handlers": {
|
||||
"/": {
|
||||
"Proxy": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
node := newServiceHostClient()
|
||||
|
||||
// 1. Register a service with TLS.
|
||||
runServeCmd(t, node, cmd("--service=svc:awesome --https=443 127.0.0.1:30123")...)
|
||||
checkJSON(t, "1. serve status --json", servingHTTPS, runServeCmd(t, node, "status", "--json"))
|
||||
|
||||
// 2. Export service config to a file.
|
||||
services := runServeCmd(t, node, "get-config", "--all")
|
||||
checkJSON(t, "2. get-config --all", `
|
||||
{
|
||||
"version": "0.0.1",
|
||||
"services": {
|
||||
"svc:awesome": {
|
||||
"endpoints": {
|
||||
"tcp:443": {
|
||||
"front": "https",
|
||||
"backend": "http://127.0.0.1:30123"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`, services)
|
||||
|
||||
// 3. Reset serve config.
|
||||
runServeCmd(t, node, "reset")
|
||||
checkJSON(t, "3. serve status --json after reset", `{}`, runServeCmd(t, node, "status", "--json"))
|
||||
|
||||
// 4. Import the file.
|
||||
runServeCmd(t, node, "set-config", "--all", writeTmpServeConfig(t, services))
|
||||
checkJSON(t, "4. serve status --json after set-config --all", servingHTTPS, runServeCmd(t, node, "status", "--json"))
|
||||
}
|
||||
@@ -5,7 +5,11 @@ package cli
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/feature/buildfeatures"
|
||||
)
|
||||
|
||||
// fileRow is a config file that set-config accepts. The file means the state
|
||||
@@ -17,6 +21,7 @@ type fileRow struct {
|
||||
file string // applied with set-config
|
||||
means []string // tailscale serve commands that build the same state
|
||||
export string // what get-config then writes; file if empty
|
||||
hujson bool // file is HuJSON, which needs support built in
|
||||
}
|
||||
|
||||
// TestServeConfigExistingFiles pins what every 0.0.1 spelling set-config has
|
||||
@@ -25,6 +30,113 @@ type fileRow struct {
|
||||
// differently.
|
||||
func TestServeConfigExistingFiles(t *testing.T) {
|
||||
testFileRows(t, []fileRow{
|
||||
// A string endpoint's scheme names both the listener and the back-end.
|
||||
{
|
||||
// The file from #18381: plaintext HTTP on port 443.
|
||||
name: "http_on_443",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "http://127.0.0.1:30123"}}}}`,
|
||||
means: []string{"--service=svc:foo --http=443 http://127.0.0.1:30123"},
|
||||
},
|
||||
{
|
||||
name: "http",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000"}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 http://localhost:3000"},
|
||||
},
|
||||
{
|
||||
name: "https",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://localhost:8443"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 https://localhost:8443"},
|
||||
},
|
||||
{
|
||||
name: "https_on_80",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "https://localhost:8443"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=80 https://localhost:8443"},
|
||||
},
|
||||
{
|
||||
name: "tcp",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
means: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://localhost:9481"}}}}`,
|
||||
means: []string{"--service=svc:foo --tls-terminated-tcp=443 tcp://localhost:9481"},
|
||||
},
|
||||
{
|
||||
name: "tun",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"*": "TUN"}}}}`,
|
||||
means: []string{"--service=svc:foo --tun"},
|
||||
},
|
||||
|
||||
// https+insecure and file are served over HTTPS, on any port.
|
||||
{
|
||||
name: "https_insecure",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "https+insecure://localhost:8443"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=80 https+insecure://localhost:8443"},
|
||||
},
|
||||
{
|
||||
name: "file",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "file://$DIR/index.html"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=80 $DIR/index.html"},
|
||||
},
|
||||
{
|
||||
name: "file_directory",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 $DIR"},
|
||||
},
|
||||
{
|
||||
name: "file_path_cleaned",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR/./index.html"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 $DIR/index.html"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR/index.html"}}}}`,
|
||||
},
|
||||
{
|
||||
// Not a path, so served as a proxy to the port. The export names the
|
||||
// HTTPS listener, which a string to that proxy cannot.
|
||||
name: "file_not_a_path",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://3000"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 3000"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://127.0.0.1:3000"}}}}}`,
|
||||
},
|
||||
|
||||
// Unix sockets: tailscaled speaks plain HTTP or TCP to the socket.
|
||||
{
|
||||
name: "http_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://unix:/run/app.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 unix:/run/app.sock"},
|
||||
},
|
||||
{
|
||||
name: "http_unix_relative",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://unix:relative.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 unix:relative.sock"},
|
||||
},
|
||||
{
|
||||
name: "https_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://unix:/run/app.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 unix:/run/app.sock"},
|
||||
},
|
||||
{
|
||||
name: "https_insecure_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https+insecure://unix:/run/app.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 unix:/run/app.sock"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://unix:/run/app.sock"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "tcp_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:22": "tcp://unix:/run/ssh.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --tcp=22 unix:/run/ssh.sock"},
|
||||
},
|
||||
{
|
||||
name: "tcp_unix_relative",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:22": "tcp://unix:relative.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --tcp=22 unix:relative.sock"},
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://unix:/run/app.sock"}}}}`,
|
||||
means: []string{"--service=svc:foo --tls-terminated-tcp=443 unix:/run/app.sock"},
|
||||
},
|
||||
|
||||
// Hosts.
|
||||
{
|
||||
name: "ipv6_unbracketed",
|
||||
@@ -54,6 +166,40 @@ func TestServeConfigExistingFiles(t *testing.T) {
|
||||
means: []string{"--service=svc:foo --tcp=5432 tcp://DB.Example.com:5432"},
|
||||
},
|
||||
|
||||
// Ports.
|
||||
{
|
||||
name: "port_range",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:8000-8002": "http://localhost:9000-9002"}}}}`,
|
||||
means: []string{
|
||||
"--service=svc:foo --http=8000 http://localhost:9000",
|
||||
"--service=svc:foo --http=8001 http://localhost:9001",
|
||||
"--service=svc:foo --http=8002 http://localhost:9002",
|
||||
},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {
|
||||
"tcp:8000": "http://localhost:9000",
|
||||
"tcp:8001": "http://localhost:9001",
|
||||
"tcp:8002": "http://localhost:9002"
|
||||
}}}}`,
|
||||
},
|
||||
{
|
||||
// A Unix socket has no ports, so every source port goes to it.
|
||||
name: "port_range_to_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:8000-8001": "tcp://unix:/run/app.sock"}}}}`,
|
||||
means: []string{
|
||||
"--service=svc:foo --tcp=8000 unix:/run/app.sock",
|
||||
"--service=svc:foo --tcp=8001 unix:/run/app.sock",
|
||||
},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {
|
||||
"tcp:8000": "tcp://unix:/run/app.sock",
|
||||
"tcp:8001": "tcp://unix:/run/app.sock"
|
||||
}}}}`,
|
||||
},
|
||||
{
|
||||
name: "max_port",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:65535": "http://localhost:8080"}}}}`,
|
||||
means: []string{"--service=svc:foo --http=65535 http://localhost:8080"},
|
||||
},
|
||||
|
||||
// Advertisement.
|
||||
{
|
||||
name: "advertised_false",
|
||||
@@ -89,6 +235,39 @@ func TestServeConfigExistingFiles(t *testing.T) {
|
||||
file: `{"version": "0.0.1"}`,
|
||||
},
|
||||
|
||||
// Several at once, and the HuJSON a hand-written file may use.
|
||||
{
|
||||
name: "several_services",
|
||||
file: `{"version": "0.0.1", "services": {
|
||||
"svc:foo": {"endpoints": {
|
||||
"tcp:80": "http://localhost:3000",
|
||||
"tcp:443": "https+insecure://localhost:8443",
|
||||
"tcp:5432": "tcp://localhost:5432",
|
||||
"tcp:9481": "tls-terminated-tcp://localhost:9481"
|
||||
}},
|
||||
"svc:bar": {"endpoints": {"*": "TUN"}, "advertised": false}
|
||||
}}`,
|
||||
means: []string{
|
||||
"--service=svc:foo --http=80 http://localhost:3000",
|
||||
"--service=svc:foo --https=443 https+insecure://localhost:8443",
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
"--service=svc:foo --tls-terminated-tcp=9481 tcp://localhost:9481",
|
||||
"--service=svc:bar --tun",
|
||||
"drain svc:bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "comments_and_trailing_commas",
|
||||
file: `{
|
||||
// A hand-written file.
|
||||
"version": "0.0.1",
|
||||
"services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432",},},},
|
||||
}`,
|
||||
means: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
hujson: true,
|
||||
},
|
||||
|
||||
// One Service.
|
||||
{
|
||||
name: "service",
|
||||
@@ -110,6 +289,120 @@ func TestServeConfigExistingFiles(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestServeConfigObjectFiles pins the object form of an endpoint, which names
|
||||
// the listener as front and writes the back-end in the string form. get-config
|
||||
// writes it only where the string would name a different listener.
|
||||
func TestServeConfigObjectFiles(t *testing.T) {
|
||||
testFileRows(t, []fileRow{
|
||||
{
|
||||
name: "https_to_http",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 http://localhost:3000"},
|
||||
},
|
||||
{
|
||||
name: "https_on_80_to_http",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=80 http://localhost:3000"},
|
||||
},
|
||||
{
|
||||
name: "http_to_https",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "https://localhost:8443"}}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 https://localhost:8443"},
|
||||
},
|
||||
{
|
||||
name: "http_to_https_insecure",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "https+insecure://localhost:8443"}}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 https+insecure://localhost:8443"},
|
||||
},
|
||||
{
|
||||
name: "http_to_file",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "file://$DIR/index.html"}}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 $DIR/index.html"},
|
||||
},
|
||||
{
|
||||
name: "port_range",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:8000-8001": {"front": "https", "backend": "http://localhost:9000-9001"}}}}}`,
|
||||
means: []string{
|
||||
"--service=svc:foo --https=8000 http://localhost:9000",
|
||||
"--service=svc:foo --https=8001 http://localhost:9001",
|
||||
},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {
|
||||
"tcp:8000": {"front": "https", "backend": "http://localhost:9000"},
|
||||
"tcp:8001": {"front": "https", "backend": "http://localhost:9001"}
|
||||
}}}}`,
|
||||
},
|
||||
{
|
||||
name: "beside_strings",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {
|
||||
"tcp:443": {"front": "https", "backend": "http://localhost:3000"},
|
||||
"tcp:80": "http://localhost:3000"
|
||||
}, "advertised": false}}}`,
|
||||
means: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:foo --http=80 http://localhost:3000",
|
||||
"drain svc:foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "service",
|
||||
flag: "--service=svc:foo",
|
||||
file: `{"version": "0.0.1", "endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 http://localhost:3000"},
|
||||
},
|
||||
|
||||
// An object where a string would have done is written back as the
|
||||
// string.
|
||||
{
|
||||
name: "https_to_https",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "https://localhost:8443"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 https://localhost:8443"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://localhost:8443"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "https_to_https_insecure",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "https+insecure://localhost:8443"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 https+insecure://localhost:8443"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https+insecure://localhost:8443"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "https_to_file",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "file://$DIR/index.html"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 $DIR/index.html"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR/index.html"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "http_to_http",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "http://localhost:3000"}}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 http://localhost:3000"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "https_to_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://unix:/run/app.sock"}}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 unix:/run/app.sock"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://unix:/run/app.sock"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "tcp",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": {"front": "tcp", "backend": "tcp://localhost:5432"}}}}}`,
|
||||
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: "tls_terminated_tcp",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "tls-terminated-tcp", "backend": "tcp://localhost:9481"}}}}}`,
|
||||
means: []string{"--service=svc:foo --tls-terminated-tcp=443 tcp://localhost:9481"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://localhost:9481"}}}}`,
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp_to_unix",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "tls-terminated-tcp", "backend": "tcp://unix:/run/app.sock"}}}}}`,
|
||||
means: []string{"--service=svc:foo --tls-terminated-tcp=443 unix:/run/app.sock"},
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://unix:/run/app.sock"}}}}`,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// testFileRows applies each row's file with set-config, checks it builds the
|
||||
// state its commands build, without warnings, and that get-config writes its
|
||||
// export, which set-config reads back as the same state.
|
||||
@@ -118,6 +411,9 @@ func testFileRows(t *testing.T, rows []fileRow) {
|
||||
for _, tt := range rows {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
skipOnWindows(t, tt.file)
|
||||
if tt.hujson && !buildfeatures.HasHuJSONConf {
|
||||
t.Skip("built without HuJSON support")
|
||||
}
|
||||
dir := serveTestDir(t)
|
||||
flag := cmp.Or(tt.flag, "--all")
|
||||
want := buildState(t, dir, tt.means)
|
||||
@@ -135,6 +431,173 @@ func testFileRows(t *testing.T, rows []fileRow) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeConfigRejectedFiles pins files set-config refuses, by the end of
|
||||
// its error, and checks the node is left as it was.
|
||||
func TestServeConfigRejectedFiles(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
flag string // --all if empty
|
||||
file string
|
||||
// The end of set-config's error. The JSON decoder words what comes
|
||||
// before it differently from run to run.
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "missing_version",
|
||||
file: `{"services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000"}}}}`,
|
||||
err: `config file looks like a Services configuration file but is missing the required "version" field`,
|
||||
},
|
||||
{
|
||||
name: "unknown_version",
|
||||
file: `{"version": "0.0.2", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000"}}}}`,
|
||||
err: `unsupported config file version "0.0.2"`,
|
||||
},
|
||||
{
|
||||
name: "not_an_endpoint",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": 80}}}}`,
|
||||
err: `unmarshal JSON number into Go string within "/services/svc:foo/endpoints/tcp:80"`,
|
||||
},
|
||||
|
||||
// Object form: front must be a listener.
|
||||
{
|
||||
name: "front_missing",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"backend": "http://localhost:3000"}}}}}`,
|
||||
err: `invalid front protocol ""`,
|
||||
},
|
||||
{
|
||||
name: "front_https_insecure",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https+insecure", "backend": "https://localhost:8443"}}}}}`,
|
||||
err: `invalid front protocol "https+insecure"`,
|
||||
},
|
||||
{
|
||||
name: "front_file",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "file", "backend": "file:///srv"}}}}}`,
|
||||
err: `invalid front protocol "file"`,
|
||||
},
|
||||
{
|
||||
name: "front_tun",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"*": {"front": "TUN", "backend": "TUN"}}}}}`,
|
||||
err: `invalid front protocol "TUN"`,
|
||||
},
|
||||
|
||||
// Object form: the back-end must be one the listener can reach.
|
||||
{
|
||||
name: "backend_missing",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https"}}}}}`,
|
||||
err: `handler not of form <proto>://<destination>`,
|
||||
},
|
||||
{
|
||||
name: "web_front_tcp_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "tcp://localhost:22"}}}}}`,
|
||||
err: `front protocol "https" cannot proxy to a "tcp" back-end`,
|
||||
},
|
||||
{
|
||||
name: "web_front_tls_terminated_tcp_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "http", "backend": "tls-terminated-tcp://localhost:22"}}}}}`,
|
||||
err: `front protocol "http" cannot proxy to a "tls-terminated-tcp" back-end`,
|
||||
},
|
||||
{
|
||||
name: "web_front_tun_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "TUN"}}}}}`,
|
||||
err: `front protocol "https" cannot proxy to a "TUN" back-end`,
|
||||
},
|
||||
{
|
||||
name: "tcp_front_web_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": {"front": "tcp", "backend": "http://localhost:5432"}}}}}`,
|
||||
err: `front protocol "tcp" cannot proxy to a "http" back-end`,
|
||||
},
|
||||
{
|
||||
name: "tcp_front_file_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": {"front": "tcp", "backend": "file:///srv"}}}}}`,
|
||||
err: `front protocol "tcp" cannot proxy to a "file" back-end`,
|
||||
},
|
||||
{
|
||||
// TLS is terminated at the listener; the back-end is plain TCP.
|
||||
name: "tls_terminated_tcp_front_tls_terminated_tcp_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "tls-terminated-tcp", "backend": "tls-terminated-tcp://localhost:9481"}}}}}`,
|
||||
err: `front protocol "tls-terminated-tcp" cannot proxy to a "tls-terminated-tcp" back-end`,
|
||||
},
|
||||
{
|
||||
// tailscaled speaks plain HTTP to a Unix socket.
|
||||
name: "unix_backend_over_https",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "https://unix:/run/app.sock"}}}}}`,
|
||||
err: `unix socket "unix:/run/app.sock" must use the "http" back-end protocol`,
|
||||
},
|
||||
{
|
||||
name: "unix_backend_over_https_insecure",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "https+insecure://unix:/run/app.sock"}}}}}`,
|
||||
err: `unix socket "unix:/run/app.sock" must use the "http" back-end protocol`,
|
||||
},
|
||||
{
|
||||
// A string's non-path file:// target is served as a proxy or
|
||||
// text; the object form refuses it.
|
||||
name: "file_backend_not_a_path",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "file://3000"}}}}}`,
|
||||
err: `file path "3000" is not absolute`,
|
||||
},
|
||||
{
|
||||
name: "file_backend_text",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "file://text:hello"}}}}}`,
|
||||
err: `file path "text:hello" is not absolute`,
|
||||
},
|
||||
{
|
||||
// A newer client may add members; this one must not drop them.
|
||||
name: "unknown_member",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": {"front": "tcp", "backend": "tcp://localhost:5432", "proxyProtocol": 1}}}}}`,
|
||||
err: `unknown object member name "proxyProtocol" within "/services/svc:foo/endpoints/tcp:5432"`,
|
||||
},
|
||||
{
|
||||
name: "single_service_object_incoherent",
|
||||
flag: "--service=svc:foo",
|
||||
file: `{"version": "0.0.1", "endpoints": {"tcp:443": {"front": "https", "backend": "tcp://localhost:22"}}}`,
|
||||
err: `front protocol "https" cannot proxy to a "tcp" back-end`,
|
||||
},
|
||||
|
||||
// What the CLI serves but the format has no way to say, in either
|
||||
// form. See TestServeConfigNotRepresentable for get-config's side.
|
||||
{
|
||||
name: "text",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "text:hello"}}}}`,
|
||||
err: `handler not of form <proto>://<destination>`,
|
||||
},
|
||||
{
|
||||
name: "text_backend",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "text:hello"}}}}}`,
|
||||
err: `handler not of form <proto>://<destination>`,
|
||||
},
|
||||
{
|
||||
name: "proxy_with_path",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000/api"}}}}`,
|
||||
err: `port range "3000/api": invalid first integer`,
|
||||
},
|
||||
{
|
||||
name: "backend_with_path",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000/api"}}}}}`,
|
||||
err: `port range "3000/api": invalid first integer`,
|
||||
},
|
||||
{
|
||||
name: "proxy_without_port",
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://example.com"}}}}`,
|
||||
err: `hostport must contain a colon (":")`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
dir := serveTestDir(t)
|
||||
flag := cmp.Or(tt.flag, "--all")
|
||||
path := writeTmpServeConfig(t, tt.file)
|
||||
node := buildState(t, dir, otherState)
|
||||
_, _, err := serveRun(node, "set-config", flag, path)
|
||||
prefix := fmt.Sprintf("could not read config from file %q: ", path)
|
||||
if err == nil || !strings.HasPrefix(err.Error(), prefix) || !strings.HasSuffix(err.Error(), tt.err) {
|
||||
t.Fatalf("set-config err = %v\nwant: %s...%s", err, prefix, tt.err)
|
||||
}
|
||||
assertSameState(t, buildState(t, dir, otherState), node)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeConfigNotRepresentable pins what the config file format cannot
|
||||
// say. The CLI builds each state, get-config refuses it with an error naming
|
||||
// the service, the port and what is lost, rather than writing a file without
|
||||
@@ -259,3 +722,43 @@ func TestServeConfigNotRepresentable(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestServeConfigUncleanPaths pins get-config on a path the CLI would have
|
||||
// cleaned, as serve status --json or set-raw can leave it: written as it is,
|
||||
// and read back cleaned.
|
||||
func TestServeConfigUncleanPaths(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
legacy string // a serve status --json document that builds the state
|
||||
export string // what get-config --all writes
|
||||
means []string // tailscale serve commands that build what export applies
|
||||
}{
|
||||
{
|
||||
name: "trailing_slash",
|
||||
legacy: `{"Services": {"svc:foo": {"TCP": {"443": {"HTTPS": true}}, "Web": {"foo.test.ts.net:443": {"Handlers": {"/": {"Path": "$DIR/"}}}}}}}`,
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR/"}}}}`,
|
||||
means: []string{"--service=svc:foo --https=443 $DIR"},
|
||||
},
|
||||
{
|
||||
name: "dot_object",
|
||||
legacy: `{"Services": {"svc:foo": {"TCP": {"80": {"HTTP": true}}, "Web": {"foo.test.ts.net:80": {"Handlers": {"/": {"Path": "$DIR/./index.html"}}}}}}}`,
|
||||
export: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "file://$DIR/./index.html"}}}}}`,
|
||||
means: []string{"--service=svc:foo --http=80 $DIR/index.html"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
skipOnWindows(t, tt.legacy)
|
||||
dir := serveTestDir(t)
|
||||
lc := newServiceHostClient()
|
||||
if _, _, err := serveRun(lc, "set-config", "--all", writeTmpServeConfig(t, expandDir(tt.legacy, dir))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
export := expandDir(tt.export, dir)
|
||||
checkJSON(t, "get-config --all", export, runServeCmd(t, lc, "get-config", "--all"))
|
||||
got := newServiceHostClient()
|
||||
setConfig(t, got, "--all", export)
|
||||
assertSameState(t, buildState(t, dir, tt.means), got)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,36 @@ func TestServeConfigMixed(t *testing.T) {
|
||||
want []string // tailscale serve commands that build the same state
|
||||
}{
|
||||
// A file, then the CLI changes it.
|
||||
{
|
||||
name: "file_then_cli_adds_port",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
},
|
||||
want: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "file_then_cli_replaces_backend",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://localhost:8443"}}}}`,
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
},
|
||||
want: []string{"--service=svc:foo --https=443 http://localhost:3000"},
|
||||
},
|
||||
{
|
||||
// The file from #18381, then the CLI turns the listener into HTTPS.
|
||||
// tailscaled refuses to change a port's serve type in place.
|
||||
name: "file_then_cli_replaces_listener",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "http://127.0.0.1:30123"}}}}`,
|
||||
"--service=svc:foo --http=443 off",
|
||||
"--service=svc:foo --https=443 http://127.0.0.1:30123",
|
||||
},
|
||||
want: []string{"--service=svc:foo --https=443 http://127.0.0.1:30123"},
|
||||
},
|
||||
{
|
||||
// The CLI can take a file-built service past what the format can
|
||||
// say; get-config then refuses it on both nodes alike.
|
||||
@@ -38,6 +68,49 @@ func TestServeConfigMixed(t *testing.T) {
|
||||
"--service=svc:foo --http=80 --set-path=/api http://localhost:4000",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "file_then_cli_turns_port_off",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000", "tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
"--service=svc:foo --tcp=5432 off",
|
||||
},
|
||||
want: []string{"--service=svc:foo --http=80 http://localhost:3000"},
|
||||
},
|
||||
{
|
||||
name: "file_then_drain",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
"drain svc:foo",
|
||||
},
|
||||
want: []string{
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
"drain svc:foo",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "file_drained_then_advertise",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false}}}`,
|
||||
"advertise svc:foo",
|
||||
},
|
||||
want: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
|
||||
},
|
||||
{
|
||||
name: "file_then_clear",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"*": "TUN"}}, "svc:bar": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
"clear svc:foo",
|
||||
},
|
||||
want: []string{"--service=svc:bar --tcp=5432 tcp://localhost:5432"},
|
||||
},
|
||||
{
|
||||
name: "file_applied_twice",
|
||||
steps: []string{
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
`set-config --all {"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
},
|
||||
want: []string{"--service=svc:foo --https=443 http://localhost:3000"},
|
||||
},
|
||||
|
||||
// The CLI, then a file replaces one service or all of them.
|
||||
{
|
||||
@@ -114,6 +187,25 @@ func TestServeConfigMixed(t *testing.T) {
|
||||
|
||||
// One node's export applied over another's state. See
|
||||
// TestServeConfigRoundTrip for --all.
|
||||
{
|
||||
name: "export_of_one_service_over_another_state",
|
||||
from: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
},
|
||||
steps: []string{
|
||||
"--service=svc:foo --http=80 text:hello",
|
||||
"--service=svc:bar --tcp=22 tcp://localhost:22",
|
||||
"drain svc:bar",
|
||||
"set-config --service=svc:foo $FROM",
|
||||
},
|
||||
want: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
"--service=svc:bar --tcp=22 tcp://localhost:22",
|
||||
"drain svc:bar",
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "export_of_drained_service_over_advertised",
|
||||
from: []string{
|
||||
|
||||
@@ -55,6 +55,180 @@ func TestServeConfigRoundTrip(t *testing.T) {
|
||||
services services
|
||||
advertised []string
|
||||
}{
|
||||
// Web. A string endpoint's scheme names both the listener and the
|
||||
// back-end, with https+insecure and file served over HTTPS. Where the
|
||||
// listener is not the one the string implies, the endpoint is an
|
||||
// object naming it.
|
||||
{
|
||||
name: "https_to_http",
|
||||
cli: []string{"--service=svc:foo --https=443 http://localhost:3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "https_to_https",
|
||||
cli: []string{"--service=svc:foo --https=443 https://localhost:8443"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://localhost:8443"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "https://localhost:8443"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "https_to_https_insecure",
|
||||
cli: []string{"--service=svc:foo --https=443 https+insecure://localhost:8443"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https+insecure://localhost:8443"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "https+insecure://localhost:8443"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "https_to_file",
|
||||
cli: []string{"--service=svc:foo --https=443 $DIR/index.html"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "file://$DIR/index.html"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Path: "$DIR/index.html"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "https_to_unix",
|
||||
cli: []string{"--service=svc:foo --https=443 unix:/run/app.sock"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "https://unix:/run/app.sock"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "unix:/run/app.sock"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_to_http",
|
||||
cli: []string{"--service=svc:foo --http=80 http://localhost:3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://localhost:3000"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_to_https",
|
||||
cli: []string{"--service=svc:foo --http=80 https://localhost:8443"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "https://localhost:8443"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "https://localhost:8443"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_to_https_insecure",
|
||||
cli: []string{"--service=svc:foo --http=80 https+insecure://localhost:8443"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "https+insecure://localhost:8443"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "https+insecure://localhost:8443"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_to_file",
|
||||
cli: []string{"--service=svc:foo --http=80 $DIR/index.html"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "http", "backend": "file://$DIR/index.html"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Path: "$DIR/index.html"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_to_unix",
|
||||
cli: []string{"--service=svc:foo --http=80 unix:/run/app.sock"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": "http://unix:/run/app.sock"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "unix:/run/app.sock"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
// The CLI's default: HTTPS on 443 to a local plain-HTTP port.
|
||||
name: "bare_port",
|
||||
cli: []string{"--service=svc:foo 3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://127.0.0.1:3000"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://127.0.0.1:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
// The listener is not implied by the port number.
|
||||
name: "https_on_80",
|
||||
cli: []string{"--service=svc:foo --https=80 http://localhost:3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:80": {"front": "https", "backend": "http://localhost:3000"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{80: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "http_on_443",
|
||||
cli: []string{"--service=svc:foo --http=443 http://localhost:3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "http://localhost:3000"}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTP: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
|
||||
// TCP forwarding.
|
||||
{
|
||||
name: "tcp",
|
||||
cli: []string{"--service=svc:foo --tcp=5432 tcp://localhost:5432"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}}}}`,
|
||||
services: services{"svc:foo": {TCP: tcp{5432: {TCPForward: "localhost:5432"}}}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "tcp_bare_port",
|
||||
cli: []string{"--service=svc:foo --tcp=5432 5432"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:5432": "tcp://127.0.0.1:5432"}}}}`,
|
||||
services: services{"svc:foo": {TCP: tcp{5432: {TCPForward: "127.0.0.1:5432"}}}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "tcp_to_unix",
|
||||
cli: []string{"--service=svc:foo --tcp=2222 unix:/run/ssh.sock"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:2222": "tcp://unix:/run/ssh.sock"}}}}`,
|
||||
services: services{"svc:foo": {TCP: tcp{2222: {TCPForward: "unix:/run/ssh.sock"}}}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp",
|
||||
cli: []string{"--service=svc:foo --tls-terminated-tcp=443 tcp://localhost:9481"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://localhost:9481"}}}}`,
|
||||
services: services{"svc:foo": {TCP: tcp{443: {TCPForward: "localhost:9481", TerminateTLS: "foo.test.ts.net"}}}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp_to_unix",
|
||||
cli: []string{"--service=svc:foo --tls-terminated-tcp=443 unix:/run/app.sock"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": "tls-terminated-tcp://unix:/run/app.sock"}}}}`,
|
||||
services: services{"svc:foo": {TCP: tcp{443: {TCPForward: "unix:/run/app.sock", TerminateTLS: "foo.test.ts.net"}}}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
|
||||
// Host spelling: IPv6 hosts are bracketed, and case is kept.
|
||||
{
|
||||
name: "tcp_ipv6",
|
||||
@@ -73,6 +247,16 @@ func TestServeConfigRoundTrip(t *testing.T) {
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "web_ipv6_object",
|
||||
cli: []string{"--service=svc:foo --https=443 http://[::1]:3000"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://[::1]:3000"}}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://[::1]:3000"}}}},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "mixed_case_host",
|
||||
cli: []string{"--service=svc:foo --http=80 http://App.example.com:3000"},
|
||||
@@ -84,6 +268,15 @@ func TestServeConfigRoundTrip(t *testing.T) {
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
|
||||
// TUN mode.
|
||||
{
|
||||
name: "tun",
|
||||
cli: []string{"--service=svc:foo --tun"},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {"*": "TUN"}}}}`,
|
||||
services: services{"svc:foo": {Tun: true}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
|
||||
// Advertisement.
|
||||
{
|
||||
name: "drained",
|
||||
@@ -110,6 +303,57 @@ func TestServeConfigRoundTrip(t *testing.T) {
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {}}}}`,
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
|
||||
// More than one endpoint or Service.
|
||||
{
|
||||
name: "multiple_ports",
|
||||
cli: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:foo --http=80 http://localhost:3000",
|
||||
"--service=svc:foo --https=8443 https://localhost:8443",
|
||||
"--service=svc:foo --tcp=5432 tcp://localhost:5432",
|
||||
},
|
||||
file: `{"version": "0.0.1", "services": {"svc:foo": {"endpoints": {
|
||||
"tcp:443": {"front": "https", "backend": "http://localhost:3000"},
|
||||
"tcp:80": "http://localhost:3000",
|
||||
"tcp:8443": "https://localhost:8443",
|
||||
"tcp:5432": "tcp://localhost:5432"
|
||||
}}}}`,
|
||||
services: services{"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}, 80: {HTTP: true}, 8443: {HTTPS: true}, 5432: {TCPForward: "localhost:5432"}},
|
||||
Web: web{
|
||||
"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}},
|
||||
"foo.test.ts.net:80": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}},
|
||||
"foo.test.ts.net:8443": {Handlers: handlers{"/": {Proxy: "https://localhost:8443"}}},
|
||||
},
|
||||
}},
|
||||
advertised: []string{"svc:foo"},
|
||||
},
|
||||
{
|
||||
name: "multiple_services",
|
||||
cli: []string{
|
||||
"--service=svc:foo --https=443 http://localhost:3000",
|
||||
"--service=svc:bar --tcp=5432 tcp://localhost:5432",
|
||||
"--service=svc:baz --tun",
|
||||
"drain svc:bar",
|
||||
"advertise svc:qux",
|
||||
},
|
||||
file: `{"version": "0.0.1", "services": {
|
||||
"svc:foo": {"endpoints": {"tcp:443": {"front": "https", "backend": "http://localhost:3000"}}},
|
||||
"svc:bar": {"endpoints": {"tcp:5432": "tcp://localhost:5432"}, "advertised": false},
|
||||
"svc:baz": {"endpoints": {"*": "TUN"}},
|
||||
"svc:qux": {"endpoints": {}}
|
||||
}}`,
|
||||
services: services{
|
||||
"svc:foo": {
|
||||
TCP: tcp{443: {HTTPS: true}},
|
||||
Web: web{"foo.test.ts.net:443": {Handlers: handlers{"/": {Proxy: "http://localhost:3000"}}}},
|
||||
},
|
||||
"svc:bar": {TCP: tcp{5432: {TCPForward: "localhost:5432"}}},
|
||||
"svc:baz": {Tun: true},
|
||||
},
|
||||
advertised: []string{"svc:baz", "svc:foo", "svc:qux"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
|
||||
@@ -758,8 +758,13 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
|
||||
if len(defaultHandler.AcceptAppCaps) > 0 {
|
||||
return nil, unrepresentable(port, "--accept-app-caps")
|
||||
}
|
||||
front := conffile.ProtoHTTP
|
||||
if config.HTTPS {
|
||||
front = conffile.ProtoHTTPS
|
||||
}
|
||||
if defaultHandler.Path != "" {
|
||||
mak.Set(&sdf.Endpoints, &ppr, &conffile.Target{
|
||||
Front: front,
|
||||
Protocol: conffile.ProtoFile,
|
||||
Destination: defaultHandler.Path,
|
||||
DestinationPorts: tailcfg.PortRange{},
|
||||
@@ -768,12 +773,8 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
|
||||
if strings.HasPrefix(defaultHandler.Proxy, "unix:") {
|
||||
// HTTP over unix socket: h.Proxy is "unix:/path" without "://".
|
||||
// The inbound protocol is HTTP(S); infer from useTLS.
|
||||
httpProto := conffile.ProtoHTTP
|
||||
if config.HTTPS {
|
||||
httpProto = conffile.ProtoHTTPS
|
||||
}
|
||||
mak.Set(&sdf.Endpoints, &ppr, &conffile.Target{
|
||||
Protocol: httpProto,
|
||||
Protocol: front,
|
||||
Destination: defaultHandler.Proxy,
|
||||
})
|
||||
} else {
|
||||
@@ -787,6 +788,7 @@ func (e *serveEnv) runServeGetConfig(ctx context.Context, args []string) (err er
|
||||
}
|
||||
|
||||
mak.Set(&sdf.Endpoints, &ppr, &conffile.Target{
|
||||
Front: front,
|
||||
Protocol: conffile.ServiceProtocol(proto),
|
||||
Destination: host,
|
||||
DestinationPorts: tailcfg.PortRange{First: uint16(destPort), Last: uint16(destPort)},
|
||||
@@ -964,7 +966,7 @@ func (e *serveEnv) runServeSetConfig(ctx context.Context, args []string) (err er
|
||||
if ppr.Proto != int(ipproto.TCP) {
|
||||
return fmt.Errorf("service %q: source ports must be TCP", name)
|
||||
}
|
||||
serveType, _ := serveTypeFromConfString(ep.Protocol)
|
||||
serveType, _ := serveTypeFromConfString(ep.Listener())
|
||||
for port := ppr.Ports.First; port <= ppr.Ports.Last; port++ {
|
||||
var target string
|
||||
if ep.Protocol == conffile.ProtoFile {
|
||||
|
||||
@@ -6,11 +6,13 @@
|
||||
package conffile
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
jsonv2 "github.com/go-json-experiment/json"
|
||||
@@ -56,6 +58,8 @@ type ServiceDetailsFile struct {
|
||||
// localhost).
|
||||
// For example, "tcp:443" -> "tcp://localhost:8000" is an endpoint definition
|
||||
// mapping traffic on the TCP port 443 of the Service to port 8080 on localhost.
|
||||
// "tcp:443" -> {"front": "https", "backend": "http://localhost:8000"}
|
||||
// instead terminates HTTPS on port 443 in front of it; see [Target].
|
||||
// The Proto in the key must be populated.
|
||||
// As a special case, if the only mapping provided is "*" -> "TUN", that
|
||||
// enables TUN/L3 mode, where packets are delivered to the Tailscale network
|
||||
@@ -83,7 +87,17 @@ const (
|
||||
|
||||
// Target is a destination for traffic to go to when it arrives at a Tailscale
|
||||
// Service host.
|
||||
//
|
||||
// In a file it is a string, "<proto>://<destination>", whose scheme names
|
||||
// both the listener and how to reach the destination. Where the listener is
|
||||
// not the one the string implies, such as HTTPS in front of a plain-HTTP
|
||||
// back-end, it is an object, {"front": "<listener>", "backend": "<string>"}.
|
||||
type Target struct {
|
||||
// Front is the listener's protocol, set only by the object form:
|
||||
// ProtoHTTP, ProtoHTTPS, ProtoTCP or ProtoTLSTerminatedTCP. If empty, the
|
||||
// listener is the one Protocol implies; see [Target.Listener].
|
||||
Front ServiceProtocol
|
||||
|
||||
// The protocol over which to communicate with the Destination.
|
||||
// Protocol == ProtoTUN is a special case, activating "TUN mode" where
|
||||
// packets are delivered to the Tailscale TUN interface and then manually
|
||||
@@ -105,6 +119,12 @@ type Target struct {
|
||||
DestinationPorts tailcfg.PortRange
|
||||
}
|
||||
|
||||
// targetObject is the object form of a [Target].
|
||||
type targetObject struct {
|
||||
Front ServiceProtocol `json:"front"`
|
||||
Backend string `json:"backend"`
|
||||
}
|
||||
|
||||
// UnmarshalJSON implements [jsonv1.Unmarshaler].
|
||||
func (t *Target) UnmarshalJSON(buf []byte) error {
|
||||
return jsonv2.Unmarshal(buf, t)
|
||||
@@ -112,11 +132,23 @@ func (t *Target) UnmarshalJSON(buf []byte) error {
|
||||
|
||||
// UnmarshalJSONFrom implements [jsonv2.UnmarshalerFrom].
|
||||
func (t *Target) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
|
||||
if dec.PeekKind() == '{' {
|
||||
var o targetObject
|
||||
// Fail on members a later format adds rather than drop them.
|
||||
if err := jsonv2.UnmarshalDecode(dec, &o, jsonv2.RejectUnknownMembers(true)); err != nil {
|
||||
return err
|
||||
}
|
||||
return t.parseObject(o)
|
||||
}
|
||||
var str string
|
||||
if err := jsonv2.UnmarshalDecode(dec, &str); err != nil {
|
||||
return err
|
||||
}
|
||||
return t.parseString(str)
|
||||
}
|
||||
|
||||
// parseString reads the string form.
|
||||
func (t *Target) parseString(str string) error {
|
||||
// The TUN case does not look like a standard <url>://<proto> arrangement,
|
||||
// so handled separately.
|
||||
if str == "TUN" {
|
||||
@@ -169,6 +201,69 @@ func (t *Target) UnmarshalJSONFrom(dec *jsontext.Decoder) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Listener returns the protocol t is served on: Front, or else the one the
|
||||
// string form implies, where https+insecure and file are served over HTTPS.
|
||||
func (t *Target) Listener() ServiceProtocol {
|
||||
if t.Front == "" && (t.Protocol == ProtoHTTPSInsecure || t.Protocol == ProtoFile) {
|
||||
return ProtoHTTPS
|
||||
}
|
||||
return cmp.Or(t.Front, t.Protocol)
|
||||
}
|
||||
|
||||
// parseObject reads the object form: backend in the string form, served
|
||||
// behind the listener front names.
|
||||
func (t *Target) parseObject(o targetObject) error {
|
||||
var b Target
|
||||
if err := b.parseString(o.Backend); err != nil {
|
||||
return err
|
||||
}
|
||||
switch o.Front {
|
||||
case ProtoHTTP, ProtoHTTPS:
|
||||
switch b.Protocol {
|
||||
case ProtoHTTP, ProtoHTTPS, ProtoHTTPSInsecure, ProtoFile:
|
||||
default:
|
||||
return fmt.Errorf("front protocol %q cannot proxy to a %q back-end", o.Front, b.Protocol)
|
||||
}
|
||||
// tailscaled speaks plain HTTP to a Unix socket.
|
||||
if strings.HasPrefix(b.Destination, "unix:") && b.Protocol != ProtoHTTP {
|
||||
return fmt.Errorf("unix socket %q must use the %q back-end protocol", b.Destination, ProtoHTTP)
|
||||
}
|
||||
// set-config serves anything else as a proxy or text target.
|
||||
if b.Protocol == ProtoFile && !filepath.IsAbs(b.Destination) {
|
||||
return fmt.Errorf("file path %q is not absolute", b.Destination)
|
||||
}
|
||||
case ProtoTCP, ProtoTLSTerminatedTCP:
|
||||
if b.Protocol != ProtoTCP {
|
||||
return fmt.Errorf("front protocol %q cannot proxy to a %q back-end", o.Front, b.Protocol)
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("invalid front protocol %q", o.Front)
|
||||
}
|
||||
*t = b
|
||||
t.Front = o.Front
|
||||
return nil
|
||||
}
|
||||
|
||||
// MarshalJSON writes t as the string form, or as the object form if Front
|
||||
// names a listener other than the one the string implies.
|
||||
func (t *Target) MarshalJSON() ([]byte, error) {
|
||||
s, err := t.MarshalText()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if implied := (Target{Protocol: t.Protocol}); t.Listener() == implied.Listener() {
|
||||
return jsonv2.Marshal(string(s))
|
||||
}
|
||||
o := targetObject{Front: t.Front, Backend: string(s)}
|
||||
// Never write an object the loader refuses.
|
||||
if err := new(Target).parseObject(o); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return jsonv2.Marshal(o)
|
||||
}
|
||||
|
||||
// MarshalText writes t's Protocol and destination in the string form, which
|
||||
// does not say Front; [Target.MarshalJSON] writes the endpoint.
|
||||
func (t *Target) MarshalText() ([]byte, error) {
|
||||
var out string
|
||||
switch t.Protocol {
|
||||
|
||||
@@ -6,8 +6,12 @@
|
||||
package conffile
|
||||
|
||||
import (
|
||||
"cmp"
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"tailscale.com/tailcfg"
|
||||
@@ -109,6 +113,305 @@ func TestTargetUnixSocketRoundtrip(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func port(p uint16) tailcfg.PortRange { return tailcfg.PortRange{First: p, Last: p} }
|
||||
|
||||
// TestTargetJSON pins how an endpoint reads into a Target and is written back.
|
||||
// A string names the listener by its scheme; an object names it as front, with
|
||||
// the back-end in the string form. A Target is written as the string unless
|
||||
// Front names a listener the string does not imply.
|
||||
func TestTargetJSON(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in string
|
||||
want Target
|
||||
out string // what MarshalJSON writes for want; in if empty
|
||||
}{
|
||||
// Strings.
|
||||
{
|
||||
name: "http",
|
||||
in: `"http://127.0.0.1:3000"`,
|
||||
want: Target{Protocol: ProtoHTTP, Destination: "127.0.0.1", DestinationPorts: port(3000)},
|
||||
},
|
||||
{
|
||||
name: "https",
|
||||
in: `"https://127.0.0.1:8443"`,
|
||||
want: Target{Protocol: ProtoHTTPS, Destination: "127.0.0.1", DestinationPorts: port(8443)},
|
||||
},
|
||||
{
|
||||
name: "https_insecure",
|
||||
in: `"https+insecure://127.0.0.1:8443"`,
|
||||
want: Target{Protocol: ProtoHTTPSInsecure, Destination: "127.0.0.1", DestinationPorts: port(8443)},
|
||||
},
|
||||
{
|
||||
name: "file",
|
||||
in: `"file:///var/www"`,
|
||||
want: Target{Protocol: ProtoFile, Destination: "/var/www"},
|
||||
},
|
||||
{
|
||||
name: "tcp",
|
||||
in: `"tcp://127.0.0.1:22"`,
|
||||
want: Target{Protocol: ProtoTCP, Destination: "127.0.0.1", DestinationPorts: port(22)},
|
||||
},
|
||||
{
|
||||
name: "tls_terminated_tcp",
|
||||
in: `"tls-terminated-tcp://127.0.0.1:22"`,
|
||||
want: Target{Protocol: ProtoTLSTerminatedTCP, Destination: "127.0.0.1", DestinationPorts: port(22)},
|
||||
},
|
||||
{
|
||||
name: "tun",
|
||||
in: `"TUN"`,
|
||||
want: Target{Protocol: ProtoTUN, DestinationPorts: tailcfg.PortRangeAny},
|
||||
},
|
||||
{
|
||||
name: "port_range",
|
||||
in: `"http://localhost:9000-9002"`,
|
||||
want: Target{Protocol: ProtoHTTP, Destination: "localhost", DestinationPorts: tailcfg.PortRange{First: 9000, Last: 9002}},
|
||||
},
|
||||
|
||||
// Hosts are kept as written, and IPv6 hosts are unbracketed in memory
|
||||
// as net.JoinHostPort expects.
|
||||
{
|
||||
name: "ipv6",
|
||||
in: `"tcp://[::1]:5432"`,
|
||||
want: Target{Protocol: ProtoTCP, Destination: "::1", DestinationPorts: port(5432)},
|
||||
},
|
||||
{
|
||||
name: "ipv6_unbracketed",
|
||||
in: `"tcp://::1:5432"`,
|
||||
want: Target{Protocol: ProtoTCP, Destination: "::1", DestinationPorts: port(5432)},
|
||||
out: `"tcp://[::1]:5432"`,
|
||||
},
|
||||
{
|
||||
name: "ipv6_zone",
|
||||
in: `"tcp://[fe80::1%eth0]:5432"`,
|
||||
want: Target{Protocol: ProtoTCP, Destination: "fe80::1%eth0", DestinationPorts: port(5432)},
|
||||
},
|
||||
{
|
||||
name: "host_case",
|
||||
in: `"http://App.Example.com:3000"`,
|
||||
want: Target{Protocol: ProtoHTTP, Destination: "App.Example.com", DestinationPorts: port(3000)},
|
||||
},
|
||||
|
||||
// Objects.
|
||||
{
|
||||
name: "object_https_to_http",
|
||||
in: `{"front":"https","backend":"http://127.0.0.1:3000"}`,
|
||||
want: Target{Front: ProtoHTTPS, Protocol: ProtoHTTP, Destination: "127.0.0.1", DestinationPorts: port(3000)},
|
||||
},
|
||||
{
|
||||
name: "object_http_to_https",
|
||||
in: `{"front":"http","backend":"https://127.0.0.1:8443"}`,
|
||||
want: Target{Front: ProtoHTTP, Protocol: ProtoHTTPS, Destination: "127.0.0.1", DestinationPorts: port(8443)},
|
||||
},
|
||||
{
|
||||
name: "object_http_to_https_insecure",
|
||||
in: `{"front":"http","backend":"https+insecure://127.0.0.1:8443"}`,
|
||||
want: Target{Front: ProtoHTTP, Protocol: ProtoHTTPSInsecure, Destination: "127.0.0.1", DestinationPorts: port(8443)},
|
||||
},
|
||||
{
|
||||
name: "object_http_to_file",
|
||||
in: `{"front":"http","backend":"file:///var/www"}`,
|
||||
want: Target{Front: ProtoHTTP, Protocol: ProtoFile, Destination: "/var/www"},
|
||||
},
|
||||
{
|
||||
name: "object_ipv6",
|
||||
in: `{"front":"https","backend":"http://[::1]:3000"}`,
|
||||
want: Target{Front: ProtoHTTPS, Protocol: ProtoHTTP, Destination: "::1", DestinationPorts: port(3000)},
|
||||
},
|
||||
{
|
||||
name: "object_https_to_unix",
|
||||
in: `{"front":"https","backend":"http://unix:/run/app.sock"}`,
|
||||
want: Target{Front: ProtoHTTPS, Protocol: ProtoHTTP, Destination: "unix:/run/app.sock"},
|
||||
},
|
||||
{
|
||||
name: "object_tls_terminated_tcp",
|
||||
in: `{"front":"tls-terminated-tcp","backend":"tcp://127.0.0.1:22"}`,
|
||||
want: Target{Front: ProtoTLSTerminatedTCP, Protocol: ProtoTCP, Destination: "127.0.0.1", DestinationPorts: port(22)},
|
||||
},
|
||||
{
|
||||
// The same Target as its string, so written as the string.
|
||||
name: "object_https_to_https",
|
||||
in: `{"front":"https","backend":"https://127.0.0.1:8443"}`,
|
||||
want: Target{Front: ProtoHTTPS, Protocol: ProtoHTTPS, Destination: "127.0.0.1", DestinationPorts: port(8443)},
|
||||
out: `"https://127.0.0.1:8443"`,
|
||||
},
|
||||
{
|
||||
name: "object_tcp",
|
||||
in: `{"front":"tcp","backend":"tcp://127.0.0.1:22"}`,
|
||||
want: Target{Front: ProtoTCP, Protocol: ProtoTCP, Destination: "127.0.0.1", DestinationPorts: port(22)},
|
||||
out: `"tcp://127.0.0.1:22"`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var got Target
|
||||
if err := got.UnmarshalJSON([]byte(tt.in)); err != nil {
|
||||
t.Fatalf("Unmarshal(%s): %v", tt.in, err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Errorf("Unmarshal(%s) = %+v; want %+v", tt.in, got, tt.want)
|
||||
}
|
||||
skipObjectFileOnWindows(t, tt.want)
|
||||
// get-config writes with the standard library.
|
||||
b, err := json.Marshal(&tt.want)
|
||||
if err != nil {
|
||||
t.Fatalf("Marshal(%+v): %v", tt.want, err)
|
||||
}
|
||||
if want := cmp.Or(tt.out, tt.in); string(b) != want {
|
||||
t.Errorf("Marshal(%+v) = %s; want %s", tt.want, b, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTargetJSONErrors pins endpoints the loader refuses, by the end of the
|
||||
// error.
|
||||
func TestTargetJSONErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
in string
|
||||
err string
|
||||
}{
|
||||
{`"bogus"`, `handler not of form <proto>://<destination>`},
|
||||
{`"gopher://127.0.0.1:70"`, `unsupported protocol`},
|
||||
{`"tcp://unix:"`, `unix socket path cannot be empty`},
|
||||
{`"http://127.0.0.1:3000/api"`, `port range "3000/api": invalid first integer`},
|
||||
|
||||
// front must be a listener.
|
||||
{`{"backend":"http://127.0.0.1:3000"}`, `invalid front protocol ""`},
|
||||
{`{"front":"https+insecure","backend":"https://127.0.0.1:8443"}`, `invalid front protocol "https+insecure"`},
|
||||
{`{"front":"file","backend":"file:///var/www"}`, `invalid front protocol "file"`},
|
||||
{`{"front":"TUN","backend":"TUN"}`, `invalid front protocol "TUN"`},
|
||||
|
||||
// backend must be one the listener can reach.
|
||||
{`{"front":"https"}`, `handler not of form <proto>://<destination>`},
|
||||
{`{"front":"https","backend":"tcp://127.0.0.1:22"}`, `front protocol "https" cannot proxy to a "tcp" back-end`},
|
||||
{`{"front":"http","backend":"tls-terminated-tcp://127.0.0.1:22"}`, `front protocol "http" cannot proxy to a "tls-terminated-tcp" back-end`},
|
||||
{`{"front":"https","backend":"TUN"}`, `front protocol "https" cannot proxy to a "TUN" back-end`},
|
||||
{`{"front":"tcp","backend":"http://127.0.0.1:80"}`, `front protocol "tcp" cannot proxy to a "http" back-end`},
|
||||
{`{"front":"tcp","backend":"file:///var/www"}`, `front protocol "tcp" cannot proxy to a "file" back-end`},
|
||||
{`{"front":"tls-terminated-tcp","backend":"tls-terminated-tcp://127.0.0.1:22"}`, `front protocol "tls-terminated-tcp" cannot proxy to a "tls-terminated-tcp" back-end`},
|
||||
{`{"front":"https","backend":"https://unix:/run/app.sock"}`, `unix socket "unix:/run/app.sock" must use the "http" back-end protocol`},
|
||||
{`{"front":"https","backend":"https+insecure://unix:/run/app.sock"}`, `unix socket "unix:/run/app.sock" must use the "http" back-end protocol`},
|
||||
{`{"front":"http","backend":"file://3000"}`, `file path "3000" is not absolute`},
|
||||
{`{"front":"https","backend":"file://text:hello"}`, `file path "text:hello" is not absolute`},
|
||||
{`{"front":"tcp","backend":"tcp://127.0.0.1:22","proxyProtocol":1}`, `unknown object member name "proxyProtocol"`},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.in, func(t *testing.T) {
|
||||
var got Target
|
||||
err := got.UnmarshalJSON([]byte(tt.in))
|
||||
if err == nil || !strings.HasSuffix(err.Error(), tt.err) {
|
||||
t.Errorf("Unmarshal(%s) err = %v; want ...%s", tt.in, err, tt.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTargetMarshalErrors pins Targets that neither form can say, which
|
||||
// MarshalJSON refuses rather than write a file the loader reads differently.
|
||||
func TestTargetMarshalErrors(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
target Target
|
||||
err string
|
||||
}{
|
||||
{
|
||||
name: "web_front_tcp_backend",
|
||||
target: Target{Front: ProtoHTTPS, Protocol: ProtoTCP, Destination: "127.0.0.1", DestinationPorts: port(22)},
|
||||
err: `front protocol "https" cannot proxy to a "tcp" back-end`,
|
||||
},
|
||||
{
|
||||
name: "unix_over_https",
|
||||
target: Target{Front: ProtoHTTP, Protocol: ProtoHTTPS, Destination: "unix:/run/app.sock"},
|
||||
err: `unix socket "unix:/run/app.sock" must use the "http" back-end protocol`,
|
||||
},
|
||||
{
|
||||
name: "relative_path",
|
||||
target: Target{Front: ProtoHTTP, Protocol: ProtoFile, Destination: "index.html"},
|
||||
err: `file path "index.html" is not absolute`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
b, err := json.Marshal(&tt.target)
|
||||
if err == nil || !strings.HasSuffix(err.Error(), tt.err) {
|
||||
t.Errorf("Marshal(%+v) = %s, %v; want error ...%s", tt.target, b, err, tt.err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTargetMarshal pins how MarshalJSON writes Targets the loader does not
|
||||
// build.
|
||||
func TestTargetMarshal(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
target Target
|
||||
want string
|
||||
}{
|
||||
{
|
||||
// DestinationPorts is unused in TUN mode.
|
||||
name: "tun_without_ports",
|
||||
target: Target{Protocol: ProtoTUN},
|
||||
want: `"TUN"`,
|
||||
},
|
||||
// A path is written as it is, even where the loader would clean it.
|
||||
{
|
||||
name: "unclean_path",
|
||||
target: Target{Front: ProtoHTTPS, Protocol: ProtoFile, Destination: "/srv/www/"},
|
||||
want: `"file:///srv/www/"`,
|
||||
},
|
||||
{
|
||||
name: "unclean_path_object",
|
||||
target: Target{Front: ProtoHTTP, Protocol: ProtoFile, Destination: "/srv/./index.html"},
|
||||
want: `{"front":"http","backend":"file:///srv/./index.html"}`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
skipObjectFileOnWindows(t, tt.target)
|
||||
b, err := json.Marshal(&tt.target)
|
||||
if err != nil || string(b) != tt.want {
|
||||
t.Errorf("Marshal(%+v) = %s, %v; want %s", tt.target, b, err, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestTargetListener pins the listener each Target is served on.
|
||||
func TestTargetListener(t *testing.T) {
|
||||
tests := []struct {
|
||||
target Target
|
||||
want ServiceProtocol
|
||||
}{
|
||||
{Target{Protocol: ProtoHTTP}, ProtoHTTP},
|
||||
{Target{Protocol: ProtoHTTPS}, ProtoHTTPS},
|
||||
{Target{Protocol: ProtoHTTPSInsecure}, ProtoHTTPS},
|
||||
{Target{Protocol: ProtoFile}, ProtoHTTPS},
|
||||
{Target{Protocol: ProtoTCP}, ProtoTCP},
|
||||
{Target{Protocol: ProtoTLSTerminatedTCP}, ProtoTLSTerminatedTCP},
|
||||
{Target{Protocol: ProtoTUN}, ProtoTUN},
|
||||
{Target{Front: ProtoHTTPS, Protocol: ProtoHTTP}, ProtoHTTPS},
|
||||
{Target{Front: ProtoHTTP, Protocol: ProtoFile}, ProtoHTTP},
|
||||
{Target{Front: ProtoTLSTerminatedTCP, Protocol: ProtoTCP}, ProtoTLSTerminatedTCP},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
if got := tt.target.Listener(); got != tt.want {
|
||||
t.Errorf("%+v.Listener() = %q; want %q", tt.target, got, tt.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// skipObjectFileOnWindows skips a Target serving a file behind a front, whose
|
||||
// path, written with forward slashes, is not absolute on Windows.
|
||||
func skipObjectFileOnWindows(t *testing.T, target Target) {
|
||||
t.Helper()
|
||||
if runtime.GOOS == "windows" && target.Front != "" && target.Protocol == ProtoFile {
|
||||
t.Skip("path is not absolute on Windows")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadServicesConfigNull(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
|
||||
Reference in new issue
Block a user