risdenk commented on a change in pull request #72: KNOX-1820 - Cleanup
KeystoreService implementations and add unit tests
URL: https://github.com/apache/knox/pull/72#discussion_r265300651
##########
File path:
gateway-server/src/main/java/org/apache/knox/gateway/services/security/impl/DefaultKeystoreService.java
##########
@@ -473,13 +482,113 @@ private KeyStore getKeystore(Path keystorePath, String
keystoreType, String alia
readLock.lock();
try {
- return getKeystore(keystoreFile, keystoreType,
getKeystorePassword(alias));
+ return loadKeyStore(keystorePath, keystoreType,
getKeyStorePassword(alias));
} finally {
readLock.unlock();
}
}
- private char[] getKeystorePassword(String alias) throws
KeystoreServiceException {
+ private boolean isKeyStoreAvailable(final Path keyStoreFilePath, String
storeType, char[] password) throws KeyStoreException, IOException {
+ if (keyStoreFilePath.toFile().exists()) {
+ try (InputStream input = Files.newInputStream(keyStoreFilePath)) {
+ final KeyStore keyStore = KeyStore.getInstance(storeType);
+ keyStore.load(input, password);
+ return true;
+ } catch (NoSuchAlgorithmException | CertificateException e) {
+ LOG.failedToLoadKeystore(keyStoreFilePath.toString(), storeType, e);
+ } catch (IOException | KeyStoreException e) {
+ LOG.failedToLoadKeystore(keyStoreFilePath.toString(), storeType, e);
+ throw e;
+ }
+ }
+ return false;
+ }
+
+ // Package private for unit test access
+ KeyStore createKeyStore(Path keystoreFilePath, String keystoreType, char[]
password) throws KeystoreServiceException {
+ try (OutputStream out = createKeyStoreFile(keystoreFilePath)) {
+ KeyStore ks = KeyStore.getInstance(keystoreType);
+ ks.load(null, null);
+ ks.store(out, password);
+ return ks;
+ } catch (NoSuchAlgorithmException | CertificateException |
KeyStoreException | IOException e) {
+ LOG.failedToCreateKeystore(keystoreFilePath.toString(), keystoreType, e);
+ throw new KeystoreServiceException(e);
+ }
+ }
+
+ private static OutputStream createKeyStoreFile(Path keystoreFilePath) throws
IOException {
+ File file = keystoreFilePath.toFile();
+ if (file.exists()) {
+ if (file.isDirectory()) {
+ throw new IOException(file.getAbsolutePath());
+ } else if (!file.canWrite()) {
+ throw new IOException(file.getAbsolutePath());
+ }
+ } else {
+ File dir = file.getParentFile();
+ if (!dir.exists()) {
+ if (!dir.mkdirs()) {
+ throw new IOException(file.getAbsolutePath());
+ }
+ }
+ }
+ return Files.newOutputStream(file.toPath());
Review comment:
Might be safer to return the Path from here. Looks like this method is only
used in one place. The caller could open the output stream (and ensure it is
closed).
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services