Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use more typed-builder #378

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 101 additions & 72 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ version = "0.0.1-pre.3"

[patch.crates-io]
redis = { git = "https://github.com/aumetra/redis-rs.git", rev = "3c4ee09d432a69e1d87d66dcba14c519467c9b81" }
typed-builder = { git = "https://github.com/idanarye/rust-typed-builder.git", rev = "fd1bd8336aeb0dc334060635e2a6a1d8dc329e3c" }
4 changes: 2 additions & 2 deletions crates/kitsune-cache/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ edition.workspace = true
version.workspace = true

[dependencies]
async-trait = "0.1.73"
async-trait = "0.1.74"
deadpool-redis = "0.13.0"
derive_builder = "0.12.0"
enum_dispatch = "0.3.12"
moka = { version = "0.12.1", features = ["sync"] }
redis = "0.23.3"
serde = "1.0.189"
simd-json = "0.12.0"
thiserror = "1.0.49"
tracing = "0.1.39"
typed-builder = "0.17.0"

[dev-dependencies]
tokio = { version = "1.33.0", features = ["macros", "rt"] }
17 changes: 5 additions & 12 deletions crates/kitsune-cache/src/redis.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use super::{CacheBackend, CacheResult};
use async_trait::async_trait;
use derive_builder::Builder;
use redis::AsyncCommands;
use serde::{de::DeserializeOwned, Serialize};
use std::{fmt::Display, marker::PhantomData, time::Duration};
use typed_builder::TypedBuilder;

#[derive(Builder)]
#[builder(pattern = "owned")]
#[derive(TypedBuilder)]
pub struct Redis<K, V>
where
K: ?Sized,
{
#[builder(default = "\"DEFAULT-REDIS-CACHER\".into()")]
#[builder(default = "DEFAULT-REDIS-CACHER".into())]
namespace: String,
#[builder(setter(into))]
prefix: String,
redis_conn: deadpool_redis::Pool,
ttl: Duration,

// Type phantom data
#[builder(setter(skip))]
#[builder(default, setter(skip))]
_key: PhantomData<K>,
#[builder(setter(skip))]
#[builder(default, setter(skip))]
_value: PhantomData<V>,
}

Expand All @@ -39,12 +38,6 @@ where
.prefix(prefix)
.ttl(ttl)
.build()
.unwrap()
}

#[must_use]
pub fn builder() -> RedisBuilder<K, V> {
RedisBuilder::default()
}

