Skip to content

Commit

Permalink
Merge pull request #12 from shubhexists/4-print-success-messsges-also
Browse files Browse the repository at this point in the history
feat(logs): added success logs
  • Loading branch information
shubhexists authored Sep 5, 2024
2 parents 178146f + 85ae1e8 commit 2157718
Show file tree
Hide file tree
Showing 14 changed files with 530 additions and 119 deletions.
11 changes: 11 additions & 0 deletions core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ readme = "README.md"
openssl = "0.10"
clap = { version = "4.4.8", features = ["derive"] }
dirs = "5.0"
lazy_static = "1.4"
lazy_static = "1.4"
colored = "2.0"
213 changes: 187 additions & 26 deletions core/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ use crate::{
Certificate,
},
};
use colored::*;
use openssl::{
pkey::{PKey, Private},
x509::{X509Req, X509},
};
use std::error;
use std::{
error::{self, Error},
path::PathBuf,
};

pub fn generate(
domains: Vec<String>,
Expand All @@ -28,58 +32,136 @@ pub fn generate(
request: bool,
install: bool,
) -> Result<(), Box<dyn error::Error>> {
println!();
if request {
println!("Generated Certificate Requests for :");
for domain in &domains {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
let (ca_req_certificate, private_key) =
CAReq::new(distinguished_name)?.generate_certificate()?;
save_csr_certificate(domain.to_string(), &output, ca_req_certificate, private_key)?;
let is_saved: Result<PathBuf, Box<dyn Error>> =
save_csr_certificate(domain.to_string(), &output, ca_req_certificate, private_key);
match is_saved {
Ok(path) => {
println!(" - \"{}\" ✅", domain);
println!();
println!(
"{}: Your request certs and their corresponding keys are saved at: {:?}",
"Note".green(),
path
);
}
Err(_err) => {
println!(" - \"{}\" ❌", domain);
}
}
}
println!();
return Ok(());
}

if let Some(certfile) = certfile {
if let Some(keyfile) = keyfile {
let (cert, pkey) = CACert::load_ca_cert(&certfile, &keyfile)?;
let (cert, pkey) = match CACert::load_ca_cert(&certfile, &keyfile) {
Ok(cert_pkey) => cert_pkey,
Err(err) => {
eprintln!("{}", err);
std::process::exit(1);
}
};
if let Some(csr) = &csr {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
let csr_object: X509Req = CAReq::read_csr_from_file(csr)?;
let csr_object: X509Req = match CAReq::read_csr_from_file(csr) {
Ok(csr) => csr,
Err(err) => {
eprintln!("{}", err);
std::process::exit(1);
}
};
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, _private_key) = LeafCert::generate_certificate(
println!();
let (leaf_certificate, _private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&cert,
&pkey,
Some(&csr_object),
)?;
save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?;
) {
Ok((a, b)) => {
println!("Generating Certificate for Signing Request Successful! 👍");
(a, b)
}
Err(err) => {
println!("Generating Certificate for Signing Request Failed! 👎");
eprintln!("{}", err);
std::process::exit(1);
}
};
match save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate) {
Ok(()) => {}
Err(err) => {
println!("{}", err);
std::process::exit(1);
}
};
} else {
println!();
println!("Generated Certificate for : ");
for domain in &domains {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, private_key) =
LeafCert::generate_certificate(leaf_cert_object, &cert, &pkey, None)?;
let (leaf_certificate, private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&cert,
&pkey,
None,
) {
Ok((a, b)) => {
println!(" - \"{}\" ✅", domain);
(a, b)
}
Err(err) => {
println!(" - \"{}\" ❌", domain);
eprintln!("{}", err);
std::process::exit(1);
}
};
if let Some(private_key) = private_key {
save_pem_key_pair(
match save_pem_key_pair(
&output,
leaf_certificate,
domain.to_string(),
private_key,
)?;
) {
Ok(()) => {}
Err(err) => {
println!("{}", err);
}
};
} else {
eprintln!(
"Oops! We lost your private key for domain {}. Please try again!",
domain
"{}{}{}",
"Oops! We lost your private key for domain ".yellow(),
domain.yellow(),
". Please try again!".yellow()
)
}
}
println!();
println!(
"{}: All Successful Certificates and their corresponding keys are saved at : {}",
"Note".green(),
output.unwrap()
);
}
if install {}
println!();
return Ok(());
} else {
eprintln!("Corresponding KeyFile Not Found");
eprintln!("{}: Corresponding KeyFile Not Found", "Error".red());
println!();
std::process::exit(1);
}
}
Expand All @@ -91,20 +173,47 @@ pub fn generate(
create_distinguished_name(&commonname, &country, &state);
let csr_object: X509Req = CAReq::read_csr_from_file(csr)?;
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, _private_key) = LeafCert::generate_certificate(
println!();
let (leaf_certificate, _private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&d_cert,
&d_pkey,
Some(&csr_object),
)?;
) {
Ok((a, b)) => {
println!("Generating Certificate for Signing Request Successful! 👍");
(a, b)
}
Err(err) => {
println!("Generating Certificate for Signing Request Failed! 👎");
eprintln!("{}", err);
std::process::exit(1);
}
};
save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?;
} else {
println!();
println!("Generated Certificate for : ");
for domain in &domains {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, private_key) =
LeafCert::generate_certificate(leaf_cert_object, &d_cert, &d_pkey, None)?;
let (leaf_certificate, private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&d_cert,
&d_pkey,
None,
) {
Ok((a, b)) => {
println!(" - \"{}\" ✅", domain);
(a, b)
}
Err(err) => {
println!(" - \"{}\" ❌", domain);
eprintln!("{}", err);
std::process::exit(1);
}
};
if let Some(private_key) = private_key {
save_pem_key_pair(&output, leaf_certificate, domain.to_string(), private_key)?;
} else {
Expand All @@ -114,14 +223,23 @@ pub fn generate(
)
}
}
println!();
println!(
"{}: All Successful Certificates and their corresponding keys are saved at : {}",
"Note".green(),
output.unwrap()
);
}

