Skip to content

Commit

Permalink
RANGER-2331 Ranger-KMS - KeySecure HSM Integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gautamborad committed Feb 25, 2019
1 parent fe8df85 commit fc42892
Show file tree
Hide file tree
Showing 13 changed files with 718 additions and 16 deletions.
51 changes: 51 additions & 0 deletions kms/config/kms-webapp/dbks-site.xml
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,57 @@
<name>ranger.ks.kerberos.keytab</name>
<value></value>
</property>

<!-- Key-Secure Config START-->

<property>
<name>ranger.kms.keysecure.enabled</name>
<value>false</value>
<description></description>
</property>

<property>
<name>ranger.kms.keysecure.UserPassword.Authentication</name>
<value>true</value>
<description></description>
</property>
<property>
<name>ranger.kms.keysecure.masterkey.name</name>
<value>safenetmasterkey</value>
<description>Safenet key secure master key name</description>
</property>
<property>
<name>ranger.kms.keysecure.login.username</name>
<value>user1</value>
<description>Safenet key secure username</description>
</property>
<property>
<name>ranger.kms.keysecure.login.password</name>
<value>t1e2s3t4</value>
<description>Safenet key secure user password</description>
</property>
<property>
<name>ranger.kms.keysecure.login.password.alias</name>
<value>ranger.ks.login.password</value>
<description>Safenet key secure user password</description>
</property>
<property>
<name>ranger.kms.keysecure.hostname</name>
<value>SunPKCS11-keysecurehn</value>
<description>Safenet key secure hostname</description>
</property>
<property>
<name>ranger.kms.keysecure.masterkey.size</name>
<value>256</value>
<description>key size</description>
</property>
<property>
<name>ranger.kms.keysecure.sunpkcs11.cfg.filepath</name>
<value>/opt/safenetConf/64/8.3.1/sunpkcs11.cfg</value>
<description>Location of Safenet key secure library configuration file</description>
</property>

<!-- Key-Secure Config END-->

<!-- HSM Config -->
<property>
Expand Down
19 changes: 19 additions & 0 deletions kms/scripts/DBMKTOKEYSECURE.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# 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.
# -------------------------------------------------------------------------------------
RANGER_KMS_HOME=`dirname $0`
cp="${RANGER_KMS_HOME}/cred/lib/*:${RANGER_KMS_HOME}/./ews/webapp/WEB-INF/classes/conf/:${RANGER_KMS_HOME}/ews/webapp/config:${RANGER_KMS_HOME}/ews/lib/*:${RANGER_KMS_HOME}/ews/webapp/lib/*:${RANGER_KMS_HOME}/ews/webapp/META-INF"
java -cp "${cp}" org.apache.hadoop.crypto.key.DBToKeySecure ${1} ${2} ${3} ${4}
19 changes: 19 additions & 0 deletions kms/scripts/KEYSECUREMKTOKMSDB.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
# 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.
# -------------------------------------------------------------------------------------
RANGER_KMS_HOME=`dirname $0`
cp="${RANGER_KMS_HOME}/cred/lib/*:${RANGER_KMS_HOME}/./ews/webapp/WEB-INF/classes/conf/:${RANGER_KMS_HOME}/ews/webapp/WEB-INF/classes/lib/*:${RANGER_KMS_HOME}/ews/webapp/config:${RANGER_KMS_HOME}/ews/lib/*:${RANGER_KMS_HOME}/ews/webapp/lib/*:${RANGER_KMS_HOME}/ews/webapp/META-INF"
java -cp "${cp}" org.apache.hadoop.crypto.key.KeySecureToRangerDBMKUtil ${1}
10 changes: 10 additions & 0 deletions kms/scripts/install.properties
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,16 @@ HSM_ENABLED=false
HSM_PARTITION_NAME=par19
HSM_PARTITION_PASSWORD=S@fenet123

#------------------------- Ranger SAFENET KEYSECURE CONFIG ------------------------------
KEYSECURE_ENABLED=false
KEYSECURE_USER_PASSWORD_AUTHENTICATION=true
KEYSECURE_MASTERKEY_NAME=safenetkeysecure
KEYSECURE_USERNAME=user1
KEYSECURE_PASSWORD=t1e2s3t4
KEYSECURE_HOSTNAME=SunPKCS11-keysecurehn
KEYSECURE_MASTER_KEY_SIZE=256
KEYSECURE_LIB_CONFIG_PATH=/opt/safenetConf/64/8.3.1/sunpkcs11.cfg

#
# ------- UNIX User CONFIG ----------------
#
Expand Down
86 changes: 85 additions & 1 deletion kms/scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ HSM_ENABLED=$(get_prop 'HSM_ENABLED' $PROPFILE)
HSM_PARTITION_NAME=$(get_prop 'HSM_PARTITION_NAME' $PROPFILE)
HSM_PARTITION_PASSWORD=$(get_prop 'HSM_PARTITION_PASSWORD' $PROPFILE)

