2022-09-08 08:45:27 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
|
|
|
package recoveryserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-01-16 05:19:03 -05:00
|
|
|
"errors"
|
2022-09-08 08:45:27 -04:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/disk-mapper/recoverproto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/testdialer"
|
2023-01-16 05:19:03 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/kms/kms"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/oid"
|
2022-09-08 08:45:27 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.uber.org/goleak"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-01-16 05:19:03 -05:00
|
|
|
goleak.VerifyTestMain(m,
|
|
|
|
// https://github.com/census-instrumentation/opencensus-go/issues/1262
|
|
|
|
goleak.IgnoreTopFunction("go.opencensus.io/stats/view.(*worker).start"),
|
|
|
|
)
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServe(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
log := logger.NewTest(t)
|
|
|
|
uuid := "uuid"
|
2023-01-16 05:19:03 -05:00
|
|
|
server := New(atls.NewFakeIssuer(oid.Dummy{}), newStubKMS(nil, nil), log)
|
2022-09-08 08:45:27 -04:00
|
|
|
dialer := testdialer.NewBufconnDialer()
|
|
|
|
listener := dialer.GetListener("192.0.2.1:1234")
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
// Serve method returns when context is canceled
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, _, err := server.Serve(ctx, listener, uuid)
|
|
|
|
assert.ErrorIs(err, context.Canceled)
|
|
|
|
}()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
wg.Wait()
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
server = New(atls.NewFakeIssuer(oid.Dummy{}), newStubKMS(nil, nil), log)
|
2022-09-08 08:45:27 -04:00
|
|
|
dialer = testdialer.NewBufconnDialer()
|
|
|
|
listener = dialer.GetListener("192.0.2.1:1234")
|
|
|
|
|
|
|
|
// Serve method returns without error when the server is shut down
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
_, _, err := server.Serve(context.Background(), listener, uuid)
|
|
|
|
assert.NoError(err)
|
|
|
|
}()
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
server.grpcServer.GracefulStop()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Serve method returns an error when serving is unsuccessful
|
|
|
|
_, _, err := server.Serve(context.Background(), listener, uuid)
|
|
|
|
assert.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRecover(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2023-01-16 05:19:03 -05:00
|
|
|
kmsURI string
|
|
|
|
storageURI string
|
|
|
|
factory kmsFactory
|
2022-09-08 08:45:27 -04:00
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2023-01-16 05:19:03 -05:00
|
|
|
// base64 encoded: key=masterkey&salt=somesalt
|
|
|
|
kmsURI: "kms://cluster-kms?key=bWFzdGVya2V5&salt=c29tZXNhbHQ=",
|
|
|
|
storageURI: "storage://no-store",
|
|
|
|
factory: newStubKMS(nil, nil),
|
2022-09-08 08:45:27 -04:00
|
|
|
},
|
2023-01-16 05:19:03 -05:00
|
|
|
"kms init fails": {
|
|
|
|
factory: newStubKMS(errors.New("setup failed"), nil),
|
|
|
|
wantErr: true,
|
2022-09-08 08:45:27 -04:00
|
|
|
},
|
2023-01-16 05:19:03 -05:00
|
|
|
"GetDEK fails": {
|
|
|
|
kmsURI: "kms://cluster-kms?key=bWFzdGVya2V5&salt=c29tZXNhbHQ=",
|
|
|
|
storageURI: "storage://no-store",
|
|
|
|
factory: newStubKMS(nil, errors.New("GetDEK failed")),
|
|
|
|
wantErr: true,
|
2022-09-08 08:45:27 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
serverUUID := "uuid"
|
2023-01-16 05:19:03 -05:00
|
|
|
server := New(atls.NewFakeIssuer(oid.Dummy{}), tc.factory, logger.NewTest(t))
|
2022-09-08 08:45:27 -04:00
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
listener := netDialer.GetListener("192.0.2.1:1234")
|
|
|
|
|
|
|
|
var diskKey, measurementSecret []byte
|
|
|
|
var serveErr error
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
defer wg.Wait()
|
|
|
|
|
|
|
|
serveCtx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
diskKey, measurementSecret, serveErr = server.Serve(serveCtx, listener, serverUUID)
|
|
|
|
}()
|
|
|
|
|
|
|
|
conn, err := dialer.New(nil, nil, netDialer).Dial(ctx, "192.0.2.1:1234")
|
|
|
|
require.NoError(err)
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
req := recoverproto.RecoverMessage{
|
|
|
|
KmsUri: tc.kmsURI,
|
|
|
|
StorageUri: tc.storageURI,
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|
2023-01-16 05:19:03 -05:00
|
|
|
_, err = recoverproto.NewAPIClient(conn).Recover(ctx, &req)
|
2022-09-08 08:45:27 -04:00
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
if tc.wantErr {
|
2022-09-08 08:45:27 -04:00
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2023-01-16 05:19:03 -05:00
|
|
|
require.NoError(serveErr)
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.NotNil(measurementSecret)
|
|
|
|
assert.NotNil(diskKey)
|
2022-09-08 08:45:27 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-16 05:19:03 -05:00
|
|
|
func newStubKMS(setupErr, getDEKErr error) kmsFactory {
|
|
|
|
return func(ctx context.Context, storageURI string, kmsURI string) (kms.CloudKMS, error) {
|
|
|
|
if setupErr != nil {
|
|
|
|
return nil, setupErr
|
|
|
|
}
|
|
|
|
return &stubKMS{getDEKErr: getDEKErr}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubKMS struct {
|
2023-02-08 06:03:54 -05:00
|
|
|
kms.CloudKMS
|
2023-01-16 05:19:03 -05:00
|
|
|
getDEKErr error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stubKMS) GetDEK(ctx context.Context, dekID string, dekSize int) ([]byte, error) {
|
|
|
|
if s.getDEKErr != nil {
|
|
|
|
return nil, s.getDEKErr
|
|
|
|
}
|
|
|
|
return []byte("someDEK"), nil
|
2022-09-08 08:45:27 -04:00
|
|
|
}
|