diff --git a/be/src/exec/operator/meta_scan_operator.cpp b/be/src/exec/operator/meta_scan_operator.cpp index c94c7e213de8eb..f394cbf55d2bea 100644 --- a/be/src/exec/operator/meta_scan_operator.cpp +++ b/be/src/exec/operator/meta_scan_operator.cpp @@ -30,7 +30,7 @@ Status MetaScanLocalState::_init_scanners(std::list* scanners) { for (auto& scan_range : _scan_ranges) { std::shared_ptr scanner = MetaScanner::create_shared(state(), this, p._tuple_id, scan_range, p._limit, - custom_profile(), p._user_identity); + custom_profile(), p._user_identity, p._current_roles); RETURN_IF_ERROR(scanner->init(state(), _conjuncts)); scanners->push_back(scanner); } @@ -55,6 +55,9 @@ MetaScanOperatorX::MetaScanOperatorX(ObjectPool* pool, const TPlanNode& tnode, i if (tnode.meta_scan_node.__isset.current_user_ident) { _user_identity = tnode.meta_scan_node.current_user_ident; } + if (tnode.meta_scan_node.__isset.current_roles) { + _current_roles = tnode.meta_scan_node.current_roles; + } } } // namespace doris diff --git a/be/src/exec/operator/meta_scan_operator.h b/be/src/exec/operator/meta_scan_operator.h index e14e9282f4f273..e7e8d1d731afc7 100644 --- a/be/src/exec/operator/meta_scan_operator.h +++ b/be/src/exec/operator/meta_scan_operator.h @@ -19,6 +19,7 @@ #include +#include #include #include "common/status.h" @@ -61,6 +62,8 @@ class MetaScanOperatorX final : public ScanOperatorX { TupleId _tuple_id; TUserIdentity _user_identity; + // SU narrowing: the session's active role subset (TMetaScanNode.current_roles). Empty = none. + std::set _current_roles; }; /// Instantiated once in scan_operator.cpp; suppresses per-TU implicit instantiation. diff --git a/be/src/exec/operator/schema_scan_operator.cpp b/be/src/exec/operator/schema_scan_operator.cpp index aa9add4beeba3c..cd97c9d77b1e7b 100644 --- a/be/src/exec/operator/schema_scan_operator.cpp +++ b/be/src/exec/operator/schema_scan_operator.cpp @@ -113,6 +113,12 @@ Status SchemaScanOperatorX::init(const TPlanNode& tnode, RuntimeState* state) { } } + // SU narrowing: carry the SU-narrowed role subset from the FE plan to the scanners so the + // BE->FE metadata RPCs narrow name visibility (empty/unset = no narrowing, prior behavior). + if (tnode.schema_scan_node.__isset.current_roles) { + _common_scanner_param->current_roles = tnode.schema_scan_node.current_roles; + } + if (tnode.schema_scan_node.__isset.ip) { _common_scanner_param->ip = state->obj_pool()->add(new std::string(tnode.schema_scan_node.ip)); diff --git a/be/src/exec/scan/meta_scanner.cpp b/be/src/exec/scan/meta_scanner.cpp index 282713257d7758..49659e7545250b 100644 --- a/be/src/exec/scan/meta_scanner.cpp +++ b/be/src/exec/scan/meta_scanner.cpp @@ -27,6 +27,7 @@ #include #include #include +#include #include "common/cast_set.h" #include "common/logging.h" @@ -55,13 +56,21 @@ namespace doris { MetaScanner::MetaScanner(RuntimeState* state, ScanLocalStateBase* local_state, TupleId tuple_id, const TScanRangeParams& scan_range, int64_t limit, RuntimeProfile* profile, - TUserIdentity user_identity) + TUserIdentity user_identity, std::set current_roles) : Scanner(state, local_state, limit, profile), _meta_eos(false), _tuple_id(tuple_id), _user_identity(user_identity), + _current_roles(std::move(current_roles)), _scan_range(scan_range.scan_range) {} +void MetaScanner::_set_session_identity(TMetadataTableRequestParams* params) const { + params->__set_current_user_ident(_user_identity); + if (!_current_roles.empty()) { + params->__set_current_roles(_current_roles); + } +} + Status MetaScanner::_open_impl(RuntimeState* state) { VLOG_CRITICAL << "MetaScanner::open"; RETURN_IF_ERROR(Scanner::_open_impl(state)); @@ -313,6 +322,7 @@ Status MetaScanner::_build_backends_metadata_request(const TMetaScanRange& meta_ // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::BACKENDS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_backends_metadata_params(meta_scan_range.backends_params); request->__set_metada_table_params(metadata_table_params); @@ -332,6 +342,7 @@ Status MetaScanner::_build_frontends_metadata_request(const TMetaScanRange& meta // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::FRONTENDS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_frontends_metadata_params(meta_scan_range.frontends_params); request->__set_metada_table_params(metadata_table_params); @@ -351,6 +362,7 @@ Status MetaScanner::_build_frontends_disks_metadata_request(const TMetaScanRange // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::FRONTENDS_DISKS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_frontends_metadata_params(meta_scan_range.frontends_params); request->__set_metada_table_params(metadata_table_params); @@ -368,7 +380,7 @@ Status MetaScanner::_build_workload_sched_policy_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::WORKLOAD_SCHED_POLICY); - metadata_table_params.__set_current_user_ident(_user_identity); + _set_session_identity(&metadata_table_params); request->__set_metada_table_params(metadata_table_params); return Status::OK(); @@ -384,7 +396,7 @@ Status MetaScanner::_build_catalogs_metadata_request(const TMetaScanRange& meta_ // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::CATALOGS); - metadata_table_params.__set_current_user_ident(_user_identity); + _set_session_identity(&metadata_table_params); request->__set_metada_table_params(metadata_table_params); return Status::OK(); @@ -404,6 +416,7 @@ Status MetaScanner::_build_materialized_views_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::MATERIALIZED_VIEWS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_materialized_views_metadata_params( meta_scan_range.materialized_views_params); @@ -425,6 +438,7 @@ Status MetaScanner::_build_partitions_metadata_request(const TMetaScanRange& met // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::PARTITIONS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_partitions_metadata_params(meta_scan_range.partitions_params); request->__set_metada_table_params(metadata_table_params); @@ -444,6 +458,7 @@ Status MetaScanner::_build_jobs_metadata_request(const TMetaScanRange& meta_scan // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::JOBS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_jobs_metadata_params(meta_scan_range.jobs_params); request->__set_metada_table_params(metadata_table_params); @@ -463,6 +478,7 @@ Status MetaScanner::_build_tasks_metadata_request(const TMetaScanRange& meta_sca // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::TASKS); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_tasks_metadata_params(meta_scan_range.tasks_params); request->__set_metada_table_params(metadata_table_params); @@ -483,6 +499,7 @@ Status MetaScanner::_build_partition_values_metadata_request( // create TMetadataTableRequestParams TMetadataTableRequestParams metadata_table_params; metadata_table_params.__set_metadata_type(TMetadataType::PARTITION_VALUES); + _set_session_identity(&metadata_table_params); metadata_table_params.__set_partition_values_metadata_params( meta_scan_range.partition_values_params); diff --git a/be/src/exec/scan/meta_scanner.h b/be/src/exec/scan/meta_scanner.h index 64725c72d98739..2662c997b9629f 100644 --- a/be/src/exec/scan/meta_scanner.h +++ b/be/src/exec/scan/meta_scanner.h @@ -21,6 +21,7 @@ #include #include +#include #include #include "common/factory_creator.h" @@ -49,7 +50,7 @@ class MetaScanner : public Scanner { public: MetaScanner(RuntimeState* state, ScanLocalStateBase* local_state, TupleId tuple_id, const TScanRangeParams& scan_range, int64_t limit, RuntimeProfile* profile, - TUserIdentity user_identity); + TUserIdentity user_identity, std::set current_roles); Status _open_impl(RuntimeState* state) override; Status close(RuntimeState* state) override; @@ -86,6 +87,11 @@ class MetaScanner : public Scanner { bool _meta_eos; TupleId _tuple_id; TUserIdentity _user_identity; + // SU narrowing: the session's active role subset, forwarded on every metadata request so the + // FE narrows its privilege checks to it. Empty = not narrowed. + std::set _current_roles; + // Stamps who is asking (and the narrowing, if any) on a metadata request. + void _set_session_identity(TMetadataTableRequestParams* params) const; const TupleDescriptor* _tuple_desc = nullptr; std::vector _batch_data; const TScanRange& _scan_range; diff --git a/be/src/information_schema/schema_authentication_integrations_scanner.cpp b/be/src/information_schema/schema_authentication_integrations_scanner.cpp index 3f3943326470b9..ce443103d41471 100644 --- a/be/src/information_schema/schema_authentication_integrations_scanner.cpp +++ b/be/src/information_schema/schema_authentication_integrations_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaAuthenticationIntegrationsScanner::_get_authentication_integrations schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } if (_param->common_param->frontend_conjuncts) { schema_table_request_params.__set_frontend_conjuncts( *_param->common_param->frontend_conjuncts); diff --git a/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp b/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp index 8abe2b4e4b3068..b23cbf1e5745a8 100644 --- a/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp +++ b/be/src/information_schema/schema_catalog_meta_cache_stats_scanner.cpp @@ -94,6 +94,9 @@ Status SchemaCatalogMetaCacheStatsScanner::_fetch_from_fe(size_t column_count, schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::CATALOG_META_CACHE_STATS); diff --git a/be/src/information_schema/schema_columns_scanner.cpp b/be/src/information_schema/schema_columns_scanner.cpp index 2952caf8aa608f..d6ea1c9e4c9adb 100644 --- a/be/src/information_schema/schema_columns_scanner.cpp +++ b/be/src/information_schema/schema_columns_scanner.cpp @@ -85,6 +85,9 @@ Status SchemaColumnsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -339,6 +342,9 @@ Status SchemaColumnsScanner::_get_new_desc() { if (nullptr != _param->common_param->current_user_ident) { desc_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + desc_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { desc_params.__set_user(*(_param->common_param->user)); @@ -374,6 +380,9 @@ Status SchemaColumnsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_extensions_scanner.cpp b/be/src/information_schema/schema_extensions_scanner.cpp index a31d35ed7a7443..504d402f66a4ed 100644 --- a/be/src/information_schema/schema_extensions_scanner.cpp +++ b/be/src/information_schema/schema_extensions_scanner.cpp @@ -65,6 +65,9 @@ Status SchemaExtensionsScanner::_get_extensions_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::EXTENSIONS); diff --git a/be/src/information_schema/schema_files_scanner.cpp b/be/src/information_schema/schema_files_scanner.cpp index 2171c121fa5a6d..12d879b6040627 100644 --- a/be/src/information_schema/schema_files_scanner.cpp +++ b/be/src/information_schema/schema_files_scanner.cpp @@ -93,6 +93,9 @@ Status SchemaFilesScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_metadata_name_ids_scanner.cpp b/be/src/information_schema/schema_metadata_name_ids_scanner.cpp index 790f34920cb0fa..7f0538e34da418 100644 --- a/be/src/information_schema/schema_metadata_name_ids_scanner.cpp +++ b/be/src/information_schema/schema_metadata_name_ids_scanner.cpp @@ -66,6 +66,9 @@ Status SchemaMetadataNameIdsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -105,6 +108,9 @@ Status SchemaMetadataNameIdsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_per_db_scanner.cpp b/be/src/information_schema/schema_per_db_scanner.cpp index 2f4f3f11a2731d..7d858070a66c70 100644 --- a/be/src/information_schema/schema_per_db_scanner.cpp +++ b/be/src/information_schema/schema_per_db_scanner.cpp @@ -52,6 +52,9 @@ Status SchemaPerDbScanner::start(RuntimeState* state) { } if (_param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } // The planner lifts an exact `TABLE_SCHEMA = '...'` out of the query for us. Without // it every visible database is listed and asked for its rows, and everything but one @@ -82,6 +85,9 @@ Status SchemaPerDbScanner::get_onedb_info_from_fe(int64_t db_id) { schema_table_request_params.columns_name.emplace_back(column.name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_catalog(*_param->common_param->catalog); schema_table_request_params.__set_dbId(db_id); // Same reason as the database pattern above: an exact `TABLE_NAME = '...'` lets the FE diff --git a/be/src/information_schema/schema_processlist_scanner.cpp b/be/src/information_schema/schema_processlist_scanner.cpp index 1795b4aeabbad7..e504623a464737 100644 --- a/be/src/information_schema/schema_processlist_scanner.cpp +++ b/be/src/information_schema/schema_processlist_scanner.cpp @@ -72,6 +72,9 @@ Status SchemaProcessListScanner::start(RuntimeState* state) { request.__set_show_full_sql(true); request.__set_time_zone(state->timezone()); request.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + request.__set_current_roles(_param->common_param->current_roles); + } for (const auto& fe_addr : _param->common_param->fe_addr_list) { TShowProcessListResult tmp_ret; diff --git a/be/src/information_schema/schema_profiling_scanner.cpp b/be/src/information_schema/schema_profiling_scanner.cpp index 145f9fa4a84d3d..4a5415f25f4738 100644 --- a/be/src/information_schema/schema_profiling_scanner.cpp +++ b/be/src/information_schema/schema_profiling_scanner.cpp @@ -71,6 +71,9 @@ Status SchemaProfilingScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_role_mappings_scanner.cpp b/be/src/information_schema/schema_role_mappings_scanner.cpp index f0c08f23230c16..aefb4001ba9322 100644 --- a/be/src/information_schema/schema_role_mappings_scanner.cpp +++ b/be/src/information_schema/schema_role_mappings_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaRoleMappingsScanner::_get_role_mappings_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } if (_param->common_param->frontend_conjuncts) { schema_table_request_params.__set_frontend_conjuncts( *_param->common_param->frontend_conjuncts); diff --git a/be/src/information_schema/schema_scanner.h b/be/src/information_schema/schema_scanner.h index 886c7269ea542d..d634a2141331c0 100644 --- a/be/src/information_schema/schema_scanner.h +++ b/be/src/information_schema/schema_scanner.h @@ -73,6 +73,9 @@ struct SchemaScannerCommonParam { const std::string* catalog = nullptr; std::set fe_addr_list; std::set required_columns; + // SU narrowing: the session's active role subset (TSchemaScanNode.current_roles), forwarded + // on the BE->FE metadata RPCs so they narrow name visibility. Empty = not narrowed. + std::set current_roles; }; // scanner parameter from frontend diff --git a/be/src/information_schema/schema_schema_privileges_scanner.cpp b/be/src/information_schema/schema_schema_privileges_scanner.cpp index 453e135c298a79..17a130e282f3ac 100644 --- a/be/src/information_schema/schema_schema_privileges_scanner.cpp +++ b/be/src/information_schema/schema_schema_privileges_scanner.cpp @@ -61,6 +61,9 @@ Status SchemaSchemaPrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_schemata_scanner.cpp b/be/src/information_schema/schema_schemata_scanner.cpp index e9d25684780e92..0bf0ebf3db371a 100644 --- a/be/src/information_schema/schema_schemata_scanner.cpp +++ b/be/src/information_schema/schema_schemata_scanner.cpp @@ -60,6 +60,9 @@ Status SchemaSchemataScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp b/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp index f52d5399bc4df7..0203df685eefee 100644 --- a/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp +++ b/be/src/information_schema/schema_sql_block_rule_status_scanner.cpp @@ -72,6 +72,9 @@ Status SchemaSqlBlockRuleStatusScanner::_get_sql_block_rule_status_block_from_fe TSchemaTableRequestParams schema_table_request_params; schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_frontend_conjuncts(*_param->common_param->frontend_conjuncts); TFetchSchemaTableDataRequest request; diff --git a/be/src/information_schema/schema_table_privileges_scanner.cpp b/be/src/information_schema/schema_table_privileges_scanner.cpp index c9b70c905b437c..79e9fbe313dc0a 100644 --- a/be/src/information_schema/schema_table_privileges_scanner.cpp +++ b/be/src/information_schema/schema_table_privileges_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaTablePrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_tables_scanner.cpp b/be/src/information_schema/schema_tables_scanner.cpp index ef94909fd734f0..2d68568f012a14 100644 --- a/be/src/information_schema/schema_tables_scanner.cpp +++ b/be/src/information_schema/schema_tables_scanner.cpp @@ -82,6 +82,9 @@ Status SchemaTablesScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -117,6 +120,9 @@ Status SchemaTablesScanner::_get_new_table() { table_params.__set_required_columns(_param->common_param->required_columns); if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_user_privileges_scanner.cpp b/be/src/information_schema/schema_user_privileges_scanner.cpp index 9cfe5fe6fa00db..471f570547dfd6 100644 --- a/be/src/information_schema/schema_user_privileges_scanner.cpp +++ b/be/src/information_schema/schema_user_privileges_scanner.cpp @@ -60,6 +60,9 @@ Status SchemaUserPrivilegesScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_user_scanner.cpp b/be/src/information_schema/schema_user_scanner.cpp index 260ee8a814fee4..661fbcde383bcb 100644 --- a/be/src/information_schema/schema_user_scanner.cpp +++ b/be/src/information_schema/schema_user_scanner.cpp @@ -73,6 +73,9 @@ Status SchemaUserScanner::start(RuntimeState* state) { if (nullptr != _param->common_param->current_user_ident) { request.__set_current_user_ident(*_param->common_param->current_user_ident); } + if (!_param->common_param->current_roles.empty()) { + request.__set_current_roles(_param->common_param->current_roles); + } RETURN_IF_ERROR(SchemaHelper::show_user(*(_param->common_param->ip), _param->common_param->port, request, &_user_result)); diff --git a/be/src/information_schema/schema_view_dependency_scanner.cpp b/be/src/information_schema/schema_view_dependency_scanner.cpp index a3e6842f736fb1..d9cf1bd9033063 100644 --- a/be/src/information_schema/schema_view_dependency_scanner.cpp +++ b/be/src/information_schema/schema_view_dependency_scanner.cpp @@ -63,6 +63,9 @@ Status SchemaViewDependencyScanner::_get_view_dependency_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_view_dependency_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } schema_table_request_params.__set_frontend_conjuncts(*_param->common_param->frontend_conjuncts); TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::VIEW_DEPENDENCY); diff --git a/be/src/information_schema/schema_views_scanner.cpp b/be/src/information_schema/schema_views_scanner.cpp index 8d99f2b24fa71b..fa30e30f7223ee 100644 --- a/be/src/information_schema/schema_views_scanner.cpp +++ b/be/src/information_schema/schema_views_scanner.cpp @@ -64,6 +64,9 @@ Status SchemaViewsScanner::start(RuntimeState* state) { } if (nullptr != _param->common_param->current_user_ident) { db_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + db_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { db_params.__set_user(*(_param->common_param->user)); @@ -91,6 +94,9 @@ Status SchemaViewsScanner::_get_new_table() { } if (nullptr != _param->common_param->current_user_ident) { table_params.__set_current_user_ident(*(_param->common_param->current_user_ident)); + if (!_param->common_param->current_roles.empty()) { + table_params.__set_current_roles(_param->common_param->current_roles); + } } else { if (nullptr != _param->common_param->user) { table_params.__set_user(*(_param->common_param->user)); diff --git a/be/src/information_schema/schema_workload_group_privileges.cpp b/be/src/information_schema/schema_workload_group_privileges.cpp index 458d89239b6d06..b831382c13430a 100644 --- a/be/src/information_schema/schema_workload_group_privileges.cpp +++ b/be/src/information_schema/schema_workload_group_privileges.cpp @@ -55,6 +55,9 @@ Status SchemaWorkloadGroupPrivilegesScanner::_get_workload_group_privs_block_fro schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_GROUP_PRIVILEGES); diff --git a/be/src/information_schema/schema_workload_groups_scanner.cpp b/be/src/information_schema/schema_workload_groups_scanner.cpp index 527696de3cd759..e8c005279fad45 100644 --- a/be/src/information_schema/schema_workload_groups_scanner.cpp +++ b/be/src/information_schema/schema_workload_groups_scanner.cpp @@ -68,6 +68,9 @@ Status SchemaWorkloadGroupsScanner::_get_workload_groups_block_from_fe() { schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_GROUPS); diff --git a/be/src/information_schema/schema_workload_sched_policy_scanner.cpp b/be/src/information_schema/schema_workload_sched_policy_scanner.cpp index 12a73f8ec1928d..cfd475e4b46686 100644 --- a/be/src/information_schema/schema_workload_sched_policy_scanner.cpp +++ b/be/src/information_schema/schema_workload_sched_policy_scanner.cpp @@ -59,6 +59,9 @@ Status SchemaWorkloadSchedulePolicyScanner::_get_workload_schedule_policy_block_ schema_table_request_params.columns_name.emplace_back(_s_tbls_columns[i].name); } schema_table_request_params.__set_current_user_ident(*_param->common_param->current_user_ident); + if (!_param->common_param->current_roles.empty()) { + schema_table_request_params.__set_current_roles(_param->common_param->current_roles); + } TFetchSchemaTableDataRequest request; request.__set_schema_table_name(TSchemaTableName::WORKLOAD_SCHEDULE_POLICY); diff --git a/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java b/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java index f6e776b5b7048d..460c5645fe45eb 100644 --- a/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java +++ b/fe/fe-authorization/fe-authorization-api/src/main/java/org/apache/doris/authorization/AccessAction.java @@ -54,5 +54,7 @@ public enum AccessAction { /** Using a stage. Distinct from {@link #USAGE}: it is a privilege of its own. */ STAGE_USAGE, /** Reading a view's definition. */ - SHOW_VIEW + SHOW_VIEW, + /** Switching the session to another user (SU). Only ever granted globally. */ + PROXY } diff --git a/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java b/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java index 711375303bc2e3..78463773c0b1bd 100644 --- a/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java +++ b/fe/fe-authorization/fe-authorization-plugins/fe-authorization-plugin-ranger-doris/src/main/java/org/apache/doris/catalog/authorizer/ranger/doris/DorisAccessType.java @@ -63,6 +63,11 @@ public static DorisAccessType of(AccessAction action) { return USAGE; case SHOW_VIEW: return SHOW_VIEW; + case PROXY: + // The Doris Ranger service has no access type for switching the session to another user; the + // closest question it can answer is administration, which already implies PROXY_PRIV in the + // engine, so a Ranger-authorized cluster grants SU to its administrators and to nobody else. + return ADMIN; default: // Guessing would ask Ranger about an access type its service definition does not have, and // every such request is denied - an action added to Doris would silently stop being grantable. diff --git a/fe/fe-authorization/fe-authorization-spi/README.md b/fe/fe-authorization/fe-authorization-spi/README.md index 776909b88284da..ac5a61cd9adcb4 100644 --- a/fe/fe-authorization/fe-authorization-spi/README.md +++ b/fe/fe-authorization/fe-authorization-spi/README.md @@ -164,7 +164,7 @@ Every plugin jar must declare, in its MANIFEST, the authorization plugin API it - 1.0 + 2.0 diff --git a/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java b/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java index 186d58331dbb90..a6adc92c4623f6 100644 --- a/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java +++ b/fe/fe-authorization/fe-authorization-spi/src/test/java/org/apache/doris/authorization/spi/AuthorizationPluginSurfaceTest.java @@ -136,8 +136,9 @@ public void authorizationApiMajorTracksTheRecordedSurfaceChange() throws IOExcep Assertions.assertNotNull(in, "missing authorization plugin API version resource"); version.load(in); } - // First published surface of this family. - Assertions.assertEquals("1.0", version.getProperty("api.version")); + // 1.0 was the first published surface of this family; 2.0 added AccessAction.PROXY (the privilege + // behind SU), a constant a plugin built against 1.x has never seen. + Assertions.assertEquals("2.0", version.getProperty("api.version")); } @Test diff --git a/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt b/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt index c43ceadcfdc461..a1de6432a34263 100644 --- a/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt +++ b/fe/fe-authorization/fe-authorization-spi/src/test/resources/authorization-plugin-surface.txt @@ -7,6 +7,7 @@ org.apache.doris.authorization.AccessAction@DROP org.apache.doris.authorization.AccessAction@GRANT org.apache.doris.authorization.AccessAction@LOAD org.apache.doris.authorization.AccessAction@NODE +org.apache.doris.authorization.AccessAction@PROXY org.apache.doris.authorization.AccessAction@SELECT org.apache.doris.authorization.AccessAction@SHOW_VIEW org.apache.doris.authorization.AccessAction@STAGE_USAGE diff --git a/fe/fe-authorization/pom.xml b/fe/fe-authorization/pom.xml index 425e08677031c3..9248c6c6aea132 100644 --- a/fe/fe-authorization/pom.xml +++ b/fe/fe-authorization/pom.xml @@ -43,7 +43,7 @@ under the License. branch into a refusal, which is a behaviour change for a plugin nobody rebuilt. A change to fe-extension-spi means bumping all five families. --> - 1.0 + 2.0 diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java index dba702de95debb..6dfd3c06c1a778 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/AccessPrivilege.java @@ -39,7 +39,8 @@ public enum AccessPrivilege { DROP_PRIV(10, "Privilege for dropping database or table"), ADMIN_PRIV(11, "All privileges except NODE_PRIV"), USAGE_PRIV(12, "Privilege for use resource"), - SHOW_VIEW_PRIV(13, "Privilege for show view"); + SHOW_VIEW_PRIV(13, "Privilege for show view"), + PROXY_PRIV(14, "Privilege for switching the session to another user"); private int flag; private String desc; @@ -50,7 +51,7 @@ private AccessPrivilege(int flag, String desc) { } public List toDorisPrivilege() { - Preconditions.checkState(flag > 0 && flag < 14); + Preconditions.checkState(flag > 0 && flag < 15); switch (flag) { case 1: case 6: @@ -78,6 +79,8 @@ public List toDorisPrivilege() { return Lists.newArrayList(Privilege.USAGE_PRIV); case 13: return Lists.newArrayList(Privilege.SHOW_VIEW_PRIV); + case 14: + return Lists.newArrayList(Privilege.PROXY_PRIV); default: return null; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java index 2d7aaecd38d4d3..b3be1f4a7328a8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/BuiltinScalarFunctions.java @@ -469,6 +469,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsDiff; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsSub; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha1; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha2; @@ -1086,6 +1087,7 @@ public class BuiltinScalarFunctions implements FunctionHelper { scalar(MilliSecondTimestamp.class, "millisecond_timestamp"), scalar(MicroSecondTimestamp.class, "microsecond_timestamp"), scalar(RandomBytes.class, "random_bytes"), + scalar(SessionIsNarrowed.class, "session_is_narrowed"), scalar(SessionUser.class, "session_user"), scalar(Sha1.class, "sha1", "sha"), scalar(Sha2.class, "sha2"), diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java index 4b040857c46fd2..b56ab4b56fc8fe 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/Env.java @@ -5623,6 +5623,53 @@ public String getMasterHost() { return this.masterInfo.getHost(); } + /** + * Whether the master FE reports (through its heartbeat) the same build as this FE. Gates the + * forwarding of request fields an older master would silently ignore, such as a session's SU + * role narrowing: the master is upgraded last in a rolling upgrade, so a newer follower must not + * hand such a statement to a master that cannot apply the field. An unknown or dead master is + * treated as a different build. + */ + public boolean masterRunsSameBuild() { + if (isMaster()) { + return true; + } + return frontendRunsSameBuild(getMasterHost(), getMasterRpcPort()); + } + + /** + * Whether every alive FE reports the same build as this FE (see {@link #frontendRunsSameBuild}): + * the gate of a session-narrowed (SU) session's requests that fan out to every FE, which an FE + * of an older build would answer against the target's full role union. + */ + public boolean aliveFrontendsRunSameBuild() { + String local = Frontend.localBuildVersion(); + for (Frontend fe : frontends.values()) { + if (fe.isAlive() && !local.equals(fe.getVersion())) { + return false; + } + } + return true; + } + + /** + * Whether the FE at this RPC address is this FE itself or reports the same build as this FE. + * Every FE sees every other FE's build: the master heartbeats all of them and journals the + * responses (HbPackage, FrontendHbResponse.version), which the other FEs replay. An FE this FE + * does not know, or a dead one, counts as a different build. + */ + public boolean frontendRunsSameBuild(String host, int rpcPort) { + if (host.equals(selfNode.getHost()) && rpcPort == Config.rpc_port) { + return true; + } + for (Frontend fe : frontends.values()) { + if (fe.getHost().equals(host) && fe.getRpcPort() == rpcPort) { + return fe.isAlive() && Frontend.localBuildVersion().equals(fe.getVersion()); + } + } + return false; + } + public PolicyMgr getPolicyMgr() { diff --git a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java index 1e429a1b288f26..c3edefa8aa8f65 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java +++ b/fe/fe-core/src/main/java/org/apache/doris/catalog/InternalSchema.java @@ -146,6 +146,8 @@ public class InternalSchema { ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); AUDIT_SCHEMA.add(new ColumnDef("user", ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); + AUDIT_SCHEMA.add(new ColumnDef("authenticated_user", + ScalarType.createVarchar(128), ColumnNullableType.NULLABLE)); AUDIT_SCHEMA.add(new ColumnDef("frontend_ip", ScalarType.createVarchar(1024), ColumnNullableType.NULLABLE)); // default ctl and db diff --git a/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java b/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java index 5c9fb5fb33e995..12f9ef0b62d658 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java +++ b/fe/fe-core/src/main/java/org/apache/doris/common/cache/NereidsSqlCacheManager.java @@ -95,6 +95,7 @@ import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; /** * NereidsSqlCacheManager @@ -326,8 +327,14 @@ private String generateCacheKey(ConnectContext connectContext, String sqlOrMd5) String currentCatalogName = currentCatalog != null ? currentCatalog.getName() : ""; String currentDatabase = connectContext.getDatabase(); String currentDatabaseName = currentDatabase != null ? currentDatabase : ""; - return currentCatalogName + "." + currentDatabaseName + ":" + connectContext.getCurrentUserIdentity().toString() - + ":" + sqlOrMd5; + String key = currentCatalogName + "." + currentDatabaseName + ":" + + connectContext.getCurrentUserIdentity().toString() + ":" + sqlOrMd5; + if (connectContext.isSessionRoleNarrowed()) { + // a session-narrowed (SU) session and the identity's own session see different privilege + // and policy views: their entries are kept apart instead of evicting each other on every hit + key = key + ":su=" + new TreeSet<>(connectContext.getSessionRoleOverride()); + } + return key; } private String normalizeSql(String sql) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java index ce8e53c251e599..bdb5439a71533d 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/datasource/tvf/source/MetadataScanNode.java @@ -37,6 +37,7 @@ import com.google.common.collect.Lists; +import java.util.HashSet; import java.util.List; public class MetadataScanNode extends ExternalScanNode { @@ -65,6 +66,11 @@ protected void toThrift(TPlanNode planNode) { metaScanNode.setMetadataType(this.tvf.getMetadataType()); TUserIdentity tCurrentUser = ConnectContext.get().getCurrentUserIdentity().toThrift(); metaScanNode.setCurrentUserIdent(tCurrentUser); + // SU narrowing: carry the session's active role subset so the BE->FE metadata requests + // narrow their privilege checks to the same set the session sees; null = not narrowed. + if (ConnectContext.get().isSessionRoleNarrowed()) { + metaScanNode.setCurrentRoles(new HashSet<>(ConnectContext.get().getSessionRoleOverride())); + } planNode.setMetaScanNode(metaScanNode); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java b/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java index 985da4f9f36a4f..4561ab2d330416 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java +++ b/fe/fe-core/src/main/java/org/apache/doris/httpv2/rest/TableQueryPlanAction.java @@ -362,6 +362,7 @@ private static void addToAuditLog(ConnectContext ctx, String origStmt, Statement .setTimestamp(ctx.getStartTime()) .setClientIp(ctx.getClientIP()) .setUser(ctx.getQualifiedUser()) + .setAuthenticatedUser(AuditLogHelper.authenticatedUser(ctx)) .setFeIp(FrontendOptions.getLocalHostAddress()) .setCtl(catalog == null ? InternalCatalog.INTERNAL_CATALOG_NAME : catalog.getName()) .setDb(ctx.getDatabase()) diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java index e172d62d97538a..bcfb970fba8840 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/AccessTranslation.java @@ -108,6 +108,7 @@ public final class AccessTranslation { ACTION_OF_PRIVILEGE.put(Privilege.CLUSTER_USAGE_PRIV, AccessAction.CLUSTER_USAGE); ACTION_OF_PRIVILEGE.put(Privilege.STAGE_USAGE_PRIV, AccessAction.STAGE_USAGE); ACTION_OF_PRIVILEGE.put(Privilege.SHOW_VIEW_PRIV, AccessAction.SHOW_VIEW); + ACTION_OF_PRIVILEGE.put(Privilege.PROXY_PRIV, AccessAction.PROXY); // The retired bit indices carry the same meaning as the ones that replaced them, so they translate // to the same action - the same normalization Role.compatibilityAuthIndexChange() applies to stored // grants. @@ -128,6 +129,7 @@ public final class AccessTranslation { PRIVILEGE_OF_ACTION.put(AccessAction.CLUSTER_USAGE, Privilege.CLUSTER_USAGE_PRIV); PRIVILEGE_OF_ACTION.put(AccessAction.STAGE_USAGE, Privilege.STAGE_USAGE_PRIV); PRIVILEGE_OF_ACTION.put(AccessAction.SHOW_VIEW, Privilege.SHOW_VIEW_PRIV); + PRIVILEGE_OF_ACTION.put(AccessAction.PROXY, Privilege.PROXY_PRIV); // Sorted by name so that two constants naming the same privileges with the same match - today // SHOW_RESOURCES and SHOW_WORKLOAD_GROUP - always resolve to the same one of the pair. They ask the diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java index c04dc12afec942..b385e1301382b2 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Auth.java @@ -29,9 +29,11 @@ import org.apache.doris.catalog.DatabaseIf; import org.apache.doris.catalog.Env; import org.apache.doris.catalog.InfoSchemaDb; +import org.apache.doris.catalog.MysqlDb; import org.apache.doris.catalog.TableIf; import org.apache.doris.cloud.datasource.CloudInternalCatalog; import org.apache.doris.cloud.proto.Cloud; +import org.apache.doris.cloud.system.CloudSystemInfoService; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.AuthenticationException; import org.apache.doris.common.AuthorizationException; @@ -83,14 +85,15 @@ import java.io.DataOutput; import java.io.IOException; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; import java.util.Set; import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Predicate; import java.util.stream.Collectors; @@ -220,15 +223,6 @@ public boolean checkPlainPasswordForTest(String remoteUser, String remoteHost, S } } - public Set getRolesByUser(UserIdentity user, boolean showUserDefaultRole) { - readLock(); - try { - return userRoleManager.getRolesByUser(user, showUserDefaultRole); - } finally { - readUnlock(); - } - } - public boolean requiresCertificateAuth(UserIdentity userIdentity) { return userIdentity != null && userIdentity.hasSanRequirement(); } @@ -296,7 +290,80 @@ public void checkPasswordForUserIdentity(UserIdentity userIdentity, byte[] remot } } + // SU narrowing: FE-side SU narrowing for BE->FE metadata RPC handler threads, which have NO + // ConnectContext. The handler installs the calling session's narrowed role NAMES here (keyed by + // the target identity) around its privilege filter; getRolesByUserWithLdap then narrows exactly + // as a live SU session would. The handler MUST clear it in a finally (thrift threads are pooled). + private static final ThreadLocal>> RPC_SESSION_NARROWING = + new ThreadLocal<>(); + + public static void setRpcSessionNarrowing(UserIdentity user, Set roleNames) { + Map> m = Maps.newHashMap(); + m.put(user, roleNames); + RPC_SESSION_NARROWING.set(m); + } + + public static void clearRpcSessionNarrowing() { + RPC_SESSION_NARROWING.remove(); + } + + /** + * The RPC narrowing installed on this thread, or null when there is none. A handler that hands + * part of its work to another thread captures it here and installs it there with + * {@link #installRpcSessionNarrowing}: a ThreadLocal does not follow the work, and a privilege + * check on the other thread would otherwise resolve the target's full role union. + */ + public static Map> captureRpcSessionNarrowing() { + return RPC_SESSION_NARROWING.get(); + } + + /** Installs a captured narrowing on this thread (none when it is null); clear it in a finally. */ + public static void installRpcSessionNarrowing(Map> narrowing) { + if (narrowing == null) { + RPC_SESSION_NARROWING.remove(); + } else { + RPC_SESSION_NARROWING.set(narrowing); + } + } + + // The SU-narrowed role set of a target identity: the override roles the target STILL holds + the + // implicit information_schema/mysql read baseline (SU_NARROWING_BASELINE). The ceiling is + // re-evaluated on every resolution, exactly as a normal session re-reads its grants on every + // check: a role revoked from the target after the switch (or dropped and re-created under the + // same name, or every role after DROP USER) drops out of the open narrowed session at once. + // Shared by the live-session, forwarded and RPC-handler paths. + private Set narrowedRoleSet(UserIdentity target, Set overrideRoleNames) { + Set stillGranted = getGrantedRoleNames(target); + Set narrowed = Sets.newHashSet(); + for (String roleName : overrideRoleNames) { + if (!stillGranted.contains(roleName)) { + continue; + } + Role role = roleManager.getRole(roleName); + if (role != null) { + narrowed.add(role); + } + } + narrowed.add(SU_NARROWING_BASELINE); + return narrowed; + } + public Set getRolesByUserWithLdap(UserIdentity userIdentity) { + Map> rpcNarrow = RPC_SESSION_NARROWING.get(); + if (rpcNarrow != null) { + Set rpcOverride = rpcNarrow.get(userIdentity); + if (rpcOverride != null) { + // SU narrowing: a BE->FE metadata RPC carried this session's narrowed role subset. + return narrowedRoleSet(userIdentity, rpcOverride); + } + } + ConnectContext ctx = ConnectContext.get(); + if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity()) && ctx.isSessionRoleNarrowed()) { + // SU-narrowed session: the override REPLACES every role source (user grants, LDAP, + // authenticated roles), so none of them is resolved first. Return the override roles the + // target still holds + the information_schema/mysql read baseline (see narrowedRoleSet). + return narrowedRoleSet(userIdentity, ctx.getSessionRoleOverride()); + } Set roles = Sets.newHashSet(); Set roleNames = userRoleManager.getRolesByUser(userIdentity); for (String roleName : roleNames) { @@ -311,7 +378,6 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { roles.addAll(ldapRoles); } } - ConnectContext ctx = ConnectContext.get(); if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity())) { for (String roleName : ctx.getAuthenticatedRoles()) { Role role = roleManager.getRole(roleName); @@ -323,11 +389,184 @@ public Set getRolesByUserWithLdap(UserIdentity userIdentity) { return roles; } + /** + * The global privilege bits the named roles confer, union-ed; a name that is not a role + * contributes nothing. Used by the SU escalation check: a switch may not confer an + * administrative bit the switching account does not already hold. + */ + public PrivBitSet getGlobalPrivsOfRoles(Collection roleNames) { + readLock(); + try { + PrivBitSet conferred = PrivBitSet.of(); + for (String roleName : roleNames) { + Role role = roleManager.getRole(roleName); + if (role != null) { + role.getGlobalPrivTable().getPrivs(conferred); + } + } + return conferred; + } finally { + readUnlock(); + } + } + + /** + * The role names granted to an identity (user grants + LDAP), ignoring any SU narrowing of the + * current session. Used for the SU ceiling check and for existence probing. + */ + public Set getGrantedRoleNames(UserIdentity userIdentity) { + readLock(); + try { + Set names = Sets.newHashSet(userRoleManager.getRolesByUser(userIdentity)); + if (isLdapAuthEnabled()) { + Set ldapRoles = ldapManager.getUserRoles(userIdentity.getQualifiedUser()); + if (!CollectionUtils.isEmpty(ldapRoles)) { + for (Role r : ldapRoles) { + names.add(r.getRoleName()); + } + } + } + return names; + } finally { + readUnlock(); + } + } + + // The implicit information_schema/mysql read grants that RoleManager.createDefaultRole gives + // every user's default role, isolated into a synthetic role so an SU-narrowed session keeps + // them (self-filtered by the session's real privileges) without re-including the default role's + // privilege-bearing direct grants. Its name carries the default-role prefix, so it can be neither + // CREATEd nor bound by a row policy (both validate the role), and it is hidden from every role + // listing (getRoleNamesByUserWithLdap). Built once, at class load, from constant inputs: a + // failure here is a build defect, and the FE must not start without the role. + public static final String SU_NARROWING_BASELINE_ROLE = RoleManager.DEFAULT_ROLE_PREFIX + "su_narrowing_baseline"; + private static final Role SU_NARROWING_BASELINE = buildNarrowingBaselineRole(); + + private static Role buildNarrowingBaselineRole() { + try { + List tablePatterns = Lists.newArrayList(); + TablePattern info = new TablePattern(DEFAULT_CATALOG, InfoSchemaDb.DATABASE_NAME, "*"); + info.analyze(); + tablePatterns.add(info); + TablePattern mysql = new TablePattern(DEFAULT_CATALOG, MysqlDb.DATABASE_NAME, "*"); + mysql.analyze(); + tablePatterns.add(mysql); + WorkloadGroupPattern wg = new WorkloadGroupPattern(WorkloadGroupMgr.DEFAULT_GROUP_NAME); + wg.analyze(); + return new Role(SU_NARROWING_BASELINE_ROLE, tablePatterns, + PrivBitSet.of(Privilege.SELECT_PRIV), wg, PrivBitSet.of(Privilege.USAGE_PRIV)); + } catch (AnalysisException e) { + throw new IllegalStateException("failed to build the SU narrowing baseline role", e); + } + } + + /** + * The SU narrowing in force for this identity on this thread -- the live session's override, + * or the subset a BE->FE metadata RPC carried -- or null when it is not narrowed. The two + * legs of every placement answer (this FE's SHOW output, the information_schema scan through + * the BE) read the same narrowing, so they agree. + */ + private static Set narrowingOf(UserIdentity userIdentity) { + Map> rpcNarrow = RPC_SESSION_NARROWING.get(); + if (rpcNarrow != null && rpcNarrow.containsKey(userIdentity)) { + return rpcNarrow.get(userIdentity); + } + ConnectContext ctx = ConnectContext.get(); + if (ctx != null && userIdentity.equals(ctx.getCurrentUserIdentity()) && ctx.isSessionRoleNarrowed()) { + return ctx.getSessionRoleOverride(); + } + return null; + } + + /** + * PLACEMENT widening: in an SU-narrowed session, the effective identity's own granted roles + * that the override left out (its default role, where direct grants live, plus explicit and + * LDAP roles). Placement is not data authority: the narrowed session runs in the person's + * workload group and compute group, and the person's USAGE on them is typically a direct grant + * that narrowing would otherwise drop, so the person's lane follows the person. Only the USAGE + * bit of these roles' workload-group and compute-group grants reaches through + * ({@link #placementUsageThroughOwnRoles}): never their global privileges, never GRANT_PRIV. + * GRANT/REVOKE ... ON WORKLOAD GROUP / CLUSTER, the listings of grants (SHOW GRANTS, + * workload_group_privileges) and every data check stay on the narrowed set. Empty for a + * session that is not narrowed. + */ + private Set placementOnlyRoles(UserIdentity userIdentity) { + Set override = narrowingOf(userIdentity); + if (override == null) { + return Collections.emptySet(); + } + Set ownRoles = Sets.newHashSet(); + for (String roleName : getGrantedRoleNames(userIdentity)) { + if (override.contains(roleName)) { + continue; + } + Role role = roleManager.getRole(roleName); + if (role != null) { + ownRoles.add(role); + } + } + return ownRoles; + } + + /** + * The one question the placement widening answers: "may this session run here?". Consulted + * after the narrowed set said no, and only when the predicate is satisfiable by USAGE alone, + * so an administrative predicate (the GRANT gate's USAGE+GRANT) never reaches it; each own + * role contributes its workload-group / compute-group grant on the named group and nothing + * else. + */ + private boolean placementUsageThroughOwnRoles(UserIdentity currentUser, PrivPredicate wanted, + Predicate grantsUsage) { + if (!Privilege.satisfy(PrivBitSet.of(Privilege.USAGE_PRIV), wanted)) { + return false; + } + for (Role role : placementOnlyRoles(currentUser)) { + if (grantsUsage.test(role)) { + return true; + } + } + return false; + } + + private static boolean grantsWorkloadGroupUsage(Role role, String workloadGroupName) { + PrivBitSet privs = PrivBitSet.of(); + role.getWorkloadGroupPrivTable().getPrivs(workloadGroupName, privs); + return privs.containsPrivs(Privilege.USAGE_PRIV); + } + + private static boolean grantsComputeGroupUsage(Role role, String computeGroupName) { + PrivBitSet privs = PrivBitSet.of(); + role.getCloudClusterPrivTable().getPrivs(computeGroupName, privs); + // a grant on the owning virtual compute group covers its members (Role.checkCloudPriv) + String virtualComputeGroup = ((CloudSystemInfoService) Env.getCurrentSystemInfo()) + .ownedByVirtualComputeGroup(computeGroupName); + if (virtualComputeGroup != null) { + role.getCloudClusterPrivTable().getPrivs(virtualComputeGroup, privs); + } + // GRANT USAGE_PRIV ON CLUSTER is stored as CLUSTER_USAGE_PRIV (PrivBitSet.convertResourcePrivToCloudPriv) + return privs.containsPrivs(Privilege.USAGE_PRIV, Privilege.CLUSTER_USAGE_PRIV); + } + + /** + * The role names an identity effectively holds right now, as every built-in privilege check + * resolves them: an SU narrowing of the current session (or of the RPC thread) replaces the + * union, LDAP roles are included, the per-user default role and the synthetic SU baseline are + * not. This is what an authorization source is told when it asks the engine who holds which + * roles, so a policy written against Doris roles sees the same set the engine enforces. + */ + public Set getEffectiveRoleNames(UserIdentity user) { + return getRoleNamesByUserWithLdap(user, false); + } + public Set getRoleNamesByUserWithLdap(UserIdentity user, boolean showUserDefaultRole) { Set rolesByUserWithLdap = getRolesByUserWithLdap(user); Set res = Sets.newHashSetWithExpectedSize(rolesByUserWithLdap.size()); for (Role role : rolesByUserWithLdap) { String roleName = role.getRoleName(); + if (roleName.equals(SU_NARROWING_BASELINE_ROLE)) { + // the synthetic SU baseline is an implementation detail, never a role anybody granted + continue; + } if (showUserDefaultRole || !roleName.startsWith(RoleManager.DEFAULT_ROLE_PREFIX)) { res.add(roleName); } @@ -614,7 +853,9 @@ protected boolean checkWorkloadGroupPriv(UserIdentity currentUser, String worklo return true; } } - return false; + // placement: USAGE through the person's own roles in an SU-narrowed session + return placementUsageThroughOwnRoles(currentUser, wanted, + role -> grantsWorkloadGroupUsage(role, workloadGroupName)); } finally { readUnlock(); } @@ -638,12 +879,21 @@ protected boolean checkCloudPriv(UserIdentity currentUser, String cloudName, return true; } } - return false; + // compute-group USAGE is placement (see placementOnlyRoles); stage and storage-vault + // usage are data access and stay on the narrowed set + return type == ResourceTypeEnum.CLUSTER + && placementUsageThroughOwnRoles(currentUser, wanted, + role -> grantsComputeGroupUsage(role, cloudName)); } finally { readUnlock(); } } + /** Whether LDAP authentication is on and names this user: the existence probe of an LDAP-only account. */ + public boolean ldapUserExists(String qualifiedUser) { + return isLdapAuthEnabled() && ldapManager.doesUserExist(qualifiedUser); + } + // Check if LDAP authentication is enabled. private boolean isLdapAuthEnabled() { return AuthenticateType.getAuthTypeConfig() == AuthenticateType.LDAP; @@ -1605,8 +1855,10 @@ private void getUserAuthInfo(List> userAuthInfos, UserIdentity user userAuthInfo.add(requireSan); userAuthInfo.add(Strings.nullToEmpty(user.getAuthIntegration())); // ============== Roles ============== - userAuthInfo.add(Joiner.on(",").join(userRoleManager - .getRolesByUser(userIdent, ConnectContext.get().getSessionVariable().showUserDefaultRole))); + // the same resolution as every privilege column below: an SU-narrowed session lists + // its narrowed set, not the identity's full role union + userAuthInfo.add(Joiner.on(",").join(getRoleNamesByUserWithLdap(userIdent, + ConnectContext.get().getSessionVariable().showUserDefaultRole))); } } // ==============GlobalPrivs============== @@ -1780,8 +2032,9 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit Set currentUserRole = null; if (!isCurrentUserAdmin) { - currentUserRole = userRoleManager.getRolesByUser(currentUserIdentity, false); - currentUserRole = currentUserRole == null ? new HashSet<>() : currentUserRole; + // a listing of GRANTS: the narrowed set in an SU-narrowed session (the lanes the + // session may run in are answered by the USAGE check, not listed here) + currentUserRole = getRoleNamesByUserWithLdap(currentUserIdentity, false); } roleManager.getRoleWorkloadGroupPrivs(result, currentUserRole); } finally { @@ -1791,27 +2044,27 @@ public void getUserRoleWorkloadGroupPrivs(List> result, UserIdentit private ResourcePrivTable getUserCloudClusterPrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getCloudClusterPrivTable()); + // a listing of GRANTS: the narrowed set, like every other SHOW GRANTS column + for (Role role : getRolesByUserWithLdap(userIdentity)) { + table.merge(role.getCloudClusterPrivTable()); } return table; } private ResourcePrivTable getUserCloudStagePrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getCloudStagePrivTable()); + // stage usage is data access: the narrowed set, like checkCloudPriv + for (Role role : getRolesByUserWithLdap(userIdentity)) { + table.merge(role.getCloudStagePrivTable()); } return table; } private ResourcePrivTable getUserStorageVaultPrivTable(UserIdentity userIdentity) { ResourcePrivTable table = new ResourcePrivTable(); - Set roles = userRoleManager.getRolesByUser(userIdentity); - for (String roleName : roles) { - table.merge(roleManager.getRole(roleName).getStorageVaultPrivTable()); + // storage-vault usage is data access: the narrowed set, like checkCloudPriv + for (Role role : getRolesByUserWithLdap(userIdentity)) { + table.merge(role.getStorageVaultPrivTable()); } return table; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java index 5a1b63d1f6a164..2df7b31d2841dc 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/EngineAuthorizationContext.java @@ -70,11 +70,13 @@ public void servedBy(AuthorizationPlugin source) { * *

