Skip to content

Add keys generate command #1695

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
15 changes: 8 additions & 7 deletions arduino/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -698,21 +698,22 @@ func (e *TempDirCreationFailedError) ToRPCStatus() *status.Status {
return status.New(codes.Unavailable, e.Error())
}

// TempFileCreationFailedError is returned if a temp file could not be created
type TempFileCreationFailedError struct {
Cause error
// FileCreationFailedError is returned if a temp file could not be created
type FileCreationFailedError struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

As this is an exported object, this will be a breaking change and so the standard procedure should be followed to communicate that to the users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not though about it, but you are right

Message string
Cause error
}

func (e *TempFileCreationFailedError) Error() string {
return composeErrorMsg(tr("Cannot create temp file"), e.Cause)
func (e *FileCreationFailedError) Error() string {
return composeErrorMsg(e.Message, e.Cause)
}

func (e *TempFileCreationFailedError) Unwrap() error {
func (e *FileCreationFailedError) Unwrap() error {
return e.Cause
}

// ToRPCStatus converts the error into a *status.Status
func (e *TempFileCreationFailedError) ToRPCStatus() *status.Status {
func (e *FileCreationFailedError) ToRPCStatus() *status.Status {
return status.New(codes.Unavailable, e.Error())
}

Expand Down
2 changes: 2 additions & 0 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/cli/generatedocs"
"github.com/arduino/arduino-cli/cli/globals"
"github.com/arduino/arduino-cli/cli/keys"
"github.com/arduino/arduino-cli/cli/lib"
"github.com/arduino/arduino-cli/cli/monitor"
"github.com/arduino/arduino-cli/cli/outdated"
Expand Down Expand Up @@ -93,6 +94,7 @@ func createCliCommandTree(cmd *cobra.Command) {
cmd.AddCommand(core.NewCommand())
cmd.AddCommand(daemon.NewCommand())
cmd.AddCommand(generatedocs.NewCommand())
cmd.AddCommand(keys.NewCommand())
cmd.AddCommand(lib.NewCommand())
cmd.AddCommand(monitor.NewCommand())
cmd.AddCommand(outdated.NewCommand())
Expand Down
88 changes: 88 additions & 0 deletions cli/keys/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package keys

import (
"context"
"os"

"github.com/arduino/arduino-cli/cli/errorcodes"
"github.com/arduino/arduino-cli/cli/feedback"
"github.com/arduino/arduino-cli/commands/keys"
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var (
keyName []string // Name of the custom keys to generate. Can be used multiple times for multiple security keys.
algorithmType string // Algorithm type to use
keysKeychain string // Path of the dir where to save the custom keys
)

func initGenerateCommand() *cobra.Command {
generateCommand := &cobra.Command{
Use: "generate",
Short: tr("Generate the security keys."),
Long: tr("Generate the security keys required for secure boot"),
Example: "" +
" " + os.Args[0] + " keys generate -t ecdsa-p256 --key-name ecdsa-p256-signing-key.pem --key-name ecdsa-p256-encrypt-key.pem --keys-keychain /home/user/Arduino/MyKeys\n" +
" " + os.Args[0] + " keys generate --key-name ecdsa-p256-signing-key.pem",
Args: cobra.NoArgs,
Run: runGenerateCommand,
}

generateCommand.Flags().StringVarP(&algorithmType, "type", "t", "ecdsa-p256", tr("Algorithm type to use"))
generateCommand.Flags().StringArrayVar(&keyName, "key-name", []string{}, tr("Name of the custom key to generate. Can be used multiple times for multiple security keys."))
generateCommand.MarkFlagRequired("key-name")
generateCommand.RegisterFlagCompletionFunc("key-name", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
defaultKeyNames := []string{"ecdsa-p256-signing-key.pem", "ecdsa-p256-encrypt-key.pem"}
return defaultKeyNames, cobra.ShellCompDirectiveDefault
})
generateCommand.Flags().StringVar(&keysKeychain, "keys-keychain", "", tr("The path of the dir where to save the custom keys"))

return generateCommand
}

func runGenerateCommand(command *cobra.Command, args []string) {

logrus.Info("Executing `arduino-cli keys generate`")

resp, err := keys.Generate(context.Background(), &rpc.KeysGenerateRequest{
AlgorithmType: algorithmType,
KeyName: keyName,
KeysKeychain: keysKeychain,
})
if err != nil {
feedback.Errorf(tr("Error during Generate: %v"), err)
os.Exit(errorcodes.ErrGeneric)
}
feedback.PrintResult(result{resp})
}

// output from this command requires special formatting, let's create a dedicated
// feedback.Result implementation
type result struct {
Keychain *rpc.KeysGenerateResponse `json:"keys_keychain"`
}

func (dr result) Data() interface{} {
return dr.Keychain
}

func (dr result) String() string {
return (tr("Keys created in: %s", dr.Keychain.KeysKeychain))
}
39 changes: 39 additions & 0 deletions cli/keys/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// This file is part of arduino-cli.
//
// Copyright 2020 ARDUINO SA (http://www.arduino.cc/)
//
// This software is released under the GNU General Public License version 3,
// which covers the main part of arduino-cli.
// The terms of this license can be found at:
// https://www.gnu.org/licenses/gpl-3.0.en.html
//
// You can be released from the requirements of the above licenses by purchasing
// a commercial license. Buying such a license is mandatory if you want to
// modify or otherwise use the software for commercial activities involving the
// Arduino software without disclosing the source code of your own applications.
// To purchase a commercial license, send an email to [email protected].

package keys

import (
"os"

"github.com/arduino/arduino-cli/i18n"
"github.com/spf13/cobra"
)

var tr = i18n.Tr

// NewCommand created a new `keys` command
func NewCommand() *cobra.Command {
keysCommand := &cobra.Command{
Use: "keys",
Short: tr("Arduino keys commands."),
Long: tr("Arduino keys commands. Useful to operate on security keys"),
Example: " " + os.Args[0] + " keys generate --key-name ecdsa-p256-signing-key.pem --keys-keychain /home/user/Arduino/MyKeys",
}

keysCommand.AddCommand(initGenerateCommand())

return keysCommand
}
8 changes: 4 additions & 4 deletions commands/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do

var tmp *paths.Path
if tmpFile, err := ioutil.TempFile("", ""); err != nil {
return nil, &arduino.TempFileCreationFailedError{Cause: err}
return nil, &arduino.FileCreationFailedError{Message: tr("Cannot create temp file"), Cause: err}
} else if err := tmpFile.Close(); err != nil {
return nil, &arduino.TempFileCreationFailedError{Cause: err}
return nil, &arduino.FileCreationFailedError{Message: tr("Cannot create temp file"), Cause: err}
} else {
tmp = paths.New(tmpFile.Name())
}
Expand Down Expand Up @@ -502,9 +502,9 @@ func UpdateIndex(ctx context.Context, req *rpc.UpdateIndexRequest, downloadCB Do
URLSig.Path += ".sig"

if t, err := ioutil.TempFile("", ""); err != nil {
return nil, &arduino.TempFileCreationFailedError{Cause: err}
return nil, &arduino.FileCreationFailedError{Message: tr("Cannot create temp file"), Cause: err}
} else if err := t.Close(); err != nil {
return nil, &arduino.TempFileCreationFailedError{Cause: err}
return nil, &arduino.FileCreationFailedError{Message: tr("Cannot create temp file"), Cause: err}
} else {
tmpSig = paths.New(t.Name())
}
Expand Down
Loading