if install {
generate_install(d_cert)?;
}
} else {
if noca {
eprintln!("Error: No CA Certificates found and generation of a new one is disabled by `--no-ca`");
eprintln!(
"{}: No CA Certificates found and generation of a new one is disabled by `--no-ca`",
"Error".red()
);
std::process::exit(1)
}
let distinguished_name: DistinguishedName =
Expand All @@ -134,37 +252,80 @@ pub fn generate(
create_distinguished_name(&commonname, &country, &state);
let csr_object: X509Req = CAReq::read_csr_from_file(csr)?;
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, _private_key) = LeafCert::generate_certificate(
println!();
let (leaf_certificate, _private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&created_cert,
&created_key,
Some(&csr_object),
)?;
) {
Ok((a, b)) => {
println!("Generating Certificate for Signing Request Successful! 👍");
(a, b)
}
Err(err) => {
println!("Generating Certificate for Signing Request Failed! 👎");
eprintln!("{}", err);
std::process::exit(1);
}
};
save_pem_certificate("csr_cert.pem".to_string(), output, leaf_certificate)?;
} else {
println!();
println!("Generated Certificate for : ");
for domain in &domains {
let distinguished_name: DistinguishedName =
create_distinguished_name(&commonname, &country, &state);
let leaf_cert_object: LeafCert = LeafCert::new(distinguished_name)?;
let (leaf_certificate, private_key) = LeafCert::generate_certificate(
let (leaf_certificate, private_key) = match LeafCert::generate_certificate(
leaf_cert_object,
&created_cert,
&created_key,
None,
)?;
) {
Ok((a, b)) => {
println!(" - \"{}\" ✅", domain);
(a, b)
}
Err(err) => {
println!(" - \"{}\" ❌", domain);
eprintln!("{}", err);
std::process::exit(1);
}
};
if let Some(private_key) = private_key {
save_pem_key_pair(&output, leaf_certificate, domain.to_string(), private_key)?;
match save_pem_key_pair(
&output,
leaf_certificate,
domain.to_string(),
private_key,
) {
Ok(()) => {}
Err(err) => {
println!("{}", err);
}
};
} else {
eprintln!(
"Oops! We lost your private key for domain {}. Please try again!",
domain
"{}{}{}",
"Oops! We lost your private key for domain ".yellow(),
domain.yellow(),
". Please try again!".yellow()
)
}
}

println!();
println!(
"{}: All Successful Certificates and their corresponding keys are saved at : {}",
"Note".green(),
output.unwrap()
);
}
if install {
generate_install(created_cert)?;
}
}
println!();
Ok(())
}
Loading

0 comments on commit 2157718

Please sign in to comment.