-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
server: generate TLS certificates if not provided
If TLS is enabled, but not TLS key or certificate files are created, make them on the fly. If existing files exist, re-use them. Issue: #255
- Loading branch information
Showing
6 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// SPDX-FileCopyrightText: (C) 2023 Jason Ish <jason@codemonkey.net> | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
use crate::prelude::*; | ||
use anyhow::Result; | ||
use rcgen::{Certificate, CertificateParams, DistinguishedName, DnType}; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
|
||
static ORG_NAME: &str = "EveBox Server"; | ||
static CN_NAME: &str = "EveBox Server"; | ||
static CERT_FILENAME: &str = "cert.pem"; | ||
static KEY_FILENAME: &str = "key.pem"; | ||
|
||
pub(crate) fn create_and_write_cert<P: AsRef<Path>>(dir: P) -> Result<(PathBuf, PathBuf)> { | ||
let mut params: CertificateParams = Default::default(); | ||
params.not_before = rcgen::date_time_ymd(2023, 1, 1); | ||
params.not_after = rcgen::date_time_ymd(3023, 1, 1); | ||
params.distinguished_name = DistinguishedName::new(); | ||
params | ||
.distinguished_name | ||
.push(DnType::OrganizationName, ORG_NAME); | ||
params.distinguished_name.push(DnType::CommonName, CN_NAME); | ||
params.subject_alt_names = vec![rcgen::SanType::DnsName("localhost".to_string())]; | ||
let cert = Certificate::from_params(params)?; | ||
let dir = dir.as_ref(); | ||
let cert_path = dir.join(CERT_FILENAME); | ||
let key_path = dir.join(KEY_FILENAME); | ||
fs::write(&cert_path, cert.serialize_pem()?.as_bytes())?; | ||
fs::write(&key_path, cert.serialize_private_key_pem().as_bytes())?; | ||
Ok((cert_path, key_path)) | ||
} | ||
|
||
pub(crate) fn get_or_create_cert<P: AsRef<Path>>(dir: P) -> Result<(PathBuf, PathBuf)> { | ||
let dir = dir.as_ref(); | ||
let cert_path = dir.join(CERT_FILENAME); | ||
let key_path = dir.join(KEY_FILENAME); | ||
|
||
if cert_path.exists() && key_path.exists() { | ||
info!( | ||
"Found existing TLS certificate and key: {}, {}", | ||
cert_path.display(), | ||
key_path.display() | ||
); | ||
Ok((cert_path, key_path)) | ||
} else { | ||
let (cert_path, key_path) = create_and_write_cert(dir)?; | ||
info!( | ||
"Created new TLS certificate and key: {}, {}", | ||
cert_path.display(), | ||
key_path.display() | ||
); | ||
Ok((cert_path, key_path)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters