Skip to content
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
4 changes: 4 additions & 0 deletions client/foundries_tuf_root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (a *Api) TufProdRootGet(factory string) (*AtsTufRoot, error) {
return a.tufRootGet(factory, true, -1)
}

func (a *Api) TufProdRootGetVer(factory string, version int) (*AtsTufRoot, error) {
return a.tufRootGet(factory, true, version)
}

func (a *Api) TufRootPost(factory string, root []byte) (string, error) {
return a.tufRootPost(factory, false, root)
}
Expand Down
73 changes: 73 additions & 0 deletions subcommands/keys/tuf_download_roots.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package keys

import (
"archive/tar"
"compress/gzip"
"fmt"
"os"

canonical "github.com/docker/go/canonical/json"
"github.com/spf13/cobra"
"github.com/spf13/viper"

"github.com/foundriesio/fioctl/client"
"github.com/foundriesio/fioctl/subcommands"
)

func init() {
downloadRoots := &cobra.Command{
Use: "download-roots <archive path>",
Short: "Download all versions of the Factory's TUF root metadata into a tarball",
Run: doDownloadRoots,
Args: cobra.ExactArgs(1),
}
downloadRoots.Flags().BoolP("prod", "", false, "Download the production versions")
tufCmd.AddCommand(downloadRoots)
}

func doDownloadRoots(cmd *cobra.Command, args []string) {
factory := viper.GetString("factory")
prod, _ := cmd.Flags().GetBool("prod")
dstPath := args[0]

var getRoot func(factory string, version int) (*client.AtsTufRoot, error)
if prod {
getRoot = api.TufProdRootGetVer
} else {
getRoot = api.TufRootGetVer
}

file, err := os.Create(dstPath)
subcommands.DieNotNil(err)
defer file.Close()

gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()

tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()

for ver := 1; ; ver++ {
var root *client.AtsTufRoot
root, err = getRoot(factory, ver)
if err != nil {
if herr := client.AsHttpError(err); herr != nil && herr.Response.StatusCode == 404 {
break

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, write some success message at the end?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its currently outputting

$ fioctl keys tuf download-roots --prod /tmp/roots.tgz
= Adding 1.root.json
= Adding 2.root.json
= Adding 3.root.json
= Adding 4.root.json
= Adding 5.root.json
= Adding 6.root.json
= Adding 7.root.json
= Adding 8.root.json
= Adding 9.root.json

We generally (we aren't totally consistent) don't print a "success" message at the end of a command.

}
subcommands.DieNotNil(err)
}

bytes, err := canonical.MarshalCanonical(root)
subcommands.DieNotNil(err)

fmt.Printf("= Adding %d.root.json\n", ver)
header := &tar.Header{
Name: fmt.Sprintf("%d.root.json", ver),
Size: int64(len(bytes)),
Mode: 0644,
}
subcommands.DieNotNil(tarWriter.WriteHeader(header))
_, err = tarWriter.Write(bytes)
subcommands.DieNotNil(err)
}
}
Loading