From 445516e9e954537c552129afc1d22f972456e745 Mon Sep 17 00:00:00 2001 From: Nicholas Rempel Date: Thu, 21 Oct 2021 17:11:24 -0700 Subject: [PATCH] Revise config construction --- epp-client/Cargo.toml | 2 - epp-client/src/config.rs | 44 +++-------------- epp-client/src/connection/client.rs | 74 ++++++++++------------------- 3 files changed, 30 insertions(+), 90 deletions(-) diff --git a/epp-client/Cargo.toml b/epp-client/Cargo.toml index d9d44cc..ef674e4 100644 --- a/epp-client/Cargo.toml +++ b/epp-client/Cargo.toml @@ -13,11 +13,9 @@ repository = "https://github.com/masalachai/epp-client" epp-client-macros = "0.1" # { path = "../epp-client-macros" } bytes = "1" chrono = "0.4" -confy = "0.4" futures = "0.3" env_logger = "0.9" log = "0.4" -lazy_static = "1.4" quick-xml = { version = "0.22", features = [ "serialize" ] } rustls = "0.20" rustls-pemfile = "0.2" diff --git a/epp-client/src/config.rs b/epp-client/src/config.rs index 753c379..eee867d 100644 --- a/epp-client/src/config.rs +++ b/epp-client/src/config.rs @@ -47,24 +47,13 @@ //! let tls = registry.tls_files().unwrap(); //! ``` -use confy; -use lazy_static::lazy_static; use rustls::{Certificate, PrivateKey}; use rustls_pemfile; use serde::{Deserialize, Serialize}; use std::collections::HashMap; -use std::default; use std::io::{Seek, SeekFrom}; use std::{fs, io}; -lazy_static! { - /// Static reference to the config file - pub static ref CONFIG: EppClientConfig = match confy::load("epp-client") { - Ok(cfg) => cfg, - Err(e) => panic!("Config read error: {}", e), - }; -} - /// Paths to the client certificate and client key PEM files #[derive(Serialize, Deserialize, Debug)] pub struct EppClientTlsFiles { @@ -75,12 +64,12 @@ pub struct EppClientTlsFiles { /// Connection details to connect to and authenticate with a registry #[derive(Serialize, Deserialize, Debug)] pub struct EppClientConnection { - host: String, - port: u16, - username: String, - password: String, - ext_uris: Option>, - tls_files: Option, + pub host: String, + pub port: u16, + pub username: String, + pub password: String, + pub ext_uris: Option>, + pub tls_files: Option, } /// Config that stores settings for multiple registries @@ -89,27 +78,6 @@ pub struct EppClientConfig { pub registry: HashMap, } -impl default::Default for EppClientConfig { - fn default() -> Self { - let mut registries: HashMap = HashMap::new(); - let registrar = EppClientConnection { - host: "epphost".to_string(), - port: 700, - username: "username".to_string(), - password: "password".to_string(), - ext_uris: Some(vec![]), - tls_files: Some(EppClientTlsFiles { - cert_chain: "/path/to/certificate/chain/pemfile".to_string(), - key: "/path/to/private/key/pemfile".to_string(), - }), - }; - registries.insert("verisign".to_string(), registrar); - Self { - registry: registries, - } - } -} - impl EppClientConnection { /// Returns the EPP host and port no as a tuple pub fn connection_details(&self) -> (String, u16) { diff --git a/epp-client/src/connection/client.rs b/epp-client/src/connection/client.rs index 980494c..c24b857 100644 --- a/epp-client/src/connection/client.rs +++ b/epp-client/src/connection/client.rs @@ -28,12 +28,10 @@ //! ``` use futures::executor::block_on; -use std::sync::mpsc; use std::time::SystemTime; use std::{error::Error, fmt::Debug}; -// use std::sync::Arc; -use crate::config::CONFIG; +use crate::config::EppClientConfig; use crate::connection::registry::{epp_connect, EppConnection}; use crate::epp::request::{generate_client_tr_id, EppHello, EppLogin, EppLogout}; use crate::epp::response::{ @@ -41,40 +39,6 @@ use crate::epp::response::{ }; use crate::epp::xml::EppXml; use crate::error; - -/// Connects to the registry and returns an logged-in instance of EppClient for further transactions -async fn connect(registry: &'static str) -> Result> { - let registry_creds = match CONFIG.registry(registry) { - Some(creds) => creds, - None => return Err(format!("missing credentials for {}", registry).into()), - }; - - let (tx, rx) = mpsc::channel(); - - tokio::spawn(async move { - let stream = epp_connect(registry_creds).await.unwrap(); - let credentials = registry_creds.credentials(); - let ext_uris = registry_creds.ext_uris(); - - let ext_uris = - ext_uris.map(|uris| uris.iter().map(|u| u.to_string()).collect::>()); - - let connection = EppConnection::new(registry.to_string(), stream) - .await - .unwrap(); - - let client = EppClient::build(connection, credentials, ext_uris) - .await - .unwrap(); - - tx.send(client).unwrap(); - }); - - let client = rx.recv()?; - - Ok(client) -} - /// Instances of the EppClient type are used to transact with the registry. /// Once initialized, the EppClient instance can serialize EPP requests to XML and send them /// to the registry and deserialize the XML responses from the registry to local types @@ -82,7 +46,6 @@ pub struct EppClient { credentials: (String, String), ext_uris: Option>, connection: EppConnection, - // pub client_tr_id_fn: Arc String + Send + Sync>, } /// A function to generate a simple client TRID. Should only be used for testing, library users @@ -96,20 +59,26 @@ pub fn default_client_tr_id_fn(client: &EppClient) -> String { } impl EppClient { - /// Fetches the username used in the registry connection - pub fn username(&self) -> String { - self.credentials.0.to_string() - } - - // pub fn set_client_tr_id_fn(&mut self, func: F) - // where F: Fn(&EppClient) -> String + Send + Sync + 'static { - // self.client_tr_id_fn = Arc::new(func); - // } - /// Creates a new EppClient object and does an EPP Login to a given registry to become ready /// for subsequent transactions on this client instance - pub async fn new(registry: &'static str) -> Result> { - connect(registry).await + pub async fn new( + config: &EppClientConfig, + registry: &str, + ) -> Result> { + let registry_creds = match config.registry(registry) { + Some(creds) => creds, + None => return Err(format!("missing credentials for {}", registry).into()), + }; + + let stream = epp_connect(registry_creds).await?; + let credentials = registry_creds.credentials(); + let ext_uris = registry_creds.ext_uris(); + + let ext_uris = + ext_uris.map(|uris| uris.iter().map(|u| u.to_string()).collect::>()); + + let connection = EppConnection::new(registry.to_string(), stream).await?; + EppClient::build(connection, credentials, ext_uris).await } /// Makes a login request to the registry and initializes an EppClient instance with it @@ -171,6 +140,11 @@ impl EppClient { } } + /// Fetches the username used in the registry connection + pub fn username(&self) -> String { + self.credentials.0.to_string() + } + /// Accepts raw EPP XML and returns the raw EPP XML response to it. /// Not recommended for direct use but sometimes can be useful for debugging pub async fn transact_xml(&mut self, xml: &str) -> Result> {