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 (
|
2022-06-28 11:03:28 -04:00
|
|
|
"bytes"
|
|
|
|
"context"
|
2022-03-22 11:03:15 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2022-06-28 11:03:28 -04:00
|
|
|
"net"
|
2022-07-29 02:24:13 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/cli/internal/cloudcmd"
|
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"
|
2022-11-15 09:40:49 -05: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/crypto"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/file"
|
|
|
|
"github.com/edgelesssys/constellation/v2/internal/grpc/dialer"
|
|
|
|
"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/spf13/cobra"
|
2022-06-28 11:03:28 -04:00
|
|
|
"google.golang.org/grpc"
|
2022-03-22 11:03:15 -04:00
|
|
|
)
|
|
|
|
|
2022-06-08 02:14:28 -04:00
|
|
|
// NewVerifyCmd returns a new cobra.Command for the verify command.
|
|
|
|
func NewVerifyCmd() *cobra.Command {
|
2022-03-22 11:03:15 -04:00
|
|
|
cmd := &cobra.Command{
|
2022-09-07 09:38:29 -04:00
|
|
|
Use: "verify",
|
2022-05-09 11:02:47 -04:00
|
|
|
Short: "Verify the confidential properties of a Constellation cluster",
|
2022-07-01 04:57:29 -04:00
|
|
|
Long: `Verify the confidential properties of a Constellation cluster.
|
|
|
|
|
2022-07-05 07:52:36 -04:00
|
|
|
If arguments aren't specified, values are read from ` + "`" + constants.ClusterIDsFileName + "`.",
|
2022-12-07 05:48:54 -05:00
|
|
|
Args: cobra.ExactArgs(0),
|
2022-04-27 05:17:41 -04:00
|
|
|
RunE: runVerify,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-09-11 09:58:31 -04:00
|
|
|
cmd.Flags().String("cluster-id", "", "expected cluster identifier")
|
2022-07-01 04:57:29 -04:00
|
|
|
cmd.Flags().StringP("node-endpoint", "e", "", "endpoint of the node to verify, passed as HOST[:PORT]")
|
2022-03-22 11:03:15 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
type verifyCmd struct {
|
|
|
|
log debugLog
|
|
|
|
}
|
|
|
|
|
2023-03-20 06:03:36 -04:00
|
|
|
func runVerify(cmd *cobra.Command, _ []string) error {
|
2022-11-21 11:02:33 -05:00
|
|
|
log, err := newCLILogger(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating logger: %w", err)
|
|
|
|
}
|
|
|
|
defer log.Sync()
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
2022-11-21 11:02:33 -05:00
|
|
|
verifyClient := &constellationVerifier{
|
|
|
|
dialer: dialer.New(nil, nil, &net.Dialer{}),
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
v := &verifyCmd{log: log}
|
|
|
|
return v.verify(cmd, fileHandler, verifyClient)
|
2022-04-27 05:17:41 -04:00
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
func (v *verifyCmd) verify(cmd *cobra.Command, fileHandler file.Handler, verifyClient verifyClient) error {
|
|
|
|
flags, err := v.parseVerifyFlags(cmd, fileHandler)
|
2022-04-27 05:17:41 -04:00
|
|
|
if err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Using flags: %+v", flags)
|
2022-03-22 11:03:15 -04:00
|
|
|
|
2023-01-18 07:10:24 -05:00
|
|
|
v.log.Debugf("Loading configuration file from %q", flags.configPath)
|
2023-01-31 05:45:31 -05:00
|
|
|
conf, err := config.New(fileHandler, flags.configPath, flags.force)
|
2023-02-07 06:56:25 -05:00
|
|
|
var configValidationErr *config.ValidationError
|
|
|
|
if errors.As(err, &configValidationErr) {
|
|
|
|
cmd.PrintErrln(configValidationErr.LongMessage())
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
if err != nil {
|
2023-02-07 06:56:25 -05:00
|
|
|
return err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
v.log.Debugf("Creating aTLS Validator for %s", conf.AttestationVariant)
|
|
|
|
validators, err := cloudcmd.NewValidator(conf, flags.maaURL, v.log)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Updating expected PCRs")
|
2022-04-27 05:17:41 -04:00
|
|
|
if err := validators.UpdateInitPCRs(flags.ownerID, flags.clusterID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-26 04:58:39 -04:00
|
|
|
nonce, err := crypto.GenerateRandomBytes(32)
|
2022-06-28 11:03:28 -04:00
|
|
|
if err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Generated random nonce: %x", nonce)
|
2022-06-28 11:03:28 -04:00
|
|
|
|
|
|
|
if err := verifyClient.Verify(
|
|
|
|
cmd.Context(),
|
|
|
|
flags.endpoint,
|
|
|
|
&verifyproto.GetAttestationRequest{
|
2023-01-17 09:28:07 -05:00
|
|
|
Nonce: nonce,
|
2022-06-28 11:03:28 -04:00
|
|
|
},
|
2022-08-12 09:59:45 -04:00
|
|
|
validators.V(cmd),
|
2022-06-28 11:03:28 -04:00
|
|
|
); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
|
|
|
cmd.Println("OK")
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
func (v *verifyCmd) parseVerifyFlags(cmd *cobra.Command, fileHandler file.Handler) (verifyFlags, error) {
|
2022-07-01 04:57:29 -04:00
|
|
|
configPath, err := cmd.Flags().GetString("config")
|
|
|
|
if err != nil {
|
|
|
|
return verifyFlags{}, fmt.Errorf("parsing config path argument: %w", err)
|
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
v.log.Debugf("Flag 'config' set to %q", configPath)
|
2022-11-21 11:02:33 -05:00
|
|
|
|
2022-09-11 09:58:31 -04:00
|
|
|
ownerID := ""
|
2022-07-26 04:58:39 -04:00
|
|
|
clusterID, err := cmd.Flags().GetString("cluster-id")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-07-26 04:58:39 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("parsing cluster-id argument: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
v.log.Debugf("Flag 'cluster-id' set to %q", clusterID)
|
2022-11-21 11:02:33 -05:00
|
|
|
|
2022-05-04 02:50:50 -04:00
|
|
|
endpoint, err := cmd.Flags().GetString("node-endpoint")
|
|
|
|
if err != nil {
|
2022-06-09 10:10:42 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("parsing node-endpoint argument: %w", err)
|
2022-05-04 02:50:50 -04:00
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
v.log.Debugf("Flag 'node-endpoint' set to %q", endpoint)
|
2022-05-04 02:50:50 -04:00
|
|
|
|
2023-01-31 05:45:31 -05:00
|
|
|
force, err := cmd.Flags().GetBool("force")
|
|
|
|
if err != nil {
|
|
|
|
return verifyFlags{}, fmt.Errorf("parsing force argument: %w", err)
|
|
|
|
}
|
2023-01-31 06:12:19 -05:00
|
|
|
v.log.Debugf("Flag 'force' set to %t", force)
|
2023-01-31 05:45:31 -05:00
|
|
|
|
2023-03-21 07:46:49 -04:00
|
|
|
var idFile clusterid.File
|
|
|
|
if err := fileHandler.ReadJSON(constants.ClusterIDsFileName, &idFile); err != nil && !errors.Is(err, afero.ErrFileNotFound) {
|
|
|
|
return verifyFlags{}, fmt.Errorf("reading cluster ID file: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-09-11 09:51:38 -04:00
|
|
|
// Get empty values from ID file
|
2022-07-01 04:57:29 -04:00
|
|
|
emptyEndpoint := endpoint == ""
|
|
|
|
emptyIDs := ownerID == "" && clusterID == ""
|
2022-09-11 09:51:38 -04:00
|
|
|
if emptyEndpoint || emptyIDs {
|
2023-01-18 07:10:24 -05:00
|
|
|
v.log.Debugf("Trying to supplement empty flag values from %q", constants.ClusterIDsFileName)
|
2023-03-21 07:46:49 -04:00
|
|
|
if emptyEndpoint {
|
|
|
|
cmd.Printf("Using endpoint from %q. Specify --node-endpoint to override this.\n", constants.ClusterIDsFileName)
|
|
|
|
endpoint = idFile.IP
|
|
|
|
}
|
|
|
|
if emptyIDs {
|
|
|
|
cmd.Printf("Using ID from %q. Specify --cluster-id to override this.\n", constants.ClusterIDsFileName)
|
|
|
|
ownerID = idFile.OwnerID
|
|
|
|
clusterID = idFile.ClusterID
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate
|
|
|
|
if ownerID == "" && clusterID == "" {
|
2022-09-11 09:58:31 -04:00
|
|
|
return verifyFlags{}, errors.New("cluster-id not provided to verify the cluster")
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
endpoint, err = addPortIfMissing(endpoint, constants.VerifyServiceNodePortGRPC)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-07-01 04:57:29 -04:00
|
|
|
return verifyFlags{}, fmt.Errorf("validating endpoint argument: %w", err)
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{
|
2022-05-13 05:56:43 -04:00
|
|
|
endpoint: endpoint,
|
|
|
|
configPath: configPath,
|
|
|
|
ownerID: ownerID,
|
|
|
|
clusterID: clusterID,
|
2023-03-21 07:46:49 -04:00
|
|
|
maaURL: idFile.AttestationURL,
|
2023-01-31 05:45:31 -05:00
|
|
|
force: force,
|
2022-04-27 05:17:41 -04:00
|
|
|
}, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
type verifyFlags struct {
|
2022-05-13 05:56:43 -04:00
|
|
|
endpoint string
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
configPath string
|
2023-03-21 07:46:49 -04:00
|
|
|
maaURL string
|
2023-01-31 05:45:31 -05:00
|
|
|
force bool
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-07-29 02:24:13 -04:00
|
|
|
func addPortIfMissing(endpoint string, defaultPort int) (string, error) {
|
|
|
|
if endpoint == "" {
|
|
|
|
return "", errors.New("endpoint is empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err := net.SplitHostPort(endpoint)
|
|
|
|
if err == nil {
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return net.JoinHostPort(endpoint, strconv.Itoa(defaultPort)), nil
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
2022-07-29 02:24:13 -04:00
|
|
|
|
|
|
|
return "", err
|
2022-07-01 04:57:29 -04:00
|
|
|
}
|
|
|
|
|
2022-06-28 11:03:28 -04:00
|
|
|
type constellationVerifier struct {
|
2022-06-28 12:33:27 -04:00
|
|
|
dialer grpcInsecureDialer
|
2022-11-21 11:02:33 -05:00
|
|
|
log debugLog
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify retrieves an attestation statement from the Constellation and verifies it using the validator.
|
|
|
|
func (v *constellationVerifier) Verify(
|
|
|
|
ctx context.Context, endpoint string, req *verifyproto.GetAttestationRequest, validator atls.Validator,
|
|
|
|
) error {
|
2023-01-18 07:10:24 -05:00
|
|
|
v.log.Debugf("Dialing endpoint: %q", endpoint)
|
2022-06-28 11:03:28 -04:00
|
|
|
conn, err := v.dialer.DialInsecure(ctx, endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("dialing init server: %w", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
client := verifyproto.NewAPIClient(conn)
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Sending attestation request")
|
2022-06-28 11:03:28 -04:00
|
|
|
resp, err := client.GetAttestation(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting attestation: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-21 11:02:33 -05:00
|
|
|
v.log.Debugf("Verifying attestation")
|
2023-03-29 03:06:10 -04:00
|
|
|
signedData, err := validator.Validate(ctx, resp.Attestation, req.Nonce)
|
2022-06-28 11:03:28 -04:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("validating attestation: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-01-17 09:28:07 -05:00
|
|
|
if !bytes.Equal(signedData, []byte(constants.ConstellationVerifyServiceUserData)) {
|
|
|
|
return errors.New("signed data in attestation does not match expected user data")
|
2022-06-28 11:03:28 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type verifyClient interface {
|
|
|
|
Verify(ctx context.Context, endpoint string, req *verifyproto.GetAttestationRequest, validator atls.Validator) error
|
|
|
|
}
|
|
|
|
|
2022-06-28 12:33:27 -04:00
|
|
|
type grpcInsecureDialer interface {
|
2022-06-28 11:03:28 -04:00
|
|
|
DialInsecure(ctx context.Context, endpoint string) (conn *grpc.ClientConn, err error)
|
|
|
|
}
|