KEYSECURE_ENABLED=$(get_prop 'KEYSECURE_ENABLED' $PROPFILE)
KEYSECURE_USER_PASSWORD_AUTHENTICATION=$(get_prop 'KEYSECURE_USER_PASSWORD_AUTHENTICATION' $PROPFILE)
KEYSECURE_MASTERKEY_NAME=$(get_prop 'KEYSECURE_MASTERKEY_NAME' $PROPFILE)
KEYSECURE_USERNAME=$(get_prop 'KEYSECURE_USERNAME' $PROPFILE)
KEYSECURE_PASSWORD=$(get_prop 'KEYSECURE_PASSWORD' $PROPFILE)
KEYSECURE_HOSTNAME=$(get_prop 'KEYSECURE_HOSTNAME' $PROPFILE)
KEYSECURE_MASTER_KEY_SIZE=$(get_prop 'KEYSECURE_MASTER_KEY_SIZE' $PROPFILE)
KEYSECURE_LIB_CONFIG_PATH=$(get_prop 'KEYSECURE_LIB_CONFIG_PATH' $PROPFILE)

kms_principal=$(get_prop 'kms_principal' $PROPFILE)
kms_keytab=$(get_prop 'kms_keytab' $PROPFILE)
hadoop_conf=$(get_prop 'hadoop_conf' $PROPFILE)
Expand Down Expand Up @@ -204,6 +213,17 @@ password_validation(){
fi
fi
}

password_validation_safenet_keysecure(){
if [ -z "$1" ]
then
log "[I] Blank password is not allowed for" $2". Please enter valid password."
exit 1
else
log "[I]" $2 "password validated."
fi
}

