[
https://issues.apache.org/jira/browse/KNOX-1820?focusedWorklogId=213028&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-213028
]
ASF GitHub Bot logged work on KNOX-1820:
----------------------------------------
Author: ASF GitHub Bot
Created on: 14/Mar/19 09:59
Start Date: 14/Mar/19 09:59
Worklog Time Spent: 10m
Work Description: smolnar82 commented on pull request #72: KNOX-1820 -
Cleanup KeystoreService implementations and add unit tests
URL: https://github.com/apache/knox/pull/72#discussion_r265491644
##########
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());
+ }
Review comment:
Since there is no difference if the given file is a directory or it cannot
be written you may merge those `if` cases:
`if (file.isDirectory() || !file.canWrite())`
----------------------------------------------------------------
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]
Issue Time Tracking
-------------------
Worklog Id: (was: 213028)
Time Spent: 1h 10m (was: 1h)
> Cleanup KeystoreService implementations and add unit tests
> ----------------------------------------------------------
>
> Key: KNOX-1820
> URL: https://issues.apache.org/jira/browse/KNOX-1820
> Project: Apache Knox
> Issue Type: Improvement
> Components: Server
> Reporter: Robert Levas
> Assignee: Robert Levas
> Priority: Minor
> Labels: cleanup
> Fix For: 1.3.0
>
> Time Spent: 1h 10m
> Remaining Estimate: 0h
>
> {{org.apache.knox.gateway.services.security.impl.BaseKeystoreService}} should
> be an abstract class extended by
> {{org.apache.knox.gateway.services.security.impl.CMFKeystoreService}} and
> {{DefaultKeystoreService}.
> Unit test for these classes are missing and should be added.
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)