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

Importing RSA and EC keypairs #1050

Merged
merged 18 commits into from
Jan 1, 2022
Merged

Importing RSA and EC keypairs #1050

merged 18 commits into from
Jan 1, 2022

Conversation

venafi-iw
Copy link
Contributor

Summary

This PR provides support for importing RSA and EC keypairs that were generated using 3rd party utilities such as OpenSSL. Imported keypairs are converted to Cosign keypairs.

Ticket Link

This PR is in relation to #549

Release Notes

  • Added support for importing EC and RSA keypairs generated using 3rd party utilities such as OpenSSL
  • New command line option import-key-pair
  • added instructions on how to import keypairs

@dlorenc
Copy link
Member

dlorenc commented Nov 14, 2021

Looks like DCO is missing! You'll need to use the git commit -s flag

Signed-off-by: Ivan Wallis <[email protected]>
Copy link

@SantiagoTorres SantiagoTorres left a comment

Choose a reason for hiding this comment

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

Overall, it looks in a good direction, but I wonder how this fits the broader codebase (I think there's a lot of possibly repeated code).

I also feel there are a couple of minor fix/tweaks that may be making the code slightly harder to review. I wonder if we could split those into a smaller PR...

}
fmt.Fprintln(os.Stderr)
fmt.Fprint(os.Stderr, "Enter password for private key again: ")
pw2, err := term.ReadPassword(0)

Choose a reason for hiding this comment

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

I'm not entirely fond of pw2 when this should be a confirmation. I'm not sure what the golang guidelines are for variable naming, but maybe something more explicit would make the code more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I can update the variable to something more explcit such as confirmpw

Copy link
Contributor

Choose a reason for hiding this comment

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

small ping on this comment

pkg/cosign/keys.go Outdated Show resolved Hide resolved
pkg/cosign/keys.go Show resolved Hide resolved
pkg/cosign/keys.go Outdated Show resolved Hide resolved
pkg/cosign/keys.go Show resolved Hide resolved
Signed-off-by: Ivan Wallis <[email protected]>
@venafi-iw venafi-iw requested a review from asraa November 16, 2021 06:40
Copy link
Contributor

@asraa asraa left a comment

Choose a reason for hiding this comment

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

Thanks! looking really good so far :D appreciate all the changes


}

