2022-04-12 08:24:36 -04:00
|
|
|
package keyservice
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-29 09:26:29 -04:00
|
|
|
"github.com/edgelesssys/constellation/bootstrapper/role"
|
2022-06-15 09:58:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
2022-06-29 10:17:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/metadata"
|
2022-06-13 05:40:27 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/atlscredentials"
|
2022-06-28 10:51:30 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/logger"
|
2022-06-15 09:58:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/oid"
|
2022-06-29 10:17:23 -04:00
|
|
|
"github.com/edgelesssys/constellation/kms/kmsproto"
|
2022-04-11 08:25:19 -04:00
|
|
|
"github.com/edgelesssys/constellation/state/keyservice/keyproto"
|
2022-04-12 08:24:36 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-30 09:24:36 -04:00
|
|
|
"go.uber.org/goleak"
|
2022-04-12 08:24:36 -04:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/test/bufconn"
|
|
|
|
)
|
|
|
|
|
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-04-11 08:25:19 -04:00
|
|
|
func TestRequestKeyLoop(t *testing.T) {
|
2022-06-29 10:17:23 -04:00
|
|
|
defaultInstance := metadata.InstanceMetadata{
|
2022-04-12 08:24:36 -04:00
|
|
|
Name: "test-instance",
|
|
|
|
ProviderID: "/test/provider",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: []string{"192.0.2.1"},
|
2022-04-12 08:24:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
server *stubAPIServer
|
2022-04-26 10:54:05 -04:00
|
|
|
wantCalls int
|
2022-06-29 10:17:23 -04:00
|
|
|
listResponse []metadata.InstanceMetadata
|
2022-04-12 08:24:36 -04:00
|
|
|
dontStartServer bool
|
|
|
|
}{
|
|
|
|
"success": {
|
2022-06-29 10:17:23 -04:00
|
|
|
server: &stubAPIServer{requestStateDiskKeyResp: &kmsproto.GetDataKeyResponse{}},
|
|
|
|
listResponse: []metadata.InstanceMetadata{defaultInstance},
|
2022-04-12 08:24:36 -04:00
|
|
|
},
|
|
|
|
"no error if server throws an error": {
|
|
|
|
server: &stubAPIServer{
|
2022-06-29 10:17:23 -04:00
|
|
|
requestStateDiskKeyResp: &kmsproto.GetDataKeyResponse{},
|
2022-04-12 08:24:36 -04:00
|
|
|
requestStateDiskKeyErr: errors.New("error"),
|
|
|
|
},
|
2022-06-29 10:17:23 -04:00
|
|
|
listResponse: []metadata.InstanceMetadata{defaultInstance},
|
2022-04-12 08:24:36 -04:00
|
|
|
},
|
|
|
|
"no error if the server can not be reached": {
|
2022-06-29 10:17:23 -04:00
|
|
|
server: &stubAPIServer{requestStateDiskKeyResp: &kmsproto.GetDataKeyResponse{}},
|
|
|
|
listResponse: []metadata.InstanceMetadata{defaultInstance},
|
2022-04-12 08:24:36 -04:00
|
|
|
dontStartServer: true,
|
|
|
|
},
|
|
|
|
"no error if no endpoint is available": {
|
2022-06-29 10:17:23 -04:00
|
|
|
server: &stubAPIServer{requestStateDiskKeyResp: &kmsproto.GetDataKeyResponse{}},
|
2022-04-12 08:24:36 -04:00
|
|
|
},
|
|
|
|
"works for multiple endpoints": {
|
2022-06-29 10:17:23 -04:00
|
|
|
server: &stubAPIServer{requestStateDiskKeyResp: &kmsproto.GetDataKeyResponse{}},
|
|
|
|
listResponse: []metadata.InstanceMetadata{
|
2022-04-12 08:24:36 -04:00
|
|
|
defaultInstance,
|
|
|
|
{
|
|
|
|
Name: "test-instance-2",
|
|
|
|
ProviderID: "/test/provider",
|
2022-06-29 09:26:29 -04:00
|
|
|
Role: role.ControlPlane,
|
2022-05-24 04:04:42 -04:00
|
|
|
PrivateIPs: []string{"192.0.2.2"},
|
2022-04-12 08:24:36 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
require := require.New(t)
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
keyReceived := make(chan struct{}, 1)
|
2022-04-12 08:24:36 -04:00
|
|
|
listener := bufconn.Listen(1)
|
|
|
|
defer listener.Close()
|
|
|
|
|
2022-06-15 09:58:23 -04:00
|
|
|
creds := atlscredentials.New(atls.NewFakeIssuer(oid.Dummy{}), nil)
|
2022-06-13 05:40:27 -04:00
|
|
|
s := grpc.NewServer(grpc.Creds(creds))
|
2022-06-29 10:17:23 -04:00
|
|
|
kmsproto.RegisterAPIServer(s, tc.server)
|
2022-04-12 08:24:36 -04:00
|
|
|
|
|
|
|
if !tc.dontStartServer {
|
|
|
|
go func() { require.NoError(s.Serve(listener)) }()
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
keyWaiter := &KeyAPI{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: logger.NewTest(t),
|
2022-04-12 08:24:36 -04:00
|
|
|
metadata: stubMetadata{listResponse: tc.listResponse},
|
|
|
|
keyReceived: keyReceived,
|
|
|
|
timeout: 500 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
|
|
|
// notify the API a key was received after 1 second
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-04-11 08:25:19 -04:00
|
|
|
keyReceived <- struct{}{}
|
2022-04-12 08:24:36 -04:00
|
|
|
}()
|
|
|
|
|
2022-07-14 09:45:04 -04:00
|
|
|
keyWaiter.requestKeyLoop(
|
2022-04-12 08:24:36 -04:00
|
|
|
"1234",
|
|
|
|
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
|
|
|
|
return listener.DialContext(ctx)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
s.Stop()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
func TestPushStateDiskKey(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
testAPI *KeyAPI
|
|
|
|
request *keyproto.PushStateDiskKeyRequest
|
|
|
|
wantErr bool
|
2022-04-11 08:25:19 -04:00
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
2022-07-26 04:58:39 -04:00
|
|
|
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), MeasurementSecret: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")},
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"key already set": {
|
|
|
|
testAPI: &KeyAPI{
|
|
|
|
keyReceived: make(chan struct{}, 1),
|
|
|
|
key: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"),
|
|
|
|
},
|
2022-07-26 04:58:39 -04:00
|
|
|
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"), MeasurementSecret: []byte("CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
"incorrect size of pushed key": {
|
2022-04-26 10:54:05 -04:00
|
|
|
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
2022-07-26 04:58:39 -04:00
|
|
|
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("AAAAAAAAAAAAAAAA"), MeasurementSecret: []byte("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB")},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"incorrect size of measurement secret": {
|
|
|
|
testAPI: &KeyAPI{keyReceived: make(chan struct{}, 1)},
|
|
|
|
request: &keyproto.PushStateDiskKeyRequest{StateDiskKey: []byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"), MeasurementSecret: []byte("BBBBBBBBBBBBBBBB")},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantErr: true,
|
2022-04-11 08:25:19 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
2022-06-28 10:51:30 -04:00
|
|
|
tc.testAPI.log = logger.NewTest(t)
|
2022-04-11 08:25:19 -04:00
|
|
|
_, err := tc.testAPI.PushStateDiskKey(context.Background(), tc.request)
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-04-11 08:25:19 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.request.StateDiskKey, tc.testAPI.key)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestResetKey(t *testing.T) {
|
2022-06-28 10:51:30 -04:00
|
|
|
api := New(logger.NewTest(t), nil, nil, time.Second)
|
2022-04-11 08:25:19 -04:00
|
|
|
|
|
|
|
api.key = []byte{0x1, 0x2, 0x3}
|
|
|
|
api.ResetKey()
|
|
|
|
assert.Nil(t, api.key)
|
|
|
|
}
|
|
|
|
|
2022-04-12 08:24:36 -04:00
|
|
|
type stubAPIServer struct {
|
2022-06-29 10:17:23 -04:00
|
|
|
requestStateDiskKeyResp *kmsproto.GetDataKeyResponse
|
2022-04-12 08:24:36 -04:00
|
|
|
requestStateDiskKeyErr error
|
2022-06-29 10:17:23 -04:00
|
|
|
kmsproto.UnimplementedAPIServer
|
2022-04-12 08:24:36 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:17:23 -04:00
|
|
|
func (s *stubAPIServer) GetDataKey(ctx context.Context, req *kmsproto.GetDataKeyRequest) (*kmsproto.GetDataKeyResponse, error) {
|
2022-04-12 08:24:36 -04:00
|
|
|
return s.requestStateDiskKeyResp, s.requestStateDiskKeyErr
|
|
|
|
}
|
|
|
|
|
2022-04-11 08:25:19 -04:00
|
|
|
type stubMetadata struct {
|
2022-06-29 10:17:23 -04:00
|
|
|
listResponse []metadata.InstanceMetadata
|
2022-04-11 08:25:19 -04:00
|
|
|
}
|
|
|
|
|
2022-06-29 10:17:23 -04:00
|
|
|
func (s stubMetadata) List(ctx context.Context) ([]metadata.InstanceMetadata, error) {
|
2022-04-11 08:25:19 -04:00
|
|
|
return s.listResponse, nil
|
|
|
|
}
|