Skip to content

Commit

Permalink
replace SocketAddr with String in certificate requests
Browse files Browse the repository at this point in the history
  • Loading branch information
Keksoj committed Mar 16, 2023
1 parent c1f5b6e commit 2516e3b
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 40 deletions.
10 changes: 4 additions & 6 deletions bin/src/acme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ pub fn main(
let http = http_frontend_address
.parse::<SocketAddr>()
.with_context(|| "invalid HTTP frontend address format")?;
let https = https_frontend_address
.parse::<SocketAddr>()
.with_context(|| "invalid HTTPS frontend address format")?;
let https = https_frontend_address;

let old_certificate_file = match old_certificate_path {
Some(path) => {
Expand Down Expand Up @@ -344,7 +342,7 @@ fn remove_proxying(

fn add_certificate(
channel: &mut Channel<Request, Response>,
frontend: &SocketAddr,
frontend: &str,
hostname: &str,
certificate_path: &str,
chain_path: &str,
Expand All @@ -363,7 +361,7 @@ fn add_certificate(

let request = match old_fingerprint {
None => Request::AddCertificate(AddCertificate {
address: *frontend,
address: frontend.to_owned(),
certificate: CertificateAndKey {
certificate,
certificate_chain,
Expand All @@ -375,7 +373,7 @@ fn add_certificate(
}),

Some(f) => Request::ReplaceCertificate(ReplaceCertificate {
address: *frontend,
address: frontend.to_owned(),
new_certificate: CertificateAndKey {
certificate,
certificate_chain,
Expand Down
6 changes: 3 additions & 3 deletions bin/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -786,7 +786,7 @@ pub enum CertificateCmd {
long = "address",
help = "listener address, format: IP:port"
)]
address: SocketAddr,
address: String,
#[clap(long = "certificate", help = "path to the certificate")]
certificate: String,
#[clap(long = "certificate-chain", help = "path to the certificate chain")]
Expand All @@ -804,7 +804,7 @@ pub enum CertificateCmd {
long = "address",
help = "listener address, format: IP:port"
)]
address: SocketAddr,
address: String,
#[clap(aliases = &["cert"], long = "certificate", help = "path to the certificate")]
certificate: Option<String>,
#[clap(short = 'f', long = "fingerprint", help = "certificate fingerprint")]
Expand All @@ -817,7 +817,7 @@ pub enum CertificateCmd {
long = "address",
help = "listener address, format: IP:port"
)]
address: SocketAddr,
address: String,
#[clap(long = "new-certificate", help = "path to the new certificate")]
certificate: String,
#[clap(
Expand Down
8 changes: 3 additions & 5 deletions bin/src/ctl/request_builder.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::net::SocketAddr;

use anyhow::{bail, Context};

use sozu_command_lib::{
Expand Down Expand Up @@ -438,7 +436,7 @@ impl CommandManager {

pub fn add_certificate(
&mut self,
address: SocketAddr,
address: String,
certificate_path: &str,
certificate_chain_path: &str,
key_path: &str,
Expand All @@ -458,7 +456,7 @@ impl CommandManager {

pub fn replace_certificate(
&mut self,
address: SocketAddr,
address: String,
new_certificate_path: &str,
new_certificate_chain_path: &str,
new_key_path: &str,
Expand Down Expand Up @@ -500,7 +498,7 @@ impl CommandManager {

pub fn remove_certificate(
&mut self,
address: SocketAddr,
address: String,
certificate_path: Option<&str>,
fingerprint: Option<&str>,
) -> anyhow::Result<()> {
Expand Down
7 changes: 6 additions & 1 deletion command/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,13 @@ impl HttpFrontendConfig {
let mut v = Vec::new();

if self.key.is_some() && self.certificate.is_some() {
// <<<<<<< HEAD
v.push(Request::AddCertificate(AddCertificate {
address: self.address,
// address: self.address,
// =======
// v.push(ProxyRequestOrder::AddCertificate(AddCertificate {
address: self.address.to_string(),
// >>>>>>> b5ce4fb8 (replace SocketAddr with String in certificate requests)
certificate: CertificateAndKey {
key: self.key.clone().unwrap(),
certificate: self.certificate.clone().unwrap(),
Expand Down
6 changes: 3 additions & 3 deletions command/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ pub struct FrontendFilters {

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AddCertificate {
pub address: SocketAddr,
pub address: String,
pub certificate: CertificateAndKey,
#[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")]
pub names: Vec<String>,
Expand All @@ -280,13 +280,13 @@ pub struct AddCertificate {

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct RemoveCertificate {
pub address: SocketAddr,
pub address: String,
pub fingerprint: CertificateFingerprint,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ReplaceCertificate {
pub address: SocketAddr,
pub address: String,
pub new_certificate: CertificateAndKey,
pub old_fingerprint: CertificateFingerprint,
#[serde(skip_serializing_if = "Vec::is_empty", default = "Vec::new")]
Expand Down
30 changes: 22 additions & 8 deletions command/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,14 @@ impl ConfigState {
.with_context(|| "cannot calculate the certificate's fingerprint")?,
);

let address = add
.address
.parse()
.with_context(|| "Could not parse socket address")?;

let entry = self
.certificates
.entry(add.address)
.entry(address)
.or_insert_with(HashMap::new);

if entry.contains_key(&fingerprint) {
Expand All @@ -345,7 +350,11 @@ impl ConfigState {
}

fn remove_certificate(&mut self, remove: &RemoveCertificate) -> anyhow::Result<()> {
if let Some(index) = self.certificates.get_mut(&remove.address) {
let address = remove
.address
.parse()
.with_context(|| "Could not parse socket address")?;
if let Some(index) = self.certificates.get_mut(&address) {
index.remove(&remove.fingerprint);
}

Expand All @@ -357,8 +366,13 @@ impl ConfigState {
/// - insert the new certificate with the new fingerprint as key
/// - check that the new entry is present in the certificates hashmap
fn replace_certificate(&mut self, replace: &ReplaceCertificate) -> anyhow::Result<()> {
let address = replace
.address
.parse()
.with_context(|| "Could not parse socket address")?;

self.certificates
.get_mut(&replace.address)
.get_mut(&address)
.with_context(|| format!("No certificate to replace for address {}", replace.address))?
.remove(&replace.old_fingerprint);

Expand All @@ -367,7 +381,7 @@ impl ConfigState {
.with_context(|| "cannot obtain the certificate's fingerprint")?,
);

self.certificates.get_mut(&replace.address).map(|certs| {
self.certificates.get_mut(&address).map(|certs| {
certs.insert(
new_fingerprint.clone(),
(replace.new_certificate.clone(), replace.new_names.clone()),
Expand All @@ -376,7 +390,7 @@ impl ConfigState {

if !self
.certificates
.get(&replace.address)
.get(&address)
.with_context(|| {
"Unlikely error. This entry in the certificate hashmap should be present"
})?
Expand Down Expand Up @@ -502,7 +516,7 @@ impl ConfigState {
for (front, certs) in self.certificates.iter() {
for (certificate_and_key, names) in certs.values() {
v.push(Request::AddCertificate(AddCertificate {
address: *front,
address: front.to_string(),
certificate: certificate_and_key.clone(),
names: names.clone(),
expired_at: None,
Expand Down Expand Up @@ -915,7 +929,7 @@ impl ConfigState {

for &(address, fingerprint) in removed_certificates {
v.push(Request::RemoveCertificate(RemoveCertificate {
address,
address: address.to_string(),
fingerprint: fingerprint.clone(),
}));
}
Expand All @@ -927,7 +941,7 @@ impl ConfigState {
.and_then(|certs| certs.get(fingerprint))
{
v.push(Request::AddCertificate(AddCertificate {
address,
address: address.to_string(),
certificate: certificate_and_key.clone(),
names: names.clone(),
expired_at: None,
Expand Down
2 changes: 1 addition & 1 deletion e2e/src/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ pub fn try_tls_endpoint() -> State {
versions: vec![],
};
let add_certificate = AddCertificate {
address: front_address,
address: front_address.to_string(),
certificate: certificate_and_key,
names: vec![],
expired_at: None,
Expand Down
9 changes: 6 additions & 3 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,10 +1122,11 @@ impl HttpsProxy {
&mut self,
add_certificate: AddCertificate,
) -> anyhow::Result<Option<ProxyResponseContent>> {
let address = add_certificate.address.parse()?;
match self
.listeners
.values()
.find(|l| l.borrow().address == add_certificate.address)
.find(|l| l.borrow().address == address)
{
Some(listener) => listener
.borrow_mut()
Expand All @@ -1145,10 +1146,11 @@ impl HttpsProxy {
&mut self,
remove_certificate: RemoveCertificate,
) -> anyhow::Result<Option<ProxyResponseContent>> {
let address = remove_certificate.address.parse()?;
match self
.listeners
.values()
.find(|l| l.borrow().address == remove_certificate.address)
.find(|l| l.borrow().address == address)
{
Some(listener) => listener
.borrow_mut()
Expand All @@ -1168,10 +1170,11 @@ impl HttpsProxy {
&mut self,
replace_certificate: ReplaceCertificate,
) -> anyhow::Result<Option<ProxyResponseContent>> {
let address = replace_certificate.address.parse()?;
match self
.listeners
.values()
.find(|l| l.borrow().address == replace_certificate.address)
.find(|l| l.borrow().address == address)
{
Some(listener) => listener
.borrow_mut()
Expand Down
20 changes: 10 additions & 10 deletions lib/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ mod tests {

#[test]
fn lifecycle() -> Result<(), Box<dyn Error + Send + Sync>> {
let address = "127.0.0.1:8080".parse()?;
let address = "127.0.0.1:8080".to_string();
let mut resolver = GenericCertificateResolver::new();
let certificate_and_key = CertificateAndKey {
certificate: String::from(include_str!("../assets/certificate.pem")),
Expand All @@ -674,7 +674,7 @@ mod tests {
.map_err(|err| GenericCertificateResolverError::PemParseError(err.to_string()))?;

let fingerprint = resolver.add_certificate(&AddCertificate {
address,
address: address.clone(),
certificate: certificate_and_key,
names: vec![],
expired_at: None,
Expand Down Expand Up @@ -711,7 +711,7 @@ mod tests {

#[test]
fn name_override() -> Result<(), Box<dyn Error + Send + Sync>> {
let address = "127.0.0.1:8080".parse()?;
let address = "127.0.0.1:8080".to_string();
let mut resolver = GenericCertificateResolver::new();
let certificate_and_key = CertificateAndKey {
certificate: String::from(include_str!("../assets/certificate.pem")),
Expand All @@ -724,7 +724,7 @@ mod tests {
.map_err(|err| GenericCertificateResolverError::PemParseError(err.to_string()))?;

let fingerprint = resolver.add_certificate(&AddCertificate {
address,
address: address.clone(),
certificate: certificate_and_key,
names: vec!["localhost".into(), "lolcatho.st".into()],
expired_at: None,
Expand Down Expand Up @@ -772,7 +772,7 @@ mod tests {

#[test]
fn replacement() -> Result<(), Box<dyn Error + Send + Sync>> {
let address = "127.0.0.1:8080".parse()?;
let address = "127.0.0.1:8080".to_string();
let mut resolver = GenericCertificateResolver::new();

// ---------------------------------------------------------------------
Expand All @@ -789,7 +789,7 @@ mod tests {

let names_1y = resolver.certificate_names(&pem)?;
let fingerprint_1y = resolver.add_certificate(&AddCertificate {
address,
address: address.clone(),
certificate: certificate_and_key_1y,
names: vec![],
expired_at: None,
Expand Down Expand Up @@ -843,7 +843,7 @@ mod tests {

#[test]
fn expiration_override() -> Result<(), Box<dyn Error + Send + Sync>> {
let address = "127.0.0.1:8080".parse()?;
let address = "127.0.0.1:8080".to_string();
let mut resolver = GenericCertificateResolver::new();

// ---------------------------------------------------------------------
Expand All @@ -860,7 +860,7 @@ mod tests {

let names_1y = resolver.certificate_names(&pem)?;
let fingerprint_1y = resolver.add_certificate(&AddCertificate {
address,
address: address.clone(),
certificate: certificate_and_key_1y,
names: vec![],
expired_at: Some(
Expand Down Expand Up @@ -972,12 +972,12 @@ mod tests {

// ---------------------------------------------------------------------
// load certificates in resolver
let address = "127.0.0.1:8080".parse()?;
let address = "127.0.0.1:8080".to_string();
let mut resolver = GenericCertificateResolver::default();

for certificate in &certificates {
resolver.add_certificate(&AddCertificate {
address,
address: address.clone(),
certificate: certificate.to_owned(),
names: vec![],
expired_at: None,
Expand Down

0 comments on commit 2516e3b

Please sign in to comment.