Implement thiserror and improve output formatting (#91)

* implemented an error struct with all possible errors using thiserror

* Replaced die_with with ClientError enum derived with thiserror.
This enables prettier and more structured error handling than anyhow

* Added proper parsing of Gandi API Responses.
This also makes it possible to output prettier logs.

* improved error message

* anyhow is better for main fn because it outputs anyhow errors correctly
This commit is contained in:
jannikac 2023-02-11 04:08:54 +01:00 committed by GitHub
parent 5cdd7b9e83
commit 8413555d2f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 189 additions and 49 deletions

1
Cargo.lock generated
View file

@ -666,6 +666,7 @@ dependencies = [
"regex", "regex",
"reqwest", "reqwest",
"serde", "serde",
"thiserror",
"tokio", "tokio",
"toml", "toml",
] ]

View file

@ -33,6 +33,7 @@ anyhow = "1.0"
governor = "0.5" governor = "0.5"
async-trait = "0.1" async-trait = "0.1"
die-exit = "0.4" die-exit = "0.4"
thiserror = "1.0.38"
[dev-dependencies] [dev-dependencies]
httpmock = "0.6" httpmock = "0.6"

View file

@ -1,13 +1,26 @@
use crate::opts; use crate::opts;
use directories::ProjectDirs; use directories::ProjectDirs;
use serde::Deserialize; use serde::Deserialize;
use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
use std::{fs, io};
use thiserror::Error;
fn default_types() -> Vec<String> { fn default_types() -> Vec<String> {
DEFAULT_TYPES.iter().map(|v| v.to_string()).collect() DEFAULT_TYPES.iter().map(|v| v.to_string()).collect()
} }
#[derive(Error, Debug)]
pub enum ConfigError {
#[error("Failed to read config file: {0} ")]
Io(#[from] io::Error),
#[error("Failed to parse config file: {0}")]
Parse(#[from] toml::de::Error),
#[error("Entry '{0}' has invalid type '{1}'")]
Validation(String, String),
#[error("Can't find config directory")]
ConfigNotFound(),
}
#[derive(Deserialize, Debug)] #[derive(Deserialize, Debug)]
pub struct Entry { pub struct Entry {
pub name: String, pub name: String,
@ -65,18 +78,20 @@ impl Config {
} }
} }
fn load_config_from<P: std::convert::AsRef<std::path::Path>>(path: P) -> anyhow::Result<Config> { fn load_config_from<P: std::convert::AsRef<std::path::Path>>(
path: P,
) -> Result<Config, ConfigError> {
let contents = fs::read_to_string(path)?; let contents = fs::read_to_string(path)?;
Ok(toml::from_str(&contents)?) Ok(toml::from_str(&contents)?)
} }
pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> { pub fn load_config(opts: &opts::Opts) -> Result<Config, ConfigError> {
let mut config = match &opts.config { let mut config = match &opts.config {
Some(config_path) => load_config_from(config_path), Some(config_path) => load_config_from(config_path),
None => { None => {
let confpath = ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns") let confpath = ProjectDirs::from("me", "kaangenc", "gandi-dynamic-dns")
.map(|dir| PathBuf::from(dir.config_dir()).join("config.toml")) .map(|dir| PathBuf::from(dir.config_dir()).join("config.toml"))
.ok_or_else(|| anyhow::anyhow!("Can't find config directory")); .ok_or(ConfigError::ConfigNotFound());
confpath confpath
.and_then(|path| { .and_then(|path| {
println!("Checking for config: {}", path.to_string_lossy()); println!("Checking for config: {}", path.to_string_lossy());
@ -105,11 +120,14 @@ pub fn load_config(opts: &opts::Opts) -> anyhow::Result<Config> {
Ok(config) Ok(config)
} }
pub fn validate_config(config: &Config) -> anyhow::Result<()> { pub fn validate_config(config: &Config) -> Result<(), ConfigError> {
for entry in &config.entry { for entry in &config.entry {
for entry_type in Config::types(entry) { for entry_type in Config::types(entry) {
if entry_type != "A" && entry_type != "AAAA" { if entry_type != "A" && entry_type != "AAAA" {
anyhow::bail!("Entry {} has invalid type {}", entry.name, entry_type); return Err(ConfigError::Validation(
entry.name.clone(),
entry_type.to_string(),
));
} }
} }
} }

View file

@ -1,10 +1,12 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::ClientError;
use super::ip_source::IPSource; use super::ip_source::IPSource;
pub(crate) struct IPSourceIcanhazip; pub(crate) struct IPSourceIcanhazip;
async fn get_ip(api_url: &str) -> anyhow::Result<String> { async fn get_ip(api_url: &str) -> Result<String, ClientError> {
let response = reqwest::get(api_url).await?; let response = reqwest::get(api_url).await?;
let text = response.text().await?; let text = response.text().await?;
Ok(text) Ok(text)
@ -12,14 +14,14 @@ async fn get_ip(api_url: &str) -> anyhow::Result<String> {
#[async_trait] #[async_trait]
impl IPSource for IPSourceIcanhazip { impl IPSource for IPSourceIcanhazip {
async fn get_ipv4(&self) -> anyhow::Result<String> { async fn get_ipv4(&self) -> Result<String, ClientError> {
Ok(get_ip("https://ipv4.icanhazip.com") Ok(get_ip("https://ipv4.icanhazip.com")
.await? .await?
// icanazip puts a newline at the end // icanazip puts a newline at the end
.trim() .trim()
.to_string()) .to_string())
} }
async fn get_ipv6(&self) -> anyhow::Result<String> { async fn get_ipv6(&self) -> Result<String, ClientError> {
Ok(get_ip("https://ipv6.icanhazip.com") Ok(get_ip("https://ipv6.icanhazip.com")
.await? .await?
// icanazip puts a newline at the end // icanazip puts a newline at the end

View file

@ -1,7 +1,9 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::ClientError;
#[async_trait] #[async_trait]
pub trait IPSource { pub trait IPSource {
async fn get_ipv4(&self) -> anyhow::Result<String>; async fn get_ipv4(&self) -> Result<String, ClientError>;
async fn get_ipv6(&self) -> anyhow::Result<String>; async fn get_ipv6(&self) -> Result<String, ClientError>;
} }

View file

@ -1,10 +1,12 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::ClientError;
use super::ip_source::IPSource; use super::ip_source::IPSource;
pub(crate) struct IPSourceIpify; pub(crate) struct IPSourceIpify;
async fn get_ip(api_url: &str) -> anyhow::Result<String> { async fn get_ip(api_url: &str) -> Result<String, ClientError> {
let response = reqwest::get(api_url).await?; let response = reqwest::get(api_url).await?;
let text = response.text().await?; let text = response.text().await?;
Ok(text) Ok(text)
@ -12,10 +14,10 @@ async fn get_ip(api_url: &str) -> anyhow::Result<String> {
#[async_trait] #[async_trait]
impl IPSource for IPSourceIpify { impl IPSource for IPSourceIpify {
async fn get_ipv4(&self) -> anyhow::Result<String> { async fn get_ipv4(&self) -> Result<String, ClientError> {
get_ip("https://api.ipify.org").await get_ip("https://api.ipify.org").await
} }
async fn get_ipv6(&self) -> anyhow::Result<String> { async fn get_ipv6(&self) -> Result<String, ClientError> {
get_ip("https://api6.ipify.org").await get_ip("https://api6.ipify.org").await
} }
} }

View file

@ -1,10 +1,12 @@
use async_trait::async_trait; use async_trait::async_trait;
use crate::ClientError;
use super::ip_source::IPSource; use super::ip_source::IPSource;
pub(crate) struct IPSourceSeeIP; pub(crate) struct IPSourceSeeIP;
async fn get_ip(api_url: &str) -> anyhow::Result<String> { async fn get_ip(api_url: &str) -> Result<String, ClientError> {
let response = reqwest::get(api_url).await?; let response = reqwest::get(api_url).await?;
let text = response.text().await?; let text = response.text().await?;
Ok(text) Ok(text)
@ -12,10 +14,10 @@ async fn get_ip(api_url: &str) -> anyhow::Result<String> {
#[async_trait] #[async_trait]
impl IPSource for IPSourceSeeIP { impl IPSource for IPSourceSeeIP {
async fn get_ipv4(&self) -> anyhow::Result<String> { async fn get_ipv4(&self) -> Result<String, ClientError> {
get_ip("https://ip4.seeip.org").await get_ip("https://ip4.seeip.org").await
} }
async fn get_ipv6(&self) -> anyhow::Result<String> { async fn get_ipv6(&self) -> Result<String, ClientError> {
get_ip("https://ip6.seeip.org").await get_ip("https://ip6.seeip.org").await
} }
} }

View file

@ -2,12 +2,13 @@ use crate::config::Config;
use crate::gandi::GandiAPI; use crate::gandi::GandiAPI;
use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify}; use crate::ip_source::{ip_source::IPSource, ipify::IPSourceIpify};
use clap::Parser; use clap::Parser;
use config::IPSourceName; use config::{ConfigError, IPSourceName};
use ip_source::icanhazip::IPSourceIcanhazip; use ip_source::icanhazip::IPSourceIcanhazip;
use ip_source::seeip::IPSourceSeeIP; use ip_source::seeip::IPSourceSeeIP;
use opts::Opts; use opts::Opts;
use reqwest::header::InvalidHeaderValue;
use reqwest::{header, Client, ClientBuilder, StatusCode}; use reqwest::{header, Client, ClientBuilder, StatusCode};
use serde::Serialize; use serde::{Deserialize, Serialize};
use std::{num::NonZeroU32, sync::Arc, time::Duration}; use std::{num::NonZeroU32, sync::Arc, time::Duration};
use tokio::join; use tokio::join;
use tokio::{self, task::JoinHandle, time::sleep}; use tokio::{self, task::JoinHandle, time::sleep};
@ -16,13 +17,44 @@ mod gandi;
mod ip_source; mod ip_source;
mod opts; mod opts;
use die_exit::*; use die_exit::*;
use thiserror::Error;
/// 30 requests per minute, see https://api.gandi.net/docs/reference/ /// 30 requests per minute, see https://api.gandi.net/docs/reference/
const GANDI_RATE_LIMIT: u32 = 30; const GANDI_RATE_LIMIT: u32 = 30;
/// If we hit the rate limit, wait up to this many seconds before next attempt /// If we hit the rate limit, wait up to this many seconds before next attempt
const GANDI_DELAY_JITTER: u64 = 20; const GANDI_DELAY_JITTER: u64 = 20;
fn api_client(api_key: &str) -> anyhow::Result<Client> { #[derive(Error, Debug)]
pub enum ClientError {
#[error("Error occured while reading config: {0}")]
Config(#[from] ConfigError),
#[error("Error while accessing the Gandi API: {0}")]
Api(#[from] ApiError),
#[error("Error while converting the API key to a header: {0}")]
InvalidHeader(#[from] InvalidHeaderValue),
#[error("Error while sending request: {0}")]
Request(#[from] reqwest::Error),
#[error("Error while joining async tasks: {0}")]
TaskJoin(#[from] tokio::task::JoinError),
#[error("Unexpected type in config: {0}")]
BadEntry(String),
#[error("Entry '{0}' includes type A which requires an IPv4 adress but no IPv4 adress could be determined because: {1}")]
Ipv4missing(String, String),
#[error("Entry '{0}' includes type AAAA which requires an IPv6 adress but no IPv6 adress could be determined because: {1}")]
Ipv6missing(String, String),
}
#[derive(Error, Debug)]
pub enum ApiError {
#[error("API returned 403 - Forbidden. Message: {message:?}")]
Forbidden { message: String },
#[error("API returned 403 - Unauthorized. Provided API key is possibly incorrect")]
Unauthorized(),
#[error("API returned {0} - {0}")]
Unknown(StatusCode, String),
}
fn api_client(api_key: &str) -> Result<Client, ClientError> {
let client_builder = ClientBuilder::new(); let client_builder = ClientBuilder::new();
let key = format!("Apikey {}", api_key); let key = format!("Apikey {}", api_key);
@ -42,12 +74,27 @@ pub struct APIPayload {
pub rrset_ttl: u32, pub rrset_ttl: u32,
} }
#[derive(Debug)]
struct ResponseFeedback {
entry_name: String,
entry_type: String,
response: Result<String, ApiError>,
}
#[derive(Deserialize)]
struct ApiResponse {
message: String,
cause: Option<String>,
code: Option<i32>,
object: Option<String>,
}
async fn run( async fn run(
base_url: &str, base_url: &str,
ip_source: &Box<dyn IPSource>, ip_source: &Box<dyn IPSource>,
conf: &Config, conf: &Config,
opts: &Opts, opts: &Opts,
) -> anyhow::Result<()> { ) -> Result<(), ClientError> {
let mut last_ipv4: Option<String> = None; let mut last_ipv4: Option<String> = None;
let mut last_ipv6: Option<String> = None; let mut last_ipv6: Option<String> = None;
@ -80,7 +127,7 @@ async fn run(
if !ipv4_same || !ipv6_same || conf.always_update { if !ipv4_same || !ipv6_same || conf.always_update {
let client = api_client(&conf.api_key)?; let client = api_client(&conf.api_key)?;
let mut tasks: Vec<JoinHandle<(StatusCode, String)>> = Vec::new(); let mut tasks: Vec<JoinHandle<Result<ResponseFeedback, ClientError>>> = Vec::new();
println!("Attempting to update DNS entries now"); println!("Attempting to update DNS entries now");
let governor = Arc::new(governor::RateLimiter::direct(governor::Quota::per_minute( let governor = Arc::new(governor::RateLimiter::direct(governor::Quota::per_minute(
@ -100,10 +147,22 @@ async fn run(
} }
.url(); .url();
let ip = match entry_type { let ip = match entry_type {
"A" => ipv4.die_with(|error| format!("Needed IPv4 for {fqdn}: {error}")), "A" => match ipv4 {
"AAAA" => ipv6.die_with(|error| format!("Needed IPv6 for {fqdn}: {error}")), Ok(ref value) => Ok(value),
bad_entry_type => die!("Unexpected type in config: {}", bad_entry_type), Err(ref err) => Err(ClientError::Ipv4missing(
}; entry.name.clone(),
err.to_string(),
)),
},
"AAAA" => match ipv6 {
Ok(ref value) => Ok(value),
Err(ref err) => Err(ClientError::Ipv6missing(
entry.name.clone(),
err.to_string(),
)),
},
&_ => Err(ClientError::BadEntry(entry_type.to_string())),
}?;
let payload = APIPayload { let payload = APIPayload {
rrset_values: vec![ip.to_string()], rrset_values: vec![ip.to_string()],
rrset_ttl: Config::ttl(entry, conf), rrset_ttl: Config::ttl(entry, conf),
@ -111,28 +170,82 @@ async fn run(
let req = client.put(url).json(&payload); let req = client.put(url).json(&payload);
let task_governor = governor.clone(); let task_governor = governor.clone();
let entry_type = entry_type.to_string(); let entry_type = entry_type.to_string();
let task = tokio::task::spawn(async move { let entry_name = entry.name.to_string();
task_governor.until_ready_with_jitter(retry_jitter).await;
println!("Updating {} record for {}", entry_type, &fqdn); let task: JoinHandle<Result<ResponseFeedback, ClientError>> =
match req.send().await { tokio::task::spawn(async move {
Ok(response) => ( task_governor.until_ready_with_jitter(retry_jitter).await;
response.status(), println!("Updating {} record for {}", entry_type, &fqdn);
response
.text() let resp = req.send().await?;
.await
.unwrap_or_else(|error| error.to_string()), let response_feedback = match resp.status() {
), StatusCode::CREATED => {
Err(error) => (StatusCode::IM_A_TEAPOT, error.to_string()), let body: ApiResponse = resp.json().await?;
} ResponseFeedback {
}); entry_name,
entry_type,
response: Ok(body.message),
}
}
StatusCode::UNAUTHORIZED => ResponseFeedback {
entry_name,
entry_type,
response: Err(ApiError::Unauthorized()),
},
StatusCode::FORBIDDEN => {
let body: ApiResponse = resp.json().await?;
ResponseFeedback {
entry_name: entry_name.clone(),
entry_type,
response: Err(ApiError::Forbidden {
message: body.message,
}),
}
}
_ => {
let status = resp.status();
let body: ApiResponse = resp.json().await?;
ResponseFeedback {
entry_name,
entry_type,
response: Err(ApiError::Unknown(status, body.message)),
}
}
};
Ok(response_feedback)
});
tasks.push(task); tasks.push(task);
} }
} }
let results = futures::future::try_join_all(tasks).await?; let results = futures::future::try_join_all(tasks).await?;
println!("Updates done for {} entries", results.len()); // Only count successfull requests
for (status, body) in results { println!(
println!("{} - {}", status, body); "Updates done for {} entries",
results
.iter()
.filter_map(|item| item.as_ref().ok())
.filter(|item| item.response.is_ok())
.count()
);
for item in results {
match item {
Ok(value) => println!(
"{}",
match value.response {
Ok(val) => format!(
"Record '{}' ({}): {}",
value.entry_name, value.entry_type, val
),
Err(err) => format!(
"Record '{}' ({}): {}",
value.entry_name, value.entry_type, err
),
}
),
Err(err) => println!("{}", err),
}
} }
} else { } else {
println!("IP address has not changed since last update"); println!("IP address has not changed since last update");
@ -153,15 +266,14 @@ async fn run(
#[tokio::main(flavor = "current_thread")] #[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> { async fn main() -> anyhow::Result<()> {
let opts = opts::Opts::parse(); let opts = opts::Opts::parse();
let conf = config::load_config(&opts) let conf = config::load_config(&opts)?;
.die_with(|error| format!("Failed to read config file: {}", error));
let ip_source: Box<dyn IPSource> = match conf.ip_source { let ip_source: Box<dyn IPSource> = match conf.ip_source {
IPSourceName::Ipify => Box::new(IPSourceIpify), IPSourceName::Ipify => Box::new(IPSourceIpify),
IPSourceName::Icanhazip => Box::new(IPSourceIcanhazip), IPSourceName::Icanhazip => Box::new(IPSourceIcanhazip),
IPSourceName::SeeIP => Box::new(IPSourceSeeIP), IPSourceName::SeeIP => Box::new(IPSourceSeeIP),
}; };
config::validate_config(&conf).die_with(|error| format!("Invalid config: {}", error)); config::validate_config(&conf)?;
run("https://api.gandi.net", &ip_source, &conf, &opts).await?; run("https://api.gandi.net", &ip_source, &conf, &opts).await?;
Ok(()) Ok(())
} }
@ -170,7 +282,7 @@ async fn main() -> anyhow::Result<()> {
mod tests { mod tests {
use std::{env::temp_dir, time::Duration}; use std::{env::temp_dir, time::Duration};
use crate::{config, ip_source::ip_source::IPSource, opts::Opts, run}; use crate::{config, ip_source::ip_source::IPSource, opts::Opts, run, ClientError};
use async_trait::async_trait; use async_trait::async_trait;
use httpmock::MockServer; use httpmock::MockServer;
use tokio::{fs, task::LocalSet, time::sleep}; use tokio::{fs, task::LocalSet, time::sleep};
@ -179,10 +291,10 @@ mod tests {
#[async_trait] #[async_trait]
impl IPSource for IPSourceMock { impl IPSource for IPSourceMock {
async fn get_ipv4(&self) -> anyhow::Result<String> { async fn get_ipv4(&self) -> Result<String, ClientError> {
Ok("192.168.0.0".to_string()) Ok("192.168.0.0".to_string())
} }
async fn get_ipv6(&self) -> anyhow::Result<String> { async fn get_ipv6(&self) -> Result<String, ClientError> {
Ok("fe80:0000:0208:74ff:feda:625c".to_string()) Ok("fe80:0000:0208:74ff:feda:625c".to_string())
} }
} }