28
Gentoo Tip – Emerging Git so that it works with Subversion
1 Comment · Posted by chrislovecnm in Uncategorized
So you are wanting to use git to clone a subversion repository because you know that git rocks and subversion is so 5 years ago! Like a great geek you run:
% git svn clone https://svn.apache.org/repos/asf/camel/trunk/ camel-trunk
But you then you get the following error:
fatal: git was built without support for git-svn (NO_PERL=YesPlease).
No need to nash you teeth you need to add two lines to /etc/portage/package.use file..
dev-vcs/git subversion perl
dev-vcs/subversion perl -dso
No tags
16
Encrypting Spring 3 Java Based Configurations Values With Jasypt
2 Comments · Posted by chrislovecnm in Java
Since I have gotten a TON of comments and traffic in regards to my intial spring configuration post located here, I thought I would add another post about spring config and @values.
Encrypting passwords is always a pain in the butt. No other way to put it. To add insult to injury we needed to encrypt a password with-in a spring based web application. Well jasypt (Java Simplified Encryption) project to the rescue. They support basic spring property value replacement of encrypted value with-in spring 2.0 supported xml out of the box.
You have nothing special to do to integrate Jasypt with Spring, as all of the encryption tools (digesters and encryptors) in jasypt have the adequate design to be correctly instantiated and dependency-injected from a Spring application context. 1
I have created a properties loader that supports spring 3.0 @value injection. On to the examples that can be d/l from my github repo.
Basic Spring Config
<bean id="environmentVariablesConfiguration" class="org.jasypt.encryption.pbe.config.EnvironmentPBEConfig"
p:algorithm="PBEWithMD5AndDES" p:passwordSysPropertyName="APP_PASS" />
<bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor"
p:config-ref="environmentVariablesConfiguration" />
<bean id="propertyConfigurer"
class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer"
p:locations-ref="passwordProps">
<constructor-arg ref="configurationEncryptor" />
</bean>
<util:list id="passwordProps">
<value>classpath:/cnm.properties</value>
</util:list>
<bean id="testBean" class="test.beans.BeanTestImpl" p:foo="${cnm.foo}" />
In this example, the encryption password will be read from an system variable called “APP_PASS”. This is the password that is used to decrypt encrypted values. The EncryptablePropertyPlaceholderConfigurer will read the cnm.properties file and make its values accessible as ${vars}. A “configurationEncryptor” bean (which implements org.jasypt.encryption.StringEncryptor) is set as a constructor arg.
Simple Spring Unit Test
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:/applicationContext-jasypt.xml"})
public class BasicJasyptTest {
@Autowired
private ITestBean testBean;
@BeforeClass
public static void setupEnvVar() {
System.setProperty("APP_PASS", "151987thisismysalt!!");
}
@Test
public void testBean() {
assertThat(testBean.getFoo(), equalTo("MyPassword"));
}
}
The above test creates the spring application context and injects the decrypted values correctly. The jaspypt beans are using a system property that contains the password that is used to decrypt the values. When the context is created the jasypt components loads and decrypts the properties values that are delimited with the ‘ENC()’ marker.
For example:
- cnm.username=clove is left alone
- cnm.foo=ENC(1FTzaim1KT6w12vRRyCICP/6lPklg1Jw) is decrypted
Well that is awesome and all … but very XML intensive, and not cool enough for me. So lets move on to using @value’s in spring 3.0.
Spring Config Using 3.0 Contexts
<context:component-scan base-package="test.beans" /> <bean id="pbeConfig" class="net.cnmconsulting.spring.config.util.FileStringPBEConfig" p:algorithm="PBEWithMD5AndDES" p:passwordName="classpath:/cnm.token"> </bean> <bean id="configurationEncryptor" class="org.jasypt.encryption.pbe.StandardPBEStringEncryptor" p:config-ref="pbeConfig" /> <bean id="propertyConfigurer" class="org.jasypt.spring.properties.EncryptablePropertyPlaceholderConfigurer" p:locations-ref="passwordProps" p:searchSystemEnvironment="true"> <constructor-arg ref="configurationEncryptor" /> </bean> <bean id="props" class="net.cnmconsulting.spring.config.PropertiesFactoryBean" p:locations-ref="passwordProps"> <constructor-arg ref="configurationEncryptor" /> </bean> <util:set id="passwordProps"> <value>classpath:/cnm.properties</value> </util:set>
So two new components are in the spring config above. First we are using context:component to load beans automatically, and secondly our PropertiesFactoryBean is being used that allows for @value injections. Let’s look at part of the the bean.
Spring Bean with encrypted @value
@Service("propTest")
public class PropTestBeanImpl implements PropTest {
@Value("#{props['cnm.username']}")
protected String username;
@Value("#{props['cnm.foo']}")
protected String password;
The bean is pretty self explanatory. Set the cnm.foo value to the field password, and this is what the PropertiesFactoryBean does.
Let’s test it!
Spring Bean Unit Test for Encypted @Value
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath*:/applicationContext-cmn-props.xml"})
public class CNMPropertiesFactoryBeanTest {
// Inject the bean for testing
@Autowired
private PropTest propTest;
@BeforeClass
public static void setupEnvVar() {
System.setProperty("APP_PASS", "151987thisismysalt!!");
}
/*
* Test will assure that the propTest bean will have the correct values injects,
* and the encypted values are decrypted upon injection
*/
@Test
public void testProp() {
assertNotNull(propTest.getUsername());
assertThat(propTest.getFoo(), equalTo("MyPassword"));
assertThat(propTest.getUsername(), equalTo("clove"));
}
}
As you can see the propTest bean is tested to ensure that the password and username are set correctly.
Again the examples are located in my github repo. Please download fork and improve!
In closing let me mention that jasypt (Java Simplified Encryption) project is very useful and wraps multiple encryption components very elegantly. Check out the feature list. Also you may wonder, how the heck do I create the encrypted passwords using jasypt. My PropertyAppContextReplaceTest unit test provides an example.
Configuration · Encryption · Spring · Value
Springsource has released the Javaconfig Framework as a core component of Spring 3.0. There is a trend in the industry to move from XML meta data toward using more annotation driven meta data. I say pick your poison, as one can mess up either.
I do like the readability of using Java code for configuration. Reading the Java classes used for the configuration has a shorter learning curve than reading XML files. Also I can directly unit test my configuration.
The Example
File Highlights
The example code located in on github here. I am using eclipse m2eclipse and spring plugins, and I would recommend importing the project as a maven project.
The Java class used for configuration:
@Configuration
// spring config that loads the properties file
@ImportResource("classpath:/properties-config.xml")
public class AppConfig {
/**
* Using property 'EL' syntax to load values from the
* jetProperties value
*/
private @Value("#{jetProperties['jetBean.name']}") String name;
private @Value("#{jetProperties['jetBean.price']}") Long price;
private @Value("#{jetProperties['jetBean.url']}") URL url;
/**
* Create a jetBean within the Spring Application Context
* @return a bean
*/
public @Bean(name = "jetBean")
JetBean jetBean() {
JetBean bean = new JetBeanImpl();
bean.setName(name);
bean.setPrice(price);
bean.setUrl(url);
return bean;
}
}
The highlights for this class:
- @Configuration – Basic annotation for Java based configuration.
- @ImportResource – Allows us to import a spring xml file to add more functionality to the configuration that this class is building.
- @Value – Creates expression driven dependency injection.
- @Bean – Create a bean managed by the spring container.
The properties-config.xml file:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- define the properties file to use -->
<util:properties id="jetProperties"
location="classpath:/jet.properties" />
</beans>
One line that matters. Line number 8. Create a java.util.Properties instance with values loaded from the supplied properties file.
Other files such as the POJO’s and properties files are included in the example. They are very vanilla, so I did not include them in the post. The test case has some changes in it.
public class JetBeanTest {
@Test
public void testJetBean() {
// create the spring container using the AppConfig
// @Configuration class
ApplicationContext ctx =
new AnnotationConfigApplicationContext(AppConfig.class);
JetBean jetBean = ctx.getBean(JetBean.class);
assertThat(jetBean.getName(), equalTo("Gulf Stream G550"));
assertThat(jetBean.getPrice(), equalTo(Long.valueOf(60000000)));
URL gulfstream;
try {
gulfstream =
new URL("http://www.gulfstream.com/products/g550/");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
fail("error creating URL");
throw new RuntimeException("error creating URL");
}
assertThat(jetBean.getUrl(), equalTo(gulfstream));
}
}
The main change is the new class AnnotationConfigApplicationContext which drives the creation of the Spring application context via Java configuration.
Recap
Use @Configuration to annotate your configuration class. Mark your beans with @Bean. The following annotations are supported:
- @Configuration
- @Bean
- @DependsOn
- @Primary
- @Lazy
- @Import
- @ImportResource
- @Value
Using @Value with Spring Expression Language
Now starts the crazy cool stuff. The @Value can be used on fields, methods and parameters. Plus, Spring Expression Language (SpEL) defines the ‘value’ through the syntax #{ < SpEL expression > }. The syntax can get a bit harry, but is incredibly powerful. In this example I have used SpEL to load values from the javaProperties java.lang.Properties Object with syntax like:
// jetProperties java.lang.Properties bean in context.
// jetBean.name value in the property file
@Value("#{jetProperties['jetBean.name']}") String name;
Using @Value and SpEL gives you the functionality to do such things as: setting default values, accessing system level properties, logical operators, regex, mathematical operations.
To summarize as Uncle Ben said:
With great power there must come great responsibility.
Link to the example code located in on github here.
Much thanks to Chris Beams and his blog post on Springsource.
Configuration · Java · Spring · Value