-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Arun Kumar
committed
May 3, 2017
1 parent
9cbaf6b
commit c81a4de
Showing
4 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...provider/src/main/java/com/mesosphere/dcos/cassandra/keyprovider/AdobeKmsKeyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package com.mesosphere.dcos.cassandra.keyprovider; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.ByteBuffer; | ||
import java.security.Key; | ||
import java.security.KeyStore; | ||
import java.util.Base64; | ||
|
||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
import org.apache.cassandra.config.TransparentDataEncryptionOptions; | ||
import org.apache.cassandra.security.KeyProvider; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.amazonaws.auth.AWSCredentials; | ||
import com.amazonaws.auth.BasicAWSCredentials; | ||
import com.amazonaws.regions.Region; | ||
import com.amazonaws.regions.Regions; | ||
import com.amazonaws.services.kms.AWSKMSClient; | ||
import com.amazonaws.services.kms.model.AWSKMSException; | ||
import com.amazonaws.services.kms.model.DecryptRequest; | ||
import com.amazonaws.services.kms.model.DecryptResult; | ||
import com.mesosphere.dcos.cassandra.keyprovider.exception.KmsAccessException; | ||
|
||
public class AdobeKmsKeyProvider implements KeyProvider | ||
{ | ||
private static final Logger logger = LoggerFactory.getLogger(AdobeKmsKeyProvider.class); | ||
static final String PROP_KEYSTORE = "keystore"; | ||
static final String PROP_KEYSTORE_PW = "keystore_password"; | ||
static final String PROP_KEYSTORE_TYPE = "store_type"; | ||
static final String PROP_KEY_PW = "key_password"; | ||
private final KeyStore store; | ||
private final boolean isJceks; | ||
private final TransparentDataEncryptionOptions options; | ||
public AdobeKmsKeyProvider(TransparentDataEncryptionOptions options) | ||
{ | ||
logger.info("arun: key adobekeyprovider"); | ||
this.options = options; | ||
store= null; | ||
isJceks = true; | ||
// logger.info("initializing keystore from file {}", options.get(PROP_KEYSTORE)); | ||
// try | ||
// { | ||
// | ||
// URL url = new URL("https://s3-us-west-1.amazonaws.com/cassandra-arun-bucket/adobe.keystore"); | ||
// InputStream is = url.openStream(); | ||
// store = KeyStore.getInstance(options.get(PROP_KEYSTORE_TYPE)); | ||
// store.load(is, options.get(PROP_KEYSTORE_PW).toCharArray()); | ||
// isJceks = store.getType().equalsIgnoreCase("jceks"); | ||
// } | ||
// catch (Exception e) | ||
// { | ||
// throw new RuntimeException("couldn't load keystore", e); | ||
// } | ||
} | ||
public Key getSecretKey(String keyAlias) throws IOException | ||
{ | ||
// there's a lovely behavior with jceks files that all aliases are lower-cased | ||
// if (isJceks) | ||
// keyAlias = keyAlias.toLowerCase(); | ||
// Key key; | ||
// try | ||
// { | ||
// String password = options.get(PROP_KEY_PW); | ||
// if (password == null || password.isEmpty()) | ||
// password = options.get(PROP_KEYSTORE_PW); | ||
// key = store.getKey(keyAlias, password.toCharArray()); | ||
// } | ||
// catch (Exception e) | ||
// { | ||
// throw new IOException("unable to load key from keystore"); | ||
// } | ||
// if (key == null) | ||
// throw new IOException(String.format("key %s was not found in keystore", keyAlias)); | ||
// return key; | ||
|
||
logger.info("AdobeKmsKeyProvider going to load key from AWSKMS"); | ||
try{ | ||
String decryptDataKey = "AQEDAHgbHAx5l16Yxh+2rJYL40KbyL53JpAAJ64JrRkw4iA2RQAAAG4wbAYJKoZIhvcNAQcGoF8wXQIBADBYBgkqhkiG9w0BBwEwHgYJYIZIAWUDBAEuMBEEDMqDqemUdlfQC1EQGAIBEIArhWoMmXK07/BcnRbMyWCVurtAUknUH3nUyRLObCxZbTu3b5cfyTgFTF5Akw==";//Base64.getEncoder().encodeToString(generateDatakey.getCiphertextBlob().array()); | ||
ByteBuffer decryptDataKeyByte = ByteBuffer.wrap(Base64.getDecoder().decode(decryptDataKey)); | ||
|
||
ByteBuffer datakey = getDatakeyDecrypted(decryptDataKeyByte); | ||
final SecretKeySpec encryptionKey = new SecretKeySpec(datakey.array(), "AES"); | ||
return encryptionKey; | ||
}catch (KmsAccessException e) { | ||
logger.error("Could not decrypt/load KMS:"+e.getMessage()); | ||
throw new IOException("Could not decrypt/load KMS:"+e.getMessage()); | ||
} | ||
} | ||
private static AWSKMSClient getKMSClient() { | ||
AWSCredentials awsCredentials = new BasicAWSCredentials( | ||
"AKIAILEX5VDPIBNW7U4Q", | ||
"jPxpQ1zeBFVb6iDJIvc05RqBzYwjpdImxKCCKzIr"); | ||
AWSKMSClient awskmsClient = new AWSKMSClient(awsCredentials); | ||
awskmsClient.setRegion(Region.getRegion(Regions.fromName("us-west-1"))); | ||
return awskmsClient; | ||
} | ||
|
||
private static ByteBuffer getDatakeyDecrypted(ByteBuffer encryptedDatakey) throws KmsAccessException { | ||
DecryptRequest decryptRequest = new DecryptRequest(); | ||
decryptRequest.setCiphertextBlob(encryptedDatakey); | ||
DecryptResult decryptResult; | ||
try { | ||
decryptResult = getKMSClient().decrypt(decryptRequest); | ||
} catch (AWSKMSException e) { | ||
throw new KmsAccessException("Could not decrypt KMS", e); | ||
} | ||
return decryptResult.getPlaintext(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...src/main/java/com/mesosphere/dcos/cassandra/keyprovider/exception/KmsAccessException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mesosphere.dcos.cassandra.keyprovider.exception; | ||
|
||
public class KmsAccessException extends Exception { | ||
|
||
public KmsAccessException() { | ||
} | ||
|
||
public KmsAccessException(String message) { | ||
super(message); | ||
} | ||
|
||
public KmsAccessException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public KmsAccessException(Throwable cause) { | ||
super(cause); | ||
} | ||
} |