Using ldapRegistry properties in Java running on Liberty Profile - websphere-liberty

I have some Java code running on Liberty Profile which authenticates with MS Active Directory and makes LDAP searches. The code use JNDI and does what it is intended to do. The problem is that the LDAP parameters are hard coded in Java. Works, but could be improved:
ldap.put(Context.PROVIDER_URL, "ldap://ad.foo.bar:389");
ldap.put(Context.SECURITY_PRINCIPAL, "CN=account,OU=A,DC=ad,DC=foo,DC=bar");
ldap.put(Context.SECURITY_CREDENTIALS, "apoorlykeptsecret");
...
ldapCtxt = new InitialDirContext(ldap);
Is it possible to pick the LDAP properties up from an ldapRegistry element in the server.xml file instead? Or am I on the wrong track? Is there a better way to approach this?

Yes, you should be able to configure that using the <ldapRegistry> element in server.xml.
For example:
<ldapRegistry id="ldap" realm="SampleLdapADRealm"
host="ldapserver.mycity.mycompany.com" port="389"
baseDN="cn=users,dc=adtest,dc=mycity,dc=mycompany,dc=com"
bindDN="cn=testuser,cn=users,dc=adtest,dc=mycity,dc=mycompany,dc=com"
bindPassword="testuserpwd"
ldapType="Microsoft Active Directory">
<activedFilters
userFilter="(&(sAMAccountName=%v)(objectcategory=user))"
groupFilter="(&(cn=%v)(objectcategory=group))"
userIdMap="user:sAMAccountName"
groupIdMap="*:cn"
groupMemberIdMap="memberOf:member" >
</activedFilters>
</ldapRegistry>
For full doc see: Configuring LDAP user registries in Liberty
As an alternative quick approach, you could put those values in your server.xml configuration as JNDI entries and obtain the values in your application by JNDI lookup. For example:
<jndiEntry jndiName="ldap/provider_url" value="ldap://ad.foo.bar:389"/>
<jndiEntry jndiName="ldap/secuirty_principal" value="CN=account,OU=A,DC=ad,DC=foo,DC=bar"/>
<!-- WARNING: Storing passwords is JNDI is not secure -->
<jndiEntry jndiName="ldap/security_credentials" value="apoorlykeptsecret"/>
String providerUrl = InitialContext.doLookup("ldap/provider_url");
// etc...

Related

Binding datasource to application when using springBootApplication in Liberty?

When deploying "regular" web apps to Liberty, I was used to binding the global datasource configured in Liberty's server.xml to the individual application by using a child element within the element, like this:
<application context-root="helloApp" location="..." name="helloApp" type="war">
<application-bnd>
<data-source id="db2appDs" name="jdbc/datasource" binding-name="jdbc/theDB"/>
</application-bnd>
...
</application>
<dataSource id="db2ds" jndiName="jdbc/theDB" type="javax.sql.DataSource">
...
</dataSource>
When configuring my first Spring Boot application to deploy to Liberty, I am trying to use the new <springBootApplication> element for it - but I don't seem to be able to add a binding for the datasource I want to use the same way, as this element doesn't seem to support such a child. (It seems to want only <classloader> as a child).
I've seen people suggest I use an #Resource annotation that includes both application-local JDNI name and global JNDI name for the datasorce - but that defeats the purpose, since I don't want to know what the global name is until deploy time.
Is there another way to do this, like we used to before? Or are applications deployed through <springBootApplication> expected to know the global JNDI name of the datasource(s) they want?
Application-defined datasources are not supported for <springBootApplication/>’s. While your application may certainly access a Liberty datasource using its global JNDI name, you should configure the spring.datasource.jndi-name property within your Spring Boot application as described in section 29.1.3 of the Spring Boot features reference. For your example try spring.datasource.jndi-name=jdbc/theDB.

Is there a way to pass application specific properties in Websphere?

