From 146b78dcb6948d8d7a8ab3e7ae7df064cfbe624d Mon Sep 17 00:00:00 2001 From: Brandon Thomas Date: Mon, 15 Jun 2026 19:08:58 -0400 Subject: [PATCH] crate for singualr type --- Cargo.lock | 7 ++ Cargo.toml | 2 + .../artcraft/artcraft_api_key/Cargo.toml | 20 +++++ .../artcraft/artcraft_api_key/src/lib.rs | 82 +++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 crates/api_clients/artcraft/artcraft_api_key/Cargo.toml create mode 100644 crates/api_clients/artcraft/artcraft_api_key/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 159bd14281..d55134eda4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -712,6 +712,13 @@ dependencies = [ "workspace-build-acceleration", ] +[[package]] +name = "artcraft_api_key" +version = "0.0.1" +dependencies = [ + "workspace-build-acceleration", +] + [[package]] name = "artcraft_client" version = "0.0.1" diff --git a/Cargo.toml b/Cargo.toml index fad9ecea3d..04fda1964b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ members = [ # API clients for external services # These might be custom implementations or wrappers over other client libraries. "crates/api_clients/artcraft/artcraft_api_defs", + "crates/api_clients/artcraft/artcraft_api_key", "crates/api_clients/artcraft/artcraft_client", "crates/api_clients/artcraft/artcraft_router", "crates/api_clients/cloud_storage", @@ -232,6 +233,7 @@ workspace-build-acceleration = { path = "crates/workspace-build-acceleration" } actix_artcraft = { path = "crates/lib/actix_artcraft" } actix_helpers = { path = "crates/lib/actix_helpers" } artcraft_api_defs = { path = "crates/api_clients/artcraft/artcraft_api_defs" } +artcraft_api_key = { path = "crates/api_clients/artcraft/artcraft_api_key" } artcraft_client = { path = "crates/api_clients/artcraft/artcraft_client" } artcraft_router = { path = "crates/api_clients/artcraft/artcraft_router" } bootstrap = { path = "crates/lib/bootstrap" } diff --git a/crates/api_clients/artcraft/artcraft_api_key/Cargo.toml b/crates/api_clients/artcraft/artcraft_api_key/Cargo.toml new file mode 100644 index 0000000000..0b52325d40 --- /dev/null +++ b/crates/api_clients/artcraft/artcraft_api_key/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "artcraft_api_key" +edition = "2024" +version = "0.0.1" +authors = [ + "Brandon Thomas ", + "Brandon Thomas ", +] +publish = false + +[package.metadata.cargo-udeps.ignore] +normal = ["workspace-build-acceleration"] + +[lib] +name = "artcraft_api_key" +path = "src/lib.rs" + +[dependencies] +# Hackari workspace (faster compile times) +workspace-build-acceleration.workspace = true diff --git a/crates/api_clients/artcraft/artcraft_api_key/src/lib.rs b/crates/api_clients/artcraft/artcraft_api_key/src/lib.rs new file mode 100644 index 0000000000..e6d1dbef90 --- /dev/null +++ b/crates/api_clients/artcraft/artcraft_api_key/src/lib.rs @@ -0,0 +1,82 @@ +use std::fmt; + +/// Number of leading characters shown when an [`ArtcraftApiKey`] is formatted. The remainder of +/// the secret is redacted so the full key never lands in logs or error messages. +const REDACTED_PREFIX_CHAR_COUNT: usize = 10; + +/// An Artcraft API key (e.g. `artcraft_api_`). +/// +/// `Debug` and `Display` both redact the value, printing only the first +/// [`REDACTED_PREFIX_CHAR_COUNT`] characters followed by an ellipsis so the full secret is never +/// accidentally logged. +#[derive(Clone, PartialEq, Eq, Hash)] +pub struct ArtcraftApiKey(pub String); + +impl ArtcraftApiKey { + pub fn new_from_str(value: &str) -> ArtcraftApiKey { + ArtcraftApiKey(value.to_string()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } + + /// The redacted form used by both `Debug` and `Display`: the first + /// [`REDACTED_PREFIX_CHAR_COUNT`] characters followed by an ellipsis. + fn redacted(&self) -> String { + let prefix: String = self.0.chars().take(REDACTED_PREFIX_CHAR_COUNT).collect(); + format!("{prefix}…") + } +} + +impl fmt::Display for ArtcraftApiKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.redacted()) + } +} + +impl fmt::Debug for ArtcraftApiKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ArtcraftApiKey({:?})", self.redacted()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + const SAMPLE_KEY: &str = "artcraft_api_55ax0zhd580m598r6n4n7szdwjb2b28sypapvawh"; + + #[test] + fn new_from_str_and_as_str_round_trip() { + let key = ArtcraftApiKey::new_from_str(SAMPLE_KEY); + assert_eq!(key.as_str(), SAMPLE_KEY); + assert_eq!(key.0, SAMPLE_KEY); + } + + #[test] + fn display_redacts_to_first_ten_characters() { + let key = ArtcraftApiKey::new_from_str(SAMPLE_KEY); + assert_eq!(format!("{key}"), "artcraft_a…"); + } + + #[test] + fn debug_redacts_to_first_ten_characters() { + let key = ArtcraftApiKey::new_from_str(SAMPLE_KEY); + assert_eq!(format!("{key:?}"), "ArtcraftApiKey(\"artcraft_a…\")"); + } + + #[test] + fn formatting_does_not_leak_the_full_secret() { + let key = ArtcraftApiKey::new_from_str(SAMPLE_KEY); + let suffix = &SAMPLE_KEY[REDACTED_PREFIX_CHAR_COUNT..]; + assert!(!format!("{key}").contains(suffix)); + assert!(!format!("{key:?}").contains(suffix)); + } + + #[test] + fn shorter_than_prefix_is_printed_in_full() { + let key = ArtcraftApiKey::new_from_str("abc"); + assert_eq!(format!("{key}"), "abc…"); + } +}