Keycloak: Outbound SSL Connection in SPI - keycloak

I am implementing a custom OIDC Protocol Mapper for Keycloak 4.x. Within I have to make a https connection, which currently fails because I do not have a truststore configured on my http connection.
I have added a truststore SPI to my standalone.xml (as described here https://www.keycloak.org/docs/latest/server_installation/index.html#_truststore):
<spi name="truststore">
<provider name="file" enabled="true">
<properties>
<property name="file" value="${jboss.server.config.dir}/nak.test.jks"/>
<property name="password" value="****"/>
<property name="hostname-verification-policy" value="WILDCARD"/>
<property name="disabled" value="false"/>
</properties>
</provider>
</spi>
But I do not know how to get a HttpClient instance which uses this truststore. I have tried with:
org.apache.http.client.HttpClient httpClient = new org.keycloak.connections.httpclient.HttpClientBuilder().build();
and
DefaultHttpClientFactory factory = new DefaultHttpClientFactory();
HttpClientProvider httpClientProvider = factory.create((KeycloakSession)session);
return httpClientProvider.getHttpClient();
whereas the first just does not have the truststore configured, and the second fails with a NPE.
So how do I get an outbound http connection which uses my truststore SPI?

See https://lists.jboss.org/pipermail/keycloak-user/2018-July/014878.html
I implemented as follows, which uses the Truststore SPI of Keycloak:
private org.apache.http.client.HttpClient getConnection(org.keycloak.models.KeycloakSession session) {
org.keycloak.connections.httpclient.HttpClientProvider cp = session.getProvider(org.keycloak.connections.httpclient.HttpClientProvider.class);
HttpClient httpClient = cp.getHttpClient();
return httpClient;
}

Related

safety disconnect oracle db from a jboss application

I'm maintenance a old system it run in a jboss container,and it use ibatis and spring access an oracle db. Now this system's db related functions are discarded and the db will be closed. How should i do to safety disconnect this system with db (assuming the application code can deal with all exceptions except SqlException)
the key configuration is as follows:
xxx-ds.xml:
<datasources>
...
</datasources>
daoContext.xml:
<jee:jndi-lookup id="oracleSource" jndi-name="java:/DefaultDS"/>
<!-- SqlMap setup for iBATIS Database Layer -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="META-INF/sql-map-config.xml"/>
</bean>
<bean id="xxxx" class="path.to.class.xxxxDao">
<property name="dataSource" ref="oracleSource"/>
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean>
path.to.class.xxxxDao is extends org.springframework.orm.ibatis.support.SqlMapClientDaoSupport and implements db access methods.
Replace jndi data source with a mock db

The endpoint reference (EPR) for the Operation not found using glassfish n wso2esb

I am new to wso2 esb.
I am trying a simple web service program.
package testmart;
import javax.jws.WebService;
#WebService
public class testone {
public String testMethod()
{
return "success";
}
}
I am using glassfish server, created a proxy on wso2 esb by giving following details:
web service url: http://localhost:8080/testmart/testoneService
wsdl url :http://localhost:8080/testmart/testoneService?wsdl
After creating its showing "success" msg. But when I test it, the following error comes up:
<soapenv:Fault xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:axis2ns7="http://www.w3.org/2003/05/soap-envelope">
<soapenv:Code>
<soapenv:Value>axis2ns7:Client</soapenv:Value>
</soapenv:Code>
<soapenv:Reason>
<soapenv:Text xml:lang="en-US" xmlns:xml="http://www.w3.org/XML/1998/namespace">
The endpoint reference (EPR) for the Operation not found is /services/testmart1.testmart1HttpSoap12Endpoint and the WSA Action = null. If this EPR was previously reachable, please contact the server administrator.
</soapenv:Text>
</soapenv:Reason>
<soapenv:Detail/>
</soapenv:Fault>
Below is my esb source view code:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://ws.apache.org/ns/synapse">
<registry provider="org.wso2.carbon.mediation.registry.WSO2Registry">
<parameter name="cachableDuration">15000</parameter>
</registry>
<proxy name="testmart1"
transports="https http local"
startOnLoad="true"
trace="disable">
<description/>
<target>
<endpoint>
<address uri="http://localhost:8080/testmart/testoneService"/>
</endpoint>
<outSequence>
<send/>
</outSequence>
</target>
<publishWSDL uri="http://localhost:8080/testmart/testoneService?wsdl"/>
</proxy>
<sequence name="fault">
<log level="full">
<property name="MESSAGE" value="Executing default 'fault' sequence"/>
<property name="ERROR_CODE" expression="get-property('ERROR_CODE')"/>
<property name="ERROR_MESSAGE" expression="get-property('ERROR_MESSAGE')"/>
</log>
<drop/>
</sequence>
<sequence name="main">
<in>
<log level="full"/>
<filter source="get-property('To')" regex="http://localhost:9000.*">
<send/>
</filter>
</in>
<out>
<send/>
</out>
<description>The main sequence for the message mediation</description>
</sequence>
</definitions>
I read all post about this error but could not solve this problem.
Any sort of help would be highly appreciated as this was the first example I tried and stuck from hours.
Thanks in advance.
When you send the request check if the SOAPAction is set. If not, you can specify it in two ways:
ESB level
Set the property before the send mediator
<property name="SOAPAction" value="urn:SOAPAction" scope="transport"/>
Client level
You can specify the SOAPAction in the client side code. Specify it in the options as shown below.
options.setAction("urn:SOAPAction");
in web interface from WSO2 ESB click "Source View" for your proxy and put this lines:
...
<parameter name="serviceType">proxy</parameter>
<parameter name="disableOperationValidation">true</parameter>
...
before the tag:<description/>
On Websphere Application Server, in the same situation, it helped deleting the Temp folders while the server was stopped.
You are using Wso2ESB, so you must need to specify like this:
#WebMethod(action = "testMethod", operationName = "testMethod")
public String testMethod()
The above annotation has to be used in the Web service implementation class.

Set keep-alive in HttpClient in Spring xml

We are using the apache HttpClient library to manage a pool of connections for some Java remoting.
Looking at the TCP traffic, it appears that the keep-alive flag is NOT being set, and our firewall is chopping off these connections.
Our spring config looks like this:
<bean name="httpClient" class="org.apache.commons.httpclient.HttpClient">
<property name="timeout" value="${pdcCreditCheck.maxTimeout}"/>
<property name="httpConnectionManager" ref="remotingConnectionManger"/>
</bean>
<bean id="remotingConnectionManger"
class="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager">
<property name="maxTotalConnections" value="5"/>
<property name="maxConnectionsPerHost" value="5"/>
</bean>
I was hoping that there might be some flag I could set here, which corresponds to the TCP keep-alive flag.
Any help would be appreciated.
You might try to use the JVM flag http.keepAlive. For more info:
http://docs.oracle.com/javase/7/docs/technotes/guides/net/properties.html

Problems changing the default configuration of JBoss Mail service

Right now i have to make migration of one application.
Basic configuration:
JBOSS 5.1.0 GA
Seam 2.2.0.GA
JBPM 3.2.2
RichFaces 3.3.1.GA
Hibernate 3.3.1.GA
OS: Debian/Linux
Application has been deployed on JBoss server with its own configuration.
On old machine there was postfix (localhost, port 25) for sending messages.
On new machine i have different host and port.
What i have now in components.xml:
<mail:mail-session session-jndi-name="java:/Mail" />
My mail-service.xml:
<mbean code="org.jboss.mail.MailService"
name="jboss:service=Mail">
<attribute name="JNDIName">java:/Mail</attribute>
<attribute name="User">XXX</attribute>
<attribute name="Password">XXX</attribute>
<attribute name="Configuration">
<!-- A test configuration -->
<configuration>
<!-- Change to your mail server prototocol -->
<property name="mail.store.protocol" value="pop3"/>
<property name="mail.transport.protocol" value="smtp"/>
<!-- Change to the user who will receive mail -->
<property name="mail.user" value="XXX"/>
<!-- Change to the mail server -->
<property name="mail.pop3.host" value="localhost"/>
<!-- Change to the SMTP gateway server -->
<property name="mail.smtp.host" value="XXX"/>
<property name="mail.smtp.auth" value="true"></property>
<property name="mail.smtp.ssl.enable" value="false"></property>
<property name="mail.smtp.port" value="587"/>
<property name="mail.smtp.connectiontimeout" value="20000"></property>
<property name="mail.smtp.timeout" value="20000"></property>
<!-- The mail server port -->
<!-- Change to the address mail will be from -->
<property name="mail.from" value="XXX"/>
<!-- Enable debugging output from the javamail classes -->
<property name="mail.debug" value="true"/>
</configuration>
</attribute>
<depends>jboss:service=Naming</depends>
I'm intrested only in sending messages.
But still:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1282)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
<xxx>
Caused by: java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at java.net.Socket.connect(Socket.java:478)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250)
... 59 more
Of course connection is refused because application is still pointed on localhost:25.
I have tried to find with grep other configurations, allocations, etc. mail-service is started (jmx-console info). Still its looked like aplication is not configured properly but using defaults.. Whats wrong?
PS: I'm not specialist in JBoss. I don't have root account on this hosting server.