init_variables(){
curDt=`date '+%Y%m%d%H%M%S'`

Expand Down Expand Up @@ -546,9 +566,14 @@ update_properties() {
DB_CREDENTIAL_ALIAS="ranger.ks.jpa.jdbc.credential.alias"

HSM_PARTITION_PASSWD="ranger.ks.hsm.partition.password"
HSM_PARTITION_PASSWORD_ALIAS="ranger.kms.hsm.partition.password"
HSM_PARTITION_PASSWORD_ALIAS="ranger.kms.hsm.partition.password"

KEYSECURE_PASSWD="ranger.kms.keysecure.login.password"
KEYSECURE_PASSWORD_ALIAS="ranger.ks.login.password"


HSM_ENABLED=`echo $HSM_ENABLED | tr '[:lower:]' '[:upper:]'`
KEYSECURE_ENABLED=`echo $KEYSECURE_ENABLED | tr '[:lower:]' '[:upper:]'`

if [ "${keystore}" != "" ]
then
Expand All @@ -574,6 +599,21 @@ update_properties() {
updatePropertyToFilePy $propertyName $newPropertyValue $to_file
fi

if [ "${KEYSECURE_ENABLED}" == "TRUE" ]
then
password_validation_safenet_keysecure "$KEYSECURE_PASSWORD" "KEYSECURE User Password"
$PYTHON_COMMAND_INVOKER ranger_credential_helper.py -l "cred/lib/*" -f "$keystore" -k "${KEYSECURE_PASSWORD_ALIAS}" -v "${KEYSECURE_PASSWORD}" -c 1

propertyName=ranger.kms.keysecure.login.password.alias
newPropertyValue="${KEYSECURE_PASSWORD_ALIAS}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.login.password
newPropertyValue="_"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file
fi


propertyName=ranger.ks.jpa.jdbc.credential.alias
newPropertyValue="${DB_CREDENTIAL_ALIAS}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file
Expand Down Expand Up @@ -605,6 +645,11 @@ update_properties() {
propertyName="${HSM_PARTITION_PASSWD}"
newPropertyValue="${HSM_PARTITION_PASSWORD}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName="${KEYSECURE_PASSWD}"
newPropertyValue="${KEYSECURE_PASSWORD}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

fi

if test -f $keystore; then
Expand Down Expand Up @@ -669,6 +714,45 @@ update_properties() {
updatePropertyToFilePy $propertyName $newPropertyValue $to_file
fi

########### SAFENET KEYSECURE CONFIG #################


if [ "${KEYSECURE_ENABLED}" != "TRUE" ]
then
propertyName=ranger.kms.keysecure.enabled
newPropertyValue="false"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file
else
propertyName=ranger.kms.keysecure.enabled
newPropertyValue="true"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.UserPassword.Authentication
newPropertyValue="${KEYSECURE_USER_PASSWORD_AUTHENTICATION}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.masterkey.name
newPropertyValue="${KEYSECURE_MASTERKEY_NAME}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.login.username
newPropertyValue="${KEYSECURE_USERNAME}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.hostname
newPropertyValue="${KEYSECURE_HOSTNAME}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.masterkey.size
newPropertyValue="${KEYSECURE_MASTER_KEY_SIZE}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

propertyName=ranger.kms.keysecure.sunpkcs11.cfg.filepath
newPropertyValue="${KEYSECURE_LIB_CONFIG_PATH}"
updatePropertyToFilePy $propertyName $newPropertyValue $to_file

fi

to_file_kms_site=$PWD/ews/webapp/WEB-INF/classes/conf/ranger-kms-site.xml
if test -f $to_file_kms_site; then
log "[I] $to_file_kms_site file found"
Expand Down
128 changes: 128 additions & 0 deletions kms/src/main/java/org/apache/hadoop/crypto/key/DBToKeySecure.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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.hadoop.crypto.key;

import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.ranger.kms.dao.DaoManager;

import com.sun.org.apache.xml.internal.security.utils.Base64;

public class DBToKeySecure {

private static final String ENCRYPTION_KEY = "ranger.db.encrypt.key.password";
private static final String KEYSECURE_MASTERKEY_NAME = "ranger.kms.keysecure.masterkey.name";
// private static final String KEYSECURE_PROTOCOL = "ranger.kms.keysecure.protocol";
private static final String KEYSECURE_LOGIN = "ranger.kms.keysecure.login";
private static final String CFGFILEPATH = "ranger.kms.keysecure.sunpkcs11.cfg.filepath";

public static void showUsage() {
System.err
.println("USAGE: java "
+ DBToKeySecure.class.getName()
+ " <keySecureMasterKeyName> <keySecureUsername> <keySecurePassword> <sunpkcs11CfgFilePath>");
}

public static void main(String[] args) {

if (args.length < 4) {
System.err.println("Invalid number of parameters found.");
showUsage();
System.exit(1);
} else {

Configuration conf = RangerKeyStoreProvider.getDBKSConf();

String keyName = args[0];
if (keyName == null || keyName.trim().isEmpty()) {
System.err.println("Key Secure master key name not provided.");
showUsage();
System.exit(1);
}

String username = args[1];
if (username == null || username.trim().isEmpty()) {
System.err.println("Key Secure username not provided.");
showUsage();
System.exit(1);
}
String password = args[2];
if (password == null || password.trim().isEmpty()) {
System.err.println("Key Secure password not provided.");
showUsage();
System.exit(1);
}

String cfgFilePath = args[3];
if (cfgFilePath == null || cfgFilePath.trim().isEmpty()) {
System.err.println("sunpkcs11 Configuration File Path not provided");
showUsage();
System.exit(1);
}

boolean result = new DBToKeySecure().doExportMKToKeySecure(keyName, username, password, cfgFilePath, conf);
if (result) {
System.out
.println("Master Key from Ranger KMS DB has been successfully imported into Key Secure.");
} else {
System.out
.println("Import of Master Key from DB has been unsuccessful.");
System.exit(1);
}
System.exit(0);
}
}

private boolean doExportMKToKeySecure(String keyName, String username, String password, String cfgFilePath, Configuration conf) {
try {
String keySecureMKPassword = conf.get(ENCRYPTION_KEY);
if (keySecureMKPassword == null
|| keySecureMKPassword.trim().equals("")
|| keySecureMKPassword.trim().equals("_")
|| keySecureMKPassword.trim().equals("crypted")) {
throw new IOException("Master Key Jceks does not exists");
}

conf.set(CFGFILEPATH, cfgFilePath);
conf.set(KEYSECURE_MASTERKEY_NAME, keyName);
conf.set(KEYSECURE_LOGIN,username + ":" + password);

RangerKMSDB rangerkmsDb = new RangerKMSDB(conf);
DaoManager daoManager = rangerkmsDb.getDaoManager();
String mkPassword = conf.get(ENCRYPTION_KEY);

// Get Master Key from Ranger DB
RangerMasterKey rangerMasterKey = new RangerMasterKey(daoManager);
String mkey = rangerMasterKey.getMasterKey(mkPassword);
byte[] key = Base64.decode(mkey);

if (conf != null) {
RangerSafenetKeySecure rangerSafenetKeySecure = new RangerSafenetKeySecure(
conf);
return rangerSafenetKeySecure.setMasterKey(password, key,conf);
}

return false;
} catch (Throwable t) {
throw new RuntimeException(
"Unable to import Master key from Ranger DB to KeySecure ",
t);
}

}

}
Loading

0 comments on commit fc42892

Please sign in to comment.