plusplusjiajia commented on code in PR #758: URL: https://github.com/apache/paimon-rust/pull/758#discussion_r3906385535
########## crates/paimon/src/table/query_auth.rs: ########## @@ -0,0 +1,320 @@ +// 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. + +//! What the REST server authorized a user to read from one table. + +use crate::api::AuthTableQueryResponse; + +/// The server's answer for one user on one table, kept unparsed. +/// +/// `session` pins it to the handle that asked: `to_arrow` is public and the +/// response names neither table nor principal. Java binds nothing. Routing +/// options are unbound on purpose — sound only while unrestricted grants +/// authorize. +#[derive(Debug, PartialEq)] +pub(crate) struct QueryAuthGrant { + response: AuthTableQueryResponse, + session: u64, +} + +impl QueryAuthGrant { + pub(crate) fn new(response: AuthTableQueryResponse, session: u64) -> Self { + Self { response, session } + } + + /// The only case this client can serve. + pub(crate) fn is_unrestricted(&self) -> bool { + self.response.is_unrestricted() + } + + /// Travelled and branch views read a schema the server did not rule on. + /// Everything else follows from the session, which only the catalog mints. + pub(crate) fn matches_table(&self, table: &super::Table) -> bool { + !table.is_time_traveled() + && !table.is_branch_reference() + && table.query_auth_session() == Some(self.session) + } +} + +/// `value_stats` and `write_cols` are public on every split, and an older file +/// can name a since-dropped column the server never ruled on. Refused rather +/// than scrubbed: rewriting encoded stats is how bounds get mismatched. +pub(crate) async fn reject_unauthorized_stats( + plan: &super::Plan, + current: &crate::spec::TableSchema, + schemas: &super::schema_manager::SchemaManager, +) -> crate::Result<()> { + let refuse = |column: &str| { + Err(unsupported(&format!( + "a data file still carries statistics for '{column}', which the current schema — the \ + one the server authorized — does not have" + ))) + }; + let named = |name: &String| current.fields().iter().any(|f| f.name() == name); + let mut checked = std::collections::HashSet::new(); + for split in plan.splits() { + for file in split.data_files() { + for column in file + .value_stats_cols + .iter() + .chain(file.write_cols.iter()) + .flatten() + { + if !named(column) { + return refuse(column); + } + } + // The file's own schema is the authority: a name can be dropped and + // re-added under a new id, and the lists may be absent entirely. + if file.schema_id == current.id() || !checked.insert(file.schema_id) { + continue; + } + let older = schemas.schema(file.schema_id).await?; + if let Some(gone) = older.fields().iter().find(|f| { + !current + .fields() + .iter() + .any(|c| c.id() == f.id() && c.name() == f.name()) + }) { + return refuse(gone.name()); + } + } + } + Ok(()) +} + +/// A refusal naming the option, so callers never match on prose. +pub(crate) fn unsupported(reason: &str) -> crate::Error { + crate::Error::Unsupported { + message: format!( + "reading a table with 'query-auth.enabled' = true is not supported: {reason}" + ), + } +} + +/// Column permissions cover real schema fields, so the server can neither grant +/// nor refuse `_ROW_ID` and friends. +pub(crate) fn reject_system_columns<'a>( + names: impl IntoIterator<Item = &'a str>, +) -> crate::Result<()> { + for name in names { + if crate::spec::is_reserved_system_field_name(name) { + return Err(unsupported(&format!( + "the system column '{name}' is not one the server can authorize: column \ + permissions are granted over table columns" + ))); + } + } + Ok(()) +} + +/// The read resolves older files by field id, so a non-canonical `(id, name)` +/// pair reads as something no grant covered. System fields have no entry. +pub(crate) fn reject_noncanonical_fields( + read_type: &[crate::spec::DataField], + schema_fields: &[crate::spec::DataField], +) -> crate::Result<()> { + for field in read_type { + if crate::spec::is_reserved_system_field_name(field.name()) { + continue; + } + let canonical = schema_fields + .iter() + .any(|f| f.id() == field.id() && f.name() == field.name()); Review Comment: @JingsongLi Good catch. Both checks now compare id, name and full data type; DataType derives structural equality, so nested shapes are covered recursively. End-to-end test as you asked: a Row column read with a read type that keeps the authorized (id, name) but carries an extra nested child — refused. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
