Skip to content

Commit

Permalink
Merge pull request openwrt#19669 from hgl/acme
Browse files Browse the repository at this point in the history
acme: refactor
  • Loading branch information
tohojo authored Oct 24, 2022
2 parents e90b4c0 + 230c2d5 commit 14f151a
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 42 deletions.
40 changes: 23 additions & 17 deletions net/acme-acmesh/files/hook.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ACME=/usr/lib/acme/client/acme.sh
LOG_TAG=acme-acmesh
# webroot option deprecated, use the hardcoded value directly in the next major version
WEBROOT=${webroot:-/var/run/acme/challenge}
NOTIFY=/usr/lib/acme/notify

# shellcheck source=net/acme/files/functions.sh
. /usr/lib/acme/functions.sh
Expand All @@ -12,9 +13,7 @@ WEBROOT=${webroot:-/var/run/acme/challenge}
export CURL_CA_BUNDLE=/etc/ssl/certs/ca-certificates.crt
export NO_TIMESTAMP=1

cmd="$1"

case $cmd in
case $1 in
get)
set --
[ "$debug" = 1 ] && set -- "$@" --debug
Expand All @@ -38,20 +37,25 @@ get)
staging_moved=1
else
set -- "$@" --renew --home "$state_dir" -d "$main_domain"
log info "$*"
trap 'ACTION=renewed-failed hotplug-call acme;exit 1' INT
"$ACME" "$@"
log info "$ACME $*"
trap '$NOTIFY renew-failed;exit 1' INT
$ACME "$@"
status=$?
trap - INT

case $status in
0) ;; # renewed ok, handled by acme.sh hook, ignore.
2) ;; # renew skipped, ignore.
0)
$NOTIFY renewed
exit;;
2)
# renew skipped, ignore.
exit
;;
*)
ACTION=renew-failed hotplug-call acme
$NOTIFY renew-failed
exit 1
;;
esac
return 0
fi
fi

Expand Down Expand Up @@ -83,6 +87,9 @@ get)
elif [ "$calias" ]; then
set -- "$@" --challenge-alias "$calias"
fi
if [ "$dns_wait" ]; then
set -- "$@" --dnssleep "$dns_wait"
fi
elif [ "$standalone" = 1 ]; then
set -- "$@" --standalone --listen-v6
else
Expand All @@ -92,11 +99,11 @@ get)

set -- "$@" --issue --home "$state_dir"

log info "$*"
trap 'ACTION=issue-failed hotplug-call acme;exit 1' INT
log info "$ACME $*"
trap '$NOTIFY issue-failed;exit 1' INT
"$ACME" "$@" \
--pre-hook 'ACTION=prepare hotplug-call acme' \
--renew-hook 'ACTION=renewed hotplug-call acme'
--pre-hook "$NOTIFY prepare" \
--renew-hook "$NOTIFY renewed"
status=$?
trap - INT

Expand All @@ -106,7 +113,7 @@ get)
ln -s "$domain_dir/$main_domain.key" /etc/ssl/acme
ln -s "$domain_dir/fullchain.cer" "/etc/ssl/acme/$main_domain.fullchain.cer"
ln -s "$domain_dir/ca.cer" "/etc/ssl/acme/$main_domain.chain.cer"
ACTION=issued hotplug-call acme
$NOTIFY issued
;;
*)
if [ "$staging_moved" = 1 ]; then
Expand All @@ -117,8 +124,7 @@ get)
mv "$domain_dir" "$failed_dir"
log err "State moved to $failed_dir"
fi
ACTION=issue-failed hotplug-call acme
return 0
$NOTIFY issue-failed
;;
esac
;;
Expand Down
6 changes: 4 additions & 2 deletions net/acme-common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
include $(TOPDIR)/rules.mk

PKG_NAME:=acme-common
PKG_VERSION:=1.0.0
PKG_VERSION:=1.0.1

PKG_MAINTAINER:=Toke Høiland-Jørgensen <[email protected]>
PKG_LICENSE:=GPL-3.0-only
Expand All @@ -34,17 +34,19 @@ define Package/acme-common/conffiles
endef

define Package/acme-common/install
$(INSTALL_DIR) $(1)/etc/acme
$(INSTALL_DIR) $(1)/etc/ssl/acme
$(INSTALL_DIR) $(1)/etc/config
$(INSTALL_CONF) ./files/acme.config $(1)/etc/config/acme
$(INSTALL_DIR) $(1)/usr/bin
$(INSTALL_BIN) ./files/acme.sh $(1)/usr/bin/acme
$(INSTALL_DIR) $(1)/usr/lib/acme
$(INSTALL_DATA) ./files/functions.sh $(1)/usr/lib/acme
$(INSTALL_BIN) ./files/acme-notify.sh $(1)/usr/lib/acme/notify
$(INSTALL_DIR) $(1)/etc/init.d
$(INSTALL_BIN) ./files/acme.init $(1)/etc/init.d/acme
$(INSTALL_DIR) $(1)/etc/uci-defaults
$(INSTALL_DATA) ./files/acme.uci-defaults $(1)/etc/uci-defaults/acme
$(INSTALL_DIR) $(1)/etc/hotplug.d/acme
endef

define Package/acme/postinst
Expand Down
17 changes: 17 additions & 0 deletions net/acme-common/files/acme-notify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh
set -u

event="$1"

