okumin commented on code in PR #6928: URL: https://github.com/apache/hive/pull/6928#discussion_r4419575145
########## ql/src/java/org/apache/hadoop/hive/ql/txn/compactor/CompactorPropertyValidator.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.hive.ql.txn.compactor; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConfUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + * The allow list bounding which configuration parameters a table property may override on a compaction + * job, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST}. This config limits the scope + * of the overridable config parameters, because most of the parameters have no effect on a compaction job. + * With this limitation, the regex defined by the deny list can be kept simple and short. + * <p> + * The config parameters are first evaluated against the allow list config. If they match, then they will be + * evaluated against the deny list, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_DENYLIST}. + * The purpose of the deny list is to filter for parameters which could lead to security gaps. + * <p> + * The admins can extend the allow list through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND}. + * The config parameters listed here are also evaluated against the deny list to prevent accidentally allowing + * configs which hold security risk. + * <p> + * As last step, all config parameters are checked against the already existing lists such as + * {@link HiveConf.ConfVars#HIVE_CONF_RESTRICTED_LIST}, {@link HiveConf.ConfVars#HIVE_CONF_HIDDEN_LIST} + * and {@link HiveConf.ConfVars#HIVE_CONF_LOCKED_LIST} + * This applies for the parameters defined through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND} + * as well. + */ +final class CompactorPropertyValidator { + + private static final Logger LOG = LoggerFactory.getLogger(CompactorPropertyValidator.class); + private static final Map<String, Pattern> ALLOW_PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map<String, Pattern> DENY_PATTERN_CACHE = new ConcurrentHashMap<>(); + + private static final Pattern MATCHES_NOTHING = Pattern.compile("(?!)"); Review Comment: ```suggestion private static final Pattern MATCHES_NOTHING = Pattern.compile("?!"); ``` ########## ql/src/java/org/apache/hadoop/hive/ql/txn/compactor/CompactorPropertyValidator.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.hive.ql.txn.compactor; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConfUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + * The allow list bounding which configuration parameters a table property may override on a compaction + * job, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST}. This config limits the scope + * of the overridable config parameters, because most of the parameters have no effect on a compaction job. + * With this limitation, the regex defined by the deny list can be kept simple and short. + * <p> + * The config parameters are first evaluated against the allow list config. If they match, then they will be + * evaluated against the deny list, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_DENYLIST}. + * The purpose of the deny list is to filter for parameters which could lead to security gaps. + * <p> + * The admins can extend the allow list through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND}. + * The config parameters listed here are also evaluated against the deny list to prevent accidentally allowing + * configs which hold security risk. + * <p> + * As last step, all config parameters are checked against the already existing lists such as + * {@link HiveConf.ConfVars#HIVE_CONF_RESTRICTED_LIST}, {@link HiveConf.ConfVars#HIVE_CONF_HIDDEN_LIST} + * and {@link HiveConf.ConfVars#HIVE_CONF_LOCKED_LIST} + * This applies for the parameters defined through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND} + * as well. + */ +final class CompactorPropertyValidator { + + private static final Logger LOG = LoggerFactory.getLogger(CompactorPropertyValidator.class); + private static final Map<String, Pattern> ALLOW_PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map<String, Pattern> DENY_PATTERN_CACHE = new ConcurrentHashMap<>(); + + private static final Pattern MATCHES_NOTHING = Pattern.compile("(?!)"); + private static final Pattern DENY_EVERYTHING = Pattern.compile(".*"); + + private CompactorPropertyValidator() { + } + + /** + * Decides whether a parameter is within the set a table property may override. + * + * @param conf configuration the allow list settings are read from + * @param parameterName the config parameter name with its "compactor." or "tblprops." prefix already removed + */ + static boolean isParameterAllowed(Configuration conf, String parameterName) { + String key = parameterName.trim(); + if (StringUtils.isBlank(key)) { + return false; + } + + Pattern allowPattern = + getAllowPattern(HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST), MATCHES_NOTHING, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST.varname); + + if ((allowPattern != null && allowPattern.matcher(key).find()) || isOnAllowListAppend(conf, key)) { + Pattern denyPattern = + getDenyPattern(HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST), DENY_EVERYTHING, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST.varname); + if (denyPattern != null && denyPattern.matcher(key).find()) { + LOG.warn("Ignoring compaction property '{}', as it is not permitted to override by table properties per {}.", + key, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST.varname); + return false; + } + + if (matchesAnyPrefix(HiveConfUtil.getHiddenSet(conf), key) || matchesAnyPrefix(HiveConfUtil.getLockedSet(conf), + key) || matchesAnyPrefix(HiveConfUtil.getRestrictedSet(conf), key)) { + LOG.warn("Ignoring compaction property '{}' as it is immutable at runtime per {}, {} or {}.", key, + HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST.varname, HiveConf.ConfVars.HIVE_CONF_HIDDEN_LIST.varname, + HiveConf.ConfVars.HIVE_CONF_LOCKED_LIST.varname); + return false; + } + return true; + } else { + LOG.warn("Ignoring compaction property '{}', as it is not allowed by {} or {}.", key, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST.varname, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND.varname); + return false; + } + } + + private static boolean isOnAllowListAppend(Configuration conf, String key) { + String configured = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND); + if (StringUtils.isBlank(configured)) { + return false; + } + for (String exception : configured.split(",")) { + if (key.equalsIgnoreCase(exception.trim())) { + return true; + } + } + return false; + } + + private static Pattern getAllowPattern(String regexConfig, Pattern fallbackRegex, String configName) { + return getPattern(regexConfig, fallbackRegex, configName, ALLOW_PATTERN_CACHE); + } + + private static Pattern getDenyPattern(String regexConfig, Pattern fallbackRegex, String configName) { + return getPattern(regexConfig, fallbackRegex, configName, DENY_PATTERN_CACHE); + } + + private static Pattern getPattern(String regexConfig, Pattern fallbackRegex, String configName, + Map<String, Pattern> PATTERN_CACHE) { Review Comment: ```suggestion Map<String, Pattern> patternCache) { ``` ########## ql/src/java/org/apache/hadoop/hive/ql/txn/compactor/CompactorPropertyValidator.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.hive.ql.txn.compactor; + +import org.apache.commons.lang3.StringUtils; +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.conf.HiveConfUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; + +/** + * The allow list bounding which configuration parameters a table property may override on a compaction + * job, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST}. This config limits the scope + * of the overridable config parameters, because most of the parameters have no effect on a compaction job. + * With this limitation, the regex defined by the deny list can be kept simple and short. + * <p> + * The config parameters are first evaluated against the allow list config. If they match, then they will be + * evaluated against the deny list, read from {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_DENYLIST}. + * The purpose of the deny list is to filter for parameters which could lead to security gaps. + * <p> + * The admins can extend the allow list through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND}. + * The config parameters listed here are also evaluated against the deny list to prevent accidentally allowing + * configs which hold security risk. + * <p> + * As last step, all config parameters are checked against the already existing lists such as + * {@link HiveConf.ConfVars#HIVE_CONF_RESTRICTED_LIST}, {@link HiveConf.ConfVars#HIVE_CONF_HIDDEN_LIST} + * and {@link HiveConf.ConfVars#HIVE_CONF_LOCKED_LIST} + * This applies for the parameters defined through {@link HiveConf.ConfVars#HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND} + * as well. + */ +final class CompactorPropertyValidator { + + private static final Logger LOG = LoggerFactory.getLogger(CompactorPropertyValidator.class); + private static final Map<String, Pattern> ALLOW_PATTERN_CACHE = new ConcurrentHashMap<>(); + private static final Map<String, Pattern> DENY_PATTERN_CACHE = new ConcurrentHashMap<>(); + + private static final Pattern MATCHES_NOTHING = Pattern.compile("(?!)"); + private static final Pattern DENY_EVERYTHING = Pattern.compile(".*"); + + private CompactorPropertyValidator() { + } + + /** + * Decides whether a parameter is within the set a table property may override. + * + * @param conf configuration the allow list settings are read from + * @param parameterName the config parameter name with its "compactor." or "tblprops." prefix already removed + */ + static boolean isParameterAllowed(Configuration conf, String parameterName) { + String key = parameterName.trim(); + if (StringUtils.isBlank(key)) { + return false; + } + + Pattern allowPattern = + getAllowPattern(HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST), MATCHES_NOTHING, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST.varname); + + if ((allowPattern != null && allowPattern.matcher(key).find()) || isOnAllowListAppend(conf, key)) { + Pattern denyPattern = + getDenyPattern(HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST), DENY_EVERYTHING, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST.varname); + if (denyPattern != null && denyPattern.matcher(key).find()) { + LOG.warn("Ignoring compaction property '{}', as it is not permitted to override by table properties per {}.", + key, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_DENYLIST.varname); + return false; + } + + if (matchesAnyPrefix(HiveConfUtil.getHiddenSet(conf), key) || matchesAnyPrefix(HiveConfUtil.getLockedSet(conf), + key) || matchesAnyPrefix(HiveConfUtil.getRestrictedSet(conf), key)) { + LOG.warn("Ignoring compaction property '{}' as it is immutable at runtime per {}, {} or {}.", key, + HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST.varname, HiveConf.ConfVars.HIVE_CONF_HIDDEN_LIST.varname, + HiveConf.ConfVars.HIVE_CONF_LOCKED_LIST.varname); + return false; + } + return true; + } else { + LOG.warn("Ignoring compaction property '{}', as it is not allowed by {} or {}.", key, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST.varname, + HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND.varname); + return false; + } + } + + private static boolean isOnAllowListAppend(Configuration conf, String key) { + String configured = HiveConf.getVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND); + if (StringUtils.isBlank(configured)) { + return false; + } + for (String exception : configured.split(",")) { + if (key.equalsIgnoreCase(exception.trim())) { + return true; + } + } + return false; + } + + private static Pattern getAllowPattern(String regexConfig, Pattern fallbackRegex, String configName) { + return getPattern(regexConfig, fallbackRegex, configName, ALLOW_PATTERN_CACHE); + } + + private static Pattern getDenyPattern(String regexConfig, Pattern fallbackRegex, String configName) { + return getPattern(regexConfig, fallbackRegex, configName, DENY_PATTERN_CACHE); + } + + private static Pattern getPattern(String regexConfig, Pattern fallbackRegex, String configName, + Map<String, Pattern> PATTERN_CACHE) { + + String regex = StringUtils.trimToEmpty(regexConfig); + + if (regex.isEmpty()) { + return null; + } + return PATTERN_CACHE.computeIfAbsent(regex, r -> { Review Comment: ```suggestion return patternCache.computeIfAbsent(regex, r -> { ``` ########## ql/src/test/org/apache/hadoop/hive/ql/txn/compactor/TestCompactorPropertyValidator.java: ########## @@ -0,0 +1,296 @@ +/* + * 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.hive.ql.txn.compactor; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestCompactorPropertyValidator { + + @ParameterizedTest + @ValueSource(strings = { + "mapreduce.map.memory.mb", + "mapreduce.reduce.memory.mb", + "mapreduce.map.cpu.vcores", + "mapreduce.reduce.cpu.vcores", + "hive.tez.container.size", + "tez.task.resource.memory.mb", + "mapreduce.job.heap.memory-mb.ratio", + "mapreduce.task.io.sort.factor", + "mapreduce.task.io.sort.mb", + "yarn.app.mapreduce.am.resource.cpu-vcores", + "yarn.app.mapreduce.am.resource.mb", + "tez.am.resource.memory.mb", + "tez.am.resource.cpu.vcores", + "tez.grouping.min-size", + "tez.grouping.max-size", + "tez.grouping.split-waves"}) + void testResourceConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "hive.compactor.gather.stats", + "hive.mr.compactor.gather.stats", + "hive.compactor.compact.insert.only", + "hive.compactor.crud.query.based", + "hive.compactor.max.num.delta", + "hive.compactor.aborted.txn.time.threshold", + "hive.compactor.abortedtxn.threshold", + "hive.compactor.active.delta.dir.threshold", + "hive.compactor.delta.num.threshold", + "hive.compactor.obsolete.delta.dir.threshold", + "hive.compactor.delta.pct.threshold", + "hive.compactor.small.delta.dir.threshold"}) + void testCompactorConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "mapreduce.job.queuename", + "mapred.job.queue.name", + "tez.queue.name", + "hive.compactor.job.queue", + "hive.compactor.request.queue"}) + void testQueueConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "orc.compress", + "orc.stripe.size", + "orc.bloom.filter.columns", + "hive.exec.orc.default.buffer.size"}) + void testOrcConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "fs.defaultFS", + "fs.s3a.access.key", + "fs.AbstractFileSystem.abfs.impl", + "hadoop.security.authentication", + "hadoop.security.auth_to_local", + "hadoop.rpc.socket.factory.class.default", + "hadoop.htrace.span.receiver.classes", + "hadoop.ssl.enabled.protocols", + "hadoop.zk.kerberos.principal", + "hadoop.http.authentication.kerberos.principal", + "hive.exec.pre.hooks", + "hive.exec.post.hooks", + "hive.metastore.rawstore.impl", + "hadoop.zk.kerberos.keytab", + "hadoop.http.authentication.kerberos.keytab", + "hive.security.authenticator.manager", + "hive.server2.job.credential.provider.path", + "hive.intermediate.compression.codec", + "hive.metastore.end.function.listeners", + "hadoop.token.files", + "javax.jdo.PersistenceManagerFactoryClass", + "mapreduce.map.java.opts", + "mapred.child.java.opts", + "hive.tez.java.opts", + "hive.exim.uri.scheme.whitelist", + "hive.exec.script.maxerrsize", + "hive.script.operator.env.blacklist", + "hive.llap.plugin.acl", + "hive.exec.scratchdir", + "hive.metastore.event.message.factory", + "hive.exec.plan", + "hive.aux.jars.path", + "hive.added.jars.path", + "hive.exec.perf.logger", + "hive.log4j.file", + "hive.exec.script.wrapper", + "hive.qtest.additional.partial.mask.pattern", + "mapreduce.job.classloader", + "mapreduce.application.classpath", + "yarn.application.classpath", + "mapreduce.task.profile.params", + "hadoop.tmp.dir", + "hive.added.archives.path", + "hive.tez.hs2.user.access", + "orc.encrypt"}) + void testParametersRejected(String key) { + assertFalse(allowed(new HiveConf(), key), + key + " has to be rejected by hive.compactor.conf.allowlist or hive.compactor.conf.denylist"); + } + + @ParameterizedTest + @ValueSource(strings = { + "hive.exec.pre.hooks", + "hive.exec.post.hooks", + "hive.exec.failure.hooks", + "hive.exec.driver.run.hooks", + "hive.exec.query.redactor.hooks", + "hive.exec.perf.logger", + "hive.optimize.cte.suggester.class", + "hive.tez.mapreduce.output.committer.class", + "tez.task.scale.memory.allocator.class", + "tez.task.scale.task.memory.allocator.class", + "orc.filter.plugin", + "orc.filter.plugin.allowlist", + "mapreduce.map.output.compress.codec", + "hive.exec.script.wrapper", + "hive.exec.script.trust", + "hive.exec.script.allow.partial.consumption", + "mapreduce.map.java.opts", + "mapreduce.reduce.java.opts", + "hive.tez.java.opts", + "mapreduce.map.env", + "mapreduce.reduce.env", + "hive.exec.scratchdir", + "hive.exec.local.scratchdir", + "hive.exec.stagingdir", + "hive.exec.submitviachild", + "hive.exec.submit.local.task.via.child", + "hive.exec.plan", + "hive.exec.log4j.file", + "orc.key.provider", + "orc.encrypt","orc.mask", + "hive.vectorized.adaptor.custom.udf.whitelist", + "hive.tez.hs2.user.access", + "hive.stats.dbclass", + "hive.stats.default.publisher", + "hive.stats.default.aggregator", + "orc.kryo.sarg", + "hive.compactor.table.props", + "hive.compactor.is.compressed", + "hive.compactor.input.tmp.dir", + "hive.compactor.input.dir"}) + void testParametersAllowedByAllowlistButRejectedByDenyList(String key) { Review Comment: Should this case run with `HIVE_COMPACTOR_CONF_ALLOWLIST=.*`? ########## common/src/java/org/apache/hadoop/hive/conf/HiveConfUtil.java: ########## @@ -97,6 +97,40 @@ public static Set<String> getHiddenSet(Configuration configuration) { return hiddenSet; } + /** + * Getting the set of the configurations which are immutable at runtime. Unlike + * {@link HiveConf#verifyAndSet(String, String)}, this works on any {@link Configuration} and not + * only on a {@link HiveConf}, which makes it usable on a JobConf. + * @param configuration The original configuration + * @return The list of the configuration prefixes which must not be modified at runtime + */ + public static Set<String> getRestrictedSet(Configuration configuration) { + Set<String> restrictedSet = new HashSet<String>(); + String restrictedListStr = HiveConf.getVar(configuration, HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST); + if (restrictedListStr != null) { + for (String entry : restrictedListStr.split(",")) { + restrictedSet.add(entry.trim()); + } + } + + String internalVariableListStr = HiveConf.getVar(configuration, ConfVars.HIVE_CONF_INTERNAL_VARIABLE_LIST); + if (internalVariableListStr != null) { + for (String entry : internalVariableListStr.split(",")) { + restrictedSet.add(entry.trim()); + } + } + + restrictedSet.add(ConfVars.HIVE_IN_TEST.varname); + restrictedSet.add(ConfVars.HIVE_CONF_RESTRICTED_LIST.varname); + restrictedSet.add(ConfVars.HIVE_CONF_HIDDEN_LIST.varname); + restrictedSet.add(ConfVars.HIVE_CONF_INTERNAL_VARIABLE_LIST.varname); + restrictedSet.add(ConfVars.HIVE_CONF_LOCKED_LIST.varname); + restrictedSet.add(ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST.varname); + restrictedSet.add(ConfVars.HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND.varname); + restrictedSet.add(ConfVars.HIVE_COMPACTOR_CONF_DENYLIST.varname); + return restrictedSet; + } Review Comment: How about using this method here in [HiveConf.setupRestrictList](https://github.com/kuczoram/hive/blob/b49390ba456b27460892c0dd57f4d81ae66a322b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java#L7120-L7124) for better consistency? At least, it sounds safer to add HIVE_COMPACTOR_CONF_ALLOWLIST, HIVE_COMPACTOR_CONF_ALLOWLIST_APPEND, and HIVE_COMPACTOR_CONF_DENYLIST there. ########## ql/src/test/org/apache/hadoop/hive/ql/txn/compactor/TestMRCompactorJobQueueConfiguration.java: ########## @@ -62,6 +65,99 @@ void testCreateBaseJobConfHasCorrectJobQueue(ConfSetup input) { assertEquals(input.expectedQueue, c.getQueueName(), "Test failed for the following input:" + input); } + /** + * A table property must not be able to inject JVM arguments into the compaction map containers, + * since the compaction job may run as a privileged user (see metastore.compactor.run.as.user) + * while the property itself is settable by anyone owning the table. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromTableProperties() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.mapreduce.map.java.opts", + "-javaagent:/tmp/evil.jar"), Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("mapreduce.map.java.opts"), "JVM options must not be settable via tblproperties"); + } + + /** + * Same as above, but through the ALTER TABLE ... COMPACT ... WITH OVERWRITE TBLPROPERTIES route, + * which reaches the same sink via a different argument. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromCompactionProperties() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("compactor.tmpjars", + "hdfs:///tmp/evil.jar"), Collections.emptyMap()); + assertNull(c.get("tmpjars"), "Classpath additions must not be settable via ALTER TABLE COMPACT"); + } + + /** + * Keys on hive.conf.restricted.list are immutable at runtime and rejected by + * {@link HiveConf#verifyAndSet(String, String)}. The compactor writes through the unchecked + * setter, so it must apply the same policy itself. + */ + @Test + void testCreateBaseJobConfIgnoresRestrictedListProperty() { + JobConf c = createBaseJobConf( + Collections.singletonMap("compactor.hive.security.authenticator.manager", "com.attacker.Authenticator"), + Collections.emptyMap(), Collections.singletonMap(HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST.varname, + "hive.security.authenticator.manager")); + assertNotEquals("com.attacker.Authenticator", c.get("hive.security.authenticator.manager"), + "Restricted configs must not be overridable via tblproperties"); + } + + /** Values on hive.conf.hidden.list are credentials; they must not be redirectable either. */ + @Test + void testCreateBaseJobConfIgnoresHiddenListProperty() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.fs.s3a.secret.key", "attacker-key"), + Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("fs.s3a.secret.key"), "Hidden configs must not be settable via tblproperties"); + } + + /** Guards against the fix being over-broad: documented resource tuning must keep working. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateOverrides() { + Map<String, String> tableProperties = new HashMap<>(); + tableProperties.put("compactor.mapreduce.map.memory.mb", "2048"); + tableProperties.put("compactor.hive.compactor.gather.stats", "false"); + JobConf c = createBaseJobConf(tableProperties, Collections.emptyMap(), Collections.emptyMap()); + assertEquals("2048", c.get("mapreduce.map.memory.mb")); + assertEquals("false", c.get("hive.compactor.gather.stats")); + } + + /** + * The "tblprops." prefix of ALTER TABLE ... COMPACT does not reach the job configuration, but it + * does reach the table properties handed to the ORC record writers, which select a key provider, + * an encryption or masking policy, and a filter plugin from that map. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), + Collections.singletonMap("tblprops.orc.key.provider", "kms://attacker"), Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertNull(tableProps.get("orc.key.provider"), + "Encryption key provider must not be settable via ALTER TABLE COMPACT"); + } + + /** Guards against over-blocking: ordinary ORC tuning through "tblprops." must still work. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("tblprops.orc.compress", "ZLIB"), + Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertEquals("ZLIB", tableProps.get("orc.compress")); + } + + + private static JobConf createBaseJobConf(Map<String, String> tableProperties, Review Comment: ```suggestion private static JobConf createBaseJobConf(Map<String, String> tableProperties, ``` ########## ql/src/test/org/apache/hadoop/hive/ql/txn/compactor/TestCompactorPropertyValidator.java: ########## @@ -0,0 +1,296 @@ +/* + * 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.hive.ql.txn.compactor; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestCompactorPropertyValidator { + + @ParameterizedTest + @ValueSource(strings = { + "mapreduce.map.memory.mb", + "mapreduce.reduce.memory.mb", + "mapreduce.map.cpu.vcores", + "mapreduce.reduce.cpu.vcores", + "hive.tez.container.size", + "tez.task.resource.memory.mb", + "mapreduce.job.heap.memory-mb.ratio", + "mapreduce.task.io.sort.factor", + "mapreduce.task.io.sort.mb", + "yarn.app.mapreduce.am.resource.cpu-vcores", + "yarn.app.mapreduce.am.resource.mb", + "tez.am.resource.memory.mb", + "tez.am.resource.cpu.vcores", + "tez.grouping.min-size", + "tez.grouping.max-size", + "tez.grouping.split-waves"}) + void testResourceConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "hive.compactor.gather.stats", + "hive.mr.compactor.gather.stats", + "hive.compactor.compact.insert.only", + "hive.compactor.crud.query.based", + "hive.compactor.max.num.delta", + "hive.compactor.aborted.txn.time.threshold", + "hive.compactor.abortedtxn.threshold", + "hive.compactor.active.delta.dir.threshold", + "hive.compactor.delta.num.threshold", + "hive.compactor.obsolete.delta.dir.threshold", + "hive.compactor.delta.pct.threshold", + "hive.compactor.small.delta.dir.threshold"}) + void testCompactorConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "mapreduce.job.queuename", + "mapred.job.queue.name", + "tez.queue.name", + "hive.compactor.job.queue", + "hive.compactor.request.queue"}) + void testQueueConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "orc.compress", + "orc.stripe.size", + "orc.bloom.filter.columns", + "hive.exec.orc.default.buffer.size"}) + void testOrcConfigs(String key) { + assertTrue(allowed(new HiveConf(), key), key + " has to be settable in compaction job config."); + } + + @ParameterizedTest + @ValueSource(strings = { + "fs.defaultFS", + "fs.s3a.access.key", + "fs.AbstractFileSystem.abfs.impl", + "hadoop.security.authentication", + "hadoop.security.auth_to_local", + "hadoop.rpc.socket.factory.class.default", + "hadoop.htrace.span.receiver.classes", + "hadoop.ssl.enabled.protocols", + "hadoop.zk.kerberos.principal", + "hadoop.http.authentication.kerberos.principal", + "hive.exec.pre.hooks", + "hive.exec.post.hooks", + "hive.metastore.rawstore.impl", + "hadoop.zk.kerberos.keytab", + "hadoop.http.authentication.kerberos.keytab", + "hive.security.authenticator.manager", + "hive.server2.job.credential.provider.path", + "hive.intermediate.compression.codec", + "hive.metastore.end.function.listeners", + "hadoop.token.files", + "javax.jdo.PersistenceManagerFactoryClass", + "mapreduce.map.java.opts", + "mapred.child.java.opts", + "hive.tez.java.opts", + "hive.exim.uri.scheme.whitelist", + "hive.exec.script.maxerrsize", + "hive.script.operator.env.blacklist", + "hive.llap.plugin.acl", + "hive.exec.scratchdir", + "hive.metastore.event.message.factory", + "hive.exec.plan", + "hive.aux.jars.path", + "hive.added.jars.path", + "hive.exec.perf.logger", + "hive.log4j.file", + "hive.exec.script.wrapper", + "hive.qtest.additional.partial.mask.pattern", + "mapreduce.job.classloader", + "mapreduce.application.classpath", + "yarn.application.classpath", + "mapreduce.task.profile.params", + "hadoop.tmp.dir", + "hive.added.archives.path", + "hive.tez.hs2.user.access", + "orc.encrypt"}) Review Comment: I suspect these patterns are not tested. - `(^tmp(jars|files|archives)$)` - `credentials[^.]*` ########## ql/src/test/org/apache/hadoop/hive/ql/txn/compactor/TestMRCompactorJobQueueConfiguration.java: ########## @@ -62,6 +65,99 @@ void testCreateBaseJobConfHasCorrectJobQueue(ConfSetup input) { assertEquals(input.expectedQueue, c.getQueueName(), "Test failed for the following input:" + input); } + /** + * A table property must not be able to inject JVM arguments into the compaction map containers, + * since the compaction job may run as a privileged user (see metastore.compactor.run.as.user) + * while the property itself is settable by anyone owning the table. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromTableProperties() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.mapreduce.map.java.opts", + "-javaagent:/tmp/evil.jar"), Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("mapreduce.map.java.opts"), "JVM options must not be settable via tblproperties"); + } + + /** + * Same as above, but through the ALTER TABLE ... COMPACT ... WITH OVERWRITE TBLPROPERTIES route, + * which reaches the same sink via a different argument. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromCompactionProperties() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("compactor.tmpjars", + "hdfs:///tmp/evil.jar"), Collections.emptyMap()); + assertNull(c.get("tmpjars"), "Classpath additions must not be settable via ALTER TABLE COMPACT"); + } + + /** + * Keys on hive.conf.restricted.list are immutable at runtime and rejected by + * {@link HiveConf#verifyAndSet(String, String)}. The compactor writes through the unchecked + * setter, so it must apply the same policy itself. + */ + @Test + void testCreateBaseJobConfIgnoresRestrictedListProperty() { + JobConf c = createBaseJobConf( + Collections.singletonMap("compactor.hive.security.authenticator.manager", "com.attacker.Authenticator"), + Collections.emptyMap(), Collections.singletonMap(HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST.varname, + "hive.security.authenticator.manager")); + assertNotEquals("com.attacker.Authenticator", c.get("hive.security.authenticator.manager"), + "Restricted configs must not be overridable via tblproperties"); + } + + /** Values on hive.conf.hidden.list are credentials; they must not be redirectable either. */ + @Test + void testCreateBaseJobConfIgnoresHiddenListProperty() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.fs.s3a.secret.key", "attacker-key"), + Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("fs.s3a.secret.key"), "Hidden configs must not be settable via tblproperties"); + } + + /** Guards against the fix being over-broad: documented resource tuning must keep working. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateOverrides() { + Map<String, String> tableProperties = new HashMap<>(); + tableProperties.put("compactor.mapreduce.map.memory.mb", "2048"); + tableProperties.put("compactor.hive.compactor.gather.stats", "false"); + JobConf c = createBaseJobConf(tableProperties, Collections.emptyMap(), Collections.emptyMap()); + assertEquals("2048", c.get("mapreduce.map.memory.mb")); + assertEquals("false", c.get("hive.compactor.gather.stats")); + } + + /** + * The "tblprops." prefix of ALTER TABLE ... COMPACT does not reach the job configuration, but it + * does reach the table properties handed to the ORC record writers, which select a key provider, + * an encryption or masking policy, and a filter plugin from that map. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), + Collections.singletonMap("tblprops.orc.key.provider", "kms://attacker"), Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertNull(tableProps.get("orc.key.provider"), + "Encryption key provider must not be settable via ALTER TABLE COMPACT"); + } + + /** Guards against over-blocking: ordinary ORC tuning through "tblprops." must still work. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("tblprops.orc.compress", "ZLIB"), + Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertEquals("ZLIB", tableProps.get("orc.compress")); + } + + + private static JobConf createBaseJobConf(Map<String, String> tableProperties, + Map<String, String> compactionProperties, Map<String, String> confProperties) { + Table tbl = createPersonTable(); + tbl.setParameters(new HashMap<>(tableProperties)); + MRCompactor compactor = new MRCompactor(null); + CompactionInfo ci = new CompactionInfo(tbl.getDbName(), tbl.getTableName(), null, CompactionType.MAJOR); + ci.properties = new StringableMap(new HashMap<>(compactionProperties)).toString(); + HiveConf conf = new HiveConf(); + confProperties.forEach(conf::set); + return compactor.createBaseJobConf(conf, "test-job", tbl, tbl.getSd(), new ValidReaderWriteIdList(), ci); + } Review Comment: ```suggestion MRCompactor compactor = new MRCompactor(null); CompactionInfo ci = new CompactionInfo(tbl.getDbName(), tbl.getTableName(), null, CompactionType.MAJOR); ci.properties = new StringableMap(new HashMap<>(compactionProperties)).toString(); HiveConf conf = new HiveConf(); confProperties.forEach(conf::set); return compactor.createBaseJobConf(conf, "test-job", tbl, tbl.getSd(), new ValidReaderWriteIdList(), ci); } ``` ########## ql/src/test/org/apache/hadoop/hive/ql/txn/compactor/TestMRCompactorJobQueueConfiguration.java: ########## @@ -62,6 +65,99 @@ void testCreateBaseJobConfHasCorrectJobQueue(ConfSetup input) { assertEquals(input.expectedQueue, c.getQueueName(), "Test failed for the following input:" + input); } + /** + * A table property must not be able to inject JVM arguments into the compaction map containers, + * since the compaction job may run as a privileged user (see metastore.compactor.run.as.user) + * while the property itself is settable by anyone owning the table. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromTableProperties() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.mapreduce.map.java.opts", + "-javaagent:/tmp/evil.jar"), Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("mapreduce.map.java.opts"), "JVM options must not be settable via tblproperties"); + } + + /** + * Same as above, but through the ALTER TABLE ... COMPACT ... WITH OVERWRITE TBLPROPERTIES route, + * which reaches the same sink via a different argument. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedMRPropertyFromCompactionProperties() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("compactor.tmpjars", + "hdfs:///tmp/evil.jar"), Collections.emptyMap()); + assertNull(c.get("tmpjars"), "Classpath additions must not be settable via ALTER TABLE COMPACT"); + } + + /** + * Keys on hive.conf.restricted.list are immutable at runtime and rejected by + * {@link HiveConf#verifyAndSet(String, String)}. The compactor writes through the unchecked + * setter, so it must apply the same policy itself. + */ + @Test + void testCreateBaseJobConfIgnoresRestrictedListProperty() { + JobConf c = createBaseJobConf( + Collections.singletonMap("compactor.hive.security.authenticator.manager", "com.attacker.Authenticator"), + Collections.emptyMap(), Collections.singletonMap(HiveConf.ConfVars.HIVE_CONF_RESTRICTED_LIST.varname, + "hive.security.authenticator.manager")); + assertNotEquals("com.attacker.Authenticator", c.get("hive.security.authenticator.manager"), + "Restricted configs must not be overridable via tblproperties"); + } + + /** Values on hive.conf.hidden.list are credentials; they must not be redirectable either. */ + @Test + void testCreateBaseJobConfIgnoresHiddenListProperty() { + JobConf c = createBaseJobConf(Collections.singletonMap("compactor.fs.s3a.secret.key", "attacker-key"), + Collections.emptyMap(), Collections.emptyMap()); + assertNull(c.get("fs.s3a.secret.key"), "Hidden configs must not be settable via tblproperties"); + } + + /** Guards against the fix being over-broad: documented resource tuning must keep working. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateOverrides() { + Map<String, String> tableProperties = new HashMap<>(); + tableProperties.put("compactor.mapreduce.map.memory.mb", "2048"); + tableProperties.put("compactor.hive.compactor.gather.stats", "false"); + JobConf c = createBaseJobConf(tableProperties, Collections.emptyMap(), Collections.emptyMap()); + assertEquals("2048", c.get("mapreduce.map.memory.mb")); + assertEquals("false", c.get("hive.compactor.gather.stats")); + } + + /** + * The "tblprops." prefix of ALTER TABLE ... COMPACT does not reach the job configuration, but it + * does reach the table properties handed to the ORC record writers, which select a key provider, + * an encryption or masking policy, and a filter plugin from that map. + */ + @Test + void testCreateBaseJobConfIgnoresDeniedTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), + Collections.singletonMap("tblprops.orc.key.provider", "kms://attacker"), Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertNull(tableProps.get("orc.key.provider"), + "Encryption key provider must not be settable via ALTER TABLE COMPACT"); + } + + /** Guards against over-blocking: ordinary ORC tuning through "tblprops." must still work. */ + @Test + void testCreateBaseJobConfStillAppliesLegitimateTblProperty() { + JobConf c = createBaseJobConf(Collections.emptyMap(), Collections.singletonMap("tblprops.orc.compress", "ZLIB"), + Collections.emptyMap()); + Map<String, String> tableProps = new StringableMap(c.get("hive.compactor.table.props")); + assertEquals("ZLIB", tableProps.get("orc.compress")); + } + + + private static JobConf createBaseJobConf(Map<String, String> tableProperties, + Map<String, String> compactionProperties, Map<String, String> confProperties) { Review Comment: ```suggestion Map<String, String> compactionProperties, Map<String, String> confProperties) { ``` -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
