2022-03-22 11:03:15 -04:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
"github.com/edgelesssys/constellation/cli/cloud/cloudcmd"
|
|
|
|
"github.com/edgelesssys/constellation/cli/cloudprovider"
|
|
|
|
"github.com/edgelesssys/constellation/cli/file"
|
|
|
|
"github.com/edgelesssys/constellation/cli/proto"
|
|
|
|
"github.com/edgelesssys/constellation/internal/config"
|
|
|
|
"github.com/spf13/afero"
|
2022-03-22 11:03:15 -04:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
rpcStatus "google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newVerifyCmd() *cobra.Command {
|
|
|
|
cmd := &cobra.Command{
|
2022-04-27 05:17:41 -04:00
|
|
|
Use: "verify {azure|gcp} IP PORT",
|
2022-03-22 11:03:15 -04:00
|
|
|
Short: "Verify the confidential properties of your Constellation.",
|
|
|
|
Long: "Verify the confidential properties of your Constellation.",
|
2022-04-27 05:17:41 -04:00
|
|
|
Args: cobra.MatchAll(
|
|
|
|
cobra.ExactArgs(3),
|
|
|
|
isCloudProvider(0),
|
|
|
|
isIP(1),
|
|
|
|
isPort(2),
|
|
|
|
),
|
|
|
|
RunE: runVerify,
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
cmd.Flags().String("owner-id", "", "verify the Constellation using the owner identity derived from the master secret.")
|
|
|
|
cmd.Flags().String("unique-id", "", "verify the Constellation using the unique cluster identity.")
|
2022-03-22 11:03:15 -04:00
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
func runVerify(cmd *cobra.Command, args []string) error {
|
|
|
|
provider := cloudprovider.FromString(args[0])
|
|
|
|
ip := args[1]
|
|
|
|
port := args[2]
|
|
|
|
fileHandler := file.NewHandler(afero.NewOsFs())
|
|
|
|
protoClient := &proto.Client{}
|
|
|
|
defer protoClient.Close()
|
|
|
|
return verify(cmd.Context(), cmd, provider, ip, port, fileHandler, protoClient)
|
|
|
|
}
|
|
|
|
|
|
|
|
func verify(ctx context.Context, cmd *cobra.Command, provider cloudprovider.Provider, ip, port string, fileHandler file.Handler, protoClient protoClient) error {
|
|
|
|
flags, err := parseVerifyFlags(cmd)
|
|
|
|
if err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
config, err := config.FromFile(fileHandler, flags.devConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
validators, err := cloudcmd.NewValidators(provider, config)
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
if err := validators.UpdateInitPCRs(flags.ownerID, flags.clusterID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if validators.Warnings() != "" {
|
|
|
|
cmd.Print(validators.Warnings())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := protoClient.Connect(ip, port, validators.V()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := protoClient.GetState(ctx); err != nil {
|
2022-03-22 11:03:15 -04:00
|
|
|
if err, ok := rpcStatus.FromError(err); ok {
|
|
|
|
return fmt.Errorf("unable to verify Constellation cluster: %s", err.Message())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2022-04-27 05:17:41 -04:00
|
|
|
|
|
|
|
cmd.Println("OK")
|
2022-03-22 11:03:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
func parseVerifyFlags(cmd *cobra.Command) (verifyFlags, error) {
|
2022-03-22 11:03:15 -04:00
|
|
|
ownerID, err := cmd.Flags().GetString("owner-id")
|
|
|
|
if err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
clusterID, err := cmd.Flags().GetString("unique-id")
|
|
|
|
if err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
if ownerID == "" && clusterID == "" {
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{}, errors.New("neither owner ID nor unique ID provided to verify the Constellation")
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
devConfigPath, err := cmd.Flags().GetString("dev-config")
|
2022-03-22 11:03:15 -04:00
|
|
|
if err != nil {
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{}, err
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
return verifyFlags{
|
|
|
|
devConfigPath: devConfigPath,
|
|
|
|
ownerID: ownerID,
|
|
|
|
clusterID: clusterID,
|
|
|
|
}, nil
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
2022-04-27 05:17:41 -04:00
|
|
|
type verifyFlags struct {
|
|
|
|
ownerID string
|
|
|
|
clusterID string
|
|
|
|
devConfigPath string
|
2022-03-22 11:03:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// verifyCompletion handels the completion of CLI arguments. It is frequently called
|
|
|
|
// while the user types arguments of the command to suggest completion.
|
|
|
|
func verifyCompletion(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
|
|
switch len(args) {
|
2022-04-27 05:17:41 -04:00
|
|
|
case 0:
|
|
|
|
return []string{"gcp", "azure"}, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
case 1, 2:
|
2022-03-22 11:03:15 -04:00
|
|
|
return []string{}, cobra.ShellCompDirectiveNoFileComp
|
|
|
|
default:
|
|
|
|
return []string{}, cobra.ShellCompDirectiveError
|
|
|
|
}
|
|
|
|
}
|