How to perform JAXB2 marshalling of input request in REST with Spring MVC3?

I am new to Spring MVC (Spring 3) and REST, and I am trying out a little toy app to try out GET and POST web services. I have read the Spring's official reference, and found these questions in Stackoverflow, RequestBody of a REST application , Pass a request parameter in Spring MVC 3 but I am still stucked in making it works. Can anyone give me hints on what I missed?
My Controller is like this:
#Controller
#RequestMapping(value = "/echo")
public class EchoControllerImpl implements EchoController {
#RequestMapping(method = RequestMethod.POST, value = "/echoByPost")
public ModelAndView echoByPost(#ModelAttribute EchoDto input) {
// ...
}
}
I have putted corresponding converter in the app ctx:
<mvc:annotation-driven />
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller" >
<property name="classesToBeBound">
<list>
<value>foo.EchoDto</value>
</list>
</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="marshallingHttpMessageConverter" />
<ref bean="stringHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean id="marshallingHttpMessageConverter"
class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
</bean>
<!-- other beans like ViewResolvers -->
I even tried to add these to web.xml as I saw somewhere mentioning about it (though I don't really know what does it means)
<filter>
<filter-name>httpPutFormFilter</filter-name>
<filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpPutFormFilter</filter-name>
<servlet-name>spring-rest</servlet-name>
</filter-mapping>
Then I tried to invoke my web service by Curl:
curl -d "echoDto=<EchoDto><message>adrian</message></EchoDto>" http://localhost:8001/foo/rest/echo/echoByPost.xml
I found that my incoming object is not created through unmarshalling of JAXB2. Instead, seems that the ctor EchoDto(String) is called which the contains whole request xml message.
(I also tried annotating the parameter with #RequestBody instead but it is even worse, that I cannot even invoke the controller method)
Can someone tell me what I have missed?
The Jaxb2Marshaller is setup correctly with the DTO class, coz I am able to use it as returned model object in case of another GET REST webserivce call.
You need to set the content type of the request and you don't need the echoDto=:
curl -H "Content-Type: application/xml" -d "<EchoDto><message>adrian</message></EchoDto>" http://localhost:8001/foo/rest/echo/echoByPost.xml