-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
agent: split SSL/TLS entries by service
Each service has different styles of configuring the SSL/TLS server key-certificate pair for the domain. Split in separate files for easier handling. Signed-off-by: Alexandru Ciobotaru <alcioa@amazon.com>
- Loading branch information
Showing
2 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
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,68 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::mngtok::Error; | ||
use nix::unistd; | ||
use std::fs::OpenOptions; | ||
use std::io::{BufRead, BufReader, Write}; | ||
use std::os::unix::fs::OpenOptionsExt; | ||
use std::os::unix::io::AsRawFd; | ||
|
||
pub struct HttpdService {} | ||
|
||
impl HttpdService { | ||
pub fn write_tls_entries( | ||
path: &str, | ||
uid: Option<nix::unistd::Uid>, | ||
gid: Option<nix::unistd::Gid>, | ||
key_uri: &str, | ||
cert_path: Option<String>, | ||
) -> Result<(), Error> { | ||
let mut to_write = String::new(); | ||
OpenOptions::new() | ||
.read(true) | ||
.open(path) | ||
.map_err(Error::TargetIoError) | ||
.and_then(|file| { | ||
let reader = BufReader::new(&file); | ||
for line in reader.lines() { | ||
if let Ok(l) = line { | ||
let nl = { | ||
if l.starts_with("SSLCertificateKeyFile") { | ||
format!("SSLCertificateKeyFile \"{}\"", key_uri) | ||
} else if l.starts_with("SSLCertificateFile") { | ||
match cert_path { | ||
Some(ref cp) => format!("SSLCertificateFile \"{}\"", cp), | ||
None => l, | ||
} | ||
} else { | ||
l | ||
} | ||
}; | ||
to_write.push_str(&nl); | ||
to_write.push_str("\n"); | ||
} | ||
} | ||
Ok(()) | ||
})?; | ||
|
||
OpenOptions::new() | ||
.write(true) | ||
.truncate(true) | ||
.mode(0o440) | ||
.open(path) | ||
.map_err(Error::TargetIoError) | ||
.and_then(|mut file| { | ||
unistd::fchown(file.as_raw_fd(), uid, gid).map_err(Error::NixError)?; | ||
nix::sys::stat::fchmod( | ||
file.as_raw_fd(), | ||
// Safe becase 0o440 is valid. | ||
unsafe { nix::sys::stat::Mode::from_bits_unchecked(0o440) }, | ||
) | ||
.map_err(Error::NixError)?; | ||
write!(file, "{}", to_write).map_err(Error::TargetIoError)?; | ||
Ok(()) | ||
})?; | ||
Ok(()) | ||
} | ||
} |
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,45 @@ | ||
// Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use super::mngtok::Error; | ||
use nix::unistd; | ||
use std::fs::OpenOptions; | ||
use std::io::Write; | ||
use std::os::unix::fs::OpenOptionsExt; | ||
use std::os::unix::io::AsRawFd; | ||
|
||
pub struct NginxService {} | ||
|
||
impl NginxService { | ||
pub fn write_tls_entries( | ||
path: &str, | ||
uid: Option<nix::unistd::Uid>, | ||
gid: Option<nix::unistd::Gid>, | ||
key_uri: &str, | ||
cert_path: Option<String>, | ||
) -> Result<(), Error> { | ||
OpenOptions::new() | ||
.write(true) | ||
.create(true) | ||
.truncate(true) | ||
.mode(0o440) | ||
.open(path) | ||
.map_err(Error::TargetIoError) | ||
.and_then(|mut file| { | ||
unistd::fchown(file.as_raw_fd(), uid, gid).map_err(Error::NixError)?; | ||
nix::sys::stat::fchmod( | ||
file.as_raw_fd(), | ||
// Safe becase 0o440 is valid. | ||
unsafe { nix::sys::stat::Mode::from_bits_unchecked(0o440) }, | ||
) | ||
.map_err(Error::NixError)?; | ||
write!(file, "ssl_certificate_key \"engine:pkcs11:{}\";\n", key_uri) | ||
.map_err(Error::TargetIoError)?; | ||
if let Some(cp) = cert_path { | ||
write!(file, "ssl_certificate \"{}\";\n", cp).map_err(Error::TargetIoError)?; | ||
} | ||
Ok(()) | ||
})?; | ||
Ok(()) | ||
} | ||
} |