2018-11-21 21:29:18 +00:00
|
|
|
package ca
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"fmt"
|
2019-01-08 01:48:56 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-11-21 21:29:18 +00:00
|
|
|
"reflect"
|
2019-01-10 18:57:06 +00:00
|
|
|
"sort"
|
2018-11-21 21:29:18 +00:00
|
|
|
"testing"
|
2019-01-23 22:33:16 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/api"
|
2018-11-21 21:29:18 +00:00
|
|
|
)
|
|
|
|
|
2019-01-10 23:31:40 +00:00
|
|
|
func Test_newTLSOptionCtx(t *testing.T) {
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient("https://ca.smallstep.com", WithTransport(http.DefaultTransport))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("NewClient() error = %v", err)
|
|
|
|
}
|
|
|
|
|
2019-01-10 23:31:40 +00:00
|
|
|
type args struct {
|
|
|
|
c *Client
|
|
|
|
config *tls.Config
|
2019-01-23 22:33:16 +00:00
|
|
|
sign *api.SignResponse
|
2019-01-10 23:31:40 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
2019-01-12 03:08:08 +00:00
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *TLSOptionCtx
|
2019-01-10 23:31:40 +00:00
|
|
|
}{
|
2019-01-23 22:33:16 +00:00
|
|
|
{"ok", args{client, &tls.Config{}, &api.SignResponse{}}, &TLSOptionCtx{Client: client, Config: &tls.Config{}, Sign: &api.SignResponse{}, mutableConfig: newMutableTLSConfig()}},
|
2019-01-10 23:31:40 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-23 22:33:16 +00:00
|
|
|
if got := newTLSOptionCtx(tt.args.c, tt.args.config, tt.args.sign); !reflect.DeepEqual(got, tt.want) {
|
2019-01-12 03:08:08 +00:00
|
|
|
t.Errorf("newTLSOptionCtx() = %v, want %v", got, tt.want)
|
2019-01-10 23:31:40 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-09 21:20:28 +00:00
|
|
|
func TestTLSOptionCtx_apply(t *testing.T) {
|
2018-11-21 21:29:18 +00:00
|
|
|
fail := func() TLSOption {
|
2019-01-09 21:20:28 +00:00
|
|
|
return func(ctx *TLSOptionCtx) error {
|
2018-11-21 21:29:18 +00:00
|
|
|
return fmt.Errorf("an error")
|
|
|
|
}
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
|
|
|
|
type fields struct {
|
|
|
|
Config *tls.Config
|
|
|
|
}
|
2018-11-21 21:29:18 +00:00
|
|
|
type args struct {
|
|
|
|
options []TLSOption
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-09 21:20:28 +00:00
|
|
|
fields fields
|
2018-11-21 21:29:18 +00:00
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-09 21:20:28 +00:00
|
|
|
{"ok", fields{&tls.Config{}}, args{[]TLSOption{RequireAndVerifyClientCert()}}, false},
|
|
|
|
{"ok", fields{&tls.Config{}}, args{[]TLSOption{VerifyClientCertIfGiven()}}, false},
|
|
|
|
{"fail", fields{&tls.Config{}}, args{[]TLSOption{VerifyClientCertIfGiven(), fail()}}, true},
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Config: tt.fields.Config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
if err := ctx.apply(tt.args.options); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("TLSOptionCtx.apply() error = %v, wantErr %v", err, tt.wantErr)
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRequireAndVerifyClientCert(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want *tls.Config
|
|
|
|
}{
|
|
|
|
{"ok", &tls.Config{ClientAuth: tls.RequireAndVerifyClientCert}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Config: &tls.Config{},
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
if err := RequireAndVerifyClientCert()(ctx); err != nil {
|
2018-11-21 21:29:18 +00:00
|
|
|
t.Errorf("RequireAndVerifyClientCert() error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) {
|
|
|
|
t.Errorf("RequireAndVerifyClientCert() = %v, want %v", ctx.Config, tt.want)
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyClientCertIfGiven(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want *tls.Config
|
|
|
|
}{
|
|
|
|
{"ok", &tls.Config{ClientAuth: tls.VerifyClientCertIfGiven}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Config: &tls.Config{},
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
if err := VerifyClientCertIfGiven()(ctx); err != nil {
|
2018-11-21 21:29:18 +00:00
|
|
|
t.Errorf("VerifyClientCertIfGiven() error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) {
|
|
|
|
t.Errorf("VerifyClientCertIfGiven() = %v, want %v", ctx.Config, tt.want)
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRootCA(t *testing.T) {
|
|
|
|
cert := parseCertificate(rootPEM)
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(cert)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
cert *x509.Certificate
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *tls.Config
|
|
|
|
}{
|
|
|
|
{"ok", args{cert}, &tls.Config{RootCAs: pool}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Config: &tls.Config{},
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
if err := AddRootCA(tt.args.cert)(ctx); err != nil {
|
2018-11-21 21:29:18 +00:00
|
|
|
t.Errorf("AddRootCA() error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-02-23 23:29:56 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) && !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddRootCA() = %v, want %v", ctx.Config, tt.want)
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddClientCA(t *testing.T) {
|
|
|
|
cert := parseCertificate(rootPEM)
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(cert)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
cert *x509.Certificate
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
args args
|
|
|
|
want *tls.Config
|
|
|
|
}{
|
|
|
|
{"ok", args{cert}, &tls.Config{ClientCAs: pool}},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Config: &tls.Config{},
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
if err := AddClientCA(tt.args.cert)(ctx); err != nil {
|
2018-11-21 21:29:18 +00:00
|
|
|
t.Errorf("AddClientCA() error = %v", err)
|
|
|
|
return
|
|
|
|
}
|
2021-02-23 23:29:56 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) && !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddClientCA() = %v, want %v", ctx.Config, tt.want)
|
2018-11-21 21:29:18 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
|
|
|
|
func TestAddRootsToRootCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := parseCertificate(string(root))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(cert)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-08 01:48:56 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{RootCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddRootsToRootCAs()}); (err != nil) != tt.wantErr {
|
2019-01-08 01:48:56 +00:00
|
|
|
t.Errorf("AddRootsToRootCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2021-02-23 23:29:56 +00:00
|
|
|
if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddRootsToRootCAs() = %v, want %v", ctx.Config, tt.want)
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRootsToClientCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := parseCertificate(string(root))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(cert)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-08 01:48:56 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddRootsToClientCAs()}); (err != nil) != tt.wantErr {
|
2019-01-08 01:48:56 +00:00
|
|
|
t.Errorf("AddRootsToClientCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2021-02-23 23:29:56 +00:00
|
|
|
if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddRootsToClientCAs() = %v, want %v", ctx.Config, tt.want)
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddFederationToRootCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
crt1 := parseCertificate(string(root))
|
|
|
|
crt2 := parseCertificate(string(federated))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(crt1)
|
|
|
|
pool.AddCert(crt2)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-08 01:48:56 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{RootCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddFederationToRootCAs()}); (err != nil) != tt.wantErr {
|
2019-01-08 01:48:56 +00:00
|
|
|
t.Errorf("AddFederationToRootCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) {
|
2019-01-10 18:57:06 +00:00
|
|
|
// Federated roots are randomly sorted
|
|
|
|
if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) || ctx.Config.ClientCAs != nil {
|
|
|
|
t.Errorf("AddFederationToRootCAs() = %v, want %v", ctx.Config, tt.want)
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddFederationToClientCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-08 01:48:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
crt1 := parseCertificate(string(root))
|
|
|
|
crt2 := parseCertificate(string(federated))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(crt1)
|
|
|
|
pool.AddCert(crt2)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-08 01:48:56 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-01-09 21:20:28 +00:00
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddFederationToClientCAs()}); (err != nil) != tt.wantErr {
|
2019-01-08 01:48:56 +00:00
|
|
|
t.Errorf("AddFederationToClientCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) {
|
2019-01-10 18:57:06 +00:00
|
|
|
// Federated roots are randomly sorted
|
|
|
|
if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) || ctx.Config.RootCAs != nil {
|
|
|
|
t.Errorf("AddFederationToClientCAs() = %v, want %v", ctx.Config, tt.want)
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddRootsToCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-09 21:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cert := parseCertificate(string(root))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(cert)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-09 21:20:28 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool, RootCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddRootsToCAs()}); (err != nil) != tt.wantErr {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddRootsToCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
2021-02-23 23:29:56 +00:00
|
|
|
if !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) || !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddRootsToCAs() = %v, want %v", ctx.Config, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestAddFederationToCAs(t *testing.T) {
|
|
|
|
ca := startCATestServer()
|
|
|
|
defer ca.Close()
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
client, err := NewClient(ca.URL, WithRootFile("testdata/secrets/root_ca.crt"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientFail, err := NewClient(ca.URL, WithTransport(http.DefaultTransport))
|
2019-01-09 21:20:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
root, err := ioutil.ReadFile("testdata/secrets/root_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
federated, err := ioutil.ReadFile("testdata/secrets/federated_ca.crt")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
crt1 := parseCertificate(string(root))
|
|
|
|
crt2 := parseCertificate(string(federated))
|
|
|
|
pool := x509.NewCertPool()
|
|
|
|
pool.AddCert(crt1)
|
|
|
|
pool.AddCert(crt2)
|
|
|
|
|
2019-01-12 03:08:08 +00:00
|
|
|
type args struct {
|
|
|
|
client *Client
|
|
|
|
config *tls.Config
|
|
|
|
}
|
2019-01-09 21:20:28 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
2019-01-12 03:08:08 +00:00
|
|
|
args args
|
2019-01-09 21:20:28 +00:00
|
|
|
want *tls.Config
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-01-12 03:08:08 +00:00
|
|
|
{"ok", args{client, &tls.Config{}}, &tls.Config{ClientCAs: pool, RootCAs: pool}, false},
|
|
|
|
{"fail", args{clientFail, &tls.Config{}}, &tls.Config{}, true},
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := &TLSOptionCtx{
|
2019-01-23 03:54:12 +00:00
|
|
|
Client: tt.args.client,
|
|
|
|
Config: tt.args.config,
|
|
|
|
mutableConfig: newMutableTLSConfig(),
|
2019-01-09 21:20:28 +00:00
|
|
|
}
|
2019-01-23 03:54:12 +00:00
|
|
|
if err := ctx.apply([]TLSOption{AddFederationToCAs()}); (err != nil) != tt.wantErr {
|
2019-01-09 21:20:28 +00:00
|
|
|
t.Errorf("AddFederationToCAs() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(ctx.Config, tt.want) {
|
2019-01-10 18:57:06 +00:00
|
|
|
// Federated roots are randomly sorted
|
|
|
|
if !equalPools(ctx.Config.ClientCAs, tt.want.ClientCAs) || !equalPools(ctx.Config.RootCAs, tt.want.RootCAs) {
|
|
|
|
t.Errorf("AddFederationToCAs() = %v, want %v", ctx.Config, tt.want)
|
|
|
|
}
|
2019-01-08 01:48:56 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-01-10 18:57:06 +00:00
|
|
|
|
|
|
|
func equalPools(a, b *x509.CertPool) bool {
|
2021-02-23 23:29:56 +00:00
|
|
|
if reflect.DeepEqual(a, b) {
|
|
|
|
return true
|
|
|
|
}
|
2019-01-10 18:57:06 +00:00
|
|
|
subjects := a.Subjects()
|
|
|
|
sA := make([]string, len(subjects))
|
|
|
|
for i := range subjects {
|
|
|
|
sA[i] = string(subjects[i])
|
|
|
|
}
|
|
|
|
subjects = b.Subjects()
|
|
|
|
sB := make([]string, len(subjects))
|
|
|
|
for i := range subjects {
|
|
|
|
sB[i] = string(subjects[i])
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
sort.Strings(sA)
|
|
|
|
sort.Strings(sB)
|
2019-01-10 18:57:06 +00:00
|
|
|
return reflect.DeepEqual(sA, sB)
|
|
|
|
}
|