Revise config construction

This commit is contained in:
Nicholas Rempel 2021-10-21 17:11:24 -07:00 committed by masalachai
parent 08c9c6e8fd
commit 445516e9e9
3 changed files with 30 additions and 90 deletions

View File

@ -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"

View File

@ -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<Vec<String>>,
tls_files: Option<EppClientTlsFiles>,
pub host: String,
pub port: u16,
pub username: String,
pub password: String,
pub ext_uris: Option<Vec<String>>,
pub tls_files: Option<EppClientTlsFiles>,
}
/// Config that stores settings for multiple registries
@ -89,27 +78,6 @@ pub struct EppClientConfig {
pub registry: HashMap<String, EppClientConnection>,
}
impl default::Default for EppClientConfig {
fn default() -> Self {
let mut registries: HashMap<String, EppClientConnection> = 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) {

View File

@ -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<EppClient, Box<dyn Error>> {
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::<Vec<String>>());
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<Vec<String>>,
connection: EppConnection,
// pub client_tr_id_fn: Arc<dyn Fn(&EppClient) -> 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<F>(&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<EppClient, Box<dyn Error>> {
connect(registry).await
pub async fn new(
config: &EppClientConfig,
registry: &str,
) -> Result<EppClient, Box<dyn Error>> {
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::<Vec<String>>());
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<String, Box<dyn Error>> {