Skip to content

Commit

Permalink
feat: fixed the record creation for each domain registered
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-cne committed Jul 30, 2024
1 parent 873211f commit 40d8de0
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 49 deletions.
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,32 @@ log:

# The keys and domains configuration
keys:
- key1:
- domain1
- domain2
- key2:
- domain3
- domain4
key1:
sub.example.fr:
mname: ns-acme.example.fr.
rname: postmaster.example.fr.
example.fr:
mname: ns-acme.example.fr.
rname: postmaster.example.fr.
key2:
another-example.fr:
mname: ns-acme.another-example.fr.
rname: postmaster.another-example.fr.
fake.another-example.fr:
mname: ns-acme.another-example.fr.
rname: postmaster.another-example.fr.
```
In the previous example, the `dnsr` server will handle the domain1, domain2, domain3 and domain4 domains.
The key1 will be used to handle the domain1 and domain2 domains and the key2 will be used to handle the domain3 and domain4 domains.
In the previous example, the `dnsr` server will handle the `sub.example.fr`, `example.fr`, `another-example.fr` and `fake.another-example.fr` domains.
The key1 will be used to handle the `sub.example.fr` and `example.fr` domains and the key2 will be used to handle the `another-example.fr` and `fake.another-example.fr` domains.

The record created is the following for the `sub.example.fr` domain:

```text
_acme-challenge.sub.example.fr. 3600 IN SOA ns-acme.example.fr. postmaster.example.fr. 1722353587 10800 3600 605800 3600
```

**Note**: The prefix `_acme-challenge` is automatically added to the domain name.

**Note**: The dnsr server constantly whatches the `config.yml` file for changes.
If the file is modified, the server will reload the domains (e.g. add or remove domains).
Expand Down
20 changes: 20 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum ErrorKind {
TSIGFileAlreadyExist,
RingUnspecified,
Base16,
Utf8,
}

impl std::fmt::Display for Error {
Expand All @@ -40,6 +41,7 @@ impl std::fmt::Display for ErrorKind {
TSIGFileAlreadyExist => write!(f, "tsig file already exists"),
RingUnspecified => write!(f, "ring unspecified error"),
Base16 => write!(f, "base16 error"),
Utf8 => write!(f, "utf8 error"),
}
}
}
Expand All @@ -53,6 +55,15 @@ impl From<ErrorKind> for Error {
}
}

impl From<std::str::Utf8Error> for Error {
fn from(value: std::str::Utf8Error) -> Self {
Self {
kind: ErrorKind::Utf8,
message: Some(value.to_string()),
}
}
}

impl From<notify::Error> for Error {
fn from(value: notify::Error) -> Self {
Self {
Expand Down Expand Up @@ -80,6 +91,15 @@ impl From<domain::base::name::FromStrError> for Error {
}
}

impl From<domain::base::name::NameError> for Error {
fn from(value: domain::base::name::NameError) -> Self {
Self {
kind: ErrorKind::DomainStr,
message: Some(value.to_string()),
}
}
}

impl From<domain::zonetree::error::ZoneTreeModificationError> for Error {
fn from(value: domain::zonetree::error::ZoneTreeModificationError) -> Self {
Self {
Expand Down
74 changes: 43 additions & 31 deletions src/key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use core::str;
use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;

use bytes::BytesMut;
use domain::base::iana::Class;
use domain::base::{Record, Serial, Ttl};
use domain::rdata::Soa;
Expand All @@ -12,10 +14,10 @@ use serde::Deserialize;
use crate::error::Result;

#[derive(Debug, Clone, Deserialize, Default)]
pub struct Keys(HashMap<KeyFile, HashMap<String, DomainInfo>>);
pub struct Keys(HashMap<KeyFile, HashMap<DomainName, DomainInfo>>);

impl Deref for Keys {
type Target = HashMap<KeyFile, HashMap<String, DomainInfo>>;
type Target = HashMap<KeyFile, HashMap<DomainName, DomainInfo>>;

fn deref(&self) -> &Self::Target {
&self.0
Expand All @@ -28,67 +30,77 @@ pub struct DomainInfo {
rname: String,
}

pub trait TryIntoZone {
fn try_into_zone(self) -> Result<Zone>;
}

pub trait TryIntoZones {
fn try_into_zones(self) -> Result<Vec<domain::zonetree::Zone>>;
}
#[derive(Debug, Clone, Deserialize, PartialEq, Eq, Hash)]
pub struct DomainName(String);

trait TryIntoStoredName {
fn try_into_stored_name(self) -> Result<StoredName>;
pub trait TryInto<T> {
fn try_into_t(self) -> Result<T>;
}

impl TryIntoZones for &HashMap<String, DomainInfo> {
fn try_into_zones(self) -> Result<Vec<domain::zonetree::Zone>> {
self.iter().map(|d| d.try_into_zone()).collect()
impl TryInto<Vec<domain::zonetree::Zone>> for &HashMap<DomainName, DomainInfo> {
fn try_into_t(self) -> Result<Vec<domain::zonetree::Zone>> {
self.iter().map(|d| d.try_into_t()).collect()
}
}

impl TryFrom<&DomainInfo> for SharedRrset {
type Error = crate::error::Error;

fn try_from(value: &DomainInfo) -> std::result::Result<Self, Self::Error> {
let mut owner = BytesMut::with_capacity(16 + value.mname.len());
owner.extend_from_slice(b"_acme-challenge.");
owner.extend_from_slice(value.mname.as_bytes());

let record: StoredRecord = Record::new(
(&value.mname).try_into_stored_name()?,
owner.freeze().try_into_t()?,
Class::IN,
Ttl::HOUR,
Soa::new(
(&value.mname).try_into_stored_name()?,
(&value.rname).try_into_stored_name()?,
(&value.mname).try_into_t()?,
(&value.rname).try_into_t()?,
Serial::now(),
Ttl::from_secs(10800),
Ttl::HOUR,
Ttl::HOUR,
Ttl::HOUR,
Ttl::from_secs(605800),
Ttl::HOUR,
)
.into(),
);
log::debug!(target: "record", "new record created: {:?}", record);
let rset: Rrset = record.into();

Ok(rset.into_shared())
}
}

impl<S> TryIntoZone for (S, &DomainInfo)
where
S: AsRef<str>,
{
fn try_into_zone(self) -> Result<Zone> {
impl TryInto<Zone> for (&DomainName, &DomainInfo) {
fn try_into_t(self) -> Result<Zone> {
let (name, info) = self;
let mut builder = ZoneBuilder::new((&name).try_into_stored_name()?, Class::IN);
builder.insert_rrset(&name.try_into_stored_name()?, info.try_into()?)?;
Ok(builder.build())
let mut builder = ZoneBuilder::new(name.try_into_t()?, Class::IN);
builder.insert_rrset(&name.try_into_t()?, info.try_into()?)?;
let zone = builder.build();
log::debug!(target: "zone", "new zone created: {:?}", zone);
Ok(zone)
}
}

impl TryInto<StoredName> for &DomainName {
fn try_into_t(self) -> Result<StoredName> {
let mut owner = BytesMut::with_capacity(16 + self.0.len());
owner.extend_from_slice(b"_acme-challenge.");
owner.extend_from_slice(self.0.as_bytes());

owner.freeze().try_into_t()
}
}

impl<S> TryIntoStoredName for S
impl<B> TryInto<StoredName> for B
where
S: AsRef<str>,
B: AsRef<[u8]>,
{
fn try_into_stored_name(self) -> Result<StoredName> {
Ok(StoredName::bytes_from_str(self.as_ref())?)
fn try_into_t(self) -> Result<StoredName> {
let str = str::from_utf8(self.as_ref())?;
Ok(StoredName::bytes_from_str(str)?)
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher as NotifyWatcher

use crate::dns::State;
use crate::error::{ErrorKind, Result};
use crate::key::{DomainInfo, KeyFile, Keys, TryIntoZone, TryIntoZones};
use crate::key::{DomainInfo, DomainName, KeyFile, Keys, TryInto};

#[derive(Debug, Clone)]
pub struct Watcher;
Expand Down Expand Up @@ -46,7 +46,7 @@ fn initialize_dns_zones(keys: &Keys, state: &Arc<State>) -> Result<()> {
}

for (k, v) in keys.iter() {
v.try_into_zones()?
v.try_into_t()?
.into_iter()
.try_for_each(|z| state.insert_zone(z))?;
match k.generate_key_file() {
Expand Down Expand Up @@ -86,10 +86,10 @@ fn handle_file_change(keys: &Keys, config_path: &Path, state: &Arc<State>) -> Re

fn handle_deleted_keys<'i, I>(state: &Arc<State>, deleted_keys: I) -> Result<()>
where
I: IntoIterator<Item = (&'i KeyFile, &'i HashMap<String, DomainInfo>)>,
I: IntoIterator<Item = (&'i KeyFile, &'i HashMap<DomainName, DomainInfo>)>,
{
for (k, v) in deleted_keys {
v.try_into_zones()?.into_iter().for_each(|z| {
v.try_into_t()?.into_iter().for_each(|z| {
let _ = state.remove_zone(z.apex_name(), z.class());
});

Expand All @@ -102,10 +102,10 @@ where

fn handle_added_keys<'i, I>(state: &Arc<State>, added_keys: I) -> Result<()>
where
I: IntoIterator<Item = (&'i KeyFile, &'i HashMap<String, DomainInfo>)>,
I: IntoIterator<Item = (&'i KeyFile, &'i HashMap<DomainName, DomainInfo>)>,
{
for (k, v) in added_keys {
v.try_into_zones()?.into_iter().for_each(|z| {
v.try_into_t()?.into_iter().for_each(|z| {
let _ = state.insert_zone(z);
});

Expand All @@ -120,23 +120,23 @@ fn handle_modified_keys<'i, I>(state: &Arc<State>, modified_keys: I) -> Result<(
where
I: IntoIterator<
Item = (
&'i HashMap<String, DomainInfo>,
&'i HashMap<String, DomainInfo>,
&'i HashMap<DomainName, DomainInfo>,
&'i HashMap<DomainName, DomainInfo>,
),
>,
{
for (nv, ov) in modified_keys {
ov.iter()
.filter(|&(d, _)| nv.get(d).is_none())
.try_for_each(|d| -> Result<()> {
let zone: Zone = d.try_into_zone()?;
let zone: Zone = d.try_into_t()?;
let _ = state.remove_zone(zone.apex_name(), zone.class());
Ok(())
})?;
nv.iter()
.filter(|&(d, _)| ov.get(d).is_none())
.try_for_each(|d| -> Result<()> {
let zone: Zone = d.try_into_zone()?;
let zone: Zone = d.try_into_t()?;
let _ = state.insert_zone(zone);
Ok(())
})?;
Expand Down

0 comments on commit 40d8de0

Please sign in to comment.