Reading passwords stored in WildFly's Elytron credential store using Java?
First forgive my writing in English. The best way i now it's using this code, with library Maven version 1.12.1.Final. Other libraries like the recent Alpha have errors with this code.
<dependency>
<groupId>org.wildfly.security</groupId>
<artifactId>wildfly-elytron</artifactId>
<version>1.12.1.Final</version>
</dependency>
Method
public Password giveMeAPass(String alias) throws NoSuchAlgorithmException, CredentialStoreException, InvalidKeySpecException {
/*
* Create a ProtectionParameter for access to the store.
*/
Password storePassword = ClearPassword.createRaw(
ClearPassword.ALGORITHM_CLEAR,
"storepass".toCharArray());
ProtectionParameter protectionParameter = new CredentialSourceProtectionParameter(
IdentityCredentials.NONE.withCredential(
new PasswordCredential(storePassword)));
Security.addProvider(elytronProvider);
CredentialStore credentialStore = CredentialStore.getInstance(
"KeyStoreCredentialStore", csProvider);
// Configure and Initialise the CredentialStore
String configPath = System.getProperty("jboss.server.data.dir");
Map<String, String> configuration = new HashMap<>();
String path = configPath + File.separator + "test.jceks";
configuration.put("keyStoreType", "JCEKS");
configuration.put("location", path);
configuration.put("modifiable", "false");
//Inicialize credentialStore
credentialStore.initialize(configuration, protectionParameter);
return credentialStore.retrieve(alias, PasswordCredential.class).getPassword();
}
This method is based on your credential store.
I created the store in a different extension instead of jceks. Once that is fixed, I can able to read the password from the store. Took a while to figure that out because WildFly did not complain while creating the store and all worked fine except reading it programatically.