Search in sources :

Example 1 with SimpleCredentialStore

use of org.wildfly.test.security.common.elytron.SimpleCredentialStore in project wildfly by wildfly.

the class CredentialStoreTestCase method testCredentialStoreCreating.

/**
     * Tests credential store with automatically created PKCS12 keystore.
     */
@Test
@Ignore
public void testCredentialStoreCreating() throws Exception {
    String storeName = NAME;
    File tempFolder = Utils.createTemporaryFolder(storeName);
    String fileName = System.currentTimeMillis() + ".p12";
    File ksFile = new File(tempFolder, fileName);
    assertTrue(tempFolder.isDirectory());
    assertFalse(ksFile.exists());
    try {
        try (CLIWrapper cli = new CLIWrapper(true)) {
            cli.sendLine(String.format("/path=%s:add(path=\"%s\")", storeName, asAbsolutePath(tempFolder)));
            SimpleCredentialStore storeConfig = SimpleCredentialStore.builder().withName(storeName).withKeyStorePath(Path.builder().withPath(fileName).withRelativeTo(storeName).build()).withCredential(CredentialReference.builder().withClearText("pkcs12pass").build()).withKeyStoreType("PKCS12").withModifiable(true).withCreate(true).withAlias("elytron", "rocks!").build();
            try {
                storeConfig.create(cli);
                assertContainsAliases(cli, storeName, "elytron");
                assertTrue(ksFile.exists());
                cli.sendLine(String.format("/subsystem=elytron/credential-store=%s/alias=another-secret:add(secret-value=\"%1$s\")", storeName));
                assertCredentialValue(storeName, "elytron", "rocks!");
                assertCredentialValue(storeName, "another-secret", storeName);
            } finally {
                // this should remove alias "elytron" from KeyStore file and remove credential {@value NAME} from domain
                // model
                storeConfig.remove(cli);
            }
            // KeyStore file should not be removed after
            assertTrue(ksFile.exists());
            KeyStore ks = KeyStore.getInstance("PKCS12");
            try (FileInputStream fis = new FileInputStream(ksFile)) {
                ks.load(fis, "pkcs12pass".toCharArray());
                assertEquals(1, ks.size());
                assertTrue(ks.aliases().nextElement().contains("another-secret"));
            }
        }
    } finally {
        FileUtils.deleteQuietly(tempFolder);
    }
}
Also used : CLIWrapper(org.jboss.as.test.integration.management.util.CLIWrapper) SimpleCredentialStore(org.wildfly.test.security.common.elytron.SimpleCredentialStore) File(java.io.File) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 KeyStore (java.security.KeyStore)1 CLIWrapper (org.jboss.as.test.integration.management.util.CLIWrapper)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 SimpleCredentialStore (org.wildfly.test.security.common.elytron.SimpleCredentialStore)1