Skip to content

Commit

Permalink
Fix flaky oidc tests (#2000)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdt authored Nov 20, 2024
1 parent 9c64d1f commit 3f0e152
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,4 @@ env_logger.workspace = true
pretty_assertions.workspace = true
jsonwebtoken.workspace = true
axum.workspace = true
reqwest.workspace = true
68 changes: 62 additions & 6 deletions crates/core/src/auth/token_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,14 @@ impl TokenValidator for OidcTokenValidator {
let raw_issuer = get_raw_issuer(token)?;
// TODO: Consider checking for trailing slashes or requiring a scheme.
let oidc_url = format!("{}/.well-known/openid-configuration", raw_issuer);
let keys = Jwks::from_oidc_url(oidc_url).await?;
log::debug!("Fetching key for issuer {}", raw_issuer.clone());
let key_or_error = Jwks::from_oidc_url(oidc_url).await;
// TODO: We should probably add debouncing to avoid spamming the logs.
// Alternatively we could add a backoff before retrying.
if let Err(e) = &key_or_error {
log::warn!("Error fetching public key for issuer {}: {:?}", raw_issuer, e);
}
let keys = key_or_error?;
let validator = JwksValidator {
issuer: raw_issuer,
keyset: keys,
Expand Down Expand Up @@ -317,6 +324,8 @@ impl TokenValidator for JwksValidator {

#[cfg(test)]
mod tests {
use std::time::Duration;

use crate::auth::identity::{IncomingClaims, SpacetimeIdentityClaims};
use crate::auth::token_validation::{
BasicTokenValidator, CachingOidcTokenValidator, FullTokenValidator, OidcTokenValidator, TokenSigner,
Expand Down Expand Up @@ -533,6 +542,29 @@ mod tests {
.unwrap();
});

// Wait for server to be ready
let client = reqwest::Client::new();
let health_check_url = format!("{}/ok", base_url);

let mut attempts = 0;
const MAX_ATTEMPTS: u32 = 10;
const DELAY_MS: u64 = 50;

while attempts < MAX_ATTEMPTS {
match client.get(&health_check_url).send().await {
Ok(response) if response.status().is_success() => break,
_ => {
log::debug!("Server not ready. Waiting...");
tokio::time::sleep(Duration::from_millis(DELAY_MS)).await;
attempts += 1;
}
}
}

if attempts == MAX_ATTEMPTS {
return Err(anyhow::anyhow!("Server failed to start after maximum attempts"));
}

Ok(OIDCServerHandle {
base_url,
shutdown_tx,
Expand Down Expand Up @@ -590,13 +622,19 @@ mod tests {

#[tokio::test]
async fn test_oidc_flow() -> anyhow::Result<()> {
run_oidc_test(OidcTokenValidator).await
for _ in 0..10 {
run_oidc_test(OidcTokenValidator).await?
}
Ok(())
}

#[tokio::test]
async fn test_caching_oidc_flow() -> anyhow::Result<()> {
let v = CachingOidcTokenValidator::get_default();
run_oidc_test(v).await
for _ in 0..10 {
let v = CachingOidcTokenValidator::get_default();
run_oidc_test(v).await?;
}
Ok(())
}

#[tokio::test]
Expand Down Expand Up @@ -639,8 +677,26 @@ mod tests {
let mut y = openssl::bn::BigNum::new()?;
eck.public_key().affine_coordinates(&group, &mut x, &mut y, &mut ctx)?;

let x_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(x.to_vec());
let y_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(y.to_vec());
let x_bytes = x.to_vec();
let y_bytes = y.to_vec();

let x_padded = if x_bytes.len() < 32 {
let mut padded = vec![0u8; 32];
padded[32 - x_bytes.len()..].copy_from_slice(&x_bytes);
padded
} else {
x_bytes
};

let y_padded = if y_bytes.len() < 32 {
let mut padded = vec![0u8; 32];
padded[32 - y_bytes.len()..].copy_from_slice(&y_bytes);
padded
} else {
y_bytes
};
let x_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(x_padded);
let y_b64 = base64::prelude::BASE64_URL_SAFE_NO_PAD.encode(y_padded);

let mut jwks = serde_json::json!(
{
Expand Down

2 comments on commit 3f0e152

@github-actions
Copy link

@github-actions github-actions bot commented on 3f0e152 Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 3f0e152 Nov 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Callgrind benchmark results

Callgrind Benchmark Report

These benchmarks were run using callgrind,
an instruction-level profiler. They allow comparisons between sqlite (sqlite), SpacetimeDB running through a module (stdb_module), and the underlying SpacetimeDB data storage engine (stdb_raw). Callgrind emulates a CPU to collect the below estimates.

Measurement changes larger than five percent are in bold.

In-memory benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 6426 6426 0.00% 6594 6526 1.04%
sqlite 5579 5579 0.00% 6071 6039 0.53%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 1 u64 76579 76586 -0.01% 77243 76974 0.35%
stdb_raw u32_u64_str no_index 64 128 2 string 119910 118828 0.91% 120860 119446 1.18%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 25112 25126 -0.06% 25770 25620 0.59%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 24078 24078 0.00% 24666 24462 0.83%
sqlite u32_u64_str no_index 64 128 2 string 144695 144695 0.00% 146097 146085 0.01%
sqlite u32_u64_str no_index 64 128 1 u64 124044 124044 0.00% 125200 125204 -0.00%
sqlite u32_u64_str btree_each_column 64 128 1 u64 131361 131361 0.00% 132621 132673 -0.04%
sqlite u32_u64_str btree_each_column 64 128 2 string 134494 134494 0.00% 136076 135972 0.08%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 879002 878642 0.04% 925610 897890 3.09%
stdb_raw u32_u64_str btree_each_column 64 128 1025700 1029707 -0.39% 1052160 1086439 -3.16%
sqlite u32_u64_str unique_0 64 128 398320 398320 0.00% 414832 417048 -0.53%
sqlite u32_u64_str btree_each_column 64 128 983643 983637 0.00% 1022749 1019991 0.27%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 153810 153810 0.00% 153970 153926 0.03%
stdb_raw u32_u64_str unique_0 64 16835 16835 0.00% 16979 16935 0.26%
sqlite u32_u64_str unique_0 1024 1068281 1068275 0.00% 1071675 1071443 0.02%
sqlite u32_u64_str unique_0 64 76261 76261 0.00% 77231 77207 0.03%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47528 47528 0.00% 50214 50214 0.00%
64 bsatn 25509 25509 0.00% 27787 27787 0.00%
16 bsatn 8200 8200 0.00% 9594 9594 0.00%
16 json 12188 12188 0.00% 14126 14126 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 20547693 20132320 2.06% 21041767 20581686 2.24%
stdb_raw u32_u64_str unique_0 64 128 1288257 1287088 0.09% 1321797 1320788 0.08%
sqlite u32_u64_str unique_0 1024 1024 1802182 1802182 0.00% 1811350 1811422 -0.00%
sqlite u32_u64_str unique_0 64 128 128528 128528 0.00% 131370 131418 -0.04%
On-disk benchmarks

callgrind: empty transaction

db total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw 6431 6431 0.00% 6603 6527 1.16%
sqlite 5621 5621 0.00% 6165 6149 0.26%

callgrind: filter

db schema indices count preload _column data_type total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str no_index 64 128 1 u64 76584 76591 -0.01% 77240 76955 0.37%
stdb_raw u32_u64_str no_index 64 128 2 string 118826 118833 -0.01% 119644 119531 0.09%
stdb_raw u32_u64_str btree_each_column 64 128 2 string 25118 25115 0.01% 25748 25569 0.70%
stdb_raw u32_u64_str btree_each_column 64 128 1 u64 24083 24083 0.00% 24655 24447 0.85%
sqlite u32_u64_str no_index 64 128 1 u64 125965 125965 0.00% 127429 127461 -0.03%
sqlite u32_u64_str no_index 64 128 2 string 146616 146616 0.00% 148330 148354 -0.02%
sqlite u32_u64_str btree_each_column 64 128 2 string 136616 136634 -0.01% 138644 138678 -0.02%
sqlite u32_u64_str btree_each_column 64 128 1 u64 133457 133457 0.00% 135283 135215 0.05%

callgrind: insert bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 64 128 828207 827836 0.04% 843009 878606 -4.05%
stdb_raw u32_u64_str btree_each_column 64 128 978552 978914 -0.04% 1034618 1035576 -0.09%
sqlite u32_u64_str unique_0 64 128 415857 415857 0.00% 431635 433783 -0.50%
sqlite u32_u64_str btree_each_column 64 128 1021898 1021898 0.00% 1059770 1056934 0.27%

callgrind: iterate

db schema indices count total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 153815 153815 0.00% 153959 153915 0.03%
stdb_raw u32_u64_str unique_0 64 16840 16840 0.00% 16984 16940 0.26%
sqlite u32_u64_str unique_0 1024 1071343 1071343 0.00% 1075065 1074993 0.01%
sqlite u32_u64_str unique_0 64 78033 78033 0.00% 79343 79279 0.08%

callgrind: serialize_product_value

count format total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
64 json 47528 47528 0.00% 50214 50214 0.00%
64 bsatn 25509 25509 0.00% 27787 27787 0.00%
16 bsatn 8200 8200 0.00% 9594 9594 0.00%
16 json 12188 12188 0.00% 14126 14126 0.00%

callgrind: update bulk

db schema indices count preload total reads + writes old total reads + writes Δrw estimated cycles old estimated cycles Δcycles
stdb_raw u32_u64_str unique_0 1024 1024 19055832 19049442 0.03% 19571954 19602572 -0.16%
stdb_raw u32_u64_str unique_0 64 128 1241227 1241286 -0.00% 1304039 1306940 -0.22%
sqlite u32_u64_str unique_0 1024 1024 1809743 1809743 0.00% 1818407 1818347 0.00%
sqlite u32_u64_str unique_0 64 128 132654 132654 0.00% 135524 135560 -0.03%

Please sign in to comment.