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

Cryptography module, integrity checking commands and small fixes #108

Merged
merged 17 commits into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# App related
build/
testing.log
*.out
*.exe
.cpackget*
tmp/
.vagrant/
# IDEs
.vscode/
58 changes: 58 additions & 0 deletions cmd/commands/checksum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright Contributors to the cpackget project. */

package commands

import (
"github.com/open-cmsis-pack/cpackget/cmd/cryptography"
errs "github.com/open-cmsis-pack/cpackget/cmd/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var checksumCreateCmdFlags struct {
// hashAlgorithm is the cryptographic hash function to be used
hashAlgorithm string

// outputDir is the target directory where the checksum file is written to
outputDir string
}

func init() {
ChecksumCreateCmd.Flags().StringVarP(&checksumCreateCmdFlags.hashAlgorithm, "hash-algorithm", "a", "", "specifies the hash function to be used")
chaws marked this conversation as resolved.
Show resolved Hide resolved
ChecksumCreateCmd.Flags().StringVarP(&checksumCreateCmdFlags.outputDir, "output-dir", "o", "", "specifies output directory for the checksum file")
}

var ChecksumCreateCmd = &cobra.Command{
Use: "checksum-create [<local .path pack>]",
Short: "Generates a .checksum file containing the digests of a pack",
// TODO: Long, show valid hash algorithms
chaws marked this conversation as resolved.
Show resolved Hide resolved
Args: cobra.MinimumNArgs(0),
chaws marked this conversation as resolved.
Show resolved Hide resolved
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {

if len(args) == 0 {
log.Error("missing .pack local path")
return errs.ErrIncorrectCmdArgs
}

return cryptography.GenerateChecksum(args[0], checksumCreateCmdFlags.outputDir, checksumCreateCmdFlags.hashAlgorithm)
},
}

var ChecksumVerifyCmd = &cobra.Command{
Use: "checksum-verify [<local .path pack>] [<local .checksum path>]",
Short: "Verifies the integrity of a pack using its .checksum file",
// TODO: Long
Args: cobra.MinimumNArgs(0),
chaws marked this conversation as resolved.
Show resolved Hide resolved
PersistentPreRunE: configureInstaller,
RunE: func(cmd *cobra.Command, args []string) error {

if len(args) != 2 {
log.Error("Please provide path to pack and checksum file")
return errs.ErrIncorrectCmdArgs
}

return cryptography.VerifyChecksum(args[0], args[1])
},
}
24 changes: 19 additions & 5 deletions cmd/commands/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package commands

import (
"os"
"runtime"

"github.com/open-cmsis-pack/cpackget/cmd/installer"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -13,11 +16,10 @@ import (
var overwrite bool

var IndexCmd = &cobra.Command{
Deprecated: "Consider running `cpackget update-index` instead",
Use: "index <index url>",
Short: "Updates public index",
Long: `Updates the public index in CMSIS_PACK_ROOT/.Web/index.pidx using the file specified by the given url.
If there's already an index file, cpackget won't overwrite it. Use "-f" to do so.`,
Deprecated: "Consider running `cpackget update-index` instead",
Use: "index <index url>",
Short: "Updates public index",
Long: getLongIndexDescription(),
PersistentPreRunE: configureInstaller,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -27,6 +29,18 @@ If there's already an index file, cpackget won't overwrite it. Use "-f" to do so
},
}

// getLongIndexDescription prints a "Windows friendly" long description,
// using the correct path slashes
func getLongIndexDescription() string {
if runtime.GOOS == "windows" {
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `\.Web\index.pidx using the file specified by the given url.
If there's already an index file, cpackget won't overwrite it. Use "-f" to do so.`
} else {
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `/.Web/index.pidx using the file specified by the given url.
If there's already an index file, cpackget won't overwrite it. Use "-f" to do so.`
}
}

func init() {
IndexCmd.Flags().BoolVarP(&overwrite, "force", "f", false, "forces cpackget to overwrite an existing public index file")
}
2 changes: 2 additions & 0 deletions cmd/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var AllCommands = []*cobra.Command{
RmCmd,
ListCmd,
UpdateIndexCmd,
ChecksumCreateCmd,
ChecksumVerifyCmd,
}

// createPackRoot is a flag that determines if the pack root should be created or not
Expand Down
22 changes: 18 additions & 4 deletions cmd/commands/update_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
package commands

import (
"os"
"runtime"

"github.com/open-cmsis-pack/cpackget/cmd/installer"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
Expand All @@ -15,10 +18,9 @@ var updateIndexCmdFlags struct {
}

var UpdateIndexCmd = &cobra.Command{
Use: "update-index",
Short: "Update the public index",
Long: `Update the public index in CMSIS_PACK_ROOT/.Web/index.pidx using the URL in <url> tag inside index.pidx.
By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.`,
Use: "update-index",
Short: "Update the public index",
Long: getLongUpdateDescription(),
PersistentPreRunE: configureInstaller,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -27,6 +29,18 @@ By default it will also check if all PDSC files under .Web/ need update as well.
},
}

// getLongUpdateDescription prints a "Windows friendly" long description,
// using the correct path slashes
func getLongUpdateDescription() string {
if runtime.GOOS == "windows" {
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `\.Web\index.pidx using the URL in <url> tag inside index.pidx.
By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.`
} else {
return `Updates the public index in ` + os.Getenv("CMSIS_PACK_ROOT") + `/.Web/index.pidx using the URL in <url> tag inside index.pidx.
By default it will also check if all PDSC files under .Web/ need update as well. This can be disabled via the "--sparse" flag.`
}
}

func init() {
UpdateIndexCmd.Flags().BoolVarP(&updateIndexCmdFlags.sparse, "sparse", "s", false, "avoid updating the pdsc files within .Web/ folder")
}
Loading