Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 55 additions & 5 deletions src/handlers/http/modal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@
*
*/

use std::{fmt, path::Path, sync::Arc, time::Duration};
use std::{
fmt,
path::Path,
sync::{
Arc,
atomic::{AtomicBool, Ordering},
},
time::Duration,
};

use actix_web::{App, HttpServer, http::KeepAlive, middleware::from_fn, web::ServiceConfig};
use actix_web_prometheus::PrometheusMetrics;
Expand All @@ -39,7 +47,7 @@ use crate::{
cli::Options,
hottier::{GLOBAL_HOTTIER, HotTierManager, StreamHotTier},
metastore::metastore_traits::MetastoreObject,
oauth::{OAuthProvider, connect_oidc},
oauth::{GlobalClient, OAuthProvider, connect_oidc},
option::Mode,
parseable::{DEFAULT_TENANT, PARSEABLE},
storage::{ObjectStorageProvider, PARSEABLE_ROOT_DIRECTORY},
Expand All @@ -59,6 +67,40 @@ pub mod utils;

pub static OIDC_CLIENT: OnceCell<Arc<RwLock<Box<dyn OAuthProvider>>>> = OnceCell::new();

/// True while OIDC is configured but the provider has not been discovered yet. The login endpoint then answers
/// 503 (try again) instead of behaving as if OIDC were not configured at all.
pub static OIDC_PENDING: AtomicBool = AtomicBool::new(false);

const OIDC_RETRY_INITIAL: Duration = Duration::from_secs(1);
const OIDC_RETRY_MAX: Duration = Duration::from_secs(60);

fn set_oidc_client(client: GlobalClient) {
OIDC_CLIENT.get_or_init(|| Arc::new(RwLock::new(Box::new(client) as Box<dyn OAuthProvider>)));
OIDC_PENDING.store(false, Ordering::Release);
}

/// Discovers the OIDC provider, retrying with capped exponential backoff until it answers.
///
/// Startup used to fail outright when the provider was unreachable, so an outage of the identity provider (or
/// simply starting Parseable before it) kept Parseable down and took basic-auth logins and ingestion with it.
async fn retry_oidc_discovery(config: crate::oidc::OpenidConfig) {
let mut delay = OIDC_RETRY_INITIAL;
loop {
tokio::time::sleep(delay).await;
match connect_oidc(config.clone()).await {
Ok(client) => {
set_oidc_client(client);
info!(issuer = %config.issuer, "OIDC provider discovered; OIDC login is enabled");
return;
}
Err(e) => {
delay = (delay * 2).min(OIDC_RETRY_MAX);
warn!(issuer = %config.issuer, retry_in_secs = delay.as_secs(), "OIDC discovery failed: {e}");
}
}
}
}

// to be decided on what the Default version should be
pub const DEFAULT_VERSION: &str = "v4";

Expand Down Expand Up @@ -92,9 +134,17 @@ pub trait ParseableServer {
Self: Sized,
{
if let Some(config) = oidc_client {
let gc = connect_oidc(config).await?;
OIDC_CLIENT
.get_or_init(|| Arc::new(RwLock::new(Box::new(gc) as Box<dyn OAuthProvider>)));
match connect_oidc(config.clone()).await {
Ok(gc) => set_oidc_client(gc),
Err(e) => {
warn!(
issuer = %config.issuer,
"OIDC discovery failed: {e}; starting without OIDC login and retrying in the background"
);
OIDC_PENDING.store(true, Ordering::Release);
tokio::spawn(retry_oidc_discovery(config));
}
}
}

// get the ssl stuff
Expand Down
15 changes: 14 additions & 1 deletion src/handlers/http/oidc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ use crate::handlers::http::modal::utils::rbac_utils::{get_metadata, put_metadata
use crate::{
handlers::{
COOKIE_AGE_DAYS, SESSION_COOKIE_NAME, USER_COOKIE_NAME, USER_ID_COOKIE_NAME,
http::{cluster::sync_user_creation, modal::OIDC_CLIENT},
http::{
cluster::sync_user_creation,
modal::{OIDC_CLIENT, OIDC_PENDING},
},
},
oauth::OAuthSession,
parseable::{DEFAULT_TENANT, PARSEABLE},
Expand Down Expand Up @@ -86,10 +89,17 @@ pub async fn login(
));
}

// Read the pending flag before the client. `set_oidc_client` installs the client before clearing the flag
// (release/acquire), so a request that sees "not pending" and no client really has no OIDC configured, and one
// racing with recovery sees either the installed client or "pending", never a false "not configured".
let oidc_pending = OIDC_PENDING.load(Ordering::Acquire);
let oidc_client = OIDC_CLIENT.get();

let session_key = extract_session_key_from_req(&req).ok();
let (session_key, oidc_client) = match (session_key, oidc_client) {
(None, None) if oidc_pending => {
return Err(OIDCError::ProviderUnavailable);
}
(None, None) => return Ok(redirect_no_oauth_setup(query.redirect.clone())),
(None, Some(client)) => {
let redirect = query.into_inner().redirect.to_string();
Expand Down Expand Up @@ -626,6 +636,8 @@ pub enum OIDCError {
BadRequest(String),
#[error("Unauthorized")]
Unauthorized,
#[error("The OIDC provider is not reachable yet; try again shortly")]
ProviderUnavailable,
}

impl actix_web::ResponseError for OIDCError {
Expand All @@ -635,6 +647,7 @@ impl actix_web::ResponseError for OIDCError {
Self::Serde(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::BadRequest(_) => StatusCode::BAD_REQUEST,
Self::Unauthorized => StatusCode::UNAUTHORIZED,
Self::ProviderUnavailable => StatusCode::SERVICE_UNAVAILABLE,
}
}

Expand Down
Loading