|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "kudu/master/ranger_authz_provider.h" |
| 19 | + |
| 20 | +#include <algorithm> |
| 21 | +#include <ostream> |
| 22 | + |
| 23 | +#include <gflags/gflags.h> |
| 24 | +#include <glog/logging.h> |
| 25 | + |
| 26 | +#include "kudu/common/common.pb.h" |
| 27 | +#include "kudu/gutil/map-util.h" |
| 28 | +#include "kudu/ranger/ranger.pb.h" |
| 29 | +#include "kudu/security/token.pb.h" |
| 30 | +#include "kudu/util/status.h" |
| 31 | + |
| 32 | +DECLARE_string(ranger_config_path); |
| 33 | + |
| 34 | +namespace kudu { |
| 35 | +class MetricEntity; |
| 36 | +namespace master { |
| 37 | + |
| 38 | +using kudu::security::ColumnPrivilegePB; |
| 39 | +using kudu::security::TablePrivilegePB; |
| 40 | +using kudu::ranger::ActionPB; |
| 41 | +using kudu::ranger::ActionHash; |
| 42 | +using std::string; |
| 43 | +using std::unordered_set; |
| 44 | + |
| 45 | +RangerAuthzProvider::RangerAuthzProvider(const scoped_refptr<MetricEntity>& metric_entity) : |
| 46 | + client_(metric_entity) {} |
| 47 | + |
| 48 | +Status RangerAuthzProvider::Start() { |
| 49 | + RETURN_NOT_OK(client_.Start()); |
| 50 | + |
| 51 | + return Status::OK(); |
| 52 | +} |
| 53 | + |
| 54 | +Status RangerAuthzProvider::AuthorizeCreateTable(const string& table_name, |
| 55 | + const string& user, |
| 56 | + const string& /*owner*/) { |
| 57 | + if (IsTrustedUser(user)) { |
| 58 | + return Status::OK(); |
| 59 | + } |
| 60 | + return client_.AuthorizeAction(user, ActionPB::CREATE, table_name); |
| 61 | +} |
| 62 | + |
| 63 | +Status RangerAuthzProvider::AuthorizeDropTable(const string& table_name, |
| 64 | + const string& user) { |
| 65 | + if (IsTrustedUser(user)) { |
| 66 | + return Status::OK(); |
| 67 | + } |
| 68 | + return client_.AuthorizeAction(user, ActionPB::DROP, table_name); |
| 69 | +} |
| 70 | + |
| 71 | +Status RangerAuthzProvider::AuthorizeAlterTable(const string& old_table, |
| 72 | + const string& new_table, |
| 73 | + const string& user) { |
| 74 | + if (IsTrustedUser(user)) { |
| 75 | + return Status::OK(); |
| 76 | + } |
| 77 | + // Table alteration requires ALTER ON TABLE if no rename is done. To prevent |
| 78 | + // privilege escalation we require ALL on the old table and CREATE on the new |
| 79 | + // table (database). |
| 80 | + if (old_table == new_table) { |
| 81 | + return client_.AuthorizeAction(user, ActionPB::ALTER, old_table); |
| 82 | + } |
| 83 | + |
| 84 | + RETURN_NOT_OK(client_.AuthorizeAction(user, ActionPB::ALL, old_table)); |
| 85 | + return client_.AuthorizeAction(user, ActionPB::CREATE, new_table); |
| 86 | +} |
| 87 | + |
| 88 | +Status RangerAuthzProvider::AuthorizeGetTableMetadata(const string& table_name, |
| 89 | + const string& user) { |
| 90 | + if (IsTrustedUser(user)) { |
| 91 | + return Status::OK(); |
| 92 | + } |
| 93 | + return client_.AuthorizeAction(user, ActionPB::METADATA, table_name); |
| 94 | +} |
| 95 | + |
| 96 | +Status RangerAuthzProvider::AuthorizeListTables(const string& user, |
| 97 | + unordered_set<string>* table_names, |
| 98 | + bool* checked_table_names) { |
| 99 | + if (IsTrustedUser(user)) { |
| 100 | + *checked_table_names = false; |
| 101 | + return Status::OK(); |
| 102 | + } |
| 103 | + |
| 104 | + *checked_table_names = true; |
| 105 | + return client_.AuthorizeActionMultipleTables(user, ActionPB::METADATA, table_names); |
| 106 | +} |
| 107 | + |
| 108 | +Status RangerAuthzProvider::AuthorizeGetTableStatistics(const string& table_name, |
| 109 | + const string& user) { |
| 110 | + if (IsTrustedUser(user)) { |
| 111 | + return Status::OK(); |
| 112 | + } |
| 113 | + // Statistics contain data (e.g. number of rows) that requires the 'SELECT ON TABLE' |
| 114 | + // privilege. |
| 115 | + return client_.AuthorizeAction(user, ActionPB::SELECT, table_name); |
| 116 | +} |
| 117 | + |
| 118 | +Status RangerAuthzProvider::FillTablePrivilegePB(const string& table_name, |
| 119 | + const string& user, |
| 120 | + const SchemaPB& schema_pb, |
| 121 | + TablePrivilegePB* pb) { |
| 122 | + DCHECK(pb); |
| 123 | + DCHECK(pb->has_table_id()); |
| 124 | + if (IsTrustedUser(user) || client_.AuthorizeAction(user, ActionPB::ALL, table_name).ok()) { |
| 125 | + pb->set_delete_privilege(true); |
| 126 | + pb->set_insert_privilege(true); |
| 127 | + pb->set_scan_privilege(true); |
| 128 | + pb->set_update_privilege(true); |
| 129 | + return Status::OK(); |
| 130 | + } |
| 131 | + |
| 132 | + unordered_set<ActionPB, ActionHash> actions = { |
| 133 | + ActionPB::DELETE, |
| 134 | + ActionPB::INSERT, |
| 135 | + ActionPB::UPDATE, |
| 136 | + ActionPB::SELECT |
| 137 | + }; |
| 138 | + |
| 139 | + // Check if the user has any table-level privileges. If yes, we set them. If |
| 140 | + // select is included, we can also return. |
| 141 | + auto s = client_.AuthorizeActions(user, table_name, &actions); |
| 142 | + if (s.ok()) { |
| 143 | + for (const ActionPB& action : actions) { |
| 144 | + switch (action) { |
| 145 | + case ActionPB::DELETE: |
| 146 | + pb->set_delete_privilege(true); |
| 147 | + break; |
| 148 | + case ActionPB::UPDATE: |
| 149 | + pb->set_update_privilege(true); |
| 150 | + break; |
| 151 | + case ActionPB::INSERT: |
| 152 | + pb->set_insert_privilege(true); |
| 153 | + break; |
| 154 | + case ActionPB::SELECT: |
| 155 | + pb->set_scan_privilege(true); |
| 156 | + break; |
| 157 | + default: |
| 158 | + LOG(WARNING) << "Unexpected action returned by Ranger: " << ActionPB_Name(action); |
| 159 | + break; |
| 160 | + } |
| 161 | + if (pb->scan_privilege()) { |
| 162 | + return Status::OK(); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + // If select is not allowed on the table level we need to dig in and set |
| 168 | + // select permissions on the column level. |
| 169 | + static ColumnPrivilegePB scan_col_privilege; |
| 170 | + scan_col_privilege.set_scan_privilege(true); |
| 171 | + |
| 172 | + unordered_set<string> column_names; |
| 173 | + for (const auto& col : schema_pb.columns()) { |
| 174 | + column_names.emplace(col.name()); |
| 175 | + } |
| 176 | + |
| 177 | + // If AuthorizeAction returns NotAuthorized, that means no column-level select |
| 178 | + // is allowed to the user. In this case we return the previous status. |
| 179 | + // Otherwise we populate schema_pb and return OK. |
| 180 | + // |
| 181 | + // TODO(abukor): revisit if it's worth merge this into the previous request |
| 182 | + if (!client_.AuthorizeActionMultipleColumns(user, ActionPB::SELECT, table_name, |
| 183 | + &column_names).ok()) { |
| 184 | + return s; |
| 185 | + } |
| 186 | + |
| 187 | + for (const auto& col : schema_pb.columns()) { |
| 188 | + if (ContainsKey(column_names, col.name())) { |
| 189 | + InsertOrDie(pb->mutable_column_privileges(), col.id(), scan_col_privilege); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + |
| 194 | + return Status::OK(); |
| 195 | +} |
| 196 | + |
| 197 | +bool RangerAuthzProvider::IsEnabled() { |
| 198 | + return !FLAGS_ranger_config_path.empty(); |
| 199 | +} |
| 200 | + |
| 201 | +} // namespace master |
| 202 | +} // namespace kudu |
0 commit comments