Skip to content

Commit 8895cd7

Browse files
amphihertrste
authored andcommitted
vm-migration: tls: remove every unwrap
Removes all the unwraps from the TLS code to make sure the VMM doesn't panic. On-behalf-of: SAP [email protected] Signed-off-by: Sebastian Eydam <[email protected]>
1 parent 2041809 commit 8895cd7

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

vm-migration/src/tls.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ pub enum TlsError {
3535

3636
#[error("Error during TLS handshake: {0}")]
3737
HandshakeError(String),
38+
39+
#[error("Error handling PEM file")]
40+
RustlsPemError(#[from] rustls::pki_types::pem::Error),
3841
}
3942

4043
// This TlsStream will be later encapsulated in a SocketStream. Thus it has to
@@ -186,19 +189,19 @@ pub struct TlsConnectionWrapper {
186189
}
187190

188191
impl TlsConnectionWrapper {
189-
pub fn new(cert_dir: &Path) -> Self {
192+
pub fn new(cert_dir: &Path) -> Result<Self, MigratableError> {
190193
let certs = CertificateDer::pem_file_iter(cert_dir.join("server-cert.pem"))
191-
.unwrap()
192-
.map(|cert| cert.unwrap())
193-
.collect();
194-
let key = PrivateKeyDer::from_pem_file(cert_dir.join("server-key.pem")).unwrap();
194+
.map_err(TlsError::RustlsPemError)?
195+
.map(|cert| cert.map_err(TlsError::RustlsPemError))
196+
.collect::<Result<Vec<CertificateDer<'_>>, TlsError>>()?;
197+
let key = PrivateKeyDer::from_pem_file(cert_dir.join("server-key.pem"))
198+
.map_err(TlsError::RustlsPemError)?;
195199
let config = ServerConfig::builder()
196200
.with_no_client_auth()
197201
.with_single_cert(certs, key)
198-
.map_err(TlsError::RustlsError)
199-
.unwrap();
202+
.map_err(TlsError::RustlsError)?;
200203
let config = Arc::new(config);
201-
Self { config }
204+
Ok(Self { config })
202205
}
203206

204207
pub fn wrap(
@@ -232,8 +235,9 @@ pub fn client_stream(
232235
let mut root_store = RootCertStore::empty();
233236
root_store.add_parsable_certificates(
234237
CertificateDer::pem_file_iter(cert_dir.join("ca-cert.pem"))
235-
.expect("Cannot open CA file")
236-
.map(|result| result.unwrap()),
238+
.map_err(TlsError::RustlsPemError)?
239+
.map(|cert| cert.map_err(TlsError::RustlsPemError))
240+
.collect::<Result<Vec<CertificateDer<'_>>, TlsError>>()?,
237241
);
238242
let config = ClientConfig::builder()
239243
.with_root_certificates(root_store)

vmm/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ fn receive_migration_listener(
14881488
} else {
14891489
Ok(ReceiveListener::Tls(
14901490
listener,
1491-
TlsConnectionWrapper::new(receiver_data_migration.tls_dir.as_ref().unwrap()),
1491+
TlsConnectionWrapper::new(receiver_data_migration.tls_dir.as_ref().unwrap())?,
14921492
))
14931493
}
14941494
} else if let Some(path) = receiver_data_migration.receiver_url.strip_prefix("unix:") {

0 commit comments

Comments
 (0)