2

我在 WildFly 12 上使用 Elytron 来存储编码的数据源密码。

我使用以下 CLI 命令来存储密码:

/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})

到目前为止,这非常有效。现在我需要一种以编程方式读取此密码的方法,以便创建 PostgreSQL 数据库转储。

4

1 回答 1

1

我发现了一种可能性,但不知何故,这感觉像是一个不恰当的解决方案。

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);

我愿意接受任何更好的解决方案。

于 2018-05-03T09:30:10.943 回答