Saturday, December 12. 2020
Masking passwords for the wildfly-config.xml file
Today's entry is a summary about how to mask passwords inside the wildfly-config.xml file. This file is the client side configuration currently recommended to be used for calling an Enterprise Java Bean (EJB) deployed in a wildfly server. The EJB endpoints are secured by default in wildfly (username and password) and that information needs to be present in the configuration. Masked passwords are used (directly or indirectly) to not display plain passwords inside that file. But the masking process is not as easy as expected, so I decided to create a new blog entry.
Let's start just downloading and installing the new wildfly 21. The server is just exploded and a management (for the console) and an application user (for the EJB interface) are created.
wget https://download.jboss.org/wildfly/21.0.1.Final/wildfly-21.0.1.Final.zip
unzip wildfly-21.0.1.Final.zip
cd wildfly-21.0.1.Final/bin/
./add-user.sh -u admin -p admin
./add-user.sh -a -u ejbuser -p password123
./standalone.sh
Now a very simple test application is created. The hello-worl-ejb app is just a hello world but using the EJB technology. For that an interface is needed.
public interface HelloWorldRemote {
public String sayHello(String name);
}
And the implementation which is annotated to be a stateless enterprise java bean.
@Stateless
@Remote(HelloWorldRemote.class)
public class HelloWorldBean implements HelloWorldRemote {
@Override
public String sayHello(String name) {
return "Hello " + (name == null? "World" : name) + "!";
}
}
Finally a client is developed to call the previous bean and display the resulting salute.
public class HelloWorldClient {
public static void main(String... args) throws Exception {
Properties jndiProperties = new Properties();
jndiProperties.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
InitialContext context = new InitialContext(jndiProperties);
final String lookupName = "ejb:/hello-world-ejb/HelloWorldBean!es.rickyepoderi.ejb.HelloWorldRemote";
HelloWorldRemote helloWorld = (HelloWorldRemote) context.lookup(lookupName);
System.out.println(helloWorld.sayHello(args.length > 0? args[0] : null));
}
}
As you see, in the client nothing is defined about the URL or the login information (although you can use other properties the idea is using the wildfly-config.xml file). My first try is just using a clear password inside a minimal configuration to connect using the new http invocation (for more information about EJB in wildfly you can check this detailed post). So, with no masking, the configuration file can be the following.
<configuration>
<authentication-client xmlns="urn:elytron:client:1.4">
<credential-stores>
<authentication-rules>
<rule use-configuration="clear"/>
</authentication-rules>
<authentication-configurations>
<configuration name="clear">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<clear-password password="password123"/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
<http-client xmlns="urn:wildfly-http-client:1.0">
<defaults></defaults>
</http-client>
<discovery xmlns="urn:wildfly-discovery:1.0">
<discovery-provider>
<static>
<service uri="http://localhost:8080/wildfly-services" abstract-type="ejb" abstract-type-authority="jboss">
<attribute name="ejb-module" value="hello-world-ejb" />
</service>
</static>
</discovery-provider>
</discovery>
</configuration>
The file is quite easy, in the urn:elytron:client section there is only one authentication configuration clear, which specifies the username and password in plain text, and is directly assigned to all the EJB connections by the rule. The urn:wildfly-discovery part is used to configure the wildfly server URL and urn:wildfly-http-client is just added to know that it exists and it can be used to add specific details for the http client. The final project for the sample application can be downloaded at the end of the entry. Just package the application, deploy the resulting war file into the wildfly server and execute using the exec plugin.
cd hello-world-ejb
mvn clean package
# deploy target/hello-world-ejb.war
mvn exec:java
...
[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ hello-world-ejb ---
...
Hello World!
This configuration works but the password for the ejbuser is displayed in plain inside the file. How can it be masked? The only useful information I found about the subject is this entry in Ashley Abdel-Sayed's Blog. Mainly it says that you can use a masked password but some java code is needed. Please do not try to use the elytron-tool.sh command for creating them, its mask option is for picketlink integration and it does not return a valid masked password. Maybe this changes in the future, but now this tool is not suitable for the task. Therefore I have developed another program that using Ashley's idea can be executed to create one. The project can also be downloaded at the end of the entry and it accepts different arguments.
cd masked-pasword
mvn clean package
mvn exec:java -Dexec.args="-s 12345678 -i 100 password123"
...
Masked Password: $masked-MD5-DES$somearbitrarycrazystringthatdoesnotmatter$100$MTIzNDU2Nzg=$/Nym2s/dssNzlEkNlD+ycQ==$o8IbHwo2ZNk=
Snippet: <masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
Password Verified 'true'
...
In the previous example default algorithm is used to create a masked password for password123 with salt 12345678 and one hundred iterations. The tool has a simple usage information that can be used to specify different options (algorithm for example). So now a new authentication configuration can be added to use the previous masked password.
<authentication-client xmlns="urn:elytron:client:1.4">
<authentication-rules>
<rule use-configuration="masked"/>
</authentication-rules>
<authentication-configurations>
<configuration name="masked">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
The clear password is substituted by the masked one reported by the utility program and the new configuration masked is used by the rule. And this works perfectly. The password is masked (reversible algorithm) but at least it is not in plain.
But let's add a final configuration. The wildfly-config.xml can also be configured to use a credential store. A store that contains secret information like passwords. It can be created using the elytron-tool.sh utility.
cd ${WILDFLY_HOME}/bin
./elytron-tool.sh credential-store --create --location "store.jceks" --password password123
./elytron-tool.sh credential-store --location "store.jceks" --password password123 --add ejbpwd --secret password123
cp store.jceks /path/to/hello-world-ejb/
The previous execution initializes a store.jceks credential store with password password123 and adds the alias ejbpwd with the password for the ejbuser inside it. The store file is moved to the project folder to be referenced by the configuration. Now the wildfly-config.xml uses the store and the alias to obtain the password for the EJB user. But the general password for the store should also be provided in the configuration... And how do we provide it not being in clear? Using a masked password of course. So in this last option the authentication section looks like this.
<authentication-client xmlns="urn:elytron:client:1.4">
<credential-stores>
<credential-store name="store">
<attributes>
<attribute name="keyStoreType" value="JCEKS" />
<attribute name="location" value="./store.jceks" />
</attributes>
<protection-parameter-credentials>
<masked-password iteration-count="100" salt="12345678" initialization-vector="o8IbHwo2ZNk=" masked-password="/Nym2s/dssNzlEkNlD+ycQ=="/>
</protection-parameter-credentials>
</credential-store>
</credential-stores>
<authentication-rules>
<rule use-configuration="store"/>
</authentication-rules>
<authentication-configurations>
<configuration name="store">
<sasl-mechanism-selector selector="DIGEST-MD5"/>
<set-user-name name="ejbuser"/>
<credentials>
<credential-store-reference store="store" alias="ejbpwd"/>
</credentials>
</configuration>
</authentication-configurations>
</authentication-client>
This last configuration is the most complete. The authentication section uses a credential store. It can handle several aliases with different secrets associated. And the general password for the store is presented using a masked password.
So the summary is that although the wildfly-config.xml can manage masked passwords to not present them in plain, the information is not very clear and you need some java code to generate them. In this entry a maven project (you can download it from here) was used to generate the masked passwords following the same idea that Ashley Abdel-Sayed presented in his blog. This way it's easier to create them. Finally the maven project for the EJB test application can also be downloaded from here. The final wildfly-config.xml in the project contains the three authentication configurations presented in the entry. You can switch from one to the other just modifying the rule (use-configuration attribute).
Best regards!
Comments