Skip to content

Commit

Permalink
offline-update: Add command to print info about bundle
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Sul <[email protected]>
  • Loading branch information
mike-sul committed Apr 9, 2024
1 parent 5cc7750 commit 07cf129
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions subcommands/targets/offline-update.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ type (
buildTag string
fetchedApps *client.FetchedApps
}

ouBundleMeta struct {
Type string `json:"type"`
Tag string `json:"tag"`
Targets []string `json:"targets"`
}
ouBundleTufMeta struct {
tuf.SignedCommon
ouBundleMeta `json:"x-fio-offline-bundle"`
}
)

var (
Expand Down Expand Up @@ -80,6 +90,7 @@ func init() {
offlineUpdateCmd.MarkFlagsMutuallyExclusive("tag", "wave")
offlineUpdateCmd.MarkFlagsMutuallyExclusive("prod", "wave")
initSignCmd(offlineUpdateCmd)
initCheckCmd(offlineUpdateCmd)
}

func initSignCmd(parentCmd *cobra.Command) {
Expand All @@ -100,6 +111,56 @@ and is printed by this command.`,
parentCmd.AddCommand(signCmd)
}

func initCheckCmd(parentCmd *cobra.Command) {
checkCmd := &cobra.Command{
Use: "check <path to an offline bundle>",
Short: "Check and print info about an offline bundle",
Long: `Check and print info about an offline bundle.
Run this command if you would like to get information about an offline bundle.
Specifically, what targets it includes, what the type of the targets (CI or production),
a bundle's expiration time', etc.`,
Run: doCheckBundle,
Args: cobra.ExactArgs(1),
}
parentCmd.AddCommand(checkCmd)
}

func doCheckBundle(cmd *cobra.Command, args []string) {
tufMetaPath := path.Join(args[0], "tuf")
bundleTufMeta, err := getBundleTargetsMeta(tufMetaPath)
subcommands.DieNotNil(err)
if bundleTufMeta == nil {

Check failure on line 133 in subcommands/targets/offline-update.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA5011(related information): this check suggests that the pointer can be nil (staticcheck)
subcommands.DieNotNil(errors.New("The bundle metadata has not been found; `bundle-targets.json` is missing in " + tufMetaPath))
}
bundleMeta := ouBundleTufMeta{}
subcommands.DieNotNil(json.Unmarshal(*bundleTufMeta.Signed, &bundleMeta))

Check failure on line 137 in subcommands/targets/offline-update.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA5011: possible nil pointer dereference (staticcheck)
fmt.Println("Bundle targets info:")
fmt.Printf("\tType:\t\t%s\n", bundleMeta.ouBundleMeta.Type)
fmt.Printf("\tTag:\t\t%s\n", bundleMeta.Tag)
fmt.Printf("\tExpires:\t%s\n", bundleMeta.Expires)
fmt.Println("\tTargets:")
for _, target := range bundleMeta.Targets {
fmt.Printf("\t\t\t%s\n", target)
}
fmt.Println("\tSignatures:")
for _, sig := range bundleTufMeta.Signatures {

Check failure on line 147 in subcommands/targets/offline-update.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA5011: possible nil pointer dereference (staticcheck)
fmt.Printf("\t\t\t%s\n", sig.KeyID)
}

rootMeta, err := getLatestRoot(tufMetaPath)
subcommands.DieNotNil(err)
fmt.Println("\tAllowed keys:")
for _, key := range rootMeta.Signed.Roles["targets"].KeyIDs {
fmt.Printf("\t\t\t%s\n", key)
}
fmt.Printf("\tThreshold:\t%d\n", rootMeta.Signed.Roles["targets"].Threshold)
numberOfMissingSignatures := rootMeta.Signed.Roles["targets"].Threshold - len(bundleTufMeta.Signatures)

Check failure on line 158 in subcommands/targets/offline-update.go

View workflow job for this annotation

GitHub Actions / golangci-lint

SA5011: possible nil pointer dereference (staticcheck)
if numberOfMissingSignatures > 0 {
fmt.Printf("\tMissing:\t%d\n", numberOfMissingSignatures)
}
}

func doSignBundle(cmd *cobra.Command, args []string) {
offlineKeysFile, _ := cmd.Flags().GetString("keys")
offlineKeys, err := keys.GetOfflineCreds(offlineKeysFile)
Expand Down

0 comments on commit 07cf129

Please sign in to comment.