# Call hotplug first, giving scripts a chance to modify certificates before
# reloadaing the services
ACTION=$event hotplug-call acme

case $event in
renewed)
ubus call service event '{"type":"acme.renew","data":{}}'
;;
issued)
ubus call service event '{"type":"acme.issue","data":{}}'
;;
esac
4 changes: 2 additions & 2 deletions net/acme-common/files/acme.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ config acme

config cert 'example_wildcard'
option enabled 0
option use_staging 1
option staging 1
list domains example.org
list domains sub.example.org
list domains *.sub.example.org
Expand All @@ -17,6 +17,6 @@ config cert 'example_wildcard'

config cert 'example'
option enabled 0
option use_staging 1
option staging 1
list domains example.org
list domains sub.example.org
36 changes: 26 additions & 10 deletions net/acme-common/files/acme.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#
# Authors: Toke Høiland-Jørgensen <[email protected]>

export state_dir='/etc/acme'
export state_dir=/etc/acme
export account_email=
export debug=0
export challenge_dir='/var/run/acme/challenge'
export run_dir=/var/run/acme
NFT_HANDLE=
HOOK=/usr/lib/acme/hook
LOG_TAG=acme
Expand All @@ -23,6 +23,9 @@ LOG_TAG=acme

cleanup() {
log debug "cleaning up"
if [ -e $run_dir/lock ]; then
rm $run_dir/lock
fi
if [ "$NFT_HANDLE" ]; then
# $NFT_HANDLE contains the string 'handle XX' so pass it unquoted to nft
nft delete rule inet fw4 input $NFT_HANDLE
Expand All @@ -33,7 +36,7 @@ load_options() {
section=$1

# compatibility for old option name
config_get_bool use_staging "$section" staging
config_get_bool staging "$section" use_staging
if [ -z "$staging" ]; then
config_get_bool staging "$section" staging 0
fi
Expand All @@ -56,11 +59,13 @@ load_options() {
export days
config_get standalone "$section" standalone 0
export standalone
config_get dns_wait "$section" dns_wait
export dns_wait

config_get webroot "$section" webroot
export webroot
if [ "$webroot" ]; then
log warn "Option \"webroot\" is deprecated, please remove it and change your web server's config so it serves ACME challenge requests from /var/run/acme/challenge."
log warn "Option \"webroot\" is deprecated, please remove it and change your web server's config so it serves ACME challenge requests from $run_dir/challenge."
fi
}

Expand Down Expand Up @@ -112,6 +117,15 @@ load_globals() {
return 1
}

cmd_get() {
trap cleanup EXIT

config_load acme
config_foreach load_globals acme

config_foreach get_cert cert
}

usage() {
cat <<EOF
Usage: acme <command> [arguments]
Expand All @@ -128,12 +142,14 @@ fi

case $1 in
get)
config_load acme
config_foreach load_globals acme

mkdir -p /etc/ssl/acme
trap cleanup EXIT
config_foreach get_cert cert
mkdir -p $run_dir
{
if ! flock -n 200; then
log err "Another ACME instance is already running."
exit 1
fi
cmd_get "$@"
} 200>$run_dir/lock
;;
*)
usage
Expand Down
4 changes: 0 additions & 4 deletions net/haproxy/files/acme.hotplug
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,3 @@ issued|renewed)
>"/etc/ssl/acme/$main_domain.combined.cer"
;;
esac

if [ "$ACTION" = renewed ]; then
/etc/init.d/haproxy reload
fi
4 changes: 4 additions & 0 deletions net/haproxy/files/haproxy.init
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ start_service() {
procd_close_instance
}

service_triggers() {
procd_add_raw_trigger acme.renew 5000 /etc/init.d/haproxy reload
}

extra_command "check" "Check haproxy config"
check() {
$HAPROXY_BIN -c -q -V -f $HAPROXY_CONFIG
Expand Down
5 changes: 1 addition & 4 deletions net/nginx/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk

PKG_NAME:=nginx
PKG_VERSION:=1.21.3
PKG_RELEASE:=2
PKG_RELEASE:=3

PKG_SOURCE:=nginx-$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://nginx.org/download/
Expand Down Expand Up @@ -376,9 +376,6 @@ ifeq ($(CONFIG_NGINX_NAXSI),y)
endif
$(if $(CONFIG_NGINX_NAXSI),$($(INSTALL_BIN) $(PKG_BUILD_DIR)/nginx-naxsi/naxsi_config/naxsi_core.rules $(1)/etc/nginx))
$(if $(CONFIG_NGINX_NAXSI),$(chmod 0640 $(1)/etc/nginx/naxsi_core.rules))

$(INSTALL_DIR) $(1)/etc/hotplug.d/acme
$(INSTALL_DATA) ./files/acme.hotplug $(1)/etc/hotplug.d/acme/00-nginx
endef

Package/nginx-all-module/install = $(Package/nginx-ssl/install)
Expand Down
3 changes: 0 additions & 3 deletions net/nginx/files/acme.hotplug

This file was deleted.

5 changes: 5 additions & 0 deletions net/nginx/files/nginx.init
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ reload_service() {
}


service_triggers() {
procd_add_raw_trigger acme.renew 5000 /etc/init.d/nginx reload
}


extra_command "relog" "Reopen log files (without reloading)"
relog() {
[ -d /var/log/nginx ] || mkdir -p /var/log/nginx
Expand Down

0 comments on commit 14f151a

Please sign in to comment.