From 4ce5771694d7a1aeef89f3cda738127af878f752 Mon Sep 17 00:00:00 2001 From: Sean Kavanagh Date: Sun, 9 Jan 2022 15:39:50 -0500 Subject: [PATCH] Added logic for Bastillion-EC2 Upgrade --- src/main/java/Upgrade.java | 7 +------ .../manage/util/EncryptionUtil.java | 18 ++++++++++++++--- .../bastillion/manage/util/KeyStoreUtil.java | 20 ++++++++++++++----- 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/main/java/Upgrade.java b/src/main/java/Upgrade.java index 5dd6c85..b0d14fa 100644 --- a/src/main/java/Upgrade.java +++ b/src/main/java/Upgrade.java @@ -42,7 +42,7 @@ public static void main(String[] args) { password = "filepwd " + password; } assert connectionURL != null; - connectionURL = connectionURL.replaceAll("keydb/bastillion", DBUtils.DB_PATH + "keydb/bastillion"); + connectionURL = connectionURL.replaceAll("jdbc:h2:", "jdbc:h2:" + DBUtils.DB_PATH); System.out.println("connectionURL : " + connectionURL); Properties info = new Properties(); @@ -52,7 +52,6 @@ public static void main(String[] args) { try { H2Upgrade.upgrade(connectionURL, info, 200); } catch (Exception ignore) { - ignore.printStackTrace(); } Connection con = DBUtils.getConn(); @@ -65,7 +64,6 @@ public static void main(String[] args) { stmt.executeUpdate("ALTER TABLE system RENAME COLUMN \"USER\" to USERNAME"); DBUtils.closeStmt(stmt); } catch (Exception ignore) { - ignore.printStackTrace(); } try { @@ -73,7 +71,6 @@ public static void main(String[] args) { stmt.executeUpdate("ALTER TABLE system RENAME COLUMN \"user\" to USERNAME"); DBUtils.closeStmt(stmt); } catch (Exception ignore) { - ignore.printStackTrace(); } try { @@ -81,7 +78,6 @@ public static void main(String[] args) { stmt.executeUpdate("ALTER TABLE terminal_log RENAME COLUMN \"USER\" to USERNAME"); DBUtils.closeStmt(stmt); } catch (Exception ignore) { - ignore.printStackTrace(); } try { @@ -89,7 +85,6 @@ public static void main(String[] args) { stmt.executeUpdate("ALTER TABLE terminal_log RENAME COLUMN \"user\" to USERNAME"); DBUtils.closeStmt(stmt); } catch (Exception ignore) { - ignore.printStackTrace(); } DBUtils.closeConn(con); diff --git a/src/main/java/io/bastillion/manage/util/EncryptionUtil.java b/src/main/java/io/bastillion/manage/util/EncryptionUtil.java index 374581b..357c0ad 100644 --- a/src/main/java/io/bastillion/manage/util/EncryptionUtil.java +++ b/src/main/java/io/bastillion/manage/util/EncryptionUtil.java @@ -23,10 +23,22 @@ import org.apache.commons.lang3.StringUtils; public class EncryptionUtil { - private static final byte[] key = KeyStoreUtil.getSecretBytes("KEYBOX-ENCRYPTION_KEY"); - public static final String CRYPT_ALGORITHM = "AES"; - public static final String HASH_ALGORITHM = "SHA-256"; + public static final String ENCRYPTION_KEY_NM; + public static final String KEYBOX_ENCRYPTION_KEY = "KEYBOX-ENCRYPTION_KEY"; + public static final String EC2BOX_ENCRYPTION_KEY = "EC2BOX-ENCRYPTION_KEY"; + private static final byte[] keybox = KeyStoreUtil.getSecretBytes(KEYBOX_ENCRYPTION_KEY); + private static final byte[] ec2box = KeyStoreUtil.getSecretBytes(EC2BOX_ENCRYPTION_KEY); + private static final byte[] key; + static { + if(keybox != null && keybox.length > 0) { + key = keybox; + ENCRYPTION_KEY_NM = KEYBOX_ENCRYPTION_KEY; + } else { + key = ec2box; + ENCRYPTION_KEY_NM = EC2BOX_ENCRYPTION_KEY; + } + } private EncryptionUtil() { } diff --git a/src/main/java/io/bastillion/manage/util/KeyStoreUtil.java b/src/main/java/io/bastillion/manage/util/KeyStoreUtil.java index c6f5e76..340768b 100644 --- a/src/main/java/io/bastillion/manage/util/KeyStoreUtil.java +++ b/src/main/java/io/bastillion/manage/util/KeyStoreUtil.java @@ -28,8 +28,6 @@ public class KeyStoreUtil { private static KeyStore keyStore = null; private static final String keyStoreFile; private static final char[] KEYSTORE_PASS; - private static final byte[] key; - public static final String ENCRYPTION_KEY_ALIAS = "KEYBOX-ENCRYPTION_KEY"; public KeyStoreUtil() { } @@ -41,7 +39,7 @@ public static byte[] getSecretBytes(String alias) { SecretKeyEntry entry = (SecretKeyEntry) keyStore.getEntry(alias, new PasswordProtection(KEYSTORE_PASS)); value = entry.getSecretKey().getEncoded(); } catch (Exception ex) { - ex.printStackTrace(); + //ex.printStackTrace(); } return value; @@ -81,7 +79,7 @@ public static void initializeKeyStore() { try { keyStore = KeyStore.getInstance("JCEKS"); keyStore.load((InputStream) null, KEYSTORE_PASS); - setSecret("KEYBOX-ENCRYPTION_KEY", key); + setSecret(EncryptionUtil.ENCRYPTION_KEY_NM, getKey()); FileOutputStream fos = new FileOutputStream(keyStoreFile); keyStore.store(fos, KEYSTORE_PASS); fos.close(); @@ -91,10 +89,22 @@ public static void initializeKeyStore() { } + private static byte[] getKey() { + byte[] key; + if (EncryptionUtil.ENCRYPTION_KEY_NM.equals(EncryptionUtil.KEYBOX_ENCRYPTION_KEY)) { + key = new byte[]{100, 51, 50, 116, 112, 100, 77, 111, 73, 56, 120, 122, 97, 80, 111, 100}; + } else { + key = new byte[]{'t', '3', '2', 'm', 'p', 'd', 'M', 'O', 'i', '8', 'x', 'z', 'a', 'P', 'o', 'd'}; + } + return key; + } + + static { keyStoreFile = DBUtils.DB_PATH + "bastillion.jceks"; KEYSTORE_PASS = new char[]{'G', '~', 'r', 'x', 'Z', 'E', 'w', 'f', 'a', '[', '!', 'f', 'Z', 'd', '*', 'L', '8', 'm', 'h', 'u', '#', 'j', '9', ':', '~', ';', 'U', '>', 'O', 'i', '8', 'r', 'C', '}', 'f', 't', '%', '[', 'H', 'h', 'M', '&', 'K', ':', 'l', '5', 'c', 'H', '6', 'r', 'A', 'E', '.', 'F', 'Y', 'W', '}', '{', '*', '8', 'd', 'E', 'C', 'A', '6', 'F', 'm', 'j', 'u', 'A', 'Q', '%', '{', '/', '@', 'm', '&', '5', 'S', 'q', '4', 'Q', '+', 'Y', '|', 'X', 'W', 'z', '8', '<', 'j', 'd', 'a', '}', '`', '0', 'N', 'B', '3', 'i', 'v', '5', 'U', ' ', '2', 'd', 'd', '(', '&', 'J', '_', '9', 'o', '(', '2', 'I', '`', ';', '>', '#', '$', 'X', 'j', '&', '&', '%', '>', '#', '7', 'q', '>', ')', 'L', 'A', 'v', 'h', 'j', 'i', '8', '~', ')', 'a', '~', 'W', '/', 'l', 'H', 'L', 'R', '+', '\\', 'i', 'R', '_', '+', 'y', 's', '0', 'n', '\'', '=', '{', 'B', ':', 'l', '1', '%', '^', 'd', 'n', 'H', 'X', 'B', '$', 'f', '"', '#', ')', '{', 'L', '/', 'q', '\'', 'O', '%', 's', 'M', 'Q', ']', 'D', 'v', ';', 'L', 'C', 'd', '?', 'D', 'l', 'h', 'd', 'i', 'N', '4', 'R', '>', 'O', ';', '$', '(', '4', '-', '0', '^', 'Y', ')', '5', 'V', 'M', '7', 'S', 'a', 'c', 'D', 'C', 'w', 'A', 'o', 'n', 's', 'r', '*', 'G', '[', 'l', 'h', '$', 'U', 's', '_', 'D', 'f', 'X', '~', '.', '7', 'B', 'A', 'E', '(', '#', ']', ':', '`', ',', 'k', 'y'}; - key = new byte[]{100, 51, 50, 116, 112, 100, 77, 111, 73, 56, 120, 122, 97, 80, 111, 100}; + + File f = new File(keyStoreFile); if (f.isFile() && f.canRead()) { try {