Skip to content

Commit

Permalink
cli: optionally validate coordinator in set and verify
Browse files Browse the repository at this point in the history
  • Loading branch information
burgerdev committed Jan 30, 2024
1 parent 96a5972 commit c20f5ba
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
14 changes: 13 additions & 1 deletion cli/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -41,6 +42,8 @@ func newSetCmd() *cobra.Command {
cmd.Flags().StringP("manifest", "m", manifestFilename, "path to manifest (.json) file")
cmd.Flags().StringP("coordinator", "c", "", "endpoint the coordinator can be reached at")
must(cobra.MarkFlagRequired(cmd.Flags(), "coordinator"))
// TODO(burgerdev): default --policy should be derived from released artifacts.
cmd.Flags().String("policy", "", "expected policy hash of the coordinator (64 hex-encoded bytes, will not be checked if empty)")

return cmd
}
Expand Down Expand Up @@ -84,7 +87,7 @@ func runSet(cmd *cobra.Command, args []string) error {
}
log.Debug("Using KDS cache dir", "dir", kdsDir)

validateOptsGen := newCoordinatorValidateOptsGen()
validateOptsGen := newCoordinatorValidateOptsGen(flags.policy)
kdsCache := fsstore.New(kdsDir, log.WithGroup("kds-cache"))
kdsGetter := snp.NewCachedHTTPSGetter(kdsCache, snp.NeverGCTicker, log.WithGroup("kds-getter"))
validator := snp.NewValidator(validateOptsGen, kdsGetter, log.WithGroup("snp-validator"))
Expand Down Expand Up @@ -122,6 +125,7 @@ func runSet(cmd *cobra.Command, args []string) error {
type setFlags struct {
manifestPath string
coordinator string
policy []byte
}

func parseSetFlags(cmd *cobra.Command) (*setFlags, error) {
Expand All @@ -136,6 +140,14 @@ func parseSetFlags(cmd *cobra.Command) (*setFlags, error) {
if err != nil {
return nil, fmt.Errorf("failed to get coordinator flag: %w", err)
}
policyString, err := cmd.Flags().GetString("policy")
if err != nil {
return nil, fmt.Errorf("failed to get policy flag: %w", err)
}
flags.policy, err = hex.DecodeString(policyString)
if err != nil {
return nil, fmt.Errorf("hex-decoding policy flag: %w", err)
}

return flags, nil
}
Expand Down
18 changes: 16 additions & 2 deletions cli/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"crypto/sha256"
"encoding/hex"
"fmt"
"net"
"os"
Expand Down Expand Up @@ -40,6 +41,8 @@ func newVerifyCmd() *cobra.Command {
cmd.Flags().StringP("output", "o", verifyDir, "directory to write files to")
cmd.Flags().StringP("coordinator", "c", "", "endpoint the coordinator can be reached at")
must(cobra.MarkFlagRequired(cmd.Flags(), "coordinator"))
// TODO(burgerdev): default --policy should be derived from released artifacts.
cmd.Flags().String("policy", "", "expected policy hash of the coordinator (64 hex-encoded bytes, will not be checked if empty)")

return cmd
}
Expand All @@ -62,7 +65,7 @@ func runVerify(cmd *cobra.Command, _ []string) error {
}
log.Debug("Using KDS cache dir", "dir", kdsDir)

validateOptsGen := newCoordinatorValidateOptsGen()
validateOptsGen := newCoordinatorValidateOptsGen(flags.policy)
kdsCache := fsstore.New(kdsDir, log.WithGroup("kds-cache"))
kdsGetter := snp.NewCachedHTTPSGetter(kdsCache, snp.NeverGCTicker, log.WithGroup("kds-getter"))
validator := snp.NewValidator(validateOptsGen, kdsGetter, log.WithGroup("snp-validator"))
Expand Down Expand Up @@ -107,6 +110,7 @@ func runVerify(cmd *cobra.Command, _ []string) error {
type verifyFlags struct {
coordinator string
outputDir string
policy []byte
}

func parseVerifyFlags(cmd *cobra.Command) (*verifyFlags, error) {
Expand All @@ -118,14 +122,23 @@ func parseVerifyFlags(cmd *cobra.Command) (*verifyFlags, error) {
if err != nil {
return nil, err
}
policyString, err := cmd.Flags().GetString("policy")
if err != nil {
return nil, err
}
policy, err := hex.DecodeString(policyString)
if err != nil {
return nil, fmt.Errorf("hex-decoding policy flag: %w", err)
}

return &verifyFlags{
coordinator: coordinator,
outputDir: outputDir,
policy: policy,
}, nil
}

func newCoordinatorValidateOptsGen() *snp.StaticValidateOptsGenerator {
func newCoordinatorValidateOptsGen(hostData []byte) *snp.StaticValidateOptsGenerator {
defaultManifest := manifest.Default()
trustedIDKeyDigests, err := (&defaultManifest.ReferenceValues.SNP.TrustedIDKeyHashes).ByteSlices()
if err != nil {
Expand All @@ -134,6 +147,7 @@ func newCoordinatorValidateOptsGen() *snp.StaticValidateOptsGenerator {

return &snp.StaticValidateOptsGenerator{
Opts: &validate.Options{
HostData: hostData,
GuestPolicy: abi.SnpPolicy{
Debug: false,
SMT: true,
Expand Down
2 changes: 2 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,11 @@ set:
trap "kill $PID" EXIT
nix run .#wait-for-port-listen -- 1313
t=$(date +%s)
policy=$(nix run .#get-coordinator-policy -- ./{{ workspace_dir }}/manifest.json)
nix run .#cli -- set \
-m ./{{ workspace_dir }}/manifest.json \
-c localhost:1313 \
--policy "${policy}" \
./{{ workspace_dir }}/deployment/*.yml
duration=$(( $(date +%s) - $t ))
echo "Set manifest in $duration seconds."
Expand Down
9 changes: 9 additions & 0 deletions packages/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,13 @@ rec {
exit 1
'';
};

get-coordinator-policy = writeShellApplication {
name = "get-coordinator-policy";
runtimeInputs = [ jq ];
text = ''
set -u
jq -r <"$1" '.Policies | to_entries[] | select(.value[] | startswith("coordinator.")) | .key'
'';
};
}

0 comments on commit c20f5ba

Please sign in to comment.