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

A first attempt at using deser-hjson. Fixes #1270 #1433

Merged
merged 11 commits into from
Mar 1, 2021
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
243 changes: 107 additions & 136 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions ansible/templates/config.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
# https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist
#
# comma separated list of instances with which federation is allowed
# allowed_instances: ""
# Only one of these blocks should be uncommented
# allowed_instances: ["instance1.tld","instance2.tld"]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hjson needs quoted strings if its in a list.

# comma separated list of instances which are blocked from federating
# blocked_instances: ""
# blocked_instances: []
}
}
45 changes: 13 additions & 32 deletions api_tests/prepare-drone-federation-test.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
#!/bin/bash
set -e

export LEMMY_JWT_SECRET=changeme
export LEMMY_FEDERATION__ENABLED=true
export LEMMY_TLS_ENABLED=false
export LEMMY_SETUP__ADMIN_PASSWORD=lemmy
export LEMMY_RATE_LIMIT__POST=99999
export LEMMY_RATE_LIMIT__REGISTER=99999
export LEMMY_CAPTCHA__ENABLED=false
export LEMMY_TEST_SEND_SYNC=1
export RUST_BACKTRACE=1

Expand Down Expand Up @@ -35,52 +28,40 @@ fi

killall lemmy_server || true

echo "$PWD"

echo "start alpha"
LEMMY_HOSTNAME=lemmy-alpha:8541 \
LEMMY_PORT=8541 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha \
LEMMY_SETUP__SITE_NAME=lemmy-alpha \
target/lemmy_server >/dev/null 2>&1 &
LEMMY_HOSTNAME="lemmy-alpha:8541" \
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added an env var for hostname here, removed it from the config file

target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 &

echo "start beta"
LEMMY_HOSTNAME=lemmy-beta:8551 \
LEMMY_PORT=8551 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_beta.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_beta" \
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_beta \
LEMMY_SETUP__SITE_NAME=lemmy-beta \
target/lemmy_server >/dev/null 2>&1 &
target/lemmy_server >/tmp/lemmy_beta.out 2>&1 &

echo "start gamma"
LEMMY_HOSTNAME=lemmy-gamma:8561 \
LEMMY_PORT=8561 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_gamma.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_gamma" \
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_gamma \
LEMMY_SETUP__SITE_NAME=lemmy-gamma \
target/lemmy_server >/dev/null 2>&1 &
target/lemmy_server >/tmp/lemmy_gamma.out 2>&1 &

echo "start delta"
# An instance with only an allowlist for beta
LEMMY_HOSTNAME=lemmy-delta:8571 \
LEMMY_PORT=8571 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_delta.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_delta" \
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_delta \
LEMMY_SETUP__SITE_NAME=lemmy-delta \
target/lemmy_server >/dev/null 2>&1 &
target/lemmy_server >/tmp/lemmy_delta.out 2>&1 &

echo "start epsilon"
# An instance who has a blocklist, with lemmy-alpha blocked
LEMMY_HOSTNAME=lemmy-epsilon:8581 \
LEMMY_PORT=8581 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_epsilon" \
LEMMY_FEDERATION__BLOCKED_INSTANCES=lemmy-alpha \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_epsilon \
LEMMY_SETUP__SITE_NAME=lemmy-epsilon \
target/lemmy_server >/dev/null 2>&1 &
target/lemmy_server >/tmp/lemmy_epsilon.out 2>&1 &

echo "wait for all instances to start"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'localhost:8541/api/v2/site')" != "200" ]]; do sleep 1; done
Expand Down
5 changes: 3 additions & 2 deletions config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,10 @@
# https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist
#
# comma separated list of instances with which federation is allowed
allowed_instances: ""
# Only one of these blocks should be uncommented
# allowed_instances: ["instance1.tld","instance2.tld"]
# comma separated list of instances which are blocked from federating
blocked_instances: ""
# blocked_instances: []
}
captcha: {
enabled: true
Expand Down
19 changes: 15 additions & 4 deletions crates/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ use lemmy_db_views_actor::{
community_user_ban_view::CommunityUserBanView,
community_view::CommunityView,
};
use lemmy_utils::{claims::Claims, settings::Settings, ApiError, ConnectionId, LemmyError};
use lemmy_utils::{
claims::Claims,
settings::structs::Settings,
ApiError,
ConnectionId,
LemmyError,
};
use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
use serde::Deserialize;
use std::process::Command;
Expand Down Expand Up @@ -192,7 +198,7 @@ pub(crate) fn check_optional_url(item: &Option<Option<String>>) -> Result<(), Le
pub(crate) async fn build_federated_instances(
pool: &DbPool,
) -> Result<Option<FederatedInstances>, LemmyError> {
if Settings::get().federation.enabled {
if Settings::get().federation().enabled {
let distinct_communities = blocking(pool, move |conn| {
Community::distinct_federated_communities(conn)
})
Expand All @@ -206,8 +212,13 @@ pub(crate) async fn build_federated_instances(
.map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
.collect::<Result<Vec<String>, LemmyError>>()?;

linked.extend_from_slice(&allowed);
linked.retain(|a| !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname));
if let Some(allowed) = allowed.as_ref() {
linked.extend_from_slice(allowed);
}

if let Some(blocked) = blocked.as_ref() {
linked.retain(|a| !blocked.contains(a) && !a.eq(&Settings::get().hostname()));
}

// Sort and remove dupes
linked.sort_unstable();
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use lemmy_db_views_moderator::{
};
use lemmy_utils::{
location_info,
settings::Settings,
settings::structs::Settings,
utils::{check_slurs, check_slurs_opt},
version,
ApiError,
Expand Down Expand Up @@ -245,7 +245,7 @@ impl Perform for GetSite {
Ok(site_view) => Some(site_view),
// If the site isn't created yet, check the setup
Err(_) => {
if let Some(setup) = Settings::get().setup.as_ref() {
if let Some(setup) = Settings::get().setup().as_ref() {
let register = Register {
username: setup.admin_username.to_owned(),
email: setup.admin_email.to_owned(),
Expand Down
14 changes: 7 additions & 7 deletions crates/api/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use lemmy_utils::{
claims::Claims,
email::send_email,
location_info,
settings::Settings,
settings::structs::Settings,
utils::{
check_slurs,
generate_random_string,
Expand Down Expand Up @@ -121,7 +121,7 @@ impl Perform for Login {

// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(user.id, Settings::get().hostname)?,
jwt: Claims::jwt(user.id, Settings::get().hostname())?,
})
}
}
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Perform for Register {
.await??;

// If its not the admin, check the captcha
if !no_admins && Settings::get().captcha.enabled {
if !no_admins && Settings::get().captcha().enabled {
let check = context
.chat_server()
.send(CheckCaptcha {
Expand Down Expand Up @@ -302,7 +302,7 @@ impl Perform for Register {

// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(inserted_user.id, Settings::get().hostname)?,
jwt: Claims::jwt(inserted_user.id, Settings::get().hostname())?,
})
}
}
Expand All @@ -316,7 +316,7 @@ impl Perform for GetCaptcha {
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<Self::Response, LemmyError> {
let captcha_settings = Settings::get().captcha;
let captcha_settings = Settings::get().captcha();

if !captcha_settings.enabled {
return Ok(GetCaptchaResponse { ok: None });
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Perform for SaveUserSettings {

// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?,
jwt: Claims::jwt(updated_user.id, Settings::get().hostname())?,
})
}
}
Expand Down Expand Up @@ -1011,7 +1011,7 @@ impl Perform for PasswordChange {

// Return the jwt
Ok(LoginResponse {
jwt: Claims::jwt(updated_user.id, Settings::get().hostname)?,
jwt: Claims::jwt(updated_user.id, Settings::get().hostname())?,
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/api_structs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lemmy_db_schema::source::{
user::User_,
user_mention::{UserMention, UserMentionForm},
};
use lemmy_utils::{email::send_email, settings::Settings, utils::MentionData, LemmyError};
use lemmy_utils::{email::send_email, settings::structs::Settings, utils::MentionData, LemmyError};
use log::error;
use serde::{Deserialize, Serialize};
use url::Url;
Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn send_email_to_user(user: User_, subject_text: &str, body_text: &str, comm
let subject = &format!(
"{} - {} {}",
subject_text,
Settings::get().hostname,
Settings::get().hostname(),
user.name,
);
let html = &format!(
Expand Down
4 changes: 2 additions & 2 deletions crates/api_structs/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ pub struct SaveSiteConfig {
#[derive(Serialize)]
pub struct FederatedInstances {
pub linked: Vec<String>,
pub allowed: Vec<String>,
pub blocked: Vec<String>,
pub allowed: Option<Vec<String>>,
pub blocked: Option<Vec<String>>,
}
2 changes: 1 addition & 1 deletion crates/apub/src/activities/send/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post, user::User_};
use lemmy_utils::{
request::{retry, RecvError},
settings::Settings,
settings::structs::Settings,
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/activities/send/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use lemmy_utils::settings::Settings;
use lemmy_utils::settings::structs::Settings;
use url::{ParseError, Url};
use uuid::Uuid;

Expand Down
6 changes: 3 additions & 3 deletions crates/apub/src/activity_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use background_jobs::{
use itertools::Itertools;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::source::{community::Community, user::User_};
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use log::{debug, warn};
use reqwest::Client;
Expand Down Expand Up @@ -88,7 +88,7 @@ where
.await?
.iter()
.unique()
.filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname))
.filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname()))
.filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
.map(|inbox| inbox.to_owned())
.collect();
Expand Down Expand Up @@ -215,7 +215,7 @@ where
Kind: Serialize,
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
{
if !Settings::get().federation.enabled || inboxes.is_empty() {
if !Settings::get().federation().enabled || inboxes.is_empty() {
return Ok(());
}

Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/fetcher/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use lemmy_db_schema::source::{
};
use lemmy_db_views::{comment_view::CommentView, post_view::PostView};
use lemmy_db_views_actor::{community_view::CommunityView, user_view::UserViewSafe};
use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_utils::{settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url;
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/http/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use http::StatusCode;
use lemmy_api_structs::blocking;
use lemmy_db_queries::source::activity::Activity_;
use lemmy_db_schema::source::activity::Activity;
use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_utils::{settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/inbox/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use lemmy_db_queries::{
DbPool,
};
use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_};
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::Serialize;
use std::fmt::Debug;
Expand Down Expand Up @@ -167,7 +167,7 @@ where
let id = activity.id_unchecked().context(location_info!())?;
let activity_domain = id.domain().context(location_info!())?;

if activity_domain == Settings::get().hostname {
if activity_domain == Settings::get().hostname() {
return Err(
anyhow!(
"Error: received activity which was sent by local instance: {:?}",
Expand Down
19 changes: 10 additions & 9 deletions crates/apub/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use lemmy_db_schema::source::{
private_message::PrivateMessage,
user::User_,
};
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::Serialize;
use std::net::IpAddr;
Expand Down Expand Up @@ -64,7 +64,7 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
let domain = apub_id.domain().context(location_info!())?.to_string();
let local_instance = settings.get_hostname_without_port()?;

if !settings.federation.enabled {
if !settings.federation().enabled {
return if domain == local_instance {
Ok(())
} else {
Expand All @@ -88,22 +88,23 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
return Err(anyhow!("invalid apub id scheme {}: {}", apub_id.scheme(), apub_id).into());
}

let mut allowed_instances = Settings::get().get_allowed_instances();
let allowed_instances = Settings::get().get_allowed_instances();
let blocked_instances = Settings::get().get_blocked_instances();
if allowed_instances.is_empty() && blocked_instances.is_empty() {

if allowed_instances.is_none() && blocked_instances.is_none() {
Ok(())
} else if !allowed_instances.is_empty() {
} else if let Some(mut allowed) = allowed_instances {
// need to allow this explicitly because apub receive might contain objects from our local
// instance. split is needed to remove the port in our federation test setup.
allowed_instances.push(local_instance);
allowed.push(local_instance);

if allowed_instances.contains(&domain) {
if allowed.contains(&domain) {
Ok(())
} else {
Err(anyhow!("{} not in federation allowlist", domain).into())
}
} else if !blocked_instances.is_empty() {
if blocked_instances.contains(&domain) {
} else if let Some(blocked) = blocked_instances {
if blocked.contains(&domain) {
Err(anyhow!("{} is in federation blocklist", domain).into())
} else {
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lemmy_db_queries::{ApubObject, Crud, DbPool};
use lemmy_db_schema::source::community::Community;
use lemmy_utils::{
location_info,
settings::Settings,
settings::structs::Settings,
utils::{convert_datetime, markdown_to_html},
LemmyError,
};
Expand Down Expand Up @@ -187,7 +187,7 @@ where
let domain = object_id.domain().context(location_info!())?;

// if its a local object, return it directly from the database
if Settings::get().hostname == domain {
if Settings::get().hostname() == domain {
let object = blocking(context.pool(), move |conn| {
To::read_from_apub_id(conn, &object_id.into())
})
Expand Down
Loading