Skip to content

Commit

Permalink
[WFCORE-3425] Ensure the file that backs a KeyStore gets created if i…
Browse files Browse the repository at this point in the history
…t doesn't exist and wasn't flagged as required when attempting to store the KeyStore to the file
  • Loading branch information
fjuma committed Nov 29, 2017
1 parent 2c0a6f1 commit 8b815ea
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,9 @@ public void start(StartContext startContext) throws StartException {
throw ROOT_LOGGER.keyStoreFileNotExists(resolvedPath.getAbsolutePath());
} else {
ROOT_LOGGER.keyStoreFileNotExistsButIgnored(resolvedPath.getAbsolutePath());
resolvedPath = null;
}
}
try (InputStream is = resolvedPath != null ? new FileInputStream(resolvedPath) : null) {
try (InputStream is = (resolvedPath != null && resolvedPath.exists()) ? new FileInputStream(resolvedPath) : null) {
char[] password = resolvePassword();

ROOT_LOGGER.tracef(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.wildfly.extension.elytron;

import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.FAILED;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.OUTCOME;
import static org.jboss.as.controller.descriptions.ModelDescriptionConstants.SUCCESS;
import static org.junit.Assert.assertArrayEquals;
Expand Down Expand Up @@ -87,6 +88,13 @@ private ModelNode assertSuccess(ModelNode response) {
return response;
}

private ModelNode assertFailed(ModelNode response) {
if (! response.get(OUTCOME).asString().equals(FAILED)) {
Assert.fail(response.toJSONString(false));
}
return response;
}

@BeforeClass
public static void initTests() {
AccessController.doPrivileged(new PrivilegedAction<Integer>() {
Expand Down Expand Up @@ -597,6 +605,38 @@ public void testChangeAlias() throws Exception {
}
}

@Test
public void testStoreFileDoesNotExist() throws Exception {
String nonExistentFileName = "/does-not-exist.keystore";
Path resources = Paths.get(KeyStoresTestCase.class.getResource(".").toURI());
File file = new File(resources + nonExistentFileName);

ModelNode operation = getAddKeyStoreUsingNonExistingFileOperation(false, nonExistentFileName);
assertSuccess(services.executeOperation(operation));
try {
operation = new ModelNode();
operation.get(ClientConstants.OP_ADDR).add("subsystem", "elytron").add("key-store", KEYSTORE_NAME);
operation.get(ClientConstants.OP).set(ElytronDescriptionConstants.STORE);
assertSuccess(services.executeOperation(operation));
assertTrue(file.exists());
} finally {
removeKeyStore();
if (file.exists()) {
file.delete();
}
}
}

@Test
public void testFileDoesNotExistAndIsRequired() throws Exception {
String nonExistentFileName = "/does-not-exist.keystore";
Path resources = Paths.get(KeyStoresTestCase.class.getResource(".").toURI());
File file = new File(resources + nonExistentFileName);

ModelNode operation = getAddKeyStoreUsingNonExistingFileOperation(true, nonExistentFileName);
assertFailed(services.executeOperation(operation));
}

private void addKeyStore() throws Exception {
Path resources = Paths.get(KeyStoresTestCase.class.getResource(".").toURI());
Files.copy(resources.resolve("test.keystore"), resources.resolve("test-copy.keystore"), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
Expand Down Expand Up @@ -642,4 +682,22 @@ private void removeKeyStore() {
operation.get(ClientConstants.OP).set(ClientConstants.REMOVE_OPERATION);
assertSuccess(services.executeOperation(operation));
}

private ModelNode getAddKeyStoreUsingNonExistingFileOperation(boolean required, String nonExistentFileName) throws Exception {
Path resources = Paths.get(KeyStoresTestCase.class.getResource(".").toURI());
File file = new File(resources + nonExistentFileName);
assertTrue (! file.exists());

ModelNode operation = new ModelNode();
operation.get(ClientConstants.OPERATION_HEADERS).get("allow-resource-service-restart").set(Boolean.TRUE);
operation.get(ClientConstants.OP_ADDR).add("subsystem","elytron").add("key-store", KEYSTORE_NAME);
operation.get(ClientConstants.OP).set(ClientConstants.ADD);
operation.get(ElytronDescriptionConstants.PATH).set(resources + nonExistentFileName);
operation.get(ElytronDescriptionConstants.TYPE).set("JKS");
operation.get(CredentialReference.CREDENTIAL_REFERENCE).get(CredentialReference.CLEAR_TEXT).set("Elytron");
if (required) {
operation.get(ElytronDescriptionConstants.REQUIRED).set(true);
}
return operation;
}
}

0 comments on commit 8b815ea

Please sign in to comment.