fn compute_key(&self, key: impl Display) -> String {
Expand Down
4 changes: 2 additions & 2 deletions crates/kitsune-captcha/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
async-trait = "0.1.73"
async-trait = "0.1.74"
enum_dispatch = "0.3.12"
http = "0.2.9"
kitsune-http-client = { path = "../kitsune-http-client" }
Expand All @@ -13,4 +13,4 @@ serde_urlencoded = "0.7.1"
simd-json = "0.12.0"
strum = { version = "0.25.0", features = ["derive"] }
thiserror = "1.0.49"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
6 changes: 3 additions & 3 deletions crates/kitsune-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ ammonia = "3.3.0"
argon2 = "0.5.2"
async-recursion = "1.0.5"
async-stream = "0.3.5"
async-trait = "0.1.73"
async-trait = "0.1.74"
athena = { path = "../../lib/athena" }
autometrics = { version = "0.6.0", default-features = false }
base64-simd = "0.8.0"
Expand All @@ -20,7 +20,7 @@ diesel = "2.1.3"
diesel-async = { version = "0.4.1", features = ["postgres"] }
eyre = "0.6.8"
futures-util = "0.3.28"
garde = { version = "0.15.0", default-features = false, features = [
garde = { version = "0.16.0", default-features = false, features = [
"derive",
"email",
"email-idna",
Expand Down Expand Up @@ -67,7 +67,7 @@ time = "0.3.30"
tokio = { version = "1.33.0", features = ["macros", "rt"] }
toml = { version = "0.8.2", default-features = false, features = ["parse"] }
tracing = "0.1.39"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
url = "2.4.1"
zxcvbn = { version = "2.2.2", default-features = false }

Expand Down
3 changes: 1 addition & 2 deletions crates/kitsune-core/src/activitypub/deliverer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ impl Deliverer {
let private_key = PrivateKey::builder()
.key_id(&account.public_key_id)
.key(RsaKeyPair::from_pkcs8(pkcs8_document.as_bytes())?)
.build()
.unwrap();
.build();

let response = self.client.execute_signed(request, private_key).await?;
debug!(status_code = %response.status(), "successfully executed http request");
Expand Down
1 change: 0 additions & 1 deletion crates/kitsune-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ where
.redis_conn(pool.clone())
.ttl(Duration::from_secs(60)) // TODO: Parameterise this
.build()
.expect("[Bug] Failed to build the Redis cache")
.into()
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ speedy-uuid = { path = "../../lib/speedy-uuid", features = ["diesel"] }
thiserror = "1.0.49"
tokio = { version = "1.33.0", features = ["rt"] }
tracing-log = "0.1.3"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
4 changes: 2 additions & 2 deletions crates/kitsune-email/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ignored = ["askama_axum"] # See reason below.
[dependencies]
askama = "0.12.1"
askama_axum = "0.3.0" # Damn it, cargo. Because "kitsune" uses "askama" with the axum feature, we have to have the crate available here as well..
lettre = { version = "0.10.4", default-features = false, features = [
lettre = { version = "0.11.0", default-features = false, features = [
"builder",
"hostname",
"pool",
Expand All @@ -24,4 +24,4 @@ mrml = { version = "2.0.0-rc4", default-features = false, features = [
"render",
] }
thiserror = "1.0.49"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
2 changes: 1 addition & 1 deletion crates/kitsune-embed/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ once_cell = "1.18.0"
scraper = { version = "0.17.1", default-features = false }
smol_str = "0.2.0"
thiserror = "1.0.49"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
1 change: 0 additions & 1 deletion crates/kitsune-http-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,6 @@ impl Client {
.include_creation_timestamp(true)
.expires_in(Duration::from_secs(30)) // Make the signature expire in 30 seconds
.build()
.unwrap()
.sign(
&parts,
vec![
Expand Down
8 changes: 6 additions & 2 deletions crates/kitsune-http-signatures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ base64-simd = "0.8.0"
derive_builder = "0.12.0"
http = "0.2.9"
rayon = "1.8.0"
ring = { version = "0.17.3", features = ["std"] }
time = { version = "0.3.30", features = ["formatting", "parsing"] }
ring = { version = "0.17.4", features = ["std"] }
time = { version = "0.3.30", default-features = false, features = [
"formatting",
"parsing",
] }
thiserror = "1.0.49"
tokio = { version = "1.33.0", features = ["sync"] }
typed-builder = "0.17.0"

[dev-dependencies]
pem = "3.0.2"
Expand Down
37 changes: 8 additions & 29 deletions crates/kitsune-http-signatures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#![deny(missing_docs)]

use crate::{header::SignatureHeader, util::UnixTimestampExt};
use derive_builder::Builder;
use http::{
header::{HeaderName, InvalidHeaderName},
request::Parts,
Expand All @@ -24,6 +23,7 @@ use std::{
time::{Duration, SystemTime},
};
use tokio::sync::oneshot;
use typed_builder::TypedBuilder;

pub use crate::error::Error;
pub use ring;
Expand Down Expand Up @@ -109,8 +109,7 @@ impl SigningKey for RsaKeyPair {
/// Cryptographic key
///
/// Depending on the context its used in, it either represents a private or a public key
#[derive(Builder, Clone)]
#[builder(pattern = "owned")]
#[derive(Clone, TypedBuilder)]
pub struct PrivateKey<'a, K>
where
K: SigningKey,
Expand All @@ -122,16 +121,6 @@ where
key: K,
}

impl<'a, K> PrivateKey<'a, K>
where
K: SigningKey,
{
/// Return a builder of the private key
pub fn builder() -> PrivateKeyBuilder<'a, K> {
PrivateKeyBuilder::default()
}
}

struct SignatureString<'a> {
pub algorithm: &'a str,
pub components: &'a [SignatureComponent<'a>],
Expand Down Expand Up @@ -204,7 +193,7 @@ impl<'a> TryFrom<SignatureString<'a>> for String {
}

/// HTTP signer
#[derive(Builder, Clone)]
#[derive(Clone, TypedBuilder)]
pub struct HttpSigner {
/// Include the creation timestamp into the signing header
#[builder(default)]
Expand All @@ -216,11 +205,6 @@ pub struct HttpSigner {
}

impl HttpSigner {
/// Return a builder for the HTTP signer
pub fn builder() -> HttpSignerBuilder {
HttpSignerBuilder::default()
}

/// Sign an HTTP request
pub async fn sign<K>(
&self,
Expand Down Expand Up @@ -272,18 +256,18 @@ impl HttpSigner {

impl Default for HttpSigner {
fn default() -> Self {
Self::builder().build().unwrap()
Self::builder().build()
}
}

/// HTTP verifier
#[derive(Builder, Clone)]
#[derive(Clone, TypedBuilder)]
pub struct HttpVerifier {
/// Check whether the signature is expired
///
/// This just does a basic check if the `(expires)` header exists.
/// If you want a more aggressive check, use `enforce_expiration`
#[builder(default = "true")]
#[builder(default = true)]
check_expiration: bool,

/// Enforce the signature not being older than this specified duration
Expand All @@ -292,16 +276,11 @@ pub struct HttpVerifier {
/// - If the signature contains an `(expires)` header, we enforce the shorter one
///
/// Defaults to 5 minutes
#[builder(default = "Some(Duration::from_secs(5 * 60))")]
#[builder(default = Some(Duration::from_secs(5 * 60)))]
enforce_expiration: Option<Duration>,
}

impl HttpVerifier {
/// Return a builder for the HTTP verifier
pub fn builder() -> HttpVerifierBuilder {
HttpVerifierBuilder::default()
}

/// Verify an HTTP signature
///
/// `key_fn` is a function that obtains a public key (in its DER representation) based in its key ID
Expand Down Expand Up @@ -359,6 +338,6 @@ impl HttpVerifier {

impl Default for HttpVerifier {
fn default() -> Self {
Self::builder().build().unwrap()
Self::builder().build()
}
}
5 changes: 1 addition & 4 deletions crates/kitsune-http-signatures/tests/all_headers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ async fn all_headers() {
),
);
let public_key = self::data::get_public_key();
let signer = HttpVerifier::builder()
.check_expiration(false)
.build()
.unwrap();
let signer = HttpVerifier::builder().check_expiration(false).build();

signer
.verify(&parts, move |key_id| async move {
Expand Down
5 changes: 1 addition & 4 deletions crates/kitsune-http-signatures/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ async fn basic_signature() {
),
);
let public_key = self::data::get_public_key();
let signer = HttpVerifier::builder()
.enforce_expiration(None)
.build()
.unwrap();
let signer = HttpVerifier::builder().enforce_expiration(None).build();

signer
.verify(&parts, move |key_id| async move {
Expand Down
10 changes: 3 additions & 7 deletions crates/kitsune-http-signatures/tests/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ async fn sign_some() {
let private_key = PrivateKey::builder()
.key_id("Test")
.key(private_key)
.build()
.unwrap();
.build();

let mut parts = get_parts();
let signer = HttpSigner::builder().build().unwrap();
let signer = HttpSigner::default();
let (name, value) = signer
.sign(
&parts,
Expand All @@ -39,10 +38,7 @@ async fn sign_some() {

parts.headers.insert(name, value);

let verifier = HttpVerifier::builder()
.enforce_expiration(None)
.build()
.unwrap();
let verifier = HttpVerifier::builder().enforce_expiration(None).build();
verifier
.verify(&parts, |key_id| async move {
assert_eq!(key_id, "Test");
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-messaging/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition.workspace = true

[dependencies]
ahash = "0.8.3"
async-trait = "0.1.73"
async-trait = "0.1.74"
futures-util = "0.3.28"
kitsune-retry-policies = { path = "../kitsune-retry-policies" }
pin-project-lite = "0.2.13"
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-search/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ edition.workspace = true
version.workspace = true

[dependencies]
async-trait = "0.1.73"
async-trait = "0.1.74"
diesel = "2.1.3"
diesel-async = "0.4.1"
diesel_full_text_search = { version = "2.1.0", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion crates/kitsune-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version.workspace = true
edition.workspace = true

[dependencies]
async-trait = "0.1.73"
async-trait = "0.1.74"
bytes = "1.5.0"
enum_dispatch = "0.3.12"
futures-util = "0.3.28"
Expand Down
4 changes: 2 additions & 2 deletions kitsune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ askama = { version = "0.12.1", features = [
"with-axum",
], default-features = false }
askama_axum = "0.3.0"
async-trait = "0.1.73"
async-trait = "0.1.74"
axum = { version = "0.6.20", features = ["headers", "macros", "multipart"] }
axum-extra = { version = "0.8.0", features = [
"cookie",
Expand Down Expand Up @@ -76,7 +76,7 @@ tower-http = { version = "0.4.4", features = [
tracing = "0.1.39"
tracing-error = "0.2.0"
tracing-subscriber = "0.3.17"
typed-builder = "0.16.2"
typed-builder = "0.17.0"
url = "2.4.1"
utoipa = { version = "4.0.0", features = ["axum_extras", "uuid"] }
utoipa-swagger-ui = { version = "4.0.0", features = ["axum"] }
Expand Down
Loading
Loading