Add keys, http-sigs, webfinger

This commit is contained in:
asonix
2020-03-15 22:36:46 -05:00
parent d741e2b202
commit eea0577686
11 changed files with 561 additions and 80 deletions
+26
View File
@@ -5,6 +5,23 @@ use activitystreams::{
};
use std::collections::HashMap;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
pub id: XsdAnyUri,
pub owner: XsdAnyUri,
pub public_key_pem: String,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyExtension<T> {
public_key: PublicKey,
#[serde(flatten)]
extending: T,
}
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, PropRefs)]
#[serde(rename_all = "camelCase")]
#[prop_refs(Object)]
@@ -72,6 +89,15 @@ pub struct Endpoints {
shared_inbox: Option<XsdAnyUri>,
}
impl PublicKey {
pub fn extend<T>(self, extending: T) -> PublicKeyExtension<T> {
PublicKeyExtension {
public_key: self,
extending,
}
}
}
impl ValidObjects {
pub fn id(&self) -> &XsdAnyUri {
match self {
+77
View File
@@ -0,0 +1,77 @@
use activitystreams::primitives::XsdAnyUriError;
use actix_web::{error::ResponseError, http::StatusCode, HttpResponse};
use log::error;
use rsa_pem::KeyError;
use std::{convert::Infallible, io::Error};
#[derive(Debug, thiserror::Error)]
pub enum MyError {
#[error("Couldn't parse key, {0}")]
Key(#[from] KeyError),
#[error("Couldn't parse URI, {0}")]
Uri(#[from] XsdAnyUriError),
#[error("Couldn't perform IO, {0}")]
Io(#[from] Error),
#[error("Couldn't sign string")]
Rsa(rsa::errors::Error),
#[error("Couldn't do the json thing")]
Json(#[from] serde_json::Error),
#[error("Couldn't serialzize the signature header")]
HeaderSerialize(#[from] actix_web::http::header::ToStrError),
#[error("Couldn't parse the signature header")]
HeaderValidation(#[from] actix_web::http::header::InvalidHeaderValue),
#[error("Wrong ActivityPub kind")]
Kind,
#[error("Object has already been relayed")]
Duplicate,
#[error("Actor is blocked")]
Blocked,
#[error("Actor is not whitelisted")]
Whitelist,
#[error("Couldn't send request")]
SendRequest,
#[error("Couldn't receive request response")]
ReceiveResponse,
#[error("Response has invalid status code")]
Status,
#[error("URI is missing domain field")]
Domain,
}
impl ResponseError for MyError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse {
HttpResponse::InternalServerError()
.header("Content-Type", "application/activity+json")
.json(serde_json::json!({}))
}
}
impl From<Infallible> for MyError {
fn from(i: Infallible) -> Self {
match i {}
}
}
impl From<rsa::errors::Error> for MyError {
fn from(e: rsa::errors::Error) -> Self {
MyError::Rsa(e)
}
}
+48 -56
View File
@@ -4,20 +4,17 @@ use activitystreams::{
primitives::XsdAnyUri,
};
use actix::Addr;
use actix_web::{client::Client, http::StatusCode, web, HttpResponse};
use actix_web::{client::Client, web, HttpResponse};
use futures::join;
use log::error;
use crate::{
apub::{AcceptedActors, AcceptedObjects, ValidTypes},
db_actor::{DbActor, DbQuery, Pool},
error::MyError,
state::{State, UrlKind},
};
#[derive(Clone, Debug, thiserror::Error)]
#[error("Something went wrong :(")]
pub struct MyError;
pub async fn inbox(
db_actor: web::Data<Addr<DbActor>>,
state: web::Data<State>,
@@ -57,7 +54,7 @@ async fn handle_undo(
actor: AcceptedActors,
) -> Result<HttpResponse, MyError> {
if !input.object.is_kind("Follow") {
return Err(MyError);
return Err(MyError::Kind);
}
let inbox = actor.inbox().to_owned();
@@ -99,7 +96,7 @@ async fn handle_undo(
let undo2 = undo.clone();
let client = client.into_inner();
actix::Arbiter::spawn(async move {
let _ = deliver(&client, actor.id, &undo2).await;
let _ = deliver(&state.into_inner(), &client, actor.id, &undo2).await;
});
}
@@ -116,7 +113,7 @@ async fn handle_forward(
let inboxes = get_inboxes(&state, &actor, &object_id).await?;
deliver_many(client, inboxes, input.clone());
deliver_many(state, client, inboxes, input.clone());
Ok(response(input))
}
@@ -130,7 +127,7 @@ async fn handle_relay(
let object_id = input.object.id();
if state.is_cached(object_id).await {
return Err(MyError);
return Err(MyError::Duplicate);
}
let activity_id: XsdAnyUri = state.generate_url(UrlKind::Activity).parse()?;
@@ -149,10 +146,10 @@ async fn handle_relay(
let inboxes = get_inboxes(&state, &actor, &object_id).await?;
deliver_many(client, inboxes, announce.clone());
state.cache(object_id.to_owned(), activity_id).await;
deliver_many(state, client, inboxes, announce.clone());
Ok(response(announce))
}
@@ -171,12 +168,12 @@ async fn handle_follow(
if is_blocked {
error!("Follow from blocked listener, {}", actor.id);
return Err(MyError);
return Err(MyError::Blocked);
}
if !is_whitelisted {
error!("Follow from non-whitelisted listener, {}", actor.id);
return Err(MyError);
return Err(MyError::Whitelist);
}
if !is_listener {
@@ -220,7 +217,7 @@ async fn handle_follow(
let client = client.into_inner();
let accept2 = accept.clone();
actix::Arbiter::spawn(async move {
let _ = deliver(&client, actor_inbox, &accept2).await;
let _ = deliver(&state.into_inner(), &client, actor_inbox, &accept2).await;
});
Ok(response(accept))
@@ -242,13 +239,13 @@ async fn fetch_actor(
.await
.map_err(|e| {
error!("Couldn't send request to {} for actor, {}", actor_id, e);
MyError
MyError::SendRequest
})?
.json()
.await
.map_err(|e| {
error!("Coudn't fetch actor from {}, {}", actor_id, e);
MyError
MyError::ReceiveResponse
})?;
state.cache_actor(actor_id.to_owned(), actor.clone()).await;
@@ -256,20 +253,24 @@ async fn fetch_actor(
Ok(actor)
}
fn deliver_many<T>(client: web::Data<Client>, inboxes: Vec<XsdAnyUri>, item: T)
where
fn deliver_many<T>(
state: web::Data<State>,
client: web::Data<Client>,
inboxes: Vec<XsdAnyUri>,
item: T,
) where
T: serde::ser::Serialize + 'static,
{
let client = client.into_inner();
let state = state.into_inner();
actix::Arbiter::spawn(async move {
use futures::stream::StreamExt;
let client = client.clone();
let mut unordered = futures::stream::FuturesUnordered::new();
for inbox in inboxes {
unordered.push(deliver(&client, inbox, &item));
unordered.push(deliver(&state, &client, inbox, &item));
}
while let Some(_) = unordered.next().await {}
@@ -277,6 +278,7 @@ where
}
async fn deliver<T>(
state: &std::sync::Arc<State>,
client: &std::sync::Arc<Client>,
inbox: XsdAnyUri,
item: &T,
@@ -284,20 +286,38 @@ async fn deliver<T>(
where
T: serde::ser::Serialize,
{
use http_signature_normalization_actix::prelude::*;
use sha2::{Digest, Sha256};
let config = Config::default();
let mut digest = Sha256::new();
let key_id = state.generate_url(UrlKind::Actor);
let item_string = serde_json::to_string(item)?;
let res = client
.post(inbox.as_str())
.header("Accept", "application/activity+json")
.header("Content-Type", "application/activity+json")
.send_json(item)
.header("User-Agent", "Aode Relay v0.1.0")
.signature_with_digest(
&config,
&key_id,
&mut digest,
item_string,
|signing_string| state.sign(signing_string.as_bytes()),
)?
.send()
.await
.map_err(|e| {
error!("Couldn't send deliver request to {}, {}", inbox, e);
MyError
MyError::SendRequest
})?;
if !res.status().is_success() {
error!("Invalid response status from {}, {}", inbox, res.status());
return Err(MyError);
return Err(MyError::Status);
}
Ok(())
@@ -308,41 +328,13 @@ async fn get_inboxes(
actor: &AcceptedActors,
object_id: &XsdAnyUri,
) -> Result<Vec<XsdAnyUri>, MyError> {
let domain = object_id.as_url().host().ok_or(MyError)?.to_string();
let domain = object_id
.as_url()
.host()
.ok_or(MyError::Domain)?
.to_string();
let inbox = actor.inbox();
Ok(state.listeners_without(&inbox, &domain).await)
}
impl actix_web::error::ResponseError for MyError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
fn error_response(&self) -> HttpResponse {
HttpResponse::InternalServerError()
.header("Content-Type", "application/activity+json")
.json(serde_json::json!({}))
}
}
impl From<std::convert::Infallible> for MyError {
fn from(_: std::convert::Infallible) -> Self {
MyError
}
}
impl From<activitystreams::primitives::XsdAnyUriError> for MyError {
fn from(_: activitystreams::primitives::XsdAnyUriError) -> Self {
error!("Error parsing URI");
MyError
}
}
impl From<std::io::Error> for MyError {
fn from(e: std::io::Error) -> Self {
error!("JSON Error, {}", e);
MyError
}
}
+13 -2
View File
@@ -2,16 +2,20 @@
use activitystreams::{actor::apub::Application, context, endpoint::EndpointProperties};
use actix_web::{client::Client, middleware::Logger, web, App, HttpServer, Responder};
use bb8_postgres::tokio_postgres;
use rsa_pem::KeyExt;
mod apub;
mod db_actor;
mod error;
mod inbox;
mod label;
mod state;
mod webfinger;
use self::{
apub::PublicKey,
db_actor::DbActor,
inbox::MyError,
error::MyError,
label::ArbiterLabelFactory,
state::{State, UrlKind},
};
@@ -42,7 +46,13 @@ async fn actor_route(state: web::Data<State>) -> Result<impl Responder, MyError>
.set_inbox(state.generate_url(UrlKind::Inbox))?
.set_endpoints(endpoint)?;
Ok(inbox::response(application))
let public_key = PublicKey {
id: state.generate_url(UrlKind::MainKey).parse()?,
owner: state.generate_url(UrlKind::Actor).parse()?,
public_key_pem: state.settings.public_key.to_pem_pkcs8()?,
};
Ok(inbox::response(public_key.extend(application)))
}
#[actix_rt::main]
@@ -81,6 +91,7 @@ async fn main() -> Result<(), anyhow::Error> {
.service(web::resource("/").route(web::get().to(index)))
.service(web::resource("/inbox").route(web::post().to(inbox::inbox)))
.service(web::resource("/actor").route(web::get().to(actor_route)))
.service(actix_webfinger::resource::<_, webfinger::RelayResolver>())
})
.bind("127.0.0.1:8080")?
.run()
+11
View File
@@ -16,6 +16,16 @@ table! {
}
}
table! {
settings (id) {
id -> Uuid,
key -> Text,
value -> Text,
created_at -> Timestamp,
updated_at -> Nullable<Timestamp>,
}
}
table! {
whitelists (id) {
id -> Uuid,
@@ -28,5 +38,6 @@ table! {
allow_tables_to_appear_in_same_query!(
blocks,
listeners,
settings,
whitelists,
);
+104 -17
View File
@@ -2,7 +2,11 @@ use activitystreams::primitives::XsdAnyUri;
use anyhow::Error;
use bb8_postgres::tokio_postgres::{row::Row, Client};
use futures::try_join;
use log::{error, info};
use lru::LruCache;
use rand::thread_rng;
use rsa::{RSAPrivateKey, RSAPublicKey};
use rsa_pem::KeyExt;
use std::{collections::HashSet, sync::Arc};
use tokio::sync::RwLock;
use ttl_cache::TtlCache;
@@ -12,9 +16,7 @@ use crate::{apub::AcceptedActors, db_actor::Pool};
#[derive(Clone)]
pub struct State {
use_https: bool,
hostname: String,
whitelist_enabled: bool,
pub settings: Settings,
actor_cache: Arc<RwLock<TtlCache<XsdAnyUri, AcceptedActors>>>,
actor_id_cache: Arc<RwLock<LruCache<XsdAnyUri, XsdAnyUri>>>,
blocks: Arc<RwLock<HashSet<String>>>,
@@ -22,20 +24,73 @@ pub struct State {
listeners: Arc<RwLock<HashSet<XsdAnyUri>>>,
}
#[derive(Clone)]
pub struct Settings {
pub use_https: bool,
pub whitelist_enabled: bool,
pub hostname: String,
pub public_key: RSAPublicKey,
private_key: RSAPrivateKey,
}
pub enum UrlKind {
Activity,
Actor,
Followers,
Following,
Inbox,
MainKey,
}
#[derive(Clone, Debug, thiserror::Error)]
#[error("No host present in URI")]
pub struct HostError;
impl State {
pub fn generate_url(&self, kind: UrlKind) -> String {
#[derive(Clone, Debug, thiserror::Error)]
#[error("Error generating RSA key")]
pub struct RsaError;
impl Settings {
async fn hydrate(
client: &Client,
use_https: bool,
whitelist_enabled: bool,
hostname: String,
) -> Result<Self, Error> {
info!("SELECT value FROM settings WHERE key = 'private_key'");
let rows = client
.query("SELECT value FROM settings WHERE key = 'private_key'", &[])
.await?;
let private_key = if let Some(row) = rows.into_iter().next() {
let key_str: String = row.get(0);
KeyExt::from_pem_pkcs8(&key_str)?
} else {
info!("Generating new keys");
let mut rng = thread_rng();
let key = RSAPrivateKey::new(&mut rng, 4096).map_err(|e| {
error!("Error generating RSA key, {}", e);
RsaError
})?;
let pem_pkcs8 = key.to_pem_pkcs8()?;
info!("INSERT INTO settings (key, value, created_at) VALUES ('private_key', $1::TEXT, 'now');");
client.execute("INSERT INTO settings (key, value, created_at) VALUES ('private_key', $1::TEXT, 'now');", &[&pem_pkcs8]).await?;
key
};
let public_key = private_key.to_public_key();
Ok(Settings {
use_https,
whitelist_enabled,
hostname,
private_key,
public_key,
})
}
fn generate_url(&self, kind: UrlKind) -> String {
let scheme = if self.use_https { "https" } else { "http" };
match kind {
@@ -46,13 +101,40 @@ impl State {
UrlKind::Followers => format!("{}://{}/followers", scheme, self.hostname),
UrlKind::Following => format!("{}://{}/following", scheme, self.hostname),
UrlKind::Inbox => format!("{}://{}/inbox", scheme, self.hostname),
UrlKind::MainKey => format!("{}://{}/actor#main-key", scheme, self.hostname),
}
}
fn generate_resource(&self) -> String {
format!("relay@{}", self.hostname)
}
fn sign(&self, bytes: &[u8]) -> Result<String, crate::error::MyError> {
use rsa::{hash::Hashes, padding::PaddingScheme};
let bytes =
self.private_key
.sign(PaddingScheme::PKCS1v15, Some(&Hashes::SHA2_256), bytes)?;
Ok(base64::encode_config(bytes, base64::URL_SAFE))
}
}
impl State {
pub fn generate_url(&self, kind: UrlKind) -> String {
self.settings.generate_url(kind)
}
pub fn generate_resource(&self) -> String {
self.settings.generate_resource()
}
pub fn sign(&self, bytes: &[u8]) -> Result<String, crate::error::MyError> {
self.settings.sign(bytes)
}
pub async fn remove_listener(&self, client: &Client, inbox: &XsdAnyUri) -> Result<(), Error> {
let hs = self.listeners.clone();
log::info!("DELETE FROM listeners WHERE actor_id = {};", inbox.as_str());
info!("DELETE FROM listeners WHERE actor_id = {};", inbox.as_str());
client
.execute(
"DELETE FROM listeners WHERE actor_id = $1::TEXT;",
@@ -86,7 +168,7 @@ impl State {
}
pub async fn is_whitelisted(&self, actor_id: &XsdAnyUri) -> bool {
if !self.whitelist_enabled {
if !self.settings.whitelist_enabled {
return true;
}
@@ -155,7 +237,7 @@ impl State {
return Err(HostError.into());
};
log::info!(
info!(
"INSERT INTO blocks (domain_name, created_at) VALUES ($1::TEXT, 'now'); [{}]",
host.to_string()
);
@@ -181,7 +263,7 @@ impl State {
return Err(HostError.into());
};
log::info!(
info!(
"INSERT INTO whitelists (domain_name, created_at) VALUES ($1::TEXT, 'now'); [{}]",
host.to_string()
);
@@ -201,7 +283,7 @@ impl State {
pub async fn add_listener(&self, client: &Client, listener: XsdAnyUri) -> Result<(), Error> {
let listeners = self.listeners.clone();
log::info!(
info!(
"INSERT INTO listeners (actor_id, created_at) VALUES ($1::TEXT, 'now'); [{}]",
listener.as_str(),
);
@@ -226,6 +308,7 @@ impl State {
) -> Result<Self, Error> {
let pool1 = pool.clone();
let pool2 = pool.clone();
let pool3 = pool.clone();
let f1 = async move {
let conn = pool.get().await?;
@@ -245,12 +328,16 @@ impl State {
hydrate_listeners(&conn).await
};
let (blocks, whitelists, listeners) = try_join!(f1, f2, f3)?;
let f4 = async move {
let conn = pool3.get().await?;
Settings::hydrate(&conn, use_https, whitelist_enabled, hostname).await
};
let (blocks, whitelists, listeners, settings) = try_join!(f1, f2, f3, f4)?;
Ok(State {
use_https,
whitelist_enabled,
hostname,
settings,
actor_cache: Arc::new(RwLock::new(TtlCache::new(1024 * 8))),
actor_id_cache: Arc::new(RwLock::new(LruCache::new(1024 * 8))),
blocks: Arc::new(RwLock::new(blocks)),
@@ -261,14 +348,14 @@ impl State {
}
pub async fn hydrate_blocks(client: &Client) -> Result<HashSet<String>, Error> {
log::info!("SELECT domain_name FROM blocks");
info!("SELECT domain_name FROM blocks");
let rows = client.query("SELECT domain_name FROM blocks", &[]).await?;
parse_rows(rows)
}
pub async fn hydrate_whitelists(client: &Client) -> Result<HashSet<String>, Error> {
log::info!("SELECT domain_name FROM whitelists");
info!("SELECT domain_name FROM whitelists");
let rows = client
.query("SELECT domain_name FROM whitelists", &[])
.await?;
@@ -277,7 +364,7 @@ pub async fn hydrate_whitelists(client: &Client) -> Result<HashSet<String>, Erro
}
pub async fn hydrate_listeners(client: &Client) -> Result<HashSet<XsdAnyUri>, Error> {
log::info!("SELECT actor_id FROM listeners");
info!("SELECT actor_id FROM listeners");
let rows = client.query("SELECT actor_id FROM listeners", &[]).await?;
parse_rows(rows)
+52
View File
@@ -0,0 +1,52 @@
use crate::state::{State, UrlKind};
use activitystreams::context;
use actix_web::web::Data;
use actix_webfinger::{Link, Resolver, Webfinger};
use rsa_magic_public_key::AsMagicPublicKey;
use std::{future::Future, pin::Pin};
pub struct RelayResolver;
#[derive(Clone, Debug, thiserror::Error)]
#[error("Error resolving webfinger data")]
pub struct RelayError;
impl Resolver<Data<State>> for RelayResolver {
type Error = RelayError;
fn find(
account: &str,
domain: &str,
state: Data<State>,
) -> Pin<Box<dyn Future<Output = Result<Option<Webfinger>, Self::Error>>>> {
let domain = domain.to_owned();
let account = account.to_owned();
let fut = async move {
if domain != state.settings.hostname {
return Ok(None);
}
if account != "relay" {
return Ok(None);
}
let mut wf = Webfinger::new(&state.generate_resource());
wf.add_alias(&state.generate_url(UrlKind::Actor))
.add_activitypub(&state.generate_url(UrlKind::Actor))
.add_magic_public_key(&state.settings.public_key.as_magic_public_key())
.add_link(Link {
rel: "self".to_owned(),
href: Some(state.generate_url(UrlKind::Actor)),
template: None,
kind: Some(format!("application/ld+json; profile=\"{}\"", context())),
});
Ok(Some(wf))
};
Box::pin(fut)
}
}
impl actix_web::error::ResponseError for RelayError {}