-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mTLS): adds mTLS support to dataplane api-server
- Loading branch information
1 parent
1f8d172
commit 06d22a3
Showing
14 changed files
with
584 additions
and
6 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
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,31 @@ | ||
/* | ||
Copyright 2023 The Kubernetes Authors. | ||
SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) | ||
*/ | ||
use clap::{Parser, Subcommand}; | ||
use std::path::PathBuf; | ||
|
||
#[derive(Debug, Subcommand)] | ||
pub enum TLSConfig { | ||
TLS(ServerOnlyTLSConfig), | ||
MutualTLS(MutualTLSConfig), | ||
} | ||
|
||
#[derive(Debug, Parser, Clone)] | ||
pub struct ServerOnlyTLSConfig { | ||
#[clap(short, long)] | ||
pub server_certificate_path: PathBuf, | ||
#[clap(short, long)] | ||
pub server_private_key_path: PathBuf, | ||
} | ||
|
||
#[derive(Debug, Parser, Clone)] | ||
pub struct MutualTLSConfig { | ||
#[clap(short, long)] | ||
pub server_certificate_path: PathBuf, | ||
#[clap(short, long)] | ||
pub server_private_key_path: PathBuf, | ||
#[clap(short, long)] | ||
pub client_certificate_authority_root_path: PathBuf, | ||
} |
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,87 @@ | ||
use anyhow::Result; | ||
use api_server::config::{MutualTLSConfig, ServerOnlyTLSConfig, TLSConfig}; | ||
use api_server::setup_tls; | ||
use rcgen::{generate_simple_self_signed, Certificate, CertificateParams}; | ||
use std::fs; | ||
use tempfile::tempdir; | ||
use tonic::transport::Server; | ||
|
||
#[tokio::test] | ||
async fn test_tls_self_signed_cert() -> Result<()> { | ||
// Create a temporary directory | ||
let temp_dir = tempdir().unwrap(); | ||
|
||
// Generate self-signed certificate | ||
let cert = generate_simple_self_signed(vec!["localhost".into()])?; | ||
let cert_pem = cert.serialize_pem()?; | ||
let key_pem = cert.serialize_private_key_pem(); | ||
|
||
// Paths for the server cert and private key | ||
let cert_path = temp_dir.path().join("server.crt"); | ||
let key_path = temp_dir.path().join("server.key"); | ||
|
||
// Write cert and key to temp files | ||
fs::write(&cert_path, cert_pem.as_bytes())?; | ||
fs::write(&key_path, key_pem.as_bytes())?; | ||
|
||
// Set up a TLS config with paths to the cert and key | ||
let tls_config = Some(TLSConfig::TLS(ServerOnlyTLSConfig { | ||
server_certificate_path: cert_path.clone(), | ||
server_private_key_path: key_path.clone(), | ||
})); | ||
|
||
// Prepare a dummy server builder | ||
let builder = Server::builder(); | ||
|
||
// Run the setup_tls function and ensure no error is thrown | ||
let result = setup_tls(builder, &tls_config); | ||
assert!( | ||
result.is_ok(), | ||
"setup_tls should succeed with valid self-signed certs" | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_mtls_self_signed_cert() -> Result<()> { | ||
// Create a temporary directory | ||
let temp_dir = tempdir().unwrap(); | ||
|
||
// Generate self-signed certificate | ||
let cert = generate_simple_self_signed(vec!["localhost".into()])?; | ||
let cert_pem = cert.serialize_pem()?; | ||
let key_pem = cert.serialize_private_key_pem(); | ||
|
||
// Generate CA | ||
let ca_params = CertificateParams::default(); | ||
let ca_cert = Certificate::from_params(ca_params)?; | ||
let ca_cert_pem = ca_cert.serialize_pem()?; | ||
|
||
// Cert file paths | ||
let cert_path = temp_dir.path().join("server.crt"); | ||
let key_path = temp_dir.path().join("server.key"); | ||
let ca_cert_path = temp_dir.path().join("ca.crt"); | ||
|
||
// Write cert and key to temp files | ||
fs::write(&cert_path, cert_pem.as_bytes())?; | ||
fs::write(&key_path, key_pem.as_bytes())?; | ||
fs::write(&ca_cert_path, ca_cert_pem.as_bytes())?; | ||
|
||
// Set up a TLS config with paths to the cert and key | ||
let tls_config = Some(TLSConfig::MutualTLS(MutualTLSConfig { | ||
server_certificate_path: cert_path.clone(), | ||
server_private_key_path: key_path.clone(), | ||
client_certificate_authority_root_path: ca_cert_path.clone(), | ||
})); | ||
|
||
// Prepare a dummy server builder | ||
let builder = Server::builder(); | ||
|
||
// Run the setup_tls function and ensure no error is thrown | ||
let result = setup_tls(builder, &tls_config); | ||
assert!( | ||
result.is_ok(), | ||
"setup_tls should succeed with valid self-signed certs" | ||
); | ||
Ok(()) | ||
} |
Oops, something went wrong.