-
Notifications
You must be signed in to change notification settings - Fork 1.1k
RANGER-5567: allow validateConfig API available only for users with Ranger admin role #931
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vyommani
wants to merge
1
commit into
apache:master
Choose a base branch
from
vyommani:RANGER-5567-clean-v2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
120 changes: 120 additions & 0 deletions
120
hive-agent/src/main/java/org/apache/ranger/services/hive/client/JdbcUrlValidator.java
This file contains hidden or 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,120 @@ | ||
| /* | ||
| * 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.services.hive.client; | ||
|
|
||
| import org.apache.ranger.plugin.client.HadoopException; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.net.URLDecoder; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
||
| public final class JdbcUrlValidator { | ||
| private static final Logger LOG = LoggerFactory.getLogger(JdbcUrlValidator.class); | ||
| private static final Set<String> BLOCKED_PARAMS = Collections.unmodifiableSet( | ||
| new HashSet<>(Arrays.asList( | ||
| "socketfactory", "socketfactoryarg", "sslfactory", "sslfactoryarg", | ||
| "sslhostnameverifier", "authenticationpluginclassname", "loggerclassname", | ||
| "kerberosservername", "gssdelegatecred", "sslpasswordcallback"))); | ||
| private static final String[] DANGEROUS_PATTERNS = {"socketfactory", "sslfactory", "autodeserialize"}; | ||
|
|
||
| private JdbcUrlValidator() { | ||
| } | ||
|
|
||
| public static void validate(String jdbcUrl) throws HadoopException { | ||
| if (jdbcUrl == null || jdbcUrl.trim().isEmpty()) { | ||
| HadoopException e = new HadoopException("jdbc.url must not be null or empty"); | ||
| e.generateResponseDataMap(false, "Validation failed", "jdbc.url is required", | ||
| null, "jdbc.url"); | ||
| throw e; | ||
| } | ||
| String trimmed = jdbcUrl.trim(); | ||
| int queryStart = findQueryStart(trimmed); | ||
| if (queryStart != -1) { | ||
| String queryString = trimmed.substring(queryStart + 1); | ||
| validateQueryString(queryString, trimmed); | ||
| } | ||
| LOG.debug("jdbc.url passed validation: {}", sanitizeForLog(trimmed)); | ||
| } | ||
|
|
||
| private static void validateQueryString(String queryString, String fullUrl) throws HadoopException { | ||
| String[] tokens = queryString.split("[&;?]"); | ||
| for (String token : tokens) { | ||
| if (token.trim().isEmpty()) { | ||
| continue; | ||
| } | ||
| int eqIdx = token.indexOf('='); | ||
| String paramName = (eqIdx >= 0 ? token.substring(0, eqIdx) : token).trim(); | ||
| String decodedParamName = paramName; | ||
| try { | ||
| decodedParamName = URLDecoder.decode(paramName, StandardCharsets.UTF_8); | ||
| } catch (Exception e) { | ||
| LOG.warn("Failed to decode parameter name: {}", paramName); | ||
| } | ||
|
|
||
| String normalized = decodedParamName.toLowerCase().trim().replaceAll("[._-]", ""); | ||
| if (BLOCKED_PARAMS.contains(normalized)) { | ||
| logAndThrow("blocked parameter", normalized, paramName, fullUrl); | ||
| } | ||
| for (String danger : DANGEROUS_PATTERNS) { | ||
| if (normalized.contains(danger)) { | ||
| logAndThrow("dangerous pattern '" + danger + "'", normalized, paramName, fullUrl); | ||
| } | ||
| } | ||
| if (normalized.contains("factory") && (normalized.contains("socket") || normalized.contains("ssl") || | ||
| normalized.contains("connection") || normalized.contains("auth") || | ||
| normalized.contains("driver") || normalized.contains("datasource"))) { | ||
| logAndThrow("potentially dangerous factory parameter", normalized, paramName, fullUrl); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static String sanitizeForLog(String url) { | ||
| if (url == null) { | ||
| return "<null>"; | ||
| } | ||
| int idx = findQueryStart(url); | ||
| return idx >= 0 ? url.substring(0, idx) + "?<params_redacted>" : url; | ||
| } | ||
|
|
||
| private static int findQueryStart(String url) { | ||
| int qIdx = url.indexOf('?'); | ||
| int sIdx = url.indexOf(';'); | ||
| if (qIdx >= 0 && sIdx >= 0) { | ||
| return Math.min(qIdx, sIdx); | ||
| } else if (qIdx >= 0) { | ||
| return qIdx; | ||
| } else if (sIdx >= 0) { | ||
| return sIdx; | ||
| } | ||
| return -1; | ||
| } | ||
|
|
||
| private static void logAndThrow(String reason, String normalized, String originalParam, String fullUrl) { | ||
| LOG.warn("Rejected jdbc.url containing {} '{}' (param='{}'): {}", reason, normalized, originalParam, sanitizeForLog(fullUrl)); | ||
| HadoopException e = new HadoopException("jdbc.url contains a prohibited parameter: '" + originalParam + | ||
| "'. This parameter is not permitted for security reasons."); | ||
| e.generateResponseDataMap(false, "Invalid jdbc.url parameter", "Parameter '" + | ||
| originalParam + "' is blocked", null, "jdbc.url"); | ||
| throw e; | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.