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

fix: import keys with a default key id #2563

Merged
merged 2 commits into from
Jun 18, 2021
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
9 changes: 7 additions & 2 deletions cmd/cli/handler_jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,18 @@ func (h *JWKHandler) ImportKeys(cmd *cobra.Command, args []string) {
file, err := ioutil.ReadFile(path)
cmdx.Must(err, "Unable to read file %s", path)

keyID := flagx.MustGetString(cmd, "default-key-id")
if keyID == "" {
keyID = uuid.New()
}

if key, privateErr := josex.LoadPrivateKey(file); privateErr != nil {
key, publicErr := josex.LoadPublicKey(file)
cmdx.Must(publicErr, `Unable to read key from file %s. Decoding file to private key failed with reason "%s" and decoding it to public key failed with reason: %s`, path, privateErr, publicErr)

set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "public:"+uuid.New(), use))
set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "public:"+keyID, use))
} else {
set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "private:"+uuid.New(), use))
set.Keys = append(set.Keys, toSDKFriendlyJSONWebKey(key, "private:"+keyID, use))
}

fmt.Printf("Successfully loaded key from file: %s\n", path)
Expand Down
3 changes: 2 additions & 1 deletion cmd/keys_import.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ Please be aware that importing a private key does not automatically import its p

Examples:
hydra keys import my-set ./path/to/jwk.json ./path/to/jwk-2.json
hydra keys import my-set ./path/to/rsa.key ./path/to/rsa.pub
hydra keys import my-set ./path/to/rsa.key ./path/to/rsa.pub --default-key-id cae6b214-fb1e-4ebc-9019-95286a62eabc
`,
Run: cli.NewHandler().Keys.ImportKeys,
}
cmd.Flags().String("use", "sig", "Sets the \"use\" value of the JSON Web Key if not \"use\" value was defined by the key itself")
cmd.Flags().String("default-key-id", "", "A fallback value for keys without \"kid\" attribute to be stored with a common \"kid\", e.g. private/public keypairs")
return cmd
}