* caddytls: restore SNI conn-policy fast-path, keep first-match order * caddytls: test SNI fast-path with provisioned matchers, add benchmark Build the fast-path test policies through ConnectionPolicies.Provision so the SNI matchers are the *MatchServerName pointers used at handshake time, and add a benchmark for exact-SNI lookups across 100/1000/10000 policies.
583 lines
19 KiB
Go
583 lines
19 KiB
Go
// Copyright 2015 Matthew Holt and The Caddy Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package caddytls
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"encoding/json"
|
|
"fmt"
|
|
"reflect"
|
|
"testing"
|
|
|
|
"github.com/caddyserver/caddy/v2"
|
|
"github.com/caddyserver/caddy/v2/caddyconfig/caddyfile"
|
|
)
|
|
|
|
func TestConnectionPolicyIDNSNIMatcherFastPath(t *testing.T) {
|
|
ctx, cancel := caddy.NewContext(caddy.Context{Context: context.Background()})
|
|
defer cancel()
|
|
|
|
targetTLSConfig := &tls.Config{ClientAuth: tls.RequireAnyClientCert}
|
|
policies := ConnectionPolicies{
|
|
{
|
|
matchers: []ConnectionMatcher{MatchServerName{"つ.Localhost"}},
|
|
TLSConfig: targetTLSConfig,
|
|
},
|
|
}
|
|
|
|
const sniFastPathThreshold = 30
|
|
for i := len(policies); i < sniFastPathThreshold; i++ {
|
|
policies = append(policies, &ConnectionPolicy{
|
|
matchers: []ConnectionMatcher{MatchServerName{fmt.Sprintf("example-%d.localhost", i)}},
|
|
TLSConfig: &tls.Config{},
|
|
})
|
|
}
|
|
policies = append(policies, &ConnectionPolicy{
|
|
matchers: []ConnectionMatcher{MatchServerName{"xn--k9j.localhost"}},
|
|
TLSConfig: &tls.Config{ClientAuth: tls.NoClientCert},
|
|
})
|
|
|
|
tlsConfig := policies.TLSConfig(ctx)
|
|
got, err := tlsConfig.GetConfigForClient(&tls.ClientHelloInfo{ServerName: "XN--K9J.LOCALHOST"})
|
|
if err != nil {
|
|
t.Fatalf("GetConfigForClient() error = %v", err)
|
|
}
|
|
if got != targetTLSConfig {
|
|
t.Fatalf("expected Unicode IDN policy to match before later punycode policy")
|
|
}
|
|
}
|
|
|
|
// sniPolicy returns an unprovisioned connection policy that matches the
|
|
// given server names, configured the same way as one decoded from JSON.
|
|
func sniPolicy(names ...string) *ConnectionPolicy {
|
|
raw, err := json.Marshal(names)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return &ConnectionPolicy{MatchersRaw: caddy.ModuleMap{"sni": raw}}
|
|
}
|
|
|
|
func TestConnectionPolicySNIFastPathKeepsFirstMatch(t *testing.T) {
|
|
ctx, err := caddy.ProvisionContext(&caddy.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// each case puts a policy ahead of the exact-SNI policy for
|
|
// "site.localhost"; with more than 30 provisioned policies the SNI
|
|
// fast-path index is used, and it must still select the same policy
|
|
// a linear scan would
|
|
for i, tc := range []struct {
|
|
first *ConnectionPolicy
|
|
wantFirst bool
|
|
}{
|
|
{
|
|
// catch-all drop ahead of the exact-SNI policy
|
|
first: &ConnectionPolicy{
|
|
MatchersRaw: caddy.ModuleMap{"remote_ip": json.RawMessage(`{"ranges":["0.0.0.0/0","::/0"]}`)},
|
|
Drop: true,
|
|
},
|
|
wantFirst: true,
|
|
},
|
|
{
|
|
// wildcard-SNI policy ahead of the exact-SNI policy
|
|
first: sniPolicy("*.localhost"),
|
|
wantFirst: true,
|
|
},
|
|
{
|
|
// placeholder-SNI policy ahead of the exact-SNI policy
|
|
first: sniPolicy("{env.CADDY_TEST_UNSET}site.localhost"),
|
|
wantFirst: true,
|
|
},
|
|
{
|
|
// unrelated exact-SNI policy ahead of the exact-SNI policy
|
|
first: sniPolicy("other.localhost"),
|
|
wantFirst: false,
|
|
},
|
|
} {
|
|
policies := ConnectionPolicies{tc.first}
|
|
for j := range 30 {
|
|
policies = append(policies, sniPolicy(fmt.Sprintf("example-%d.localhost", j)))
|
|
}
|
|
site := sniPolicy("site.localhost")
|
|
policies = append(policies, site, &ConnectionPolicy{})
|
|
if err := policies.Provision(ctx); err != nil {
|
|
t.Fatalf("Test %d: provisioning policies: %v", i, err)
|
|
}
|
|
if _, ok := site.matchers[0].(*MatchServerName); !ok {
|
|
t.Fatalf("Test %d: expected provisioned SNI matcher to be *MatchServerName, got %T", i, site.matchers[0])
|
|
}
|
|
|
|
hello := &tls.ClientHelloInfo{
|
|
ServerName: "site.localhost",
|
|
Conn: testConn{addr: testAddr("198.51.100.1:443")},
|
|
}
|
|
got, err := policies.TLSConfig(ctx).GetConfigForClient(hello)
|
|
switch {
|
|
case tc.first.Drop:
|
|
if err == nil {
|
|
t.Errorf("Test %d: expected the first policy to drop the connection, got a config", i)
|
|
}
|
|
case err != nil:
|
|
t.Errorf("Test %d: unexpected error: %v", i, err)
|
|
case tc.wantFirst && got != tc.first.TLSConfig:
|
|
t.Errorf("Test %d: indexed lookup skipped the earlier matching policy", i)
|
|
case !tc.wantFirst && got != site.TLSConfig:
|
|
t.Errorf("Test %d: indexed lookup did not select the exact-SNI policy", i)
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkConnectionPolicySNIFastPath(b *testing.B) {
|
|
ctx, err := caddy.ProvisionContext(&caddy.Config{})
|
|
if err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
|
|
for _, n := range []int{100, 1000, 10000} {
|
|
b.Run(fmt.Sprintf("policies=%d", n), func(b *testing.B) {
|
|
// one exact-SNI policy per site followed by a catch-all, which
|
|
// is the shape the Caddyfile adapter produces for many sites
|
|
policies := make(ConnectionPolicies, 0, n+1)
|
|
for i := range n {
|
|
policies = append(policies, sniPolicy(fmt.Sprintf("site-%d.localhost", i)))
|
|
}
|
|
policies = append(policies, &ConnectionPolicy{})
|
|
if err := policies.Provision(ctx); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
getConfigForClient := policies.TLSConfig(ctx).GetConfigForClient
|
|
|
|
// the last site is the worst case for a linear scan
|
|
hello := &tls.ClientHelloInfo{ServerName: fmt.Sprintf("site-%d.localhost", n-1)}
|
|
want := policies[n-1].TLSConfig
|
|
|
|
b.ReportAllocs()
|
|
for b.Loop() {
|
|
got, err := getConfigForClient(hello)
|
|
if err != nil || got != want {
|
|
b.Fatalf("selected wrong policy: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClientAuthenticationUnmarshalCaddyfileWithDirectiveName(t *testing.T) {
|
|
const test_der_1 = `MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==`
|
|
const test_cert_file_1 = "../../caddytest/caddy.ca.cer"
|
|
type args struct {
|
|
d *caddyfile.Dispenser
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
args args
|
|
expected ClientAuthentication
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "empty client_auth block does not error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(
|
|
`client_auth {
|
|
}`,
|
|
),
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "providing both 'trust_pool' and 'trusted_ca_cert' returns an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(
|
|
`client_auth {
|
|
trust_pool inline MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
|
|
trusted_ca_cert MIIDSzCCAjOgAwIBAgIUfIRObjWNUA4jxQ/0x8BOCvE2Vw4wDQYJKoZIhvcNAQELBQAwFjEUMBIGA1UEAwwLRWFzeS1SU0EgQ0EwHhcNMTkwODI4MTYyNTU5WhcNMjkwODI1MTYyNTU5WjAWMRQwEgYDVQQDDAtFYXN5LVJTQSBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK5m5elxhQfMp/3aVJ4JnpN9PUSz6LlP6LePAPFU7gqohVVFVtDkChJAG3FNkNQNlieVTja/bgH9IcC6oKbROwdY1h0MvNV8AHHigvl03WuJD8g2ReVFXXwsnrPmKXCFzQyMI6TYk3m2gYrXsZOU1GLnfMRC3KAMRgE2F45twOs9hqG169YJ6mM2eQjzjCHWI6S2/iUYvYxRkCOlYUbLsMD/AhgAf1plzg6LPqNxtdlwxZnA0ytgkmhK67HtzJu0+ovUCsMv0RwcMhsEo9T8nyFAGt9XLZ63X5WpBCTUApaAUhnG0XnerjmUWb6eUWw4zev54sEfY5F3x002iQaW6cECAwEAAaOBkDCBjTAdBgNVHQ4EFgQU4CBUbZsS2GaNIkGRz/cBsD5ivjswUQYDVR0jBEowSIAU4CBUbZsS2GaNIkGRz/cBsD5ivjuhGqQYMBYxFDASBgNVBAMMC0Vhc3ktUlNBIENBghR8hE5uNY1QDiPFD/THwE4K8TZXDjAMBgNVHRMEBTADAQH/MAsGA1UdDwQEAwIBBjANBgkqhkiG9w0BAQsFAAOCAQEAKB3V4HIzoiO/Ch6WMj9bLJ2FGbpkMrcb/Eq01hT5zcfKD66lVS1MlK+cRL446Z2b2KDP1oFyVs+qmrmtdwrWgD+nfe2sBmmIHo9m9KygMkEOfG3MghGTEcS+0cTKEcoHYWYyOqQh6jnedXY8Cdm4GM1hAc9MiL3/sqV8YCVSLNnkoNysmr06/rZ0MCUZPGUtRmfd0heWhrfzAKw2HLgX+RAmpOE2MZqWcjvqKGyaRiaZks4nJkP6521aC2Lgp0HhCz1j8/uQ5ldoDszCnu/iro0NAsNtudTMD+YoLQxLqdleIh6CW+illc2VdXwj7mn6J04yns9jfE2jRjW/yTLFuQ==
|
|
}`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "trust_pool without a module argument returns an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(
|
|
`client_auth {
|
|
trust_pool
|
|
}`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "providing more than 1 mode produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
mode require request
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "not providing 'mode' argument produces an error",
|
|
args: args{d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
mode
|
|
}
|
|
`)},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "providing a single 'mode' argument sets the mode",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
mode require
|
|
}
|
|
`),
|
|
},
|
|
expected: ClientAuthentication{
|
|
Mode: "require",
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "not providing an argument to 'trusted_ca_cert' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
trusted_ca_cert
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "not providing an argument to 'trusted_leaf_cert' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
trusted_leaf_cert
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "not providing an argument to 'trusted_ca_cert_file' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
trusted_ca_cert_file
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "not providing an argument to 'trusted_leaf_cert_file' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
trusted_leaf_cert_file
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "using 'trusted_ca_cert' adapts successfully",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trusted_ca_cert %s
|
|
}`, test_der_1)),
|
|
},
|
|
expected: ClientAuthentication{
|
|
CARaw: json.RawMessage(fmt.Sprintf(`{"provider":"inline","trusted_ca_certs":["%s"]}`, test_der_1)),
|
|
},
|
|
},
|
|
{
|
|
name: "using 'inline' trust_pool loads the module successfully",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trust_pool inline {
|
|
trust_der %s
|
|
}
|
|
}
|
|
`, test_der_1)),
|
|
},
|
|
expected: ClientAuthentication{
|
|
CARaw: json.RawMessage(fmt.Sprintf(`{"provider":"inline","trusted_ca_certs":["%s"]}`, test_der_1)),
|
|
},
|
|
},
|
|
{
|
|
name: "setting 'trusted_ca_cert' and 'trust_pool' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trusted_ca_cert %s
|
|
trust_pool inline {
|
|
trust_der %s
|
|
}
|
|
}`, test_der_1, test_der_1)),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "setting 'trust_pool' and 'trusted_ca_cert' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trust_pool inline {
|
|
trust_der %s
|
|
}
|
|
trusted_ca_cert %s
|
|
}`, test_der_1, test_der_1)),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "setting 'trust_pool' and 'trusted_ca_cert' produces an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trust_pool inline {
|
|
trust_der %s
|
|
}
|
|
trusted_ca_cert_file %s
|
|
}`, test_der_1, test_cert_file_1)),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "configuring 'trusted_ca_cert_file' without an argument is an error",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(`
|
|
client_auth {
|
|
trusted_ca_cert_file
|
|
}
|
|
`),
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "configuring 'trusted_ca_cert_file' produces config with 'inline' provider",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trusted_ca_cert_file %s
|
|
}`, test_cert_file_1),
|
|
),
|
|
},
|
|
expected: ClientAuthentication{
|
|
CARaw: json.RawMessage(fmt.Sprintf(`{"provider":"inline","trusted_ca_certs":["%s"]}`, test_der_1)),
|
|
},
|
|
wantErr: false,
|
|
},
|
|
{
|
|
name: "configuring leaf certs does not conflict with 'trust_pool'",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trust_pool inline {
|
|
trust_der %s
|
|
}
|
|
trusted_leaf_cert %s
|
|
}`, test_der_1, test_der_1)),
|
|
},
|
|
expected: ClientAuthentication{
|
|
CARaw: json.RawMessage(fmt.Sprintf(`{"provider":"inline","trusted_ca_certs":["%s"]}`, test_der_1)),
|
|
TrustedLeafCerts: []string{test_der_1},
|
|
},
|
|
},
|
|
{
|
|
name: "providing trusted leaf certificate file loads the cert successfully",
|
|
args: args{
|
|
d: caddyfile.NewTestDispenser(fmt.Sprintf(`
|
|
client_auth {
|
|
trusted_leaf_cert_file %s
|
|
}`, test_cert_file_1)),
|
|
},
|
|
expected: ClientAuthentication{
|
|
TrustedLeafCerts: []string{test_der_1},
|
|
},
|
|
wantErr: false,
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
ca := &ClientAuthentication{}
|
|
if err := ca.UnmarshalCaddyfile(tt.args.d); (err != nil) != tt.wantErr {
|
|
t.Errorf("ClientAuthentication.UnmarshalCaddyfile() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr && !reflect.DeepEqual(&tt.expected, ca) {
|
|
t.Errorf("ClientAuthentication.UnmarshalCaddyfile() = %v, want %v", ca, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestClientAuthenticationProvision(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
ca ClientAuthentication
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "specifying both 'CARaw' and 'TrustedCACerts' produces an error",
|
|
ca: ClientAuthentication{
|
|
CARaw: json.RawMessage(`{"provider":"inline","trusted_ca_certs":["foo"]}`),
|
|
TrustedCACerts: []string{"foo"},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "specifying both 'CARaw' and 'TrustedCACertPEMFiles' produces an error",
|
|
ca: ClientAuthentication{
|
|
CARaw: json.RawMessage(`{"provider":"inline","trusted_ca_certs":["foo"]}`),
|
|
TrustedCACertPEMFiles: []string{"foo"},
|
|
},
|
|
wantErr: true,
|
|
},
|
|
{
|
|
name: "setting 'TrustedCACerts' provisions the cert pool",
|
|
ca: ClientAuthentication{
|
|
TrustedCACerts: []string{test_der_1},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.ca.provision(caddy.Context{})
|
|
if (err != nil) != tt.wantErr {
|
|
t.Errorf("ClientAuthentication.provision() error = %v, wantErr %v", err, tt.wantErr)
|
|
return
|
|
}
|
|
if !tt.wantErr {
|
|
if tt.ca.ca.CertPool() == nil {
|
|
t.Error("CertPool is nil, expected non-nil value")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseCaddyfileNestedMatcherSet(t *testing.T) {
|
|
for i, tc := range []struct {
|
|
input string
|
|
expect string
|
|
wantErr bool
|
|
}{
|
|
{
|
|
input: `match sni example.com`,
|
|
expect: `{"sni":["example.com"]}`,
|
|
},
|
|
{
|
|
input: `match {
|
|
sni example.com
|
|
sni example.net
|
|
}`,
|
|
expect: `{"sni":["example.com","example.net"]}`,
|
|
},
|
|
{
|
|
input: `match {
|
|
remote_ip 10.0.0.0/8
|
|
sni example.com
|
|
remote_ip !10.1.2.3
|
|
}`,
|
|
expect: `{"remote_ip":{"ranges":["10.0.0.0/8"],"not_ranges":["10.1.2.3"]},"sni":["example.com"]}`,
|
|
},
|
|
{
|
|
input: `match {
|
|
sni_regexp ^a
|
|
sni_regexp ^b
|
|
}`,
|
|
wantErr: true,
|
|
},
|
|
} {
|
|
d := caddyfile.NewTestDispenser(tc.input)
|
|
d.Next() // consume "match"
|
|
matcherSet, err := ParseCaddyfileNestedMatcherSet(d)
|
|
if tc.wantErr {
|
|
if err == nil {
|
|
t.Errorf("Test %d: expected error but got none", i)
|
|
}
|
|
continue
|
|
}
|
|
if err != nil {
|
|
t.Errorf("Test %d: unexpected error: %v", i, err)
|
|
continue
|
|
}
|
|
|
|
actual, err := json.Marshal(matcherSet)
|
|
if err != nil {
|
|
t.Fatalf("Test %d: marshaling matcher set: %v", i, err)
|
|
}
|
|
if string(actual) != tc.expect {
|
|
t.Errorf("Test %d: expected %s, got %s", i, tc.expect, actual)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestConnectionPolicyRejectsUnknownNames(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
policy ConnectionPolicy
|
|
}{
|
|
{"unknown curve", ConnectionPolicy{Curves: []string{"X25519"}}},
|
|
{"unknown protocol_min", ConnectionPolicy{ProtocolMin: "TLS1.3"}},
|
|
{"unknown protocol_max", ConnectionPolicy{ProtocolMax: "tls13"}},
|
|
{"protocol_min greater than protocol_max", ConnectionPolicy{ProtocolMin: "tls1.3", ProtocolMax: "tls1.2"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx, err := caddy.ProvisionContext(&caddy.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := (ConnectionPolicies{&tc.policy}).Provision(ctx); err == nil {
|
|
t.Fatal("expected an error, got nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestConnectionPolicyProtocolDefaults(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
name string
|
|
policy ConnectionPolicy
|
|
min, max uint16
|
|
}{
|
|
{"both empty", ConnectionPolicy{}, tls.VersionTLS12, tls.VersionTLS13},
|
|
{"min only", ConnectionPolicy{ProtocolMin: "tls1.3"}, tls.VersionTLS13, tls.VersionTLS13},
|
|
{"max only", ConnectionPolicy{ProtocolMax: "tls1.2"}, tls.VersionTLS12, tls.VersionTLS12},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
ctx, err := caddy.ProvisionContext(&caddy.Config{})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := (ConnectionPolicies{&tc.policy}).Provision(ctx); err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got := tc.policy.TLSConfig; got.MinVersion != tc.min || got.MaxVersion != tc.max {
|
|
t.Fatalf("got min=%x max=%x, want min=%x max=%x", got.MinVersion, got.MaxVersion, tc.min, tc.max)
|
|
}
|
|
})
|
|
}
|
|
}
|