Refactor server_keys service/interface and related callsites

Signed-off-by: Jason Volk <jason@zemos.net>
Signed-off-by: strawberry <strawberry@puppygock.gay>
This commit is contained in:
Jason Volk 2024-10-11 18:57:59 +00:00 committed by strawberry
parent d82ea331cf
commit c0939c3e9a
30 changed files with 1025 additions and 1378 deletions

View file

@ -490,30 +490,6 @@ pub struct Config {
#[serde(default = "default_trusted_servers")]
pub trusted_servers: Vec<OwnedServerName>,
/// Option to control whether conduwuit will query your list of trusted
/// notary key servers (`trusted_servers`) for remote homeserver signing
/// keys it doesn't know *first*, or query the individual servers first
/// before falling back to the trusted key servers.
///
/// The former/default behaviour makes federated/remote rooms joins
/// generally faster because we're querying a single (or list of) server
/// that we know works, is reasonably fast, and is reliable for just about
/// all the homeserver signing keys in the room. Querying individual
/// servers may take longer depending on the general infrastructure of
/// everyone in there, how many dead servers there are, etc.
///
/// However, this does create an increased reliance on one single or
/// multiple large entities as `trusted_servers` should generally
/// contain long-term and large servers who know a very large number of
/// homeservers.
///
/// If you don't know what any of this means, leave this and
/// `trusted_servers` alone to their defaults.
///
/// Defaults to true as this is the fastest option for federation.
#[serde(default = "true_fn")]
pub query_trusted_key_servers_first: bool,
/// max log level for conduwuit. allows debug, info, warn, or error
/// see also: https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives
/// **Caveat**:
@ -1518,10 +1494,6 @@ impl fmt::Display for Config {
.map(|server| server.host())
.join(", "),
);
line(
"Query Trusted Key Servers First",
&self.query_trusted_key_servers_first.to_string(),
);
line("OpenID Token TTL", &self.openid_token_ttl.to_string());
line(
"TURN username",

View file

@ -85,6 +85,8 @@ pub enum Error {
BadRequest(ruma::api::client::error::ErrorKind, &'static str), //TODO: remove
#[error("{0}")]
BadServerResponse(Cow<'static, str>),
#[error(transparent)]
CanonicalJson(#[from] ruma::CanonicalJsonError),
#[error("There was a problem with the '{0}' directive in your configuration: {1}")]
Config(&'static str, Cow<'static, str>),
#[error("{0}")]
@ -110,6 +112,8 @@ pub enum Error {
#[error(transparent)]
Ruma(#[from] ruma::api::client::error::Error),
#[error(transparent)]
Signatures(#[from] ruma::signatures::Error),
#[error(transparent)]
StateRes(#[from] ruma::state_res::Error),
#[error("uiaa")]
Uiaa(ruma::api::client::uiaa::UiaaInfo),

View file

@ -408,10 +408,13 @@ impl PduEvent {
serde_json::from_value(json).expect("Raw::from_value always works")
}
pub fn from_id_val(event_id: &EventId, mut json: CanonicalJsonObject) -> Result<Self, serde_json::Error> {
json.insert("event_id".to_owned(), CanonicalJsonValue::String(event_id.as_str().to_owned()));
pub fn from_id_val(event_id: &EventId, mut json: CanonicalJsonObject) -> Result<Self> {
json.insert("event_id".into(), CanonicalJsonValue::String(event_id.into()));
serde_json::from_value(serde_json::to_value(json).expect("valid JSON"))
let value = serde_json::to_value(json)?;
let pdu = serde_json::from_value(value)?;
Ok(pdu)
}
}
@ -462,13 +465,15 @@ pub fn gen_event_id_canonical_json(
let value: CanonicalJsonObject = serde_json::from_str(pdu.get())
.map_err(|e| err!(BadServerResponse(warn!("Error parsing incoming event: {e:?}"))))?;
let event_id = format!(
"${}",
// Anything higher than version3 behaves the same
ruma::signatures::reference_hash(&value, room_version_id).expect("ruma can calculate reference hashes")
)
.try_into()
.expect("ruma's reference hashes are valid event ids");
let event_id = gen_event_id(&value, room_version_id)?;
Ok((event_id, value))
}
/// Generates a correct eventId for the incoming pdu.
pub fn gen_event_id(value: &CanonicalJsonObject, room_version_id: &RoomVersionId) -> Result<OwnedEventId> {
let reference_hash = ruma::signatures::reference_hash(value, room_version_id)?;
let event_id: OwnedEventId = format!("${reference_hash}").try_into()?;
Ok(event_id)
}