2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-03-28 18:39:22 +00:00
|
|
|
|
2019-03-22 19:37:14 +00:00
|
|
|
package xds
|
|
|
|
|
|
|
|
import (
|
2019-04-29 16:27:57 +00:00
|
|
|
"bytes"
|
2020-07-09 22:04:51 +00:00
|
|
|
"path/filepath"
|
2019-03-22 19:37:14 +00:00
|
|
|
"testing"
|
2019-04-29 16:27:57 +00:00
|
|
|
"text/template"
|
2023-04-26 22:59:48 +00:00
|
|
|
"time"
|
2019-03-22 19:37:14 +00:00
|
|
|
|
2021-02-26 22:23:15 +00:00
|
|
|
envoy_cluster_v3 "github.com/envoyproxy/go-control-plane/envoy/config/cluster/v3"
|
2023-07-19 21:19:00 +00:00
|
|
|
envoy_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
|
|
|
|
envoy_tls_v3 "github.com/envoyproxy/go-control-plane/envoy/extensions/transport_sockets/tls/v3"
|
2020-12-23 17:50:28 +00:00
|
|
|
testinf "github.com/mitchellh/go-testing-interface"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/types/known/wrapperspb"
|
2020-12-23 17:50:28 +00:00
|
|
|
|
2019-03-22 19:37:14 +00:00
|
|
|
"github.com/hashicorp/consul/agent/proxycfg"
|
2019-07-02 13:43:35 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2019-03-22 19:37:14 +00:00
|
|
|
)
|
|
|
|
|
2023-08-17 18:43:21 +00:00
|
|
|
type mockCfgFetcher struct {
|
|
|
|
addressLan string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *mockCfgFetcher) AdvertiseAddrLAN() string {
|
|
|
|
return s.addressLan
|
|
|
|
}
|
|
|
|
|
2023-07-20 22:02:21 +00:00
|
|
|
func uint32ptr(i uint32) *uint32 {
|
|
|
|
return &i
|
|
|
|
}
|
|
|
|
|
2023-07-21 21:48:25 +00:00
|
|
|
func durationPtr(d time.Duration) *time.Duration {
|
|
|
|
return &d
|
|
|
|
}
|
|
|
|
|
2019-04-29 16:27:57 +00:00
|
|
|
type customClusterJSONOptions struct {
|
2021-02-22 21:00:15 +00:00
|
|
|
Name string
|
|
|
|
TLSContext string
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var customAppClusterJSONTpl = `{
|
2021-02-26 22:23:15 +00:00
|
|
|
"@type": "type.googleapis.com/envoy.config.cluster.v3.Cluster",
|
2019-04-29 16:27:57 +00:00
|
|
|
{{ if .TLSContext -}}
|
2021-02-22 21:00:15 +00:00
|
|
|
"transport_socket": {
|
|
|
|
"name": "tls",
|
|
|
|
"typed_config": {
|
2021-02-26 22:23:15 +00:00
|
|
|
"@type": "type.googleapis.com/envoy.extensions.transport_sockets.tls.v3.UpstreamTlsContext",
|
2021-02-22 21:00:15 +00:00
|
|
|
{{ .TLSContext }}
|
|
|
|
}
|
|
|
|
},
|
2019-04-29 16:27:57 +00:00
|
|
|
{{- end }}
|
|
|
|
"name": "{{ .Name }}",
|
2019-08-22 20:11:56 +00:00
|
|
|
"connectTimeout": "15s",
|
2021-02-26 22:23:15 +00:00
|
|
|
"loadAssignment": {
|
|
|
|
"clusterName": "{{ .Name }}",
|
|
|
|
"endpoints": [
|
|
|
|
{
|
|
|
|
"lbEndpoints": [
|
|
|
|
{
|
|
|
|
"endpoint": {
|
|
|
|
"address": {
|
|
|
|
"socketAddress": {
|
|
|
|
"address": "127.0.0.1",
|
|
|
|
"portValue": 8080
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2019-04-29 16:27:57 +00:00
|
|
|
}
|
2021-02-26 22:23:15 +00:00
|
|
|
]
|
|
|
|
}
|
2019-04-29 16:27:57 +00:00
|
|
|
}`
|
|
|
|
|
|
|
|
var customAppClusterJSONTemplate = template.Must(template.New("").Parse(customAppClusterJSONTpl))
|
|
|
|
|
2022-03-07 17:47:14 +00:00
|
|
|
func customAppClusterJSON(t testinf.T, opts customClusterJSONOptions) string {
|
2019-04-29 16:27:57 +00:00
|
|
|
t.Helper()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := customAppClusterJSONTemplate.Execute(&buf, opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf.String()
|
|
|
|
}
|
2020-04-16 21:00:48 +00:00
|
|
|
|
2023-07-20 22:02:21 +00:00
|
|
|
var customClusterJSONTpl = `{
|
|
|
|
"@type": "type.googleapis.com/envoy.config.cluster.v3.Cluster",
|
|
|
|
"name": "{{ .Name }}",
|
|
|
|
"connectTimeout": "15s",
|
|
|
|
"loadAssignment": {
|
|
|
|
"clusterName": "{{ .Name }}",
|
|
|
|
"endpoints": [
|
|
|
|
{
|
|
|
|
"lbEndpoints": [
|
|
|
|
{
|
|
|
|
"endpoint": {
|
|
|
|
"address": {
|
|
|
|
"socketAddress": {
|
|
|
|
"address": "1.2.3.4",
|
|
|
|
"portValue": 8443
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}`
|
|
|
|
|
|
|
|
var customClusterJSONTemplate = template.Must(template.New("").Parse(customClusterJSONTpl))
|
|
|
|
|
|
|
|
func customClusterJSON(t testinf.T, opts customClusterJSONOptions) string {
|
|
|
|
t.Helper()
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err := customClusterJSONTemplate.Execute(&buf, opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return buf.String()
|
|
|
|
}
|
|
|
|
|
2020-09-02 21:13:50 +00:00
|
|
|
func TestEnvoyLBConfig_InjectToCluster(t *testing.T) {
|
|
|
|
var tests = []struct {
|
|
|
|
name string
|
2020-09-11 15:21:43 +00:00
|
|
|
lb *structs.LoadBalancer
|
2021-02-26 22:23:15 +00:00
|
|
|
expected *envoy_cluster_v3.Cluster
|
2020-09-02 21:13:50 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "skip empty",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: "",
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "round robin",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyRoundRobin,
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{LbPolicy: envoy_cluster_v3.Cluster_ROUND_ROBIN},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "random",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyRandom,
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{LbPolicy: envoy_cluster_v3.Cluster_RANDOM},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "maglev",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyMaglev,
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{LbPolicy: envoy_cluster_v3.Cluster_MAGLEV},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "ring_hash",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: structs.LBPolicyRingHash,
|
|
|
|
RingHashConfig: &structs.RingHashConfig{
|
|
|
|
MinimumRingSize: 3,
|
|
|
|
MaximumRingSize: 7,
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{
|
|
|
|
LbPolicy: envoy_cluster_v3.Cluster_RING_HASH,
|
|
|
|
LbConfig: &envoy_cluster_v3.Cluster_RingHashLbConfig_{
|
|
|
|
RingHashLbConfig: &envoy_cluster_v3.Cluster_RingHashLbConfig{
|
2023-01-11 14:39:10 +00:00
|
|
|
MinimumRingSize: &wrapperspb.UInt64Value{Value: 3},
|
|
|
|
MaximumRingSize: &wrapperspb.UInt64Value{Value: 7},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "least_request",
|
2020-09-11 15:21:43 +00:00
|
|
|
lb: &structs.LoadBalancer{
|
2020-09-02 21:13:50 +00:00
|
|
|
Policy: "least_request",
|
|
|
|
LeastRequestConfig: &structs.LeastRequestConfig{
|
|
|
|
ChoiceCount: 3,
|
|
|
|
},
|
|
|
|
},
|
2021-02-26 22:23:15 +00:00
|
|
|
expected: &envoy_cluster_v3.Cluster{
|
|
|
|
LbPolicy: envoy_cluster_v3.Cluster_LEAST_REQUEST,
|
|
|
|
LbConfig: &envoy_cluster_v3.Cluster_LeastRequestLbConfig_{
|
|
|
|
LeastRequestLbConfig: &envoy_cluster_v3.Cluster_LeastRequestLbConfig{
|
2023-01-11 14:39:10 +00:00
|
|
|
ChoiceCount: &wrapperspb.UInt32Value{Value: 3},
|
2020-09-02 21:13:50 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range tests {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2021-02-26 22:23:15 +00:00
|
|
|
var c envoy_cluster_v3.Cluster
|
2020-09-02 21:13:50 +00:00
|
|
|
err := injectLBToCluster(tc.lb, &c)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-02-22 21:00:15 +00:00
|
|
|
require.Equal(t, tc.expected, &c)
|
2020-09-02 21:13:50 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 16:12:04 +00:00
|
|
|
|
2023-06-29 20:37:40 +00:00
|
|
|
func TestMakeJWTProviderCluster(t *testing.T) {
|
|
|
|
// All tests here depend on golden files located under: agent/xds/testdata/jwt_authn_cluster/*
|
|
|
|
tests := map[string]struct {
|
|
|
|
provider *structs.JWTProviderConfigEntry
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
"remote-jwks-not-configured": {
|
|
|
|
provider: &structs.JWTProviderConfigEntry{
|
|
|
|
Kind: "jwt-provider",
|
|
|
|
Name: "okta",
|
|
|
|
JSONWebKeySet: &structs.JSONWebKeySet{},
|
|
|
|
},
|
|
|
|
expectedError: "cannot create JWKS cluster for non remote JWKS. Provider Name: okta",
|
|
|
|
},
|
|
|
|
"local-jwks-configured": {
|
|
|
|
provider: &structs.JWTProviderConfigEntry{
|
|
|
|
Kind: "jwt-provider",
|
|
|
|
Name: "okta",
|
|
|
|
JSONWebKeySet: &structs.JSONWebKeySet{
|
|
|
|
Local: &structs.LocalJWKS{
|
|
|
|
Filename: "filename",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedError: "cannot create JWKS cluster for non remote JWKS. Provider Name: okta",
|
|
|
|
},
|
|
|
|
"https-provider-with-hostname-no-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("https://example-okta.com/.well-known/jwks.json"),
|
|
|
|
},
|
|
|
|
"http-provider-with-hostname-no-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("http://example-okta.com/.well-known/jwks.json"),
|
|
|
|
},
|
|
|
|
"https-provider-with-hostname-and-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("https://example-okta.com:90/.well-known/jwks.json"),
|
|
|
|
},
|
|
|
|
"http-provider-with-hostname-and-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("http://example-okta.com:90/.well-known/jwks.json"),
|
|
|
|
},
|
|
|
|
"https-provider-with-ip-no-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("https://127.0.0.1"),
|
|
|
|
},
|
|
|
|
"http-provider-with-ip-no-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("http://127.0.0.1"),
|
|
|
|
},
|
|
|
|
"https-provider-with-ip-and-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("https://127.0.0.1:9091"),
|
|
|
|
},
|
|
|
|
"http-provider-with-ip-and-port": {
|
|
|
|
provider: makeTestProviderWithJWKS("http://127.0.0.1:9091"),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
cluster, err := makeJWTProviderCluster(tt.provider)
|
|
|
|
if tt.expectedError != "" {
|
|
|
|
require.Error(t, err, tt.expectedError)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
gotJSON := protoToJSON(t, cluster)
|
|
|
|
require.JSONEq(t, goldenSimple(t, filepath.Join("jwt_authn_clusters", name), gotJSON), gotJSON)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeTestProviderWithJWKS(uri string) *structs.JWTProviderConfigEntry {
|
|
|
|
return &structs.JWTProviderConfigEntry{
|
|
|
|
Kind: "jwt-provider",
|
|
|
|
Name: "okta",
|
|
|
|
Issuer: "test-issuer",
|
|
|
|
JSONWebKeySet: &structs.JSONWebKeySet{
|
|
|
|
Remote: &structs.RemoteJWKS{
|
|
|
|
RequestTimeoutMs: 1000,
|
|
|
|
FetchAsynchronously: true,
|
|
|
|
URI: uri,
|
2023-07-04 13:12:06 +00:00
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: structs.DiscoveryTypeStatic,
|
|
|
|
ConnectTimeout: time.Duration(5) * time.Second,
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
TrustedCA: &structs.JWKSTLSCertTrustedCA{
|
|
|
|
Filename: "mycert.crt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeJWKSDiscoveryClusterType(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
remoteJWKS *structs.RemoteJWKS
|
|
|
|
expectedClusterType *envoy_cluster_v3.Cluster_Type
|
|
|
|
}{
|
|
|
|
"nil remote jwks": {
|
|
|
|
remoteJWKS: nil,
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{},
|
|
|
|
},
|
|
|
|
"nil jwks cluster": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{},
|
|
|
|
},
|
|
|
|
"jwks cluster defaults to Strict DNS": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_STRICT_DNS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jwks with cluster EDS": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: "EDS",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_EDS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jwks with static dns": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: "STATIC",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_STATIC,
|
2023-06-29 20:37:40 +00:00
|
|
|
},
|
|
|
|
},
|
2023-07-04 13:12:06 +00:00
|
|
|
|
|
|
|
"jwks with original dst": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: "ORIGINAL_DST",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_ORIGINAL_DST,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jwks with strict dns": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: "STRICT_DNS",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_STRICT_DNS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"jwks with logical dns": {
|
|
|
|
remoteJWKS: &structs.RemoteJWKS{
|
|
|
|
JWKSCluster: &structs.JWKSCluster{
|
|
|
|
DiscoveryType: "LOGICAL_DNS",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expectedClusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_LOGICAL_DNS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
clusterType := makeJWKSDiscoveryClusterType(tt.remoteJWKS)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedClusterType, clusterType)
|
|
|
|
})
|
2023-06-29 20:37:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 15:37:36 +00:00
|
|
|
func TestMakeJWKSClusterDNSLookupFamilyType(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
clusterType *envoy_cluster_v3.Cluster_Type
|
|
|
|
expectedDNSLookupFamily envoy_cluster_v3.Cluster_DnsLookupFamily
|
|
|
|
}{
|
|
|
|
// strict dns and logical dns are the only ones that are different
|
|
|
|
"jwks with strict dns": {
|
|
|
|
clusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_STRICT_DNS,
|
|
|
|
},
|
|
|
|
expectedDNSLookupFamily: envoy_cluster_v3.Cluster_V4_PREFERRED,
|
|
|
|
},
|
|
|
|
"jwks with logical dns": {
|
|
|
|
clusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_LOGICAL_DNS,
|
|
|
|
},
|
|
|
|
expectedDNSLookupFamily: envoy_cluster_v3.Cluster_ALL,
|
|
|
|
},
|
|
|
|
// all should be auto from here down
|
|
|
|
"jwks with cluster EDS": {
|
|
|
|
clusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_EDS,
|
|
|
|
},
|
|
|
|
expectedDNSLookupFamily: envoy_cluster_v3.Cluster_AUTO,
|
|
|
|
},
|
|
|
|
"jwks with static dns": {
|
|
|
|
clusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_STATIC,
|
|
|
|
},
|
|
|
|
expectedDNSLookupFamily: envoy_cluster_v3.Cluster_AUTO,
|
|
|
|
},
|
|
|
|
|
|
|
|
"jwks with original dst": {
|
|
|
|
clusterType: &envoy_cluster_v3.Cluster_Type{
|
|
|
|
Type: envoy_cluster_v3.Cluster_ORIGINAL_DST,
|
|
|
|
},
|
|
|
|
expectedDNSLookupFamily: envoy_cluster_v3.Cluster_AUTO,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
actualDNSLookupFamily := makeJWKSClusterDNSLookupFamilyType(tt.clusterType)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expectedDNSLookupFamily, actualDNSLookupFamily)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-29 20:37:40 +00:00
|
|
|
func TestParseJWTRemoteURL(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
uri string
|
|
|
|
expectedHost string
|
|
|
|
expectedPort int
|
|
|
|
expectedScheme string
|
|
|
|
expectError bool
|
|
|
|
}{
|
|
|
|
"invalid-url": {
|
|
|
|
uri: ".com",
|
|
|
|
expectError: true,
|
|
|
|
},
|
|
|
|
"https-hostname-no-port": {
|
|
|
|
uri: "https://test.test.com",
|
|
|
|
expectedHost: "test.test.com",
|
|
|
|
expectedPort: 443,
|
|
|
|
expectedScheme: "https",
|
|
|
|
},
|
|
|
|
"https-hostname-with-port": {
|
|
|
|
uri: "https://test.test.com:4545",
|
|
|
|
expectedHost: "test.test.com",
|
|
|
|
expectedPort: 4545,
|
|
|
|
expectedScheme: "https",
|
|
|
|
},
|
|
|
|
"https-hostname-with-port-and-path": {
|
|
|
|
uri: "https://test.test.com:4545/test",
|
|
|
|
expectedHost: "test.test.com",
|
|
|
|
expectedPort: 4545,
|
|
|
|
expectedScheme: "https",
|
|
|
|
},
|
|
|
|
"http-hostname-no-port": {
|
|
|
|
uri: "http://test.test.com",
|
|
|
|
expectedHost: "test.test.com",
|
|
|
|
expectedPort: 80,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
"http-hostname-with-port": {
|
|
|
|
uri: "http://test.test.com:4636",
|
|
|
|
expectedHost: "test.test.com",
|
|
|
|
expectedPort: 4636,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
"https-ip-no-port": {
|
|
|
|
uri: "https://127.0.0.1",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 443,
|
|
|
|
expectedScheme: "https",
|
|
|
|
},
|
|
|
|
"https-ip-with-port": {
|
|
|
|
uri: "https://127.0.0.1:3434",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 3434,
|
|
|
|
expectedScheme: "https",
|
|
|
|
},
|
|
|
|
"http-ip-no-port": {
|
|
|
|
uri: "http://127.0.0.1",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 80,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
"http-ip-with-port": {
|
|
|
|
uri: "http://127.0.0.1:9190",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 9190,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
"http-ip-with-port-and-path": {
|
|
|
|
uri: "http://127.0.0.1:9190/some/where",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 9190,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
"http-ip-no-port-with-path": {
|
|
|
|
uri: "http://127.0.0.1/test/path",
|
|
|
|
expectedHost: "127.0.0.1",
|
|
|
|
expectedPort: 80,
|
|
|
|
expectedScheme: "http",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
host, scheme, port, err := parseJWTRemoteURL(tt.uri)
|
|
|
|
if tt.expectError {
|
|
|
|
require.Error(t, err)
|
|
|
|
} else {
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, host, tt.expectedHost)
|
|
|
|
require.Equal(t, scheme, tt.expectedScheme)
|
|
|
|
require.Equal(t, port, tt.expectedPort)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-20 16:12:04 +00:00
|
|
|
// UID is just a convenience function to aid in writing tests less verbosely.
|
|
|
|
func UID(input string) proxycfg.UpstreamID {
|
|
|
|
return proxycfg.UpstreamIDFromString(input)
|
|
|
|
}
|
2023-07-19 21:19:00 +00:00
|
|
|
|
|
|
|
func TestMakeJWTCertValidationContext(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
jwksCluster *structs.JWKSCluster
|
|
|
|
expected *envoy_tls_v3.CertificateValidationContext
|
|
|
|
}{
|
|
|
|
"when nil": {
|
|
|
|
jwksCluster: nil,
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{},
|
|
|
|
},
|
|
|
|
"when trustedCA with filename": {
|
|
|
|
jwksCluster: &structs.JWKSCluster{
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
TrustedCA: &structs.JWKSTLSCertTrustedCA{
|
|
|
|
Filename: "file.crt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{
|
|
|
|
TrustedCa: &envoy_core_v3.DataSource{
|
|
|
|
Specifier: &envoy_core_v3.DataSource_Filename{
|
|
|
|
Filename: "file.crt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"when trustedCA with environment variable": {
|
|
|
|
jwksCluster: &structs.JWKSCluster{
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
TrustedCA: &structs.JWKSTLSCertTrustedCA{
|
|
|
|
EnvironmentVariable: "MY_ENV",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{
|
|
|
|
TrustedCa: &envoy_core_v3.DataSource{
|
|
|
|
Specifier: &envoy_core_v3.DataSource_EnvironmentVariable{
|
|
|
|
EnvironmentVariable: "MY_ENV",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"when trustedCA with inline string": {
|
|
|
|
jwksCluster: &structs.JWKSCluster{
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
TrustedCA: &structs.JWKSTLSCertTrustedCA{
|
|
|
|
InlineString: "<my ca cert>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{
|
|
|
|
TrustedCa: &envoy_core_v3.DataSource{
|
|
|
|
Specifier: &envoy_core_v3.DataSource_InlineString{
|
|
|
|
InlineString: "<my ca cert>",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"when trustedCA with inline bytes": {
|
|
|
|
jwksCluster: &structs.JWKSCluster{
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
TrustedCA: &structs.JWKSTLSCertTrustedCA{
|
|
|
|
InlineBytes: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{
|
|
|
|
TrustedCa: &envoy_core_v3.DataSource{
|
|
|
|
Specifier: &envoy_core_v3.DataSource_InlineBytes{
|
|
|
|
InlineBytes: []byte{1, 2, 3},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"when caCertificateProviderInstance": {
|
|
|
|
jwksCluster: &structs.JWKSCluster{
|
|
|
|
TLSCertificates: &structs.JWKSTLSCertificate{
|
|
|
|
CaCertificateProviderInstance: &structs.JWKSTLSCertProviderInstance{
|
|
|
|
InstanceName: "<my-instance-name>",
|
|
|
|
CertificateName: "<my-cert>.crt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
expected: &envoy_tls_v3.CertificateValidationContext{
|
|
|
|
CaCertificateProviderInstance: &envoy_tls_v3.CertificateProviderPluginInstance{
|
|
|
|
InstanceName: "<my-instance-name>",
|
|
|
|
CertificateName: "<my-cert>.crt",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
tt := tt
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
vc := makeJWTCertValidationContext(tt.jwksCluster)
|
|
|
|
|
|
|
|
require.Equal(t, tt.expected, vc)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|