2019-04-30 22:19:19 +00:00
|
|
|
package structs
|
|
|
|
|
|
|
|
import (
|
2019-05-02 13:11:33 +00:00
|
|
|
"bytes"
|
2019-07-12 17:20:30 +00:00
|
|
|
"strings"
|
2019-04-30 22:19:19 +00:00
|
|
|
"testing"
|
2019-07-12 17:20:30 +00:00
|
|
|
"time"
|
2019-04-30 22:19:19 +00:00
|
|
|
|
2019-05-02 13:11:33 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2019-07-12 17:20:30 +00:00
|
|
|
"github.com/hashicorp/hcl"
|
2019-04-30 22:19:19 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
// TestDecodeConfigEntry is the 'structs' mirror image of
|
|
|
|
// command/config/write/config_write_test.go:TestParseConfigEntry
|
2019-04-30 22:19:19 +00:00
|
|
|
func TestDecodeConfigEntry(t *testing.T) {
|
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
camel string
|
|
|
|
snake string
|
|
|
|
expect ConfigEntry
|
|
|
|
expectErr string
|
|
|
|
}{
|
|
|
|
// TODO(rb): test json?
|
|
|
|
{
|
|
|
|
name: "proxy-defaults: extra fields or typo",
|
|
|
|
snake: `
|
|
|
|
kind = "proxy-defaults"
|
|
|
|
name = "main"
|
|
|
|
cornfig {
|
|
|
|
"foo" = 19
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "proxy-defaults"
|
|
|
|
Name = "main"
|
|
|
|
Cornfig {
|
|
|
|
"foo" = 19
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expectErr: `invalid config key "cornfig"`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "proxy-defaults",
|
|
|
|
snake: `
|
|
|
|
kind = "proxy-defaults"
|
|
|
|
name = "main"
|
|
|
|
config {
|
|
|
|
"foo" = 19
|
|
|
|
"bar" = "abc"
|
|
|
|
"moreconfig" {
|
|
|
|
"moar" = "config"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mesh_gateway {
|
|
|
|
mode = "remote"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "proxy-defaults"
|
|
|
|
Name = "main"
|
|
|
|
Config {
|
|
|
|
"foo" = 19
|
|
|
|
"bar" = "abc"
|
|
|
|
"moreconfig" {
|
|
|
|
"moar" = "config"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MeshGateway {
|
|
|
|
Mode = "remote"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expect: &ProxyConfigEntry{
|
|
|
|
Kind: "proxy-defaults",
|
|
|
|
Name: "main",
|
2019-04-30 22:19:19 +00:00
|
|
|
Config: map[string]interface{}{
|
2019-07-12 17:20:30 +00:00
|
|
|
"foo": 19,
|
|
|
|
"bar": "abc",
|
|
|
|
"moreconfig": map[string]interface{}{
|
|
|
|
"moar": "config",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
MeshGateway: MeshGatewayConfig{
|
|
|
|
Mode: MeshGatewayModeRemote,
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-23 01:04:15 +00:00
|
|
|
{
|
|
|
|
name: "namespaces invalid top level",
|
|
|
|
snake: `
|
|
|
|
kind = "terminating-gateway"
|
|
|
|
name = "terminating-gateway"
|
|
|
|
namespace = "foo"
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "terminating-gateway"
|
|
|
|
Name = "terminating-gateway"
|
|
|
|
Namespace = "foo"
|
|
|
|
`,
|
2020-06-23 16:57:30 +00:00
|
|
|
expectErr: `invalid config key "namespace", namespaces are a consul enterprise feature`,
|
2020-06-23 01:04:15 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "namespaces invalid deep",
|
|
|
|
snake: `
|
|
|
|
kind = "ingress-gateway"
|
|
|
|
name = "ingress-web"
|
|
|
|
listeners = [
|
|
|
|
{
|
|
|
|
port = 8080
|
|
|
|
protocol = "http"
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
name = "web"
|
|
|
|
hosts = ["test.example.com", "test2.example.com"]
|
|
|
|
namespace = "frontend"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "ingress-gateway"
|
|
|
|
Name = "ingress-web"
|
|
|
|
Namespace = "blah"
|
|
|
|
Listeners = [
|
|
|
|
{
|
|
|
|
Port = 8080
|
|
|
|
Protocol = "http"
|
|
|
|
Services = [
|
|
|
|
{
|
|
|
|
Name = "web"
|
|
|
|
Hosts = ["test.example.com", "test2.example.com"]
|
|
|
|
Namespace = "frontend"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
2020-06-23 16:57:30 +00:00
|
|
|
expectErr: `invalid config key "listeners[0].services[0].namespace", namespaces are a consul enterprise feature`,
|
2020-06-23 01:04:15 +00:00
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
{
|
|
|
|
name: "service-defaults",
|
|
|
|
snake: `
|
|
|
|
kind = "service-defaults"
|
|
|
|
name = "main"
|
|
|
|
protocol = "http"
|
2019-08-19 17:19:44 +00:00
|
|
|
external_sni = "abc-123"
|
2019-07-12 17:20:30 +00:00
|
|
|
mesh_gateway {
|
|
|
|
mode = "remote"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-defaults"
|
|
|
|
Name = "main"
|
|
|
|
Protocol = "http"
|
2019-08-19 17:19:44 +00:00
|
|
|
ExternalSNI = "abc-123"
|
2019-07-12 17:20:30 +00:00
|
|
|
MeshGateway {
|
|
|
|
Mode = "remote"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expect: &ServiceConfigEntry{
|
2019-08-19 17:19:44 +00:00
|
|
|
Kind: "service-defaults",
|
|
|
|
Name: "main",
|
|
|
|
Protocol: "http",
|
|
|
|
ExternalSNI: "abc-123",
|
2019-07-12 17:20:30 +00:00
|
|
|
MeshGateway: MeshGatewayConfig{
|
|
|
|
Mode: MeshGatewayModeRemote,
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service-router: kitchen sink",
|
|
|
|
snake: `
|
|
|
|
kind = "service-router"
|
|
|
|
name = "main"
|
|
|
|
routes = [
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
path_exact = "/foo"
|
|
|
|
header = [
|
|
|
|
{
|
|
|
|
name = "debug1"
|
|
|
|
present = true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "debug2"
|
|
|
|
present = false
|
|
|
|
invert = true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "debug3"
|
|
|
|
exact = "1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "debug4"
|
|
|
|
prefix = "aaa"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "debug5"
|
|
|
|
suffix = "bbb"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "debug6"
|
|
|
|
regex = "a.*z"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
destination {
|
|
|
|
service = "carrot"
|
|
|
|
service_subset = "kale"
|
|
|
|
namespace = "leek"
|
|
|
|
prefix_rewrite = "/alternate"
|
|
|
|
request_timeout = "99s"
|
|
|
|
num_retries = 12345
|
|
|
|
retry_on_connect_failure = true
|
|
|
|
retry_on_status_codes = [401, 209]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
path_prefix = "/foo"
|
2019-07-24 01:56:39 +00:00
|
|
|
methods = [ "GET", "DELETE" ]
|
2019-07-12 17:20:30 +00:00
|
|
|
query_param = [
|
|
|
|
{
|
|
|
|
name = "hack1"
|
2019-07-24 01:55:26 +00:00
|
|
|
present = true
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "hack2"
|
2019-07-24 01:55:26 +00:00
|
|
|
exact = "1"
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "hack3"
|
2019-07-24 01:55:26 +00:00
|
|
|
regex = "a.*z"
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
match {
|
|
|
|
http {
|
|
|
|
path_regex = "/foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-router"
|
|
|
|
Name = "main"
|
|
|
|
Routes = [
|
|
|
|
{
|
|
|
|
Match {
|
|
|
|
HTTP {
|
|
|
|
PathExact = "/foo"
|
|
|
|
Header = [
|
|
|
|
{
|
|
|
|
Name = "debug1"
|
|
|
|
Present = true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "debug2"
|
|
|
|
Present = false
|
|
|
|
Invert = true
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "debug3"
|
|
|
|
Exact = "1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "debug4"
|
|
|
|
Prefix = "aaa"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "debug5"
|
|
|
|
Suffix = "bbb"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "debug6"
|
|
|
|
Regex = "a.*z"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Destination {
|
|
|
|
Service = "carrot"
|
|
|
|
ServiceSubset = "kale"
|
|
|
|
Namespace = "leek"
|
|
|
|
PrefixRewrite = "/alternate"
|
|
|
|
RequestTimeout = "99s"
|
|
|
|
NumRetries = 12345
|
|
|
|
RetryOnConnectFailure = true
|
|
|
|
RetryOnStatusCodes = [401, 209]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Match {
|
|
|
|
HTTP {
|
|
|
|
PathPrefix = "/foo"
|
2019-07-24 01:56:39 +00:00
|
|
|
Methods = [ "GET", "DELETE" ]
|
2019-07-12 17:20:30 +00:00
|
|
|
QueryParam = [
|
|
|
|
{
|
|
|
|
Name = "hack1"
|
2019-07-24 01:55:26 +00:00
|
|
|
Present = true
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "hack2"
|
2019-07-24 01:55:26 +00:00
|
|
|
Exact = "1"
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "hack3"
|
2019-07-24 01:55:26 +00:00
|
|
|
Regex = "a.*z"
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Match {
|
|
|
|
HTTP {
|
|
|
|
PathRegex = "/foo"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
expect: &ServiceRouterConfigEntry{
|
|
|
|
Kind: "service-router",
|
|
|
|
Name: "main",
|
|
|
|
Routes: []ServiceRoute{
|
|
|
|
{
|
|
|
|
Match: &ServiceRouteMatch{
|
|
|
|
HTTP: &ServiceRouteHTTPMatch{
|
|
|
|
PathExact: "/foo",
|
|
|
|
Header: []ServiceRouteHTTPMatchHeader{
|
|
|
|
{
|
|
|
|
Name: "debug1",
|
|
|
|
Present: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug2",
|
|
|
|
Present: false,
|
|
|
|
Invert: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug3",
|
|
|
|
Exact: "1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug4",
|
|
|
|
Prefix: "aaa",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug5",
|
|
|
|
Suffix: "bbb",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "debug6",
|
|
|
|
Regex: "a.*z",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Destination: &ServiceRouteDestination{
|
|
|
|
Service: "carrot",
|
|
|
|
ServiceSubset: "kale",
|
|
|
|
Namespace: "leek",
|
|
|
|
PrefixRewrite: "/alternate",
|
|
|
|
RequestTimeout: 99 * time.Second,
|
|
|
|
NumRetries: 12345,
|
|
|
|
RetryOnConnectFailure: true,
|
|
|
|
RetryOnStatusCodes: []uint32{401, 209},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Match: &ServiceRouteMatch{
|
|
|
|
HTTP: &ServiceRouteHTTPMatch{
|
|
|
|
PathPrefix: "/foo",
|
2019-07-24 01:56:39 +00:00
|
|
|
Methods: []string{"GET", "DELETE"},
|
2019-07-12 17:20:30 +00:00
|
|
|
QueryParam: []ServiceRouteHTTPMatchQueryParam{
|
|
|
|
{
|
2019-07-24 01:55:26 +00:00
|
|
|
Name: "hack1",
|
|
|
|
Present: true,
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hack2",
|
2019-07-24 01:55:26 +00:00
|
|
|
Exact: "1",
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "hack3",
|
2019-07-24 01:55:26 +00:00
|
|
|
Regex: "a.*z",
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Match: &ServiceRouteMatch{
|
|
|
|
HTTP: &ServiceRouteHTTPMatch{
|
|
|
|
PathRegex: "/foo",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
{
|
|
|
|
name: "service-splitter: kitchen sink",
|
|
|
|
snake: `
|
|
|
|
kind = "service-splitter"
|
|
|
|
name = "main"
|
|
|
|
splits = [
|
|
|
|
{
|
|
|
|
weight = 99.1
|
|
|
|
service_subset = "v1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
weight = 0.9
|
|
|
|
service = "other"
|
|
|
|
namespace = "alt"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-splitter"
|
|
|
|
Name = "main"
|
|
|
|
Splits = [
|
|
|
|
{
|
|
|
|
Weight = 99.1
|
|
|
|
ServiceSubset = "v1"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Weight = 0.9
|
|
|
|
Service = "other"
|
|
|
|
Namespace = "alt"
|
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
expect: &ServiceSplitterConfigEntry{
|
|
|
|
Kind: ServiceSplitter,
|
|
|
|
Name: "main",
|
|
|
|
Splits: []ServiceSplit{
|
|
|
|
{
|
|
|
|
Weight: 99.1,
|
|
|
|
ServiceSubset: "v1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Weight: 0.9,
|
|
|
|
Service: "other",
|
|
|
|
Namespace: "alt",
|
|
|
|
},
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service-resolver: subsets with failover",
|
|
|
|
snake: `
|
|
|
|
kind = "service-resolver"
|
|
|
|
name = "main"
|
|
|
|
default_subset = "v1"
|
|
|
|
connect_timeout = "15s"
|
|
|
|
subsets = {
|
|
|
|
"v1" = {
|
|
|
|
filter = "Service.Meta.version == v1"
|
|
|
|
},
|
|
|
|
"v2" = {
|
|
|
|
filter = "Service.Meta.version == v2"
|
|
|
|
only_passing = true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
failover = {
|
|
|
|
"v2" = {
|
|
|
|
service = "failcopy"
|
|
|
|
service_subset = "sure"
|
|
|
|
namespace = "neighbor"
|
|
|
|
datacenters = ["dc5", "dc14"]
|
|
|
|
},
|
|
|
|
"*" = {
|
|
|
|
datacenters = ["dc7"]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-resolver"
|
|
|
|
Name = "main"
|
|
|
|
DefaultSubset = "v1"
|
|
|
|
ConnectTimeout = "15s"
|
|
|
|
Subsets = {
|
|
|
|
"v1" = {
|
|
|
|
Filter = "Service.Meta.version == v1"
|
|
|
|
},
|
|
|
|
"v2" = {
|
|
|
|
Filter = "Service.Meta.version == v2"
|
|
|
|
OnlyPassing = true
|
|
|
|
},
|
|
|
|
}
|
|
|
|
Failover = {
|
|
|
|
"v2" = {
|
|
|
|
Service = "failcopy"
|
|
|
|
ServiceSubset = "sure"
|
|
|
|
Namespace = "neighbor"
|
|
|
|
Datacenters = ["dc5", "dc14"]
|
|
|
|
},
|
|
|
|
"*" = {
|
|
|
|
Datacenters = ["dc7"]
|
|
|
|
}
|
|
|
|
}`,
|
|
|
|
expect: &ServiceResolverConfigEntry{
|
|
|
|
Kind: "service-resolver",
|
|
|
|
Name: "main",
|
|
|
|
DefaultSubset: "v1",
|
|
|
|
ConnectTimeout: 15 * time.Second,
|
|
|
|
Subsets: map[string]ServiceResolverSubset{
|
|
|
|
"v1": {
|
|
|
|
Filter: "Service.Meta.version == v1",
|
|
|
|
},
|
|
|
|
"v2": {
|
|
|
|
Filter: "Service.Meta.version == v2",
|
|
|
|
OnlyPassing: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Failover: map[string]ServiceResolverFailover{
|
|
|
|
"v2": {
|
2019-08-02 20:34:54 +00:00
|
|
|
Service: "failcopy",
|
|
|
|
ServiceSubset: "sure",
|
|
|
|
Namespace: "neighbor",
|
|
|
|
Datacenters: []string{"dc5", "dc14"},
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
"*": {
|
|
|
|
Datacenters: []string{"dc7"},
|
|
|
|
},
|
|
|
|
},
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
{
|
|
|
|
name: "service-resolver: redirect",
|
|
|
|
snake: `
|
|
|
|
kind = "service-resolver"
|
|
|
|
name = "main"
|
|
|
|
redirect {
|
|
|
|
service = "other"
|
|
|
|
service_subset = "backup"
|
|
|
|
namespace = "alt"
|
|
|
|
datacenter = "dc9"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-resolver"
|
|
|
|
Name = "main"
|
|
|
|
Redirect {
|
|
|
|
Service = "other"
|
|
|
|
ServiceSubset = "backup"
|
|
|
|
Namespace = "alt"
|
|
|
|
Datacenter = "dc9"
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
expect: &ServiceResolverConfigEntry{
|
|
|
|
Kind: "service-resolver",
|
|
|
|
Name: "main",
|
|
|
|
Redirect: &ServiceResolverRedirect{
|
|
|
|
Service: "other",
|
|
|
|
ServiceSubset: "backup",
|
|
|
|
Namespace: "alt",
|
|
|
|
Datacenter: "dc9",
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "service-resolver: default",
|
|
|
|
snake: `
|
|
|
|
kind = "service-resolver"
|
|
|
|
name = "main"
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "service-resolver"
|
|
|
|
Name = "main"
|
|
|
|
`,
|
|
|
|
expect: &ServiceResolverConfigEntry{
|
|
|
|
Kind: "service-resolver",
|
|
|
|
Name: "main",
|
2019-04-30 22:19:19 +00:00
|
|
|
},
|
|
|
|
},
|
2020-03-31 16:59:10 +00:00
|
|
|
{
|
|
|
|
name: "ingress-gateway: kitchen sink",
|
|
|
|
snake: `
|
|
|
|
kind = "ingress-gateway"
|
|
|
|
name = "ingress-web"
|
|
|
|
|
2020-04-27 23:36:20 +00:00
|
|
|
tls {
|
|
|
|
enabled = true
|
|
|
|
}
|
|
|
|
|
2020-03-31 16:59:10 +00:00
|
|
|
listeners = [
|
|
|
|
{
|
|
|
|
port = 8080
|
|
|
|
protocol = "http"
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
name = "web"
|
2020-04-23 15:06:19 +00:00
|
|
|
hosts = ["test.example.com", "test2.example.com"]
|
2020-03-31 16:59:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "db"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
port = 9999
|
|
|
|
protocol = "tcp"
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
name = "mysql"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
port = 2234
|
|
|
|
protocol = "tcp"
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
name = "postgres"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "ingress-gateway"
|
|
|
|
Name = "ingress-web"
|
2020-04-27 23:36:20 +00:00
|
|
|
TLS {
|
|
|
|
Enabled = true
|
|
|
|
}
|
2020-03-31 16:59:10 +00:00
|
|
|
Listeners = [
|
|
|
|
{
|
|
|
|
Port = 8080
|
|
|
|
Protocol = "http"
|
|
|
|
Services = [
|
|
|
|
{
|
|
|
|
Name = "web"
|
2020-04-23 15:06:19 +00:00
|
|
|
Hosts = ["test.example.com", "test2.example.com"]
|
2020-03-31 16:59:10 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "db"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port = 9999
|
|
|
|
Protocol = "tcp"
|
|
|
|
Services = [
|
|
|
|
{
|
|
|
|
Name = "mysql"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Port = 2234
|
|
|
|
Protocol = "tcp"
|
|
|
|
Services = [
|
|
|
|
{
|
|
|
|
Name = "postgres"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
expect: &IngressGatewayConfigEntry{
|
|
|
|
Kind: "ingress-gateway",
|
|
|
|
Name: "ingress-web",
|
2020-04-27 23:36:20 +00:00
|
|
|
TLS: GatewayTLSConfig{
|
|
|
|
Enabled: true,
|
|
|
|
},
|
2020-03-31 16:59:10 +00:00
|
|
|
Listeners: []IngressListener{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-03-31 16:59:10 +00:00
|
|
|
Port: 8080,
|
|
|
|
Protocol: "http",
|
|
|
|
Services: []IngressService{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-04-23 15:06:19 +00:00
|
|
|
Name: "web",
|
|
|
|
Hosts: []string{"test.example.com", "test2.example.com"},
|
2020-03-31 16:59:10 +00:00
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-03-31 16:59:10 +00:00
|
|
|
Name: "db",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-03-31 16:59:10 +00:00
|
|
|
Port: 9999,
|
|
|
|
Protocol: "tcp",
|
|
|
|
Services: []IngressService{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-03-31 16:59:10 +00:00
|
|
|
Name: "mysql",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-03-31 16:59:10 +00:00
|
|
|
Port: 2234,
|
|
|
|
Protocol: "tcp",
|
|
|
|
Services: []IngressService{
|
2020-06-16 17:19:31 +00:00
|
|
|
{
|
2020-04-23 14:33:33 +00:00
|
|
|
Name: "postgres",
|
2020-03-31 16:59:10 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-03-31 19:27:32 +00:00
|
|
|
{
|
|
|
|
name: "terminating-gateway: kitchen sink",
|
|
|
|
snake: `
|
|
|
|
kind = "terminating-gateway"
|
|
|
|
name = "terminating-gw-west"
|
|
|
|
services = [
|
|
|
|
{
|
|
|
|
name = "payments",
|
|
|
|
ca_file = "/etc/payments/ca.pem",
|
|
|
|
cert_file = "/etc/payments/cert.pem",
|
|
|
|
key_file = "/etc/payments/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
sni = "mydomain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name = "*",
|
|
|
|
ca_file = "/etc/all/ca.pem",
|
|
|
|
cert_file = "/etc/all/cert.pem",
|
|
|
|
key_file = "/etc/all/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
sni = "my-alt-domain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
camel: `
|
|
|
|
Kind = "terminating-gateway"
|
|
|
|
Name = "terminating-gw-west"
|
|
|
|
Services = [
|
|
|
|
{
|
|
|
|
Name = "payments",
|
|
|
|
CAFile = "/etc/payments/ca.pem",
|
|
|
|
CertFile = "/etc/payments/cert.pem",
|
|
|
|
KeyFile = "/etc/payments/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
SNI = "mydomain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name = "*",
|
|
|
|
CAFile = "/etc/all/ca.pem",
|
|
|
|
CertFile = "/etc/all/cert.pem",
|
|
|
|
KeyFile = "/etc/all/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
SNI = "my-alt-domain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
`,
|
|
|
|
expect: &TerminatingGatewayConfigEntry{
|
|
|
|
Kind: "terminating-gateway",
|
|
|
|
Name: "terminating-gw-west",
|
|
|
|
Services: []LinkedService{
|
|
|
|
{
|
|
|
|
Name: "payments",
|
|
|
|
CAFile: "/etc/payments/ca.pem",
|
|
|
|
CertFile: "/etc/payments/cert.pem",
|
|
|
|
KeyFile: "/etc/payments/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
SNI: "mydomain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "*",
|
|
|
|
CAFile: "/etc/all/ca.pem",
|
|
|
|
CertFile: "/etc/all/cert.pem",
|
|
|
|
KeyFile: "/etc/all/tls.key",
|
2020-04-27 22:25:37 +00:00
|
|
|
SNI: "my-alt-domain",
|
2020-03-31 19:27:32 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2019-07-12 17:20:30 +00:00
|
|
|
} {
|
|
|
|
tc := tc
|
2019-04-30 22:19:19 +00:00
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
testbody := func(t *testing.T, body string) {
|
|
|
|
t.Helper()
|
2019-04-30 22:19:19 +00:00
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
var raw map[string]interface{}
|
|
|
|
err := hcl.Decode(&raw, body)
|
|
|
|
require.NoError(t, err)
|
2019-04-30 22:19:19 +00:00
|
|
|
|
2019-07-12 17:20:30 +00:00
|
|
|
got, err := DecodeConfigEntry(raw)
|
|
|
|
if tc.expectErr != "" {
|
|
|
|
require.Nil(t, got)
|
2019-04-30 22:19:19 +00:00
|
|
|
require.Error(t, err)
|
2019-07-12 17:20:30 +00:00
|
|
|
requireContainsLower(t, err.Error(), tc.expectErr)
|
2019-04-30 22:19:19 +00:00
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
2019-07-12 17:20:30 +00:00
|
|
|
require.Equal(t, tc.expect, got)
|
2019-04-30 22:19:19 +00:00
|
|
|
}
|
2019-07-12 17:20:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run(tc.name+" (snake case)", func(t *testing.T) {
|
|
|
|
testbody(t, tc.snake)
|
|
|
|
})
|
|
|
|
t.Run(tc.name+" (camel case)", func(t *testing.T) {
|
|
|
|
testbody(t, tc.camel)
|
2019-04-30 22:19:19 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-05-02 13:11:33 +00:00
|
|
|
|
|
|
|
func TestServiceConfigResponse_MsgPack(t *testing.T) {
|
|
|
|
// TODO(banks) lib.MapWalker doesn't actually fix the map[interface{}] issue
|
|
|
|
// it claims to in docs yet. When it does uncomment those cases below.
|
|
|
|
a := ServiceConfigResponse{
|
|
|
|
ProxyConfig: map[string]interface{}{
|
|
|
|
"string": "foo",
|
|
|
|
// "map": map[string]interface{}{
|
|
|
|
// "baz": "bar",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
UpstreamConfigs: map[string]map[string]interface{}{
|
2020-06-16 17:19:31 +00:00
|
|
|
"a": {
|
2019-05-02 13:11:33 +00:00
|
|
|
"string": "aaaa",
|
|
|
|
// "map": map[string]interface{}{
|
|
|
|
// "baz": "aa",
|
|
|
|
// },
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"b": {
|
2019-05-02 13:11:33 +00:00
|
|
|
"string": "bbbb",
|
|
|
|
// "map": map[string]interface{}{
|
|
|
|
// "baz": "bb",
|
|
|
|
// },
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
|
|
|
// Encode as msgPack using a regular handle i.e. NOT one with RawAsString
|
|
|
|
// since our RPC codec doesn't use that.
|
2020-02-07 21:50:24 +00:00
|
|
|
enc := codec.NewEncoder(&buf, MsgpackHandle)
|
2019-05-02 13:11:33 +00:00
|
|
|
require.NoError(t, enc.Encode(&a))
|
|
|
|
|
|
|
|
var b ServiceConfigResponse
|
|
|
|
|
2020-02-07 21:50:24 +00:00
|
|
|
dec := codec.NewDecoder(&buf, MsgpackHandle)
|
2019-05-02 13:11:33 +00:00
|
|
|
require.NoError(t, dec.Decode(&b))
|
|
|
|
|
|
|
|
require.Equal(t, a, b)
|
|
|
|
}
|
2019-05-02 19:25:29 +00:00
|
|
|
|
|
|
|
func TestConfigEntryResponseMarshalling(t *testing.T) {
|
|
|
|
|
|
|
|
cases := map[string]ConfigEntryResponse{
|
2020-06-16 17:19:31 +00:00
|
|
|
"nil entry": {},
|
|
|
|
"proxy-default entry": {
|
2019-05-02 19:25:29 +00:00
|
|
|
Entry: &ProxyConfigEntry{
|
|
|
|
Kind: ProxyDefaults,
|
|
|
|
Name: ProxyConfigGlobal,
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service-default entry": {
|
2019-05-02 19:25:29 +00:00
|
|
|
Entry: &ServiceConfigEntry{
|
|
|
|
Kind: ServiceDefaults,
|
|
|
|
Name: "foo",
|
|
|
|
Protocol: "tcp",
|
|
|
|
// Connect: ConnectConfiguration{SideCarProxy: true},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tcase := range cases {
|
|
|
|
name := name
|
|
|
|
tcase := tcase
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
|
|
|
|
data, err := tcase.MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NotEmpty(t, data)
|
|
|
|
|
|
|
|
var resp ConfigEntryResponse
|
|
|
|
require.NoError(t, resp.UnmarshalBinary(data))
|
|
|
|
|
|
|
|
require.Equal(t, tcase, resp)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-07-12 17:20:30 +00:00
|
|
|
|
|
|
|
func requireContainsLower(t *testing.T, haystack, needle string) {
|
|
|
|
t.Helper()
|
|
|
|
require.Contains(t, strings.ToLower(haystack), strings.ToLower(needle))
|
|
|
|
}
|