Skip to content

Commit

Permalink
Raise a descriptive error for invalid identifier specifications
Browse files Browse the repository at this point in the history
An identifier must have one and only one type field (`dns` or `ip`).
Rel #24
  • Loading branch information
breard-r committed Aug 26, 2020
1 parent 53d55af commit a1ff1f6
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion acmed/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use acme_common::crypto::{HashFunction, KeyType};
use acme_common::error::Error;
use glob::glob;
use log::info;
use serde::Deserialize;
use serde::{de, Deserialize, Deserializer};
use std::collections::HashMap;
use std::fmt;
use std::fs::{self, File};
Expand Down Expand Up @@ -421,6 +421,7 @@ impl Certificate {
}

#[derive(Clone, Debug, Deserialize)]
#[serde(remote = "Self")]
#[serde(deny_unknown_fields)]
pub struct Identifier {
pub challenge: String,
Expand All @@ -430,6 +431,26 @@ pub struct Identifier {
pub env: HashMap<String, String>,
}

impl<'de> Deserialize<'de> for Identifier {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let unchecked = Identifier::deserialize(deserializer)?;
let filled_nb: u8 = [unchecked.dns.is_some(), unchecked.ip.is_some()]
.iter()
.copied()
.map(u8::from)
.sum();
if filled_nb != 1 {
return Err(de::Error::custom(
"one and only one of `dns` or `ip` must be specified",
));
}
Ok(unchecked)
}
}

impl fmt::Display for Identifier {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let s = String::new();
Expand Down

0 comments on commit a1ff1f6

Please sign in to comment.