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

add flag to show sbom files during a package inspect command #678

Merged
merged 5 commits into from
Aug 22, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ zarf package inspect [PACKAGE] [flags]

```
-h, --help help for inspect
-s, --sbom View SBOM contents while inspecting the package.
--tmpdir string Specify the temporary directory to use for intermediate files
```

Expand All @@ -29,4 +30,3 @@ zarf package inspect [PACKAGE] [flags]
### SEE ALSO

* [zarf package](zarf_package.md) - Zarf package commands for creating, deploying, and inspecting packages

1 change: 1 addition & 0 deletions src/cmd/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,5 @@ func init() {
packageDeployCmd.Flags().StringVar(&config.DeployOptions.SGetKeyPath, "sget", "", "Path to public sget key file for remote packages signed via cosign")

packageInspectCmd.Flags().StringVar(&config.CommonOptions.TempDirectory, "tmpdir", "", "Specify the temporary directory to use for intermediate files")
packageInspectCmd.Flags().BoolVarP(&packager.ViewSBOM, "sbom", "s", false, "View SBOM contents while inspecting the package.")
YrrepNoj marked this conversation as resolved.
Show resolved Hide resolved
}
29 changes: 29 additions & 0 deletions src/internal/packager/inspect.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package packager

import (
"fmt"
"io/ioutil"
"path/filepath"

"github.com/AlecAivazis/survey/v2"
"github.com/defenseunicorns/zarf/src/config"
"github.com/defenseunicorns/zarf/src/internal/message"
"github.com/defenseunicorns/zarf/src/internal/utils"
"github.com/mholt/archiver/v3"
)

// ViewSBOM indicates if image SBOM information should be displayed when inspecting a package
var ViewSBOM bool

// Inspect list the contents of a package
func Inspect(packageName string) {
tempPath := createPaths()
Expand Down Expand Up @@ -39,4 +44,28 @@ func Inspect(packageName string) {
}

message.Infof("The package was built with Zarf CLI version %s\n", config.GetBuildData().Version)

if ViewSBOM {
err = archiver.Extract(packageName, "sboms", tempPath.base)
if err != nil {
message.Fatalf(err, "Unable to extract sbom information from the package.")
}

sbomViewFiles, _ := filepath.Glob(tempPath.sboms + "/sbom-viewer-*")
if len(sbomViewFiles) > 1 {
link := sbomViewFiles[0]
msg := fmt.Sprintf("This package has %d images with software bill-of-materials (SBOM) included. You can view them now in the zarf-sbom folder in this directory or to go directly to one, open this in your browser: %s\n\n", len(sbomViewFiles), link)
message.Note(msg)

// Use survey.Input to hang until user input
var value string
prompt := &survey.Input{
Message: "Hit the 'enter' key when you are done viewing the SBOM files",
Default: "",
}
_ = survey.AskOne(prompt, &value)
} else {
message.Note("There were no images with software bill-of-materials (SBOM) included.")
}
}
}