Skip to content

Commit

Permalink
Merge pull request #31 from dvdsk/fix-clippy
Browse files Browse the repository at this point in the history
Fix email and fix clippy lints
  • Loading branch information
dvdsk authored May 4, 2023
2 parents 34f622f + 8f45299 commit 2380091
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## [0.2.1] - 2023-05-05

### Fixes
- Email is now actually added to acme account info

## [0.2.0] - 2023-05-04

### Added
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

10 changes: 5 additions & 5 deletions main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
[package]
name = "renewc"
version = "0.2.0"
version = "0.2.1"
edition = "2021"
description = "Certificate renewal, with advanced diagnostics without installing anything"
license = "Apache-2.0 OR MIT"
license = "GNUv3"

[dependencies]
instant-acme = { version = "0.2", git = "https://github.com/instant-labs/instant-acme", rev="c9a7b9d" }
Expand Down Expand Up @@ -36,9 +36,9 @@ rand = "0.8"
owo-colors = { version = "3.5" }
yasna = "0.5"
der = "0.7"
async-trait = "0.1.68"
data-encoding = "2.3.3"
pem = "2.0.1"
async-trait = "0.1"
data-encoding = "2.3"
pem = "2"

[dev-dependencies]
libc = "0.2"
Expand Down
6 changes: 1 addition & 5 deletions main/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ impl<P: PemItem> Signed<P> {

let private_key = PemItem::from_pem(private_key, Label::PrivateKey)?;

Ok(Self {
private_key,
certificate,
chain,
})
Ok(Self { certificate, private_key, chain })
}
}

Expand Down
10 changes: 4 additions & 6 deletions main/src/cert/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl PemItem for Pem {
}

fn chain_into_bytes(pems: &[Self]) -> Vec<u8> {
pem::encode_many(&pems).into_bytes()
pem::encode_many(pems).into_bytes()
}

