Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions plugins/rest-plugin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
env, fs,
net::SocketAddr,
str::FromStr,
sync::{Arc, Mutex},
Expand Down Expand Up @@ -165,12 +166,25 @@ async fn run_rest_server(

match clnrest_options.protocol {
ClnrestProtocol::Https => {
let max_retries = 10;
let mut retries = 0;
while retries < max_retries && !do_certificates_exist(&clnrest_options.certs) {
log::debug!("Certificates incomplete. Retrying...");
time::sleep(Duration::from_millis(500)).await;
retries += 1;
// Only wait for cln-grpc to finish creating certificates when using
// the default lightning directory; a custom clnrest-certs directory
// (including one that does not exist yet) is never populated by
// cln-grpc.
let is_default_certs_dir = match (
fs::canonicalize(&clnrest_options.certs),
fs::canonicalize(env::current_dir()?),
) {
(Ok(certs), Ok(cwd)) => certs == cwd,
_ => false,
};
if is_default_certs_dir {
let max_retries = 50;
let mut retries = 0;
while retries < max_retries && !do_certificates_exist(&clnrest_options.certs) {
log::debug!("Certificates incomplete. Retrying...");
time::sleep(Duration::from_millis(100)).await;
retries += 1;
}
}

if !do_certificates_exist(&clnrest_options.certs) {
Expand Down
25 changes: 25 additions & 0 deletions tests/test_clnrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,31 @@ def test_options(node_factory):
assert l1.daemon.is_in_log(r'plugin-clnrest: Killing plugin: disabled itself at init: `clnrest-host` should be a valid IP.')


def test_custom_certs_no_retry_wait(node_factory):
"""Custom --clnrest-certs skips waiting for cln-grpc certificate generation."""
rest_port = str(node_factory.get_unused_port())
rest_certs = node_factory.directory + '/custom-clnrest-certs'
l1 = node_factory.get_node(options={
'clnrest-port': rest_port,
'clnrest-certs': rest_certs,
})
l1.daemon.logsearch_start = 0
l1.daemon.wait_for_log(f'plugin-clnrest: REST server running at https://127.0.0.1:{rest_port}')
assert (Path(rest_certs) / 'ca.pem').exists()
assert not l1.daemon.is_in_log('Certificates incomplete. Retrying')

# A relative path resolving to the default lightning directory must still
# wait for cln-grpc rather than racing it.
rest_port2 = str(node_factory.get_unused_port())
l2 = node_factory.get_node(options={
'disable-plugin': 'cln-grpc',
'clnrest-port': rest_port2,
'clnrest-certs': '.',
})
l2.daemon.logsearch_start = 0
l2.daemon.wait_for_log('Certificates incomplete. Retrying')


def test_http_headers(node_factory):
"""Test HTTP headers set with `clnrest-csp` and `clnrest-cors-origins` options."""
# start a node with clnrest
Expand Down
Loading