Skip to content

Commit

Permalink
Merge 1333350,1333352,1334031,1334300,GERONIMO-6345,GERONIMO-6348 int…
Browse files Browse the repository at this point in the history
…o trunk

git-svn-id: https://svn.apache.org/repos/asf/geronimo/server/trunk@1339155 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
Ming Xia committed May 16, 2012
1 parent cacca28 commit 9541447
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 96 deletions.
@@ -0,0 +1,98 @@
/*
* 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.geronimo.crypto;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;

import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.geronimo.crypto.AbstractEncryption;

/*
* @version $Rev$ $Date$
*/
public class ConfiguredEncryption extends AbstractEncryption{

private final static Log log = LogFactory.getLog(ConfiguredEncryption.class);

private SecretKeySpec spec;

public ConfiguredEncryption(String location) throws IOException{
File keyFile = new File(location);
ObjectInputStream oin = null;
if (keyFile != null) {
if (keyFile.exists()) {
FileInputStream fi = new FileInputStream(keyFile);
try {
oin = new ObjectInputStream(fi);
spec = (SecretKeySpec) oin.readObject();
} catch (ClassNotFoundException e) {
log.error("Unable to read object or class not found: ", e);
} finally {
if (oin != null)
oin.close();
if (fi != null)
fi.close();
}
} else {
SecureRandom random = new SecureRandom();
random.setSeed(System.currentTimeMillis());
byte[] bytes = new byte[16];
random.nextBytes(bytes);
spec = new SecretKeySpec(bytes, "AES");
File dir = keyFile.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
if (!dir.exists() || !dir.isDirectory()) {
throw new IllegalStateException(
"Could not create directory for secret key spec: "
+ dir);
}
FileOutputStream out = new FileOutputStream(keyFile);
try {
ObjectOutputStream oout = new ObjectOutputStream(out);
try {
oout.writeObject(spec);
oout.flush();
} finally {
oout.close();
}
} finally {
out.close();
}
log.info("Generate a new configured encryption password: "+spec.getEncoded().toString());
}
}
}

@Override
protected SecretKeySpec getSecretKeySpec() {
return spec;
}

}
Expand Up @@ -16,9 +16,13 @@
*/
package org.apache.geronimo.crypto;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.io.Serializable;
import org.apache.geronimo.crypto.ConfiguredEncryption;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
* A static class that uses registered Encryption instances to encypt and decrypt objects, typically strings.
Expand All @@ -40,16 +44,29 @@
*/
public class EncryptionManager {

private static final Map<String, Encryption> ENCRYPTORS = new ConcurrentHashMap<String, Encryption>();
private static final Map<String, Encryption> ENCRYPTORS = Collections.synchronizedMap(new HashMap<String, Encryption>());
private final static String SIMPLE_ENCRYPTION_PREFIX = "{Simple}";
private final static String CONFIGURED_ENCRYPTION_PREFIX = "{Configured}";
private final static Log log = LogFactory.getLog(EncryptionManager.class);
private static String activeEncryptionPrefix = SIMPLE_ENCRYPTION_PREFIX;
private static ConfiguredEncryption ce;

static {
ENCRYPTORS.put(SIMPLE_ENCRYPTION_PREFIX, SimpleEncryption.INSTANCE);
//login properties files used to have this
ENCRYPTORS.put("{Standard}", SimpleEncryption.INSTANCE);
}
String keyFile = System.getProperty("org.apache.geronimo.security.encryption.keyfile");

if (keyFile != null && keyFile.length() != 0) {
try {
ce = new ConfiguredEncryption(keyFile);
} catch (Exception e) {
log.error("Can not handle "+keyFile, e);
}
setEncryptionPrefix(CONFIGURED_ENCRYPTION_PREFIX, ce);
}

private static String activeEncryptionPrefix = SIMPLE_ENCRYPTION_PREFIX;
}

/**
* Encryption instances should call this to register themselves.
Expand Down
Expand Up @@ -21,21 +21,14 @@
package org.apache.geronimo.system.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.security.SecureRandom;

import javax.crypto.spec.SecretKeySpec;

import org.apache.geronimo.gbean.GBeanInfo;
import org.apache.geronimo.gbean.GBeanInfoBuilder;
import org.apache.geronimo.gbean.GBeanLifecycle;
import org.apache.geronimo.system.serverinfo.ServerInfo;
import org.apache.geronimo.crypto.AbstractEncryption;
import org.apache.geronimo.crypto.EncryptionManager;

/**
* Like SimpleEncryption except it uses a stored secret key. If the key file is missing, it makes up a new one.
Expand All @@ -52,65 +45,23 @@
*
* @version $Rev$ $Date$
*/
public class ConfiguredEncryption extends AbstractEncryption implements GBeanLifecycle {
public class ConfiguredEncryption implements GBeanLifecycle {

private final SecretKeySpec spec;
private org.apache.geronimo.crypto.ConfiguredEncryption ce;

public ConfiguredEncryption(String path, ServerInfo serverInfo) throws IOException, ClassNotFoundException {
File location = serverInfo.resolve(path);
if (location.exists()) {
FileInputStream in = new FileInputStream(location);
try {
ObjectInputStream oin = new ObjectInputStream(in);
try {
spec = (SecretKeySpec) oin.readObject();
} finally {
oin.close();
}
} finally {
in.close();
}
} else {
SecureRandom random = new SecureRandom();
random.setSeed(System.currentTimeMillis());
byte[] bytes = new byte[16];
random.nextBytes(bytes);
spec = new SecretKeySpec(bytes, "AES");
File dir = location.getParentFile();
if (!dir.exists()) {
dir.mkdirs();
}
if (!dir.exists() || !dir.isDirectory()) {
throw new IllegalStateException("Could not create directory for secret key spec: " + dir);
}
FileOutputStream out = new FileOutputStream(location);
try {
ObjectOutputStream oout = new ObjectOutputStream(out);
try {
oout.writeObject(spec);
oout.flush();
} finally {
oout.close();
}
} finally {
out.close();
}
}
ce = new org.apache.geronimo.crypto.ConfiguredEncryption(location.getAbsolutePath());
}

public void doStart() throws Exception {
EncryptionManager.setEncryptionPrefix("{Configured}", this);
}

public void doStop() throws Exception {
}

public void doFail() {
}

protected SecretKeySpec getSecretKeySpec() {
return spec;
}

public static final GBeanInfo GBEAN_INFO;

Expand Down
5 changes: 5 additions & 0 deletions plugins/bval/bval-deployer/pom.xml
Expand Up @@ -97,6 +97,11 @@
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>bval-jsr303</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
5 changes: 4 additions & 1 deletion plugins/bval/geronimo-bval/pom.xml
Expand Up @@ -66,7 +66,10 @@
<artifactId>bval-jsr303</artifactId>
</dependency>


<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>

</project>
2 changes: 1 addition & 1 deletion plugins/bval/pom.xml
Expand Up @@ -44,7 +44,7 @@
<dependency>
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>bval-jsr303</artifactId>
<version>0.3_1-incubating</version>
<version>0.4_1</version>
<exclusions>
<exclusion>
<groupId>org.apache.bval</groupId>
Expand Down
6 changes: 6 additions & 0 deletions plugins/connector-1_6/geronimo-connector-builder-1_6/pom.xml
Expand Up @@ -152,6 +152,12 @@
<groupId>org.apache.xbean</groupId>
<artifactId>xbean-finder</artifactId>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down

0 comments on commit 9541447

Please sign in to comment.