|
| 1 | +/* |
| 2 | + * Parseable Server (C) 2022 - 2023 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 std::net::SocketAddr; |
| 20 | + |
| 21 | +use arrow_array::RecordBatch; |
| 22 | +use arrow_flight::encode::FlightDataEncoderBuilder; |
| 23 | +use cookie::Cookie; |
| 24 | +use futures::stream::BoxStream; |
| 25 | +use futures_util::{Future, StreamExt, TryFutureExt, TryStreamExt}; |
| 26 | +use http_auth_basic::Credentials; |
| 27 | +use rand::distributions::{Alphanumeric, DistString}; |
| 28 | +use tonic::metadata::MetadataMap; |
| 29 | +use tonic::transport::Server; |
| 30 | +use tonic::{Request, Response, Status, Streaming}; |
| 31 | + |
| 32 | +use arrow_flight::{ |
| 33 | + flight_service_server::FlightService, flight_service_server::FlightServiceServer, Action, |
| 34 | + ActionType, Criteria, Empty, FlightData, FlightDescriptor, FlightInfo, HandshakeRequest, |
| 35 | + HandshakeResponse, PutResult, SchemaResult, Ticket, |
| 36 | +}; |
| 37 | +use tonic_web::GrpcWebLayer; |
| 38 | +use tower_http::cors::{Any, CorsLayer}; |
| 39 | + |
| 40 | +use crate::livetail::{Message, LIVETAIL}; |
| 41 | +use crate::metadata::STREAM_INFO; |
| 42 | +use crate::option::CONFIG; |
| 43 | +use crate::rbac::map::SessionKey; |
| 44 | +use crate::rbac::{self, Users}; |
| 45 | +use crate::utils; |
| 46 | + |
| 47 | +use super::SESSION_COOKIE_NAME; |
| 48 | + |
| 49 | +#[derive(Clone)] |
| 50 | +pub struct FlightServiceImpl {} |
| 51 | + |
| 52 | +#[tonic::async_trait] |
| 53 | +impl FlightService for FlightServiceImpl { |
| 54 | + type HandshakeStream = BoxStream<'static, Result<HandshakeResponse, Status>>; |
| 55 | + type ListFlightsStream = BoxStream<'static, Result<FlightInfo, Status>>; |
| 56 | + type DoGetStream = BoxStream<'static, Result<FlightData, Status>>; |
| 57 | + type DoPutStream = BoxStream<'static, Result<PutResult, Status>>; |
| 58 | + type DoActionStream = BoxStream<'static, Result<arrow_flight::Result, Status>>; |
| 59 | + type ListActionsStream = BoxStream<'static, Result<ActionType, Status>>; |
| 60 | + type DoExchangeStream = BoxStream<'static, Result<FlightData, Status>>; |
| 61 | + |
| 62 | + async fn handshake( |
| 63 | + &self, |
| 64 | + _request: Request<Streaming<HandshakeRequest>>, |
| 65 | + ) -> Result<Response<Self::HandshakeStream>, Status> { |
| 66 | + Err(Status::unimplemented( |
| 67 | + "handshake is disabled in favour of direct authentication and authorization", |
| 68 | + )) |
| 69 | + } |
| 70 | + |
| 71 | + async fn list_flights( |
| 72 | + &self, |
| 73 | + _request: Request<Criteria>, |
| 74 | + ) -> Result<Response<Self::ListFlightsStream>, Status> { |
| 75 | + Err(Status::unimplemented("Implement list_flights")) |
| 76 | + } |
| 77 | + |
| 78 | + async fn get_flight_info( |
| 79 | + &self, |
| 80 | + _request: Request<FlightDescriptor>, |
| 81 | + ) -> Result<Response<FlightInfo>, Status> { |
| 82 | + Err(Status::unimplemented("Implement get_flight_info")) |
| 83 | + } |
| 84 | + |
| 85 | + async fn get_schema( |
| 86 | + &self, |
| 87 | + _request: Request<FlightDescriptor>, |
| 88 | + ) -> Result<Response<SchemaResult>, Status> { |
| 89 | + Err(Status::unimplemented("Implement get_schema")) |
| 90 | + } |
| 91 | + |
| 92 | + async fn do_get(&self, req: Request<Ticket>) -> Result<Response<Self::DoGetStream>, Status> { |
| 93 | + let key = extract_session_key(req.metadata())?; |
| 94 | + let ticket: serde_json::Value = serde_json::from_slice(&req.into_inner().ticket) |
| 95 | + .map_err(|err| Status::internal(err.to_string()))?; |
| 96 | + let stream = extract_stream(&ticket)?; |
| 97 | + log::info!("livetail requested for stream {}", stream); |
| 98 | + match Users.authorize(key, rbac::role::Action::Query, Some(stream), None) { |
| 99 | + rbac::Response::Authorized => (), |
| 100 | + rbac::Response::UnAuthorized => { |
| 101 | + return Err(Status::permission_denied( |
| 102 | + "user is not authenticated to access this resource", |
| 103 | + )) |
| 104 | + } |
| 105 | + rbac::Response::ReloadRequired => { |
| 106 | + return Err(Status::unauthenticated("reload required")) |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + let schema = STREAM_INFO |
| 111 | + .schema(stream) |
| 112 | + .map_err(|err| Status::failed_precondition(err.to_string()))?; |
| 113 | + |
| 114 | + let rx = LIVETAIL.new_pipe( |
| 115 | + Alphanumeric.sample_string(&mut rand::thread_rng(), 32), |
| 116 | + stream.to_string(), |
| 117 | + ); |
| 118 | + |
| 119 | + let adapter_schema = schema.clone(); |
| 120 | + let rx = rx.map(move |x| match x { |
| 121 | + Message::Record(t) => Ok(utils::arrow::adapt_batch(&adapter_schema, &t)), |
| 122 | + Message::Skipped(_) => { |
| 123 | + log::warn!("livetail channel capacity is full."); |
| 124 | + Ok(RecordBatch::new_empty(adapter_schema.clone())) |
| 125 | + } |
| 126 | + }); |
| 127 | + |
| 128 | + let rb_stream = FlightDataEncoderBuilder::new() |
| 129 | + .with_schema(schema) |
| 130 | + .build(rx); |
| 131 | + |
| 132 | + let rb_stream = rb_stream.map_err(|err| Status::unknown(err.to_string())); |
| 133 | + Ok(Response::new(Box::pin(rb_stream))) |
| 134 | + } |
| 135 | + |
| 136 | + async fn do_put( |
| 137 | + &self, |
| 138 | + _request: Request<Streaming<FlightData>>, |
| 139 | + ) -> Result<Response<Self::DoPutStream>, Status> { |
| 140 | + Err(Status::unimplemented("Implement do_put")) |
| 141 | + } |
| 142 | + |
| 143 | + async fn do_action( |
| 144 | + &self, |
| 145 | + _request: Request<Action>, |
| 146 | + ) -> Result<Response<Self::DoActionStream>, Status> { |
| 147 | + Err(Status::unimplemented("Implement do_action")) |
| 148 | + } |
| 149 | + |
| 150 | + async fn list_actions( |
| 151 | + &self, |
| 152 | + _request: Request<Empty>, |
| 153 | + ) -> Result<Response<Self::ListActionsStream>, Status> { |
| 154 | + Err(Status::unimplemented("Implement list_actions")) |
| 155 | + } |
| 156 | + |
| 157 | + async fn do_exchange( |
| 158 | + &self, |
| 159 | + _request: Request<Streaming<FlightData>>, |
| 160 | + ) -> Result<Response<Self::DoExchangeStream>, Status> { |
| 161 | + Err(Status::unimplemented("Implement do_exchange")) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +pub fn server() -> impl Future<Output = Result<(), Box<dyn std::error::Error + Send>>> + Send { |
| 166 | + let mut addr: SocketAddr = CONFIG |
| 167 | + .parseable |
| 168 | + .address |
| 169 | + .parse() |
| 170 | + .expect("valid socket address"); |
| 171 | + addr.set_port(CONFIG.parseable.grpc_port); |
| 172 | + |
| 173 | + let service = FlightServiceImpl {}; |
| 174 | + |
| 175 | + let svc = FlightServiceServer::new(service); |
| 176 | + |
| 177 | + let cors = CorsLayer::new() |
| 178 | + // allow `GET` and `POST` when accessing the resource |
| 179 | + .allow_methods(Any) |
| 180 | + .allow_headers(Any) |
| 181 | + .allow_origin(Any); |
| 182 | + // allow requests from any origin |
| 183 | + |
| 184 | + Server::builder() |
| 185 | + .accept_http1(true) |
| 186 | + .layer(cors) |
| 187 | + .layer(GrpcWebLayer::new()) |
| 188 | + .add_service(svc) |
| 189 | + .serve(addr) |
| 190 | + .map_err(|err| Box::new(err) as Box<dyn std::error::Error + Send>) |
| 191 | +} |
| 192 | + |
| 193 | +fn extract_stream(body: &serde_json::Value) -> Result<&str, Status> { |
| 194 | + body.as_object() |
| 195 | + .ok_or(Status::invalid_argument("expected object in request body"))? |
| 196 | + .get("stream") |
| 197 | + .ok_or(Status::invalid_argument("stream key value is not provided"))? |
| 198 | + .as_str() |
| 199 | + .ok_or(Status::invalid_argument("stream key value is invalid")) |
| 200 | +} |
| 201 | + |
| 202 | +fn extract_session_key(headers: &MetadataMap) -> Result<SessionKey, Status> { |
| 203 | + // Extract username and password from the request using basic auth extractor. |
| 204 | + let basic = extract_basic_auth(headers).map(|creds| SessionKey::BasicAuth { |
| 205 | + username: creds.user_id, |
| 206 | + password: creds.password, |
| 207 | + }); |
| 208 | + |
| 209 | + if let Some(basic) = basic { |
| 210 | + return Ok(basic); |
| 211 | + } |
| 212 | + |
| 213 | + let session = extract_cookie(headers) |
| 214 | + .map(|cookie| ulid::Ulid::from_string(cookie.value())) |
| 215 | + .transpose() |
| 216 | + .map_err(|_| Status::invalid_argument("Cookie is tampered with or invalid"))?; |
| 217 | + |
| 218 | + if let Some(session) = session { |
| 219 | + return Ok(SessionKey::SessionId(session)); |
| 220 | + } |
| 221 | + |
| 222 | + Err(Status::unauthenticated("No authentication method supplied")) |
| 223 | +} |
| 224 | + |
| 225 | +fn extract_basic_auth(header: &MetadataMap) -> Option<Credentials> { |
| 226 | + let creds = header |
| 227 | + .get("Authorization") |
| 228 | + .and_then(|value| value.to_str().ok()) |
| 229 | + .and_then(|value| Credentials::from_header(value.to_string()).ok()); |
| 230 | + creds |
| 231 | +} |
| 232 | + |
| 233 | +fn extract_cookie(header: &MetadataMap) -> Option<Cookie> { |
| 234 | + let cookies = header |
| 235 | + .get("Cookies") |
| 236 | + .and_then(|value| value.to_str().ok()) |
| 237 | + .map(Cookie::split_parse)?; |
| 238 | + |
| 239 | + cookies |
| 240 | + .flatten() |
| 241 | + .find(|cookie| cookie.name() == SESSION_COOKIE_NAME) |
| 242 | +} |
0 commit comments