2020-09-17 19:25:34 +03:00
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2022-10-05 20:40:41 +03:00
|
|
|
"time"
|
2020-09-17 19:25:34 +03:00
|
|
|
|
|
|
|
"github.com/caddyserver/caddy/v2/caddytest"
|
|
|
|
)
|
|
|
|
|
2020-10-01 23:05:39 +03:00
|
|
|
func TestSRVReverseProxy(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
|
|
|
"listen": "localhost:2999"
|
|
|
|
},
|
2020-10-01 23:05:39 +03:00
|
|
|
"apps": {
|
2022-09-24 22:00:55 +03:00
|
|
|
"pki": {
|
2023-02-24 22:54:04 +03:00
|
|
|
"certificate_authorities": {
|
|
|
|
"local": {
|
|
|
|
"install_trust": false
|
2022-09-24 22:00:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2023-02-24 22:54:04 +03:00
|
|
|
"http": {
|
|
|
|
"grace_period": 1,
|
|
|
|
"servers": {
|
|
|
|
"srv0": {
|
|
|
|
"listen": [
|
|
|
|
":18080"
|
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
"handler": "reverse_proxy",
|
2023-04-10 23:08:40 +03:00
|
|
|
"dynamic_upstreams": {
|
|
|
|
"source": "srv",
|
|
|
|
"name": "srv.host.service.consul"
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-10-01 23:05:39 +03:00
|
|
|
]
|
2023-02-24 22:54:04 +03:00
|
|
|
}
|
|
|
|
}
|
2020-10-01 23:05:39 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
}
|
|
|
|
`, "json")
|
2020-10-01 23:05:39 +03:00
|
|
|
}
|
|
|
|
|
2020-10-13 19:35:20 +03:00
|
|
|
func TestDialWithPlaceholderUnix(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:17:48 +03:00
|
|
|
f, err := os.CreateTemp("", "*.sock")
|
2020-10-13 19:35:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create TempFile: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// a hack to get a file name within a valid path to use as socket
|
|
|
|
socketName := f.Name()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
|
|
|
|
server := http.Server{
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
w.Write([]byte("Hello, World!"))
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
unixListener, err := net.Listen("unix", socketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to listen on the socket: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go server.Serve(unixListener)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
server.Close()
|
|
|
|
})
|
|
|
|
runtime.Gosched() // Allow other goroutines to run
|
|
|
|
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
|
|
|
"listen": "localhost:2999"
|
|
|
|
},
|
2020-10-13 19:35:20 +03:00
|
|
|
"apps": {
|
2022-09-24 22:00:55 +03:00
|
|
|
"pki": {
|
2023-02-24 22:54:04 +03:00
|
|
|
"certificate_authorities": {
|
|
|
|
"local": {
|
|
|
|
"install_trust": false
|
|
|
|
}
|
2022-09-24 22:00:55 +03:00
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
},
|
|
|
|
"http": {
|
|
|
|
"grace_period": 1,
|
|
|
|
"servers": {
|
|
|
|
"srv0": {
|
|
|
|
"listen": [
|
|
|
|
":18080"
|
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
"handler": "reverse_proxy",
|
|
|
|
"upstreams": [
|
|
|
|
{
|
|
|
|
"dial": "unix/{http.request.header.X-Caddy-Upstream-Dial}"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-10-13 19:35:20 +03:00
|
|
|
]
|
2023-02-24 22:54:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-13 19:35:20 +03:00
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
}
|
2020-10-13 19:35:20 +03:00
|
|
|
`, "json")
|
|
|
|
|
2023-02-24 22:54:04 +03:00
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:18080", nil)
|
2020-10-13 19:35:20 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("X-Caddy-Upstream-Dial", socketName)
|
|
|
|
tester.AssertResponse(req, 200, "Hello, World!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReverseProxyWithPlaceholderDialAddress(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
|
|
|
"listen": "localhost:2999"
|
|
|
|
},
|
2020-10-13 19:35:20 +03:00
|
|
|
"apps": {
|
2022-09-24 22:00:55 +03:00
|
|
|
"pki": {
|
2023-02-24 22:54:04 +03:00
|
|
|
"certificate_authorities": {
|
|
|
|
"local": {
|
|
|
|
"install_trust": false
|
|
|
|
}
|
2022-09-24 22:00:55 +03:00
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
},
|
2020-10-13 19:35:20 +03:00
|
|
|
"http": {
|
2022-09-20 06:54:47 +03:00
|
|
|
"grace_period": 1,
|
2020-10-13 19:35:20 +03:00
|
|
|
"servers": {
|
|
|
|
"srv0": {
|
|
|
|
"listen": [
|
2023-02-24 22:54:04 +03:00
|
|
|
":18080"
|
2020-10-13 19:35:20 +03:00
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": [
|
|
|
|
{
|
|
|
|
"host": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
"handler": "static_response",
|
|
|
|
"body": "Hello, World!"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"terminal": true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"automatic_https": {
|
|
|
|
"skip": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"srv1": {
|
|
|
|
"listen": [
|
|
|
|
":9080"
|
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": [
|
|
|
|
{
|
|
|
|
"host": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
|
|
|
|
"handler": "reverse_proxy",
|
|
|
|
"upstreams": [
|
|
|
|
{
|
|
|
|
"dial": "{http.request.header.X-Caddy-Upstream-Dial}"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"terminal": true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"automatic_https": {
|
|
|
|
"skip": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
`, "json")
|
2020-10-13 19:35:20 +03:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:9080", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
return
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
req.Header.Set("X-Caddy-Upstream-Dial", "localhost:18080")
|
2020-10-13 19:35:20 +03:00
|
|
|
tester.AssertResponse(req, 200, "Hello, World!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReverseProxyWithPlaceholderTCPDialAddress(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
"admin": {
|
|
|
|
"listen": "localhost:2999"
|
|
|
|
},
|
2020-10-13 19:35:20 +03:00
|
|
|
"apps": {
|
2022-09-24 22:00:55 +03:00
|
|
|
"pki": {
|
2023-02-24 22:54:04 +03:00
|
|
|
"certificate_authorities": {
|
|
|
|
"local": {
|
|
|
|
"install_trust": false
|
|
|
|
}
|
2022-09-24 22:00:55 +03:00
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
},
|
2020-10-13 19:35:20 +03:00
|
|
|
"http": {
|
2022-09-20 06:54:47 +03:00
|
|
|
"grace_period": 1,
|
2020-10-13 19:35:20 +03:00
|
|
|
"servers": {
|
|
|
|
"srv0": {
|
|
|
|
"listen": [
|
2023-02-24 22:54:04 +03:00
|
|
|
":18080"
|
2020-10-13 19:35:20 +03:00
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": [
|
|
|
|
{
|
|
|
|
"host": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
"handler": "static_response",
|
|
|
|
"body": "Hello, World!"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"terminal": true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"automatic_https": {
|
|
|
|
"skip": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"srv1": {
|
|
|
|
"listen": [
|
|
|
|
":9080"
|
|
|
|
],
|
|
|
|
"routes": [
|
|
|
|
{
|
|
|
|
"match": [
|
|
|
|
{
|
|
|
|
"host": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"handle": [
|
|
|
|
{
|
|
|
|
|
|
|
|
"handler": "reverse_proxy",
|
|
|
|
"upstreams": [
|
|
|
|
{
|
2023-02-24 22:54:04 +03:00
|
|
|
"dial": "tcp/{http.request.header.X-Caddy-Upstream-Dial}:18080"
|
2020-10-13 19:35:20 +03:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"terminal": true
|
|
|
|
}
|
|
|
|
],
|
|
|
|
"automatic_https": {
|
|
|
|
"skip": [
|
|
|
|
"localhost"
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
`, "json")
|
2020-10-13 19:35:20 +03:00
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://localhost:9080", nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fail()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
req.Header.Set("X-Caddy-Upstream-Dial", "localhost")
|
|
|
|
tester.AssertResponse(req, 200, "Hello, World!")
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:25:34 +03:00
|
|
|
func TestReverseProxyHealthCheck(t *testing.T) {
|
|
|
|
tester := caddytest.NewTester(t)
|
|
|
|
tester.InitServer(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
2020-09-17 19:25:34 +03:00
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
2022-09-24 22:00:55 +03:00
|
|
|
grace_period 1ns
|
2020-09-17 19:25:34 +03:00
|
|
|
}
|
|
|
|
http://localhost:2020 {
|
|
|
|
respond "Hello, World!"
|
|
|
|
}
|
|
|
|
http://localhost:2021 {
|
|
|
|
respond "ok"
|
|
|
|
}
|
|
|
|
http://localhost:9080 {
|
|
|
|
reverse_proxy {
|
|
|
|
to localhost:2020
|
|
|
|
|
2021-08-17 19:51:26 +03:00
|
|
|
health_uri /health
|
2020-09-17 19:25:34 +03:00
|
|
|
health_port 2021
|
2022-10-13 20:30:57 +03:00
|
|
|
health_interval 10ms
|
|
|
|
health_timeout 100ms
|
2024-03-20 20:13:35 +03:00
|
|
|
health_passes 1
|
|
|
|
health_fails 1
|
2020-09-17 19:25:34 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-24 22:54:04 +03:00
|
|
|
`, "caddyfile")
|
2020-09-17 19:25:34 +03:00
|
|
|
|
2022-10-05 20:40:41 +03:00
|
|
|
time.Sleep(100 * time.Millisecond) // TODO: for some reason this test seems particularly flaky, getting 503 when it should be 200, unless we wait
|
2020-09-17 19:25:34 +03:00
|
|
|
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReverseProxyHealthCheckUnixSocket(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
tester := caddytest.NewTester(t)
|
2021-09-29 20:17:48 +03:00
|
|
|
f, err := os.CreateTemp("", "*.sock")
|
2020-09-17 19:25:34 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create TempFile: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// a hack to get a file name within a valid path to use as socket
|
|
|
|
socketName := f.Name()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
|
|
|
|
server := http.Server{
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/health") {
|
|
|
|
w.Write([]byte("ok"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write([]byte("Hello, World!"))
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
unixListener, err := net.Listen("unix", socketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to listen on the socket: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go server.Serve(unixListener)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
server.Close()
|
|
|
|
})
|
|
|
|
runtime.Gosched() // Allow other goroutines to run
|
|
|
|
|
|
|
|
tester.InitServer(fmt.Sprintf(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
2020-09-17 19:25:34 +03:00
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
2022-09-24 22:00:55 +03:00
|
|
|
grace_period 1ns
|
2020-09-17 19:25:34 +03:00
|
|
|
}
|
|
|
|
http://localhost:9080 {
|
|
|
|
reverse_proxy {
|
|
|
|
to unix/%s
|
|
|
|
|
2021-08-17 19:51:26 +03:00
|
|
|
health_uri /health
|
2020-09-17 19:25:34 +03:00
|
|
|
health_port 2021
|
|
|
|
health_interval 2s
|
|
|
|
health_timeout 5s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, socketName), "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
|
|
|
|
}
|
2021-07-07 06:43:45 +03:00
|
|
|
|
|
|
|
func TestReverseProxyHealthCheckUnixSocketWithoutPort(t *testing.T) {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.SkipNow()
|
|
|
|
}
|
|
|
|
tester := caddytest.NewTester(t)
|
2021-09-29 20:17:48 +03:00
|
|
|
f, err := os.CreateTemp("", "*.sock")
|
2021-07-07 06:43:45 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to create TempFile: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// a hack to get a file name within a valid path to use as socket
|
|
|
|
socketName := f.Name()
|
|
|
|
os.Remove(f.Name())
|
|
|
|
|
|
|
|
server := http.Server{
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
|
|
|
if strings.HasPrefix(req.URL.Path, "/health") {
|
|
|
|
w.Write([]byte("ok"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Write([]byte("Hello, World!"))
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
|
|
|
|
unixListener, err := net.Listen("unix", socketName)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to listen on the socket: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
go server.Serve(unixListener)
|
|
|
|
t.Cleanup(func() {
|
|
|
|
server.Close()
|
|
|
|
})
|
|
|
|
runtime.Gosched() // Allow other goroutines to run
|
|
|
|
|
|
|
|
tester.InitServer(fmt.Sprintf(`
|
|
|
|
{
|
2022-09-24 22:00:55 +03:00
|
|
|
skip_install_trust
|
|
|
|
admin localhost:2999
|
2021-07-07 06:43:45 +03:00
|
|
|
http_port 9080
|
|
|
|
https_port 9443
|
2022-09-24 22:00:55 +03:00
|
|
|
grace_period 1ns
|
2021-07-07 06:43:45 +03:00
|
|
|
}
|
|
|
|
http://localhost:9080 {
|
|
|
|
reverse_proxy {
|
|
|
|
to unix/%s
|
|
|
|
|
2021-08-17 19:51:26 +03:00
|
|
|
health_uri /health
|
2021-07-07 06:43:45 +03:00
|
|
|
health_interval 2s
|
|
|
|
health_timeout 5s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`, socketName), "caddyfile")
|
|
|
|
|
|
|
|
tester.AssertGetResponse("http://localhost:9080/", 200, "Hello, World!")
|
|
|
|
}
|