nsupdate 0.1.0

An asynchronous DNS UPDATE client for BIND9 with optional TSIG authentication
Documentation
use crate::NsUpdateError;
use crate::internal::{constants::TsigAlg, protocol::encode_domain_name};
use base64::{Engine, engine::general_purpose};
use std::fmt;

/// A validated TSIG key. Debug output omits the secret.
#[derive(Clone)]
pub struct TsigKey {
    pub(crate) algorithm: TsigAlg,
    pub(crate) name: String,
    pub(crate) secret: Vec<u8>,
}

impl TsigKey {
    /// Decode a nonempty base64 secret and validate the key name and algorithm.
    pub fn new(algorithm: &str, name: &str, secret_b64: &str) -> Result<Self, NsUpdateError> {
        let secret = general_purpose::STANDARD.decode(secret_b64)?;
        if secret.is_empty() {
            return Err(NsUpdateError::EmptyTsigKey);
        }
        encode_domain_name(name)?;
        Ok(Self {
            algorithm: TsigAlg::from_string(algorithm)?,
            name: name.to_string(),
            secret,
        })
    }
}

impl fmt::Debug for TsigKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("TsigKey")
            .field("algorithm", &self.algorithm)
            .field("name", &self.name)
            .field("secret", &"[REDACTED]")
            .finish()
    }
}