2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-06-28 11:03:28 -04:00
|
|
|
"context"
|
2022-03-22 11:03:15 -04:00
|
|
|
"encoding/base64"
|
2022-06-28 11:03:28 -04:00
|
|
|
"encoding/json"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
2022-06-28 11:03:28 -04:00
|
|
|
"net"
|
|
|
|
"strconv"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/atls"
|
2022-06-07 05:08:44 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-06-28 11:03:28 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/constants"
|
2022-05-16 11:32:00 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/file"
|
2022-06-28 11:03:28 -04:00
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/dialer"
|
|
|
|
"github.com/edgelesssys/constellation/internal/grpc/testdialer"
|
|
|
|
"github.com/edgelesssys/constellation/internal/oid"
|
|
|
|
"github.com/edgelesssys/constellation/verify/verifyproto"
|
2022-04-27 05:17:41 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-06-28 11:03:28 -04:00
|
|
|
"google.golang.org/grpc"
|
2022-03-22 11:03:15 -04:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
rpcStatus "google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
func TestVerifyCmdArgumentValidation(t *testing.T) {
|
2022-03-22 11:03:15 -04:00
|
|
|
testCases := map[string]struct {
|
2022-04-27 05:17:41 -04:00
|
|
|
args []string
|
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-05-04 02:50:50 -04:00
|
|
|
"no args": {[]string{}, true},
|
|
|
|
"valid azure": {[]string{"azure"}, false},
|
|
|
|
"valid gcp": {[]string{"gcp"}, false},
|
|
|
|
"invalid provider": {[]string{"invalid", "192.0.2.1", "1234"}, 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)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewVerifyCmd()
|
2022-04-27 05:17:41 -04:00
|
|
|
err := cmd.ValidateArgs(tc.args)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
func TestVerify(t *testing.T) {
|
|
|
|
zeroBase64 := base64.StdEncoding.EncodeToString([]byte("00000000000000000000000000000000"))
|
|
|
|
someErr := errors.New("failed")
|
2022-03-22 11:03:15 -04:00
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs func(*require.Assertions) afero.Fs
|
|
|
|
provider cloudprovider.Provider
|
2022-07-01 04:57:29 -04:00
|
|
|
protoClient *stubVerifyClient
|
2022-05-04 02:50:50 -04:00
|
|
|
nodeEndpointFlag string
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag string
|
2022-05-04 02:50:50 -04:00
|
|
|
ownerIDFlag string
|
|
|
|
clusterIDFlag string
|
2022-07-05 07:52:36 -04:00
|
|
|
idFile *clusterIDsFile
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint string
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
2022-04-27 05:17:41 -04:00
|
|
|
"gcp": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"azure": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
|
|
|
"default port": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:9000",
|
|
|
|
},
|
|
|
|
"endpoint not set": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
ownerIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"endpoint from id file": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
ownerIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-05 07:52:36 -04:00
|
|
|
idFile: &clusterIDsFile{Endpoint: "192.0.2.1:1234"},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
|
|
|
},
|
|
|
|
"override endpoint from details file": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.2:1234",
|
|
|
|
ownerIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-05 07:52:36 -04:00
|
|
|
idFile: &clusterIDsFile{Endpoint: "192.0.2.1:1234"},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.2:1234",
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
|
|
|
"invalid endpoint": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: ":::::",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr: true,
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"neither owner id nor cluster id set": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-01 04:57:29 -04:00
|
|
|
"use owner id from id file": {
|
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
protoClient: &stubVerifyClient{},
|
2022-07-05 07:52:36 -04:00
|
|
|
idFile: &clusterIDsFile{OwnerID: zeroBase64},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"config file not existing": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
ownerIDFlag: zeroBase64,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2022-05-13 05:56:43 -04:00
|
|
|
configFlag: "./file",
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr: true,
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"error protoClient GetState": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{verifyErr: rpcStatus.Error(codes.Internal, "failed")},
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-04-27 05:17:41 -04:00
|
|
|
"error protoClient GetState not rpc": {
|
2022-05-04 02:50:50 -04:00
|
|
|
setupFs: func(require *require.Assertions) afero.Fs { return afero.NewMemMapFs() },
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
ownerIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{verifyErr: someErr},
|
2022-05-04 02:50:50 -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)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
cmd := NewVerifyCmd()
|
2022-06-28 11:03:28 -04:00
|
|
|
cmd.Flags().String("config", "", "") // register persistent flag manually
|
2022-05-02 16:34:59 -04:00
|
|
|
out := &bytes.Buffer{}
|
2022-04-27 05:17:41 -04:00
|
|
|
cmd.SetOut(out)
|
2022-05-02 16:34:59 -04:00
|
|
|
cmd.SetErr(&bytes.Buffer{})
|
2022-05-13 05:56:43 -04:00
|
|
|
if tc.configFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("config", tc.configFlag))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
if tc.ownerIDFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("owner-id", tc.ownerIDFlag))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
if tc.clusterIDFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("cluster-id", tc.clusterIDFlag))
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-05-04 02:50:50 -04:00
|
|
|
if tc.nodeEndpointFlag != "" {
|
|
|
|
require.NoError(cmd.Flags().Set("node-endpoint", tc.nodeEndpointFlag))
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
fileHandler := file.NewHandler(tc.setupFs(require))
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-07-01 04:57:29 -04:00
|
|
|
if tc.idFile != nil {
|
2022-07-05 07:52:36 -04:00
|
|
|
require.NoError(fileHandler.WriteJSON(constants.ClusterIDsFileName, tc.idFile, file.OptNone))
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 05:19:03 -04:00
|
|
|
err := verify(cmd, tc.provider, fileHandler, tc.protoClient)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-04-26 10:54:05 -04:00
|
|
|
if tc.wantErr {
|
2022-03-22 11:03:15 -04:00
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
2022-04-27 05:17:41 -04:00
|
|
|
assert.Contains(out.String(), "OK")
|
2022-07-01 04:57:29 -04:00
|
|
|
assert.Equal(tc.wantEndpoint, tc.protoClient.endpoint)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestVerifyCompletion(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
2022-04-26 10:54:05 -04:00
|
|
|
args []string
|
|
|
|
toComplete string
|
|
|
|
wantResult []string
|
|
|
|
wantShellCD cobra.ShellCompDirective
|
2022-03-22 11:03:15 -04:00
|
|
|
}{
|
|
|
|
"first arg": {
|
2022-04-26 10:54:05 -04:00
|
|
|
args: []string{},
|
2022-04-27 05:17:41 -04:00
|
|
|
toComplete: "az",
|
|
|
|
wantResult: []string{"gcp", "azure"},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveNoFileComp,
|
|
|
|
},
|
|
|
|
"additional arg": {
|
2022-05-04 02:50:50 -04:00
|
|
|
args: []string{"gcp", "foo"},
|
2022-04-26 10:54:05 -04:00
|
|
|
wantResult: []string{},
|
|
|
|
wantShellCD: cobra.ShellCompDirectiveError,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
cmd := &cobra.Command{}
|
|
|
|
result, shellCD := verifyCompletion(cmd, tc.args, tc.toComplete)
|
2022-04-26 10:54:05 -04:00
|
|
|
assert.Equal(tc.wantResult, result)
|
|
|
|
assert.Equal(tc.wantShellCD, shellCD)
|
2022-03-22 11:03:15 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-06-28 11:03:28 -04:00
|
|
|
|
|
|
|
func TestVerifyClient(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
attestationDoc atls.FakeAttestationDoc
|
|
|
|
userData []byte
|
|
|
|
nonce []byte
|
|
|
|
attestationErr error
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
|
|
|
UserData: []byte("user data"),
|
|
|
|
Nonce: []byte("nonce"),
|
|
|
|
},
|
|
|
|
userData: []byte("user data"),
|
|
|
|
nonce: []byte("nonce"),
|
|
|
|
},
|
|
|
|
"attestation error": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
|
|
|
UserData: []byte("user data"),
|
|
|
|
Nonce: []byte("nonce"),
|
|
|
|
},
|
|
|
|
userData: []byte("user data"),
|
|
|
|
nonce: []byte("nonce"),
|
|
|
|
attestationErr: errors.New("error"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"user data does not match": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
|
|
|
UserData: []byte("wrong user data"),
|
|
|
|
Nonce: []byte("nonce"),
|
|
|
|
},
|
|
|
|
userData: []byte("user data"),
|
|
|
|
nonce: []byte("nonce"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"nonce does not match": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
|
|
|
UserData: []byte("user data"),
|
|
|
|
Nonce: []byte("wrong nonce"),
|
|
|
|
},
|
|
|
|
userData: []byte("user data"),
|
|
|
|
nonce: []byte("nonce"),
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
attestation, err := json.Marshal(tc.attestationDoc)
|
|
|
|
require.NoError(err)
|
|
|
|
verifyAPI := &stubVerifyAPI{
|
|
|
|
attestation: &verifyproto.GetAttestationResponse{Attestation: attestation},
|
|
|
|
attestationErr: tc.attestationErr,
|
|
|
|
}
|
|
|
|
|
|
|
|
netDialer := testdialer.NewBufconnDialer()
|
|
|
|
dialer := dialer.New(nil, nil, netDialer)
|
|
|
|
verifyServer := grpc.NewServer()
|
|
|
|
verifyproto.RegisterAPIServer(verifyServer, verifyAPI)
|
|
|
|
|
|
|
|
addr := net.JoinHostPort("192.0.2.1", strconv.Itoa(constants.VerifyServiceNodePortGRPC))
|
|
|
|
listener := netDialer.GetListener(addr)
|
|
|
|
go verifyServer.Serve(listener)
|
|
|
|
defer verifyServer.GracefulStop()
|
|
|
|
|
|
|
|
verifier := &constellationVerifier{dialer: dialer}
|
|
|
|
request := &verifyproto.GetAttestationRequest{
|
|
|
|
UserData: tc.userData,
|
|
|
|
Nonce: tc.nonce,
|
|
|
|
}
|
|
|
|
|
|
|
|
err = verifier.Verify(context.Background(), addr, request, atls.NewFakeValidator(oid.Dummy{}))
|
|
|
|
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubVerifyClient struct {
|
|
|
|
verifyErr error
|
2022-07-01 04:57:29 -04:00
|
|
|
endpoint string
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *stubVerifyClient) Verify(ctx context.Context, endpoint string, req *verifyproto.GetAttestationRequest, validator atls.Validator) error {
|
2022-07-01 04:57:29 -04:00
|
|
|
c.endpoint = endpoint
|
2022-06-28 11:03:28 -04:00
|
|
|
return c.verifyErr
|
|
|
|
}
|
|
|
|
|
|
|
|
type stubVerifyAPI struct {
|
|
|
|
attestation *verifyproto.GetAttestationResponse
|
|
|
|
attestationErr error
|
|
|
|
verifyproto.UnimplementedAPIServer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a stubVerifyAPI) GetAttestation(context.Context, *verifyproto.GetAttestationRequest) (*verifyproto.GetAttestationResponse, error) {
|
|
|
|
return a.attestation, a.attestationErr
|
|
|
|
}
|