Skip to content

Commit

Permalink
JAMES-1644 Add JMAP keystore configuration
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.apache.org/repos/asf/james/project/trunk@1719326 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
mbaechler committed Dec 11, 2015
1 parent f74de9c commit acc6ed6
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 30 deletions.
4 changes: 4 additions & 0 deletions dockerfiles/run/guice/destination/conf/jmap.properties
@@ -0,0 +1,4 @@
# Configuration file for JMAP

tls.keystoreURL=file://conf/keystore
tls.secret=james72laBalle
@@ -0,0 +1,4 @@
# Configuration file for JMAP

tls.keystoreURL=file://conf/keystore
tls.secret=james72laBalle
Expand Up @@ -18,11 +18,19 @@
****************************************************************/ ****************************************************************/
package org.apache.james.jmap; package org.apache.james.jmap;


import java.io.FileNotFoundException;

import javax.inject.Singleton;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.james.filesystem.api.FileSystem;
import org.apache.james.jmap.methods.RequestHandler; import org.apache.james.jmap.methods.RequestHandler;
import org.apache.james.jmap.model.ProtocolRequest; import org.apache.james.jmap.model.ProtocolRequest;
import org.apache.james.jmap.model.ProtocolResponse; import org.apache.james.jmap.model.ProtocolResponse;


import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Names; import com.google.inject.name.Names;


