Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: switch back to human readable version information #776

Merged
merged 2 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 55 additions & 10 deletions cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package main

import (
"context"
"encoding/json"
"fmt"
"os"
"os/signal"
Expand All @@ -15,6 +14,7 @@ import (
"github.com/edgelesssys/contrast/cli/cmd"
"github.com/edgelesssys/contrast/internal/constants"
"github.com/edgelesssys/contrast/internal/manifest"
"github.com/edgelesssys/contrast/internal/platforms"
"github.com/spf13/cobra"
)

Expand All @@ -25,16 +25,21 @@ func main() {
}

func execute() error {
cmd := newRootCmd()
cmd, err := newRootCmd()
if err != nil {
return fmt.Errorf("create root cmd: %w", err)
}

ctx, cancel := signalContext(context.Background(), os.Interrupt)
defer cancel()
return cmd.ExecuteContext(ctx)
}

func buildVersionString() string {
func buildVersionString() (string, error) {
var versionsBuilder strings.Builder
versionsWriter := tabwriter.NewWriter(&versionsBuilder, 0, 0, 4, ' ', 0)
fmt.Fprintf(versionsWriter, "%s\n\n", constants.Version)

fmt.Fprintf(versionsWriter, "container image versions:\n")
imageReplacements := strings.Trim(string(cmd.ReleaseImageReplacements), "\n")
for _, image := range strings.Split(imageReplacements, "\n") {
Expand All @@ -43,20 +48,60 @@ func buildVersionString() string {
fmt.Fprintf(versionsWriter, "\t%s\n", image)
}
}
if refValues, err := json.MarshalIndent(manifest.GetEmbeddedReferenceValues(), "\t", " "); err == nil {
fmt.Fprintf(versionsWriter, "embedded reference values:\t%s\n", refValues)

embeddedReferenceValues := manifest.GetEmbeddedReferenceValues()
for _, platform := range platforms.All() {
fmt.Fprintf(versionsWriter, "\nreference values for %s platform:\n", platform.String())

runtimeHandlerName, err := manifest.RuntimeHandler(platform)
if err != nil {
return "", fmt.Errorf("getting runtime handler name: %w", err)
}
fmt.Fprintf(versionsWriter, "\truntime handler:\t%s\n", runtimeHandlerName)

values, err := embeddedReferenceValues.ForPlatform(platform)
if err != nil {
return "", fmt.Errorf("getting reference values: %w", err)
}
// Make sure that there's only one set of reference values. If this is
// not the case, the output might be confusing.
if len(values.SNP)+len(values.TDX) != 1 {
return "", fmt.Errorf("platform %s doesn't have exactly one reference value", platform.String())
}
for _, snp := range values.SNP {
fmt.Fprintf(versionsWriter, "\tlaunch digest:\t%s\n", snp.TrustedMeasurement.String())
fmt.Fprint(versionsWriter, "\tdefault SNP TCB:\t\n")
fmt.Fprintf(versionsWriter, "\t bootloader:\t%d\n", snp.MinimumTCB.BootloaderVersion.UInt8())
fmt.Fprintf(versionsWriter, "\t tee:\t%d\n", snp.MinimumTCB.TEEVersion.UInt8())
fmt.Fprintf(versionsWriter, "\t snp:\t%d\n", snp.MinimumTCB.SNPVersion.UInt8())
fmt.Fprintf(versionsWriter, "\t microcode:\t%d\n", snp.MinimumTCB.MicrocodeVersion.UInt8())
}
for _, tdx := range values.TDX {
fmt.Fprintf(versionsWriter, "\tlaunch digest:\t%s\n", tdx.TrustedMeasurement.String())
}

switch platform {
case platforms.AKSCloudHypervisorSNP:
fmt.Fprintf(versionsWriter, "\tgenpolicy version:\t%s\n", constants.MicrosoftGenpolicyVersion)
case platforms.K3sQEMUSNP, platforms.K3sQEMUTDX, platforms.RKE2QEMUTDX:
fmt.Fprintf(versionsWriter, "\tgenpolicy version:\t%s\n", constants.KataGenpolicyVersion)
}
}
fmt.Fprintf(versionsWriter, "genpolicy version:\t%s\n", constants.GenpolicyVersion)

versionsWriter.Flush()
return versionsBuilder.String()
return versionsBuilder.String(), nil
}

func newRootCmd() *cobra.Command {
func newRootCmd() (*cobra.Command, error) {
version, err := buildVersionString()
if err != nil {
return nil, fmt.Errorf("build version string: %w", err)
}
root := &cobra.Command{
Use: "contrast",
Short: "contrast",
PersistentPreRun: preRunRoot,
Version: buildVersionString(),
Version: version,
}
root.SetOut(os.Stdout)

Expand All @@ -71,7 +116,7 @@ func newRootCmd() *cobra.Command {
cmd.NewRecoverCmd(),
)

return root
return root, nil
}

// signalContext returns a context that is canceled on the handed signal.
Expand Down
5 changes: 3 additions & 2 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package constants

// Version value is injected at build time.
var (
Version = "0.0.0-dev"
GenpolicyVersion = "0.0.0-dev"
Version = "0.0.0-dev"
MicrosoftGenpolicyVersion = "0.0.0-dev"
KataGenpolicyVersion = "0.0.0-dev"
)
3 changes: 2 additions & 1 deletion packages/by-name/contrast/package.nix
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ buildGoModule rec {
"-s"
"-w"
"-X github.com/edgelesssys/contrast/internal/constants.Version=${version}"
"-X github.com/edgelesssys/contrast/internal/constants.GenpolicyVersion=${genpolicy.version}"
"-X github.com/edgelesssys/contrast/internal/constants.MicrosoftGenpolicyVersion=${genpolicy.version}"
"-X github.com/edgelesssys/contrast/internal/constants.KataGenpolicyVersion=${kata.genpolicy.version}"
];

preCheck = ''
Expand Down