fn chain_from_pem(encoded: impl AsRef<[u8]>) -> eyre::Result<Vec<Self>> {
Expand Down Expand Up @@ -82,7 +82,7 @@ impl Der {
/// bytes must be valid der
#[must_use]
pub(crate) fn from_bytes(bytes: Vec<u8>) -> Self {
Self(bytes.into())
Self(bytes)
}

#[must_use]
Expand All @@ -107,17 +107,15 @@ mod tests {
let missing_linefeeds = "-----CERTIFICATE-----12oien23ie4n23you4n23h4oyu23l4en2348u7l234n23ein4o23n42h3yu4l23y432el4uy23l4e-----END CERTIFICATE-----";

for invalid in [one_dash_too_much, missing_begin, missing_linefeeds] {
let _ = Pem::from_pem(invalid.to_owned(), Label::Certificate).unwrap_err();
let _: eyre::Report = Pem::from_pem(invalid, Label::Certificate).unwrap_err();
}
}

#[test]
fn reversible() {
const ROOT_CA: &[u8] = "-----BEGIN CERTIFICATE-----\r\nMIIBkDCCATagAwIBAgIIHXJD3lzIXyMwCgYIKoZIzj0EAwIwITEfMB0GA1UEAwwW\r\ncmNnZW4gc2VsZiBzaWduZWQgY2VydDAgFw03NTAxMDEwMDAwMDBaGA8yNTAwMDQy\r\nMTE2NTk0OVowITEfMB0GA1UEAwwWcmNnZW4gc2VsZiBzaWduZWQgY2VydDBZMBMG\r\nByqGSM49AgEGCCqGSM49AwEHA0IABHtP92/H2wTvW/xZ9iSiCMnWOfaydoSWEGFi\r\nWPHBvTO0FyLEUxQKOOrunv071KrBbYECyX00Q5efWj46brjzjJajVjBUMCIGA1Ud\r\nEQQbMBmCF1NUQUdJTkcubGV0c2VuY3J5cHQub3JnMB0GA1UdDgQWBBQjX8hc3kNy\r\nHXuj5yHSZipVhCHtQDAPBgNVHRMBAf8EBTADAQH/MAoGCCqGSM49BAMCA0gAMEUC\r\nIQD7CpgwpL6KT3Ljedh5bL4x3LSY5guONLcWIfz2X9E8ngIgbrcaTmaryZfiYnnK\r\nETaDo04pY2cDOIsIy2ycUTJL084=\r\n-----END CERTIFICATE-----\r\n".as_bytes();

let der = Pem::from_pem(ROOT_CA.to_vec(), Label::Certificate)
.unwrap()
.der();
let der = Pem::from_pem(ROOT_CA, Label::Certificate).unwrap().der();
assert_ne!(der.clone().into_bytes(), ROOT_CA);
let pem: Pem = der.to_pem(Label::Certificate);
assert_eq!(pem.into_bytes(), ROOT_CA);
Expand Down
12 changes: 6 additions & 6 deletions main/src/cert/load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub enum Encoding {
}

impl Encoding {
pub(crate) fn extension(&self) -> &'static str {
pub(crate) fn extension(self) -> &'static str {
match self {
Encoding::PEM => "pem",
Encoding::DER => "der",
Expand All @@ -33,10 +33,10 @@ impl Encoding {
impl From<&Output> for Encoding {
fn from(output: &Output) -> Self {
match output {
Output::Pem => Encoding::PEM,
Output::PemSeperateKey => Encoding::PEM,
Output::PemSeperateChain => Encoding::PEM,
Output::PemAllSeperate => Encoding::PEM,
Output::Pem
| Output::PemSeperateKey
| Output::PemSeperateChain
| Output::PemAllSeperate => Encoding::PEM,
Output::Der => Encoding::DER,
}
}
Expand Down Expand Up @@ -158,7 +158,7 @@ fn load_certificate<P: PemItem>(config: &Config) -> eyre::Result<Option<MaybeSig
let encoding = Encoding::from(output);
let path = if certificate_path.is_dir() {
derive_path(
&certificate_path,
certificate_path,
&name(&config.domains)?,
"cert",
encoding.extension(),
Expand Down
10 changes: 5 additions & 5 deletions main/src/cert/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ fn write_cert(
match operation {
Operation::Append(path) => {
let mut file = fs::OpenOptions::new().append(true).open(path)?;
return file
file
.write_all(&bytes)
.wrap_err("Could not append signed certificate to pem file");
.wrap_err("Could not append signed certificate to pem file")
}
Operation::Create(path) => {
let mut file = fs::File::create(path)?;
Expand All @@ -53,9 +53,9 @@ fn write_key(
match operation {
Operation::Append(path) => {
let mut file = fs::OpenOptions::new().append(true).open(path)?;
return file
file
.write_all(&bytes)
.wrap_err("Could not append private key to pem file");
.wrap_err("Could not append private key to pem file")
}
Operation::Create(path) => {
let mut file = fs::File::create(path)
Expand Down Expand Up @@ -101,7 +101,7 @@ enum Operation<'a> {

#[instrument(level = "debug", skip(config, signed), ret)]
pub fn on_disk<P: PemItem>(config: &Config, signed: Signed<P>) -> eyre::Result<()> {
use Operation::*;
use Operation::{Append, Create};
let cert_path = cert_path(config)?;
let key_path = key_path(config)?;
let chain_path = chain_path(config)?;
Expand Down
2 changes: 1 addition & 1 deletion main/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ impl Config {
pub fn test(port: u16) -> Self {
Config {
domains: vec!["testdomain.org".into()],
email: vec!["test_email".into()],
email: vec!["[email protected]".into()],
production: false,
port,
output: args::OutputConfig::test(),
Expand Down
2 changes: 1 addition & 1 deletion main/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub async fn run<P: PemItem>(
}
};

match ui::precheck(config, cert, stdout) {
match ui::precheck(config, &cert, stdout) {
CheckResult::Refuse {
status: Some(status),
warning,
Expand Down
29 changes: 17 additions & 12 deletions main/src/renew.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::io::Read;
use std::string::String;
use std::time::Duration;

use color_eyre::eyre::{self, Context};
Expand All @@ -24,15 +25,25 @@ use server::Http01Challenge;
// Alternatively, restore an account from serialized credentials by
// using `Account::from_credentials()`.
#[tracing::instrument(skip_all)]
async fn account(production: bool) -> Result<Account, acme::Error> {
let url = if production {
async fn account(config: &Config) -> Result<Account, acme::Error> {
let url = if config.production {
LetsEncrypt::Production.url()
} else {
LetsEncrypt::Staging.url()
};
let contact: Vec<_> = config
.email
.iter()
.map(|addr| format!("mailto:{addr}"))
.collect();

Account::create(
&NewAccount {
contact: &[],
contact: contact
.iter()
.map(String::as_str)
.collect::<Vec<_>>()
.as_slice(),
terms_of_service_agreed: true,
only_return_existing: false,
},
Expand Down Expand Up @@ -150,17 +161,11 @@ fn prepare_sign_request(names: &[String]) -> Result<(Certificate, Vec<u8>), rcge

#[tracing::instrument(skip_all)]
pub async fn request<P: PemItem>(config: &Config, debug: bool) -> eyre::Result<Signed<P>> {
let Config {
domains: ref names,
production,
..
} = config;

let account = account(*production).await?;
let mut order = order(&account, names)
let account = account(config).await?;
let mut order = order(&account, &config.domains)
.await
.wrap_err("Certificate authority can not issue a certificate")
.with_note(|| format!("names: {names:?}"))?;
.with_note(|| format!("names: {:?}", config.domains))?;

let challenges = prepare_challenge(&mut order).await?;

Expand Down
2 changes: 1 addition & 1 deletion main/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl CheckResult {
}
}

pub fn precheck(config: &Config, cert: Info, stdout: &mut impl Write) -> CheckResult {
pub fn precheck(config: &Config, cert: &Info, stdout: &mut impl Write) -> CheckResult {
match (config.production, cert.staging, cert.should_renew()) {
(false, true, _) => {
CheckResult::accept( "Requesting staging cert, certificates will not be valid")
Expand Down
2 changes: 1 addition & 1 deletion main/tests/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ async fn staging_does_not_overwrite_production() {
assert!(
output.contains(end),
"stdout did not contain:\n\t{end:#?}\ninstead it was:\n\t{output:#?}"
)
);
}

#[tokio::test]
Expand Down
1 change: 0 additions & 1 deletion main/tests/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ async fn der_and_pem_equal() {
Output::PemAllSeperate,
Output::Der,
]
.into_iter()
{
config.output.output = dbg!(&format).clone();
store::on_disk(&config, original.clone()).unwrap();
Expand Down

0 comments on commit 2380091

Please sign in to comment.