2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
package watcher
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-29 08:29:50 -04:00
|
|
|
"crypto/x509"
|
2022-05-23 05:36:54 -04:00
|
|
|
"encoding/asn1"
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2022-06-15 10:00:48 -04:00
|
|
|
"path/filepath"
|
2022-05-23 05:36:54 -04:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2022-11-24 04:57:58 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/measurements"
|
2023-06-09 09:41:02 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/attestation/variant"
|
2023-04-06 11:00:56 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/spf13/afero"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
2022-06-30 09:24:36 -04:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
func TestNewUpdateableValidator(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2023-09-29 08:29:50 -04:00
|
|
|
variant variant.Variant
|
|
|
|
config config.AttestationCfg
|
|
|
|
snpCerts *stubSnpCerts
|
|
|
|
wantErr bool
|
2022-05-23 05:36:54 -04:00
|
|
|
}{
|
|
|
|
"azure": {
|
2023-05-03 05:11:53 -04:00
|
|
|
variant: variant.AzureSEVSNP{},
|
|
|
|
config: config.DefaultForAzureSEVSNP(),
|
2023-09-29 08:29:50 -04:00
|
|
|
snpCerts: &stubSnpCerts{
|
|
|
|
ask: &x509.Certificate{},
|
|
|
|
ark: &x509.Certificate{},
|
|
|
|
},
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"gcp": {
|
2023-05-03 05:11:53 -04:00
|
|
|
variant: variant.GCPSEVES{},
|
2023-03-10 05:33:06 -05:00
|
|
|
config: &config.GCPSEVES{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"qemu": {
|
2023-05-03 05:11:53 -04:00
|
|
|
variant: variant.QEMUVTPM{},
|
2023-03-10 05:33:06 -05:00
|
|
|
config: &config.QEMUVTPM{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"no file": {
|
2023-05-03 05:11:53 -04:00
|
|
|
variant: variant.AzureSEVSNP{},
|
|
|
|
wantErr: true,
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
"invalid provider": {
|
2023-05-03 05:11:53 -04:00
|
|
|
variant: fakeOID{1, 3, 9900, 9999, 9999},
|
2023-03-10 05:33:06 -05:00
|
|
|
config: &config.GCPSEVES{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2023-05-03 05:11:53 -04:00
|
|
|
wantErr: true,
|
2022-05-23 05:36:54 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
2023-05-03 05:11:53 -04:00
|
|
|
if tc.config != nil {
|
2022-05-23 05:36:54 -04:00
|
|
|
require.NoError(handler.WriteJSON(
|
2023-05-03 05:11:53 -04:00
|
|
|
filepath.Join(constants.ServiceBasePath, constants.AttestationConfigFilename),
|
|
|
|
tc.config,
|
2022-08-29 10:41:09 -04:00
|
|
|
))
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:13:01 -04:00
|
|
|
_, err := NewValidator(
|
2022-06-28 10:51:30 -04:00
|
|
|
logger.NewTest(t),
|
2023-03-14 06:46:27 -04:00
|
|
|
tc.variant,
|
2022-06-28 10:51:30 -04:00
|
|
|
handler,
|
2023-09-29 08:29:50 -04:00
|
|
|
tc.snpCerts,
|
2022-06-28 10:51:30 -04:00
|
|
|
)
|
2022-05-23 05:36:54 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-29 08:29:50 -04:00
|
|
|
type stubSnpCerts struct {
|
|
|
|
ask *x509.Certificate
|
|
|
|
ark *x509.Certificate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubSnpCerts) SevSnpCerts() (ask *x509.Certificate, ark *x509.Certificate) {
|
|
|
|
return s.ask, s.ark
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
func TestUpdate(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
|
|
|
|
// create server
|
2022-06-28 10:51:30 -04:00
|
|
|
validator := &Updatable{
|
2023-03-14 06:46:27 -04:00
|
|
|
log: logger.NewTest(t),
|
2023-03-29 03:30:13 -04:00
|
|
|
variant: variant.Dummy{},
|
2023-03-14 06:46:27 -04:00
|
|
|
fileHandler: handler,
|
2022-06-28 10:51:30 -04:00
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
// Update should fail if the file does not exist
|
|
|
|
assert.Error(validator.Update())
|
|
|
|
|
|
|
|
// write measurement config
|
|
|
|
require.NoError(handler.WriteJSON(
|
2023-05-03 05:11:53 -04:00
|
|
|
filepath.Join(constants.ServiceBasePath, constants.AttestationConfigFilename),
|
2023-03-10 05:33:06 -05:00
|
|
|
&config.DummyCfg{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2022-08-29 10:41:09 -04:00
|
|
|
))
|
2022-05-23 05:36:54 -04:00
|
|
|
|
|
|
|
// call update once to initialize the server's validator
|
|
|
|
require.NoError(validator.Update())
|
|
|
|
|
|
|
|
// create tls config and start the server
|
|
|
|
serverConfig, err := atls.CreateAttestationServerTLSConfig(nil, []atls.Validator{validator})
|
|
|
|
require.NoError(err)
|
|
|
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
_, _ = io.WriteString(w, "hello")
|
|
|
|
}))
|
|
|
|
server.TLS = serverConfig
|
|
|
|
server.StartTLS()
|
|
|
|
defer server.Close()
|
|
|
|
|
|
|
|
// test connection to server
|
2023-03-29 03:30:13 -04:00
|
|
|
clientOID := variant.Dummy{}
|
2022-05-23 05:36:54 -04:00
|
|
|
resp, err := testConnection(require, server.URL, clientOID)
|
|
|
|
require.NoError(err)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(err)
|
|
|
|
assert.EqualValues("hello", body)
|
|
|
|
|
|
|
|
// update the server's validator
|
2023-03-29 03:30:13 -04:00
|
|
|
validator.variant = variant.QEMUVTPM{}
|
2022-05-23 05:36:54 -04:00
|
|
|
require.NoError(validator.Update())
|
|
|
|
|
|
|
|
// client connection should fail now, since the server's validator expects a different OID from the client
|
2022-08-02 10:25:47 -04:00
|
|
|
resp, err = testConnection(require, server.URL, clientOID)
|
|
|
|
if err == nil {
|
|
|
|
defer resp.Body.Close()
|
|
|
|
}
|
2022-05-23 05:36:54 -04:00
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
2022-11-24 09:50:59 -05:00
|
|
|
func TestOIDConcurrency(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
require.NoError(handler.WriteJSON(
|
2023-05-03 05:11:53 -04:00
|
|
|
filepath.Join(constants.ServiceBasePath, constants.AttestationConfigFilename),
|
2023-03-10 05:33:06 -05:00
|
|
|
&config.DummyCfg{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2022-11-24 09:50:59 -05:00
|
|
|
))
|
|
|
|
|
|
|
|
// create server
|
|
|
|
validator := &Updatable{
|
2023-03-14 06:46:27 -04:00
|
|
|
log: logger.NewTest(t),
|
2023-03-29 03:30:13 -04:00
|
|
|
variant: variant.Dummy{},
|
2023-03-14 06:46:27 -04:00
|
|
|
fileHandler: handler,
|
2022-11-24 09:50:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// call update once to initialize the server's validator
|
|
|
|
require.NoError(validator.Update())
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2 * 20)
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
assert.NoError(validator.Update())
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
validator.OID()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
func TestUpdateConcurrency(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
handler := file.NewHandler(afero.NewMemMapFs())
|
|
|
|
validator := &Updatable{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: logger.NewTest(t),
|
2022-05-23 05:36:54 -04:00
|
|
|
fileHandler: handler,
|
2023-03-29 03:30:13 -04:00
|
|
|
variant: variant.Dummy{},
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
require.NoError(handler.WriteJSON(
|
2023-05-03 05:11:53 -04:00
|
|
|
filepath.Join(constants.ServiceBasePath, constants.AttestationConfigFilename),
|
2023-03-10 05:33:06 -05:00
|
|
|
&config.DummyCfg{Measurements: measurements.M{11: measurements.WithAllBytes(0x00, measurements.Enforce, measurements.PCRMeasurementLength)}},
|
2022-05-23 05:36:54 -04:00
|
|
|
file.OptNone,
|
|
|
|
))
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
assert.NoError(validator.Update())
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
func testConnection(require *require.Assertions, url string, oid variant.Getter) (*http.Response, error) {
|
2023-03-14 06:46:27 -04:00
|
|
|
clientConfig, err := atls.CreateAttestationClientTLSConfig(fakeIssuer{oid}, nil)
|
2022-05-23 05:36:54 -04:00
|
|
|
require.NoError(err)
|
|
|
|
client := http.Client{Transport: &http.Transport{TLSClientConfig: clientConfig}}
|
|
|
|
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, http.NoBody)
|
|
|
|
require.NoError(err)
|
|
|
|
return client.Do(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeIssuer struct {
|
2023-03-29 03:30:13 -04:00
|
|
|
variant.Getter
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
|
2023-03-29 03:06:10 -04:00
|
|
|
func (fakeIssuer) Issue(_ context.Context, userData []byte, nonce []byte) ([]byte, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
return json.Marshal(fakeDoc{UserData: userData, Nonce: nonce})
|
|
|
|
}
|
|
|
|
|
|
|
|
type fakeOID asn1.ObjectIdentifier
|
|
|
|
|
|
|
|
func (o fakeOID) OID() asn1.ObjectIdentifier {
|
|
|
|
return asn1.ObjectIdentifier(o)
|
|
|
|
}
|
|
|
|
|
2023-03-29 03:30:13 -04:00
|
|
|
func (o fakeOID) String() string {
|
|
|
|
return o.OID().String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o fakeOID) Equal(other variant.Getter) bool {
|
|
|
|
return o.OID().Equal(other.OID())
|
|
|
|
}
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
type fakeDoc struct {
|
|
|
|
UserData []byte
|
|
|
|
Nonce []byte
|
|
|
|
}
|