Skip to content

Commit

Permalink
Add defaults for --acme-cache and --acme-domain (#364)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Aug 23, 2022
1 parent fb810a3 commit 36de18e
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 14 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ rustls-acme = { version = "0.4.0-beta2", features = ["axum"] }
serde = { version = "1.0.137", features = ["derive"] }
serde_cbor = "0.11.2"
serde_json = "1.0.81"
sys-info = "0.9.1"
tokio = { version = "1.17.0", features = ["rt-multi-thread"] }
tokio-stream = "0.1.9"
tokio-util = {version = "0.7.3", features = ["compat"] }
Expand Down
2 changes: 0 additions & 2 deletions deploy/ord.service
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ ExecStart=/usr/local/bin/ord \
--max-index-size 1TiB \
--chain ${CHAIN} \
server \
--acme-cache /var/lib/ord/acme-cache \
--acme-contact mailto:casey@rodarmor.com \
--acme-domain signet.ordinals.com \
--https-port 443

# Process management
Expand Down
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ deploy branch chain domain:
rsync -avz deploy/checkout root@{{domain}}:deploy/checkout
ssh root@{{domain}} 'cd deploy && ./checkout {{branch}} {{chain}} {{domain}}'

deploy-mainnet branch="master": (deploy branch "signet" "signet.ordinals.com")
deploy-mainnet branch="master": (deploy branch "main" "ordinals.com")

deploy-signet branch="master": (deploy branch "main" "ordinals.com")
deploy-signet branch="master": (deploy branch "signet" "signet.ordinals.com")

log unit domain="signet.ordinals.com":
ssh root@{{domain}} 'journalctl -fu {{unit}}'
Expand Down
75 changes: 65 additions & 10 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ pub(crate) struct Server {
long,
group = "port",
help = "Listen on <HTTPS_PORT> for incoming HTTPS requests.",
requires_all = &["acme-cache", "acme-domain", "acme-contact"]
requires = "acme-contact"
)]
https_port: Option<u16>,
#[structopt(long, help = "Store ACME TLS certificates in <ACME_CACHE>.")]
Expand Down Expand Up @@ -127,7 +127,7 @@ impl Server {

let server = axum_server::Server::bind(addr).handle(handle);

match self.acceptor() {
match self.acceptor(&options)? {
Some(acceptor) => {
server
.acceptor(acceptor)
Expand All @@ -145,13 +145,30 @@ impl Server {
self.http_port.or(self.https_port).unwrap_or(80)
}

fn acceptor(&self) -> Option<AxumAcceptor> {
fn acme_cache(acme_cache: Option<&PathBuf>, options: &Options) -> Result<PathBuf> {
if let Some(acme_cache) = acme_cache {
Ok(acme_cache.clone())
} else {
Ok(options.data_dir()?.join("acme-cache"))
}
}

fn acme_domains(acme_domain: &Vec<String>) -> Result<Vec<String>> {
if !acme_domain.is_empty() {
Ok(acme_domain.clone())
} else {
Ok(vec![sys_info::hostname()?])
}
}

fn acceptor(&self, options: &Options) -> Result<Option<AxumAcceptor>> {
if self.https_port.is_some() {
let config = AcmeConfig::new(&self.acme_domain)
let config = AcmeConfig::new(Self::acme_domains(&self.acme_domain)?)
.contact(&self.acme_contact)
.cache_option(Some(DirCache::new(
self.acme_cache.as_ref().unwrap().clone(),
)))
.cache_option(Some(DirCache::new(Self::acme_cache(
self.acme_cache.as_ref(),
options,
)?)))
.directory(if cfg!(test) {
LETS_ENCRYPT_STAGING_DIRECTORY
} else {
Expand All @@ -176,9 +193,9 @@ impl Server {
}
});

Some(acceptor)
Ok(Some(acceptor))
} else {
None
Ok(None)
}
}

Expand Down Expand Up @@ -458,7 +475,7 @@ mod tests {
.to_string();

assert!(
err.starts_with("error: The following required arguments were not provided:\n --acme-cache <ACME_CACHE>\n --acme-domain <ACME_DOMAIN>\n --acme-contact <ACME_CONTACT>\n"),
err.starts_with("error: The following required arguments were not provided:\n --acme-contact <ACME_CONTACT>\n"),
"{}",
err
);
Expand Down Expand Up @@ -497,4 +514,42 @@ mod tests {
])
.is_ok());
}

#[test]
fn acme_cache_defaults_to_data_dir() {
let arguments = Arguments::try_parse_from(&["ord", "--data-dir", "foo", "server"]).unwrap();
let acme_cache = Server::acme_cache(None, &arguments.options)
.unwrap()
.display()
.to_string();
assert!(acme_cache.contains("foo/acme-cache"), "{acme_cache}")
}

#[test]
fn acme_cache_flag_is_respected() {
let arguments =
Arguments::try_parse_from(&["ord", "--data-dir", "foo", "server", "--acme-cache", "bar"])
.unwrap();
let acme_cache = Server::acme_cache(Some(&"bar".into()), &arguments.options)
.unwrap()
.display()
.to_string();
assert_eq!(acme_cache, "bar")
}

#[test]
fn acme_domain_defaults_to_hostname() {
assert_eq!(
Server::acme_domains(&Vec::new()).unwrap(),
&[sys_info::hostname().unwrap()]
);
}

#[test]
fn acme_domain_flag_is_respected() {
assert_eq!(
Server::acme_domains(&vec!["example.com".into()]).unwrap(),
&["example.com"]
);
}
}

0 comments on commit 36de18e

Please sign in to comment.