Programmatic access to password from Elytron credential-store

1.4k Views Asked by At

I am using Elytron on WildFly 12 to store a datasource password encoded.

I use the following CLI commands to store the password:

/subsystem=elytron/credential-store=ds_credentials:add( \
    location="credentials/csstore.jceks", \
    relative-to=jboss.server.data.dir, \
    credential-reference={clear-text="changeit"}, \
    create=true)

/subsystem=datasources/data-source=mydatasource/:undefine-attribute(name=password)

/subsystem=elytron/credential-store=ds_credentials:add-alias(alias=db_password, \
    secret-value="datasource_password_clear_text")

/subsystem=datasources/data-source=mydatasource/:write-attribute( \
    name=credential-reference, \
    value={store=ds_credentials, alias=db_password})

This works very well so far. Now I need a way to read this password programmatically, so I can create a PostgreSQL database dump.

1

There are 1 best solutions below

0
On

I found a possibility but somehow it feels like an improper solution.

static final String DB_PASS_ALIAS = "db_password/passwordcredential/clear/";

File keystoreFile = new File(System.getProperty("jboss.server.data.dir"),
"credentials/csstore.jceks");

// Open keystore
InputStream keystoreStream = new FileInputStream(keystoreFile);
KeyStore keystore = KeyStore.getInstance("JCEKS");
keystore.load(keystoreStream, KEYSTORE_PASS.toCharArray());

// Check if password for alias is available
if (!keystore.containsAlias(DB_PASS_ALIAS)) {
  throw new RuntimeException("Alias for key not found");
}

// Get password
Key key = keystore.getKey(DB_PASS_ALIAS, KEYSTORE_PASS.toCharArray());
// Decode password - remove offset from decoded string
final String password = new String(key.getEncoded(), 2, key.getEncoded().length - 2);

I am open to any better solutions.