public class JMAPModule extends AbstractModule { public class JMAPModule extends AbstractModule {
Expand All @@ -46,4 +54,16 @@ public ProtocolResponse process(ProtocolRequest request) {
bindConstant().annotatedWith(Names.named(JMAPServer.DEFAULT_JMAP_PORT)).to(DEFAULT_PORT); bindConstant().annotatedWith(Names.named(JMAPServer.DEFAULT_JMAP_PORT)).to(DEFAULT_PORT);
} }


@Provides
@Singleton
JMAPConfiguration provideConfiguration(FileSystem fileSystem) throws FileNotFoundException, ConfigurationException{
PropertiesConfiguration configuration = getConfiguration(fileSystem);
String keystore = configuration.getString("tls.keystoreURL");
String secret = configuration.getString("tls.secret");
return new JMAPConfiguration(keystore, secret);
}

private PropertiesConfiguration getConfiguration(FileSystem fileSystem) throws FileNotFoundException, ConfigurationException {
return new PropertiesConfiguration(fileSystem.getFile(FileSystem.FILE_PROTOCOL_AND_CONF + "jmap.properties"));
}
} }
Expand Up @@ -51,7 +51,6 @@ public JMAPModuleConfigurationPerformer(JMAPServer server, JamesSignatureHandler


@Override @Override
public void initModule() throws Exception { public void initModule() throws Exception {
signatureHandler.configure(null);
signatureHandler.init(); signatureHandler.init();
server.configure(null); server.configure(null);
} }
Expand Down
Expand Up @@ -19,9 +19,16 @@


package org.apache.james.modules; package org.apache.james.modules;


import java.io.FileNotFoundException;

import javax.inject.Singleton;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.james.jmap.JMAPConfiguration;
import org.apache.james.jmap.JMAPServer; import org.apache.james.jmap.JMAPServer;


import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.name.Names; import com.google.inject.name.Names;


public class TestJMAPServerModule extends AbstractModule{ public class TestJMAPServerModule extends AbstractModule{
Expand All @@ -30,4 +37,10 @@ public class TestJMAPServerModule extends AbstractModule{
protected void configure() { protected void configure() {
bindConstant().annotatedWith(Names.named(JMAPServer.DEFAULT_JMAP_PORT)).to(1080); bindConstant().annotatedWith(Names.named(JMAPServer.DEFAULT_JMAP_PORT)).to(1080);
} }

@Provides
@Singleton
JMAPConfiguration provideConfiguration() throws FileNotFoundException, ConfigurationException{
return new JMAPConfiguration("keystore", "james72laBalle");
}
} }
Empty file.
@@ -0,0 +1,38 @@
/****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one *
* or more contributor license agreements. See the NOTICE file *
* distributed with this work for additional information *
* regarding copyright ownership. The ASF licenses this file *
* to you under the Apache License, Version 2.0 (the *
* "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, *
* software distributed under the License is distributed on an *
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
* KIND, either express or implied. See the License for the *
* specific language governing permissions and limitations *
* under the License. *
****************************************************************/
package org.apache.james.jmap;

public class JMAPConfiguration {

public final String keystore;
public final String secret;

public JMAPConfiguration(String keystore, String secret) {
this.keystore = keystore;
this.secret = secret;
}

public String getKeystore() {
return keystore;
}

public String getSecret() {
return secret;
}
}
Expand Up @@ -33,10 +33,8 @@
import javax.inject.Singleton; import javax.inject.Singleton;


import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.filesystem.api.FileSystem; import org.apache.james.filesystem.api.FileSystem;
import org.apache.james.lifecycle.api.Configurable; import org.apache.james.jmap.JMAPConfiguration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;


Expand All @@ -45,7 +43,7 @@
import com.google.common.base.Throwables; import com.google.common.base.Throwables;


@Singleton @Singleton
public class JamesSignatureHandler implements SignatureHandler, Configurable { public class JamesSignatureHandler implements SignatureHandler {


private static final Logger LOGGER = LoggerFactory.getLogger(JamesSignatureHandler.class); private static final Logger LOGGER = LoggerFactory.getLogger(JamesSignatureHandler.class);


Expand All @@ -54,28 +52,25 @@ public class JamesSignatureHandler implements SignatureHandler, Configurable {
public static final String JKS = "JKS"; public static final String JKS = "JKS";


private final FileSystem fileSystem; private final FileSystem fileSystem;
private String secret; private final JMAPConfiguration jmapConfiguration;
private String keystoreURL;
private PrivateKey privateKey; private PrivateKey privateKey;
private PublicKey publicKey; private PublicKey publicKey;



@Inject @Inject
@VisibleForTesting JamesSignatureHandler(FileSystem fileSystem) { @VisibleForTesting JamesSignatureHandler(FileSystem fileSystem, JMAPConfiguration jmapConfiguration) {
this.fileSystem = fileSystem; this.fileSystem = fileSystem;
} this.jmapConfiguration = jmapConfiguration;

public void configure(HierarchicalConfiguration configuration) throws ConfigurationException {
keystoreURL = configuration.getString("tls.keystoreURL", "file://conf/keystoreURL");
secret = configuration.getString("tls.secret", "");
} }


@Override @Override
public void init() throws Exception { public void init() throws Exception {
KeyStore keystore = KeyStore.getInstance(JKS); KeyStore keystore = KeyStore.getInstance(JKS);
InputStream fis = fileSystem.getResource(keystoreURL); InputStream fis = fileSystem.getResource(jmapConfiguration.getKeystore());
keystore.load(fis, secret.toCharArray()); keystore.load(fis, jmapConfiguration.getSecret().toCharArray());
publicKey = keystore.getCertificate(ALIAS).getPublicKey(); publicKey = keystore.getCertificate(ALIAS).getPublicKey();
Key key = keystore.getKey(ALIAS, secret.toCharArray()); Key key = keystore.getKey(ALIAS, jmapConfiguration.getSecret().toCharArray());
if (! (key instanceof PrivateKey)) { if (! (key instanceof PrivateKey)) {
throw new Exception("Provided key is not a PrivateKey"); throw new Exception("Provided key is not a PrivateKey");
} }
Expand Down
Expand Up @@ -24,10 +24,8 @@
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;


import org.apache.commons.configuration.HierarchicalConfiguration;
import org.apache.james.filesystem.api.FileSystem; import org.apache.james.filesystem.api.FileSystem;

import org.apache.james.jmap.JMAPConfiguration;
import com.google.common.collect.Lists;


public class JamesSignatureHandlerProvider { public class JamesSignatureHandlerProvider {


Expand All @@ -48,19 +46,9 @@ public File getBasedir() throws FileNotFoundException {
return null; return null;
} }
}; };
JamesSignatureHandler signatureHandler = new JamesSignatureHandler(fileSystem); JamesSignatureHandler signatureHandler = new JamesSignatureHandler(fileSystem, new JMAPConfiguration("keystore", "james72laBalle"));
signatureHandler.configure(createTestCConfiguration());
signatureHandler.init(); signatureHandler.init();
return signatureHandler; return signatureHandler;
} }


private HierarchicalConfiguration createTestCConfiguration() {
HierarchicalConfiguration configuration = new HierarchicalConfiguration();
HierarchicalConfiguration.Node secretNode = new HierarchicalConfiguration.Node();
secretNode.setName("secret");
secretNode.setValue("james72laBalle");
configuration.addNodes("tls", Lists.newArrayList(secretNode));
return configuration;
}

} }

0 comments on commit acc6ed6

Please sign in to comment.