Skip to content

Commit

Permalink
Add a default certificate when none are found for a host
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi DEMOLIS <[email protected]>
  • Loading branch information
Wonshtrum committed Oct 27, 2023
1 parent 72e9d44 commit bf026ee
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ socket2 = { version = "^0.5.4", features = ["all"] }
thiserror = "^1.0.49"
time = "^0.3.29"
x509-parser = "^0.15.1"
once_cell = "1.18.0"

sozu-command-lib = { path = "../command", version = "^0.15.10" }

Expand Down
4 changes: 2 additions & 2 deletions lib/src/https.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
cell::RefCell,
collections::{hash_map::Entry, BTreeMap, HashMap},
io::{ErrorKind, Read},
io::ErrorKind,
net::{Shutdown, SocketAddr as StdSocketAddr},
os::unix::{io::AsRawFd, net::UnixStream},
rc::{Rc, Weak},
Expand Down Expand Up @@ -394,7 +394,7 @@ impl ProxySession for HttpsSession {
return;
}

debug!("Closing HTTPS session");
trace!("Closing HTTPS session");
self.metrics.service_stop();

// Restore gauges
Expand Down
2 changes: 1 addition & 1 deletion lib/src/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,7 @@ impl ProxySession for TcpSession {
}

// TODO: the state should handle the timeouts
info!("Closing TCP session");
trace!("Closing TCP session");
self.metrics.service_stop();

// Restore gauges
Expand Down
23 changes: 21 additions & 2 deletions lib/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
sync::{Arc, Mutex},
};

use once_cell::sync::Lazy;
use rustls::{
server::{ClientHello, ResolvesServerCert},
sign::{CertifiedKey, RsaSigningKey},
Expand All @@ -25,6 +26,20 @@ use sozu_command::{

use crate::router::trie::{Key, KeyValue, TrieNode};

// -----------------------------------------------------------------------------
// Default ParsedCertificateAndKey

static DEFAULT_CERTIFICATE: Lazy<ParsedCertificateAndKey> = Lazy::new(|| {
let certificate_and_key = CertificateAndKey {
certificate: include_str!("../assets/certificate.pem").to_string(),
certificate_chain: vec![include_str!("../assets/certificate_chain.pem").to_string()],
key: include_str!("../assets/key.pem").to_string(),
versions: vec![],
names: vec![],
};
GenericCertificateResolver::parse(&certificate_and_key).unwrap()
});

// -----------------------------------------------------------------------------
// CertificateResolver trait

Expand Down Expand Up @@ -536,8 +551,12 @@ impl ResolvesServerCert for MutexWrappedCertificateResolver {
}
}

error!("could not look up a certificate for server name '{}'", name);
None
// error!("could not look up a certificate for server name '{}'", name);
// This certificate is used for TLS tunneling with another TLS termination endpoint
// Note that this is unsafe and you should provide a valid certificate
debug!("Default certificate is used for {}", name);
incr!("tls.default_cert_used");
Self::generate_certified_key(&DEFAULT_CERTIFICATE).map(Arc::new)
}
}

Expand Down

0 comments on commit bf026ee

Please sign in to comment.