func MarshallKeyPair(keypair Key, pf PassFunc) (*Keys, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: spelling MarshalKeyPair

Copy link
Contributor

Choose a reason for hiding this comment

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

could you share this code with GenerateKeyPair? and given that it's only called within this function, maybe it should be made private.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

made changes.

pkg/cosign/keys.go Show resolved Hide resolved
pkg/cosign/keys.go Outdated Show resolved Hide resolved
test/import_test.sh Outdated Show resolved Hide resolved
Signed-off-by: Ivan Wallis <[email protected]>
@venafi-iw venafi-iw requested a review from asraa November 17, 2021 06:15
Copy link
Contributor

@asraa asraa left a comment

Choose a reason for hiding this comment

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

Sorry for the delay, taking a review! Could you check on the test failures as well?

?   	github.com/sigstore/cosign/pkg/blob	[no test files]
--- FAIL: TestImportPrivateKey (0.12s)
    keys_test.go:158: unexpected error importing key: parsing error
FAIL

Also, running and committing the change from go run -tags pivkey,pkcs11key,cgo ./cmd/help/ will fix the doc gen CI check

pkg/cosign/keys.go Outdated Show resolved Hide resolved
pkg/cosign/keys.go Outdated Show resolved Hide resolved
pkg/cosign/keys.go Show resolved Hide resolved
pkg/cosign/keys_test.go Outdated Show resolved Hide resolved
test/e2e_test.go Outdated Show resolved Hide resolved
test/import_test.sh Outdated Show resolved Hide resolved
Signed-off-by: Ivan Wallis <[email protected]>
@venafi-iw venafi-iw requested a review from asraa November 19, 2021 01:24
Copy link
Contributor

@asraa asraa left a comment

Choose a reason for hiding this comment

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

Thanks! Could you just please address the lint comments and update the branch and this would LGTM!

pkg/cosign/keys.go Outdated Show resolved Hide resolved
@venafi-iw
Copy link
Contributor Author

#1050 (comment)

Your code suggestion doesn't compile.

Signed-off-by: Ivan Wallis <[email protected]>
@venafi-iw venafi-iw requested a review from asraa December 5, 2021 00:43
@asraa
Copy link
Contributor

asraa commented Dec 6, 2021

Your code suggestion doesn't compile.

Was hoping you would take away the gist of what I was saying. I pulled your branch and completed it for you:

diff --git a/pkg/cosign/keys.go b/pkg/cosign/keys.go
index 98f92f9..d592b0e 100644
--- a/pkg/cosign/keys.go
+++ b/pkg/cosign/keys.go
@@ -71,20 +71,20 @@ func ImportKeyPair(keyPath string, pf PassFunc) (*KeysBytes, error) {
                return nil, errors.New("invalid pem block")
        }
 
+       var pk crypto.Signer
        switch p.Type {
        case RSAPrivateKeyPemType:
-               pk, err := x509.ParsePKCS1PrivateKey(p.Bytes)
+               pk, err = x509.ParsePKCS1PrivateKey(p.Bytes)
                if err != nil {
                        return nil, fmt.Errorf("parsing error")
                }
-               return marshalKeyPair(Keys{pk, pk.Public()}, pf)
        default:
-               pk, err := x509.ParseECPrivateKey(p.Bytes)
+               pk, err = x509.ParseECPrivateKey(p.Bytes)
                if err != nil {
                        return nil, fmt.Errorf("parsing error")
                }
-               return marshalKeyPair(Keys{pk, pk.Public()}, pf)
        }
+       return marshalKeyPair(Keys{pk, pk.Public()}, pf)
 }
 
 func marshalKeyPair(keypair Keys, pf PassFunc) (*KeysBytes, error) {

Signed-off-by: Ivan Wallis <[email protected]>
@venafi-iw
Copy link
Contributor Author

ImportKeyPair function updated.

pkg/cosign/keys_test.go Outdated Show resolved Hide resolved
@venafi-iw venafi-iw requested a review from asraa December 6, 2021 18:41
pkg/cosign/keys_test.go Outdated Show resolved Hide resolved
pkg/cosign/keys_test.go Outdated Show resolved Hide resolved
@venafi-iw venafi-iw requested a review from asraa December 7, 2021 00:30
@asraa asraa self-assigned this Dec 7, 2021
@asraa
Copy link
Contributor

asraa commented Dec 7, 2021

cc @lukehinds @dlorenc do you want to give this a merge? LGTM just want a final pass in case I'm missing something cosign-style-y

@lukehinds
Copy link
Member

cc @lukehinds @dlorenc do you want to give this a merge? LGTM just want a final pass in case I'm missing something cosign-style-y

looks goof from a cursory view, but there are a quite a few CI failures.

@dlorenc
Copy link
Member

dlorenc commented Dec 14, 2021

Yeah looks like a merge conflict somewhere, otherwise should be good to go.

@venafi-iw
Copy link
Contributor Author

@dlorenc Made some code updates and looks good for next steps.

@dlorenc
Copy link
Member

dlorenc commented Dec 31, 2021

This looks good to me, there's one more conflict though :(

Once that gets fixed i'll merge right away so you don't keep getting hit!

@venafi-iw
Copy link
Contributor Author

Conflict resolved

@dlorenc
Copy link
Member

dlorenc commented Jan 1, 2022

Thanks for dealing with all the delays and merge conflicts!

@dlorenc dlorenc merged commit 9a27e1f into sigstore:main Jan 1, 2022
@github-actions github-actions bot added this to the v1.5.0 milestone Jan 1, 2022
### Sign a container with imported keypair

```shell
$ cosign sign --key import import-cosign.key dlorenc/demo
Copy link
Member

@cpanato cpanato Jan 8, 2022

Choose a reason for hiding this comment

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

this import is needed?

or should be just cosign sign --key import-cosign.key dlorenc/demo ?

Copy link
Contributor Author

@venafi-iw venafi-iw Jan 8, 2022

Choose a reason for hiding this comment

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

documentation needs to be updated as it should be:

cosign sign --key import-cosign.key dlorenc/demo

#1290

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants