Add integration tests, do not show secret in logs

The code is restructured so that the library contains the actix-web
code and the binary only does commandline parsing and running of
the lib.
This commit is contained in:
2021-04-18 09:41:17 +02:00
parent 6fd36936a3
commit ce315c429c
8 changed files with 813 additions and 259 deletions
+5 -2
View File
@@ -147,6 +147,7 @@ async fn parse_args_to_config(config: ArgMatches<'_>) -> ServerConfig {
} else {
secret
};
let secret = pslink::Secret::new(secret);
let db = config
.value_of("database")
.expect(concat!(
@@ -219,9 +220,12 @@ pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError>
))
.parse::<PathBuf>()
.expect("Failed to parse Database path.");
if !db.exists() {
trace!("No database file found {}", db.display());
if config.subcommand_matches("migrate-database").is_none() {
if !(config.subcommand_matches("migrate-database").is_none()
| config.subcommand_matches("generate-env").is_none())
{
let msg = format!(
concat!(
"Database not found at {}!",
@@ -239,7 +243,6 @@ pub(crate) async fn setup() -> Result<Option<crate::ServerConfig>, ServerError>
// create an empty database file. The if above makes sure that this file does not exist.
File::create(db)?;
};
let server_config: crate::ServerConfig = parse_args_to_config(config.clone()).await;
if let Some(_migrate_config) = config.subcommand_matches("generate-env") {
+13 -183
View File
@@ -1,20 +1,11 @@
extern crate sqlx;
mod cli;
mod views;
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::{web, App, HttpServer};
use anyhow::{Context, Result};
use fluent_templates::{static_loader, FluentLoader};
use tera::Tera;
use pslink::{ServerConfig, ServerError};
use pslink::ServerConfig;
use tracing::instrument;
use tracing::{error, info, trace};
use tracing::{subscriber::set_global_default, Subscriber};
use tracing_actix_web::TracingLogger;
use tracing_opentelemetry::OpenTelemetryLayer;
use tracing_subscriber::{layer::SubscriberExt, EnvFilter, Registry};
@@ -48,185 +39,24 @@ pub fn init_subscriber(subscriber: impl Subscriber + Send + Sync) {
set_global_default(subscriber).expect("Failed to set subscriber");
}
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
static_loader! {
static LOCALES = {
locales: "./locales",
fallback_language: "en",
};
}
#[instrument]
fn build_tera() -> Result<Tera> {
let mut tera = Tera::default();
// Add translation support
tera.register_function("fluent", FluentLoader::new(&*LOCALES));
tera.add_raw_templates(vec![
("admin.html", include_str!("../../../templates/admin.html")),
("base.html", include_str!("../../../templates/base.html")),
(
"edit_link.html",
include_str!("../../../templates/edit_link.html"),
),
(
"edit_profile.html",
include_str!("../../../templates/edit_profile.html"),
),
(
"index_users.html",
include_str!("../../../templates/index_users.html"),
),
("index.html", include_str!("../../../templates/index.html")),
("login.html", include_str!("../../../templates/login.html")),
(
"not_found.html",
include_str!("../../../templates/not_found.html"),
),
(
"signup.html",
include_str!("../../../templates/signup.html"),
),
(
"submission.html",
include_str!("../../../templates/submission.html"),
),
(
"view_link.html",
include_str!("../../../templates/view_link.html"),
),
(
"view_profile.html",
include_str!("../../../templates/view_profile.html"),
),
])
.context("Failed to load Templates")?;
Ok(tera)
}
#[allow(clippy::future_not_send, clippy::too_many_lines)]
async fn webservice(server_config: ServerConfig) -> Result<()> {
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
info!(
"Running on: {}://{}/admin/login/",
&server_config.protocol, host_port
);
info!(
"If the public url is set up correctly it should be accessible via: {}://{}/admin/login/",
&server_config.protocol, &server_config.public_url
);
let tera = build_tera()?;
trace!("The tera templates are ready");
HttpServer::new(move || {
let generated = generate();
App::new()
.data(server_config.clone())
.wrap(TracingLogger)
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32])
.name("auth-cookie")
.secure(false),
))
.data(tera.clone())
.service(actix_web_static_files::ResourceFiles::new(
"/static", generated,
))
// directly go to the main page set the target with the environment variable.
.route("/", web::get().to(views::redirect_empty))
// admin block
.service(
web::scope("/admin")
// list all links
.route("/index/", web::get().to(views::index))
// invite users
.route("/signup/", web::get().to(views::signup))
.route("/signup/", web::post().to(views::process_signup))
// logout
.route("/logout/", web::to(views::logout))
// submit a new url for shortening
.route("/submit/", web::get().to(views::create_link))
.route("/submit/", web::post().to(views::process_link_creation))
// view an existing url
.service(
web::scope("/view")
.service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::view_link))
.route("/", web::get().to(views::view_link_empty)),
)
.service(
web::scope("/profile")
.route("/{user_id}", web::get().to(views::view_profile)),
)
.route("/users/", web::get().to(views::index_users)),
)
.service(
web::scope("/edit")
.service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::edit_link))
.route(
"/{redirect_id}",
web::post().to(views::process_link_edit),
),
)
.service(
web::scope("/profile")
.route("/{user_id}", web::get().to(views::edit_profile))
.route(
"/{user_id}",
web::post().to(views::process_edit_profile),
),
)
.route("/set_admin/{user_id}", web::get().to(views::toggle_admin))
.route(
"/set_language/{language}",
web::get().to(views::set_language),
),
)
.service(
web::scope("/delete").service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::process_link_delete)),
),
)
.service(
web::scope("/download")
.route("/png/{redirect_id}", web::get().to(views::download_png)),
)
// login to the admin area
.route("/login/", web::get().to(views::login))
.route("/login/", web::post().to(views::process_login)),
)
// redirect to the url hidden behind the code
.route("/{redirect_id}", web::get().to(views::redirect))
})
.bind(host_port)
.context("Failed to bind to port")
.map_err(|e| {
error!("Failed to bind to port!");
e
})?
.run()
.await
.context("Failed to run the webservice")
}
#[instrument]
#[actix_web::main]
async fn main() -> std::result::Result<(), ServerError> {
async fn main() -> std::result::Result<(), std::io::Error> {
let subscriber = get_subscriber("fhs.li", "info");
init_subscriber(subscriber);
match cli::setup().await {
Ok(Some(server_config)) => webservice(server_config).await.map_err(|e| {
println!("{:?}", e);
std::thread::sleep(std::time::Duration::from_millis(100));
std::process::exit(0);
}),
Ok(Some(server_config)) => {
pslink::webservice(server_config)
.await
.map_err(|e| {
println!("{:?}", e);
std::thread::sleep(std::time::Duration::from_millis(100));
std::process::exit(0);
})
.expect("Failed to launch the service")
.await
}
Ok(None) => {
std::thread::sleep(std::time::Duration::from_millis(100));
std::process::exit(0);
+204 -4
View File
@@ -3,14 +3,21 @@ extern crate sqlx;
pub mod forms;
pub mod models;
pub mod queries;
mod views;
use std::{fmt::Display, path::PathBuf, str::FromStr};
use actix_identity::{CookieIdentityPolicy, IdentityService};
use actix_web::HttpResponse;
use actix_web::{web, App, HttpServer};
use fluent_templates::{static_loader, FluentLoader};
use qrcode::types::QrError;
use sqlx::{Pool, Sqlite};
use std::{fmt::Display, path::PathBuf, str::FromStr};
use tera::Tera;
use thiserror::Error;
use tracing::instrument;
use tracing::{error, info, trace};
use tracing_actix_web::TracingLogger;
#[derive(Error, Debug)]
pub enum ServerError {
#[error("Failed to encrypt the password {0} - aborting!")]
@@ -148,9 +155,33 @@ impl FromStr for Protocol {
}
}
#[derive(Clone)]
pub struct Secret {
secret: String,
}
impl Secret {
#[must_use]
pub const fn new(secret: String) -> Self {
Self { secret }
}
}
impl std::fmt::Debug for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("*****SECRET*****")
}
}
impl std::fmt::Display for Secret {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("*****SECRET*****")
}
}
#[derive(Debug, Clone)]
pub struct ServerConfig {
pub secret: String,
pub secret: Secret,
pub db: PathBuf,
pub db_pool: Pool<Sqlite>,
pub public_url: String,
@@ -181,3 +212,172 @@ impl ServerConfig {
]
}
}
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
static_loader! {
static LOCALES = {
locales: "./locales",
fallback_language: "en",
};
}
#[instrument]
fn build_tera() -> Result<Tera, ServerError> {
let mut tera = Tera::default();
// Add translation support
tera.register_function("fluent", FluentLoader::new(&*LOCALES));
tera.add_raw_templates(vec![
("admin.html", include_str!("../templates/admin.html")),
("base.html", include_str!("../templates/base.html")),
(
"edit_link.html",
include_str!("../templates/edit_link.html"),
),
(
"edit_profile.html",
include_str!("../templates/edit_profile.html"),
),
(
"index_users.html",
include_str!("../templates/index_users.html"),
),
("index.html", include_str!("../templates/index.html")),
("login.html", include_str!("../templates/login.html")),
(
"not_found.html",
include_str!("../templates/not_found.html"),
),
("signup.html", include_str!("../templates/signup.html")),
(
"submission.html",
include_str!("../templates/submission.html"),
),
(
"view_link.html",
include_str!("../templates/view_link.html"),
),
(
"view_profile.html",
include_str!("../templates/view_profile.html"),
),
])?;
Ok(tera)
}
/// Launch the pslink-webservice
///
/// # Errors
/// This produces a [`ServerError`] if:
/// * Tera failed to build its templates
/// * The server failed to bind to the designated port.
#[allow(clippy::future_not_send, clippy::too_many_lines)]
pub async fn webservice(
server_config: ServerConfig,
) -> Result<actix_web::dev::Server, ServerError> {
let host_port = format!("{}:{}", &server_config.internal_ip, &server_config.port);
info!(
"Running on: {}://{}/admin/login/",
&server_config.protocol, host_port
);
info!(
"If the public url is set up correctly it should be accessible via: {}://{}/admin/login/",
&server_config.protocol, &server_config.public_url
);
let tera = build_tera()?;
trace!("The tera templates are ready");
let server = HttpServer::new(move || {
let generated = generate();
App::new()
.data(server_config.clone())
.wrap(TracingLogger)
.wrap(IdentityService::new(
CookieIdentityPolicy::new(&[0; 32])
.name("auth-cookie")
.secure(false),
))
.data(tera.clone())
.service(actix_web_static_files::ResourceFiles::new(
"/static", generated,
))
// directly go to the main page set the target with the environment variable.
.route("/", web::get().to(views::redirect_empty))
// admin block
.service(
web::scope("/admin")
// list all links
.route("/index/", web::get().to(views::index))
// invite users
.route("/signup/", web::get().to(views::signup))
.route("/signup/", web::post().to(views::process_signup))
// logout
.route("/logout/", web::to(views::logout))
// submit a new url for shortening
.route("/submit/", web::get().to(views::create_link))
.route("/submit/", web::post().to(views::process_link_creation))
// view an existing url
.service(
web::scope("/view")
.service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::view_link))
.route("/", web::get().to(views::view_link_empty)),
)
.service(
web::scope("/profile")
.route("/{user_id}", web::get().to(views::view_profile)),
)
.route("/users/", web::get().to(views::index_users)),
)
.service(
web::scope("/edit")
.service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::edit_link))
.route(
"/{redirect_id}",
web::post().to(views::process_link_edit),
),
)
.service(
web::scope("/profile")
.route("/{user_id}", web::get().to(views::edit_profile))
.route(
"/{user_id}",
web::post().to(views::process_edit_profile),
),
)
.route("/set_admin/{user_id}", web::get().to(views::toggle_admin))
.route(
"/set_language/{language}",
web::get().to(views::set_language),
),
)
.service(
web::scope("/delete").service(
web::scope("/link")
.route("/{redirect_id}", web::get().to(views::process_link_delete)),
),
)
.service(
web::scope("/download")
.route("/png/{redirect_id}", web::get().to(views::download_png)),
)
// login to the admin area
.route("/login/", web::get().to(views::login))
.route("/login/", web::post().to(views::process_login)),
)
// redirect to the url hidden behind the code
.route("/{redirect_id}", web::get().to(views::redirect))
})
.bind(host_port)
.map_err(|e| {
error!("Failed to bind to port!");
e
})?
.run();
Ok(server)
}
+1 -1
View File
@@ -146,7 +146,7 @@ impl NewUser {
let hash = Hasher::default()
.with_password(password)
.with_secret_key(secret)
.with_secret_key(&secret.secret)
.hash()?;
Ok(hash)
+27 -27
View File
@@ -17,10 +17,10 @@ use queries::{authenticate, Role};
use tera::{Context, Tera};
use tracing::{info, instrument, trace, warn};
use pslink::forms::LinkForm;
use pslink::models::{LoginUser, NewUser};
use pslink::queries;
use pslink::ServerError;
use crate::forms::LinkForm;
use crate::models::{LoginUser, NewUser};
use crate::queries;
use crate::ServerError;
#[instrument]
fn redirect_builder(target: &str) -> HttpResponse {
@@ -69,7 +69,7 @@ fn detect_language(request: &HttpRequest) -> Result<String, ServerError> {
#[instrument(skip(id, tera))]
pub async fn index(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
if let Ok(links) = queries::list_all_allowed(&id, &config).await {
@@ -88,7 +88,7 @@ pub async fn index(
#[instrument(skip(id, tera))]
pub async fn index_users(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
if let Ok(users) = queries::list_users(&id, &config).await {
@@ -107,7 +107,7 @@ pub async fn index_users(
#[instrument(skip(id, tera))]
pub async fn view_link_empty(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
view_link(tera, config, id, web::Path::from("".to_owned())).await
@@ -116,7 +116,7 @@ pub async fn view_link_empty(
#[instrument(skip(id, tera))]
pub async fn view_link(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
link_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -156,7 +156,7 @@ pub async fn view_link(
#[instrument(skip(id, tera))]
pub async fn view_profile(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
user_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -184,7 +184,7 @@ pub async fn view_profile(
#[instrument(skip(id, tera))]
pub async fn edit_profile(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
user_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -211,7 +211,7 @@ pub async fn edit_profile(
#[instrument(skip(id))]
pub async fn process_edit_profile(
data: web::Form<NewUser>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
user_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -225,7 +225,7 @@ pub async fn process_edit_profile(
#[instrument(skip(id))]
pub async fn download_png(
id: Identity,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
link_code: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
match queries::get_link(&id, &link_code.0, &config).await {
@@ -250,7 +250,7 @@ pub async fn download_png(
#[instrument(skip(id, tera))]
pub async fn signup(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
match queries::authenticate(&id, &config).await? {
@@ -271,7 +271,7 @@ pub async fn signup(
#[instrument(skip(id))]
pub async fn process_signup(
data: web::Form<NewUser>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
info!("Creating a User: {:?}", &data);
@@ -286,7 +286,7 @@ pub async fn process_signup(
#[instrument(skip(id))]
pub async fn toggle_admin(
data: web::Path<String>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
let update = queries::toggle_admin(&id, &data.0, &config).await?;
@@ -299,18 +299,18 @@ pub async fn toggle_admin(
#[instrument(skip(id))]
pub async fn set_language(
data: web::Path<String>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
queries::set_language(&id, &data.0, &config).await?;
Ok(redirect_builder("/admin/index/"))
}
#[instrument(skip(id))]
#[instrument(skip(tera, id))]
pub async fn login(
tera: web::Data<Tera>,
id: Identity,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
req: HttpRequest,
) -> Result<HttpResponse, ServerError> {
let language_code = detect_language(&req)?;
@@ -343,7 +343,7 @@ pub async fn login(
#[instrument(skip(id))]
pub async fn process_login(
data: web::Form<LoginUser>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
let user = queries::get_user_by_name(&data.username, &config).await;
@@ -354,7 +354,7 @@ pub async fn process_login(
let valid = Verifier::default()
.with_hash(&u.password)
.with_password(&data.password)
.with_secret_key(secret)
.with_secret_key(&secret.secret)
.verify()?;
if valid {
@@ -383,7 +383,7 @@ pub async fn logout(id: Identity) -> Result<HttpResponse, ServerError> {
#[instrument]
pub async fn redirect(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
data: web::Path<String>,
req: HttpRequest,
) -> Result<HttpResponse, ServerError> {
@@ -413,7 +413,7 @@ pub async fn redirect(
#[instrument]
pub async fn redirect_empty(
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
) -> Result<HttpResponse, ServerError> {
Ok(redirect_builder(&config.empty_forward_url))
}
@@ -421,7 +421,7 @@ pub async fn redirect_empty(
#[instrument(skip(id))]
pub async fn create_link(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
match queries::authenticate(&id, &config).await? {
@@ -442,7 +442,7 @@ pub async fn create_link(
#[instrument(skip(id))]
pub async fn process_link_creation(
data: web::Form<LinkForm>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
) -> Result<HttpResponse, ServerError> {
let new_link = queries::create_link(&id, data, &config).await?;
@@ -455,7 +455,7 @@ pub async fn process_link_creation(
#[instrument(skip(id))]
pub async fn edit_link(
tera: web::Data<Tera>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
link_id: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -472,7 +472,7 @@ pub async fn edit_link(
}
pub async fn process_link_edit(
data: web::Form<LinkForm>,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
id: Identity,
link_code: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
@@ -488,7 +488,7 @@ pub async fn process_link_edit(
#[instrument(skip(id))]
pub async fn process_link_delete(
id: Identity,
config: web::Data<pslink::ServerConfig>,
config: web::Data<crate::ServerConfig>,
link_code: web::Path<String>,
) -> Result<HttpResponse, ServerError> {
queries::delete_link(&id, &link_code.0, &config).await?;