diff --git a/Justfile b/Justfile index 50c8cb6a6..23f0b62f9 100644 --- a/Justfile +++ b/Justfile @@ -24,12 +24,16 @@ copy-stg *args: backend-dev *args: dotenvx run -f .env -f .env.shared -- bash ./mvnw -Dspring-boot.run.profiles=dev spring-boot:run {{args}} +# Run the leetcode auth service +las-dev *args: + cd ./internal/leetcode-auth-service && cargo build && dotenvx run -f ../../.env -- cargo run + # Run the leetcode auth stealer service -auth-dev *args: +lass-dev *args: cd ./internal/leetcode-auth-stealer-service && uv sync && GRPC_REFLECTION=true GRPC_VERBOSITY=ERROR uv run src/server.py # Generate the leetcode auth stealer gRPC stubs for the main codebloom service -auth-gen *args: +lass-gen *args: cd ./internal/leetcode-auth-stealer-service && uv sync && uv run src/generate.py # Run the backend Spring server with an exposed debugger at :5005 @@ -85,7 +89,7 @@ email-gen *args: # Run the dev servers (backend & frontend & auth service) dev *args: - npx concurrently "just backend-dev" "just frontend-dev" "just auth-dev" {{args}} + npx mprocs "just backend-dev" "just frontend-dev" "just las-dev" "just lass-dev" {{args}} # Run the dev servers (backend & frontend & auth service) but the backend will launch a debugger server. devd *args: diff --git a/example.env b/example.env index 83d173381..e42597e77 100644 --- a/example.env +++ b/example.env @@ -26,13 +26,6 @@ DISCORD_REPORTING_CHANNEL_ID= DISCORD_REPORTING_LOG_GUILD_ID= DISCORD_REPORTING_LOG_CHANNEL_ID= -# required for auth. -# sent to LASS to get an auth token for leetcode.com -# for local, you can just create a regular account on leetcode.com -# and put the username & password below. -LEETCODE_AUTH_USERNAME= -LEETCODE_AUTH_PASSWORD= - # unset will point LASS (Leetcode Auth Stealer Service) to localhost:50051 # uncomment these to use staging LASS (must be on VPN). # GRPC_LASS_HOST=stg.codebloom-leetcode-auth-stealer-service.vpn.patinanetwork.org:50051 @@ -47,10 +40,6 @@ GRPC_LASS_TIMEOUT= GH_USERNAME= GH_PASSWORD= -# the new default auth stealer configuration -LEETCODE_AUTH_USERNAME= -LEETCODE_AUTH_PASSWORD= - # optional for auth, can skip # You must attach the email used for the Github account, as the GitHub page may require a 2FA code. # As of right now, you must use port 143 as it is configured to use unencrypted IMAP. @@ -87,6 +76,13 @@ SECRET_KEY= # needs to go through the frontend. SERVER_URL=http://localhost:5173 -# required -# Redis URL +# read by leetcode-auth-service +LEETCODE_AUTH_USERNAME= +LEETCODE_AUTH_PASSWORD= REDIS_URL=redis://localhost:6379/0 +LASS_URL=http://localhost:50051 +LASS_TIMEOUT_SECONDS=180 +REFRESH_LOCK_TTL_SECONDS=210 +REFRESH_CHECK_INTERVAL_SECONDS=900 +TOKEN_MAX_AGE_SECONDS=14400 +GRPC_REFLECTION=true diff --git a/internal/README.md b/internal/README.md index f60a92966..90d079c57 100644 --- a/internal/README.md +++ b/internal/README.md @@ -4,12 +4,14 @@ This directory houses applications & services that are more related to the devel ## Structure -_Last updated: 02/14/2026_ +_Last updated: 11/03/2026_ This `internal/` directory currently consists of - `standup-bot`: a Discord bot written in [Rust](https://rust-lang.org/) to send daily standup messages to our private Discord server. - [View detailed documentation](./standup-bot/README.md) - [View detailed documentation](./scripts/README.md) +- `leetcode-auth-service`: a Rust gRPC service that stores the leetcode.com credentials in Redis and refreshes them through `leetcode-auth-stealer-service`. + - [View detailed documentation](./leetcode-auth-service/README.md) - `leetcode-auth-stealer-service`: a Python gRPC authentication service that replaces the old auth stealer service with a direct email/password one. - [View detailed documentation](./leetcode-auth-stealer-service/README.md) diff --git a/internal/leetcode-auth-service/.gitignore b/internal/leetcode-auth-service/.gitignore new file mode 100644 index 000000000..4b32e15f1 --- /dev/null +++ b/internal/leetcode-auth-service/.gitignore @@ -0,0 +1,7 @@ +src/proto + +/target +.env + +clippy-report.json +lcov.info diff --git a/internal/leetcode-auth-service/Cargo.lock b/internal/leetcode-auth-service/Cargo.lock new file mode 100644 index 000000000..d99a49aac --- /dev/null +++ b/internal/leetcode-auth-service/Cargo.lock @@ -0,0 +1,2685 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "aho-corasick" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba" +dependencies = [ + "memchr", +] + +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + +[[package]] +name = "anyhow" +version = "1.0.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" + +[[package]] +name = "arc-swap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "380962b3d93b99642e50b9febf72c071e97e8b32d86eae9cf4ef582bb960ae6e" +dependencies = [ + "rustversion", +] + +[[package]] +name = "arcstr" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03918c3dbd7701a85c6b9887732e2921175f26c350b4563841d0958c21d57e6d" + +[[package]] +name = "async-lock" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0876700b0d007ad25d5508865a4259c3af9af6d296bf663a0d586d0bc3369f6f" +dependencies = [ + "event-listener", + "event-listener-strategy", + "pin-project-lite", +] + +[[package]] +name = "async-trait" +version = "0.1.92" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82f6aeea286b8eb4dd3431a1be1b59d290ace00f5bfd8e2a159bc2a05e2c1667" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "atomic-waker" +version = "1.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" + +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + +[[package]] +name = "aws-lc-rs" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "048006790404103c488e6c5616b3e7e702a71292aadda08e8f7698a9e0d2eb53" +dependencies = [ + "aws-lc-sys", + "zeroize", +] + +[[package]] +name = "aws-lc-sys" +version = "0.47.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bfbe32ac36b3c428696ce315464181bc4029d689fd58e3f2a4e58c252242f52" +dependencies = [ + "cc", + "cmake", + "dunce", + "fs_extra", + "pkg-config", +] + +[[package]] +name = "axum" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "31b698c5f9a010f6573133b09e0de5408834d0c82f8d7475a89fc1867a71cd90" +dependencies = [ + "axum-core", + "bytes", + "form_urlencoded", + "futures-util", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-util", + "itoa", + "matchit", + "memchr", + "mime", + "percent-encoding", + "pin-project-lite", + "serde_core", + "serde_json", + "serde_path_to_error", + "serde_urlencoded", + "sync_wrapper", + "tokio", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "axum-core" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08c78f31d7b1291f7ee735c1c6780ccde7785daae9a9206026862dab7d8792d1" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "http-body-util", + "mime", + "pin-project-lite", + "sync_wrapper", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "backon" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cffb0e931875b666fc4fcb20fee52e9bbd1ef836fd9e9e04ec21555f9f85f7ef" +dependencies = [ + "fastrand", +] + +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + +[[package]] +name = "bindgen" +version = "0.72.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "993776b509cfb49c750f11b8f07a46fa23e0a1386ffc01fb1e7d343efc387895" +dependencies = [ + "bitflags", + "cexpr", + "clang-sys", + "itertools 0.13.0", + "proc-macro2", + "quote", + "regex", + "rustc-hash", + "shlex 1.3.0", + "syn 2.0.119", +] + +[[package]] +name = "bitflags" +version = "2.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06" + +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + +[[package]] +name = "bytes" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04" + +[[package]] +name = "cc" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "375989decec1a0fa424442bed520fa12ede43a48475ce8c7417d2c69b32b5f6b" +dependencies = [ + "find-msvc-tools", + "jobserver", + "libc", + "shlex 2.0.1", +] + +[[package]] +name = "cexpr" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6fac387a98bb7c37292057cffc56d62ecb629900026402633ae9160df93a8766" +dependencies = [ + "nom", +] + +[[package]] +name = "cfg-if" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600" + +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "num-traits", + "windows-link", +] + +[[package]] +name = "clang-sys" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "157a8ba7b480713b56f4c09fd13fc3e0a22a5dfab8097ba61cbc5feef950788a" +dependencies = [ + "glob", + "libc", + "libloading", +] + +[[package]] +name = "cmake" +version = "0.1.58" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0f78a02292a74a88ac736019ab962ece0bc380e3f977bf72e376c5d78ff0678" +dependencies = [ + "cc", +] + +[[package]] +name = "combine" +version = "4.6.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4356eba6976435498d05a311ee9fc486da3bc81c200d3f0d7dd9703b41913f92" +dependencies = [ + "bytes", + "futures-core", + "memchr", + "pin-project-lite", + "tokio", + "tokio-util", +] + +[[package]] +name = "core-foundation" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2a6cd9ae233e7f62ba4e9353e81a88df7fc8a5987b8d445b4d90c879bd156f6" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + +[[package]] +name = "displaydoc" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "dunce" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813" + +[[package]] +name = "either" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be" + +[[package]] +name = "equivalent" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826" + +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "event-listener" +version = "5.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a23add41df1562121a9393cb065eab5146a1242410f23a644851e90cfd669d2" +dependencies = [ + "parking", + "pin-project-lite", +] + +[[package]] +name = "event-listener-strategy" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8be9f3dfaaffdae2972880079a491a1a8bb7cbed0b8dd7a347f668b4150a3b93" +dependencies = [ + "event-listener", + "pin-project-lite", +] + +[[package]] +name = "evmap" +version = "11.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b8874945f036109c72242964c1174cf99434e30cfa45bf45fedc983f50046f8" +dependencies = [ + "hashbag", + "left-right", + "smallvec", +] + +[[package]] +name = "fastrand" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223" + +[[package]] +name = "find-msvc-tools" +version = "0.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" + +[[package]] +name = "fixedbitset" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d674e81391d1e1ab681a28d99df07927c6d4aa5b027d7da16ba32d1d21ecd99" + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "foldhash" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9c4f5dac5e15c24eb999c26181a6ca40b39fe946cbe4c263c7209467bc83af2" + +[[package]] +name = "foldhash" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "fs_extra" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42703706b716c37f96a77aea830392ad231f44c9e9a67872fa5548707e11b11c" + +[[package]] +name = "futures-channel" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f9e3d69d39e4862ffed03ed071a76f9a13ba1d9109d355b0f0aa6b15e393c4" +dependencies = [ + "futures-core", +] + +[[package]] +name = "futures-core" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92d699e522242e69e3003b94ecc1f960f3a5e015aa7c5d7486e65ad01dd94f5e" + +[[package]] +name = "futures-macro" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9fb9654ba8355388abeb8dcb4fc62f511300867002afc858860463bdd9fe0c44" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "futures-sink" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1944426bf7d03f1d14f708785e4b33efd750b36d48a157b836b3efc15ede8e1d" + +[[package]] +name = "futures-task" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd417de3d1d015fc3bfd2b1ea46dfc7bab72ef86f1cc7cc9c78e728b34a6d1fd" + +[[package]] +name = "futures-util" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0d50a92467f8ba5dd6e3ee5d4bd04d73ab2e4e1c44474a0674821dfce14b79bc" +dependencies = [ + "futures-core", + "futures-macro", + "futures-sink", + "futures-task", + "pin-project-lite", + "slab", +] + +[[package]] +name = "generator" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1be81b31c9ce4caf7f3178eeb90662bb1864d3ad363ec6bdada3201adff19c37" +dependencies = [ + "cc", + "cfg-if", + "libc", + "log", + "rustversion", + "windows-link", + "windows-result", +] + +[[package]] +name = "getrandom" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi 5.3.0", + "wasip2", +] + +[[package]] +name = "getrandom" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099" +dependencies = [ + "cfg-if", + "libc", + "r-efi 6.0.0", +] + +[[package]] +name = "glob" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4eba85ea1d0a966a983acd07deee566e67395d2d96b6fb39e62b5a833f1eb0b" + +[[package]] +name = "h2" +version = "0.4.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d29020232d6aa3fb1daca64c1127cf662cf97f254ae16c18c05b8ab635fc118" +dependencies = [ + "atomic-waker", + "bytes", + "fnv", + "futures-core", + "futures-sink", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbag" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7040a10f52cba493ddb09926e15d10a9d8a28043708a405931fe4c6f19fac064" + +[[package]] +name = "hashbrown" +version = "0.15.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9229cfe53dfd69f0609a49f65461bd93001ea1ef889cd5529dd176593f5338a1" +dependencies = [ + "foldhash 0.1.5", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" +dependencies = [ + "foldhash 0.2.0", +] + +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "http-body" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca2a8f2913ee65f60facd6a5905613afaa448497a0230cc41ce022d93290bc2c" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "http-body-util" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23169fe34a5fbcdd3f3862e78fb9b6fccd5f02a6dc6f732547005d45631ce71c" +dependencies = [ + "bytes", + "futures-core", + "http", + "http-body", + "pin-project-lite", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "hyper" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c3e324da4c95177d6291d4c8730197c0d1822f8a9766814a4a44fa5ab797c9c" +dependencies = [ + "atomic-waker", + "bytes", + "futures-channel", + "futures-core", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project-lite", + "smallvec", + "tokio", + "want", +] + +[[package]] +name = "hyper-rustls" +version = "0.27.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfa8e654703247911e29c23fbeaa261834bd9bb74efba2f9acddc37bfb127f53" +dependencies = [ + "http", + "hyper", + "hyper-util", + "rustls", + "rustls-native-certs", + "tokio", + "tokio-rustls", + "tower-service", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", +] + +[[package]] +name = "hyper-util" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ddc03d96684f9226b8a787cdb71488417b53ab5ea8fdb1dac946cb9431cc8bff" +dependencies = [ + "bytes", + "futures-channel", + "futures-util", + "http", + "http-body", + "httparse", + "hyper", + "libc", + "pin-project-lite", + "socket2", + "tokio", + "tower-service", + "tracing", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "icu_collections" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe090a42a0d3140fed8c63263ed1fecc02422cb2f4bfe55cd8de2de1b8e105dd" +dependencies = [ + "displaydoc", + "potential_utf", + "utf8_iter", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25d29b57c17c6528d0dc4c2141cfa2d08200465acb537a3909c8b9b8a4a984c0" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "404a3b8c486f205c87fe7f23ab6894d84a4ad76d6e4671ae1981ba7def87c599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20f0088bfb7441f7a37fa0c9e20cb1e9cba1cac6b9ce8cd2a6dc44294f8e8cbc" + +[[package]] +name = "icu_properties" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea83d991c56158f7cf898bf86436c1dfe7c07ee82fe88951f2695c9f37a379f" +dependencies = [ + "displaydoc", + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98f29af82d02b163fe50161388350e743bb3107cc8e8c2f7314cb505faddfcf" + +[[package]] +name = "icu_provider" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413099315636c588ee8f011190b329b971a09a8fae9d157e01dcae8379468514" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indexmap" +version = "2.14.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855" +dependencies = [ + "equivalent", + "hashbrown 0.17.1", +] + +[[package]] +name = "ipnet" +version = "2.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "791930b43c0d5973160d90a8f3894509f2b273430f5c5c73b668636d0287c5c0" + +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + +[[package]] +name = "itertools" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285" +dependencies = [ + "either", +] + +[[package]] +name = "itoa" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" + +[[package]] +name = "jobserver" +version = "0.1.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3" +dependencies = [ + "getrandom 0.4.3", + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8dc09d2154098bbf4ef54eab6b117a3c24cbbffe94046b16ee6d6d4510584d3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + +[[package]] +name = "leetcode-auth-service" +version = "0.1.0" +dependencies = [ + "anyhow", + "axum", + "bytes", + "chrono", + "http-body", + "metrics", + "metrics-exporter-prometheus", + "metrics-process", + "parking_lot", + "pin-project-lite", + "prost", + "redis", + "tokio", + "tonic", + "tonic-health", + "tonic-middleware", + "tonic-prost", + "tonic-prost-build", + "tonic-reflection", + "tower-http", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "left-right" +version = "0.11.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8bc015ded5d9b3054dbbdb63332cdd6ee42352ccef19e911e25117490e2f48ee" +dependencies = [ + "crossbeam-utils", + "loom", + "slab", +] + +[[package]] +name = "libc" +version = "0.2.191" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a" + +[[package]] +name = "libloading" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7c4b02199fee7c5d21a5ae7d8cfa79a6ef5bb2fc834d6e9058e89c825efdc55" +dependencies = [ + "cfg-if", + "windows-link", +] + +[[package]] +name = "libproc" +version = "0.14.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a54ad7278b8bc5301d5ffd2a94251c004feb971feba96c971ea4063645990757" +dependencies = [ + "bindgen", + "errno", + "libc", +] + +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + +[[package]] +name = "litemap" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae" + +[[package]] +name = "lock_api" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "224399e74b87b5f3557511d98dff8b14089b3dadafcab6bb93eab67d3aace965" +dependencies = [ + "scopeguard", +] + +[[package]] +name = "log" +version = "0.4.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c" + +[[package]] +name = "loom" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "mach2" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dae608c151f68243f2b000364e1f7b186d9c29845f7d2d85bd31b9ad77ad552b" + +[[package]] +name = "matchers" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d1525a2a28c7f4fa0fc98bb91ae755d1e2d1505079e05539e35bc876b5d65ae9" +dependencies = [ + "regex-automata", +] + +[[package]] +name = "matchit" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47e1ffaa40ddd1f3ed91f717a33c8c0ee23fff369e3aa8772b9605cc1d22f4c3" + +[[package]] +name = "memchr" +version = "2.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" + +[[package]] +name = "metrics" +version = "0.24.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89550ee9f79e88fef3119de263694973a8adb26c21d75322164fb8c493039fe2" +dependencies = [ + "portable-atomic", + "rapidhash", +] + +[[package]] +name = "metrics-exporter-prometheus" +version = "0.18.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1db0d8f1fc9e62caebd0319e11eaec5822b0186c171568f0480b46a0137f9108" +dependencies = [ + "base64", + "evmap", + "http-body-util", + "hyper", + "hyper-rustls", + "hyper-util", + "indexmap", + "ipnet", + "metrics", + "metrics-util", + "quanta", + "rustls", + "thiserror", + "tokio", + "tracing", +] + +[[package]] +name = "metrics-process" +version = "2.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4268d87f64a752f5a651314fc683f04da10be65701ea3e721ba4d74f79163cac" +dependencies = [ + "libc", + "libproc", + "mach2", + "metrics", + "once_cell", + "procfs", + "rlimit", + "windows", +] + +[[package]] +name = "metrics-util" +version = "0.20.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96f8722f8562635f92f8ed992f26df0532266eb03d5202607c20c0d7e9745e13" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "hashbrown 0.16.1", + "metrics", + "quanta", + "rand", + "rand_xoshiro", + "rapidhash", + "sketches-ddsketch", +] + +[[package]] +name = "mime" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" + +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + +[[package]] +name = "mio" +version = "1.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1788edb87fdc09c7e26304471e2f5be8cdefb1b6930d6e3985fc02ff53bf86ee" +dependencies = [ + "libc", + "wasi", + "windows-sys 0.61.2", +] + +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.21.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50" + +[[package]] +name = "openssl-probe" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e036b411754132fd6b74e94b74e9a13c5cf606fa93d1885c35860d3060820b1" + +[[package]] +name = "parking" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f38d5652c16fde515bb1ecef450ab0f6a219d619a7274976324d5e377f7dceba" + +[[package]] +name = "parking_lot" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93857453250e3077bd71ff98b6a65ea6621a19bb0f559a85248955ac12c45a1a" +dependencies = [ + "lock_api", + "parking_lot_core", +] + +[[package]] +name = "parking_lot_core" +version = "0.9.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2621685985a2ebf1c516881c026032ac7deafcda1a2c9b7850dc81e3dfcb64c1" +dependencies = [ + "cfg-if", + "libc", + "redox_syscall", + "smallvec", + "windows-link", +] + +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap", +] + +[[package]] +name = "pin-project" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2466b2336ed02bcdca6b294417127b90ec92038d1d5c4fbeac971a922e0e0924" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c96395f0a926bc13b1c17622aaddda1ecb55d49c8f1bf9777e4d877800a43f8b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a89322df9ebe1c1578d689c92318e070967d1042b512afbe49518723f4e6d5cd" + +[[package]] +name = "pkg-config" +version = "0.3.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b464fbc74e149a392436b17d523f769e057cb6877f6a5c4618bc6f11800548" + +[[package]] +name = "portable-atomic" +version = "1.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05c8b63e8d9609db387f0324918f81d68fe27748f084ef092fb35954d0539a85" + +[[package]] +name = "potential_utf" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661" +dependencies = [ + "zerovec", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "prettyplease" +version = "0.2.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b" +dependencies = [ + "proc-macro2", + "syn 2.0.119", +] + +[[package]] +name = "proc-macro2" +version = "1.0.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "procfs" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25485360a54d6861439d60facef26de713b1e126bf015ec8f98239467a2b82f7" +dependencies = [ + "bitflags", + "procfs-core", + "rustix", +] + +[[package]] +name = "procfs-core" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6401bf7b6af22f78b563665d15a22e9aef27775b79b149a66ca022468a4e405" +dependencies = [ + "bitflags", + "hex", +] + +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck", + "itertools 0.14.0", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + +[[package]] +name = "pulldown-cmark" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db2be46835f013dd3f4fa7f5f7402c537787b54ebb6fe2b94bbdd7e2669bb7b" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84bbb29c624230c4bd1047bbdb2aa47e41c860e9665ce62ba9504eebe91bf867" +dependencies = [ + "pulldown-cmark", +] + +[[package]] +name = "quanta" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3ab5a9d756f0d97bdc89019bd2e4ea098cf9cde50ee7564dde6b81ccc8f06c7" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid", + "wasi", + "web-sys", + "winapi", +] + +[[package]] +name = "quote" +version = "1.0.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "r-efi" +version = "6.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" + +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_xoshiro" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f703f4665700daf5512dcca5f43afa6af89f09db47fb56be587f80636bda2d41" +dependencies = [ + "rand_core", +] + +[[package]] +name = "rapidhash" +version = "4.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5da7e78a036ce858e8d55b7e7dc8ba3a88b78350fd2155d3591bbd966b58589e" +dependencies = [ + "rustversion", +] + +[[package]] +name = "raw-cpuid" +version = "11.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186" +dependencies = [ + "bitflags", +] + +[[package]] +name = "redis" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db7f3d75b5a3301746bff39a767bf4c5c42506b08513527308b3875bfb64a031" +dependencies = [ + "arc-swap", + "arcstr", + "async-lock", + "backon", + "bytes", + "cfg-if", + "combine", + "futures-channel", + "futures-util", + "itoa", + "percent-encoding", + "pin-project-lite", + "ryu", + "sha1_smol", + "socket2", + "tokio", + "tokio-util", + "url", + "xxhash-rust", +] + +[[package]] +name = "redox_syscall" +version = "0.5.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed2bf2547551a7053d6fdfafda3f938979645c44812fbfcda098faae3f1a362d" +dependencies = [ + "bitflags", +] + +[[package]] +name = "regex" +version = "1.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f020237b6c8eed93db2e2cb53c00c60a8e1bc73da7d073199a1180401450218d" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata", + "regex-syntax", +] + +[[package]] +name = "regex-automata" +version = "0.4.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad8553b9b26413251cbf30e620595c7a41b3887f03da04579c0e6b0d6a06b4b2" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d6f6ff9a378485b298a5286656da665ba74413d36db0979633275d2e708145d4" + +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rlimit" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f35ee2729c56bb610f6dba436bf78135f728b7373bdffae2ec815b2d3eb98cc3" +dependencies = [ + "libc", +] + +[[package]] +name = "rustc-hash" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6b1e7f9a428571be2dc5bc0505c13fb6bf936822b894ec87abf8a08a4e51742d" + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + +[[package]] +name = "rustls" +version = "0.23.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" +dependencies = [ + "aws-lc-rs", + "once_cell", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-native-certs" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a073f5dc7469f984c52ad2752b63b0807745133b6de880b7b64c1ac4c48aec4" +dependencies = [ + "openssl-probe", + "rustls-pki-types", + "schannel", + "security-framework", +] + +[[package]] +name = "rustls-pki-types" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281" +dependencies = [ + "aws-lc-rs", + "ring", + "rustls-pki-types", + "untrusted", +] + +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + +[[package]] +name = "ryu" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9774ba4a74de5f7b1c1451ed6cd5285a32eddb5cccb8cc655a4e50009e06477f" + +[[package]] +name = "schannel" +version = "0.1.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cb23c424d8ee841bb4d813677ca237e2a610067d594fec130f87d6093495ab5" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + +[[package]] +name = "scopeguard" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" + +[[package]] +name = "security-framework" +version = "3.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f4bc775c73d9a02cde8bf7b2ec4c9d12743edf609006c7facc23998404cd1d" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2691df843ecc5d231c0b14ece2acc3efb62c0a398c7e1d875f3983ce020e3" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "serde" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4148590afebada386688f18773da617792bf2ef03ffc1e4cbd2b1d45b023e0ba" +dependencies = [ + "serde_core", +] + +[[package]] +name = "serde_core" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67dca2c9c51e58a4791a4b1ed58308b39c64224d349a935ab5039aa360942a48" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.229" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7a5d71263a5a7d47b41f6b3f06ba276f10cc18b0931f1799f710578e2309348" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "serde_json" +version = "1.0.154" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "serde_path_to_error" +version = "0.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10a9ff822e371bb5403e391ecd83e182e0e77ba7f6fe0160b795797109d1b457" +dependencies = [ + "itoa", + "serde", + "serde_core", +] + +[[package]] +name = "serde_urlencoded" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha1_smol" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbfa15b3dddfee50a0fff136974b3e1bde555604ba463834a7eb7deb6417705d" + +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "shlex" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8fadd59c855ef2080decdef8ff161eb6661b86933c9d82e5ba29dc602a55aba" + +[[package]] +name = "sketches-ddsketch" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "45f82fa313d625dd72aaa65ec0c9f8c95407add0510c1bef3c18ea7257ec34ce" + +[[package]] +name = "slab" +version = "0.4.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c790de23124f9ab44544d7ac05d60440adc586479ce501c1d6d7da3cd8c9cf5" + +[[package]] +name = "smallvec" +version = "1.16.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0d5d4b1981b8bbbb7aed204cae5726d683d5d6ac7e74ec54122ea28f315e7c8" + +[[package]] +name = "socket2" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + +[[package]] +name = "syn" +version = "2.0.119" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "syn" +version = "3.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d78c8dee4c7bf0e14673097256fed6142ce9d3b85a408189d07482442145823b" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sync_wrapper" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bf256ce5efdfa370213c1dabab5935a12e49f2c58d15e9eac2870d3b4f27263" + +[[package]] +name = "synstructure" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + +[[package]] +name = "thiserror" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09e52cb86a36cede5cb101bf8908837b3e4c6e5e59fe7fd85c23fb56200d189e" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "2.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe5197923287db20a58125f0bc85c062f7f2c892de97b18c356f9efb14b28524" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "thread_local" +version = "1.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad99c4c6d32803332c548b1af0540b357b3f5fc0be8f6c6bfe8b2e6ae784070" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "tinystr" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "abc840c238154d58f9e86b2792261edfde565a17696e1cda1a449de27979801a" +dependencies = [ + "displaydoc", + "zerovec", +] + +[[package]] +name = "tokio" +version = "1.54.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" +dependencies = [ + "bytes", + "libc", + "mio", + "pin-project-lite", + "socket2", + "tokio-macros", + "windows-sys 0.61.2", +] + +[[package]] +name = "tokio-macros" +version = "2.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "tokio-rustls" +version = "0.26.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9cc2678c2cdd569ef8215e2afd7954ada2ae20b4fdd2c5fe6139a3b02d105db" +dependencies = [ + "rustls", + "tokio", +] + +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", + "tokio-util", +] + +[[package]] +name = "tokio-util" +version = "0.7.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e464cf451ba96ebfc6f9b6542f17ee8b8956e33f1e40d9690624e59d7a7f8a4b" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "libc", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tonic" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2cdfc673aabd30fe8fee92e65837b644c37c5d47f0f97ae6d5e0f1237d15e1b" +dependencies = [ + "async-trait", + "axum", + "base64", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "socket2", + "sync_wrapper", + "tokio", + "tokio-stream", + "tokio-util", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac6d916d206ed7bdda1778ddff9bda43303789c258877e7bc22fb44ee5c984b4" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tonic-health" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bc97d645691d64d96db1afe386d0d2918ac84ab56e2c91e153f22f6349a378cd" +dependencies = [ + "prost", + "tokio", + "tokio-stream", + "tonic", + "tonic-prost", +] + +[[package]] +name = "tonic-middleware" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "28272673571986af2ff4337088c2b7314dc20f45e81c4fe1e75cd78feb619002" +dependencies = [ + "async-trait", + "futures-util", + "tonic", + "tower", +] + +[[package]] +name = "tonic-prost" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eba464054e2226f182a34489b8b079bf4c6e3e7ab5fd7fc30e024f24aa92a484" +dependencies = [ + "bytes", + "prost", + "tonic", +] + +[[package]] +name = "tonic-prost-build" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ea216ddc448e08e7839ab09e20e60b06b6a1c32745ccb212c0ebd161c199f1b" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.119", + "tempfile", + "tonic-build", +] + +[[package]] +name = "tonic-reflection" +version = "0.14.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb4700e220803efd45baf3babaa1958c79adc9e0fe6fbd4422f9188d1e57d26" +dependencies = [ + "prost", + "prost-types", + "tokio", + "tokio-stream", + "tonic", + "tonic-prost", +] + +[[package]] +name = "tower" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" +dependencies = [ + "futures-core", + "futures-util", + "indexmap", + "pin-project-lite", + "slab", + "sync_wrapper", + "tokio", + "tokio-util", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-http" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08a05a66a4fdd61cbbe0a1d755ffe0ca6aba159dd4820936a0ff8a8278245b9c" +dependencies = [ + "bitflags", + "bytes", + "futures-util", + "http", + "http-body", + "http-body-util", + "percent-encoding", + "pin-project-lite", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tower-layer" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "121c2a6cda46980bb0fcd1647ffaf6cd3fc79a013de288782836f6df9c48780e" + +[[package]] +name = "tower-service" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8df9b6e13f2d32c91b9bd719c00d1958837bc7dec474d94952798cc8e69eeec3" + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "log", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-serde" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "704b1aeb7be0d0a84fc9828cae51dab5970fee5088f83d1dd7ee6f6246fc6ff1" +dependencies = [ + "serde", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7f578e5945fb242538965c2d0b04418d38ec25c79d160cd279bf0731c8d319" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex-automata", + "serde", + "serde_json", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", + "tracing-serde", +] + +[[package]] +name = "try-lock" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e421abadd41a4225275504ea4d6566923418b7f05506fbc9c0fe86ba7396114b" + +[[package]] +name = "unicase" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "357cc3acc6a036009fd6c973ed009037c732d60d0b4f6c673e9041497482a28f" + +[[package]] +name = "unicode-ident" +version = "1.0.27" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "url" +version = "2.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf8_iter" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af3bd9d3e571c26fcce9d033b87ea5ab061b8d2ffe9b7a5675d810d68f03edf0" +dependencies = [ + "utf_types", +] + +[[package]] +name = "utf_types" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3b425286980a367278453f3b6d28cdcb134b574e5a35824a6ff5eebad50bb6" + +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + +[[package]] +name = "want" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec4cdd0dd910afe868b7ef477227d8d538b46b3075031afee8a9f2acb0a2ed0b" +dependencies = [ + "try-lock", +] + +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + +[[package]] +name = "wasip2" +version = "1.0.4+wasi-0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b67efb37e106e55ce722a510d6b5f9c17f083e5fc79afc2badeb12cc313d9487" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wasm-bindgen" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a0181c14ddd6c7b8b0b0132a5f1ea2f32be0d893cd3ea97b6e3b28886b8a3d1c" +dependencies = [ + "cfg-if", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e931a02a39abdbf66966fc35902b5ed58344750a70885bed4ebb3d8eaf28ed9" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ca4b54280e455bd831e33a6b8e734baf432fe63fbc35c42ac14ce67a043a53a" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 3.0.9", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.130" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b1f9c3798725016c6ee5f2f57869ec0831e8bbc0ab0310a86e787e97f41389e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "web-sys" +version = "0.3.107" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7d179596ff5ec0f6de4db64d54011bde6289cc49603d3a6e4ce3f329207ab88" +dependencies = [ + "js-sys", + "wasm-bindgen", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "527fadee13e0c05939a6a05d5bd6eec6cd2e3dbd648b9f8e447c6518133d8580" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b2d95af1a8a14a3c7367e1ed4fc9c20e0a26e79551b1454d72583c97cc6610" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1d6f90251fe18a279739e78025bd6ddc52a7e22f921070ccdc67dde84c605cb" +dependencies = [ + "windows-core", + "windows-link", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-numerics" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e2e40844ac143cdb44aead537bbf727de9b044e107a0f1220392177d15b0f26" +dependencies = [ + "windows-core", + "windows-link", +] + +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows-threading" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3949bd5b99cafdf1c7ca86b43ca564028dfe27d66958f2470940f73d86d75b37" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "wit-bindgen" +version = "0.57.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ebf944e87a7c253233ad6766e082e3cd714b5d03812acc24c318f549614536e" + +[[package]] +name = "writeable" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" + +[[package]] +name = "xxhash-rust" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "550a2b930b62486a393c52d5c3b84bff264b28aa437ed64694d31e93b1757af7" + +[[package]] +name = "yoke" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "417ef3fcf6c300b7dbd25c85531a746931c02ad536f3cf4786ed1e88223340b3" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", + "synstructure", +] + +[[package]] +name = "zerocopy" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5fe1f8f1b06191a00962174c61aa5005e0bb391a6d80d07e24d115c01a92ed8" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.63" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863ad3ac83293fb4d740aedbfdc9240dd8d1a50c1099acd76ce80ce7c7230c7f" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "zerofrom" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879" + +[[package]] +name = "zerotrie" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f58a2799cfe9a1670c7ea8edbd52ef8edb93cb37a7f83bf31ba9e35d9dae6a40" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3743ddbf47738d929cd1ae2e199d40d0162979dfe31aef8ae646d2bf00bf97a" +dependencies = [ + "proc-macro2", + "quote", + "syn 3.0.9", +] + +[[package]] +name = "zmij" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29666d0abbfad1e3dc4dcf6144730dd3a3ab225bbbdac83319345b1b44ccfc1b" diff --git a/internal/leetcode-auth-service/Cargo.toml b/internal/leetcode-auth-service/Cargo.toml new file mode 100644 index 000000000..80b660055 --- /dev/null +++ b/internal/leetcode-auth-service/Cargo.toml @@ -0,0 +1,34 @@ +[package] +name = "leetcode-auth-service" +version = "0.1.0" +edition = "2024" +rust-version = "1.94" + +[dependencies] +tonic = "0.14.6" +tonic-health = "0.14.6" +tonic-reflection = "0.14.6" +tonic-middleware = "0.4.1" +prost = "0.14.4" +tonic-prost = "0.14.6" +tokio = { version = "1.53.1", features = ["macros", "rt-multi-thread", "time"] } +anyhow = "1.0.104" +tower-http = { version = "0.7.1", features = ["catch-panic"] } +axum = { version = "0.8.9" } +metrics = "0.24.6" +metrics-exporter-prometheus = "0.18.3" +metrics-process = "2.4.3" +bytes = "1.12.1" +http-body = "1.1.0" +pin-project-lite = "0.2.17" +tracing = "0.1.44" +tracing-subscriber = { version = "0.3.23", features = ["json", "env-filter"] } +chrono = { version = "0.4.45", default-features = false, features = ["std", "clock"] } +redis = { version = "1.8.1", default-features = false, features = ["tokio-comp", "connection-manager", "script"] } + +[dev-dependencies] +parking_lot = "0.12" +tokio = { version = "1.53.1", features = ["sync"] } + +[build-dependencies] +tonic-prost-build = "0.14" diff --git a/internal/leetcode-auth-service/Dockerfile b/internal/leetcode-auth-service/Dockerfile new file mode 100644 index 000000000..47e97d5c4 --- /dev/null +++ b/internal/leetcode-auth-service/Dockerfile @@ -0,0 +1,31 @@ +# build context is internal/ +# `docker build -f internal/leetcode-auth-service/Dockerfile internal` (from the repo root) +FROM mirror.gcr.io/library/rust:1-alpine3.24 AS build + +WORKDIR /app/leetcode-auth-service + +RUN apk add --no-cache 'musl-dev=~1.2.6' 'protobuf-dev=~31.1' + +COPY leetcode-auth-stealer-service/proto ../leetcode-auth-stealer-service/proto +COPY leetcode-auth-service/Cargo.toml leetcode-auth-service/Cargo.lock leetcode-auth-service/build.rs ./ +COPY leetcode-auth-service/proto ./proto +COPY leetcode-auth-service/src ./src + +RUN cargo build --release --locked + +FROM mirror.gcr.io/library/alpine:3.24.3 AS server-runtime + +RUN addgroup -g 10001 -S grpc-server \ + && adduser -u 10001 -S -G grpc-server -h /app grpc-server + +WORKDIR /app + +COPY --from=build /app/leetcode-auth-service/target/release/leetcode-auth-service ./ + +ENV ENVIRONMENT=production + +USER 10001:10001 + +EXPOSE 8082 50052 + +ENTRYPOINT ["./leetcode-auth-service"] diff --git a/internal/leetcode-auth-service/README.md b/internal/leetcode-auth-service/README.md new file mode 100644 index 000000000..99a538a1a --- /dev/null +++ b/internal/leetcode-auth-service/README.md @@ -0,0 +1,77 @@ +# LeetCode Auth Service + +gRPC service that owns the leetcode.com credentials (`LEETCODE_SESSION` + `csrftoken`) used by the Codebloom backend. + +- `proto/v1/leetcode_auth.proto`: `leetcodeauth.LeetcodeAuthService` + - `getToken`: returns the stored `token` and `csrf`. `NOT_FOUND` until the first refresh succeeds, `UNAVAILABLE` if Redis is down. + - `refreshToken`: starts a refresh in the background and returns right away. `started` is `false` when any instance is already refreshing; that call is ignored. + +Credentials come from [`leetcode-auth-stealer-service`](../leetcode-auth-stealer-service/README.md) (LASS). This service is the only one that calls LASS; its proto is compiled straight from `../leetcode-auth-stealer-service/proto`. + +## Storage + +One Redis hash, `codebloom:leetcode:auth:key`, with the fields `token`, `csrf` and `date_stored` (RFC 3339, UTC). The hash has no TTL and is never deleted. + +A refresh writes all three fields in one `HSET`, and only after LASS returns a non-empty token and csrf. The latest successful refresh always wins. A failed LASS call or Redis error leaves the stored credentials untouched, and `getToken` keeps serving them. + +## Refreshing + +- Only one refresh runs at a time across all instances, whether it came from `refreshToken` or the schedule. Triggers that arrive while one is running are ignored, not queued. +- The lock is `codebloom:leetcode:auth:lock`, taken with `SET NX PX`. Its value identifies the holder (hostname and the time it was taken), and it is released by a Lua script that deletes it only if that holder still owns it. It expires after `REFRESH_LOCK_TTL_SECONDS`, so a pod that crashes or panics mid-refresh blocks refreshes for at most that long. +- Every `REFRESH_CHECK_INTERVAL_SECONDS` (and once at startup) the service reads `date_stored` and refreshes if the entry is missing, incomplete, unparsable, or at least `TOKEN_MAX_AGE_SECONDS` old. + +## Quickstart + +Requires Rust 1.94 or newer and `protoc`. + +```bash +# uses the repo root .env (copied from the root example.env) +just dev # http on :8082, grpc on :50052 +``` + +```bash +grpcurl -plaintext localhost:50052 leetcodeauth.LeetcodeAuthService/getToken +grpcurl -plaintext localhost:50052 leetcodeauth.LeetcodeAuthService/refreshToken +``` + +The Docker build context is `internal/`, because `build.rs` needs LASS's proto: + +```bash +docker build -f internal/leetcode-auth-service/Dockerfile internal +``` + +## Configuration + +- `REDIS_URL`: required. +- `LEETCODE_AUTH_USERNAME`, `LEETCODE_AUTH_PASSWORD`: required, sent to LASS. +- `LASS_URL`: LASS gRPC address, default `http://localhost:50051`. +- `LASS_TIMEOUT_SECONDS`: deadline for one LASS login, default `180`. +- `REFRESH_LOCK_TTL_SECONDS`: how long the refresh lock lives if it is never released, default `LASS_TIMEOUT_SECONDS + 30`. Keep it above `LASS_TIMEOUT_SECONDS`, or a slow refresh can lose its lock and a second pod may refresh at the same time. +- `REFRESH_CHECK_INTERVAL_SECONDS`: how often the stored entry's age is checked, default `900` (15 minutes). +- `TOKEN_MAX_AGE_SECONDS`: age at which the scheduled check refreshes, default `14400` (4 hours). +- `GRPC_PORT` / `HTTP_PORT`: default `50052` / `8082`. +- `GRPC_REFLECTION`: `true` to serve gRPC reflection, default `false`. +- `HOSTNAME`: names this pod in the refresh lock's value. Kubernetes sets it to the pod name; default `unknown`. + +Zero or unparsable durations fall back to the default. + +## HTTP + +- `/livez`: always `ok`. +- `/readyz`: `503` when Redis does not answer `PING` within 3 seconds, so the pod is taken out of the Service until Redis is back. The pod starts and stays alive while Redis is down; the Redis connection opens on first use and reconnects on its own. +- `/metrics`: Prometheus, including `grpc_requests_total`, `grpc_request_duration_seconds` and `leetcode_auth_refresh_total{result="stored|lass_failed|store_failed"}`. +- `/version`: `VERSION` env var. + +## Code layout + +Same layering as `members-grpc-service`: + +| File | Holds | +| --------------------- | -------------------------------------------------------------------------------------------------- | +| `src/domain/auth.rs` | `AuthToken`, `StoredAuth` and the staleness rule. No I/O. | +| `src/db/auth.rs` | `AuthRepository` trait, `RedisAuthRepository`, and `FakeAuthRepository` for tests. | +| `src/grpc/client/lass.rs` | `LASS` trait, `LASSGrpcClient` (client for LASS), and `FakeAuthenticator` for tests. | +| `src/service/refresh.rs` | `TokenManager`: the Redis-locked refresh and the schedule. | +| `src/grpc/auth.rs` | gRPC handlers, generic over `AuthRepository` and `LASS`. | + +Tests are unit tests only and need neither Redis nor LASS. diff --git a/internal/leetcode-auth-service/build.rs b/internal/leetcode-auth-service/build.rs new file mode 100644 index 000000000..83e24c4cf --- /dev/null +++ b/internal/leetcode-auth-service/build.rs @@ -0,0 +1,24 @@ +use std::path::PathBuf; + +fn main() -> Result<(), Box> { + let out_dir = "src/proto"; + std::fs::create_dir_all(out_dir)?; + + let out_dir = PathBuf::from(out_dir); + + tonic_prost_build::configure() + .build_client(false) + .out_dir(&out_dir) + .file_descriptor_set_path(out_dir.join("descriptor.bin")) + .compile_protos(&["proto/v1/leetcode_auth.proto"], &["proto/"])?; + + tonic_prost_build::configure() + .build_server(false) + .out_dir(&out_dir) + .compile_protos( + &["../leetcode-auth-stealer-service/proto/LeetcodeAuthStealerService.proto"], + &["../leetcode-auth-stealer-service/proto/"], + )?; + + Ok(()) +} diff --git a/internal/leetcode-auth-service/proto/v1/leetcode_auth.proto b/internal/leetcode-auth-service/proto/v1/leetcode_auth.proto new file mode 100644 index 000000000..507759b8d --- /dev/null +++ b/internal/leetcode-auth-service/proto/v1/leetcode_auth.proto @@ -0,0 +1,30 @@ +syntax = "proto3"; +package leetcodeauth; + +option go_package = "patinanetwork.org/grpc/leetcode-auth-service;leetcodeauth"; +option java_package = "org.patinanetwork.codebloom.grpc.leetcodeauth.v1"; +option java_multiple_files = true; + +service LeetcodeAuthService { + // may return NOT_FOUND until the first refresh succeeds. + rpc getToken(GetTokenRequest) returns (GetTokenResponse); + + // Starts a refresh in the background and returns immediately. Calls made + // while a refresh is running are ignored. The stored credentials keep being + // served until a refresh succeeds. + rpc refreshToken(RefreshTokenRequest) returns (RefreshTokenResponse); +} + +message GetTokenRequest {} + +message GetTokenResponse { + string token = 1; + string csrf = 2; +} + +message RefreshTokenRequest {} + +message RefreshTokenResponse { + // false when a refresh was already running and this call was ignored + bool started = 1; +} diff --git a/internal/leetcode-auth-service/src/config.rs b/internal/leetcode-auth-service/src/config.rs new file mode 100644 index 000000000..ae017a589 --- /dev/null +++ b/internal/leetcode-auth-service/src/config.rs @@ -0,0 +1,95 @@ +use std::{env, time::Duration}; + +use anyhow::Context; +use tracing::info; + +#[derive(Clone)] +pub struct Config { + pub grpc_port: u16, + pub http_port: u16, + pub grpc_reflection: bool, + pub version: Option, + /// Identifies this pod as the holder of the refresh lock. + pub hostname: String, + pub redis_url: String, + pub lass_url: String, + pub lass_timeout: Duration, + /// Defaults to outlasting one LASS call, so the lock does not expire mid-refresh. + pub refresh_lock_ttl: Duration, + pub leetcode_auth_username: String, + pub leetcode_auth_password: String, + pub refresh_check_interval: Duration, + pub token_max_age: Duration, +} + +impl Config { + pub fn log(&self) { + info!( + grpc_port = self.grpc_port, + http_port = self.http_port, + grpc_reflection = self.grpc_reflection, + version = self.version.as_deref().unwrap_or("N/A"), + hostname = self.hostname, + lass_url = self.lass_url, + lass_timeout = ?self.lass_timeout, + refresh_lock_ttl = ?self.refresh_lock_ttl, + refresh_check_interval = ?self.refresh_check_interval, + token_max_age = ?self.token_max_age, + "loaded config" + ); + } + + pub fn from_env() -> anyhow::Result { + Self::from_vars(|key| env::var(key).ok()) + } + + fn from_vars(var: impl Fn(&str) -> Option) -> anyhow::Result { + let lass_timeout = seconds(&var, "LASS_TIMEOUT_SECONDS", 180); + + Ok(Self { + grpc_reflection: var("GRPC_REFLECTION") + .and_then(|s| s.parse().ok()) + .unwrap_or(false), + grpc_port: var("GRPC_PORT") + .and_then(|s| s.parse().ok()) + .unwrap_or(50052), + http_port: var("HTTP_PORT") + .and_then(|s| s.parse().ok()) + .unwrap_or(8082), + version: var("VERSION").filter(|v| !v.is_empty()), + hostname: var("HOSTNAME") + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "unknown".to_string()), + redis_url: required(&var, "REDIS_URL")?, + lass_url: var("LASS_URL") + .filter(|v| !v.is_empty()) + .unwrap_or_else(|| "http://localhost:50051".to_string()), + lass_timeout, + refresh_lock_ttl: seconds( + &var, + "REFRESH_LOCK_TTL_SECONDS", + lass_timeout.as_secs() + 30, + ), + leetcode_auth_username: required(&var, "LEETCODE_AUTH_USERNAME")?, + leetcode_auth_password: required(&var, "LEETCODE_AUTH_PASSWORD")?, + refresh_check_interval: seconds(&var, "REFRESH_CHECK_INTERVAL_SECONDS", 15 * 60), + token_max_age: seconds(&var, "TOKEN_MAX_AGE_SECONDS", 4 * 60 * 60), + }) + } +} + +fn required(var: &impl Fn(&str) -> Option, name: &str) -> anyhow::Result { + var(name) + .filter(|v| !v.is_empty()) + .with_context(|| format!("missing {name}")) +} + +/// Zero is rejected because `tokio::time::interval` panics on a zero period. +fn seconds(var: &impl Fn(&str) -> Option, name: &str, default: u64) -> Duration { + Duration::from_secs( + var(name) + .and_then(|s| s.parse().ok()) + .filter(|&v| v > 0) + .unwrap_or(default), + ) +} diff --git a/internal/leetcode-auth-service/src/db/auth.rs b/internal/leetcode-auth-service/src/db/auth.rs new file mode 100644 index 000000000..cd1601933 --- /dev/null +++ b/internal/leetcode-auth-service/src/db/auth.rs @@ -0,0 +1,230 @@ +use std::time::Duration; + +use chrono::{DateTime, SecondsFormat, Utc}; + +use redis::{RedisError, Script, aio::ConnectionManager}; + +use crate::domain::auth::{AuthToken, StoredAuth}; + +/// Hash with `token`, `csrf` and `date_stored` fields. No TTL: stale credentials are still served +/// until a refresh succeeds, and the key is never deleted. +pub const AUTH_KEY: &str = "codebloom:leetcode:auth:key"; + +/// Held by whichever pod is refreshing. Expires on its own so a crashed pod cannot block refreshes +/// forever. +pub const LOCK_KEY: &str = "codebloom:leetcode:auth:lock"; + +/// Deletes the lock only if `owner` still holds it, so a pod whose lock expired cannot release the +/// lock another pod has since taken. +const UNLOCK_IF_OWNER: &str = r" +if redis.call('GET', KEYS[1]) == ARGV[1] then + return redis.call('DEL', KEYS[1]) +end +return 0 +"; + +pub trait AuthRepository: Clone + Send + Sync + 'static { + fn get(&self) -> impl Future> + Send; + + fn store( + &self, + auth: &AuthToken, + date_stored: DateTime, + ) -> impl Future> + Send; + + /// `false` when another owner holds the lock. + fn try_lock( + &self, + owner: &str, + ttl: Duration, + ) -> impl Future> + Send; + + fn unlock(&self, owner: &str) -> impl Future> + Send; +} + +#[derive(Clone)] +pub struct RedisAuthRepository { + conn: ConnectionManager, + unlock_script: Script, +} + +impl RedisAuthRepository { + pub fn new(conn: ConnectionManager) -> Self { + Self { + conn, + unlock_script: Script::new(UNLOCK_IF_OWNER), + } + } +} + +impl AuthRepository for RedisAuthRepository { + async fn get(&self) -> Result { + let mut conn = self.conn.clone(); + let (token, csrf, date_stored): (Option, Option, Option) = + redis::cmd("HMGET") + .arg(AUTH_KEY) + .arg(&["token", "csrf", "date_stored"]) + .query_async(&mut conn) + .await?; + + Ok(StoredAuth { + auth: token + .zip(csrf) + .and_then(|(token, csrf)| AuthToken::new(token, csrf)), + date_stored: date_stored.as_deref().and_then(parse_date_stored), + }) + } + + async fn store(&self, auth: &AuthToken, date_stored: DateTime) -> Result<(), RedisError> { + let mut conn = self.conn.clone(); + + redis::cmd("HSET") + .arg(AUTH_KEY) + .arg("token") + .arg(&auth.token) + .arg("csrf") + .arg(&auth.csrf) + .arg("date_stored") + .arg(format_date_stored(date_stored)) + .query_async(&mut conn) + .await + } + + async fn try_lock(&self, owner: &str, ttl: Duration) -> Result { + let mut conn = self.conn.clone(); + let acquired: Option = redis::cmd("SET") + .arg(LOCK_KEY) + .arg(owner) + .arg("NX") + .arg("PX") + .arg(u64::try_from(ttl.as_millis()).unwrap_or(u64::MAX)) + .query_async(&mut conn) + .await?; + + Ok(acquired.is_some()) + } + + async fn unlock(&self, owner: &str) -> Result<(), RedisError> { + let mut conn = self.conn.clone(); + + self.unlock_script + .key(LOCK_KEY) + .arg(owner) + .invoke_async::<()>(&mut conn) + .await + } +} + +fn format_date_stored(value: DateTime) -> String { + value.to_rfc3339_opts(SecondsFormat::Millis, true) +} + +/// An unparsable value reads as missing, so the entry counts as stale and the next refresh +/// overwrites it. +fn parse_date_stored(value: &str) -> Option> { + DateTime::parse_from_rfc3339(value) + .ok() + .map(|value| value.with_timezone(&Utc)) +} + +#[cfg(test)] +pub mod fake { + use std::sync::Arc; + + use parking_lot::Mutex; + + use super::*; + + #[derive(Debug, Clone, Default)] + pub struct FakeState { + pub stored: StoredAuth, + /// Lock owner; expiry is not modelled. + pub lock: Option, + } + + #[derive(Clone)] + pub enum FakeAuthRepository { + Unavailable, + Stored(Arc>), + } + + impl FakeAuthRepository { + pub fn stored(stored: StoredAuth) -> Self { + Self::Stored(Arc::new(Mutex::new(FakeState { stored, lock: None }))) + } + + fn state(&self) -> Result<&Mutex, RedisError> { + match self { + Self::Unavailable => Err(std::io::Error::other("redis unavailable").into()), + Self::Stored(state) => Ok(state), + } + } + + pub fn snapshot(&self) -> FakeState { + self.state() + .expect("unavailable repository has no state") + .lock() + .clone() + } + + pub fn set_lock(&self, owner: Option<&str>) { + self.state() + .expect("unavailable repository has no state") + .lock() + .lock = owner.map(str::to_string); + } + } + + impl AuthRepository for FakeAuthRepository { + async fn get(&self) -> Result { + Ok(self.state()?.lock().stored.clone()) + } + + async fn store( + &self, + auth: &AuthToken, + date_stored: DateTime, + ) -> Result<(), RedisError> { + self.state()?.lock().stored = StoredAuth { + auth: Some(auth.clone()), + date_stored: Some(date_stored), + }; + Ok(()) + } + + async fn try_lock(&self, owner: &str, _ttl: Duration) -> Result { + let mut state = self.state()?.lock(); + if state.lock.is_some() { + return Ok(false); + } + + state.lock = Some(owner.to_string()); + Ok(true) + } + + async fn unlock(&self, owner: &str) -> Result<(), RedisError> { + let mut state = self.state()?.lock(); + if state.lock.as_deref() == Some(owner) { + state.lock = None; + } + Ok(()) + } + } +} + +#[cfg(test)] +mod tests { + use chrono::SubsecRound; + + use super::*; + + #[test] + fn date_stored_round_trips_and_rejects_garbage() { + let now = Utc::now().trunc_subsecs(3); + assert_eq!(parse_date_stored(&format_date_stored(now)), Some(now)); + + for garbage in ["", "yesterday", "2026-13-01T00:00:00.000Z"] { + assert_eq!(parse_date_stored(garbage), None, "{garbage:?}"); + } + } +} diff --git a/internal/leetcode-auth-service/src/db/client.rs b/internal/leetcode-auth-service/src/db/client.rs new file mode 100644 index 000000000..5163aff23 --- /dev/null +++ b/internal/leetcode-auth-service/src/db/client.rs @@ -0,0 +1,13 @@ +use anyhow::Context; +use redis::aio::{ConnectionManager, ConnectionManagerConfig}; + +use crate::config::Config; + +/// Connects lazily so the service starts while Redis is down and `/readyz` reports it, instead of +/// crash-looping. `ConnectionManager` reconnects on its own; clones share one connection. +pub fn connect(config: &Config) -> anyhow::Result { + let client = redis::Client::open(config.redis_url.as_str()).context("invalid REDIS_URL")?; + + ConnectionManager::new_lazy_with_config(client, ConnectionManagerConfig::new()) + .context("failed to create redis connection manager") +} diff --git a/internal/leetcode-auth-service/src/db/health_check.rs b/internal/leetcode-auth-service/src/db/health_check.rs new file mode 100644 index 000000000..b05a8a93b --- /dev/null +++ b/internal/leetcode-auth-service/src/db/health_check.rs @@ -0,0 +1,29 @@ +use std::time::Duration; + +use anyhow::Context; +use redis::aio::ConnectionManager; + +#[derive(Clone)] +pub struct HealthCheckRepository { + conn: ConnectionManager, +} + +impl HealthCheckRepository { + pub fn new(conn: ConnectionManager) -> Self { + Self { conn } + } + + pub async fn check(&self) -> anyhow::Result<()> { + let mut conn = self.conn.clone(); + + tokio::time::timeout( + Duration::from_secs(3), + redis::cmd("PING").query_async::<()>(&mut conn), + ) + .await + .context("redis health check timed out")? + .context("redis health check failed")?; + + Ok(()) + } +} diff --git a/internal/leetcode-auth-service/src/db/mod.rs b/internal/leetcode-auth-service/src/db/mod.rs new file mode 100644 index 000000000..1ed6c3cbc --- /dev/null +++ b/internal/leetcode-auth-service/src/db/mod.rs @@ -0,0 +1,7 @@ +pub mod auth; +pub mod health_check; + +pub mod client; +pub mod redis { + pub use super::client; +} diff --git a/internal/leetcode-auth-service/src/domain/auth.rs b/internal/leetcode-auth-service/src/domain/auth.rs new file mode 100644 index 000000000..7883c243a --- /dev/null +++ b/internal/leetcode-auth-service/src/domain/auth.rs @@ -0,0 +1,88 @@ +use std::time::Duration; + +use chrono::{DateTime, TimeDelta, Utc}; + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct AuthToken { + pub token: String, + pub csrf: String, +} + +impl AuthToken { + pub fn new(token: String, csrf: String) -> Option { + (!token.is_empty() && !csrf.is_empty()).then_some(Self { token, csrf }) + } +} + +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub struct StoredAuth { + pub auth: Option, + pub date_stored: Option>, +} + +impl StoredAuth { + pub fn is_stale(&self, now: DateTime, max_age: Duration) -> bool { + if self.auth.is_none() { + return true; + } + + let Some(stored_at) = self.date_stored else { + return true; + }; + + let max_age = TimeDelta::from_std(max_age).unwrap_or(TimeDelta::MAX); + now.signed_duration_since(stored_at) >= max_age + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const MAX_AGE: Duration = Duration::from_secs(4 * 60 * 60); + + fn auth() -> Option { + AuthToken::new("token".into(), "csrf".into()) + } + + #[test] + fn incomplete_credentials_are_rejected() { + assert_eq!(AuthToken::new(String::new(), "csrf".into()), None); + assert_eq!(AuthToken::new("token".into(), String::new()), None); + } + + #[test] + fn entries_go_stale_exactly_at_max_age() { + let now = Utc::now(); + let stored_at = |age: TimeDelta| StoredAuth { + auth: auth(), + date_stored: Some(now - age), + }; + + assert!( + !stored_at(TimeDelta::hours(4) - TimeDelta::milliseconds(1)).is_stale(now, MAX_AGE) + ); + assert!(stored_at(TimeDelta::hours(4)).is_stale(now, MAX_AGE)); + // A clock ahead of ours must not force a refresh. + assert!(!stored_at(TimeDelta::hours(-1)).is_stale(now, MAX_AGE)); + } + + #[test] + fn incomplete_entries_are_stale() { + let now = Utc::now(); + + for stored in [ + StoredAuth::default(), + StoredAuth { + auth: None, + date_stored: Some(now), + }, + StoredAuth { + auth: auth(), + date_stored: None, + }, + ] { + assert!(stored.is_stale(now, MAX_AGE), "{stored:?}"); + } + } +} diff --git a/internal/leetcode-auth-service/src/domain/mod.rs b/internal/leetcode-auth-service/src/domain/mod.rs new file mode 100644 index 000000000..0e4a05d59 --- /dev/null +++ b/internal/leetcode-auth-service/src/domain/mod.rs @@ -0,0 +1 @@ +pub mod auth; diff --git a/internal/leetcode-auth-service/src/grpc/auth.rs b/internal/leetcode-auth-service/src/grpc/auth.rs new file mode 100644 index 000000000..093d29916 --- /dev/null +++ b/internal/leetcode-auth-service/src/grpc/auth.rs @@ -0,0 +1,157 @@ +use tonic::{Request, Response, Status}; + +use crate::{ + db::auth::AuthRepository, + grpc::{client::lass::LASS, redis_status}, + proto::leetcodeauth::{ + GetTokenRequest, GetTokenResponse, RefreshTokenRequest, RefreshTokenResponse, + leetcode_auth_service_server, + }, + service::refresh::TokenManager, +}; + +pub use leetcode_auth_service_server::LeetcodeAuthServiceServer; + +pub struct LeetcodeAuthService { + repository: R, + token_manager: TokenManager, +} + +impl LeetcodeAuthService { + pub fn new(repository: R, token_manager: TokenManager) -> Self { + Self { + repository, + token_manager, + } + } +} + +#[tonic::async_trait] +impl leetcode_auth_service_server::LeetcodeAuthService + for LeetcodeAuthService +{ + async fn get_token( + &self, + _request: Request, + ) -> Result, Status> { + let auth = self + .repository + .get() + .await + .map_err(redis_status)? + .auth + .ok_or_else(|| Status::not_found("no leetcode credentials stored yet"))?; + + Ok(GetTokenResponse { + token: auth.token, + csrf: auth.csrf, + } + .into()) + } + + async fn refresh_token( + &self, + _request: Request, + ) -> Result, Status> { + let started = self + .token_manager + .trigger() + .await + .map_err(redis_status)? + .is_some(); + + Ok(RefreshTokenResponse { started }.into()) + } +} + +#[cfg(test)] +mod tests { + use std::time::Duration; + + use chrono::DateTime; + use tonic::Code; + + use super::{leetcode_auth_service_server::LeetcodeAuthService as _, *}; + use crate::{ + db::auth::fake::FakeAuthRepository, + domain::auth::{AuthToken, StoredAuth}, + grpc::client::lass::fake::{FakeAuthenticator, Outcome}, + }; + + fn service( + repository: FakeAuthRepository, + lass: FakeAuthenticator, + ) -> LeetcodeAuthService { + let token_refresher = TokenManager::new( + repository.clone(), + lass, + Duration::from_secs(60), + Duration::from_secs(60), + "test-pod".to_string(), + ); + LeetcodeAuthService::new(repository, token_refresher) + } + + fn token(value: &str) -> AuthToken { + AuthToken::new(value.into(), format!("{value}-csrf")).unwrap() + } + + #[tokio::test] + async fn get_token_reports_missing_and_unavailable_store() { + let lass = FakeAuthenticator::new(Outcome::Fail); + + let error = service( + FakeAuthRepository::stored(StoredAuth::default()), + lass.clone(), + ) + .get_token(Request::new(GetTokenRequest {})) + .await + .unwrap_err(); + assert_eq!(error.code(), Code::NotFound); + + let error = service(FakeAuthRepository::Unavailable, lass) + .get_token(Request::new(GetTokenRequest {})) + .await + .unwrap_err(); + assert_eq!(error.code(), Code::Unavailable); + assert_eq!(error.message(), "token store unavailable"); + } + + #[tokio::test] + async fn get_token_serves_stale_credentials_while_a_refresh_runs() { + let repository = FakeAuthRepository::stored(StoredAuth { + auth: Some(token("old")), + date_stored: Some(DateTime::UNIX_EPOCH), + }); + let lass = FakeAuthenticator::gated(Outcome::Succeed(token("new"))); + let service = service(repository, lass.clone()); + + let started = service + .refresh_token(Request::new(RefreshTokenRequest {})) + .await + .unwrap() + .into_inner() + .started; + assert!(started); + + let ignored = service + .refresh_token(Request::new(RefreshTokenRequest {})) + .await + .unwrap() + .into_inner() + .started; + assert!(!ignored); + + lass.wait_for_calls(1).await; + assert_eq!(lass.calls(), 1); + + let response = service + .get_token(Request::new(GetTokenRequest {})) + .await + .unwrap() + .into_inner(); + assert_eq!(response.token, "old"); + assert_eq!(response.csrf, "old-csrf"); + assert_eq!(lass.calls(), 1, "get_token must never start a refresh"); + } +} diff --git a/internal/leetcode-auth-service/src/grpc/client/lass.rs b/internal/leetcode-auth-service/src/grpc/client/lass.rs new file mode 100644 index 000000000..20b8b9bc3 --- /dev/null +++ b/internal/leetcode-auth-service/src/grpc/client/lass.rs @@ -0,0 +1,132 @@ +use std::time::Duration; + +use anyhow::Context; +use tonic::{ + Status, + transport::{Channel, Endpoint}, +}; + +use crate::{ + config::Config, + domain::auth::AuthToken, + proto::lass::{ + AuthenticateRequest, leetcode_auth_stealer_service_client::LeetcodeAuthStealerServiceClient, + }, +}; + +/// Logs into leetcode.com and returns fresh credentials. +/// Leetcode Auth Stealer Service +pub trait LASS: Clone + Send + Sync + 'static { + fn authenticate(&self) -> impl Future> + Send; +} + +#[derive(Clone)] +pub struct GrpcLASS { + client: LeetcodeAuthStealerServiceClient, + login: String, + password: String, +} + +impl GrpcLASS { + /// Connects lazily so this service starts, and keeps serving stored credentials, while LASS is + /// down. + pub fn new(config: &Config) -> anyhow::Result { + let channel = Endpoint::from_shared(config.lass_url.clone()) + .context("LASS_URL must be a valid URI")? + .connect_timeout(Duration::from_secs(5)) + .timeout(config.lass_timeout) + .connect_lazy(); + + Ok(Self { + client: LeetcodeAuthStealerServiceClient::new(channel), + login: config.leetcode_auth_username.clone(), + password: config.leetcode_auth_password.clone(), + }) + } +} + +impl LASS for GrpcLASS { + async fn authenticate(&self) -> Result { + let response = self + .client + .clone() + .authenticate(AuthenticateRequest { + login: self.login.clone(), + password: self.password.clone(), + }) + .await? + .into_inner(); + + AuthToken::new(response.leetcode_session, response.csrf_token) + .ok_or_else(|| Status::internal("LASS returned incomplete credentials")) + } +} + +#[cfg(test)] +pub mod fake { + use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, + }; + + use tokio::sync::Semaphore; + + use super::*; + + pub enum Outcome { + Succeed(AuthToken), + Fail, + } + + /// Counts calls. A gated fake blocks every call until [`FakeAuthenticator::release`]. + #[derive(Clone)] + pub struct FakeAuthenticator { + outcome: Arc, + calls: Arc, + gate: Arc, + } + + impl FakeAuthenticator { + pub fn new(outcome: Outcome) -> Self { + Self::with_permits(outcome, Semaphore::MAX_PERMITS) + } + + pub fn gated(outcome: Outcome) -> Self { + Self::with_permits(outcome, 0) + } + + fn with_permits(outcome: Outcome, permits: usize) -> Self { + Self { + outcome: Arc::new(outcome), + calls: Arc::new(AtomicUsize::new(0)), + gate: Arc::new(Semaphore::new(permits)), + } + } + + pub fn release(&self) { + self.gate.add_permits(1); + } + + pub fn calls(&self) -> usize { + self.calls.load(Ordering::SeqCst) + } + + pub async fn wait_for_calls(&self, calls: usize) { + while self.calls() < calls { + tokio::task::yield_now().await; + } + } + } + + impl LASS for FakeAuthenticator { + async fn authenticate(&self) -> Result { + self.calls.fetch_add(1, Ordering::SeqCst); + self.gate.acquire().await.unwrap().forget(); + + match &*self.outcome { + Outcome::Succeed(auth) => Ok(auth.clone()), + Outcome::Fail => Err(Status::unavailable("LASS unavailable")), + } + } + } +} diff --git a/internal/leetcode-auth-service/src/grpc/client/mod.rs b/internal/leetcode-auth-service/src/grpc/client/mod.rs new file mode 100644 index 000000000..3b2067cf9 --- /dev/null +++ b/internal/leetcode-auth-service/src/grpc/client/mod.rs @@ -0,0 +1 @@ +pub mod lass; diff --git a/internal/leetcode-auth-service/src/grpc/metrics.rs b/internal/leetcode-auth-service/src/grpc/metrics.rs new file mode 100644 index 000000000..6bd1ffc8a --- /dev/null +++ b/internal/leetcode-auth-service/src/grpc/metrics.rs @@ -0,0 +1,202 @@ +use std::{ + sync::Arc, + task::{Context, Poll}, + time::Instant, +}; + +use bytes::Bytes; +use metrics::{counter, histogram}; +use pin_project_lite::pin_project; +use tonic::{ + Status, + body::Body, + codegen::http::{HeaderMap, Request, Response}, +}; +use tonic_middleware::{Middleware, ServiceBound}; + +// TODO: migrate this to a separate library + +#[derive(Debug, Clone)] +pub struct GrpcRequestMetricsMiddleware; + +#[tonic::async_trait] +impl Middleware for GrpcRequestMetricsMiddleware +where + S: ServiceBound, + S::Future: Send, +{ + async fn call(&self, req: Request, mut service: S) -> Result, S::Error> { + let start = Instant::now(); + let grpc_method = Arc::from(req.uri().path().trim_start_matches('/')); + + let result = service.call(req).await; + + match result { + Ok(response) => { + if let Some(status) = status_code_from(response.headers()) { + record_grpc_request_metrics(grpc_method, status, start); + return Ok(response); + } + + let (parts, body) = response.into_parts(); + let body = ObservedGrpcBody { + body, + recorder: GrpcMetricsRecorder { + grpc_method, + start, + recorded: false, + }, + }; + + Ok(Response::from_parts(parts, Body::new(body))) + } + Err(err) => { + record_grpc_request_metrics(grpc_method, "TRANSPORT_ERROR", start); + Err(err) + } + } + } +} + +fn status_code_from(headers: &HeaderMap) -> Option<&'static str> { + headers + .get("grpc-status") + .map(|value| match value.as_bytes() { + b"0" => "OK", + b"1" => "CANCELLED", + b"2" => "UNKNOWN", + b"3" => "INVALID_ARGUMENT", + b"4" => "DEADLINE_EXCEEDED", + b"5" => "NOT_FOUND", + b"6" => "ALREADY_EXISTS", + b"7" => "PERMISSION_DENIED", + b"8" => "RESOURCE_EXHAUSTED", + b"9" => "FAILED_PRECONDITION", + b"10" => "ABORTED", + b"11" => "OUT_OF_RANGE", + b"12" => "UNIMPLEMENTED", + b"13" => "INTERNAL", + b"14" => "UNAVAILABLE", + b"15" => "DATA_LOSS", + b"16" => "UNAUTHENTICATED", + _ => "UNKNOWN", + }) +} + +fn record_grpc_request_metrics(grpc_method: Arc, status: &'static str, start: Instant) { + // Unknown services and methods answer UNIMPLEMENTED. Their paths come from the client, so + // collapse them into one label value to keep the series count bounded. + let grpc_method = if status == "UNIMPLEMENTED" { + Arc::from("unknown") + } else { + grpc_method + }; + + counter!( + "grpc_requests_total", + "method" => grpc_method.clone(), + "status" => status, + ) + .increment(1); + + histogram!( + "grpc_request_duration_seconds", + "method" => grpc_method, + "status" => status, + ) + .record(start.elapsed().as_secs_f64()); +} + +pin_project! { + struct ObservedGrpcBody { + #[pin] + body: Body, + recorder: GrpcMetricsRecorder, + } +} + +struct GrpcMetricsRecorder { + grpc_method: Arc, + start: Instant, + recorded: bool, +} + +impl GrpcMetricsRecorder { + fn record(&mut self, status: &'static str) { + if self.recorded { + return; + } + + self.recorded = true; + record_grpc_request_metrics(self.grpc_method.clone(), status, self.start); + } +} + +impl Drop for GrpcMetricsRecorder { + fn drop(&mut self) { + if !self.recorded { + self.record("INCOMPLETE"); + } + } +} + +impl http_body::Body for ObservedGrpcBody { + type Data = Bytes; + type Error = Status; + + fn poll_frame( + self: std::pin::Pin<&mut Self>, + cx: &mut Context<'_>, + ) -> Poll, Self::Error>>> { + let this = self.project(); + + match this.body.poll_frame(cx) { + Poll::Ready(Some(Ok(frame))) => { + if let Some(trailers) = frame.trailers_ref() { + let status = status_code_from(trailers).unwrap_or("UNKNOWN"); + this.recorder.record(status); + } + + Poll::Ready(Some(Ok(frame))) + } + Poll::Ready(Some(Err(status))) => { + let code = match status.code() { + tonic::Code::Ok => "OK", + tonic::Code::Cancelled => "CANCELLED", + tonic::Code::Unknown => "UNKNOWN", + tonic::Code::InvalidArgument => "INVALID_ARGUMENT", + tonic::Code::DeadlineExceeded => "DEADLINE_EXCEEDED", + tonic::Code::NotFound => "NOT_FOUND", + tonic::Code::AlreadyExists => "ALREADY_EXISTS", + tonic::Code::PermissionDenied => "PERMISSION_DENIED", + tonic::Code::ResourceExhausted => "RESOURCE_EXHAUSTED", + tonic::Code::FailedPrecondition => "FAILED_PRECONDITION", + tonic::Code::Aborted => "ABORTED", + tonic::Code::OutOfRange => "OUT_OF_RANGE", + tonic::Code::Unimplemented => "UNIMPLEMENTED", + tonic::Code::Internal => "INTERNAL", + tonic::Code::Unavailable => "UNAVAILABLE", + tonic::Code::DataLoss => "DATA_LOSS", + tonic::Code::Unauthenticated => "UNAUTHENTICATED", + }; + + this.recorder.record(code); + + Poll::Ready(Some(Err(status))) + } + Poll::Ready(None) => { + this.recorder.record("OK"); + Poll::Ready(None) + } + Poll::Pending => Poll::Pending, + } + } + + fn size_hint(&self) -> http_body::SizeHint { + self.body.size_hint() + } + + fn is_end_stream(&self) -> bool { + self.body.is_end_stream() + } +} diff --git a/internal/leetcode-auth-service/src/grpc/mod.rs b/internal/leetcode-auth-service/src/grpc/mod.rs new file mode 100644 index 000000000..50b966f2e --- /dev/null +++ b/internal/leetcode-auth-service/src/grpc/mod.rs @@ -0,0 +1,10 @@ +use tonic::Status; + +pub mod auth; +pub mod client; +pub mod metrics; + +pub fn redis_status(error: redis::RedisError) -> Status { + tracing::error!(%error, "redis operation failed"); + Status::unavailable("token store unavailable") +} diff --git a/internal/leetcode-auth-service/src/http/health.rs b/internal/leetcode-auth-service/src/http/health.rs new file mode 100644 index 000000000..47a9f1640 --- /dev/null +++ b/internal/leetcode-auth-service/src/http/health.rs @@ -0,0 +1,16 @@ +use axum::{extract::State, http::StatusCode}; + +use crate::http::state::AppState; + +pub async fn ready(State(state): State) -> Result<&'static str, StatusCode> { + state.health_check_repository.check().await.map_err(|err| { + tracing::warn!(%err, "redis health check failed"); + StatusCode::SERVICE_UNAVAILABLE + })?; + + Ok("ok") +} + +pub async fn live() -> &'static str { + "ok" +} diff --git a/internal/leetcode-auth-service/src/http/metrics.rs b/internal/leetcode-auth-service/src/http/metrics.rs new file mode 100644 index 000000000..a686769a1 --- /dev/null +++ b/internal/leetcode-auth-service/src/http/metrics.rs @@ -0,0 +1,21 @@ +use crate::http::state::AppState; +use axum::{ + extract::State, + http::{StatusCode, header}, + response::IntoResponse, +}; + +pub async fn metrics(State(state): State) -> Result { + // add more repo / non-auto metric collections here + state.sys_collector.collect(); + + let body = state.prometheus_handle.render(); + + Ok(( + [( + header::CONTENT_TYPE, + "text/plain; version=0.0.4; charset=utf-8", + )], + body, + )) +} diff --git a/internal/leetcode-auth-service/src/http/mod.rs b/internal/leetcode-auth-service/src/http/mod.rs new file mode 100644 index 000000000..53212e024 --- /dev/null +++ b/internal/leetcode-auth-service/src/http/mod.rs @@ -0,0 +1,19 @@ +pub mod health; +pub mod metrics; +pub mod state; +pub mod version; + +use axum::{Router, routing::get}; +use tower_http::catch_panic; + +use crate::http::state::AppState; + +pub fn router(state: AppState) -> Router { + Router::new() + .route("/livez", get(health::live)) + .route("/readyz", get(health::ready)) + .route("/metrics", get(metrics::metrics)) + .route("/version", get(version::version)) + .layer(catch_panic::CatchPanicLayer::new()) + .with_state(state) +} diff --git a/internal/leetcode-auth-service/src/http/state.rs b/internal/leetcode-auth-service/src/http/state.rs new file mode 100644 index 000000000..1b7014d55 --- /dev/null +++ b/internal/leetcode-auth-service/src/http/state.rs @@ -0,0 +1,12 @@ +use metrics_exporter_prometheus::PrometheusHandle; +use metrics_process::Collector; + +use crate::{config::Config, db::health_check::HealthCheckRepository}; + +#[derive(Clone)] +pub struct AppState { + pub config: Config, + pub prometheus_handle: PrometheusHandle, + pub sys_collector: Collector, + pub health_check_repository: HealthCheckRepository, +} diff --git a/internal/leetcode-auth-service/src/http/version.rs b/internal/leetcode-auth-service/src/http/version.rs new file mode 100644 index 000000000..1401dccd2 --- /dev/null +++ b/internal/leetcode-auth-service/src/http/version.rs @@ -0,0 +1,10 @@ +use axum::extract::State; + +use crate::http::state::AppState; + +// we are wasting heap allocations here, but it doesn't matter +// because no machine calls `/version`. we would only call it manually +// to see what deployed version of the service is available, that's all +pub async fn version(State(state): State) -> String { + state.config.version.clone().unwrap_or("N/A".to_string()) +} diff --git a/internal/leetcode-auth-service/src/lib.rs b/internal/leetcode-auth-service/src/lib.rs new file mode 100644 index 000000000..99b03a05c --- /dev/null +++ b/internal/leetcode-auth-service/src/lib.rs @@ -0,0 +1,7 @@ +pub mod config; +pub mod db; +pub mod domain; +pub mod grpc; +pub mod http; +pub mod proto; +pub mod service; diff --git a/internal/leetcode-auth-service/src/main.rs b/internal/leetcode-auth-service/src/main.rs new file mode 100644 index 000000000..12ecd899f --- /dev/null +++ b/internal/leetcode-auth-service/src/main.rs @@ -0,0 +1,148 @@ +use anyhow::Context; +use metrics_exporter_prometheus::PrometheusBuilder; +use tokio::net::TcpListener; +use tonic::transport::Server; +use tonic_middleware::MiddlewareLayer; +use tower_http::catch_panic; +use tracing::info; + +use leetcode_auth_service::{ + config::Config, + db::{auth::RedisAuthRepository, health_check::HealthCheckRepository, redis}, + grpc::{ + auth::{LeetcodeAuthService, LeetcodeAuthServiceServer}, + client::lass::GrpcLASS, + metrics::GrpcRequestMetricsMiddleware, + }, + http::{router, state::AppState}, + proto, + service::refresh::TokenManager, +}; + +fn init_tracing() { + let use_json = std::env::var("ENVIRONMENT") + .map(|v| v.eq_ignore_ascii_case("production") || v.eq_ignore_ascii_case("staging")) + .unwrap_or(false); + + let env_filter = tracing_subscriber::EnvFilter::try_from_default_env() + .unwrap_or_else(|_| tracing_subscriber::EnvFilter::new("info")); + + if use_json { + tracing_subscriber::fmt() + .json() + .with_env_filter(env_filter) + .init(); + } else { + tracing_subscriber::fmt() + .pretty() + .with_env_filter(env_filter) + .init(); + } +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + init_tracing(); + + let prom_handle = PrometheusBuilder::new() + .install_recorder() + .context("failed to install Prometheus recorder")?; + let sys_collector = metrics_process::Collector::default(); + sys_collector.describe(); + + let config = Config::from_env()?; + config.log(); + + let redis = redis::client::connect(&config)?; + + let repository = RedisAuthRepository::new(redis.clone()); + + let token_manager = TokenManager::new( + repository.clone(), + GrpcLASS::new(&config)?, + config.token_max_age, + config.refresh_lock_ttl, + config.hostname.clone(), + ); + + let auth_service = LeetcodeAuthService::new(repository, token_manager.clone()); + + let grpc_addr = format!("0.0.0.0:{}", config.grpc_port).parse()?; + let http_addr = format!("0.0.0.0:{}", config.http_port); + + let (grpc_health_reporter, grpc_health_svc) = tonic_health::server::health_reporter(); + grpc_health_reporter + .set_serving::>>() + .await; + grpc_health_reporter + .set_service_status("", tonic_health::ServingStatus::Serving) + .await; + + let reflection = |reflection_service: &'static str| { + tonic_reflection::server::Builder::configure() + .register_encoded_file_descriptor_set(proto::FILE_DESCRIPTOR_SET) + .with_service_name(reflection_service) + .with_service_name(proto::leetcodeauth::leetcode_auth_service_server::SERVICE_NAME) + }; + + let (grpc_reflection_svc, grpc_reflection_svc2) = if config.grpc_reflection { + ( + Some( + reflection(tonic_reflection::pb::v1::server_reflection_server::SERVICE_NAME) + .build_v1() + .context("failed to build v1 gRPC reflection service")?, + ), + Some( + reflection(tonic_reflection::pb::v1alpha::server_reflection_server::SERVICE_NAME) + .build_v1alpha() + .context("failed to build v1alpha gRPC reflection service")?, + ), + ) + } else { + (None, None) + }; + + let grpc_panic_layer = catch_panic::CatchPanicLayer::custom(|_panic_info| { + let status = tonic::Status::internal("An unhandled panic occurred"); + status.into_http::() + }); + + info!(every = ?config.refresh_check_interval, "starting scheduled refresh checks"); + let schedule = tokio::spawn(token_manager.run_schedule(config.refresh_check_interval)); + + info!(addr = %grpc_addr, "starting grpc server"); + let grpc_server = tokio::spawn(async move { + Server::builder() + .layer(grpc_panic_layer) + .layer(MiddlewareLayer::new(GrpcRequestMetricsMiddleware)) + .add_service(grpc_health_svc) + .add_service(LeetcodeAuthServiceServer::new(auth_service)) + .add_optional_service(grpc_reflection_svc) + .add_optional_service(grpc_reflection_svc2) + .serve(grpc_addr) + .await + }); + + let http_app_state = AppState { + config, + prometheus_handle: prom_handle, + sys_collector, + health_check_repository: HealthCheckRepository::new(redis), + }; + + let http_listener = TcpListener::bind(&http_addr) + .await + .context("failed to bind to http address")?; + + info!(addr = %http_addr, "starting http server"); + let http_server = + tokio::spawn(async move { axum::serve(http_listener, router(http_app_state)).await }); + + tokio::select! { + result = http_server => result.context("HTTP server task panicked")?.context("HTTP server failed")?, + result = grpc_server => result.context("gRPC server task panicked")?.context("gRPC server failed")?, + result = schedule => result.context("refresh schedule panicked")?, + } + + anyhow::bail!("a server task exited unexpectedly") +} diff --git a/internal/leetcode-auth-service/src/proto.rs b/internal/leetcode-auth-service/src/proto.rs new file mode 100644 index 000000000..591735d70 --- /dev/null +++ b/internal/leetcode-auth-service/src/proto.rs @@ -0,0 +1,10 @@ +pub mod leetcodeauth { + include!("proto/leetcodeauth.rs"); +} + +/// Client for `leetcode-auth-stealer-service` (LASS). +pub mod lass { + include!("proto/codebloom.leetcode.auth.v1.rs"); +} + +pub const FILE_DESCRIPTOR_SET: &[u8] = include_bytes!("proto/descriptor.bin"); diff --git a/internal/leetcode-auth-service/src/service/mod.rs b/internal/leetcode-auth-service/src/service/mod.rs new file mode 100644 index 000000000..1d2e0f5bb --- /dev/null +++ b/internal/leetcode-auth-service/src/service/mod.rs @@ -0,0 +1 @@ +pub mod refresh; diff --git a/internal/leetcode-auth-service/src/service/refresh.rs b/internal/leetcode-auth-service/src/service/refresh.rs new file mode 100644 index 000000000..b5ade3ab0 --- /dev/null +++ b/internal/leetcode-auth-service/src/service/refresh.rs @@ -0,0 +1,293 @@ +use std::time::Duration; + +use chrono::Utc; +use metrics::counter; +use redis::RedisError; +use tokio::{task::JoinHandle, time::MissedTickBehavior}; +use tracing::{debug, error, info, warn}; + +use crate::{db::auth::AuthRepository, grpc::client::lass::LASS}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum RefreshOutcome { + Stored, + LassFailed, + StoreFailed, +} + +impl RefreshOutcome { + fn as_str(self) -> &'static str { + match self { + Self::Stored => "stored", + Self::LassFailed => "lass_failed", + Self::StoreFailed => "store_failed", + } + } +} + +#[derive(Clone)] +pub struct TokenManager { + repository: R, + authenticator: A, + max_age: Duration, + lock_ttl: Duration, + hostname: String, +} + +impl TokenManager { + pub fn new( + repository: R, + authenticator: A, + max_age: Duration, + lock_ttl: Duration, + hostname: String, + ) -> Self { + Self { + repository, + authenticator, + max_age, + lock_ttl, + hostname, + } + } + + /// Starts a refresh in the background. `None` means another refresh holds the lock and this + /// call was ignored. If the refresh panics, the lock is not released and expires after + /// `lock_ttl`. + pub async fn trigger(&self) -> Result>, RedisError> { + let owner = lock_owner(&self.hostname); + if !self.repository.try_lock(&owner, self.lock_ttl).await? { + return Ok(None); + } + + let this = self.clone(); + Ok(Some(tokio::spawn(async move { + let outcome = this.refresh().await; + + if let Err(error) = this.repository.unlock(&owner).await { + warn!(%error, "could not release refresh lock, it will expire on its own"); + } + + counter!("leetcode_auth_refresh_total", "result" => outcome.as_str()).increment(1); + outcome + }))) + } + + async fn refresh(&self) -> RefreshOutcome { + info!("requesting new credentials from LASS"); + let auth = match self.authenticator.authenticate().await { + Ok(auth) => auth, + Err(status) => { + warn!(code = ?status.code(), message = status.message(), "LASS refresh failed"); + return RefreshOutcome::LassFailed; + } + }; + + let date_stored = Utc::now(); + match self.repository.store(&auth, date_stored).await { + Ok(()) => { + info!(%date_stored, "stored new credentials"); + RefreshOutcome::Stored + } + Err(error) => { + error!(%error, "could not store new credentials"); + RefreshOutcome::StoreFailed + } + } + } +} + +// scheduled refresh methods +impl TokenManager { + pub async fn refresh_if_stale(&self) -> Option> { + let stored = match self.repository.get().await { + Ok(stored) => stored, + Err(error) => { + warn!(%error, "could not read stored credentials, skipping scheduled refresh"); + return None; + } + }; + + if !stored.is_stale(Utc::now(), self.max_age) { + debug!("stored credentials are fresh"); + return None; + } + + info!(date_stored = ?stored.date_stored, "stored credentials are stale"); + match self.trigger().await { + Ok(Some(running)) => Some(running), + Ok(None) => { + info!("another refresh holds the lock, skipping scheduled refresh"); + None + } + Err(error) => { + warn!(%error, "could not take refresh lock, skipping scheduled refresh"); + None + } + } + } + + pub async fn run_schedule(self, every: Duration) { + let mut interval = tokio::time::interval(every); + interval.set_missed_tick_behavior(MissedTickBehavior::Delay); + + loop { + interval.tick().await; + self.refresh_if_stale().await; + } + } +} + +fn lock_owner(hostname: &str) -> String { + let now = Utc::now().timestamp_nanos_opt().unwrap_or_default(); + format!("{hostname}:{now}") +} + +#[cfg(test)] +mod tests { + use chrono::TimeDelta; + + use super::*; + use crate::{ + db::auth::fake::FakeAuthRepository, + domain::auth::{AuthToken, StoredAuth}, + grpc::client::lass::fake::{FakeAuthenticator, Outcome}, + }; + + const MAX_AGE: Duration = Duration::from_secs(4 * 60 * 60); + const LOCK_TTL: Duration = Duration::from_secs(60); + + fn token_refresher( + repository: &FakeAuthRepository, + lass: &FakeAuthenticator, + ) -> TokenManager { + TokenManager::new( + repository.clone(), + lass.clone(), + MAX_AGE, + LOCK_TTL, + "test-pod".to_string(), + ) + } + + fn token(value: &str) -> AuthToken { + AuthToken::new(value.into(), format!("{value}-csrf")).unwrap() + } + + fn stored(value: &str, age: TimeDelta) -> StoredAuth { + StoredAuth { + auth: Some(token(value)), + date_stored: Some(Utc::now() - age), + } + } + + #[tokio::test] + async fn triggers_during_a_refresh_are_ignored_until_it_finishes() { + let repository = FakeAuthRepository::stored(stored("old", TimeDelta::hours(1))); + let lass = FakeAuthenticator::gated(Outcome::Succeed(token("new"))); + let token_refresher = token_refresher(&repository, &lass); + + let running = token_refresher + .trigger() + .await + .unwrap() + .expect("first trigger starts a refresh"); + for _ in 0..5 { + assert!(token_refresher.trigger().await.unwrap().is_none()); + } + + lass.release(); + assert_eq!(running.await.unwrap(), RefreshOutcome::Stored); + assert_eq!(lass.calls(), 1); + assert_eq!(repository.snapshot().stored.auth, Some(token("new"))); + assert_eq!(repository.snapshot().lock, None); + + lass.release(); + let next = token_refresher + .trigger() + .await + .unwrap() + .expect("a finished refresh allows the next one"); + assert_eq!(next.await.unwrap(), RefreshOutcome::Stored); + assert_eq!(lass.calls(), 2); + } + + #[tokio::test] + async fn a_lock_held_by_another_instance_blocks_refreshes() { + let repository = FakeAuthRepository::stored(StoredAuth::default()); + let lass = FakeAuthenticator::new(Outcome::Succeed(token("new"))); + repository.set_lock(Some("other-pod:0")); + + let token_refresher = token_refresher(&repository, &lass); + assert!(token_refresher.trigger().await.unwrap().is_none()); + assert!(token_refresher.refresh_if_stale().await.is_none()); + assert_eq!(lass.calls(), 0); + assert_eq!(repository.snapshot().lock.as_deref(), Some("other-pod:0")); + } + + #[tokio::test] + async fn failed_refreshes_keep_stored_credentials_and_release_the_lock() { + let before = stored("old", TimeDelta::hours(5)); + let repository = FakeAuthRepository::stored(before.clone()); + let lass = FakeAuthenticator::new(Outcome::Fail); + + let running = token_refresher(&repository, &lass) + .trigger() + .await + .unwrap() + .unwrap(); + assert_eq!(running.await.unwrap(), RefreshOutcome::LassFailed); + assert_eq!(repository.snapshot().stored, before); + assert_eq!(repository.snapshot().lock, None); + } + + #[tokio::test] + async fn the_latest_refresh_overwrites_credentials_written_meanwhile() { + let repository = FakeAuthRepository::stored(stored("old", TimeDelta::hours(5))); + let lass = FakeAuthenticator::gated(Outcome::Succeed(token("ours"))); + let running = token_refresher(&repository, &lass) + .trigger() + .await + .unwrap() + .unwrap(); + lass.wait_for_calls(1).await; + + repository + .store(&token("theirs"), Utc::now()) + .await + .unwrap(); + + lass.release(); + assert_eq!(running.await.unwrap(), RefreshOutcome::Stored); + assert_eq!(repository.snapshot().stored.auth, Some(token("ours"))); + } + + #[tokio::test] + async fn scheduled_checks_only_refresh_stale_or_missing_credentials() { + for (initial, expect_refresh) in [ + (stored("fresh", TimeDelta::hours(1)), false), + (stored("stale", TimeDelta::hours(5)), true), + (StoredAuth::default(), true), + ] { + let repository = FakeAuthRepository::stored(initial); + let lass = FakeAuthenticator::new(Outcome::Succeed(token("new"))); + + match token_refresher(&repository, &lass).refresh_if_stale().await { + Some(running) => assert_eq!(running.await.unwrap(), RefreshOutcome::Stored), + None => assert!(!expect_refresh), + } + + assert_eq!(lass.calls(), usize::from(expect_refresh)); + } + } + + #[tokio::test] + async fn unreachable_store_never_calls_lass() { + let lass = FakeAuthenticator::new(Outcome::Succeed(token("new"))); + let token_refresher = token_refresher(&FakeAuthRepository::Unavailable, &lass); + + assert!(token_refresher.refresh_if_stale().await.is_none()); + assert!(token_refresher.trigger().await.is_err()); + assert_eq!(lass.calls(), 0); + } +}