Without the per-user default role: it is an artefact of how the built-in model stores a user's own * grants, not a role anybody names in a policy, and a source that saw it would be matching on a name the - * administrator never wrote. + * administrator never wrote. Resolved the way the built-in checks resolve roles, so a session-narrowed + * (SU) session's override and the narrowing a BE->FE RPC carries reach a source's role-bound policies + * exactly as they reach the engine's own; a source is never told roles the session cannot exercise. */ @Override public Set rolesOf(AuthorizedSubject subject) { - return auth.getRolesByUser(AccessTranslation.userIdentityOf(subject), false); + return auth.getEffectiveRoleNames(AccessTranslation.userIdentityOf(subject)); } @Override diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java index 2ba443a1b31bad..b1f9dc3c6828c9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/PrivPredicate.java @@ -53,6 +53,9 @@ public class PrivPredicate { // admin user privs public static final PrivPredicate ADMIN = PrivPredicate.of(PrivBitSet.of(Privilege.ADMIN_PRIV), Operator.OR); + // switch the session to another user (SU): PROXY_PRIV, which ADMIN_PRIV implies + public static final PrivPredicate PROXY = PrivPredicate.of(PrivBitSet.of(Privilege.ADMIN_PRIV, + Privilege.PROXY_PRIV), Operator.OR); public static final PrivPredicate ADMIN_OR_NODE = PrivPredicate.of( PrivBitSet.of(Privilege.ADMIN_PRIV, Privilege.NODE_PRIV), diff --git a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java index 412430749bdd2d..c530a47f8949ee 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java +++ b/fe/fe-core/src/main/java/org/apache/doris/mysql/privilege/Privilege.java @@ -48,7 +48,8 @@ public enum Privilege { CLUSTER_USAGE_PRIV("Cluster_usage_priv", 12, "Privilege for using cluster", "RESOURCE"), // 13 placeholder for stage STAGE_USAGE_PRIV("Stage_usage_priv", 13, "Privilege for using stage", "RESOURCE"), - SHOW_VIEW_PRIV("Show_view_priv", 14, "Privilege for show create view", "GLOBAL,CATALOG,DATABASE,TABLE"); + SHOW_VIEW_PRIV("Show_view_priv", 14, "Privilege for show create view", "GLOBAL,CATALOG,DATABASE,TABLE"), + PROXY_PRIV("Proxy_priv", 15, "Privilege for switching the session to another user (SU)", "GLOBAL"); public static final Map privileges; @@ -73,6 +74,7 @@ public enum Privilege { privileges.put(12, CLUSTER_USAGE_PRIV); privileges.put(13, STAGE_USAGE_PRIV); privileges.put(14, SHOW_VIEW_PRIV); + privileges.put(15, PROXY_PRIV); } @@ -86,6 +88,7 @@ public enum Privilege { CREATE_PRIV, DROP_PRIV, SHOW_VIEW_PRIV, + PROXY_PRIV, }; // only GRANT_PRIV and USAGE_PRIV can grant on workloadGroup @@ -98,6 +101,7 @@ public enum Privilege { CREATE_PRIV, DROP_PRIV, SHOW_VIEW_PRIV, + PROXY_PRIV, }; public static final Privilege[] notBelongToTablePrivileges = { @@ -116,6 +120,7 @@ public enum Privilege { .put(USAGE_PRIV, "USAGE") .put(CLUSTER_USAGE_PRIV, "USAGE") .put(SHOW_VIEW_PRIV, "SHOW VIEW") + .put(PROXY_PRIV, "PROXY") .build(); private String name; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java index 1a3624a3527d77..f1080792b95632 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/parser/LogicalPlanBuilder.java @@ -907,6 +907,7 @@ import org.apache.doris.nereids.trees.plans.commands.ShowWhiteListCommand; import org.apache.doris.nereids.trees.plans.commands.ShowWorkloadGroupsCommand; import org.apache.doris.nereids.trees.plans.commands.StartTransactionCommand; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; import org.apache.doris.nereids.trees.plans.commands.SyncCommand; import org.apache.doris.nereids.trees.plans.commands.TransactionBeginCommand; import org.apache.doris.nereids.trees.plans.commands.TransactionCommitCommand; @@ -9276,6 +9277,16 @@ public LogicalPlan visitGrantRole(DorisParser.GrantRoleContext ctx) { return new GrantRoleCommand(userIdentity, roles); } + @Override + public LogicalPlan visitSuUser(DorisParser.SuUserContext ctx) { + UserIdentity userIdentity = visitUserIdentify(ctx.userIdentify()); + List roles = ctx.roles.stream() + .map(this::visitIdentifierOrText) + .collect(ImmutableList.toImmutableList()); + String workloadGroup = ctx.wg == null ? null : visitIdentifierOrText(ctx.wg); + return new SuUserCommand(userIdentity, roles, workloadGroup); + } + @Override public AccessPrivilegeWithCols visitPrivilege(DorisParser.PrivilegeContext ctx) { AccessPrivilegeWithCols accessPrivilegeWithCols; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java index 8455e178709313..306a79f0fcbb3e 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/rules/expression/rules/FoldConstantRuleOnFE.java @@ -75,6 +75,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.NullIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.Nvl; import org.apache.doris.nereids.trees.expressions.functions.scalar.Password; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.ShortCircuitIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.User; @@ -182,6 +183,7 @@ public List> buildRules() { matches(Not.class, this::visitNot), matches(Database.class, this::visitDatabase), matches(CurrentUser.class, this::visitCurrentUser), + matches(SessionIsNarrowed.class, this::visitSessionIsNarrowed), matches(CurrentCatalog.class, this::visitCurrentCatalog), matches(User.class, this::visitUser), matches(ConnectionId.class, this::visitConnectionId), @@ -399,6 +401,15 @@ public Expression visitCurrentUser(CurrentUser currentUser, ExpressionRewriteCon return new VarcharLiteral(res); } + @Override + public Expression visitSessionIsNarrowed(SessionIsNarrowed fn, ExpressionRewriteContext context) { + if (!foldContextDependentExpressions) { + return fn; + } + ConnectContext cctx = context.cascadesContext.getConnectContext(); + return BooleanLiteral.of(cctx != null && cctx.isSessionRoleNarrowed()); + } + @Override public Expression visitCurrentCatalog(CurrentCatalog currentCatalog, ExpressionRewriteContext context) { if (!foldContextDependentExpressions) { @@ -413,7 +424,9 @@ public Expression visitUser(User user, ExpressionRewriteContext context) { if (!foldContextDependentExpressions) { return user; } - String res = context.cascadesContext.getConnectContext().getUserWithLoginRemoteIpString(); + // MySQL proxy parity: USER() is the account that authenticated the connection (the switcher + // of a session-narrowed session); CURRENT_USER() and SESSION_USER() name the effective one + String res = context.cascadesContext.getConnectContext().getAuthenticatedUserWithLoginRemoteIpString(); return new VarcharLiteral(res); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java new file mode 100644 index 00000000000000..3dc23039650344 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/functions/scalar/SessionIsNarrowed.java @@ -0,0 +1,74 @@ +// 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.doris.nereids.trees.expressions.functions.scalar; + +import org.apache.doris.catalog.FunctionSignature; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.AlwaysNotNullable; +import org.apache.doris.nereids.trees.expressions.functions.ExplicitlyCastableSignature; +import org.apache.doris.nereids.trees.expressions.shape.LeafExpression; +import org.apache.doris.nereids.trees.expressions.visitor.ExpressionVisitor; +import org.apache.doris.nereids.types.BooleanType; + +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; + +import java.util.List; + +/** + * session_is_narrowed(): TRUE iff the current session is an SU-narrowed session + * (see SuUserCommand). FE-constant-folded at plan time from ConnectContext, like + * current_user(); intended for row-policy predicates and diagnostics. + */ +public class SessionIsNarrowed extends ScalarFunction + implements LeafExpression, ExplicitlyCastableSignature, AlwaysNotNullable { + + public static final List SIGNATURES = ImmutableList.of( + FunctionSignature.ret(BooleanType.INSTANCE).args() + ); + + public SessionIsNarrowed() { + super("session_is_narrowed", ImmutableList.of()); + } + + /** constructor for withChildren and reuse signature */ + private SessionIsNarrowed(ScalarFunctionParams functionParams) { + super(functionParams); + } + + @Override + public List getSignatures() { + return SIGNATURES; + } + + @Override + public Expression withChildren(List children) { + Preconditions.checkArgument(children.isEmpty()); + return new SessionIsNarrowed(getFunctionParams(children)); + } + + @Override + public R accept(ExpressionVisitor visitor, C context) { + return visitor.visitSessionIsNarrowed(this, context); + } + + @Override + public boolean isDeterministic() { + return false; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java index b5120939365edf..d5767c0df490ab 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/expressions/visitor/ScalarFunctionVisitor.java @@ -488,6 +488,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsDiff; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsSub; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha1; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sha2; @@ -1182,6 +1183,10 @@ default R visitCurrentUser(CurrentUser currentUser, C context) { return visitScalarFunction(currentUser, context); } + default R visitSessionIsNarrowed(SessionIsNarrowed sessionIsNarrowed, C context) { + return visitScalarFunction(sessionIsNarrowed, context); + } + default R visitCutIpv6(CutIpv6 cutIpv6, C context) { return visitScalarFunction(cutIpv6, context); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java index 67a602471bbb7a..ef39efb23e1ab0 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/PlanType.java @@ -461,6 +461,7 @@ public enum PlanType { SHOW_WORKLOAD_GROUP_COMMAND, TRUNCATE_TABLE_COMMAND, GRANT_ROLE_COMMAND, + SU_USER_COMMAND, GRANT_RESOURCE_PRIVILEGE_COMMAND, GRANT_TABLE_PRIVILEGE_COMMAND, REVOKE_TABLE_PRIVILEGE_COMMAND, diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java index 38fc69b4e23c38..76283cfca7982c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ExportCommand.java @@ -133,6 +133,15 @@ public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { TableNameInfo tableNameInfo = new TableNameInfo(qualifiedTableName.get(0), qualifiedTableName.get(1), qualifiedTableName.get(2)); + // The export task re-plans SELECT ... INTO OUTFILE later, on a job thread, under the stored + // identity alone: the session's role narrowing cannot follow it there (the excluded roles' + // row policies would apply, session_is_narrowed() would fold FALSE), and the job outlives + // the session. A session-narrowed (SU) session therefore must not mint one. + if (ctx.isSessionRoleNarrowed()) { + ErrorReport.reportAnalysisException("EXPORT is not allowed in a session-narrowed (SU) session; " + + "the job would run outside the session's role narrowing", + ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR); + } // check auth if (!Env.getCurrentEnv().getAccessManager() .checkTblPriv(ctx, tableNameInfo.getCtl(), tableNameInfo.getDb(), tableNameInfo.getTbl(), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java index 0672cf57be57ae..b560e6cbcc049a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/GrantTablePrivilegeCommand.java @@ -142,8 +142,9 @@ public static void checkTablePrivileges(Collection privileges, TableP Privilege.checkIncorrectPrivilege(Privilege.notBelongToTablePrivileges, privileges); // Rule 2 if (tblPattern.getPrivLevel() != Auth.PrivLevel.GLOBAL && (privileges.contains(Privilege.ADMIN_PRIV) - || privileges.contains(Privilege.NODE_PRIV))) { - throw new AnalysisException("ADMIN_PRIV and NODE_PRIV can only be granted/revoke on/from *.*.*"); + || privileges.contains(Privilege.NODE_PRIV) || privileges.contains(Privilege.PROXY_PRIV))) { + throw new AnalysisException( + "ADMIN_PRIV, NODE_PRIV and PROXY_PRIV can only be granted/revoke on/from *.*.*"); } // Rule 3 diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java index e316ca0b3e9e0c..d61940a17d2deb 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommand.java @@ -22,6 +22,7 @@ import org.apache.doris.catalog.Table; import org.apache.doris.common.ClientPool; import org.apache.doris.common.Pair; +import org.apache.doris.common.UserException; import org.apache.doris.common.proc.FrontendsProcNode; import org.apache.doris.nereids.trees.plans.PlanType; import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; @@ -39,6 +40,7 @@ import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import java.util.HashSet; import java.util.List; import java.util.Optional; @@ -82,10 +84,18 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc } if (isShowAllFe) { + // Every other FE runs the ADMIN check of listConnectionForRpc against the role subset the + // request names. An FE of an older build ignores the field and lists every user's session + // with its SQL, so a session-narrowed (SU) session's fan-out fails closed while the FE set + // runs mixed builds, as the information_schema scans that leave this FE do. + if (ctx.isSessionRoleNarrowed() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { + throw new UserException("SHOW PROCESSLIST of a session-narrowed (SU) session is answered by every " + + "alive FE, and one runs a different FE build than this FE and could not apply the " + + "narrowing. Retry once the rolling upgrade completes, or with " + + "fetch_all_fe_for_system_table = false."); + } try { - TShowProcessListRequest request = new TShowProcessListRequest(); - request.setShowFullSql(isShowFullSql); - request.setCurrentUserIdent(ConnectContext.get().getCurrentUserIdentity().toThrift()); + TShowProcessListRequest request = buildRequest(ctx, isShowFullSql); List> frontends = FrontendsProcNode.getFrontendWithRpcPort(Env.getCurrentEnv(), false); FrontendService.Client client = null; @@ -125,6 +135,21 @@ public ShowResultSet doRun(ConnectContext ctx, StmtExecutor executor) throws Exc return new ShowResultSet(getMetaData(), rowSet); } + /** + * The request every other FE answers: who is asking and, for a session-narrowed (SU) session, the + * role subset it runs under -- the same shape as FEOpExecutor and SchemaScanNode carry it. + */ + @VisibleForTesting + static TShowProcessListRequest buildRequest(ConnectContext ctx, boolean isShowFullSql) { + TShowProcessListRequest request = new TShowProcessListRequest(); + request.setShowFullSql(isShowFullSql); + request.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); + if (ctx.isSessionRoleNarrowed()) { + request.setCurrentRoles(new HashSet<>(ctx.getSessionRoleOverride())); + } + return request; + } + /** * A row from another frontend has that frontend's columns, which differ from this one's while the * cluster runs two versions: an older frontend's row ends before the columns added since (Protocol), diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java new file mode 100644 index 00000000000000..3315fe5c35e7d1 --- /dev/null +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/SuUserCommand.java @@ -0,0 +1,193 @@ +// 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.doris.nereids.trees.plans.commands; + +import org.apache.doris.analysis.StmtType; +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthenticationException; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; +import org.apache.doris.common.FeNameFormat; +import org.apache.doris.mysql.privilege.Auth; +import org.apache.doris.mysql.privilege.PrivBitSet; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.mysql.privilege.Privilege; +import org.apache.doris.nereids.trees.plans.PlanType; +import org.apache.doris.nereids.trees.plans.visitor.PlanVisitor; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; + +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.List; +import java.util.Objects; +import java.util.Set; + +/** + * SU 'user'[@'host'] WITH ROLES ('role1'[, ...]) [WORKLOAD GROUP 'wg'] + * + *

Session-narrowed identity switch, MySQL's proxy-user model with a mandatory role list: the + * session becomes the TARGET identity (audit, current_user(), user-bound row policies, user + * properties) with a role set that REPLACES every role source, enforced at the single choke point + * {@code Auth.getRolesByUserWithLdap}. Session-only state; nothing is persisted. + * + *

Contract: + *

    + *
  • Allowed only for accounts holding {@code PROXY_PRIV} (or {@code ADMIN_PRIV}, which implies + * it); a session that does not switch keeps the switching account's own grants.
  • + *
  • Narrowing only: every requested role must already be GRANTED to the target user, so the + * session can never exceed the person's real authority.
  • + *
  • One-shot: a switched session cannot SU again, and nothing widens it. A connection reset + * reverts to the authenticated identity ({@link ConnectContext#revertSessionNarrowing}).
  • + *
+ */ +public class SuUserCommand extends Command implements NoForward { + private static final Logger LOG = LogManager.getLogger(SuUserCommand.class); + + private final UserIdentity userIdentity; + private final List roles; + private final String workloadGroup; // nullable + + public SuUserCommand(UserIdentity userIdentity, List roles, String workloadGroup) { + super(PlanType.SU_USER_COMMAND); + Preconditions.checkArgument(!roles.isEmpty(), "SU requires at least one role"); + this.userIdentity = Objects.requireNonNull(userIdentity, "userIdentity is null"); + this.roles = Objects.requireNonNull(roles, "roles is null"); + this.workloadGroup = workloadGroup; + } + + @Override + public void run(ConnectContext ctx, StmtExecutor executor) throws Exception { + // a switched session must never widen or re-target itself + if (ctx.getAuthenticatedIdentity() != null) { + throw new AnalysisException("SU is not allowed in an already-switched session"); + } + if (!Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)) { + // the access-denied error code (1227) like every sibling privilege gate; the token is what + // clients and the regression suite match + ErrorReport.reportAnalysisException(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, "PROXY_PRIV"); + } + + userIdentity.analyze(); + // The built-in accounts are never a proxy target, the same rule as COM_CHANGE_USER: the admin + // account holds the admin role, so a switch to it would hand every PROXY_PRIV holder ADMIN_PRIV. + if (userIdentity.isSystemUser()) { + throw new AnalysisException("SU to root or admin is forbidden"); + } + for (String role : roles) { + // the built-in operator/admin roles carry NODE_PRIV/ADMIN_PRIV: a switch never confers them + FeNameFormat.checkRoleName(role, false /* admin not allowed */, "Invalid role in SU"); + } + + Auth auth = Env.getCurrentEnv().getAuth(); + Set targetRoles = auth.getGrantedRoleNames(userIdentity); + if (targetRoles.isEmpty()) { + // every existing user holds at least its default role + throw new AnalysisException("SU target user does not exist: " + userIdentity); + } + if (!auth.doesUserExist(userIdentity)) { + // No local account: the roles came from LDAP, which is keyed by name alone. The target must + // exist there, and it is named the way an LDAP-only login is (no host of the switcher's + // choosing), so current_user() and the audit row name an identity that exists. + if (!auth.ldapUserExists(userIdentity.getQualifiedUser())) { + throw new AnalysisException("SU target user does not exist: " + userIdentity); + } + if (!"%".equals(userIdentity.getHost())) { + throw new AnalysisException("SU target " + userIdentity + " is an LDAP account without a Doris" + + " user entry: name it with host '%'"); + } + } + // the account state a login checks: a locked account, or one whose password expired, cannot + // be assumed through a switch either + try { + auth.getPasswdPolicyManager().checkAccountLockedAndPasswordExpiration(userIdentity); + } catch (AuthenticationException e) { + throw new AnalysisException("SU target " + userIdentity + " cannot be assumed: " + e.getMessage(), e); + } + // narrowing only: the requested roles must be a subset of the target's granted roles + for (String role : roles) { + if (!targetRoles.contains(role)) { + throw new AnalysisException("SU role '" + role + "' is not granted to the target user" + + " -- SU can only narrow, never mint authority"); + } + } + // PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the switch may never move the switcher up + // the administrative ladder. A requested role that confers a global ADMIN_PRIV, NODE_PRIV or + // GRANT_PRIV is refused unless the switching account already holds that bit itself. + checkNoAdministrativeEscalation(ctx, auth.getGlobalPrivsOfRoles(roles)); + + UserIdentity authenticated = ctx.getCurrentUserIdentity(); + // the switch, and with it the switcher's cached compute group and prepared statements + ctx.applySessionNarrowing(userIdentity, roles); + // Placement only. USAGE on the session's workload group (this clause, or the target's + // default_workload_group when omitted) is checked per query against the narrowed set, then + // through the USAGE grants of the target's own roles (Auth.placementOnlyRoles): the + // person's lane follows the person. An omitted clause therefore CLEARS any group the + // switcher's session had set, so the per-query resolution falls through to the target's + // default instead of keeping the switcher's lane. + ctx.getSessionVariable().setWorkloadGroup(Strings.nullToEmpty(workloadGroup)); + // the switch itself: authenticated + effective identity, roles, connection -- the + // statement also lands in the audit log under the effective user + LOG.info("SU: connection {} narrowed. authenticated={} effective={} roles={} workloadGroup={}", + ctx.getConnectionId(), authenticated, userIdentity, roles, + Strings.isNullOrEmpty(workloadGroup) ? "" : workloadGroup); + } + + private static void checkNoAdministrativeEscalation(ConnectContext ctx, PrivBitSet conferred) + throws AnalysisException { + refuseConferredPrivilege(ctx, conferred, Privilege.ADMIN_PRIV, PrivPredicate.ADMIN); + refuseConferredPrivilege(ctx, conferred, Privilege.NODE_PRIV, PrivPredicate.OPERATOR); + refuseConferredPrivilege(ctx, conferred, Privilege.GRANT_PRIV, PrivPredicate.GRANT); + } + + private static void refuseConferredPrivilege(ConnectContext ctx, PrivBitSet conferred, Privilege privilege, + PrivPredicate heldBySwitcher) throws AnalysisException { + if (conferred.containsPrivs(privilege) + && !Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, heldBySwitcher)) { + throw new AnalysisException("SU roles confer global " + privilege + " which the switching account" + + " does not hold -- SU can only narrow, never mint authority"); + } + } + + @Override + public R accept(PlanVisitor visitor, C context) { + return visitor.visitCommand(this, context); + } + + @Override + public StmtType stmtType() { + return StmtType.OTHER; + } + + public UserIdentity getUserIdentity() { + return userIdentity; + } + + public List getRoles() { + return roles; + } + + public String getWorkloadGroup() { + return workloadGroup; + } +} diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java index ead91a50b3eca8..e298a08242aab8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/execute/ConnectorRewriteGroupTask.java @@ -256,7 +256,8 @@ private ConnectContext buildConnectContext() { taskContext.setSessionVariable(VariableMgr.cloneSessionVariable(connectContext.getSessionVariable())); taskContext.setEnv(Env.getCurrentEnv()); taskContext.setDatabase(connectContext.getDatabase()); - taskContext.setCurrentUserIdentity(connectContext.getCurrentUserIdentity()); + // the identity AND its SU narrowing: the rewrite's INSERT-SELECT is authorized as the session + taskContext.inheritIdentityFrom(connectContext); taskContext.setRemoteIP(connectContext.getRemoteIP()); UUID uuid = UUID.randomUUID(); diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java index f70f046fec2f4b..a2332e6e5ba55c 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateJobInfo.java @@ -242,6 +242,15 @@ private void buildRecurringJob(TimerDefinition timerDefinition, } protected void checkAuth() throws AnalysisException { + // Every task of the job re-plans the INSERT's SELECT later, on a job thread, under the stored + // createUser alone: the session's role narrowing cannot follow it there (the excluded roles' + // row policies would apply), and the job outlives the session. A session-narrowed (SU) + // session therefore must not mint one, in either form. + if (ConnectContext.get().isSessionRoleNarrowed()) { + ErrorReport.reportAnalysisException("CREATE JOB is not allowed in a session-narrowed (SU) session; " + + "the job would run outside the session's role narrowing", + ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR); + } if (streamingJob) { StreamingInsertJob.checkPrivilege(ConnectContext.get(), executeSql, targetDb); return; diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java index e8a75c0299b4d6..d31ae0f8363092 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/CreateRoutineLoadInfo.java @@ -28,10 +28,13 @@ import org.apache.doris.catalog.info.PartitionNamesInfo; import org.apache.doris.common.AnalysisException; import org.apache.doris.common.Config; +import org.apache.doris.common.ErrorCode; +import org.apache.doris.common.ErrorReport; import org.apache.doris.common.FeNameFormat; import org.apache.doris.common.UserException; import org.apache.doris.common.util.TimeUtils; import org.apache.doris.common.util.Util; +import org.apache.doris.datasource.InternalCatalog; import org.apache.doris.datasource.property.fileformat.CsvFileFormatProperties; import org.apache.doris.datasource.property.fileformat.FileFormatProperties; import org.apache.doris.datasource.property.fileformat.JsonFileFormatProperties; @@ -40,6 +43,9 @@ import org.apache.doris.load.routineload.AbstractDataSourceProperties; import org.apache.doris.load.routineload.RoutineLoadDataSourcePropertyFactory; import org.apache.doris.load.routineload.RoutineLoadJob; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.expressions.functions.scalar.EncryptKeyRef; import org.apache.doris.nereids.trees.plans.commands.load.LoadColumnClause; import org.apache.doris.nereids.trees.plans.commands.load.LoadColumnDesc; import org.apache.doris.nereids.trees.plans.commands.load.LoadDeleteOnClause; @@ -55,6 +61,7 @@ import org.apache.doris.thrift.TPartialUpdateNewRowPolicy; import org.apache.doris.thrift.TUniqueKeyUpdateMode; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Maps; @@ -66,6 +73,7 @@ import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.Set; import java.util.function.Predicate; /** @@ -475,6 +483,26 @@ private void validateFlexiblePartialUpdate(OlapTable table) throws AnalysisExcep } } + /** + * A routine-load task folds every `KEY db.k` reference of the column mapping per task, on a job + * thread under the stored identity alone (RoutineLoadJob.plan -> RewriteEncryptKeyRef -> + * checkDbPriv SHOW), where the creating session's SU narrowing cannot follow. The same + * authorization runs here, at creation, under the live session: a narrowed session that may not + * read the key itself cannot create a job that resolves it. + */ + @VisibleForTesting + public static void checkEncryptKeyRefs(Expression expression, ConnectContext ctx) throws AnalysisException { + Set keyRefs = expression.collect(EncryptKeyRef.class::isInstance); + for (EncryptKeyRef keyRef : keyRefs) { + String dbName = Strings.isNullOrEmpty(keyRef.getDbName()) ? ctx.getDatabase() : keyRef.getDbName(); + if (!Env.getCurrentEnv().getAccessManager() + .checkDbPriv(ctx, InternalCatalog.INTERNAL_CATALOG_NAME, dbName, PrivPredicate.SHOW)) { + ErrorReport.reportAnalysisException(ErrorCode.ERR_DB_ACCESS_DENIED_ERROR, + PrivPredicate.SHOW.getPrivs().toString(), dbName); + } + } + } + /** * check load properties * @param ctx connect context @@ -515,6 +543,7 @@ public static RoutineLoadDesc checkLoadProperties(ConnectContext ctx, Map(); for (LoadColumnDesc columnDesc : ((LoadColumnClause) loadProperty).getColumns()) { if (columnDesc.getExpression() != null) { + checkEncryptKeyRefs(columnDesc.getExpression(), ctx); Expr expr = PlanUtils.translateToLegacyExpr(columnDesc.getExpression(), table, ctx); columnInfos.add(new ImportColumnDesc(columnDesc.getColumnName(), expr)); } else { diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java index 0453ffeb52cc7c..3185f498be07c9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetPassVarOp.java @@ -62,8 +62,9 @@ public void validate(ConnectContext ctx) throws UserException { } // check privs. - // 1. this is user itself - if (isSelf) { + // 1. this is user itself -- but never through a session-narrowed (SU) session: the switcher is + // not the person, so a credential change needs the GRANT privilege of the narrowed set + if (isSelf && ctx.getAuthenticatedIdentity() == null) { return; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java index bfbb42458e2e0f..aaf69245cc7725 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java +++ b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/info/SetUserPropertyVarOp.java @@ -77,7 +77,9 @@ public void validate(ConnectContext ctx) throws UserException { return; } } - boolean isSelf = user.equals(ctx.getQualifiedUser()); + // "self" never through a session-narrowed (SU) session: the switcher is not the person, so the + // person's account defaults may only be written with the ADMIN privilege of the narrowed set + boolean isSelf = ctx.getAuthenticatedIdentity() == null && user.equals(ctx.getQualifiedUser()); for (Pattern commPattern : UserProperty.COMMON_PROPERTIES) { Matcher matcher = commPattern.matcher(key); if (matcher.find()) { diff --git a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java index ef6e34b8dad0f4..d466b31fea3cc9 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java +++ b/fe/fe-core/src/main/java/org/apache/doris/planner/SchemaScanNode.java @@ -45,6 +45,7 @@ import org.apache.logging.log4j.Logger; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; /** @@ -96,12 +97,33 @@ public List getFrontendConjuncts() { public void finalizeForNereids() throws UserException { if (ConnectContext.get().getSessionVariable().enableSchemaScanFromMasterFe && tableName.equalsIgnoreCase("tables")) { + if (ConnectContext.get().isSessionRoleNarrowed() && !Env.getCurrentEnv().masterRunsSameBuild()) { + throw new UserException(mixedBuildMessage("the master FE")); + } frontendIP = Env.getCurrentEnv().getMasterHost(); frontendPort = Env.getCurrentEnv().getMasterRpcPort(); } else { frontendIP = FrontendOptions.getLocalHostAddress(); frontendPort = Config.rpc_port; } + TableIf tableIf = desc.getTable(); + if (ConnectContext.get().isSessionRoleNarrowed() && tableIf instanceof SchemaTable + && ((SchemaTable) tableIf).shouldFetchAllFe() && !Env.getCurrentEnv().aliveFrontendsRunSameBuild()) { + throw new UserException(mixedBuildMessage("every alive FE")); + } + } + + /** + * A session-narrowed (SU) session's role subset rides in the BE's metadata RPCs to the FE + * (TGetTablesParams.current_roles and friends). An FE of an older build silently ignores the + * field and authorizes against the target's FULL role union, so a scan whose RPCs leave this + * FE (the master, or every alive FE) fails closed while the FE set runs mixed builds, exactly + * as a forwarded statement does (Env.masterRunsSameBuild). + */ + private static String mixedBuildMessage(String target) { + return "This information_schema scan of a session-narrowed (SU) session is answered by " + target + + ", which runs a different FE build than this FE and could not apply the narrowing. " + + "Retry once the rolling upgrade completes."; } private void setFeAddrList(TPlanNode msg) { @@ -162,6 +184,14 @@ protected void toThrift(TPlanNode msg) { TUserIdentity tCurrentUser = ConnectContext.get().getCurrentUserIdentity().toThrift(); msg.schema_scan_node.setCurrentUserIdent(tCurrentUser); + // SU narrowing: carry the session's active role subset (current_roles) in the plan so the + // BE->FE metadata RPCs (getDbNames/getTableNames/listTableStatus/describeTables) narrow + // name visibility to the same set the session sees; null = normal session (no narrowing). + // A BE of an older build drops the field and its RPCs reach the FE without roles, where the + // handler runs un-narrowed: BEs are upgraded before FEs, the documented rolling-upgrade order. + if (ConnectContext.get().isSessionRoleNarrowed()) { + msg.schema_scan_node.setCurrentRoles(new HashSet<>(ConnectContext.get().getSessionRoleOverride())); + } msg.schema_scan_node.setFrontendConjuncts(GsonUtils.GSON.toJson(frontendConjuncts)); setFeAddrList(msg); } diff --git a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java index 37432e0f7d62a8..f5593e96521b56 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java +++ b/fe/fe-core/src/main/java/org/apache/doris/plugin/AuditEvent.java @@ -64,6 +64,10 @@ public enum EventType { public String clientIp = ""; @AuditField(value = "User", colName = "user") public String user = ""; + // the account that authenticated the connection: equal to `user` unless the session was + // switched (SU), in which case `user` is the effective identity and this is the switcher + @AuditField(value = "AuthenticatedUser", colName = "authenticated_user") + public String authenticatedUser = ""; @AuditField(value = "FeIp", colName = "frontend_ip") public String feIp = ""; @@ -206,6 +210,11 @@ public AuditEventBuilder setUser(String user) { return this; } + public AuditEventBuilder setAuthenticatedUser(String authenticatedUser) { + auditEvent.authenticatedUser = authenticatedUser; + return this; + } + public AuditEventBuilder setCtl(String ctl) { auditEvent.ctl = ctl; return this; diff --git a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java index e1cd07d8073a58..3920da0795c88f 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java +++ b/fe/fe-core/src/main/java/org/apache/doris/plugin/audit/AuditLoader.java @@ -155,6 +155,7 @@ private void fillLogBuffer(AuditEvent event, StringBuilder logBuffer) { // cs info appendField(logBuffer, event.clientIp); appendField(logBuffer, event.user); + appendField(logBuffer, event.authenticatedUser); appendField(logBuffer, event.feIp); // default ctl and db diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java index fc71c3f1a0e86d..4146fabfb517e1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/AuditLogHelper.java @@ -220,6 +220,16 @@ private static int countValues(List children) { return cnt; } + /** + * The account that authenticated the connection: the switcher of a session-narrowed (SU) session, + * otherwise the session user itself. Never null, so the audit row always names who logged in. + */ + public static String authenticatedUser(ConnectContext ctx) { + String user = ctx.getAuthenticatedIdentity() == null + ? ctx.getQualifiedUser() : ctx.getAuthenticatedIdentity().getQualifiedUser(); + return user == null ? "" : user; + } + private static void logAuditLogImpl(ConnectContext ctx, String origStmt, StatementBase parsedStmt, org.apache.doris.proto.Data.PQueryStatistics statistics, boolean printFuzzyVariables) { // slow query @@ -256,6 +266,7 @@ private static void logAuditLogImpl(ConnectContext ctx, String origStmt, Stateme .setTimestamp(ctx.getStartTime()) .setClientIp(ctx.getClientIP()) .setUser(ctx.getQualifiedUser()) + .setAuthenticatedUser(authenticatedUser(ctx)) .setFeIp(FrontendOptions.getLocalHostAddress()) .setCtl(catalog == null ? InternalCatalog.INTERNAL_CATALOG_NAME : catalog.getName()) .setDb(ctx.getDatabase()) diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java index 8d93e63de81941..eab00ab0fe0f2b 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java @@ -98,6 +98,7 @@ import java.nio.ByteBuffer; import java.time.Instant; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -187,6 +188,14 @@ public String protocolName() { protected volatile Principal authenticatedPrincipal; // Roles granted during authentication and bound to the current session. protected volatile Set authenticatedRoles = Collections.emptySet(); + + // SU (session-narrowed identity switch, session-only state — never persisted): + // authenticatedIdentity = the identity that AUTHENTICATED this connection; non-null iff the + // session has been switched (also the re-SU refusal marker). sessionRoleOverride = the role + // set that REPLACES every role source for privilege checks and row-policy matching, consulted + // in Auth.getRolesByUserWithLdap under the same current-identity guard as authenticatedRoles. + protected volatile UserIdentity authenticatedIdentity; + protected volatile Set sessionRoleOverride; // Variables belong to this session. protected volatile SessionVariable sessionVariable; // Store user variable in this connection @@ -369,6 +378,11 @@ public void resetConnection() throws UserException { cleanupTemporaryTables(true); dbToTempTableNamesMap.clear(); } + // A narrowed session reverts to the AUTHENTICATED identity on connection reset — + // dropping only the override would widen the session to the target's full roles. + // It reverts BEFORE the session variables are rebuilt, so the per-user defaults applied + // there are the authenticated account's, not the switched-to target's. + revertSessionNarrowing(); resetSessionVariable(); userVars = new HashMap<>(); preparedStatementContextMap.clear(); @@ -380,6 +394,86 @@ public void resetConnection() throws UserException { returnRows = 0; } + /** + * The SU switch: this session becomes the target identity under the role override, and the + * identity that authenticated the connection is kept for the audit row and the revert. What the + * session cached for the switcher goes with it: the compute group chosen for it (the next + * statement resolves placement for the target) and the prepared statements, whose plans were + * authorized under the switcher and would otherwise execute as the target. + */ + public void applySessionNarrowing(UserIdentity target, Collection roles) { + this.authenticatedIdentity = this.currentUserIdentity; + this.currentUserIdentity = target; + this.sessionRoleOverride = Collections.unmodifiableSet(new HashSet<>(roles)); + this.cloudCluster = null; + this.preparedStatementContextMap.clear(); + } + + /** Revert an SU switch: back to the authenticated identity, override cleared. No-op if not switched. */ + public void revertSessionNarrowing() { + UserIdentity authed = this.authenticatedIdentity; + if (authed != null) { + this.currentUserIdentity = authed; + this.authenticatedIdentity = null; + this.sessionRoleOverride = null; + leaveSwitchedPlacement(); + } + } + + // Leaving the switch leaves its placement: the compute group cached for the target and the + // workload-group lane the switch chose (its clause, or the cleared switcher's lane) go, so the + // identity that owns the session next resolves both for itself -- on COM_CHANGE_USER as on reset. + private void leaveSwitchedPlacement() { + this.cloudCluster = null; + this.sessionVariable.setWorkloadGroup(""); + } + + /** + * Makes this (child) context run as the parent's session does: the current identity together + * with the SU narrowing state, so a privilege check on the child resolves the same role set the + * parent session sees. Copying getCurrentUserIdentity() alone would carry the TARGET identity + * without the override, i.e. the target's full role union. + */ + public void inheritIdentityFrom(ConnectContext parent) { + this.currentUserIdentity = parent.currentUserIdentity; + this.authenticatedIdentity = parent.authenticatedIdentity; + this.sessionRoleOverride = parent.sessionRoleOverride; + } + + /** Drop SU state without restoring identity — only for paths that fully re-authenticate. */ + public void clearSessionNarrowing() { + if (this.authenticatedIdentity != null) { + leaveSwitchedPlacement(); + } + this.authenticatedIdentity = null; + this.sessionRoleOverride = null; + } + + public UserIdentity getAuthenticatedIdentity() { + return authenticatedIdentity; + } + + public void setAuthenticatedIdentity(UserIdentity authenticatedIdentity) { + this.authenticatedIdentity = authenticatedIdentity; + } + + /** + * True when SU narrowed this session: the override set (possibly empty) then replaces every role + * source. This predicate, not a null check on getSessionRoleOverride(), is the sentinel; a mocked + * context answers false here while a mocked collection getter answers an empty set. + */ + public boolean isSessionRoleNarrowed() { + return sessionRoleOverride != null; + } + + public Set getSessionRoleOverride() { + return sessionRoleOverride; + } + + public void setSessionRoleOverride(Set roles) { + this.sessionRoleOverride = roles == null ? null : Collections.unmodifiableSet(new HashSet<>(roles)); + } + private void resetSessionVariable() { sessionVariable = VariableMgr.newSessionVariable(); applyUserSessionVariableDefaults(); @@ -751,6 +845,14 @@ public String getUserWithLoginRemoteIpString() { return UserIdentity.createAnalyzedUserIdentWithIp(getQualifiedUser(), remoteIP).toString(); } + // used for select user(): the account that authenticated the connection, which in a + // session-narrowed (SU) session is the switcher -- MySQL's USER() names the proxying account + // and CURRENT_USER() the proxied one + public String getAuthenticatedUserWithLoginRemoteIpString() { + String user = authenticatedIdentity != null ? authenticatedIdentity.getQualifiedUser() : getQualifiedUser(); + return UserIdentity.createAnalyzedUserIdentWithIp(user, remoteIP).toString(); + } + public void setCurrentUserIdentity(UserIdentity currentUserIdentity) { this.currentUserIdentity = currentUserIdentity; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java index b5699152df7079..bd7deb141c20d5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java @@ -41,6 +41,7 @@ import org.apache.doris.cloud.qe.ComputeGroupException; import org.apache.doris.cloud.system.CloudSystemInfoService; import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthorizationException; import org.apache.doris.common.Config; import org.apache.doris.common.ConnectionException; import org.apache.doris.common.ErrorCode; @@ -81,6 +82,7 @@ import org.apache.doris.thrift.TUniqueId; import org.apache.doris.transaction.TransactionEntry; +import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; import com.google.common.base.Strings; import com.google.common.collect.ImmutableList; @@ -99,6 +101,7 @@ import java.util.Map; import java.util.Optional; import java.util.OptionalLong; +import java.util.Set; import java.util.UUID; /** @@ -546,6 +549,32 @@ protected void handleQueryException(Throwable throwable, String origStmt, auditAfterExec(origStmt, parsedStmt, statistics, true); } + /** + * Install the SU narrowing a forwarded statement carries (is_su_user + current_roles) on this + * proxy session, so every privilege check on the master resolves the same narrowed role set the + * origin FE would. Fail closed: a switched session that names no role subset is refused rather + * than narrowed to nothing, so an installed override is never empty and the wire field has one + * meaning on every side -- absent means "not narrowed" (the BE forwards a plan's current_roles + * only when the set is non-empty; the FE grammar requires at least one role). + */ + @VisibleForTesting + static void applyForwardedSessionNarrowing(ConnectContext ctx, TMasterOpRequest request) + throws AuthorizationException { + if (!request.isSetIsSuUser() || !request.isIsSuUser()) { + return; + } + if (!request.isSetCurrentRoles() || request.getCurrentRoles().isEmpty()) { + throw new AuthorizationException("A forwarded statement of a session-narrowed (SU) session names no " + + "role subset; refusing to run it against the target's full role union"); + } + Set roles = request.getCurrentRoles(); + ctx.setSessionRoleOverride(roles); + if (request.isSetAuthenticatedUserIdent()) { + // audit only: the master's row names the account that authenticated the connection + ctx.setAuthenticatedIdentity(UserIdentity.fromThrift(request.getAuthenticatedUserIdent())); + } + } + public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException { ctx.setDatabase(request.db); ctx.setEnv(Env.getCurrentEnv()); @@ -585,6 +614,8 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException ctx.setThreadLocalInfo(); StmtExecutor executor = null; try { + // the narrowing, or the refusal of a switched request that names none, before anything runs + applyForwardedSessionNarrowing(ctx, request); MysqlProtocolAdapter.of(ctx).restoreFromForwardRequest(ctx, request); // 0 for compatibility. int idx = request.isSetStmtIdx() ? request.getStmtIdx() : 0; @@ -653,6 +684,10 @@ public TMasterOpResult proxyExecute(TMasterOpRequest request) throws TException } else { executor.queryRetry(queryId); } + } catch (AuthorizationException e) { + // a switched forward that names no role subset: refused, reported to the origin FE as such + LOG.warn("forwarded statement refused: {}", e.getMessage()); + ctx.getState().setError(ErrorCode.ERR_SPECIFIC_ACCESS_DENIED_ERROR, e.getMessage()); } catch (IOException e) { // Client failed. LOG.warn("Process one query failed because IOException: ", e); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java index 404814ebeb3369..3f50f11069ef89 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/FEOpExecutor.java @@ -50,6 +50,7 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -194,6 +195,29 @@ protected TMasterOpRequest buildStmtForwardParams() throws AnalysisException { params.setUserIp(ctx.getRemoteIP()); params.setStmtId(ctx.getStmtId()); params.setCurrentUserIdent(ctx.getCurrentUserIdentity().toThrift()); + if (ctx.isSessionRoleNarrowed()) { + // A session-narrowed (SU) session carries its active role subset in the request + // (is_su_user / current_roles) and the receiving FE installs it before executing. An + // older FE silently ignores those fields and would authorize the statement against the + // target's FULL role union, so every SQL forward from a narrowed session -- to the master + // or, for KILL QUERY, to any other FE -- fails closed unless the receiver is known to run + // this same build (a rolling upgrade upgrades the master last). + if (!Env.getCurrentEnv().frontendRunsSameBuild(feAddr.getHostname(), feAddr.getPort())) { + throw new AnalysisException("This statement would be forwarded to FE " + feAddr.getHostname() + + ":" + feAddr.getPort() + ", which runs a different build than this FE; a " + + "session-narrowed (SU) session forwards only to an FE known to apply the narrowing. " + + "Retry once the rolling upgrade completes, or run it in a non-narrowed session."); + } + Set sessionRoleOverride = ctx.getSessionRoleOverride(); + // carry the active role subset so the receiving FE authorizes the forwarded statement + // against exactly this set, never the target's full role union + params.setIsSuUser(true); + params.setCurrentRoles(new HashSet<>(sessionRoleOverride)); + if (ctx.getAuthenticatedIdentity() != null) { + // the master's audit row names the account that authenticated the connection + params.setAuthenticatedUserIdent(ctx.getAuthenticatedIdentity().toThrift()); + } + } params.setSessionId(ctx.getSessionId()); params.setMoreResultExists(moreStmtExists); diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java index c5a0931df1e213..f613952612ef63 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java @@ -539,6 +539,9 @@ private void handleChangeUser() throws IOException { ctx.getState().setError(ErrorCode.ERR_ACCESS_DENIED_ERROR, "Authentication failed."); return; } + // A failed change keeps the current identity, so it must also keep its narrowed roles. + // Clear SU state only after the incoming credentials establish a new identity. + ctx.clearSessionNarrowing(); ctx.setCurrentUserIdentity(currentUserIdentity.get(0)); // Change default db if set. diff --git a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java index 054b8904799e07..99c2c72bbe222a 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java +++ b/fe/fe-core/src/main/java/org/apache/doris/qe/StmtExecutor.java @@ -1525,6 +1525,7 @@ public boolean isProfileSafeStmt() { } private void forwardToMaster() throws Exception { + // a session-narrowed (SU) session's same-build gate sits in FEOpExecutor.buildStmtForwardParams masterOpExecutor = new MasterOpExecutor(originStmt, context, redirectStatus, isQuery()); if (LOG.isDebugEnabled()) { LOG.debug("need to transfer to Master. stmt: {}", context.getStmtId()); diff --git a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java index dc6ae5ecc34994..65ba2108c6c526 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java +++ b/fe/fe-core/src/main/java/org/apache/doris/service/FrontendServiceImpl.java @@ -110,6 +110,7 @@ import org.apache.doris.master.MasterImpl; import org.apache.doris.meta.MetaContext; import org.apache.doris.mysql.privilege.AccessControllerManager; +import org.apache.doris.mysql.privilege.Auth; import org.apache.doris.mysql.privilege.PrivPredicate; import org.apache.doris.nereids.trees.plans.PlanNodeAndHash; import org.apache.doris.nereids.trees.plans.commands.RestoreCommand; @@ -325,6 +326,7 @@ import org.apache.doris.thrift.TUpdateFollowerPartitionStatsCacheRequest; import org.apache.doris.thrift.TUpdateFollowerStatsCacheRequest; import org.apache.doris.thrift.TUpdatePlanStatsCacheRequest; +import org.apache.doris.thrift.TUserIdentity; import org.apache.doris.thrift.TWaitingTxnStatusRequest; import org.apache.doris.thrift.TWaitingTxnStatusResult; import org.apache.doris.transaction.BeginTransactionException; @@ -500,8 +502,41 @@ public TConfirmUnusedRemoteFilesResult confirmUnusedRemoteFiles(TConfirmUnusedRe return res; } + /** + * Runs a metadata RPC handler under the calling session's SU-narrowed role subset when the + * request carries one, so every privilege check inside the handler resolves the same narrowed + * set a live SU session would (Auth.getRolesByUserWithLdap consults the thread-local). It is + * cleared in a finally because handler threads are pooled. A request without an override runs + * unchanged. + */ + private static T withSessionRoleOverride(TUserIdentity currentUserIdent, String user, String userIp, + Set currentRoles, MetadataRpcHandler handler) throws TException { + if (currentRoles == null) { + return handler.run(); + } + UserIdentity narrowedUser = currentUserIdent != null + ? UserIdentity.fromThrift(currentUserIdent) + : UserIdentity.createAnalyzedUserIdentWithIp(user, userIp); + Auth.setRpcSessionNarrowing(narrowedUser, Sets.newHashSet(currentRoles)); + try { + return handler.run(); + } finally { + Auth.clearRpcSessionNarrowing(); + } + } + + @FunctionalInterface + private interface MetadataRpcHandler { + T run() throws TException; + } + @Override public TGetDbsResult getDbNames(TGetDbsParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> getDbNamesImpl(params)); + } + + private TGetDbsResult getDbNamesImpl(TGetDbsParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get db request: {}", params); } @@ -595,6 +630,11 @@ private String getDbNameFromMysqlTableSchema(String ctl, String db) { @Override public TGetTablesResult getTableNames(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> getTableNamesImpl(params)); + } + + private TGetTablesResult getTableNamesImpl(TGetTablesParams params) throws TException { try { if (LOG.isDebugEnabled()) { LOG.debug("get table name request: {}", params); @@ -657,6 +697,11 @@ public TGetTablesResult getTableNames(TGetTablesParams params) throws TException @Override public TListTableStatusResult listTableStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTableStatusImpl(params)); + } + + private TListTableStatusResult listTableStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list table request: {}", params); } @@ -816,6 +861,12 @@ private static boolean needTableStatusStats(Set requiredColumns) { } public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTableMetadataNameIdsImpl(params)); + } + + private TListTableMetadataNameIdsResult listTableMetadataNameIdsImpl(TGetTablesParams params) + throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list simple table request: {}", params); } @@ -848,36 +899,43 @@ public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams } } PatternMatcher finalMatcher = matcher; + // the RPC narrowing withSessionRoleOverride installed is a ThreadLocal of the handler thread: + // carry it onto the executor thread, or the privilege check below runs un-narrowed + Map> narrowing = Auth.captureRpcSessionNarrowing(); ExecutorService executor = Executors.newSingleThreadExecutor(); Future future = executor.submit(() -> { - - CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName); - if (catalog != null) { - String dbName = getDbNameFromMysqlTableSchema(catalogName, params.db); - DatabaseIf db = catalog.getDbNullable(dbName); - if (db != null) { - List tables = db.getTables(); - for (TableIf table : tables) { - if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUser, catalogName, dbName, - table.getName(), PrivPredicate.SHOW)) { - continue; - } - table.readLock(); - try { - if (finalMatcher != null && !finalMatcher.match(table.getName())) { + Auth.installRpcSessionNarrowing(narrowing); + try { + CatalogIf catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(catalogName); + if (catalog != null) { + String dbName = getDbNameFromMysqlTableSchema(catalogName, params.db); + DatabaseIf db = catalog.getDbNullable(dbName); + if (db != null) { + List tables = db.getTables(); + for (TableIf table : tables) { + if (!Env.getCurrentEnv().getAccessManager().checkTblPriv(currentUser, catalogName, + dbName, table.getName(), PrivPredicate.SHOW)) { continue; } - TTableMetadataNameIds status = new TTableMetadataNameIds(); - status.setName(table.getName()); - status.setId(table.getId()); + table.readLock(); + try { + if (finalMatcher != null && !finalMatcher.match(table.getName())) { + continue; + } + TTableMetadataNameIds status = new TTableMetadataNameIds(); + status.setName(table.getName()); + status.setId(table.getId()); - tablesResult.add(status); - } finally { - table.readUnlock(); + tablesResult.add(status); + } finally { + table.readUnlock(); + } } } } + } finally { + Auth.clearRpcSessionNarrowing(); } }); try { @@ -899,6 +957,11 @@ public TListTableMetadataNameIdsResult listTableMetadataNameIds(TGetTablesParams @Override public TListPrivilegesResult listTablePrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listTablePrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listTablePrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list table privileges request: {}", params); } @@ -918,6 +981,11 @@ public TListPrivilegesResult listTablePrivilegeStatus(TGetTablesParams params) t @Override public TListPrivilegesResult listSchemaPrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listSchemaPrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listSchemaPrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list schema privileges request: {}", params); } @@ -937,6 +1005,11 @@ public TListPrivilegesResult listSchemaPrivilegeStatus(TGetTablesParams params) @Override public TListPrivilegesResult listUserPrivilegeStatus(TGetTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> listUserPrivilegeStatusImpl(params)); + } + + private TListPrivilegesResult listUserPrivilegeStatusImpl(TGetTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get list user privileges request: {}", params); } @@ -956,6 +1029,11 @@ public TListPrivilegesResult listUserPrivilegeStatus(TGetTablesParams params) th @Override public TDescribeTablesResult describeTables(TDescribeTablesParams params) throws TException { + return withSessionRoleOverride(params.current_user_ident, params.user, params.user_ip, + params.current_roles, () -> describeTablesImpl(params)); + } + + private TDescribeTablesResult describeTablesImpl(TDescribeTablesParams params) throws TException { if (LOG.isDebugEnabled()) { LOG.debug("get desc tables request: {}", params); } @@ -3416,6 +3494,28 @@ public TStatus syncCloudVersion(TFrontendSyncCloudVersionRequest request) @Override public TFetchSchemaTableDataResult fetchSchemaTableData(TFetchSchemaTableDataRequest request) throws TException { + // information_schema tables and metadata table functions: the request names the caller and, + // for a session-narrowed (SU) session, the active role subset; MetadataGenerator's privilege + // checks then resolve that set instead of the caller's full role union. + TUserIdentity currentUserIdent = null; + Set currentRoles = null; + if (request.isSetSchemaTableParams()) { + currentUserIdent = request.getSchemaTableParams().current_user_ident; + currentRoles = request.getSchemaTableParams().current_roles; + } else if (request.isSetMetadaTableParams()) { + currentUserIdent = request.getMetadaTableParams().current_user_ident; + currentRoles = request.getMetadaTableParams().current_roles; + } + if (currentRoles != null && currentUserIdent == null) { + // a narrowed request must say whom it narrows; never fall back to the full role union + return MetadataGenerator.errorResult("current user ident is not set for a narrowed request"); + } + return withSessionRoleOverride(currentUserIdent, null, null, currentRoles, + () -> fetchSchemaTableDataImpl(request)); + } + + private TFetchSchemaTableDataResult fetchSchemaTableDataImpl(TFetchSchemaTableDataRequest request) + throws TException { try { if (!request.isSetSchemaTableName()) { return MetadataGenerator.errorResult("Fetch schema table name is not set"); @@ -5758,6 +5858,13 @@ public TStatus reportCommitTxnResult(TReportCommitTxnResultRequest request) thro @Override public TShowProcessListResult showProcessList(TShowProcessListRequest request) throws TException { + // a narrowed session's ADMIN check inside listConnectionForRpc resolves the narrowed set, so a + // tenant-narrowed administrator sees only the sessions of the identity it runs as + return withSessionRoleOverride(request.current_user_ident, null, null, request.current_roles, + () -> showProcessListImpl(request)); + } + + private TShowProcessListResult showProcessListImpl(TShowProcessListRequest request) throws TException { if (!request.isSetCurrentUserIdent()) { throw new TException("Current user identity is not set"); } @@ -5778,7 +5885,12 @@ public TShowProcessListResult showProcessList(TShowProcessListRequest request) t } @Override - public TShowUserResult showUser(TShowUserRequest request) { + public TShowUserResult showUser(TShowUserRequest request) throws TException { + return withSessionRoleOverride(request.current_user_ident, null, null, request.current_roles, + () -> showUserImpl(request)); + } + + private TShowUserResult showUserImpl(TShowUserRequest request) { UserIdentity currentUser = null; if (request.isSetCurrentUserIdent()) { currentUser = UserIdentity.fromThrift(request.current_user_ident); diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java b/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java index 62970a9315cc6a..37c2cde273feb8 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/Frontend.java @@ -19,6 +19,7 @@ import org.apache.doris.catalog.Env; import org.apache.doris.common.Config; +import org.apache.doris.common.Version; import org.apache.doris.common.io.Text; import org.apache.doris.common.io.Writable; import org.apache.doris.ha.BDBHA; @@ -93,6 +94,11 @@ public String getVersion() { return version; } + /** The build string this FE reports in its heartbeat; other FEs compare their own against it. */ + public static String localBuildVersion() { + return Version.DORIS_BUILD_VERSION + "-" + Version.DORIS_BUILD_SHORT_HASH; + } + public String getLocalResourceGroup() { return localResourceGroup; } diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java b/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java index 3a16cfd5a97edb..cd2586f20a49f1 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/FrontendHbResponse.java @@ -40,6 +40,9 @@ public class FrontendHbResponse extends HeartbeatResponse implements Writable { private int arrowFlightSqlPort; @SerializedName(value = "replayedJournalId") private long replayedJournalId; + // Journaled: a non-master FE learns the build every FE runs only through the master's + // HbPackage replay, and Env.frontendRunsSameBuild compares its own build against it. + @SerializedName(value = "version") private String version; private long feStartTime; private long processUUID; diff --git a/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java b/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java index 345cd287be8bee..b4772a2e430ae5 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java +++ b/fe/fe-core/src/main/java/org/apache/doris/system/HeartbeatMgr.java @@ -25,7 +25,6 @@ import org.apache.doris.common.FeConstants; import org.apache.doris.common.ThreadPoolManager; import org.apache.doris.common.UserException; -import org.apache.doris.common.Version; import org.apache.doris.common.util.DebugPointUtil; import org.apache.doris.common.util.HttpURLUtil; import org.apache.doris.common.util.MasterDaemon; @@ -494,7 +493,7 @@ public HeartbeatResponse call() { return new FrontendHbResponse(fe.getNodeName(), Config.query_port, Config.rpc_port, Config.arrow_flight_sql_port, Env.getCurrentEnv().getMaxJournalId(), System.currentTimeMillis(), - Version.DORIS_BUILD_VERSION + "-" + Version.DORIS_BUILD_SHORT_HASH, + Frontend.localBuildVersion(), ExecuteEnv.getInstance().getStartupTime(), ExecuteEnv.getInstance().getDiskInfos(), ExecuteEnv.getInstance().getProcessUUID(), Config.local_resource_group); } else { diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java index 8bff846542f31d..50320fe3698933 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/CloudAuthTest.java @@ -35,6 +35,7 @@ import org.apache.doris.nereids.trees.plans.commands.DropRoleCommand; import org.apache.doris.nereids.trees.plans.commands.DropUserCommand; import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; +import org.apache.doris.nereids.trees.plans.commands.GrantRoleCommand; import org.apache.doris.nereids.trees.plans.commands.GrantTablePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.RevokeResourcePrivilegeCommand; import org.apache.doris.nereids.trees.plans.commands.ShowGrantsCommand; @@ -53,6 +54,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -477,4 +479,60 @@ public void testVirtualComputeGroup() throws Exception { Assertions.assertTrue(dropplan5 instanceof DropUserCommand); Assertions.assertDoesNotThrow(() -> ((DropUserCommand) dropplan5).run(connectContext, null)); } + + /** + * Compute-group (CLUSTER) USAGE is placement, like workload-group USAGE: an SU-narrowed session + * keeps exactly the compute groups the person's own session could use (direct grant and + * role-carried), never more, and the widening is scoped to the session's own identity. + * Lives here because the cluster privilege check resolves virtual compute groups through the + * cloud system-info service, which only this mocked cloud harness provides. + */ + @Test + public void testNarrowedSessionKeepsPersonsComputeGroupUsage() throws Exception { + NereidsParser parser = new NereidsParser(); + ((CreateRoleCommand) parser.parseSingle("CREATE ROLE su_cg_tenant")).run(connectContext, null); + ((CreateRoleCommand) parser.parseSingle("CREATE ROLE su_cg_lane")).run(connectContext, null); + UserIdentity person = new UserIdentity("su_cg_person", "%"); + person.analyze(); + CreateUserCommand createPerson = new CreateUserCommand( + new CreateUserInfo(false, new UserDesc(person), "su_cg_tenant", null, null)); + createPerson.getInfo().validate(); + auth.createUser(createPerson.getInfo()); + GrantRoleCommand grantLane = new GrantRoleCommand(person, Lists.newArrayList("su_cg_lane")); + grantLane.validate(); + auth.grantRoleCommand(grantLane); + // the login shape: USAGE granted DIRECTLY to the account (its default role) ... + ((GrantResourcePrivilegeCommand) parser.parseSingle( + "GRANT usage_priv ON cluster 'su_cg_person_lane' TO 'su_cg_person'@'%'")).run(connectContext, null); + // ... and one reachable only through a role the person holds but does not request + ((GrantResourcePrivilegeCommand) parser.parseSingle( + "GRANT usage_priv ON cluster 'su_cg_via_role' TO ROLE 'su_cg_lane'")).run(connectContext, null); + UserIdentity bystander = new UserIdentity("su_cg_bystander", "%"); + bystander.analyze(); + + // a real session for the person in place of the mocked one + ConnectContext session = new ConnectContext(); + session.setCurrentUserIdentity(person); + mockedCtxStatic.when(ConnectContext::get).thenReturn(session); + try { + // the person's own session + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_person_lane")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_via_role")); + Assertions.assertFalse(canUseComputeGroup(person, "su_cg_nobody")); + + // SU narrowed the session to the tenant role: placement still follows the person + session.setSessionRoleOverride(Collections.singleton("su_cg_tenant")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_person_lane")); + Assertions.assertTrue(canUseComputeGroup(person, "su_cg_via_role")); + // never wider than the person's own session, and scoped to the session's own identity + Assertions.assertFalse(canUseComputeGroup(person, "su_cg_nobody")); + Assertions.assertFalse(canUseComputeGroup(bystander, "su_cg_person_lane")); + } finally { + mockedCtxStatic.when(ConnectContext::get).thenReturn(ctx); + } + } + + private boolean canUseComputeGroup(UserIdentity user, String computeGroup) { + return accessManager.checkCloudPriv(user, computeGroup, PrivPredicate.USAGE, ResourceTypeEnum.CLUSTER); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java index e080448b212a8c..7b4c4d35486e1c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/EngineAuthorizationContextTest.java @@ -61,22 +61,21 @@ public void checkPrivilege(AuthorizedSubject subject, AuthorizedResource resourc } /** - * Roles come without the per-user default role: it is how the built-in model stores a user's own grants, - * not a role an administrator writes a policy against. + * Roles come as the engine's own checks resolve them (without the per-user default role, under the + * session's SU narrowing if any): a source's role-bound policies see the same set the engine enforces. */ @Test - public void testRolesAreReadWithoutTheInternalDefaultRole() { + public void testRolesAreReadAsTheEngineResolvesThem() { Auth auth = Mockito.mock(Auth.class); Set roles = ImmutableSet.of("analyst"); - Mockito.when(auth.getRolesByUser(Mockito.any(UserIdentity.class), Mockito.eq(false))).thenReturn(roles); + Mockito.when(auth.getEffectiveRoleNames(Mockito.any(UserIdentity.class))).thenReturn(roles); EngineAuthorizationContext context = new EngineAuthorizationContext( new AccessControllerManager(auth), auth); context.servedBy(new Recording()); Assertions.assertEquals(roles, context.rolesOf(SUBJECT)); - Mockito.verify(auth).getRolesByUser( - UserIdentity.createAnalyzedUserIdentWithIp("user1", "%"), false); + Mockito.verify(auth).getEffectiveRoleNames(UserIdentity.createAnalyzedUserIdentWithIp("user1", "%")); } @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java new file mode 100644 index 00000000000000..d920f98347cfab --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/mysql/privilege/SuUserNarrowingTest.java @@ -0,0 +1,802 @@ +// 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.doris.mysql.privilege; + +import org.apache.doris.analysis.CompoundPredicate.Operator; +import org.apache.doris.analysis.PassVar; +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.jmockit.Deencapsulation; +import org.apache.doris.common.proc.AuthProcDir; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.expressions.Expression; +import org.apache.doris.nereids.trees.plans.commands.Command; +import org.apache.doris.nereids.trees.plans.commands.CreateJobCommand; +import org.apache.doris.nereids.trees.plans.commands.CreateWorkloadGroupCommand; +import org.apache.doris.nereids.trees.plans.commands.ExportCommand; +import org.apache.doris.nereids.trees.plans.commands.GrantResourcePrivilegeCommand; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.nereids.trees.plans.commands.info.CreateRoutineLoadInfo; +import org.apache.doris.nereids.trees.plans.commands.info.SetPassVarOp; +import org.apache.doris.nereids.trees.plans.commands.info.SetUserPropertyVarOp; +import org.apache.doris.nereids.trees.plans.logical.LogicalPlan; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.StmtExecutor; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; + +/** + * SU (session-narrowed identity switch) acceptance tests: + * (a) a narrowed session loses the target's personal grants (override REPLACES the role union); + * (c) re-SU is refused; (d) a session that skips SU keeps only the switcher's own grants; + * (f) narrowing never leaks into checks of OTHER identities; (g) the narrowed session keeps the + * person's own WORKLOAD GROUP usage (placement follows the person; never wider than their own + * session); plus the PROXY_PRIV gate, the target-grants ceiling, the parser round trip and the + * no-ConnectContext metadata-RPC path. + * End-to-end row-policy + audit coverage belongs to the regression suite, not here. + */ +public class SuUserNarrowingTest extends TestWithFeService { + + private static final String CTL = InternalCatalog.INTERNAL_CATALOG_NAME; + + @Override + protected void runBeforeAll() throws Exception { + // GRANT validates database existence on the internal catalog + createDatabase("test"); + createDatabase("perso"); + createDatabase("perso2"); + } + + private UserIdentity ident(String name) { + return UserIdentity.createAnalyzedUserIdentWithIp(name, "%"); + } + + private boolean canSelectDb(UserIdentity user, String db) { + return Env.getCurrentEnv().getAuth().checkDbPriv(user, CTL, db, PrivPredicate.SELECT); + } + + private boolean canSelectInfoSchema(UserIdentity user) { + return Env.getCurrentEnv().getAuth().checkDbPriv(user, CTL, + org.apache.doris.catalog.InfoSchemaDb.DATABASE_NAME, PrivPredicate.SELECT); + } + + private boolean canUseWorkloadGroup(UserIdentity user, String wg) { + return Env.getCurrentEnv().getAuth().checkWorkloadGroupPriv(user, wg, PrivPredicate.USAGE); + } + + private void createWorkloadGroup(String name) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle("CREATE WORKLOAD GROUP IF NOT EXISTS " + name + + " PROPERTIES ('min_memory_percent'='10', 'max_memory_percent'='30%')"); + Assertions.assertTrue(plan instanceof CreateWorkloadGroupCommand); + ((CreateWorkloadGroupCommand) plan).run(connectContext, null); + } + + private void grantWorkloadGroupUsage(String sql) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle(sql); + Assertions.assertTrue(plan instanceof GrantResourcePrivilegeCommand); + ((GrantResourcePrivilegeCommand) plan).run(connectContext, null); + } + + /** A session of a service account that holds nothing but the right to switch. */ + private ConnectContext proxySession(String switcher) throws Exception { + addUser(switcher, true); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO '" + switcher + "'@'%';"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident(switcher)); + ctx.setRemoteIP("127.0.0.1"); + ctx.setThreadLocalInfo(); + return ctx; + } + + /** Runs an administrative statement on the test's admin session, then restores the thread's context. */ + private void runAsAdmin(String sql) throws Exception { + LogicalPlan plan = new NereidsParser().parseSingle(sql); + Assertions.assertTrue(plan instanceof Command, sql); + ConnectContext current = ConnectContext.get(); + connectContext.setThreadLocalInfo(); + try { + ((Command) plan).run(connectContext, new StmtExecutor(connectContext, sql)); + } finally { + current.setThreadLocalInfo(); + } + } + + private static String shownRoles(UserIdentity user) { + return Env.getCurrentEnv().getAuth().getAuthInfo(user).get(0).get(AuthProcDir.TITLE_NAMES.indexOf("Roles")); + } + + private static void assertSwitchRefused(ConnectContext ctx, UserIdentity target, String role) { + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(target, Collections.singletonList(role), null).run(ctx, null)); + // a refused switch leaves the session exactly as it was + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertFalse(ctx.isSessionRoleNarrowed()); + } + + @Test + public void testOverrideReplacesRoleUnionAndDoesNotLeak() throws Exception { + addUser("alice", true); + addUser("bystander", true); + createRole("space_a"); + createRole("bystander_r"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_a';"); + grantRole("GRANT 'space_a' TO 'alice'@'%'"); + grantRole("GRANT 'bystander_r' TO 'bystander'@'%'"); + // personal (default-role) grant on another db + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'alice'@'%';"); + + UserIdentity alice = ident("alice"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(alice); + ctx.setThreadLocalInfo(); + try { + // un-narrowed: personal + role grants both live + Assertions.assertTrue(canSelectDb(alice, "perso")); + Assertions.assertTrue(canSelectDb(alice, "test")); + + ctx.setSessionRoleOverride(Collections.singleton("space_a")); + // (a) the override REPLACES everything: the personal grant is gone... + Assertions.assertFalse(canSelectDb(alice, "perso")); + // ...while the requested role's grants remain + Assertions.assertTrue(canSelectDb(alice, "test")); + + // (f) checks against OTHER identities are untouched by this session's narrowing + Set bystanderRoles = Env.getCurrentEnv().getAuth() + .getRoleNamesByUserWithLdap(ident("bystander"), false); + Assertions.assertTrue(bystanderRoles.contains("bystander_r")); + + // revert restores the full union + ctx.setSessionRoleOverride(null); + Assertions.assertTrue(canSelectDb(alice, "perso")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSuCommandContract() throws Exception { + addUser("svc", true); + addUser("bobby", true); + createRole("r_bobby"); + createRole("r_ungranted"); + createRole("r_svc_only"); + grantRole("GRANT 'r_bobby' TO 'bobby'@'%'"); + grantRole("GRANT 'r_svc_only' TO 'svc'@'%'"); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("svc")); + ctx.setThreadLocalInfo(); + try { + // gate: neither ADMIN_PRIV nor PROXY_PRIV -> refused; (d) the session stays the service account + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_bobby"), null).run(ctx, null)); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertFalse(canSelectDb(ident("svc"), "test")); + + // PROXY_PRIV is the native gate (ADMIN_PRIV implies it), and it is global-only; the grants + // themselves run as the test's admin session, not as the service account + connectContext.setThreadLocalInfo(); + try { + Assertions.assertThrows(Exception.class, + () -> grantPriv("GRANT PROXY_PRIV ON internal.test.* TO 'svc'@'%';")); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'svc'@'%';"); + } finally { + ctx.setThreadLocalInfo(); + } + Assertions.assertTrue(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.PROXY)); + + // nonexistent target refused + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("ghost", "%"), + Collections.singletonList("r_bobby"), null).run(ctx, null)); + + // NARROWING-ONLY LAW (default ceiling = target): role exists but is not bobby's + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_ungranted"), null).run(ctx, null)); + + // legal switch + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_bobby"), "wg_probe").run(ctx, null); + Assertions.assertEquals("bobby", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assertions.assertEquals(Collections.singleton("r_bobby"), ctx.getSessionRoleOverride()); + Assertions.assertEquals("svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals("wg_probe", ctx.getSessionVariable().getWorkloadGroup()); + + // (c) re-SU refused in a switched session + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("svc", "%"), + Collections.singletonList("r_svc_only"), null).run(ctx, null)); + + // reset reverts to the AUTHENTICATED identity, never widens the target + ctx.revertSessionNarrowing(); + Assertions.assertEquals("svc", ctx.getCurrentUserIdentity().getQualifiedUser()); + Assertions.assertNull(ctx.getSessionRoleOverride()); + + // the ceiling is the TARGET's grants: a role the service holds but the target does not is refused + Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("bobby", "%"), + Collections.singletonList("r_svc_only"), null).run(ctx, null)); + Assertions.assertNull(ctx.getSessionRoleOverride()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testNarrowedSessionKeepsPersonsWorkloadGroupUsage() throws Exception { + addUser("wg_person", true); + createRole("space_wg"); + createRole("space_wg_lane"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'space_wg';"); + grantRole("GRANT 'space_wg','space_wg_lane' TO 'wg_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'wg_person'@'%';"); + createWorkloadGroup("wg_person_lane"); + createWorkloadGroup("wg_via_role"); + createWorkloadGroup("wg_nobody"); + // the login shape: USAGE granted DIRECTLY to the account (its default role), not via a role + grantWorkloadGroupUsage("GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_person_lane' TO 'wg_person'@'%';"); + // a group reachable only through a role the person holds + grantWorkloadGroupUsage("GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_via_role' TO ROLE 'space_wg_lane';"); + + UserIdentity person = ident("wg_person"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(person); + ctx.setThreadLocalInfo(); + try { + // the person's own session + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + Assertions.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + + ctx.setSessionRoleOverride(Collections.singleton("space_wg")); + // data narrowing is intact: personal grant gone, requested role live + Assertions.assertFalse(canSelectDb(person, "perso")); + Assertions.assertTrue(canSelectDb(person, "test")); + // (g) the direct USAGE survives narrowing — the person's lane follows the person + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_person_lane")); + // ...and so does role-carried USAGE, even though that role was not requested + Assertions.assertTrue(canUseWorkloadGroup(person, "wg_via_role")); + // never wider than the person's own session + Assertions.assertFalse(canUseWorkloadGroup(person, "wg_nobody")); + + // the widening is scoped to the session's OWN identity: another identity's check is untouched + Assertions.assertFalse(canUseWorkloadGroup(ident("bystander_wg"), "wg_person_lane")); + } finally { + ctx.setSessionRoleOverride(null); + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testNarrowedSessionPreservesInformationSchemaButDropsDirectGrants() throws Exception { + addUser("iuser", true); + createRole("ispace"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ispace';"); + grantRole("GRANT 'ispace' TO 'iuser'@'%'"); + // a personal (default-role) grant on another db + the implicit information_schema read + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'iuser'@'%';"); + + UserIdentity iuser = ident("iuser"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(iuser); + ctx.setThreadLocalInfo(); + try { + // un-narrowed: information_schema readable (default role), personal + role grants live + Assertions.assertTrue(canSelectInfoSchema(iuser)); + Assertions.assertTrue(canSelectDb(iuser, "perso")); + + ctx.setSessionRoleOverride(Collections.singleton("ispace")); + // narrowed: the requested role's grant stays, the DIRECT/default-role grant is dropped... + Assertions.assertTrue(canSelectDb(iuser, "test")); + Assertions.assertFalse(canSelectDb(iuser, "perso")); + // ...but the implicit information_schema read IS PRESERVED (the narrowing baseline role), + // so a narrowed session can still do metadata/client operations. + Assertions.assertTrue(canSelectInfoSchema(iuser)); + + ctx.setSessionRoleOverride(null); + Assertions.assertTrue(canSelectDb(iuser, "perso")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testParserRoundTrip() { + Object plan = new NereidsParser().parseSingle( + "SU 'alice'@'%' WITH ROLES ('space_a', 'r2') WORKLOAD GROUP 'wg_space_5'"); + Assertions.assertTrue(plan instanceof SuUserCommand); + SuUserCommand cmd = (SuUserCommand) plan; + Assertions.assertEquals(2, cmd.getRoles().size()); + Assertions.assertEquals("wg_space_5", cmd.getWorkloadGroup()); + + Object plain = new NereidsParser().parseSingle("SU 'alice' WITH ROLES ('space_a')"); + Assertions.assertTrue(plain instanceof SuUserCommand); + Assertions.assertNull(((SuUserCommand) plain).getWorkloadGroup()); + + // `su` stays usable as an identifier (nonReserved) + Object select = new NereidsParser().parseSingle("SELECT su FROM t1"); + Assertions.assertNotNull(select); + } + + // SU narrowing: a BE->FE metadata RPC (getDbNames/getTableNames/listTableStatus/describeTables) + // runs on a handler thread with NO ConnectContext. Auth.setRpcSessionNarrowing lets the handler + // apply the calling session's narrowed role subset there, so information_schema name visibility + // matches the session instead of resolving the target's FULL roles (the pre-fix leak). + @Test + public void testOmittedWorkloadGroupClauseClearsSwitchersLane() throws Exception { + addUser("lane_svc", true); + addUser("lane_person", true); + createRole("lane_role"); + grantRole("GRANT 'lane_role' TO 'lane_person'@'%'"); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'lane_svc'@'%';"); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("lane_svc")); + // the switcher's own session runs in a lane of its own + ctx.getSessionVariable().setWorkloadGroup("wg_switcher_lane"); + ctx.setThreadLocalInfo(); + try { + new SuUserCommand(new UserIdentity("lane_person", "%"), + Collections.singletonList("lane_role"), null).run(ctx, null); + // no WORKLOAD GROUP clause: the switcher's lane is dropped, so the per-query + // resolution falls through to the target's default_workload_group + Assertions.assertEquals("", ctx.getSessionVariable().getWorkloadGroup()); + + ctx.revertSessionNarrowing(); + ctx.getSessionVariable().setWorkloadGroup("wg_switcher_lane"); + new SuUserCommand(new UserIdentity("lane_person", "%"), + Collections.singletonList("lane_role"), "wg_named").run(ctx, null); + // an explicit clause places the session exactly there + Assertions.assertEquals("wg_named", ctx.getSessionVariable().getWorkloadGroup()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testRpcSessionNarrowingAppliesOnMetadataRpcThread() throws Exception { + addUser("rpcuser", true); + createRole("rpc_space"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'rpc_space';"); + grantRole("GRANT 'rpc_space' TO 'rpcuser'@'%'"); + // a personal (default-role) grant on another db, which the narrowing must DROP + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'rpcuser'@'%';"); + UserIdentity rpcuser = ident("rpcuser"); + try { + // no narrowing installed -> full roles resolved (the pre-fix behavior/leak): + // both the personal grant and the role grant are visible. + Assertions.assertTrue(canSelectDb(rpcuser, "perso")); + Assertions.assertTrue(canSelectDb(rpcuser, "test")); + + // install exactly what the metadata RPC carries in current_roles + Auth.setRpcSessionNarrowing(rpcuser, Collections.singleton("rpc_space")); + Assertions.assertFalse(canSelectDb(rpcuser, "perso")); // personal grant DROPPED + Assertions.assertTrue(canSelectDb(rpcuser, "test")); // requested role KEPT + Assertions.assertTrue(canSelectInfoSchema(rpcuser)); // info_schema baseline preserved + } finally { + Auth.clearRpcSessionNarrowing(); + } + // cleared -> full roles restored (no leak of the narrowing to later requests on this thread) + Assertions.assertTrue(canSelectDb(rpcuser, "perso")); + } + + /** + * PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the built-in accounts are never a target, the + * built-in administrative roles and the per-user default role are never requested, and a role that + * confers a global ADMIN_PRIV / NODE_PRIV / GRANT_PRIV is refused unless the switching account + * already holds that bit itself. + */ + @Test + public void testSwitchNeverClimbsTheAdministrativeLadder() throws Exception { + addUser("ladder_person", true); + addUser("ladder_admin_holder", true); + createRole("ladder_plain"); + createRole("ladder_ops_admin"); + createRole("ladder_granter"); + grantRole("GRANT 'ladder_plain','ladder_ops_admin','ladder_granter' TO 'ladder_person'@'%'"); + grantRole("GRANT 'admin' TO 'ladder_admin_holder'@'%'"); + grantPriv("GRANT ADMIN_PRIV ON *.*.* TO ROLE 'ladder_ops_admin';"); + grantPriv("GRANT GRANT_PRIV ON *.*.* TO ROLE 'ladder_granter';"); + UserIdentity person = new UserIdentity("ladder_person", "%"); + + ConnectContext ctx = proxySession("ladder_svc"); + try { + // the built-in accounts are never a target, whatever role is named + assertSwitchRefused(ctx, new UserIdentity("admin", "%"), "admin"); + assertSwitchRefused(ctx, new UserIdentity("root", "%"), "operator"); + // the built-in administrative roles and the default role are never requested, even when + // the target holds them + assertSwitchRefused(ctx, new UserIdentity("ladder_admin_holder", "%"), "admin"); + assertSwitchRefused(ctx, person, "operator"); + assertSwitchRefused(ctx, person, "default_role_rbac_ladder_person@%"); + // a granted role that confers a global administrative bit the switcher does not hold + assertSwitchRefused(ctx, person, "ladder_ops_admin"); + assertSwitchRefused(ctx, person, "ladder_granter"); + // an identity that does not analyze never reaches the switch + assertSwitchRefused(ctx, new UserIdentity("bad user", "%"), "ladder_plain"); + // the plain role is what PROXY_PRIV buys + new SuUserCommand(person, Collections.singletonList("ladder_plain"), null).run(ctx, null); + Assertions.assertEquals("ladder_person", ctx.getQualifiedUser()); + Assertions.assertFalse(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.ADMIN)); + Assertions.assertFalse(Env.getCurrentEnv().getAccessManager().checkGlobalPriv(ctx, PrivPredicate.GRANT)); + } finally { + connectContext.setThreadLocalInfo(); + } + + // an administrator already holds every bit: narrowing a person to an administrative role is + // no escalation for it + ConnectContext admin = new ConnectContext(); + admin.setCurrentUserIdentity(ident("root")); + admin.setThreadLocalInfo(); + try { + new SuUserCommand(person, Collections.singletonList("ladder_ops_admin"), null).run(admin, null); + Assertions.assertEquals("ladder_person", admin.getQualifiedUser()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * After the switch the current identity IS the target, but the switcher is not the person: the + * "self" exemptions of SET PASSWORD and SET PROPERTY are closed under the switch, so a credential + * or account-default change needs the GRANT / ADMIN privilege of the narrowed set. + */ + @Test + public void testSelfBranchesAreClosedUnderTheSwitch() throws Exception { + addUser("self_person", true); + createRole("self_role"); + grantRole("GRANT 'self_role' TO 'self_person'@'%'"); + UserIdentity person = new UserIdentity("self_person", "%"); + + // the person's own session keeps both self branches + ConnectContext own = new ConnectContext(); + own.setCurrentUserIdentity(ident("self_person")); + own.setThreadLocalInfo(); + try { + new SetPassVarOp(null, new PassVar("", true)).validate(own); + new SetPassVarOp(new UserIdentity("self_person", "%"), new PassVar("", true)).validate(own); + new SetUserPropertyVarOp("self_person", "default_init_catalog", "internal").validate(own); + } finally { + connectContext.setThreadLocalInfo(); + } + + ConnectContext ctx = proxySession("self_svc"); + try { + new SuUserCommand(person, Collections.singletonList("self_role"), null).run(ctx, null); + Assertions.assertEquals("self_person", ctx.getQualifiedUser()); + Assertions.assertThrows(AnalysisException.class, () -> + new SetPassVarOp(null, new PassVar("", true)).validate(ctx)); + Assertions.assertThrows(AnalysisException.class, () -> + new SetPassVarOp(new UserIdentity("self_person", "%"), new PassVar("", true)).validate(ctx)); + Assertions.assertThrows(AnalysisException.class, () -> + new SetUserPropertyVarOp("self_person", "default_init_catalog", "internal").validate(ctx)); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * The narrowed set is a live intersection with the target's grants, not a snapshot taken at the + * switch: a role revoked from the target afterwards (or dropped and re-created under its name, + * or every role after DROP USER) drops out of the open narrowed session on the next check, + * exactly as it would for the person's own session. SHOW GRANTS lists that same set, and never + * the synthetic baseline role. + */ + @Test + public void testCeilingIsReEvaluatedOnEveryCheck() throws Exception { + addUser("ceil_person", true); + addUser("ceil_gone", true); + createRole("ceil_role"); + createRole("ceil_gone_role"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_role';"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_gone_role';"); + grantRole("GRANT 'ceil_role' TO 'ceil_person'@'%'"); + grantRole("GRANT 'ceil_gone_role' TO 'ceil_gone'@'%'"); + UserIdentity person = ident("ceil_person"); + UserIdentity gone = ident("ceil_gone"); + + ConnectContext ctx = proxySession("ceil_svc"); + try { + new SuUserCommand(new UserIdentity("ceil_person", "%"), + Collections.singletonList("ceil_role"), null).run(ctx, null); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertEquals("ceil_role", shownRoles(person)); + + // a REVOKE from the target is honoured by the open narrowed session at once + runAsAdmin("REVOKE 'ceil_role' FROM 'ceil_person'@'%'"); + Assertions.assertFalse(canSelectDb(person, "test")); + // the information_schema/mysql baseline stays, and is not listed as a role + Assertions.assertTrue(canSelectInfoSchema(person)); + Assertions.assertEquals("", shownRoles(person)); + + // DROP ROLE + CREATE ROLE under the same name is not a grant to the target + runAsAdmin("DROP ROLE ceil_role"); + runAsAdmin("CREATE ROLE ceil_role"); + runAsAdmin("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'ceil_role'"); + Assertions.assertFalse(canSelectDb(person, "test")); + // granting it to the target again is + runAsAdmin("GRANT 'ceil_role' TO 'ceil_person'@'%'"); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertEquals("ceil_role", shownRoles(person)); + } finally { + connectContext.setThreadLocalInfo(); + } + + ConnectContext ctx2 = proxySession("ceil_svc2"); + try { + new SuUserCommand(new UserIdentity("ceil_gone", "%"), + Collections.singletonList("ceil_gone_role"), null).run(ctx2, null); + Assertions.assertTrue(canSelectDb(gone, "test")); + // a dropped target holds nothing: the session keeps only the metadata baseline + runAsAdmin("DROP USER 'ceil_gone'@'%'"); + Assertions.assertFalse(canSelectDb(gone, "test")); + Assertions.assertTrue(canSelectInfoSchema(gone)); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSyntheticBaselineRoleCannotBeNamed() { + // the default-role prefix keeps it out of CREATE ROLE, out of row-policy binding (which + // validates the role) and out of every role listing + Assertions.assertTrue(Auth.SU_NARROWING_BASELINE_ROLE.startsWith(RoleManager.DEFAULT_ROLE_PREFIX)); + Assertions.assertThrows(Exception.class, () -> runAsAdmin("CREATE ROLE " + Auth.SU_NARROWING_BASELINE_ROLE)); + Assertions.assertFalse(Env.getCurrentEnv().getAuth().doesRoleExist(Auth.SU_NARROWING_BASELINE_ROLE)); + } + + /** + * The authorization-SPI role bridge (what a ranger-hive or third-party source is told when it asks + * who holds which roles) resolves through the same choke point as the built-in checks: a narrowed + * session's override, and the narrowing a BE->FE RPC carries, reach role-bound policies of every + * source; the per-user default role and the synthetic baseline never do. + */ + @Test + public void testAuthorizationSpiRoleBridgeIsNarrowed() throws Exception { + addUser("spi_person", true); + createRole("spi_tenant_a"); + createRole("spi_tenant_b"); + grantRole("GRANT 'spi_tenant_a','spi_tenant_b' TO 'spi_person'@'%'"); + UserIdentity person = ident("spi_person"); + EngineAuthorizationContext bridge = new EngineAuthorizationContext( + Env.getCurrentEnv().getAccessManager(), Env.getCurrentEnv().getAuth()); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(person); + ctx.setThreadLocalInfo(); + try { + Assertions.assertEquals(Sets.newHashSet("spi_tenant_a", "spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + ctx.setSessionRoleOverride(Collections.singleton("spi_tenant_a")); + Assertions.assertEquals(Collections.singleton("spi_tenant_a"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + // another identity's roles are untouched by this session's narrowing + Assertions.assertTrue(bridge.rolesOf(AccessTranslation.subjectOf(ident("bystander"))).isEmpty()); + } finally { + connectContext.setThreadLocalInfo(); + } + + // the narrowing a metadata RPC carries (no ConnectContext on the handler thread) + Auth.setRpcSessionNarrowing(person, Collections.singleton("spi_tenant_b")); + try { + Assertions.assertEquals(Collections.singleton("spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + } finally { + Auth.clearRpcSessionNarrowing(); + } + Assertions.assertEquals(Sets.newHashSet("spi_tenant_a", "spi_tenant_b"), + bridge.rolesOf(AccessTranslation.subjectOf(person))); + } + + /** + * The placement widening answers "may this session run here?" and nothing else: a direct + * GRANT_PRIV on a workload group (which lives in the person's default role, outside the + * narrowed set) lets the person's own session grant the group to others, but never the narrowed + * session -- GRANT/REVOKE ... ON WORKLOAD GROUP stays on the narrowed set. Both legs of the + * answer, the live session and the narrowing a BE->FE RPC carries, agree. + */ + @Test + public void testPlacementWideningNeverReachesTheGrantGate() throws Exception { + addUser("wg_granter", true); + addUser("wg_mallory", true); + createRole("wg_tenant"); + grantRole("GRANT 'wg_tenant' TO 'wg_granter'@'%'"); + createWorkloadGroup("wg_x"); + grantWorkloadGroupUsage("GRANT USAGE_PRIV, GRANT_PRIV ON WORKLOAD GROUP 'wg_x' TO 'wg_granter'@'%';"); + UserIdentity granter = ident("wg_granter"); + PrivPredicate grantGate = PrivPredicate.of(PrivBitSet.of(Privilege.USAGE_PRIV, Privilege.GRANT_PRIV), + Operator.AND); + Auth auth = Env.getCurrentEnv().getAuth(); + + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(granter); + ctx.setThreadLocalInfo(); + try { + // the person's own session may use the group and grant it + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + Assertions.assertTrue(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + + ctx.setSessionRoleOverride(Collections.singleton("wg_tenant")); + // placement follows the person ... + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + // ... but the GRANT gate stays on the narrowed set: the direct GRANT_PRIV is inert + Assertions.assertFalse(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + Assertions.assertThrows(AnalysisException.class, () -> grantWorkloadGroupUsage( + "GRANT USAGE_PRIV ON WORKLOAD GROUP 'wg_x' TO 'wg_mallory'@'%';")); + Assertions.assertFalse(canUseWorkloadGroup(ident("wg_mallory"), "wg_x")); + } finally { + connectContext.setThreadLocalInfo(); + } + + // the RPC leg (no ConnectContext on the handler thread) answers the same + Auth.setRpcSessionNarrowing(granter, Collections.singleton("wg_tenant")); + try { + Assertions.assertTrue(canUseWorkloadGroup(granter, "wg_x")); + Assertions.assertFalse(auth.checkWorkloadGroupPriv(granter, "wg_x", grantGate)); + } finally { + Auth.clearRpcSessionNarrowing(); + } + } + + /** + * A persisted job re-plans its read later, on a job thread, under the stored identity alone, + * where the session's narrowing cannot follow: a narrowed session mints none (EXPORT, CREATE JOB + * in both forms), and a routine load's KEY references are authorized at creation under the live + * session. A child context built for a task thread inherits the narrowing with the identity. + */ + @Test + public void testNarrowedSessionMintsNoPersistedExecutor() throws Exception { + createTable("create table test.su_job_t (k int) distributed by hash(k) buckets 1 " + + "properties('replication_num' = '1')"); + addUser("job_person", true); + createRole("job_role"); + grantPriv("GRANT SELECT_PRIV, LOAD_PRIV ON internal.test.* TO ROLE 'job_role';"); + grantRole("GRANT 'job_role' TO 'job_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'job_person'@'%';"); + UserIdentity person = ident("job_person"); + + ConnectContext ctx = proxySession("job_svc"); + try { + new SuUserCommand(new UserIdentity("job_person", "%"), + Collections.singletonList("job_role"), null).run(ctx, null); + ctx.setDatabase("test"); + + LogicalPlan export = new NereidsParser().parseSingle( + "EXPORT TABLE test.su_job_t TO \"file:///tmp/su_job/\""); + Assertions.assertTrue(export instanceof ExportCommand); + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, + () -> ((ExportCommand) export).run(ctx, null)); + Assertions.assertTrue(refused.getMessage().contains("session-narrowed"), refused.getMessage()); + + for (String sql : new String[] { + "CREATE JOB su_job_s ON SCHEDULE EVERY 1 DAY DO INSERT INTO test.su_job_t" + + " SELECT * FROM test.su_job_t", + "CREATE JOB su_job_f ON STREAMING DO INSERT INTO test.su_job_t" + + " SELECT * FROM S3(\"uri\" = \"s3://bucket/x.csv\", \"format\" = \"csv\")"}) { + LogicalPlan job = new NereidsParser().parseSingle(sql); + Assertions.assertTrue(job instanceof CreateJobCommand, sql); + AnalysisException jobRefused = Assertions.assertThrows(AnalysisException.class, + () -> ((CreateJobCommand) job).run(ctx, null)); + Assertions.assertTrue(jobRefused.getMessage().contains("session-narrowed"), jobRefused.getMessage()); + } + + // the KEY reference of a routine-load column mapping is authorized at creation, as the + // narrowed session: SHOW on perso lives only in the excluded direct grant + Expression mapping = new NereidsParser().parseExpression("aes_decrypt(c1, KEY perso.k)"); + Assertions.assertThrows(AnalysisException.class, + () -> CreateRoutineLoadInfo.checkEncryptKeyRefs(mapping, ctx)); + CreateRoutineLoadInfo.checkEncryptKeyRefs( + new NereidsParser().parseExpression("aes_decrypt(c1, KEY test.k)"), ctx); + + // a child context of the narrowed session (a task thread) denies what the session denies + ConnectContext child = new ConnectContext(); + child.inheritIdentityFrom(ctx); + child.setThreadLocalInfo(); + try { + Assertions.assertEquals("job_svc", child.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertTrue(canSelectDb(person, "test")); + Assertions.assertFalse(canSelectDb(person, "perso")); + } finally { + ctx.setThreadLocalInfo(); + } + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * The account state a login checks is checked by the switch too: a target locked by ACCOUNT_LOCK + * (or by failed logins, or with an expired password) cannot be assumed until it is unlocked. + */ + @Test + public void testLockedTargetCannotBeAssumed() throws Exception { + addUser("locked_person", true); + createRole("locked_role"); + grantRole("GRANT 'locked_role' TO 'locked_person'@'%'"); + UserIdentity person = ident("locked_person"); + PasswordPolicyManager policies = Env.getCurrentEnv().getAuth().getPasswdPolicyManager(); + + ConnectContext ctx = proxySession("locked_svc"); + try { + policies.lockUser(person); + try { + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, () -> + new SuUserCommand(new UserIdentity("locked_person", "%"), + Collections.singletonList("locked_role"), null).run(ctx, null)); + Assertions.assertTrue(refused.getMessage().contains("cannot be assumed"), refused.getMessage()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + } finally { + policies.unlockUser(person); + } + new SuUserCommand(new UserIdentity("locked_person", "%"), + Collections.singletonList("locked_role"), null).run(ctx, null); + Assertions.assertEquals("locked_person", ctx.getQualifiedUser()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + /** + * What the session cached for one identity does not survive the switch or its end: the + * prepared statements (their plans were authorized under the switcher) go at the switch; the + * compute group chosen for the current identity goes at the switch and when it is left. + */ + @Test + public void testSwitchDropsTheSwitchersPreparedStatementsAndComputeGroup() throws Exception { + addUser("cache_person", true); + createRole("cache_role"); + grantRole("GRANT 'cache_role' TO 'cache_person'@'%'"); + + ConnectContext ctx = proxySession("cache_svc"); + try { + ctx.addPreparedStatementContext("stmt_of_switcher", null); + Map prepared = Deencapsulation.getField(ctx, "preparedStatementContextMap"); + Assertions.assertEquals(1, prepared.size()); + Deencapsulation.setField(ctx, "cloudCluster", "group_of_switcher"); + + new SuUserCommand(new UserIdentity("cache_person", "%"), + Collections.singletonList("cache_role"), null).run(ctx, null); + Assertions.assertTrue(prepared.isEmpty()); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + + // the group the target's statements chose goes when the switch is left, either way + Deencapsulation.setField(ctx, "cloudCluster", "group_of_target"); + ctx.revertSessionNarrowing(); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + + new SuUserCommand(new UserIdentity("cache_person", "%"), + Collections.singletonList("cache_role"), null).run(ctx, null); + Deencapsulation.setField(ctx, "cloudCluster", "group_of_target"); + ctx.clearSessionNarrowing(); + Assertions.assertNull(Deencapsulation.getField(ctx, "cloudCluster")); + } finally { + connectContext.setThreadLocalInfo(); + } + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java index be328b7317ac43..01f0a6a5b7bbfe 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/rules/expression/FoldConstantTest.java @@ -17,6 +17,7 @@ package org.apache.doris.nereids.rules.expression; +import org.apache.doris.analysis.UserIdentity; import org.apache.doris.nereids.analyzer.UnboundRelation; import org.apache.doris.nereids.exceptions.AnalysisException; import org.apache.doris.nereids.exceptions.CastException; @@ -94,6 +95,8 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.Sec; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondMicrosecondAdd; import org.apache.doris.nereids.trees.expressions.functions.scalar.SecondsAdd; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionIsNarrowed; +import org.apache.doris.nereids.trees.expressions.functions.scalar.SessionUser; import org.apache.doris.nereids.trees.expressions.functions.scalar.ShortCircuitIf; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sign; import org.apache.doris.nereids.trees.expressions.functions.scalar.Sin; @@ -107,6 +110,7 @@ import org.apache.doris.nereids.trees.expressions.functions.scalar.TimeFormat; import org.apache.doris.nereids.trees.expressions.functions.scalar.ToDays; import org.apache.doris.nereids.trees.expressions.functions.scalar.UnixTimestamp; +import org.apache.doris.nereids.trees.expressions.functions.scalar.User; import org.apache.doris.nereids.trees.expressions.literal.ArrayLiteral; import org.apache.doris.nereids.trees.expressions.literal.BigIntLiteral; import org.apache.doris.nereids.trees.expressions.literal.BooleanLiteral; @@ -144,6 +148,7 @@ import org.apache.doris.nereids.types.VarcharType; import org.apache.doris.nereids.types.VariantType; import org.apache.doris.nereids.util.MemoTestUtils; +import org.apache.doris.qe.ConnectContext; import org.apache.doris.qe.SessionVariable; import com.google.common.collect.ImmutableList; @@ -152,9 +157,11 @@ import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.lang.reflect.Field; import java.math.BigDecimal; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; +import java.util.Collections; import java.util.List; class FoldConstantTest extends ExpressionRewriteTestHelper { @@ -1906,4 +1913,57 @@ private void assertInvalidCast(String expression, String expectedMessage) { () -> new ExpressionNormalization().rewrite(analyzed, context)); Assertions.assertEquals(expectedMessage, exception.getMessage()); } + + @Test + public void testSessionIsNarrowedFoldsOnlyWithSessionContext() throws Exception { + SessionIsNarrowed fn = new SessionIsNarrowed(); + // the context-free fold (expressions that must not depend on the session) leaves it alone, + // like current_user() + Field contextFreeField = FoldConstantRuleOnFE.class.getDeclaredField("CONTEXT_FREE_VISITOR_INSTANCE"); + contextFreeField.setAccessible(true); + FoldConstantRuleOnFE contextFree = (FoldConstantRuleOnFE) contextFreeField.get(null); + Assertions.assertSame(fn, contextFree.rewrite(fn, context)); + // the session-aware fold answers from the connection's SU state + Assertions.assertEquals(BooleanLiteral.FALSE, + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(fn, context)); + cascadesContext.getConnectContext().setSessionRoleOverride(Collections.singleton("tenant")); + try { + Assertions.assertEquals(BooleanLiteral.TRUE, + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(fn, context)); + } finally { + cascadesContext.getConnectContext().setSessionRoleOverride(null); + } + } + + /** + * MySQL proxy parity under SU: user() names the account that authenticated the connection (the + * switcher), while session_user() and current_user() name the effective identity (the target). + */ + @Test + public void testUserNamesTheAuthenticatingAccountUnderSu() { + ConnectContext ctx = cascadesContext.getConnectContext(); + UserIdentity before = ctx.getCurrentUserIdentity(); + UserIdentity person = UserIdentity.createAnalyzedUserIdentWithIp("person", "%"); + String personAtIp = UserIdentity.createAnalyzedUserIdentWithIp("person", ctx.getRemoteIP()).toString(); + String svcAtIp = UserIdentity.createAnalyzedUserIdentWithIp("svc", ctx.getRemoteIP()).toString(); + ctx.setCurrentUserIdentity(person); + try { + Assertions.assertEquals(new VarcharLiteral(personAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new User(), context)); + + ctx.setAuthenticatedIdentity(UserIdentity.createAnalyzedUserIdentWithIp("svc", "%")); + ctx.setSessionRoleOverride(Collections.singleton("tenant")); + Assertions.assertEquals(new VarcharLiteral(svcAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new User(), context)); + Assertions.assertEquals(new VarcharLiteral(personAtIp), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite(new SessionUser(), context)); + Assertions.assertEquals(new VarcharLiteral(person.toString()), + FoldConstantRuleOnFE.VISITOR_INSTANCE.rewrite( + new org.apache.doris.nereids.trees.expressions.functions.scalar.CurrentUser(), context)); + } finally { + ctx.setSessionRoleOverride(null); + ctx.setAuthenticatedIdentity(null); + ctx.setCurrentUserIdentity(before); + } + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java index f8eb5bff3ff2f1..d4493fcea77009 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/nereids/trees/plans/commands/ShowProcessListCommandTest.java @@ -17,9 +17,13 @@ package org.apache.doris.nereids.trees.plans.commands; +import org.apache.doris.analysis.UserIdentity; import org.apache.doris.catalog.SchemaTable; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.thrift.TShowProcessListRequest; import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -37,6 +41,29 @@ public void testMetaDataIsTheProcesslistSchemaTable() { Assertions.assertEquals("Protocol", names.get(names.size() - 1)); } + // The request the other frontends answer names who is asking and, for a session-narrowed (SU) + // session, the role subset it runs under, so their ADMIN check is narrowed like the local one. + @Test + public void testRequestNamesTheNarrowedRoleSubset() { + UserIdentity alice = UserIdentity.createAnalyzedUserIdentWithIp("alice", "%"); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(alice); + + TShowProcessListRequest plain = ShowProcessListCommand.buildRequest(ctx, true); + Assertions.assertTrue(plain.isShowFullSql()); + Assertions.assertEquals(alice.toThrift(), plain.getCurrentUserIdent()); + Assertions.assertFalse(plain.isSetCurrentRoles()); + + ctx.setAuthenticatedIdentity(UserIdentity.createAnalyzedUserIdentWithIp("svc", "%")); + ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a")); + TShowProcessListRequest narrowed = ShowProcessListCommand.buildRequest(ctx, false); + Assertions.assertFalse(narrowed.isShowFullSql()); + // the identity the other FE checks is still the session's (switched-to) identity ... + Assertions.assertEquals(alice.toThrift(), narrowed.getCurrentUserIdent()); + // ... narrowed to the subset the session runs under + Assertions.assertEquals(Sets.newHashSet("tenant_a"), narrowed.getCurrentRoles()); + } + // SHOW PROCESSLIST merges the rows the other frontends answer with; while the cluster runs two // versions those rows have another number of columns, and the result set must stay rectangular. @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java index 21590db8fd3181..29c61964ee9368 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/plugin/audit/AuditLoaderTest.java @@ -122,6 +122,27 @@ public void testDelimiterInjectionDoesNotAlterFraming() { Assertions.assertTrue(evil.toString().contains("DROP TABLE finance.ledger")); } + // The row carries exactly one field per audit_log column, in InternalSchema.AUDIT_SCHEMA order + // (the loader's `columns` header maps by that order), and the account that authenticated the + // connection rides right beside the effective user. + @Test + public void testAuthenticatedUserRidesBesideUser() { + AuditLoader auditLoader = new AuditLoader(); + StringBuilder buffer = new StringBuilder(); + Deencapsulation.invoke(auditLoader, "fillLogBuffer", + new AuditEvent.AuditEventBuilder() + .setClientIp("10.0.0.9").setUser("alice").setAuthenticatedUser("svc_gateway") + .setDb("sales").setStmt("select 1").build(), + buffer); + List names = InternalSchema.AUDIT_SCHEMA.stream().map(ColumnDef::getName) + .collect(Collectors.toList()); + String[] fields = buffer.toString().split(String.valueOf(AuditLoader.AUDIT_TABLE_COL_SEPARATOR), -1); + Assertions.assertEquals(names.size(), fields.length, "one field per audit_log column"); + Assertions.assertEquals(names.indexOf("user") + 1, names.indexOf("authenticated_user")); + Assertions.assertEquals("alice", fields[names.indexOf("user")]); + Assertions.assertEquals("svc_gateway", fields[names.indexOf("authenticated_user")]); + } + // The sanitizer must be a no-op for ordinary statements: no data loss, no mutation. @Test public void testCleanStatementIsPreserved() { diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java index 1eb6eb4791f20b..bdd4550bec985c 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/AuditLogHelperTest.java @@ -110,4 +110,27 @@ public void testGetCloudClusterForAuditPrefersEffectiveCluster() throws Exceptio Assertions.assertEquals("hint_cluster", AuditLogHelper.getCloudClusterForAudit(ctx)); } + + @Test + public void testAuthenticatedUserFollowsTheSwitcher() { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("alice", "%")); + // an ordinary session: the account that authenticated is the session user + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // a session-narrowed (SU) session: the effective identity is the target, the switcher stays + // the authenticated account -- the audit row names both + ctx.setAuthenticatedIdentity(ctx.getCurrentUserIdentity()); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("bob", "%")); + Assertions.assertEquals("bob", ctx.getQualifiedUser()); + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // reverting the switch restores the plain mapping + ctx.revertSessionNarrowing(); + Assertions.assertEquals("alice", ctx.getQualifiedUser()); + Assertions.assertEquals("alice", AuditLogHelper.authenticatedUser(ctx)); + + // never null, even before any identity is set + Assertions.assertEquals("", AuditLogHelper.authenticatedUser(new ConnectContext())); + } } diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java new file mode 100644 index 00000000000000..bed37551293b8c --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/ForwardedSessionNarrowingTest.java @@ -0,0 +1,243 @@ +// 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.doris.qe; + +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.common.AnalysisException; +import org.apache.doris.common.AuthorizationException; +import org.apache.doris.common.Config; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.ha.FrontendNodeType; +import org.apache.doris.mysql.MysqlCapability; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.persist.HbPackage; +import org.apache.doris.persist.gson.GsonUtils; +import org.apache.doris.system.Frontend; +import org.apache.doris.system.FrontendHbResponse; +import org.apache.doris.thrift.TMasterOpRequest; +import org.apache.doris.thrift.TNetworkAddress; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.Collections; + +/** + * A session-narrowed (SU) session forwarding a statement to the master: the origin FE carries the + * session's active role subset in TMasterOpRequest (is_su_user + current_roles) and the master + * installs it on the proxy session before executing, so the statement is authorized against exactly + * that set and never the target's full role union. + */ +public class ForwardedSessionNarrowingTest extends TestWithFeService { + + private static final String CTL = InternalCatalog.INTERNAL_CATALOG_NAME; + + @Override + protected void runBeforeAll() throws Exception { + // GRANT validates database existence on the internal catalog + createDatabase("test"); + createDatabase("perso"); + } + + private UserIdentity ident(String name) { + return UserIdentity.createAnalyzedUserIdentWithIp(name, "%"); + } + + private boolean canSelectDb(UserIdentity user, String db) { + return Env.getCurrentEnv().getAccessManager().checkDbPriv(user, CTL, db, PrivPredicate.SELECT); + } + + private static TMasterOpRequest forwardRequestOf(ConnectContext ctx) throws AnalysisException { + return new ForwardParamsProbe(ctx, selfAddress()).build(); + } + + /** The single test FE, as an FE address: the one receiver that is known to run this build. */ + private static TNetworkAddress selfAddress() { + return new TNetworkAddress(Env.getCurrentEnv().getSelfNode().getHost(), Config.rpc_port); + } + + /** Exposes the request the origin FE would send for this session to the FE at the address. */ + private static class ForwardParamsProbe extends FEOpExecutor { + private ForwardParamsProbe(ConnectContext ctx, TNetworkAddress receiver) { + super(receiver, new OriginStatement("show frontends", 0), ctx, true); + } + + private TMasterOpRequest build() throws AnalysisException { + return buildStmtForwardParams(); + } + } + + @Test + public void testOriginCarriesNarrowingOnlyWhenSwitched() throws Exception { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("alice")); + ctx.setRemoteIP("127.0.0.1"); + // the forward request carries the client's negotiated capabilities; a bare test context has none + ctx.setCapability(MysqlCapability.DEFAULT_CAPABILITY); + + TMasterOpRequest plain = forwardRequestOf(ctx); + Assertions.assertFalse(plain.isSetIsSuUser()); + Assertions.assertFalse(plain.isSetCurrentRoles()); + Assertions.assertFalse(plain.isSetAuthenticatedUserIdent()); + + ctx.setAuthenticatedIdentity(ident("svc")); + ctx.setSessionRoleOverride(Sets.newHashSet("tenant_a", "tenant_a_scoped")); + TMasterOpRequest narrowed = forwardRequestOf(ctx); + Assertions.assertTrue(narrowed.isSetIsSuUser() && narrowed.isIsSuUser()); + Assertions.assertEquals(Sets.newHashSet("tenant_a", "tenant_a_scoped"), narrowed.getCurrentRoles()); + // the identity the master rebuilds is still the session's (switched-to) identity ... + Assertions.assertEquals(ctx.getQualifiedUser(), narrowed.getUser()); + // ... and the account that authenticated the connection rides along for the master's audit row + Assertions.assertEquals(ident("svc").toThrift(), narrowed.getAuthenticatedUserIdent()); + } + + @Test + public void testMasterInstallsCarriedNarrowingOnProxySession() throws Exception { + addUser("bob", true); + createRole("tenant_b"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_b';"); + grantRole("GRANT 'tenant_b' TO 'bob'@'%'"); + // a personal (default-role) grant the narrowing must drop on the master as well + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'bob'@'%';"); + UserIdentity bob = ident("bob"); + + TMasterOpRequest request = new TMasterOpRequest(); + request.setIsSuUser(true); + request.setCurrentRoles(Sets.newHashSet("tenant_b")); + request.setAuthenticatedUserIdent(ident("svc").toThrift()); + + ConnectContext proxy = new ConnectContext(); + proxy.setCurrentUserIdentity(bob); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, request); + Assertions.assertEquals(Collections.singleton("tenant_b"), proxy.getSessionRoleOverride()); + // the master's audit row names the switcher, the effective identity stays the target + Assertions.assertEquals("svc", proxy.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals("bob", proxy.getCurrentUserIdentity().getQualifiedUser()); + + proxy.setThreadLocalInfo(); + try { + Assertions.assertFalse(canSelectDb(bob, "perso")); // personal grant dropped + Assertions.assertTrue(canSelectDb(bob, "test")); // carried role kept + } finally { + connectContext.setThreadLocalInfo(); + } + // the same identity checked outside the proxy session sees its full union + Assertions.assertTrue(canSelectDb(bob, "perso")); + } + + /** + * A switched request that names no role subset (or an empty one) is refused, never narrowed to + * nothing: an installed override is never empty, so "absent = not narrowed" is the one meaning of + * current_roles on every side of the wire. + */ + @Test + public void testMasterRefusesASwitchedRequestWithoutRoles() { + TMasterOpRequest noRoles = new TMasterOpRequest(); + noRoles.setIsSuUser(true); + ConnectContext proxy = new ConnectContext(); + Assertions.assertThrows(AuthorizationException.class, + () -> ConnectProcessor.applyForwardedSessionNarrowing(proxy, noRoles)); + Assertions.assertFalse(proxy.isSessionRoleNarrowed()); + + TMasterOpRequest emptyRoles = new TMasterOpRequest(); + emptyRoles.setIsSuUser(true); + emptyRoles.setCurrentRoles(Sets.newHashSet()); + Assertions.assertThrows(AuthorizationException.class, + () -> ConnectProcessor.applyForwardedSessionNarrowing(proxy, emptyRoles)); + Assertions.assertFalse(proxy.isSessionRoleNarrowed()); + } + + @Test + public void testUnswitchedRequestLeavesProxySessionUnnarrowed() throws Exception { + ConnectContext proxy = new ConnectContext(); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, new TMasterOpRequest()); + Assertions.assertNull(proxy.getSessionRoleOverride()); + Assertions.assertNull(proxy.getAuthenticatedIdentity()); + + // a role list without the switched flag is not a narrowing + TMasterOpRequest rolesOnly = new TMasterOpRequest(); + rolesOnly.setCurrentRoles(Sets.newHashSet("tenant_c")); + ConnectProcessor.applyForwardedSessionNarrowing(proxy, rolesOnly); + Assertions.assertNull(proxy.getSessionRoleOverride()); + } + + @Test + public void testMasterItselfAlwaysRunsTheSameBuild() { + // the single-FE test environment is its own master: nothing to forward, nothing to refuse + Assertions.assertTrue(Env.getCurrentEnv().isMaster()); + Assertions.assertTrue(Env.getCurrentEnv().masterRunsSameBuild()); + TNetworkAddress self = selfAddress(); + Assertions.assertTrue(Env.getCurrentEnv().frontendRunsSameBuild(self.getHostname(), self.getPort())); + // an FE this FE does not know counts as a different build + Assertions.assertFalse(Env.getCurrentEnv().frontendRunsSameBuild("203.0.113.9", 9020)); + } + + /** + * The same-build gate sits at the single place that sets is_su_user, so every SQL forward of a + * narrowed session -- to the master, or for KILL QUERY to any other FE -- fails closed unless the + * receiving FE is known to run this build. A plain session forwards anywhere. + */ + @Test + public void testNarrowedForwardToAnFeOfUnknownBuildIsRefused() throws Exception { + TNetworkAddress unknown = new TNetworkAddress("203.0.113.9", 9020); + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(ident("alice")); + ctx.setRemoteIP("127.0.0.1"); + ctx.setCapability(MysqlCapability.DEFAULT_CAPABILITY); + Assertions.assertFalse(new ForwardParamsProbe(ctx, unknown).build().isSetIsSuUser()); + + ctx.setAuthenticatedIdentity(ident("svc")); + ctx.setSessionRoleOverride(Collections.singleton("tenant_a")); + AnalysisException refused = Assertions.assertThrows(AnalysisException.class, + () -> new ForwardParamsProbe(ctx, unknown).build()); + Assertions.assertTrue(refused.getMessage().contains("different build"), refused.getMessage()); + // the FE known to run this build still takes the narrowed forward + Assertions.assertTrue(forwardRequestOf(ctx).isIsSuUser()); + } + + /** + * A non-master FE learns the master's build only from the HbPackage journal the master writes + * after every heartbeat round: the version must survive the Gson round trip of that journal and + * land on the replayed Frontend, or masterRunsSameBuild() is false off-master forever. + */ + @Test + public void testHeartbeatJournalCarriesTheBuildToReplayingFrontends() { + String build = Frontend.localBuildVersion(); + FrontendHbResponse hb = new FrontendHbResponse("fe_probe", 9030, 9020, 0, 7L, System.currentTimeMillis(), + build, 0L, Collections.emptyList(), 1L, ""); + FrontendHbResponse roundTripped = GsonUtils.GSON.fromJson(GsonUtils.GSON.toJson(hb), + FrontendHbResponse.class); + Assertions.assertEquals(build, roundTripped.getVersion()); + + HbPackage hbPackage = new HbPackage(); + hbPackage.addHbResponse(hb); + HbPackage replayed = GsonUtils.GSON.fromJson(GsonUtils.GSON.toJson(hbPackage), HbPackage.class); + Assertions.assertEquals(1, replayed.getHbResults().size()); + FrontendHbResponse fromJournal = (FrontendHbResponse) replayed.getHbResults().get(0); + + // what a follower's HeartbeatMgr.replayHearbeat does with the journaled response + Frontend fe = new Frontend(FrontendNodeType.FOLLOWER, "fe_probe", "203.0.113.9", 9010); + Assertions.assertTrue(fe.handleHbResponse(fromJournal, true)); + Assertions.assertTrue(fe.isAlive()); + Assertions.assertEquals(build, fe.getVersion()); + Assertions.assertEquals(9020, fe.getRpcPort()); + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java new file mode 100644 index 00000000000000..8a86358ff082f2 --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/qe/MysqlConnectProcessorSuTest.java @@ -0,0 +1,200 @@ +// 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.doris.qe; + +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.mysql.MysqlHandshakePacket; +import org.apache.doris.mysql.privilege.PrivPredicate; +import org.apache.doris.mysql.protocol.MysqlProtocolAdapter; +import org.apache.doris.nereids.parser.NereidsParser; +import org.apache.doris.nereids.trees.plans.commands.CreateUserCommand; +import org.apache.doris.nereids.trees.plans.commands.SuUserCommand; +import org.apache.doris.utframe.TestWithFeService; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; + +public class MysqlConnectProcessorSuTest extends TestWithFeService { + @Override + protected void runBeforeAll() throws Exception { + createDatabase("su_change_tenant"); + createDatabase("su_change_personal"); + addUser("su_change_svc", false); + addUser("su_change_person", false); + addUser("su_change_login", false); + createRole("su_change_role"); + grantPriv("GRANT PROXY_PRIV ON *.*.* TO 'su_change_svc'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_tenant.* TO ROLE 'su_change_role'"); + grantRole("GRANT 'su_change_role' TO 'su_change_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_person'@'%'"); + grantPriv("GRANT SELECT_PRIV ON internal.su_change_personal.* TO 'su_change_login'@'%'"); + // an account WITH a password, for the rejected-password branch of COM_CHANGE_USER + String createWithPassword = "CREATE USER 'su_change_pwd'@'%' IDENTIFIED BY 'Secret_123'"; + ((CreateUserCommand) new NereidsParser().parseSingle(createWithPassword)) + .run(connectContext, new StmtExecutor(connectContext, createWithPassword)); + } + + private ConnectContext switchedContext() throws Exception { + return switchedContext(null); + } + + private ConnectContext switchedContext(String workloadGroup) throws Exception { + ConnectContext ctx = new ConnectContext(); + ctx.setCurrentUserIdentity(UserIdentity.createAnalyzedUserIdentWithIp("su_change_svc", "%")); + ctx.setRemoteIP("127.0.0.1"); + MysqlProtocolAdapter.of(ctx).setHandshakePacket(new MysqlHandshakePacket(0)); + ctx.setThreadLocalInfo(); + new SuUserCommand(new UserIdentity("su_change_person", "%"), + Collections.singletonList("su_change_role"), workloadGroup).run(ctx, null); + return ctx; + } + + private boolean canSelect(ConnectContext ctx, String db) { + return Env.getCurrentEnv().getAccessManager().checkDbPriv(ctx.getCurrentUserIdentity(), + InternalCatalog.INTERNAL_CATALOG_NAME, db, PrivPredicate.SELECT); + } + + private void changeUser(ConnectContext ctx, String user) throws Exception { + changeUser(ctx, user, new byte[0]); + } + + private void changeUser(ConnectContext ctx, String user, byte[] nativeScramble) throws Exception { + // COM_CHANGE_USER payload, with a native-password scramble (empty: no password) and no default database. + ByteBuffer packet = ByteBuffer.allocate(256).order(ByteOrder.LITTLE_ENDIAN); + packet.put(user.getBytes(StandardCharsets.UTF_8)).put((byte) 0); + packet.put((byte) nativeScramble.length); + packet.put(nativeScramble); + packet.put((byte) 0); + packet.putShort((short) 33); + packet.put(MysqlHandshakePacket.AUTH_PLUGIN_NAME.getBytes(StandardCharsets.UTF_8)).put((byte) 0); + packet.flip(); + MysqlConnectProcessor processor = new MysqlConnectProcessor(ctx); + Field packetField = MysqlConnectProcessor.class.getDeclaredField("packetBuf"); + packetField.setAccessible(true); + packetField.set(processor, packet); + Method handler = MysqlConnectProcessor.class.getDeclaredMethod("handleChangeUser"); + handler.setAccessible(true); + handler.invoke(processor); + } + + private void assertStillNarrowed(ConnectContext ctx) { + Assertions.assertEquals("su_change_person", ctx.getQualifiedUser()); + Assertions.assertTrue(canSelect(ctx, "su_change_tenant")); + Assertions.assertFalse(canSelect(ctx, "su_change_personal")); + Assertions.assertEquals("su_change_svc", ctx.getAuthenticatedIdentity().getQualifiedUser()); + Assertions.assertEquals(Collections.singleton("su_change_role"), ctx.getSessionRoleOverride()); + Assertions.assertFalse(ctx.isKilled()); + } + + @Test + public void testForbiddenChangeUserPreservesNarrowing() throws Exception { + try { + for (String user : new String[] {"root", "admin"}) { + ConnectContext ctx = switchedContext(); + changeUser(ctx, user); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testUnknownUserPreservesNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + changeUser(ctx, "su_change_nonexistent"); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testRejectedPasswordPreservesNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + // an existing account, a 20-byte native scramble that cannot match its password + byte[] wrongScramble = new byte[20]; + Arrays.fill(wrongScramble, (byte) 1); + changeUser(ctx, "su_change_pwd", wrongScramble); + Assertions.assertEquals(QueryState.MysqlStateType.ERR, ctx.getState().getStateType()); + assertStillNarrowed(ctx); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSuccessfulChangeUserClearsNarrowing() throws Exception { + try { + ConnectContext ctx = switchedContext(); + changeUser(ctx, "su_change_login"); + Assertions.assertEquals(QueryState.MysqlStateType.OK, ctx.getState().getStateType()); + Assertions.assertEquals("su_change_login", ctx.getQualifiedUser()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertTrue(canSelect(ctx, "su_change_personal")); + Assertions.assertFalse(canSelect(ctx, "su_change_tenant")); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testSuccessfulChangeUserLeavesTheSwitchersLane() throws Exception { + try { + ConnectContext ctx = switchedContext("su_change_lane"); + Assertions.assertEquals("su_change_lane", ctx.getSessionVariable().getWorkloadGroup()); + changeUser(ctx, "su_change_login"); + Assertions.assertEquals(QueryState.MysqlStateType.OK, ctx.getState().getStateType()); + Assertions.assertEquals("su_change_login", ctx.getQualifiedUser()); + // the lane the switch chose goes with the switch: the new identity resolves its own + Assertions.assertEquals("", ctx.getSessionVariable().getWorkloadGroup()); + } finally { + connectContext.setThreadLocalInfo(); + } + } + + @Test + public void testConnectionResetRestoresAuthenticatedIdentity() throws Exception { + try { + ConnectContext ctx = switchedContext(); + ctx.resetConnection(); + Assertions.assertEquals("su_change_svc", ctx.getQualifiedUser()); + Assertions.assertNull(ctx.getAuthenticatedIdentity()); + Assertions.assertNull(ctx.getSessionRoleOverride()); + Assertions.assertFalse(canSelect(ctx, "su_change_personal")); + Assertions.assertFalse(canSelect(ctx, "su_change_tenant")); + } finally { + connectContext.setThreadLocalInfo(); + } + } +} diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java index 6c139546424c67..55c4e532187178 100644 --- a/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java +++ b/fe/fe-core/src/test/java/org/apache/doris/service/FrontendServiceImplTest.java @@ -907,6 +907,23 @@ public void testShowUser() throws Exception { // Fail closed: a request without a caller identity (e.g. a pre-upgrade BE that does not // set the field) exposes no rows rather than leaking every account. Assertions.assertTrue(impl.showUser(new TShowUserRequest()).getUserinfoList().isEmpty()); + + // An SU-narrowed caller is authorized against the role subset the request carries: a + // role administrator narrowed to a plain role sees only its own row. + addUser("show_user_adm", true); + executeCommand("create role show_user_adm_role"); + executeCommand("create role show_user_plain_role"); + executeCommand("grant grant_priv on *.*.* to role 'show_user_adm_role'"); + executeCommand("grant 'show_user_adm_role', 'show_user_plain_role' to 'show_user_adm'@'%'"); + TShowUserRequest admRequest = new TShowUserRequest(); + admRequest.setCurrentUserIdent( + UserIdentity.createAnalyzedUserIdentWithIp("show_user_adm", "%").toThrift()); + Assertions.assertTrue(impl.showUser(admRequest).getUserinfoList().size() >= 2, + "a role administrator sees every account"); + admRequest.setCurrentRoles(Collections.singleton("show_user_plain_role")); + List> narrowedRows = impl.showUser(admRequest).getUserinfoList(); + Assertions.assertEquals(1, narrowedRows.size(), "narrowed to a plain role: own row only"); + Assertions.assertEquals("show_user_adm", narrowedRows.get(0).get(userNameIdx)); } @Test diff --git a/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java new file mode 100644 index 00000000000000..e91789610f498d --- /dev/null +++ b/fe/fe-core/src/test/java/org/apache/doris/service/NarrowedMetadataRpcTest.java @@ -0,0 +1,303 @@ +// 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.doris.service; + +import org.apache.doris.analysis.UserIdentity; +import org.apache.doris.catalog.Env; +import org.apache.doris.datasource.InternalCatalog; +import org.apache.doris.qe.ConnectContext; +import org.apache.doris.qe.ConnectPoolMgr; +import org.apache.doris.thrift.TDescribeTablesParams; +import org.apache.doris.thrift.TFetchSchemaTableDataRequest; +import org.apache.doris.thrift.TFetchSchemaTableDataResult; +import org.apache.doris.thrift.TGetDbsParams; +import org.apache.doris.thrift.TGetTablesParams; +import org.apache.doris.thrift.TListPrivilegesResult; +import org.apache.doris.thrift.TSchemaTableName; +import org.apache.doris.thrift.TSchemaTableRequestParams; +import org.apache.doris.thrift.TShowProcessListRequest; +import org.apache.doris.thrift.TStatusCode; +import org.apache.doris.utframe.TestWithFeService; + +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.List; +import java.util.Set; + +/** + * A BE->FE schema-table request from a session-narrowed (SU) session carries the session's active + * role subset (TSchemaTableRequestParams.current_roles); the handler installs it so MetadataGenerator's + * privilege checks resolve that set instead of the caller's full role union. + */ +public class NarrowedMetadataRpcTest extends TestWithFeService { + + private final ExecuteEnv exeEnv = Mockito.mock(ExecuteEnv.class); + + private static final Set NARROWED = Sets.newHashSet("tenant_c"); + private static final UserIdentity CAROL = UserIdentity.createAnalyzedUserIdentWithIp("carol", "%"); + + @Override + protected void runBeforeAll() throws Exception { + createDatabase("test"); + createDatabase("perso"); + createTable("create table perso.t1 (k int) distributed by hash(k) buckets 1" + + " properties('replication_num' = '1')"); + createTable("create table test.t1 (k int) distributed by hash(k) buckets 1" + + " properties('replication_num' = '1')"); + addUser("carol", true); + createRole("tenant_c"); + grantPriv("GRANT SELECT_PRIV ON internal.test.* TO ROLE 'tenant_c';"); + grantPriv("GRANT SELECT_PRIV ON internal.test.t1 TO ROLE 'tenant_c';"); + grantRole("GRANT 'tenant_c' TO 'carol'@'%'"); + // personal (default-role) grants the narrowing must drop, one per privilege level + grantPriv("GRANT SELECT_PRIV ON internal.perso.* TO 'carol'@'%';"); + grantPriv("GRANT SELECT_PRIV ON internal.perso.t1 TO 'carol'@'%';"); + // dave: a plain tenant role and an ADMIN-bearing one the switch leaves out; a bystander session + addUser("dave", true); + addUser("bystander_rpc", true); + createRole("tenant_d"); + createRole("ops_d"); + grantPriv("GRANT ADMIN_PRIV ON *.*.* TO ROLE 'ops_d';"); + grantRole("GRANT 'tenant_d','ops_d' TO 'dave'@'%'"); + } + + /** The caller as the request names it: the identity struct, or the deprecated user/user_ip pair. */ + private static void nameCaller(TGetDbsParams params, boolean byIdentity) { + if (byIdentity) { + params.setCurrentUserIdent(CAROL.toThrift()); + } else { + params.setUser("carol"); + params.setUserIp("%"); + } + } + + private static void nameCaller(TGetTablesParams params, boolean byIdentity) { + if (byIdentity) { + params.setCurrentUserIdent(CAROL.toThrift()); + } else { + params.setUser("carol"); + params.setUserIp("%"); + } + } + + private static TGetDbsParams dbs(Set currentRoles, boolean byIdentity) { + TGetDbsParams params = new TGetDbsParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + nameCaller(params, byIdentity); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + private static TGetTablesParams tables(String db, Set currentRoles, boolean byIdentity) { + TGetTablesParams params = new TGetTablesParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDb(db); + nameCaller(params, byIdentity); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + /** A privilege-status request (TGetTablesParams without a db) from the named caller. */ + private static TGetTablesParams privileges(String user, Set currentRoles, boolean byIdentity) { + TGetTablesParams params = new TGetTablesParams(); + if (byIdentity) { + params.setCurrentUserIdent(UserIdentity.createAnalyzedUserIdentWithIp(user, "%").toThrift()); + } else { + params.setUser(user); + params.setUserIp("%"); + } + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + /** The rows of a privilege-status listing on the named schema. */ + private static long rowsOn(TListPrivilegesResult result, String schema) { + return result.getPrivileges().stream().filter(row -> schema.equals(row.getSchema())).count(); + } + + /** How many tables information_schema.metadata_name_ids lists in db for the caller. */ + private static int metadataNameIds(FrontendServiceImpl impl, String db, Set currentRoles, + boolean byIdentity) throws Exception { + return impl.listTableMetadataNameIds(tables(db, currentRoles, byIdentity)).getTables().size(); + } + + private static TDescribeTablesParams describe(String db, Set currentRoles) { + TDescribeTablesParams params = new TDescribeTablesParams(); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDb(db); + params.setTablesName(Lists.newArrayList("t1")); + params.setCurrentUserIdent(CAROL.toThrift()); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + return params; + } + + private static TFetchSchemaTableDataRequest databaseProperties(String db, Set currentRoles) { + TSchemaTableRequestParams params = new TSchemaTableRequestParams(); + params.setCurrentUserIdent(UserIdentity.createAnalyzedUserIdentWithIp("carol", "%").toThrift()); + params.setCatalog(InternalCatalog.INTERNAL_CATALOG_NAME); + params.setDbId(Env.getCurrentInternalCatalog().getDbNullable(db).getId()); + if (currentRoles != null) { + params.setCurrentRoles(currentRoles); + } + TFetchSchemaTableDataRequest request = new TFetchSchemaTableDataRequest(); + request.setSchemaTableName(TSchemaTableName.DATABASE_PROPERTIES); + request.setSchemaTableParams(params); + return request; + } + + private static int rows(TFetchSchemaTableDataResult result) { + Assertions.assertEquals(TStatusCode.OK, result.getStatus().getStatusCode()); + return result.getDataBatch().size(); + } + + @Test + public void testSchemaTableRequestNarrowsPrivilegeChecks() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + // un-narrowed: the personal grant on perso and the role grant on test both show + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("perso", null)))); + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("test", null)))); + + // narrowed to the tenant role: the personal grant is gone, the role grant remains + Set narrowed = Sets.newHashSet("tenant_c"); + Assertions.assertEquals(0, rows(impl.fetchSchemaTableData(databaseProperties("perso", narrowed)))); + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("test", narrowed)))); + + // the narrowing did not stick to the handler thread + Assertions.assertEquals(1, rows(impl.fetchSchemaTableData(databaseProperties("perso", null)))); + } + + @Test + public void testNarrowedRequestWithoutIdentityFailsClosed() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + TFetchSchemaTableDataRequest request = databaseProperties("test", Sets.newHashSet("tenant_c")); + request.getSchemaTableParams().unsetCurrentUserIdent(); + TFetchSchemaTableDataResult result = impl.fetchSchemaTableData(request); + Assertions.assertNotEquals(TStatusCode.OK, result.getStatus().getStatusCode()); + } + + /** + * Every one of the eight wrapped name-visibility handlers narrows alike, whether the request + * names the caller by identity or by the deprecated user/user_ip pair: the personal grants on + * perso vanish under the tenant role, the role grants on test stay, and nothing sticks to the + * handler thread. listTableMetadataNameIds decides on an executor thread of its own, and the + * global privilege listing is gated on the caller's own global SHOW, which dave holds only + * through the ADMIN-bearing role the switch leaves out. + */ + @Test + public void testEveryNameVisibilityHandlerNarrows() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + for (boolean byIdentity : new boolean[] {true, false}) { + Assertions.assertTrue(impl.getDbNames(dbs(null, byIdentity)).getDbs().contains("perso")); + Assertions.assertFalse(impl.getDbNames(dbs(NARROWED, byIdentity)).getDbs().contains("perso")); + Assertions.assertTrue(impl.getDbNames(dbs(NARROWED, byIdentity)).getDbs().contains("test")); + + Assertions.assertEquals(1, impl.getTableNames(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(0, impl.getTableNames(tables("perso", NARROWED, byIdentity)).getTables().size()); + Assertions.assertEquals(1, impl.getTableNames(tables("test", NARROWED, byIdentity)).getTables().size()); + + Assertions.assertEquals(1, impl.listTableStatus(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(0, impl.listTableStatus(tables("perso", NARROWED, byIdentity)).getTables().size()); + Assertions.assertEquals(1, impl.listTableStatus(tables("test", NARROWED, byIdentity)).getTables().size()); + + // the check runs on the handler's executor thread: the narrowing must follow it there + Assertions.assertEquals(1, metadataNameIds(impl, "perso", null, byIdentity)); + Assertions.assertEquals(0, metadataNameIds(impl, "perso", NARROWED, byIdentity)); + Assertions.assertEquals(1, metadataNameIds(impl, "test", NARROWED, byIdentity)); + + TGetTablesParams own = privileges("carol", null, byIdentity); + TGetTablesParams narrowed = privileges("carol", NARROWED, byIdentity); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(own), "perso")); + Assertions.assertEquals(0, rowsOn(impl.listTablePrivilegeStatus(narrowed), "perso")); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(narrowed), "test")); + Assertions.assertEquals(1, rowsOn(impl.listSchemaPrivilegeStatus(own), "perso")); + Assertions.assertEquals(0, rowsOn(impl.listSchemaPrivilegeStatus(narrowed), "perso")); + Assertions.assertEquals(1, rowsOn(impl.listSchemaPrivilegeStatus(narrowed), "test")); + + TGetTablesParams daveNarrowed = privileges("dave", Sets.newHashSet("tenant_d"), byIdentity); + Assertions.assertFalse(impl.listUserPrivilegeStatus(privileges("dave", null, byIdentity)) + .getPrivileges().isEmpty()); + Assertions.assertTrue(impl.listUserPrivilegeStatus(daveNarrowed).getPrivileges().isEmpty()); + + // the narrowing did not stick to the handler thread + Assertions.assertEquals(1, impl.listTableStatus(tables("perso", null, byIdentity)).getTables().size()); + Assertions.assertEquals(1, metadataNameIds(impl, "perso", null, byIdentity)); + Assertions.assertEquals(1, rowsOn(impl.listTablePrivilegeStatus(own), "perso")); + } + Assertions.assertEquals(1, impl.describeTables(describe("perso", null)).getColumns().size()); + Assertions.assertEquals(0, impl.describeTables(describe("perso", NARROWED)).getColumns().size()); + Assertions.assertEquals(1, impl.describeTables(describe("test", NARROWED)).getColumns().size()); + } + + /** + * The FE->FE processlist fan-out: a target holding an ADMIN-bearing role the switch left out sees + * every session un-narrowed, and only its own sessions under the narrowing. + */ + @Test + public void testShowProcessListNarrowsTheAdminCheck() throws Exception { + FrontendServiceImpl impl = new FrontendServiceImpl(exeEnv); + UserIdentity dave = UserIdentity.createAnalyzedUserIdentWithIp("dave", "%"); + ConnectPoolMgr pool = ExecuteEnv.getInstance().getScheduler().getConnectPoolMgr(); + ConnectContext daves = registered(pool, dave, 7001); + ConnectContext bystanders = registered(pool, + UserIdentity.createAnalyzedUserIdentWithIp("bystander_rpc", "%"), 7002); + try { + TShowProcessListRequest request = new TShowProcessListRequest(); + request.setShowFullSql(true); + request.setCurrentUserIdent(dave.toThrift()); + Set everyUser = usersListed(impl.showProcessList(request).getProcessList()); + Assertions.assertTrue(everyUser.contains("bystander_rpc"), everyUser.toString()); + + request.setCurrentRoles(Sets.newHashSet("tenant_d")); + Set narrowedUsers = usersListed(impl.showProcessList(request).getProcessList()); + Assertions.assertEquals(Sets.newHashSet("dave"), narrowedUsers); + } finally { + pool.unregisterConnection(daves); + pool.unregisterConnection(bystanders); + } + } + + private static ConnectContext registered(ConnectPoolMgr pool, UserIdentity user, int connectionId) { + ConnectContext ctx = new ConnectContext(); + ctx.setEnv(Env.getCurrentEnv()); + ctx.setCurrentUserIdentity(user); + ctx.setConnectionId(connectionId); + Assertions.assertEquals(-1, pool.registerConnection(ctx)); + return ctx; + } + + /** The User column (index 2) of every processlist row. */ + private static Set usersListed(List> rows) { + Set users = Sets.newHashSet(); + for (List row : rows) { + users.add(row.get(2)); + } + return users; + } +} diff --git a/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt b/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt index 8103c329ff5cf5..027410838eefc2 100644 --- a/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt +++ b/fe/fe-core/src/test/resources/access-control-behavior-baseline.txt @@ -1,61 +1,61 @@ -ACTIONS: ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ACTIONS: ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE USERS: admin_user,node_user,local_user,ranger_user,nobody -builtin | - | admin_user | global | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | global | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | global | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | global | - builtin | - | ranger_user | global | - builtin | - | nobody | global | - -builtin | - | admin_user | resource:res1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | resource:res1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | resource:res1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | resource:res1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE builtin | - | ranger_user | resource:res1 | - builtin | - | nobody | resource:res1 | - -builtin | - | admin_user | workloadgrp:wg1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | workloadgrp:wg1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | workloadgrp:wg1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | workloadgrp:wg1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE builtin | - | ranger_user | workloadgrp:wg1 | - builtin | - | nobody | workloadgrp:wg1 | - -builtin | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | - | admin_user | vault:sv1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | - | admin_user | vault:sv1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | - | node_user | vault:sv1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | - | local_user | vault:sv1 | - builtin | - | ranger_user | vault:sv1 | - builtin | - | nobody | vault:sv1 | - -builtin | internal | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR,SHOW builtin | internal | local_user | catalog | SHOW builtin | internal | ranger_user | catalog | SHOW builtin | internal | nobody | catalog | SHOW -builtin | internal | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SHOW builtin | internal | local_user | catalog:skipchk | SHOW builtin | internal | ranger_user | catalog:skipchk | SHOW builtin | internal | nobody | catalog:skipchk | SHOW -builtin | internal | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | internal | local_user | database | SHOW builtin | internal | ranger_user | database | - builtin | internal | nobody | database | - -builtin | internal | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | internal | local_user | table | SELECT,SHOW builtin | internal | ranger_user | table | - builtin | internal | nobody | table | - -builtin | internal | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | internal | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | internal | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | internal | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | internal | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | internal | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | internal | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | internal | admin_user | rowfilter | - builtin | internal | node_user | rowfilter | - builtin | internal | local_user | rowfilter | [builtin_row_policy | pcol1 = 1 | RESTRICTIVE] @@ -71,36 +71,36 @@ builtin | internal | node_user | mask:pcol2 | - builtin | internal | local_user | mask:pcol2 | - builtin | internal | ranger_user | mask:pcol2 | - builtin | internal | nobody | mask:pcol2 | - -builtin | ext_plain | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | catalog | SHOW builtin | ext_plain | ranger_user | catalog | - builtin | ext_plain | nobody | catalog | - -builtin | ext_plain | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | catalog:skipchk | SHOW builtin | ext_plain | ranger_user | catalog:skipchk | - builtin | ext_plain | nobody | catalog:skipchk | - -builtin | ext_plain | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | database | SHOW builtin | ext_plain | ranger_user | database | - builtin | ext_plain | nobody | database | - -builtin | ext_plain | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_plain | local_user | table | SELECT,SHOW builtin | ext_plain | ranger_user | table | - builtin | ext_plain | nobody | table | - -builtin | ext_plain | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | ext_plain | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR -builtin | ext_plain | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -builtin | ext_plain | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | ext_plain | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | local_user | column:pcol1 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | ranger_user | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | nobody | column:pcol1 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE / unsupported=OPERATOR +builtin | ext_plain | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR / unsupported=ADMIN,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | local_user | column:pcol2 | ALL,SELECT,SHOW / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | ranger_user | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_plain | nobody | column:pcol2 | - / unsupported=ADMIN,ADMIN_OR_NODE,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,OPERATOR,PROXY,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_plain | admin_user | rowfilter | - builtin | ext_plain | node_user | rowfilter | - builtin | ext_plain | local_user | rowfilter | - @@ -116,32 +116,32 @@ builtin | ext_plain | node_user | mask:pcol2 | - builtin | ext_plain | local_user | mask:pcol2 | - builtin | ext_plain | ranger_user | mask:pcol2 | - builtin | ext_plain | nobody | mask:pcol2 | - -builtin | ext_ranger | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | catalog | SHOW builtin | ext_ranger | ranger_user | catalog | - builtin | ext_ranger | nobody | catalog | - -builtin | ext_ranger | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SELECT,SHOW builtin | ext_ranger | local_user | catalog:skipchk | SELECT,SHOW builtin | ext_ranger | ranger_user | catalog:skipchk | SELECT,SHOW builtin | ext_ranger | nobody | catalog:skipchk | SELECT,SHOW -builtin | ext_ranger | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | database | - builtin | ext_ranger | ranger_user | database | SHOW builtin | ext_ranger | nobody | database | - -builtin | ext_ranger | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | table | - builtin | ext_ranger | ranger_user | table | ALL,SELECT,SHOW builtin | ext_ranger | nobody | table | - -builtin | ext_ranger | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | column:pcol1 | - builtin | ext_ranger | ranger_user | column:pcol1 | ALL,SELECT,SHOW builtin | ext_ranger | nobody | column:pcol1 | - -builtin | ext_ranger | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_ranger | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_ranger | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_ranger | local_user | column:pcol2 | - builtin | ext_ranger | ranger_user | column:pcol2 | ALL,SELECT,SHOW @@ -161,32 +161,32 @@ builtin | ext_ranger | node_user | mask:pcol2 | - builtin | ext_ranger | local_user | mask:pcol2 | - builtin | ext_ranger | ranger_user | mask:pcol2 | - builtin | ext_ranger | nobody | mask:pcol2 | - -builtin | ext_legacy | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | catalog | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | catalog | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_legacy | local_user | catalog | SHOW builtin | ext_legacy | ranger_user | catalog | - builtin | ext_legacy | nobody | catalog | - -builtin | ext_legacy | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | catalog:skipchk | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | catalog:skipchk | ADMIN_OR_NODE,ALL,OPERATOR,SELECT,SHOW builtin | ext_legacy | local_user | catalog:skipchk | SELECT,SHOW builtin | ext_legacy | ranger_user | catalog:skipchk | SELECT,SHOW builtin | ext_legacy | nobody | catalog:skipchk | SELECT,SHOW -builtin | ext_legacy | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | database | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | database | - builtin | ext_legacy | nobody | database | - -builtin | ext_legacy | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | table | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | table | - builtin | ext_legacy | nobody | table | - -builtin | ext_legacy | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | column:pcol1 | ADMIN_OR_NODE,ALL,OPERATOR -builtin | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | ranger_user | column:pcol1 | - builtin | ext_legacy | nobody | column:pcol1 | - -builtin | ext_legacy | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +builtin | ext_legacy | admin_user | column:pcol2 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE builtin | ext_legacy | node_user | column:pcol2 | ADMIN_OR_NODE,ALL,OPERATOR builtin | ext_legacy | local_user | column:pcol2 | - builtin | ext_legacy | ranger_user | column:pcol2 | - @@ -221,11 +221,11 @@ ranger | - | node_user | workloadgrp:wg1 | - ranger | - | local_user | workloadgrp:wg1 | - ranger | - | ranger_user | workloadgrp:wg1 | ALL,SHOW_RESOURCES,SHOW_WORKLOAD_GROUP,USAGE ranger | - | nobody | workloadgrp:wg1 | - -ranger | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE -ranger | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | admin_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | node_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | local_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | ranger_user | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | - | nobody | workloadgrp:default | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | - | admin_user | vault:sv1 | - ranger | - | node_user | vault:sv1 | - ranger | - | local_user | vault:sv1 | - @@ -378,17 +378,17 @@ ranger | ext_legacy | ranger_user | catalog:skipchk | SELECT,SHOW ranger | ext_legacy | nobody | catalog:skipchk | SELECT,SHOW ranger | ext_legacy | admin_user | database | - ranger | ext_legacy | node_user | database | - -ranger | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | database | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | database | - ranger | ext_legacy | nobody | database | - ranger | ext_legacy | admin_user | table | - ranger | ext_legacy | node_user | table | - -ranger | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | table | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | table | - ranger | ext_legacy | nobody | table | - ranger | ext_legacy | admin_user | column:pcol1 | - ranger | ext_legacy | node_user | column:pcol1 | - -ranger | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE +ranger | ext_legacy | local_user | column:pcol1 | ADMIN,ADMIN_OR_NODE,ALL,ALTER,ALTER_CREATE,ALTER_CREATE_DROP,CREATE,DROP,GRANT,LOAD,OPERATOR,PROXY,SELECT,SHOW,SHOW_RESOURCES,SHOW_VIEW,SHOW_WORKLOAD_GROUP,USAGE ranger | ext_legacy | ranger_user | column:pcol1 | - ranger | ext_legacy | nobody | column:pcol1 | - ranger | ext_legacy | admin_user | column:pcol2 | - diff --git a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 index f23c812d449657..309e9135568c5d 100644 --- a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 +++ b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisLexer.g4 @@ -556,6 +556,7 @@ STREAMS: 'STREAMS'; STREAMING: 'STREAMING'; STRING: 'STRING'; STRUCT: 'STRUCT'; +SU: 'SU'; SUBJECT: 'SUBJECT'; SUBSTR: 'SUBSTR'; SUBSTRING: 'SUBSTRING'; diff --git a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 index 33d0115ebfcdb8..5727554409b429 100644 --- a/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 +++ b/fe/fe-sql-parser/src/main/antlr4/org/apache/doris/nereids/DorisParser.g4 @@ -952,6 +952,9 @@ grantRevokeStatement identifierOrTextOrAsterisk FROM (userIdentify | ROLE identifierOrText) #revokeResourcePrivilege | REVOKE privilegeList ON multipartIdentifierOrAsterisk FROM (userIdentify | ROLE identifierOrText) #revokeTablePrivilege + | SU userIdentify WITH ROLES LEFT_PAREN + roles+=identifierOrText (COMMA roles+=identifierOrText)* RIGHT_PAREN + (WORKLOAD GROUP wg=identifierOrText)? #suUser ; privilege @@ -2619,6 +2622,7 @@ nonReserved | STREAMING | STRING | STRUCT + | SU | SUBJECT | SUBSTR | SUBSTRING diff --git a/gensrc/thrift/FrontendService.thrift b/gensrc/thrift/FrontendService.thrift index 7d96a9e7415bce..b8a4f27781b384 100644 --- a/gensrc/thrift/FrontendService.thrift +++ b/gensrc/thrift/FrontendService.thrift @@ -65,9 +65,10 @@ struct TDescribeTablesParams { 5: optional Types.TUserIdentity current_user_ident // to replace the user and user ip 6: optional bool show_hidden_columns = false 7: optional string catalog - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 8: optional set reserved_field_8 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 8: optional set current_roles // Report COLUMN_KEY the way MySQL does. Forwarded from the schema scan node because // this request carries no session of its own. 9: optional bool mysql_compatible_index_metadata = false @@ -101,9 +102,10 @@ struct TGetDbsParams { 4: optional Types.TUserIdentity current_user_ident // to replace the user and user ip 5: optional string catalog 6: optional bool get_null_catalog //if catalog is empty , get dbName ="NULL" and dbId = -1. - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 7: optional set reserved_field_7 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 7: optional set current_roles } // getDbNames returns a list of database names , database ids and catalog names ,catalog ids @@ -128,9 +130,10 @@ struct TGetTablesParams { 6: optional string type 7: optional string catalog 8: optional string table - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 9: optional set reserved_field_9 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows name visibility to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 9: optional set current_roles // Columns needed by schema table callers. If unset, the callee returns the // full table status for backward compatibility. 10: optional set required_columns @@ -428,11 +431,16 @@ struct TMasterOpRequest { 32: optional bool moreResultExists // Server has more result to send 33: optional map connect_attributes - // Reserved for downstream fields `current_roles` and `is_su_user` to keep - // thrift field ids wire-compatible across maintained branches. Do not reuse these ids. - 34: optional set reserved_field_34 - 35: optional bool reserved_field_35 + // Session-narrowed (SU) session: the active role subset that REPLACES the target's role + // union while the master executes this statement, and the flag that the session is + // switched. A switched session that carries no (or an empty) role list is refused by the + // master (fail closed): an installed narrowing is never empty. Ids 34/35 were reserved + // for exactly these fields. + 34: optional set current_roles + 35: optional bool is_su_user 36: optional string connectingFeLocalResourceGroup + // SU: the account that authenticated the connection, so the master's audit row names it too + 37: optional Types.TUserIdentity authenticated_user_ident // selectdb cloud 1000: optional string cloud_cluster @@ -967,9 +975,10 @@ struct TMetadataTableRequestParams { 12: optional PlanNodes.TMetaCacheStatsParams meta_cache_stats_params 13: optional PlanNodes.TPartitionValuesMetadataParams partition_values_metadata_params 14: optional PlanNodes.THudiMetadataParams hudi_metadata_params - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 15: optional set reserved_field_15 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 15: optional set current_roles 16: optional PlanNodes.TLanceIndexMetadataParams lance_index_metadata_params } @@ -982,9 +991,10 @@ struct TSchemaTableRequestParams { 6: optional string time_zone // used for DATETIME field 7: optional string frontend_conjuncts 8: optional i64 thread_id // mysql connection id for fetching ConnectContext if needed - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 9: optional set reserved_field_9 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 9: optional set current_roles // The one table the query asked for, when it pinned one with `TABLE_NAME = '...'`. // Lets the FE answer from that table instead of walking the whole database. 10: optional string table_name @@ -1628,9 +1638,10 @@ struct TShowProcessListRequest { 1: optional bool show_full_sql 2: optional Types.TUserIdentity current_user_ident 3: optional string time_zone - // Reserved for downstream field `current_roles` to keep thrift field ids - // wire-compatible across maintained branches. Do not reuse this id. - 4: optional set reserved_field_4 + // Session-narrowed (SU) session: the active role subset the calling session runs under, so + // the handler narrows its privilege checks to that set. Unset = not narrowed. This id was + // reserved for exactly this field. + 4: optional set current_roles } struct TShowProcessListResult { @@ -1639,6 +1650,7 @@ struct TShowProcessListResult { struct TShowUserRequest { 1: optional Types.TUserIdentity current_user_ident // to filter rows by the requesting user's privileges + 2: optional set current_roles // the requesting session's SU-narrowed role subset, if any } struct TShowUserResult { diff --git a/gensrc/thrift/PlanNodes.thrift b/gensrc/thrift/PlanNodes.thrift index cd02c8127ec903..a3ec3a5ac4a393 100644 --- a/gensrc/thrift/PlanNodes.thrift +++ b/gensrc/thrift/PlanNodes.thrift @@ -1056,12 +1056,18 @@ struct TSchemaScanNode { // Captured from the session at plan time, because the FE cannot see the session of the // query when the BE calls back into it for schema metadata. 17: optional bool mysql_compatible_index_metadata = false + // SU-narrowed session's active role subset; the BE forwards it on its metadata RPCs + // (current_roles on TGetDbsParams / TGetTablesParams / TDescribeTablesParams). Unset = not narrowed. + 18: optional set current_roles } struct TMetaScanNode { 1: required Types.TTupleId tuple_id 2: optional Types.TMetadataType metadata_type 3: optional Types.TUserIdentity current_user_ident + // SU-narrowed session's active role subset; the BE forwards it on its metadata RPCs + // (current_roles on TMetadataTableRequestParams). Unset = not narrowed. + 4: optional set current_roles } struct TTestExternalScanNode { diff --git a/regression-test/data/audit/test_audit_log_behavior.out b/regression-test/data/audit/test_audit_log_behavior.out index 40ed22056b07e2..d620965d8b2dd2 100644 --- a/regression-test/data/audit/test_audit_log_behavior.out +++ b/regression-test/data/audit/test_audit_log_behavior.out @@ -4,6 +4,7 @@ query_id varchar(128) Yes true \N time datetime(3) Yes true \N client_ip varchar(128) Yes true \N user varchar(128) Yes false \N NONE +authenticated_user varchar(128) Yes false \N NONE frontend_ip varchar(1024) Yes false \N NONE catalog varchar(128) Yes false \N NONE db varchar(128) Yes false \N NONE diff --git a/regression-test/suites/auth_p0/test_su_session_narrowing.groovy b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy new file mode 100644 index 00000000000000..0c2db9f0e8e973 --- /dev/null +++ b/regression-test/suites/auth_p0/test_su_session_narrowing.groovy @@ -0,0 +1,213 @@ +// 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. + +// SU 'user' WITH ROLES (...): a session-narrowed identity switch (MySQL's proxy-user model with a +// mandatory role list). A service account holding PROXY_PRIV runs a session as a person, restricted +// to a subset of the person's granted roles; Doris enforces the narrowed privileges, the person's +// direct grants are inert under the switch, name visibility follows the narrowed set, the switch is +// one-shot, and the same person's own login is unaffected. +suite("test_su_session_narrowing", "p0,auth") { + String svc = 'su_narrow_svc' + String person = 'su_narrow_person' + String pwd = 'C123_567p' + String tenantRole = 'su_narrow_tenant' + String otherRole = 'su_narrow_other' + String extraRole = 'su_narrow_extra' + String roleDb = 'su_narrow_role_db' + String ownDb = 'su_narrow_own_db' + + try_sql("DROP ROW POLICY IF EXISTS su_narrow_tenant_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_extra_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_person_rows ON ${roleDb}.t") + try_sql("DROP USER ${svc}") + try_sql("DROP USER ${person}") + try_sql("DROP ROLE ${tenantRole}") + try_sql("DROP ROLE ${otherRole}") + try_sql("DROP ROLE ${extraRole}") + sql """DROP DATABASE IF EXISTS ${roleDb}""" + sql """DROP DATABASE IF EXISTS ${ownDb}""" + + sql """CREATE DATABASE ${roleDb}""" + sql """CREATE DATABASE ${ownDb}""" + sql """CREATE TABLE ${roleDb}.t (k INT) DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ('replication_num' = '1')""" + sql """CREATE TABLE ${ownDb}.t (k INT) DISTRIBUTED BY HASH(k) BUCKETS 1 + PROPERTIES ('replication_num' = '1')""" + sql """INSERT INTO ${roleDb}.t VALUES (1), (2)""" + sql """INSERT INTO ${ownDb}.t VALUES (1)""" + + sql """CREATE ROLE ${tenantRole}""" + sql """CREATE ROLE ${otherRole}""" + sql """CREATE ROLE ${extraRole}""" + sql """CREATE USER '${person}' IDENTIFIED BY '${pwd}'""" + sql """CREATE USER '${svc}' IDENTIFIED BY '${pwd}'""" + // the person: a tenant role on one database, a direct grant on another, and a second role + // the switch will leave out + sql """GRANT SELECT_PRIV ON ${roleDb}.* TO ROLE '${tenantRole}'""" + sql """GRANT '${tenantRole}' TO '${person}'@'%'""" + sql """GRANT '${extraRole}' TO '${person}'@'%'""" + sql """GRANT SELECT_PRIV ON ${ownDb}.* TO '${person}'@'%'""" + // row policies: one PERMISSIVE per role (OR-ed for whoever holds the roles), one RESTRICTIVE + // bound to the person (follows the identity): the person's own session sees k IN (1, 2), a + // session narrowed to the tenant role sees k = 1 only + sql """CREATE ROW POLICY su_narrow_tenant_rows ON ${roleDb}.t AS PERMISSIVE + TO ROLE '${tenantRole}' USING (k = 1)""" + sql """CREATE ROW POLICY su_narrow_extra_rows ON ${roleDb}.t AS PERMISSIVE + TO ROLE '${extraRole}' USING (k = 2)""" + sql """CREATE ROW POLICY su_narrow_person_rows ON ${roleDb}.t AS RESTRICTIVE + TO '${person}'@'%' USING (k < 3)""" + // the service account: nothing but the right to switch + sql """GRANT PROXY_PRIV ON *.*.* TO '${svc}'@'%'""" + // the framework connects with the default database in the URL, which needs a privilege on it + sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${svc}'@'%'""" + sql """GRANT SELECT_PRIV ON ${context.config.defaultDb}.* TO '${person}'@'%'""" + if (isCloudMode()) { + // compute-group USAGE is placement: the narrowed session runs in the person's lane + def clusters = sql """SHOW CLUSTERS""" + assertTrue(!clusters.isEmpty()) + def validCluster = clusters[0][0] + sql """GRANT USAGE_PRIV ON CLUSTER `${validCluster}` TO '${person}'@'%'""" + sql """GRANT USAGE_PRIV ON CLUSTER `${validCluster}` TO '${svc}'@'%'""" + } + + try { + // PROXY_PRIV is a global privilege, listed like any other + test { + sql """GRANT PROXY_PRIV ON ${roleDb}.* TO '${svc}'@'%'""" + exception "PROXY_PRIV" + } + def grants = sql """SHOW GRANTS FOR '${svc}'@'%'""" + assertTrue(grants.toString().contains("Proxy_priv")) + + // without PROXY_PRIV (or ADMIN_PRIV) the switch is refused + connect(person, pwd, context.config.jdbcUrl) { + test { + sql """SU '${person}'@'%' WITH ROLES ('${tenantRole}')""" + exception "PROXY_PRIV" + } + } + + // the ceiling is the target's granted roles: a role the person does not hold is refused + connect(svc, pwd, context.config.jdbcUrl) { + test { + sql """SU '${person}'@'%' WITH ROLES ('${otherRole}')""" + exception "not granted" + } + // PROXY_PRIV is a lesser privilege than ADMIN_PRIV: the built-in accounts are never a + // target and the built-in administrative roles are never requested + test { + sql """SU 'admin'@'%' WITH ROLES ('admin')""" + exception "forbidden" + } + test { + sql """SU '${person}'@'%' WITH ROLES ('operator')""" + exception "Invalid role" + } + } + + connect(svc, pwd, context.config.jdbcUrl) { + // before the switch the service account sees nothing of the person's + test { + sql """SELECT * FROM ${roleDb}.t""" + exception "denied" + } + + sql """SU '${person}'@'%' WITH ROLES ('${tenantRole}')""" + + // the session is the person, narrowed to the requested role + def who = sql """SELECT current_user()""" + assertTrue(who[0][0].toString().contains(person)) + def narrowed = sql """SELECT CAST(session_is_narrowed() AS INT)""" + assertEquals(1, narrowed[0][0] as int) + + // the requested role's grant works, under the requested role's row policy only: the + // excluded role's PERMISSIVE policy does not widen the rows, the person's own applies + def rows = sql """SELECT k FROM ${roleDb}.t ORDER BY k""" + assertEquals(1, rows.size()) + assertEquals(1, rows[0][0] as int) + // ... the person's direct grant is inert under the switch + test { + sql """SELECT * FROM ${ownDb}.t""" + exception "denied" + } + + // name visibility follows the narrowed set, on the FE and through the BE scanners + def dbs = sql """SHOW DATABASES LIKE '${ownDb}'""" + assertEquals(0, dbs.size()) + def visibleRole = sql """SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${roleDb}'""" + assertEquals(1, visibleRole[0][0] as int) + def visibleOwn = sql """SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = '${ownDb}'""" + assertEquals(0, visibleOwn[0][0] as int) + // placement listings agree on both legs: SHOW WORKLOAD GROUPS (answered by this FE) and + // information_schema.workload_groups (answered through the BE) list the same groups + def shownGroups = sql """SHOW WORKLOAD GROUPS""" + def scannedGroups = sql """SELECT COUNT(*) FROM information_schema.workload_groups""" + assertEquals(shownGroups.size(), scannedGroups[0][0] as int) + + // one-shot: a switched session cannot switch again, not even to itself + test { + sql """SU '${svc}'@'%' WITH ROLES ('${tenantRole}')""" + exception "already-switched" + } + + // a persisted job re-plans its read later under the stored identity, where the + // narrowing cannot follow: a narrowed session mints none + test { + sql """EXPORT TABLE ${roleDb}.t TO "file:///tmp/${roleDb}/" """ + exception "session-narrowed" + } + test { + sql """CREATE JOB ${roleDb}_job ON SCHEDULE EVERY 1 DAY + DO INSERT INTO ${roleDb}.t SELECT * FROM ${roleDb}.t""" + exception "session-narrowed" + } + + // the switcher is not the person: the "self" exemptions of SET PASSWORD and SET PROPERTY + // are closed under the switch + test { + sql """SET PASSWORD = PASSWORD('${pwd}')""" + exception "denied" + } + test { + sql """SET PROPERTY 'default_init_catalog' = 'internal'""" + exception "denied" + } + } + + // the person's own login is untouched: both grants live, both roles' policies apply, + // nothing is narrowed + connect(person, pwd, context.config.jdbcUrl) { + def own = sql """SELECT COUNT(*) FROM ${ownDb}.t""" + assertEquals(1, own[0][0] as int) + def role = sql """SELECT COUNT(*) FROM ${roleDb}.t""" + assertEquals(2, role[0][0] as int) + def narrowed = sql """SELECT CAST(session_is_narrowed() AS INT)""" + assertEquals(0, narrowed[0][0] as int) + } + } finally { + try_sql("DROP ROW POLICY IF EXISTS su_narrow_tenant_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_extra_rows ON ${roleDb}.t") + try_sql("DROP ROW POLICY IF EXISTS su_narrow_person_rows ON ${roleDb}.t") + try_sql("DROP USER ${svc}") + try_sql("DROP USER ${person}") + try_sql("DROP ROLE ${tenantRole}") + try_sql("DROP ROLE ${otherRole}") + try_sql("DROP ROLE ${extraRole}") + try_sql("DROP DATABASE IF EXISTS ${roleDb}") + try_sql("DROP DATABASE IF EXISTS ${ownDb}") + } +}