You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by GitBox <gi...@apache.org> on 2022/10/13 01:45:55 UTC

[GitHub] [cloudstack] sureshanaparti commented on a diff in pull request #6812: Normalize encryption on global configurations values

sureshanaparti commented on code in PR #6812:
URL: https://github.com/apache/cloudstack/pull/6812#discussion_r994033419


##########
engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade41710to41800.java:
##########
@@ -83,4 +92,55 @@ public void updateSystemVmTemplates(Connection conn) {
             throw new CloudRuntimeException("Failed to find / register SystemVM template(s)");
         }
     }
+
+    protected void decryptGlobalConfigurationValues(Connection conn) {
+        LOG.info("Decrypting global configuration values from the following tables: account_details and domain_details.");
+
+        Map<Long, String> accountsMap = selectValuesWithCustomConfigs(conn, ACCOUNT_DETAILS);
+        executeUpdateOnConfigValues(conn, accountsMap, ACCOUNT_DETAILS);
+        LOG.info("Successfully decrypted configurations from account_details table.");
+
+        Map<Long, String> domainsMap = selectValuesWithCustomConfigs(conn, DOMAIN_DETAILS);
+        executeUpdateOnConfigValues(conn, domainsMap, DOMAIN_DETAILS);
+        LOG.info("Successfully decrypted configurations from domain_details table.");
+    }
+
+    protected Map<Long, String> selectValuesWithCustomConfigs(Connection conn, String table) {
+        Map<Long, String> accountValuesToBeUpdated = new HashMap<>();
+        String selectDetails = String.format("SELECT details.id, details.value from cloud.%s details, cloud.configuration c " +
+                "WHERE details.name = c.name AND c.category NOT IN ('Hidden', 'Secure') ORDER BY details.id;", table);
+
+        try (PreparedStatement pstmt = conn.prepareStatement(selectDetails)) {
+            try (ResultSet result = pstmt.executeQuery()) {
+                while (result.next()) {
+                    accountValuesToBeUpdated.put(result.getLong("id"), result.getString("value"));
+                }
+            }
+            return accountValuesToBeUpdated;
+        } catch (SQLException e) {
+            String message = String.format("Unable to retrieve data from table [%s] due to [%s].", table, e.getMessage());
+            LOG.error(message, e);
+            throw new CloudRuntimeException(message, e);
+        }
+    }
+
+    protected void executeUpdateOnConfigValues(Connection conn, Map<Long, String> valuesMapToBeUpdated, String table) {
+        String updateConfigValues = String.format("UPDATE cloud.%s SET value = ? WHERE id = ?;", table);
+
+        for (Map.Entry<Long, String> map : valuesMapToBeUpdated.entrySet()) {
+            String decryptedValue = DBEncryptionUtil.decrypt(map.getValue());
+
+            try (PreparedStatement pstmt = conn.prepareStatement(updateConfigValues)) {
+                pstmt.setString(1, decryptedValue);
+                pstmt.setLong(2, map.getKey());
+
+                LOG.info(String.format("Updating config with ID [%s] to value [%s].", map.getKey(), decryptedValue));
+                pstmt.executeUpdate();
+            } catch (SQLException e) {
+                String message = String.format("Unable to update config value with ID [%s] on table [%s] due to [%s].", map.getKey(), table, e.getMessage());
+                LOG.error(message, e);
+                throw new CloudRuntimeException(message, e);

Review Comment:
   @BryanMLima Can log and continue updating other values in the map instead of throwing error here (which will not halt the upgrade process)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@cloudstack.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org