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 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"
|
2023-04-03 09:06:27 -04:00
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-10-11 06:24:33 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/clusterid"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/atls"
|
2023-04-03 09:06:27 -04: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"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/cloud/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/config"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/constants"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/testdialer"
|
2022-11-21 11:02:33 -05:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/verify/verifyproto"
|
2022-04-27 05:17:41 -04:00
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"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 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 {
|
2023-08-04 07:53:51 -04:00
|
|
|
provider cloudprovider.Provider
|
|
|
|
protoClient *stubVerifyClient
|
|
|
|
formatter *stubAttDocFormatter
|
|
|
|
nodeEndpointFlag string
|
|
|
|
clusterIDFlag string
|
|
|
|
idFile *clusterid.File
|
|
|
|
wantEndpoint string
|
|
|
|
skipConfigCreation bool
|
|
|
|
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
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: 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",
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"azure": {
|
2022-05-04 02:50:50 -04:00
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: 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",
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
|
|
|
"default port": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-08-01 10:51:34 -04:00
|
|
|
wantEndpoint: "192.0.2.1:" + strconv.Itoa(constants.VerifyServiceNodePortGRPC),
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-07-01 04:57:29 -04:00
|
|
|
},
|
|
|
|
"endpoint not set": {
|
2022-09-11 09:58:31 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
clusterIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-09-11 09:58:31 -04:00
|
|
|
wantErr: true,
|
2022-07-01 04:57:29 -04:00
|
|
|
},
|
|
|
|
"endpoint from id file": {
|
2022-09-11 09:58:31 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
clusterIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
2022-10-11 06:24:33 -04:00
|
|
|
idFile: &clusterid.File{IP: "192.0.2.1"},
|
2022-09-11 09:58:31 -04:00
|
|
|
wantEndpoint: "192.0.2.1:" + strconv.Itoa(constants.VerifyServiceNodePortGRPC),
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-07-01 04:57:29 -04:00
|
|
|
},
|
|
|
|
"override endpoint from details file": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.2:1234",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: zeroBase64,
|
2022-07-01 04:57:29 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2022-10-11 06:24:33 -04:00
|
|
|
idFile: &clusterid.File{IP: "192.0.2.1"},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.2:1234",
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-05-04 02:50:50 -04:00
|
|
|
},
|
|
|
|
"invalid endpoint": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: ":::::",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{},
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
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
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-05-04 02:50:50 -04:00
|
|
|
wantErr: true,
|
2022-03-22 11:03:15 -04:00
|
|
|
},
|
2022-07-01 04:57:29 -04:00
|
|
|
"use owner id from id file": {
|
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
protoClient: &stubVerifyClient{},
|
2022-10-11 06:24:33 -04:00
|
|
|
idFile: &clusterid.File{OwnerID: zeroBase64},
|
2022-07-01 04:57:29 -04:00
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
2022-07-01 04:57:29 -04:00
|
|
|
},
|
2022-05-13 05:56:43 -04:00
|
|
|
"config file not existing": {
|
2023-08-04 07:53:51 -04:00
|
|
|
provider: cloudprovider.GCP,
|
|
|
|
clusterIDFlag: zeroBase64,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
formatter: &stubAttDocFormatter{},
|
|
|
|
skipConfigCreation: true,
|
|
|
|
wantErr: true,
|
2022-04-27 05:17:41 -04:00
|
|
|
},
|
|
|
|
"error protoClient GetState": {
|
2022-05-04 02:50:50 -04:00
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{verifyErr: rpcStatus.Error(codes.Internal, "failed")},
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
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
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
2022-09-11 09:58:31 -04:00
|
|
|
clusterIDFlag: zeroBase64,
|
2022-06-28 11:03:28 -04:00
|
|
|
protoClient: &stubVerifyClient{verifyErr: someErr},
|
2023-04-03 09:06:27 -04:00
|
|
|
formatter: &stubAttDocFormatter{},
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"format error": {
|
|
|
|
provider: cloudprovider.Azure,
|
|
|
|
nodeEndpointFlag: "192.0.2.1:1234",
|
|
|
|
clusterIDFlag: zeroBase64,
|
|
|
|
protoClient: &stubVerifyClient{},
|
|
|
|
wantEndpoint: "192.0.2.1:1234",
|
|
|
|
formatter: &stubAttDocFormatter{formatErr: 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()
|
2023-08-04 07:53:51 -04:00
|
|
|
cmd.Flags().String("workspace", "", "") // register persistent flag manually
|
|
|
|
cmd.Flags().Bool("force", true, "") // 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-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-08-29 08:18:05 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewMemMapFs())
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-08-04 07:53:51 -04:00
|
|
|
if !tc.skipConfigCreation {
|
|
|
|
cfg := defaultConfigWithExpectedMeasurements(t, config.Default(), tc.provider)
|
|
|
|
require.NoError(fileHandler.WriteYAML(constants.ConfigFilename, cfg))
|
|
|
|
}
|
2022-07-01 04:57:29 -04:00
|
|
|
if tc.idFile != nil {
|
2023-08-04 07:53:51 -04:00
|
|
|
require.NoError(fileHandler.WriteJSON(constants.ClusterIDsFilename, tc.idFile, file.OptNone))
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v := &verifyCmd{log: logger.NewTest(t)}
|
2023-06-06 04:32:22 -04:00
|
|
|
err := v.verify(cmd, fileHandler, tc.protoClient, tc.formatter, stubAttestationFetcher{})
|
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
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
type stubAttDocFormatter struct {
|
|
|
|
formatErr error
|
|
|
|
}
|
|
|
|
|
2023-08-04 11:48:14 -04:00
|
|
|
func (f *stubAttDocFormatter) format(_ context.Context, _ string, _ bool, _ bool, _ measurements.M, _ string) (string, error) {
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", f.formatErr
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormat(t *testing.T) {
|
|
|
|
formatter := func() *attestationDocFormatterImpl {
|
|
|
|
return &attestationDocFormatterImpl{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
|
|
|
formatter *attestationDocFormatterImpl
|
|
|
|
doc string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"invalid doc": {
|
|
|
|
formatter: formatter(),
|
|
|
|
doc: "invalid",
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-08-04 11:48:14 -04:00
|
|
|
_, err := tc.formatter.format(context.Background(), tc.doc, false, false, nil, "")
|
2023-04-03 09:06:27 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(t, err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseCerts(t *testing.T) {
|
|
|
|
formatter := func() *attestationDocFormatterImpl {
|
|
|
|
return &attestationDocFormatterImpl{
|
|
|
|
log: logger.NewTest(t),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
validCert := `-----BEGIN CERTIFICATE-----
|
|
|
|
MIIFTDCCAvugAwIBAgIBADBGBgkqhkiG9w0BAQowOaAPMA0GCWCGSAFlAwQCAgUA
|
|
|
|
oRwwGgYJKoZIhvcNAQEIMA0GCWCGSAFlAwQCAgUAogMCATCjAwIBATB7MRQwEgYD
|
|
|
|
VQQLDAtFbmdpbmVlcmluZzELMAkGA1UEBhMCVVMxFDASBgNVBAcMC1NhbnRhIENs
|
|
|
|
YXJhMQswCQYDVQQIDAJDQTEfMB0GA1UECgwWQWR2YW5jZWQgTWljcm8gRGV2aWNl
|
|
|
|
czESMBAGA1UEAwwJU0VWLU1pbGFuMB4XDTIyMTEyMzIyMzM0N1oXDTI5MTEyMzIy
|
|
|
|
MzM0N1owejEUMBIGA1UECwwLRW5naW5lZXJpbmcxCzAJBgNVBAYTAlVTMRQwEgYD
|
|
|
|
VQQHDAtTYW50YSBDbGFyYTELMAkGA1UECAwCQ0ExHzAdBgNVBAoMFkFkdmFuY2Vk
|
|
|
|
IE1pY3JvIERldmljZXMxETAPBgNVBAMMCFNFVi1WQ0VLMHYwEAYHKoZIzj0CAQYF
|
|
|
|
K4EEACIDYgAEVGm4GomfpkiziqEYP61nfKaz5OjDLr8Y0POrv4iAnFVHAmBT81Ms
|
|
|
|
gfSLKL5r3V3mNzl1Zh7jwSBft14uhGdwpARoK0YNQc4OvptqVIiv2RprV53DMzge
|
|
|
|
rtwiumIargiCo4IBFjCCARIwEAYJKwYBBAGceAEBBAMCAQAwFwYJKwYBBAGceAEC
|
|
|
|
BAoWCE1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1R9FNnlQCOGrkuVxpDwa2TbbvEEzQP7RXxotA\n\t\tKlxejvrY4VuK8agNqvffVofbdIIperK65K4+0mYIb+A6fU8QQHlCbti4ERSZ6UYD\n\t\tF/SjRih31+SAtWb42jueAA==\n\t\t-----END CERTIFICATE-----\n\tSome Cert (1):\n\t\tSerial Number: 0\n\t\tSubject: CN=SEV-VCEK,OU=Engineering,O=Advanced Micro Devices,L=Santa Clara,ST=CA,C=US\n\t\tIssuer: CN=SEV-Milan,OU=Engineering,O=Advanced Micro Devices,L=Santa Clara,ST=CA,C=US\n\t\tNot Before: 2022-11-23 22:33:47 +0000 UTC\n\t\tNot After: 2029-11-23 22:33:47 +0000 UTC\n\t\tSignature Algorithm: SHA384-RSAPSS\n\t\tPublic Key Algorithm: ECDSA\n"
|
|
|
|
|
|
|
|
testCases := map[string]struct {
|
2023-07-26 07:43:39 -04:00
|
|
|
formatter *attestationDocFormatterImpl
|
|
|
|
cert []byte
|
|
|
|
expected string
|
|
|
|
wantErr bool
|
2023-04-03 09:06:27 -04:00
|
|
|
}{
|
|
|
|
"one cert": {
|
2023-07-26 07:43:39 -04:00
|
|
|
formatter: formatter(),
|
|
|
|
cert: []byte(validCert),
|
|
|
|
expected: validCertExpected,
|
2023-04-03 09:06:27 -04:00
|
|
|
},
|
|
|
|
"invalid cert": {
|
2023-07-26 07:43:39 -04:00
|
|
|
formatter: formatter(),
|
|
|
|
cert: []byte("invalid"),
|
|
|
|
wantErr: true,
|
2023-04-03 09:06:27 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
b := &strings.Builder{}
|
2023-07-26 07:43:39 -04:00
|
|
|
err := tc.formatter.parseCerts(b, "Some Cert", tc.cert)
|
2023-04-03 09:06:27 -04:00
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
assert.Equal(tc.expected, b.String())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
func TestVerifyClient(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
attestationDoc atls.FakeAttestationDoc
|
|
|
|
nonce []byte
|
|
|
|
attestationErr error
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"success": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
2023-01-17 09:28:07 -05:00
|
|
|
UserData: []byte(constants.ConstellationVerifyServiceUserData),
|
2022-06-28 11:03:28 -04:00
|
|
|
Nonce: []byte("nonce"),
|
|
|
|
},
|
2023-01-17 09:28:07 -05:00
|
|
|
nonce: []byte("nonce"),
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
|
|
|
"attestation error": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
2023-01-17 09:28:07 -05:00
|
|
|
UserData: []byte(constants.ConstellationVerifyServiceUserData),
|
2022-06-28 11:03:28 -04:00
|
|
|
Nonce: []byte("nonce"),
|
|
|
|
},
|
|
|
|
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"),
|
|
|
|
},
|
2023-01-17 09:28:07 -05:00
|
|
|
nonce: []byte("nonce"),
|
|
|
|
wantErr: true,
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
|
|
|
"nonce does not match": {
|
|
|
|
attestationDoc: atls.FakeAttestationDoc{
|
2023-01-17 09:28:07 -05:00
|
|
|
UserData: []byte(constants.ConstellationVerifyServiceUserData),
|
2022-06-28 11:03:28 -04:00
|
|
|
Nonce: []byte("wrong nonce"),
|
|
|
|
},
|
2023-01-17 09:28:07 -05:00
|
|
|
nonce: []byte("nonce"),
|
|
|
|
wantErr: true,
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
verifier := &constellationVerifier{dialer: dialer, log: logger.NewTest(t)}
|
2022-06-28 11:03:28 -04:00
|
|
|
request := &verifyproto.GetAttestationRequest{
|
2023-01-17 09:28:07 -05:00
|
|
|
Nonce: tc.nonce,
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
_, err = verifier.Verify(context.Background(), addr, request, atls.NewFakeValidator(variant.Dummy{}))
|
2022-06-28 11:03:28 -04:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-03 09:06:27 -04:00
|
|
|
func (c *stubVerifyClient) Verify(_ context.Context, endpoint string, _ *verifyproto.GetAttestationRequest, _ atls.Validator) (string, error) {
|
2022-07-01 04:57:29 -04:00
|
|
|
c.endpoint = endpoint
|
2023-04-03 09:06:27 -04:00
|
|
|
return "", c.verifyErr
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
|
|
|
|
func TestAddPortIfMissing(t *testing.T) {
|
|
|
|
testCases := map[string]struct {
|
|
|
|
endpoint string
|
|
|
|
defaultPort int
|
|
|
|
wantResult string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
|
|
|
"ip and port": {
|
|
|
|
endpoint: "192.0.2.1:2",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantResult: "192.0.2.1:2",
|
|
|
|
},
|
|
|
|
"hostname and port": {
|
|
|
|
endpoint: "foo:2",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantResult: "foo:2",
|
|
|
|
},
|
|
|
|
"ip": {
|
|
|
|
endpoint: "192.0.2.1",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantResult: "192.0.2.1:3",
|
|
|
|
},
|
|
|
|
"hostname": {
|
|
|
|
endpoint: "foo",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantResult: "foo:3",
|
|
|
|
},
|
|
|
|
"empty endpoint": {
|
|
|
|
endpoint: "",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
"invalid endpoint": {
|
|
|
|
endpoint: "foo:2:2",
|
|
|
|
defaultPort: 3,
|
|
|
|
wantErr: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, tc := range testCases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
require := require.New(t)
|
|
|
|
|
|
|
|
res, err := addPortIfMissing(tc.endpoint, tc.defaultPort)
|
|
|
|
if tc.wantErr {
|
|
|
|
assert.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(err)
|
|
|
|
assert.Equal(tc.wantResult, res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|