Flyway executing SQL DDL scripts using Kerberos auth - java

I have a use case where need to integrate Flyway within application and execute the DDL scripts during application start up which is a normal flyway work of executing the scripts on basis of seeing diff in schema_version table.
The DB used is MSSQL server
The problem here is when defining the FluentConfiguration and using it in creating Flyway Bean
#Bean
public FluentConfiguration flywayBaseConfiguration() {
FluentConfiguration configuration = new FluentConfiguration();
configuration.baselineOnMigrate(true);
configuration.locations("classpath:com/schemafiles/db_release/DBSQLSERVER");
configuration.table("schema_version");
configuration.sqlMigrationPrefix("");
configuration.sqlMigrationSeparator("UTC-");
configuration.dataSource(String.format("jdbc:jtds:sqlserver:/DBDEVSERVER/TestDB", confManager.getOrThrow(DB_URL))
, confManager.getOrThrow("db.username"));
return configuration;
}
#Bean
public Flyway flyway(FluentConfiguration fluentConfiguration) {
Flyway flyway = new Flyway(fluentConfiguration);
flyway.migrate();
return flyway;
}
This fluent config defined above will be used for connecting flyway to DB.
Now if i see the data source connection for the application connecting to prod DB its as follows
<bean id="prodDataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="connectionProperties">
<props>
<prop key="appName">${app.name}</prop>
<prop key="useKerberos">true</prop>
<prop key="loginTimeout">${db.login.timeout}</prop>
<prop key="socketTimeout">${db.socket.timeout}</prop>
</props>
</property>
<property name="driverClassName" value="net.sourceforge.jtds.jdbc.Driver"/>
<property name="url" value="jdbc:jtds:sqlserver://DBDEVSERVER/TestDB"/>
</bean>
This is using Kerberos auth in connecting to DB . We are only providing the user name and rest everything is governed by kerberos security realm.
The question is does Flyway Fluent Configuration support something related to Kerberos auth in order to be able to execute the DDL scripts without providing the password ??..

Related

Spring data JPA - Do not hard code password in XML file

I am writing a Spring Jar (Without Spring boot) which connects to database. Almost all the tutorials which I saw connects to Database using the spring XML and the password is hard coded in the XML file.
This is in no way allowed in production environment and way out of standards in terms of security.
Can someone please help me if there is a way to inject password from a method call which inturn retrieves the password from a secured vault and provides the password to datasource object in runtime.
something like below.
<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="org.postgresql.Driver" />
<property name="url" value="jdbc:postgresql://localhost:5432/postgres" />
<property name="username" value="postgres" />
<property name="password" value=getPasswordfromSecureVault("username") />
<property name="suppressClose" value="true" />
<jdbc:initialize-database >
<jdbc:script location="create-tables.sql"/>
</jdbc:initialize-database>
Based on the spring boot tag that you have, I'll assume that you're using spring boot for your project. With spring boot, it is very easy to externalize configuration for your application. It also auto-configures your datasource with a connection pool if you use the starters they provide.
In your case, you could benefit from moving to using spring boot's Externalized Configuration.
What that would allow you to do is to use an environment variable to configure your spring application. For example, to configure your password, use the environment variable SPRING_DATASOURCE_PASSWORD. You could also use the Java option -Dspring.datasource.password=[password] when starting the application, for example:
java -jar -Dspring.datasource.password=password app.jar
If using docker containers, the environment variable way is generally my go to as it's very straight forward for people to understand.
If you really want to configure the password from a method call, you can do that too using java configuration. Create a bean of type DataSource and Spring should pick it up and use it.
#Configuration
class AppConfiguration {
#Bean
public DataSource dataSource() {
String password = // Get your password
return DataSourceBuilder.create().password(password).build();
}
}

Passing Multiple Provider/Broker URL Spring JMS ndi template

We are using Spring JMS integration for connecting our application to Tibco EMS product. Our jndi template is defined as follows bean:
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${app.java.naming.factory.initial}</prop>
<prop key="java.naming.provider.url">${app.java.naming.provider.url}</prop>
</props>
</property>
</bean>
Value of app.java.naming.provider.url is defined in property file as of now as :
app.java.naming.provider.url=tcp://server1:7222
I want to make our application pass multiple provider urls is it possible as follows:
app.java.naming.provider.url=tcp://server1:7222,tcp://server2:7222,tcp://server3:7222,tcp://server4:7222
At given time only one server will up, but if other goes down we dont want to change config and re-deploy hence above mechanism.
How Spring JNDI template works with multiple provider urls.

