-
-
Notifications
You must be signed in to change notification settings - Fork 409
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
Changes from 4 commits
f014be1
ea0ce60
1c34618
1c6c318
0ff01b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
} | ||
|
||
|
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) | ||
umbynos marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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)) | ||
} |
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 | ||
} |
Uh oh!
There was an error while loading. Please reload this page.