|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iceberg.hive; |
| 21 | + |
| 22 | +import java.security.PrivilegedExceptionAction; |
| 23 | +import java.util.Collections; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.UUID; |
| 27 | +import java.util.function.Consumer; |
| 28 | +import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars; |
| 29 | +import org.apache.hadoop.hive.ql.security.authorization.plugin.metastore.HiveMetaStoreAuthorizer; |
| 30 | +import org.apache.hadoop.security.UserGroupInformation; |
| 31 | +import org.apache.iceberg.CatalogProperties; |
| 32 | +import org.apache.iceberg.CatalogUtil; |
| 33 | +import org.apache.iceberg.Schema; |
| 34 | +import org.apache.iceberg.catalog.Namespace; |
| 35 | +import org.apache.iceberg.catalog.TableIdentifier; |
| 36 | +import org.apache.iceberg.exceptions.ForbiddenException; |
| 37 | +import org.assertj.core.api.Assertions; |
| 38 | +import org.junit.jupiter.api.AfterEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 41 | + |
| 42 | +class TestHiveCatalogAccessControl { |
| 43 | + private static final Schema DUMMY_SCHEMA = new Schema(); |
| 44 | + |
| 45 | + @RegisterExtension |
| 46 | + private static final HiveMetastoreExtension HIVE_METASTORE_EXTENSION = HiveMetastoreExtension.builder() |
| 47 | + .withConfig(Map.of( |
| 48 | + ConfVars.HIVE_AUTHORIZATION_MANAGER.getVarname(), MockHiveAuthorizerFactory.class.getName(), |
| 49 | + ConfVars.PRE_EVENT_LISTENERS.getVarname(), HiveMetaStoreAuthorizer.class.getName() |
| 50 | + )).build(); |
| 51 | + |
| 52 | + @AfterEach |
| 53 | + void afterEach() throws Exception { |
| 54 | + HIVE_METASTORE_EXTENSION.metastore().reset(); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void testNamespace() throws Exception { |
| 59 | + var namespace = Namespace.of("permission_test_db"); |
| 60 | + asAuthorized(catalog -> catalog.createNamespace(namespace, Collections.emptyMap())); |
| 61 | + asUnauthorized(catalog -> { |
| 62 | + // Should HMS omit namespaces? |
| 63 | + Assertions.assertThat(catalog.listNamespaces()).isEqualTo(List.of(Namespace.of("default"), namespace)); |
| 64 | + Assertions.assertThatThrownBy(() -> catalog.namespaceExists(namespace)).isInstanceOf(ForbiddenException.class); |
| 65 | + Assertions.assertThatThrownBy(() -> catalog.loadNamespaceMetadata(namespace)) |
| 66 | + .isInstanceOf(ForbiddenException.class); |
| 67 | + var newNamespace = Namespace.of("new_db"); |
| 68 | + Assertions.assertThatThrownBy(() -> catalog.createNamespace(newNamespace)).isInstanceOf(ForbiddenException.class); |
| 69 | + Assertions.assertThatThrownBy(() -> catalog.dropNamespace(namespace)).isInstanceOf(ForbiddenException.class); |
| 70 | + var properties = Collections.singletonMap("key", "value"); |
| 71 | + Assertions.assertThatThrownBy(() -> catalog.setProperties(namespace, properties)) |
| 72 | + .isInstanceOf(ForbiddenException.class); |
| 73 | + var propertyKeys = properties.keySet(); |
| 74 | + Assertions.assertThatThrownBy(() -> catalog.removeProperties(namespace, propertyKeys)) |
| 75 | + .isInstanceOf(ForbiddenException.class); |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testTable() throws Exception { |
| 81 | + Namespace namespace = Namespace.of("permission_test_db"); |
| 82 | + TableIdentifier table = TableIdentifier.of(namespace, "permission_test_table"); |
| 83 | + asAuthorized(catalog -> { |
| 84 | + catalog.createNamespace(namespace, Collections.emptyMap()); |
| 85 | + catalog.createTable(table, new Schema()); |
| 86 | + }); |
| 87 | + asUnauthorized(catalog -> { |
| 88 | + // Should HMS omit namespaces? |
| 89 | + Assertions.assertThat(catalog.listTables(namespace)).isEqualTo(Collections.singletonList(table)); |
| 90 | + Assertions.assertThatThrownBy(() -> catalog.tableExists(table)).isInstanceOf(ForbiddenException.class); |
| 91 | + Assertions.assertThatThrownBy(() -> catalog.loadTable(table)).isInstanceOf(ForbiddenException.class); |
| 92 | + var newTable = TableIdentifier.of(namespace, "new_table"); |
| 93 | + Assertions.assertThatThrownBy(() -> catalog.createTable(newTable, DUMMY_SCHEMA)) |
| 94 | + .isInstanceOf(ForbiddenException.class); |
| 95 | + Assertions.assertThatThrownBy(() -> catalog.renameTable(table, newTable)).isInstanceOf(ForbiddenException.class); |
| 96 | + Assertions.assertThatThrownBy(() -> catalog.dropTable(table)).isInstanceOf(ForbiddenException.class); |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void testView() throws Exception { |
| 102 | + Namespace namespace = Namespace.of("permission_test_db"); |
| 103 | + TableIdentifier view = TableIdentifier.of(namespace, "permission_test_view"); |
| 104 | + asAuthorized(catalog -> { |
| 105 | + catalog.createNamespace(namespace, Collections.emptyMap()); |
| 106 | + catalog.buildView(view).withQuery("hive", "SELECT 1 AS id").withSchema(new Schema()) |
| 107 | + .withDefaultNamespace(namespace).create(); |
| 108 | + }); |
| 109 | + asUnauthorized(catalog -> { |
| 110 | + // Should HMS omit namespaces? |
| 111 | + Assertions.assertThat(catalog.listViews(namespace)).isEqualTo(Collections.singletonList(view)); |
| 112 | + Assertions.assertThatThrownBy(() -> catalog.viewExists(view)).isInstanceOf(ForbiddenException.class); |
| 113 | + Assertions.assertThatThrownBy(() -> catalog.loadView(view)).isInstanceOf(ForbiddenException.class); |
| 114 | + var newView = TableIdentifier.of(namespace, "new_view"); |
| 115 | + var builder = catalog.buildView(newView).withQuery("hive", "SELECT 1 AS id").withSchema(DUMMY_SCHEMA) |
| 116 | + .withDefaultNamespace(namespace); |
| 117 | + Assertions.assertThatThrownBy(builder::create).isInstanceOf(ForbiddenException.class); |
| 118 | + Assertions.assertThatThrownBy(() -> catalog.renameView(view, newView)).isInstanceOf(ForbiddenException.class); |
| 119 | + Assertions.assertThatThrownBy(() -> catalog.dropView(view)).isInstanceOf(ForbiddenException.class); |
| 120 | + }); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + void testTransaction() throws Exception { |
| 125 | + var namespace = Namespace.of("permission_test_db"); |
| 126 | + asAuthorized(catalog -> catalog.createNamespace(namespace, Collections.emptyMap())); |
| 127 | + asUnauthorized(catalog -> { |
| 128 | + var newTable = TableIdentifier.of(namespace, "new_table"); |
| 129 | + Assertions.assertThatThrownBy(() -> catalog.newCreateTableTransaction(newTable, DUMMY_SCHEMA)) |
| 130 | + .isInstanceOf(ForbiddenException.class); |
| 131 | + Assertions.assertThatThrownBy(() -> catalog.newReplaceTableTransaction(newTable, DUMMY_SCHEMA, true)) |
| 132 | + .isInstanceOf(ForbiddenException.class); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + private static void asAuthorized(Consumer<HiveCatalog> consumer) throws Exception { |
| 137 | + withUser("authorized_user", consumer); |
| 138 | + } |
| 139 | + |
| 140 | + private static void asUnauthorized(Consumer<HiveCatalog> consumer) throws Exception { |
| 141 | + withUser(MockHiveAuthorizer.PERMISSION_TEST_USER, consumer); |
| 142 | + } |
| 143 | + |
| 144 | + private static void withUser(String username, Consumer<HiveCatalog> consumer) throws Exception { |
| 145 | + var ugi = UserGroupInformation.createRemoteUser(username); |
| 146 | + ugi.doAs((PrivilegedExceptionAction<Void>) () -> { |
| 147 | + try (HiveCatalog catalog = createCatalog()) { |
| 148 | + consumer.accept(catalog); |
| 149 | + return null; |
| 150 | + } |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + private static HiveCatalog createCatalog() { |
| 155 | + return (HiveCatalog) |
| 156 | + CatalogUtil.loadCatalog( |
| 157 | + HiveCatalog.class.getName(), |
| 158 | + UUID.randomUUID().toString(), |
| 159 | + Map.of( |
| 160 | + CatalogProperties.CLIENT_POOL_CACHE_KEYS, "ugi" |
| 161 | + ), |
| 162 | + HIVE_METASTORE_EXTENSION.hiveConf()); |
| 163 | + } |
| 164 | +} |
0 commit comments