|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2024 Parseable, Inc. |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU Affero General Public License as |
| 6 | + * published by the Free Software Foundation, either version 3 of the |
| 7 | + * License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU Affero General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +use itertools::Itertools; |
| 20 | + |
| 21 | +use crate::rbac::{ |
| 22 | + map::SessionKey, |
| 23 | + role::{Action, Permission}, |
| 24 | + Users, |
| 25 | +}; |
| 26 | + |
| 27 | +use super::{CorrelationError, TableConfig}; |
| 28 | + |
| 29 | +pub async fn user_auth_for_query( |
| 30 | + session_key: &SessionKey, |
| 31 | + table_configs: &[TableConfig], |
| 32 | +) -> Result<(), CorrelationError> { |
| 33 | + let tables = table_configs.iter().map(|t| &t.table_name).collect_vec(); |
| 34 | + let permissions = Users.get_permissions(session_key); |
| 35 | + |
| 36 | + for table_name in tables { |
| 37 | + let mut authorized = false; |
| 38 | + |
| 39 | + // in permission check if user can run query on the stream. |
| 40 | + // also while iterating add any filter tags for this stream |
| 41 | + for permission in permissions.iter() { |
| 42 | + match permission { |
| 43 | + Permission::Stream(Action::All, _) => { |
| 44 | + authorized = true; |
| 45 | + break; |
| 46 | + } |
| 47 | + Permission::StreamWithTag(Action::Query, ref stream, _) |
| 48 | + if stream == table_name || stream == "*" => |
| 49 | + { |
| 50 | + authorized = true; |
| 51 | + } |
| 52 | + _ => (), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + if !authorized { |
| 57 | + return Err(CorrelationError::Unauthorized); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + Ok(()) |
| 62 | +} |
0 commit comments