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

Add PKCS8 private key support #730

Merged
merged 3 commits into from
Mar 19, 2017
Merged

Add PKCS8 private key support #730

merged 3 commits into from
Mar 19, 2017

Conversation

shiroari
Copy link
Contributor

@shiroari shiroari commented Mar 9, 2017

Hi,

This pull request adds support for PKCS8 in key.pem file (default one is PKCS1). Also I changed a bit way of working with readers and stream by using try-with-resources statement and added a few tests.

Note that I remove wrapping FileReader to BufferedReader. I think that was unnecessary because PEMParser is inherited from BufferedReader and prefetches data itself.

@codecov
Copy link

codecov bot commented Mar 9, 2017

Codecov Report

Merging #730 into master will increase coverage by 0.31%.
The diff coverage is 76.47%.

@@             Coverage Diff              @@
##             master     #730      +/-   ##
============================================
+ Coverage     47.48%   47.79%   +0.31%     
- Complexity     1002     1009       +7     
============================================
  Files           125      125              
  Lines          6596     6605       +9     
  Branches        847      851       +4     
============================================
+ Hits           3132     3157      +25     
+ Misses         3196     3177      -19     
- Partials        268      271       +3
Impacted Files Coverage Δ Complexity Δ
...a/io/fabric8/maven/docker/access/KeyStoreUtil.java 83.33% <76.47%> (+83.33%) 7 <4> (+7) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c185afd...058fe58. Read the comment docs.

@rhuss
Copy link
Collaborator

rhuss commented Mar 15, 2017

Thanks a lot ! I'm bit loaded right now, but will integrate the PR (which makes much sense) over the weekend hopefully.

Could you do me please a favour in the meantime and sign-off the commit as described in https://github.com/fabric8io/docker-maven-plugin/blob/master/CONTRIBUTING.md ?

thanks again ....

Signed-off-by: Stas Sukhanov <[email protected]>
@shiroari
Copy link
Contributor Author

Hopefully I did it right. Thank you.

@rhuss
Copy link
Collaborator

rhuss commented Mar 15, 2017

looks good ;-) thanks !

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

build trigger test - please ignore ....

retest this please

Copy link
Collaborator

@rhuss rhuss left a comment

Choose a reason for hiding this comment

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

Looks good to and especially thank you for adding tests.

Only some minor comments (see inline).

Would be also awesome if you add the change to changelog.md.

return generatePrivateKey((PrivateKeyInfo) readObject);
}
}
return null;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I would throw an exception in that case, since a private key is mandatory for the further processing. It better to stop where the issue happens and where we have most of the error context (i.e. read object type which is wrong).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the comments. That would be definitely more correct to throw exception here. Will fix.

}

private static void addCA(KeyStore keyStore, String caPath) throws KeyStoreException, FileNotFoundException, CertificateException {
private static void addCA(KeyStore keyStore, String caPath) throws IOException, KeyStoreException,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is FileNotFoundException not good enough here ?

InputStream is = new FileInputStream(certPath);
Collection<? extends Certificate> certs = CertificateFactory.getInstance("X509").generateCertificates(is);
return new ArrayList<>(certs).toArray(new Certificate[certs.size()]);
private static Certificate[] loadCertificates(String certPath) throws IOException, CertificateException {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same here: Why not keep FileNotFoundException as the more specific exception ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The problem here that try-with-resources statement automatically closes input stream and close method throws IOException and then it goes to addCA.

PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyPair.getPrivateKeyInfo().getEncoded());
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
static PrivateKey loadPrivateKey(String keyPath) throws IOException, GeneralSecurityException {
try (Reader reader = new FileReader(keyPath);
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for adding resource management here

@rhuss
Copy link
Collaborator

rhuss commented Mar 19, 2017

Thanks ! Looks good to me

[merge]

@fusesource-ci fusesource-ci merged commit d4f5291 into fabric8io:master Mar 19, 2017
rgbj pushed a commit to rgbj/docker-maven-plugin that referenced this pull request Jun 21, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants