pradeepagrawal8184 commented on code in PR #1208: URL: https://github.com/apache/ranger/pull/1208#discussion_r3958914775
########## security-admin/src/main/java/org/apache/ranger/patch/PatchServicePasswordV2Migration_J10067.java: ########## @@ -0,0 +1,238 @@ +/* + * 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.ranger.patch; + +import org.apache.commons.lang3.StringUtils; +import org.apache.ranger.biz.ServiceDBStore; +import org.apache.ranger.db.RangerDaoManager; +import org.apache.ranger.entity.XXService; +import org.apache.ranger.entity.XXServiceConfigMap; +import org.apache.ranger.plugin.util.PasswordUtils; +import org.apache.ranger.plugin.util.RangerSupportedCryptoAlgo; +import org.apache.ranger.util.CLIUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * One-time upgrade migration for RANGER-5773 (service-config credential encryption key was + * stored alongside its own ciphertext). The code-level fix (see PasswordUtils.encryptPasswordV2/ + * decryptPasswordV2 and their callers in ServiceDBStore/RangerServiceService) only changes how + * *new* writes are stored — every password already in x_service_config_map before this patch + * runs still carries the vulnerable v1 format, with the key embedded in the same row as its + * ciphertext. This patch is what actually closes that exposure for existing data: it decrypts + * every password-type config value still in the legacy format and re-writes it in the v2 format + * (key sourced from configuration, never stored). + * <p> + * Modeled on {@link PatchPasswordEncryption_J10001}, which did the equivalent migration for the + * legacy x_asset config column when password encryption was first introduced. + * <p> + * Design notes worth a reviewer's attention rather than being silently assumed: + * - Single-pass, single-transaction, like PatchPasswordEncryption_J10001 (batchSize is not set, + * so BaseLoader runs execLoad() exactly once and commits at the end). For a very large + * x_service_config_map this is a real limitation (lock/timeout risk) — matches existing + * precedent, but is worth a second opinion on very large deployments before this ships, + * rather than assuming precedent alone settles it. + * - Per-row failures are caught and logged, NOT allowed to abort the whole patch — this covers + * the full per-row body (this row's XXService/service-def lookups included, not just the + * decrypt/re-encrypt/write step) precisely so a transient DAO issue on one row can't do it + * either. This is a deliberate improvement over the precedent (which has no per-row error + * handling at all, so a single bad row would abort the entire migration via BaseLoader's + * top-level catch). A row that fails to migrate is simply left in v1 format — still fully + * functional, just not yet protected — rather than blocking every other row from being + * migrated. + * - Idempotent / safe to re-run: rows already in v2 format (isV2Format()) are skipped, so this + * patch can be re-run (e.g. after fixing a config issue that caused failures) without + * re-encrypting already-migrated rows. + * - Config (key + algorithm) is validated ONCE up front, before any row is touched — see + * {@link #validateMigrationConfig()} — rather than letting a bad config burn every single row + * as its own per-row failure with the same root cause. + * - A row is only treated as "legacy-format, needs migrating" when {@code + * PasswordUtils.isLegacyFormat()} recognizes it (first field names a real crypto algorithm), + * not merely because its stored value contains a comma. The old, looser + * {@code storedValue.contains(",")} heuristic could misclassify a plaintext password that + * happens to contain a comma as "already encrypted" and attempt to decrypt/re-encrypt it — + * rows that don't match either recognized format are counted as notEncrypted and left alone. + */ +@Component +public class PatchServicePasswordV2Migration_J10067 extends BaseLoader { Review Comment: Also make an entry in core schema file of all DB's like : https://github.com/apache/ranger/blob/master/security-admin/db/mysql/optimized/current/ranger_core_db_mysql.sql#L2009 -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
