I am trying to integrate spring vault as an extra module in my web application. I have created a new service EncryptionService which makes a call to my Spring Vault ApiVaultClient as it is shown bellow :
My Vault configuration VaultConfig.java
:
package com.myproject.encryption;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.vault.authentication.ClientAuthentication;
import org.springframework.vault.authentication.TokenAuthentication;
import org.springframework.vault.client.VaultEndpoint;
import org.springframework.vault.config.AbstractVaultConfiguration;
import org.springframework.vault.core.VaultTemplate;
@Configuration
public class VaultConfig extends AbstractVaultConfiguration {
private Properties props;
private String token;
private String host;
private int port;
public VaultConfig() {
this.props = new Properties();
InputStream vaultConfig = this.getClass().getClassLoader().getResourceAsStream("vault-config.properties");
if(vaultConfig!=null) {
try {
props.load(vaultConfig);
} catch (IOException ex) {
//LOG.error(ex.getMessage());
}
} else {
//LOG.error(new FileNotFoundException("property file vault-config.properties not found in the classpath"));
}
this.token = props.getProperty("vault.token");
this.host = props.getProperty("vault.uri");
}
@Override
public ClientAuthentication clientAuthentication() {
return new TokenAuthentication(this.token);
}
@Override
public VaultEndpoint vaultEndpoint() {
VaultEndpoint endpoint = null;
try {
endpoint = VaultEndpoint.from(new URI(this.host));
} catch (URISyntaxException ex) {
//LOG.error(ex.getMessage());
}
return endpoint;
}
/*I added this method hoping that this might solve the bean creation problem of vaultOperations in ApiVaultClient but it didn't work out either*/
@Bean
public VaultTemplate getVaultTemplate() {
VaultTemplate vaultTemplate = new VaultTemplate(vaultEndpoint(), clientAuthentication());
return vaultTemplate;
}
/**
* Setters & Getters
*/
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
}
My REST Client ApiVaultClient.java
:
package com.myproject.encryption;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.vault.core.VaultOperations;
import org.springframework.vault.core.VaultTransitOperations;
@Component("apiVaultClient")
public class ApiVaultClient{
@Autowired
private VaultOperations vaultOperations;
private VaultTransitOperations transitOperations;
private String keyStore;
private Properties props;
@PostConstruct
public void init() {
this.transitOperations = vaultOperations.opsForTransit();
InputStream vaultConfig = this.getClass().getClassLoader().getResourceAsStream("vault-config.properties");
this.props = new Properties();
if(vaultConfig!=null) {
try {
props.load(vaultConfig);
} catch (IOException ex) {
//LOG.error(ex.getMessage());
}
} else {
//LOG.error(new FileNotFoundException("property file vault-config.properties not found in the classpath"));
}
this.keyStore = props.getProperty("vault.key.store");
}
public String encrypt(String clearText) {
String cipherText = transitOperations.encrypt(this.keyStore, clearText);
return cipherText;
}
public String decrypt(String cipherText) {
String clearText = transitOperations.decrypt(this.keyStore, cipherText);
return clearText;
}
public String getKeyStore() {
return keyStore;
}
public void setKeyStore(String keyStore) {
this.keyStore = keyStore;
}
public VaultOperations getVaultOperations() {
return vaultOperations;
}
public void setVaultOperations(VaultOperations vaultOperations) {
this.vaultOperations = vaultOperations;
}
public VaultTransitOperations getTransitOperations() {
return transitOperations;
}
public void setTransitOperations(VaultTransitOperations transitOperations) {
this.transitOperations = transitOperations;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
}
My EncryptionService
implementation :
package com.myproject.business.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import com.myproject.business.service.EncryptionService;
import com.myproject.encryption.ApiVaultClient;
public class EncryptionServiceImpl implements EncryptionService{
@Autowired
private ApiVaultClient apiVaultClient;
@Override
public String encrypt(String clearText) {
return apiVaultClient.encrypt(clearText);
}
@Override
public String decrypt(String cipherText) {
return apiVaultClient.decrypt(cipherText);
}
public ApiVaultClient getApiVaultClient() {
return apiVaultClient;
}
public void setApiVaultClient(ApiVaultClient apiVaultClient) {
this.apiVaultClient = apiVaultClient;
}
}
My vault-config.properties
:
vault.uri=http://127.0.0.1:8200
vault.token=s.ty3r7is9tSlXfC4GFMHvwDa5
vault.key.store=transit/keys/store
My serviceContext.xml
which lies in my business module :
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
<context:component-scan base-package="com.myproject.business.service.impl" />
<context:component-scan base-package="com.myproject.encryption" />
<!-- other services are skipped -->
<bean id="encryptionService" parent="txnProxyTemplate">
<property name="target">
<bean class="com.myproject.business.service.impl.EncryptionServiceImpl">
<property name="apiVaultClient" ref="apiVaultClient" />
</bean>
</property>
</bean>
</beans>
When starting my application I get the following error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'apiVaultClient': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.vault.core.VaultOperations com.myproject.encryption.ApiVaultClientImpl.vaultOperations; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:290)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:410)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.vault.core.VaultOperations com.myproject.encryption.ApiVaultClientImpl.vaultOperations; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:518)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
... 22 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'vaultTemplate' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:603)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1057)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:490)
... 24 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.core.VaultTemplate org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate()] threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:592)
... 36 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientHttpRequestFactoryWrapper' defined in class path resource [com/myproject/encryption/VaultConfig.class]: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:603)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1057)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:305)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.clientHttpRequestFactoryWrapper(<generated>)
at org.springframework.vault.config.AbstractVaultConfiguration.vaultTemplate(AbstractVaultConfiguration.java:108)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.CGLIB$vaultTemplate$12(<generated>)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c$$FastClassBySpringCGLIB$$1da77a86.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:293)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.vaultTemplate(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
... 37 more
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.vault.config.AbstractVaultConfiguration$ClientFactoryWrapper org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper()] threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:181)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:592)
... 58 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.springframework.vault.support.SslConfiguration$KeyStoreConfiguration
at org.springframework.vault.support.SslConfiguration.unconfigured(SslConfiguration.java:323)
at org.springframework.vault.config.AbstractVaultConfiguration.sslConfiguration(AbstractVaultConfiguration.java:225)
at org.springframework.vault.config.AbstractVaultConfiguration.clientHttpRequestFactoryWrapper(AbstractVaultConfiguration.java:207)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.CGLIB$clientHttpRequestFactoryWrapper$13(<generated>)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c$$FastClassBySpringCGLIB$$1da77a86.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:293)
at com.myproject.encryption.VaultConfig$$EnhancerBySpringCGLIB$$5cf9316c.clientHttpRequestFactoryWrapper(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
I tried to follow what most tutorials propose by the book but in vain. Any help will be more than appreciated.