|
| 1 | +// SPDX-License-Identifier: BUSL-1.1 |
| 2 | + |
| 3 | +//! Authorization parity for materialized HTTP SQL queries. |
| 4 | +
|
| 5 | +mod common; |
| 6 | + |
| 7 | +use std::sync::Arc; |
| 8 | +use std::time::Duration; |
| 9 | + |
| 10 | +use common::pgwire_harness::TestServer; |
| 11 | +use nodedb::config::auth::AuthMode; |
| 12 | +use nodedb::control::security::apikey::CreateKeyParams; |
| 13 | +use nodedb::control::security::identity::{Permission, Role}; |
| 14 | +use nodedb::control::security::permission::collection_target; |
| 15 | +use nodedb::control::state::SharedState; |
| 16 | +use nodedb::types::{DatabaseId, TenantId}; |
| 17 | + |
| 18 | +struct AuthenticatedHttpEndpoint { |
| 19 | + local_addr: std::net::SocketAddr, |
| 20 | + _server: tokio::task::JoinHandle<()>, |
| 21 | +} |
| 22 | + |
| 23 | +async fn start_authenticated_http(shared: Arc<SharedState>) -> AuthenticatedHttpEndpoint { |
| 24 | + let listener = tokio::net::TcpListener::bind("127.0.0.1:0") |
| 25 | + .await |
| 26 | + .expect("bind authenticated HTTP listener"); |
| 27 | + let local_addr = listener.local_addr().expect("authenticated HTTP address"); |
| 28 | + let (bus, _) = nodedb::control::shutdown::ShutdownBus::new(Arc::clone(&shared.shutdown)); |
| 29 | + let handle = tokio::spawn(async move { |
| 30 | + nodedb::control::server::http::server::run_with_listener( |
| 31 | + listener, |
| 32 | + shared, |
| 33 | + AuthMode::Password, |
| 34 | + None, |
| 35 | + bus, |
| 36 | + ) |
| 37 | + .await |
| 38 | + .expect("authenticated HTTP server"); |
| 39 | + }); |
| 40 | + tokio::time::sleep(Duration::from_millis(40)).await; |
| 41 | + |
| 42 | + AuthenticatedHttpEndpoint { |
| 43 | + local_addr, |
| 44 | + _server: handle, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +fn create_api_key(shared: &SharedState, username: &str, roles: Vec<Role>) -> String { |
| 49 | + let user_id = shared |
| 50 | + .credentials |
| 51 | + .create_service_account(username, TenantId::new(1), roles, vec![DatabaseId::DEFAULT]) |
| 52 | + .expect("create database-scoped service account"); |
| 53 | + shared |
| 54 | + .api_keys |
| 55 | + .create_key( |
| 56 | + CreateKeyParams { |
| 57 | + username, |
| 58 | + user_id, |
| 59 | + tenant_id: TenantId::new(1), |
| 60 | + expires_secs: 0, |
| 61 | + scope: vec![], |
| 62 | + accessible_databases: vec![DatabaseId::DEFAULT], |
| 63 | + }, |
| 64 | + Some(shared.credentials.catalog()), |
| 65 | + ) |
| 66 | + .expect("create API key") |
| 67 | +} |
| 68 | + |
| 69 | +async fn post_query(http: &AuthenticatedHttpEndpoint, token: &str, sql: &str) -> reqwest::Response { |
| 70 | + reqwest::Client::new() |
| 71 | + .post(format!("http://{}/v1/query", http.local_addr)) |
| 72 | + .header("Authorization", format!("Bearer {token}")) |
| 73 | + .json(&serde_json::json!({"sql": sql})) |
| 74 | + .send() |
| 75 | + .await |
| 76 | + .expect("POST authenticated query") |
| 77 | +} |
| 78 | + |
| 79 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 80 | +async fn query_rejects_database_outside_api_key_scope() { |
| 81 | + let srv = TestServer::start().await; |
| 82 | + srv.exec("CREATE DATABASE private_http_db") |
| 83 | + .await |
| 84 | + .expect("create inaccessible database"); |
| 85 | + let token = create_api_key(&srv.shared, "http_db_reader", vec![Role::ReadOnly]); |
| 86 | + let http = start_authenticated_http(Arc::clone(&srv.shared)).await; |
| 87 | + |
| 88 | + let response = reqwest::Client::new() |
| 89 | + .post(format!("http://{}/v1/query", http.local_addr)) |
| 90 | + .header("Authorization", format!("Bearer {token}")) |
| 91 | + .header("X-NodeDB-Database", "private_http_db") |
| 92 | + .json(&serde_json::json!({"sql": "SELECT 1"})) |
| 93 | + .send() |
| 94 | + .await |
| 95 | + .expect("POST cross-database query"); |
| 96 | + |
| 97 | + assert_eq!( |
| 98 | + response.status(), |
| 99 | + reqwest::StatusCode::FORBIDDEN, |
| 100 | + "HTTP queries must enforce the API key's database scope before planning or execution" |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 105 | +async fn query_rejects_cross_database_write_without_mutating_target() { |
| 106 | + let srv = TestServer::start().await; |
| 107 | + srv.exec("CREATE DATABASE private_write_db") |
| 108 | + .await |
| 109 | + .expect("create inaccessible write database"); |
| 110 | + srv.exec("USE DATABASE private_write_db") |
| 111 | + .await |
| 112 | + .expect("switch to write database as superuser"); |
| 113 | + srv.exec("CREATE COLLECTION private_write_rows") |
| 114 | + .await |
| 115 | + .expect("create private write collection"); |
| 116 | + srv.exec("USE DATABASE default") |
| 117 | + .await |
| 118 | + .expect("return to default database"); |
| 119 | + |
| 120 | + let token = create_api_key(&srv.shared, "http_db_writer", vec![Role::ReadWrite]); |
| 121 | + let http = start_authenticated_http(Arc::clone(&srv.shared)).await; |
| 122 | + let response = reqwest::Client::new() |
| 123 | + .post(format!("http://{}/v1/query", http.local_addr)) |
| 124 | + .header("Authorization", format!("Bearer {token}")) |
| 125 | + .header("X-NodeDB-Database", "private_write_db") |
| 126 | + .json(&serde_json::json!({ |
| 127 | + "sql": "INSERT INTO private_write_rows { id: 'forbidden', value: 5 }" |
| 128 | + })) |
| 129 | + .send() |
| 130 | + .await |
| 131 | + .expect("POST cross-database write"); |
| 132 | + |
| 133 | + srv.exec("USE DATABASE private_write_db") |
| 134 | + .await |
| 135 | + .expect("inspect write database"); |
| 136 | + let rows = srv |
| 137 | + .query_text("SELECT id FROM private_write_rows") |
| 138 | + .await |
| 139 | + .expect("query private write rows"); |
| 140 | + assert!( |
| 141 | + rows.is_empty(), |
| 142 | + "a cross-database write must not mutate the target: {rows:?}" |
| 143 | + ); |
| 144 | + assert_eq!( |
| 145 | + response.status(), |
| 146 | + reqwest::StatusCode::FORBIDDEN, |
| 147 | + "cross-database HTTP writes must be rejected" |
| 148 | + ); |
| 149 | +} |
| 150 | + |
| 151 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 152 | +async fn query_rejects_system_catalog_for_non_superuser() { |
| 153 | + let srv = TestServer::start().await; |
| 154 | + let token = create_api_key(&srv.shared, "http_catalog_reader", vec![Role::ReadOnly]); |
| 155 | + let http = start_authenticated_http(Arc::clone(&srv.shared)).await; |
| 156 | + |
| 157 | + let response = post_query(&http, &token, "SELECT * FROM _system.audit_log").await; |
| 158 | + |
| 159 | + assert_eq!( |
| 160 | + response.status(), |
| 161 | + reqwest::StatusCode::FORBIDDEN, |
| 162 | + "system catalog access must require a superuser on every SQL transport" |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +#[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 167 | +async fn query_honors_explicit_collection_grant_for_custom_role() { |
| 168 | + let srv = TestServer::start().await; |
| 169 | + srv.exec("CREATE COLLECTION granted_rows") |
| 170 | + .await |
| 171 | + .expect("create granted collection"); |
| 172 | + srv.exec("INSERT INTO granted_rows { id: 'visible', value: 9 }") |
| 173 | + .await |
| 174 | + .expect("seed granted collection"); |
| 175 | + |
| 176 | + let username = "http_explicit_reader"; |
| 177 | + let token = create_api_key( |
| 178 | + &srv.shared, |
| 179 | + username, |
| 180 | + vec![Role::Custom("http_explicit_role".into())], |
| 181 | + ); |
| 182 | + srv.shared |
| 183 | + .permissions |
| 184 | + .grant( |
| 185 | + &collection_target(TenantId::new(1), "granted_rows"), |
| 186 | + &format!("user:{username}"), |
| 187 | + Permission::Read, |
| 188 | + "nodedb", |
| 189 | + Some(srv.shared.credentials.catalog()), |
| 190 | + ) |
| 191 | + .expect("grant collection read"); |
| 192 | + let http = start_authenticated_http(Arc::clone(&srv.shared)).await; |
| 193 | + |
| 194 | + let response = post_query(&http, &token, "SELECT * FROM granted_rows").await; |
| 195 | + |
| 196 | + assert_eq!( |
| 197 | + response.status(), |
| 198 | + reqwest::StatusCode::OK, |
| 199 | + "HTTP authorization must honor PermissionStore grants before built-in role fallback" |
| 200 | + ); |
| 201 | +} |
0 commit comments