We have a websphere application server where multiple applications are deployed. All applications use a common property(Key) but have different Value. For example :
spring.profiles.active=test in one application, spring.profiles.active=UAT in some other application.
Is it possible to pass these different values to the applications during start-up in Websphere ?
If we set these values in JVM options in the Generic JVM Arguments text box then it will become same for all the applications which we don't want.
Set these properties at application level in websphere so that when applications are started -
For application 1 - spring.profiles.active=test
For application 2 - spring.profiles.active=UAT
This document indicates that you can set the spring.profiles.active property in a WebApplicationInitializer per web application. Each application could then read its own specifically named property from System properties. Alternatively if using Liberty (the question didn't specify between traditional WebSphere vs Liberty), then you could use MicroProfile Config to define a property with a common name that is defined differently per application via appProperties, for example as shown in this knowledge center article. But you would still need the WebApplicationInitializer to read the value from MicroProfile Config.
An example would be something like the following:
Config config = ConfigProvider.getConfig();
servletContext.setInitParameter(
"spring.profiles.active",
config.getValue("ProfilesActive", String.class));
server.xml:
<server>
<featureManager>
<feature>mpConfig-1.3</feature>
.. other features
</featureManager>
<application location="app1.war">
<appProperties>
<property name="ProfilesActive" value="test"/>
</appProperties>
</application>
<application location="app2.war">
<appProperties>
<property name="ProfilesActive" value="UAT"/>
</appProperties>
</application>
</server>

NonSqlTransientException in Liberty Server

am facing NonSqlTransientException Null userid not supported while starting the liberty server where my server.xml contain authdata
The authData configuration in Liberty is only for container managed authentication. If you are using application authentication (as is the case for JNDI lookup without resource reference, or if using a resource reference that is set to authentication type of Application), then authData does not apply. If you are using a resource reference with Container authentication, then you can use the authData, but there is an additional configuration step to associate the authData with the dataSource. This can be done in either of the following ways, documented in this knowledge center article.
One option is to configure the containerAuthDataRef of the dataSource to point at the id of the authData element (you'll need to add an id for it if it doesn't have one). Here is an example,
<authData id="myAuth" user="user1" password="pwd1"/>
<dataSource jndiName="jdbc/myDataSource" containerAuthDataRef="myAuth">
<jdbcDriver libraryRef=...
<properties...
</dataSource>
The other option is to specify the authData's id under the authentication-alias in the application's bindings (such as ibm-web-bnd.xml or ibm-ejb-jar-bnd.xml) for the data source. For example, the following bindings are based on the server config from the previous example,
<resource-ref name="java:app/env/jdbc/myDataSourceRef" binding-name="jdbc/myDataSource">
<authentication-alias name="myAuth"/>
</resource-ref>
It should be noted that the former is a default for container authentication that is used in the absence of the latter. So if you specify both ways, then the latter takes precedence and will be used instead.

Configuring env-entry values in WebSphere Liberty?

In WAS Full Profile you can change the values for env-entries defined in an application's web.xml file while and after deployment. Is there any such feature in WebSphere Liberty profile?
Sample env-entry:
<env-entry>
<description>Some Config String</description>
<env-entry-name>MyConfigString</env-entry-name>
<env-entry-type>java.lang.String</env-entry-type>
<env-entry-value>Dev-Value to be replaced in Production during deployment</env-entry-value>
</env-entry>
This env-entry is injected with an #Resource(name = "MyConfigString") annotation.
The infocenter for IBM Worklight (which runs on Liberty) describes a way using jndiEntry elements in server.xml . I tried this on Liberty 8.5.5.3 but it does not work and still injects the default value from web.xml.
Has anybody found a working way to configure env-entry values in Liberty?
The Liberty profile doesn't currently support configuring bindings without modifying the ibm-*-bnd.xml files in the application. As a workaround, you can use:
#Resource(lookup="MyConfigString")
...in the application and:
<jndiEntry jndiName="MyConfigString" value='"xyz"'/>
...in server.xml as described in the knowledge center.

Configuring a JNDI resource that isn't a datasource in Liberty Profile

I'm trying to run an existing WebSphere application in Liberty Profile but have run into a problem. The application has a resource environment entry configured in the server which I need to translate into a Liberty Profile resource. How can I configure a JNDI resource in the server.xml, that isn't a datasource (dataSource) or a constant (jndiEntry)?
Many thanks
You can configure this using the element in the server.xml. This is documented in the infocenter. Essentially you enable the jndi feature in the server.xml using this:
<featureManager>
<feature>jndi-1.0</feature>
</featureManager>
Then you can configure the JNDI entries. You can only do simple types using this, so no complex objects. To configure your entry you then do this:
<jndiEntry jndiName="myProp/philosopher" value="plato" />
The Liberty profile does type inference, so if you expressed this:
<jndiEntry jndiName="myProp/philosopher" value="1234" />
you get an Number from JNDI. If you express this:
<jndiEntry jndiName="myProp/philosopher" value="1234.3D" />
You get a Double.
If you want a number as a string literal you would express it using quotes:
<jndiEntry jndiName="myProp/philosopher" value='"1234.3D"' />
To get this from your application you can do a global lookup such as:
Context ctx = new InitialContext();
Object jndiConstant = ctx.lookup("myProp/philosopher");
String philosopher = (String) jndiConstant;
You can also map this to a resource environment entry in the ibm-web-bnd.xml file:
<env-entry name="philosopher" binding-name="myProp/philosopher" />
and then use this code to look it up:
Context ctx = new InitialContext();
Object jndiConstant = ctx.lookup("java:comp/env/philosopher");
String philosopher = (String) jndiConstant;
Currently this is not possible with Liberty Profile. This question was answered in the IBM WasDev forum here https://developer.ibm.com/answers/questions/6221/resource-environment-entries-in-liberty-profile/?community=wasdev
A RFE process (31525) has been created for it to support it in a future release.
In 8.5.5.x there are several new entries:
For example: To configure an URL you can use the jndiURLEntry

Resources