Spring Integration and UriEndpointMapping

I've managed to setup a web service using Spring Integration that goes through a ws inbound gateway and then on to a service activator. Now I want to change the setup so that the inbound gateway is no longer the default endpoint mapping because there are other existing services that need to be available. When I change the UriEndpointMapping so that the gateway is no longer the default endpoint, I get this message: org.springframework.ws.server.EndpointNotFound - No endpoint mapping found for [SaajSoapMessage ....
I've tried changing the UriEndpointMapping to set the mappings property where I set the url to the gateway reference, and I've tried setting the endpointMap property and used a number of different keys, all without success. The Spring Integration documentation doesn't indicate how to set the UriEndpointMapping for multiple endpoints that are mapped to gateways, and the examples I've found on the web don't work for me either. I'm at a loss on how to proceed.
FYI, this configuration needs to be done in a spring xml file if at all possible.
This works:
<bean class="org.springframework.ws.server.endpoint.mapping.UriEndpointMapping">
<property name="defaultEndpoint" ref="reconGateway"/>
</bean>
This is my latest try, but it fails.
<bean class="org.springframework.ws.server.endpoint.mapping.UriEndpointMapping">
<property name="usePath" value="true"/>
<property name="mappings">
<props>
<prop key="http://localhost:8081/intfacade-web/reconService">reconGateway</prop>
</props>
</property>
</bean>
Any help would be greatly appreciated. Oh, I have tried with just the path and that didn't work either.
Thanks!
Anyone?

Can HibernateTemplate coexist with EntityManager?

We have a spring 3 application that still uses the deprecated HibernateTemplate for persistence and want to migrate to the more modern JPA EntityManager.
Is it possible to use both APIs in parallel during the migration (possibly even both in a single transaction), so that we can do the migration in small steps?
Or will we have to do it big bang?
Sure, why not.
The easiest would be to drop your LocalSessionFactoryBean and HibernateTransactionManager configuration and replace it with LocalContainerEntityManagerFactoryBean and JpaTransactionManager, respectively.
Then to obtain a SessionFactory add the HibernateJpaSessionFactoryBean, which exposes the underlying SessionFactory for the EntityManagerFactory.
This way both technologies should peacefully coexist.
There are some reports that doing this leads to a an exception stating No CurrentSessionContext configured!. If you get it add the following to either your persistence.xml
<property name="hibernate.current_session_context_class" value="org.springframework.orm.hibernate4.SpringSessionContext"/>
or jpaProperties of the LocalContainerEntityManagerFactoryBean.
<property name="jpaProperties">
<props>
<prop name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>
<props>
<property>

how do I change persistence.xml at run time

I am new to openJPA.
I have a scenario where, depending upon the server where my application is running, I need to change the settings to persistance.xml.
For eg. if its running on Server A, then it should use different database(different url), different password etc. and if the application is running on Server B then it should use different information.
And could you also tell me, which way should it be done, using datasource or simply putting properties under persistence-unit.
FYI I am using WS app. server 7 and RAD 7.5
Any type of help would be highly appreciated.
You're using an application server so you don't need to set database connection settings in the persistence.xml file. You should be able to create a JNDI data source in your appserver and then use that. EAch server could have the data source have the same JNDI name and then there'll be no need for any persistence.xml differences.
Workshop, JPA, and DataSources seems particularly relevant to you. As does Setting up a JNDI data source in WebSphere 6.0/6.1 and WebSphere + JNDI + Spring Framework + Hibernate.
Are you using Spring? If so, then the problem is easy to solve: you don't put the data source information in your persistence.xml, you put it in your application context and that'll have different configuration on each server.
For example:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:database.properties"/>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${database.class}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
and each server could have a different database.properties file on each server (where each is in the classpath somewhere in this example):
database.username=scratch
database.password=scratch
database.class=oracle.jdbc.OracleDriver
database.url=jdbc:oracle:thin:#localhost:1521:XE
Changing persistence.xml at runtime is going to be problematic as that's not really how JPA is designed.
Of course, you can use JNDI data sources with Spring also.

Categories

Resources