2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
package atls
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/asn1"
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/oid"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-15 04:35:15 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-15 04:35:15 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m)
|
|
|
|
}
|
|
|
|
|
2022-03-22 11:03:15 -04:00
|
|
|
func TestTLSConfig(t *testing.T) {
|
2022-06-15 09:58:23 -04:00
|
|
|
oid1 := fakeOID{asn1.ObjectIdentifier{1, 3, 9900, 1}}
|
|
|
|
oid2 := fakeOID{asn1.ObjectIdentifier{1, 3, 9900, 2}}
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-05-24 10:33:44 -04:00
|
|
|
clientIssuer Issuer
|
|
|
|
clientValidators []Validator
|
|
|
|
serverIssuer Issuer
|
|
|
|
serverValidators []Validator
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-05-24 10:33:44 -04:00
|
|
|
"client->server basic": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-05-24 10:33:44 -04:00
|
|
|
"client->server multiple validators": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid2),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1), NewFakeValidator(oid2)},
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-05-24 10:33:44 -04:00
|
|
|
"client->server validate error": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{FakeValidator{oid1, errors.New("failed")}},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-05-24 10:33:44 -04:00
|
|
|
"client->server unknown oid": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid2)},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"client->server client cert is not verified": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
},
|
|
|
|
"server->client basic": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
2022-05-24 10:33:44 -04:00
|
|
|
},
|
|
|
|
"server->client multiple validators": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1), NewFakeValidator(oid2)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid2),
|
2022-05-24 10:33:44 -04:00
|
|
|
},
|
|
|
|
"server->client validate error": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverValidators: []Validator{FakeValidator{oid1, errors.New("failed")}},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"server->client unknown oid": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverValidators: []Validator{NewFakeValidator(oid2)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"mutual basic": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
},
|
|
|
|
"mutual multiple validators": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid2),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1), NewFakeValidator(oid2)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid2),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1), NewFakeValidator(oid2)},
|
2022-05-24 10:33:44 -04:00
|
|
|
},
|
2022-05-25 03:34:05 -04:00
|
|
|
"mutual fails if client sends no attestation": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-05-25 03:34:05 -04:00
|
|
|
"mutual fails if server sends no attestation": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-25 03:34:05 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
2022-05-24 10:33:44 -04:00
|
|
|
"mutual validate error client side": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{FakeValidator{oid1, errors.New("failed")}},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"mutual validate error server side": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
serverValidators: []Validator{FakeValidator{oid1, errors.New("failed")}},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"mutual unknown oid from client": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid1),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid2),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"mutual unknown oid from server": {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverIssuer: NewFakeIssuer(oid2),
|
|
|
|
serverValidators: []Validator{NewFakeValidator(oid1)},
|
|
|
|
clientIssuer: NewFakeIssuer(oid1),
|
|
|
|
clientValidators: []Validator{NewFakeValidator(oid1)},
|
2022-05-24 10:33:44 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create server
|
|
|
|
//
|
|
|
|
|
2022-05-24 10:33:44 -04:00
|
|
|
serverConfig, err := CreateAttestationServerTLSConfig(tc.serverIssuer, tc.serverValidators)
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = io.WriteString(w, "hello")
|
|
|
|
}))
|
|
|
|
server.TLS = serverConfig
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create client
|
|
|
|
//
|
|
|
|
|
2022-05-24 10:33:44 -04:00
|
|
|
clientConfig, err := CreateAttestationClientTLSConfig(tc.clientIssuer, tc.clientValidators)
|
2022-03-22 11:03:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
client := http.Client{Transport: &http.Transport{TLSClientConfig: clientConfig}}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Test connection
|
|
|
|
//
|
|
|
|
|
|
|
|
server.StartTLS()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL, http.NoBody)
|
|
|
|
require.NoError(err)
|
|
|
|
resp, err := client.Do(req)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(err)
|
|
|
|
assert.EqualValues("hello", body)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-15 04:35:15 -04:00
|
|
|
func TestClientConnectionConcurrency(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create servers.
|
|
|
|
//
|
|
|
|
|
|
|
|
const serverCount = 15
|
|
|
|
|
|
|
|
var urls []string
|
|
|
|
|
|
|
|
for i := 0; i < serverCount; i++ {
|
2022-06-15 09:58:23 -04:00
|
|
|
serverCfg, err := CreateAttestationServerTLSConfig(NewFakeIssuer(oid.Dummy{}), NewFakeValidators(oid.Dummy{}))
|
2022-06-15 04:35:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = io.WriteString(w, "hello")
|
|
|
|
}))
|
|
|
|
server.TLS = serverCfg
|
|
|
|
|
|
|
|
server.StartTLS()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
urls = append(urls, server.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create client.
|
|
|
|
//
|
|
|
|
|
2022-06-15 09:58:23 -04:00
|
|
|
clientConfig, err := CreateAttestationClientTLSConfig(NewFakeIssuer(oid.Dummy{}), NewFakeValidators(oid.Dummy{}))
|
2022-06-15 04:35:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
client := http.Client{Transport: &http.Transport{TLSClientConfig: clientConfig}}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare a request for each server.
|
|
|
|
//
|
|
|
|
|
|
|
|
var reqs []*http.Request
|
|
|
|
for _, url := range urls {
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody)
|
|
|
|
require.NoError(err)
|
|
|
|
reqs = append(reqs, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the request concurrently and collect the errors in a channel.
|
|
|
|
// The config of the client is reused, so the nonce isn't fresh.
|
|
|
|
// This explicitly checks for data races on the clientConnection.
|
|
|
|
//
|
|
|
|
|
|
|
|
errChan := make(chan error, serverCount)
|
|
|
|
|
|
|
|
for _, req := range reqs {
|
|
|
|
go func(req *http.Request) {
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err == nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Wait for the requests to finish and check the errors.
|
|
|
|
//
|
|
|
|
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
assert.NoError(<-errChan)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerConnectionConcurrency(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create servers.
|
|
|
|
// The serverCfg is reused.
|
|
|
|
//
|
|
|
|
|
|
|
|
const serverCount = 10
|
|
|
|
|
|
|
|
var urls []string
|
|
|
|
|
2022-06-15 09:58:23 -04:00
|
|
|
serverCfg, err := CreateAttestationServerTLSConfig(NewFakeIssuer(oid.Dummy{}), NewFakeValidators(oid.Dummy{}))
|
2022-06-15 04:35:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = io.WriteString(w, "hello")
|
|
|
|
}))
|
|
|
|
server.TLS = serverCfg
|
|
|
|
|
|
|
|
server.StartTLS()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
urls = append(urls, server.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Create client.
|
|
|
|
//
|
|
|
|
|
2022-06-15 09:58:23 -04:00
|
|
|
clientConfig, err := CreateAttestationClientTLSConfig(NewFakeIssuer(oid.Dummy{}), NewFakeValidators(oid.Dummy{}))
|
2022-06-15 04:35:15 -04:00
|
|
|
require.NoError(err)
|
|
|
|
client := http.Client{Transport: &http.Transport{TLSClientConfig: clientConfig}}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Prepare a request for each server.
|
|
|
|
//
|
|
|
|
|
|
|
|
var reqs []*http.Request
|
|
|
|
for _, url := range urls {
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody)
|
|
|
|
require.NoError(err)
|
|
|
|
reqs = append(reqs, req)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Do the request concurrently and collect the errors in a channel.
|
|
|
|
//
|
|
|
|
|
|
|
|
errChan := make(chan error, serverCount)
|
|
|
|
|
|
|
|
for _, req := range reqs {
|
|
|
|
go func(req *http.Request) {
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err == nil {
|
|
|
|
resp.Body.Close()
|
|
|
|
}
|
|
|
|
errChan <- err
|
|
|
|
}(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Wait for the requests to finish and check the errors.
|
|
|
|
//
|
|
|
|
|
|
|
|
for i := 0; i < serverCount; i++ {
|
|
|
|
assert.NoError(<-errChan)
|
|
|
|
}
|
|
|
|
}
|