From: Nicolas Frey <n.frey@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH proxmox-acme 1/2] dns: schema: add generate schema script
Date: Mon, 3 Nov 2025 09:03:09 +0100 [thread overview]
Message-ID: <20251103080310.60168-2-n.frey@proxmox.com> (raw)
In-Reply-To: <20251103080310.60168-1-n.frey@proxmox.com>
Create a bash script, which parses out the required information using
the `dns_<provider>_info` variable in each of the providers' dnsapi
scripts. This variable includes the Options, their descriptions and
sometimes the default/optional values.This is then used to generate
the json schema, which is then normalized. If the contents of the
variable are malformed or it is empty, an empty entry will be
generated.
Signed-off-by: Nicolas Frey <n.frey@proxmox.com>
---
src/Makefile | 4 ++
src/generate_schema.sh | 135 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 139 insertions(+)
create mode 100755 src/generate_schema.sh
diff --git a/src/Makefile b/src/Makefile
index 9ee97c9..2a0d567 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -176,6 +176,10 @@ normalize-schema: dns-challenge-schema.json
$^ > $^.tmp
mv $^.tmp $^
+.PHONY: generate-schema
+generate-schema:
+ ./generate_schema.sh -o dns-challenge-schema.json
+
.PHONY: install
install:
install -D -m 0744 proxmox-acme ${DESTDIR}${ACMEDIR}/proxmox-acme
diff --git a/src/generate_schema.sh b/src/generate_schema.sh
new file mode 100755
index 0000000..993ca6e
--- /dev/null
+++ b/src/generate_schema.sh
@@ -0,0 +1,135 @@
+#!/usr/bin/bash
+
+# This script generates a valid dns challenge schema using information provided by the
+# semi-structured dns_*_info in all dnsapi scripts provided by acme.sh.
+#
+# If the variable is not structured the way it is expected or present, then it will simply
+# generate an empty entry in the schema (as of writing this, this is only the case for mydevil
+# and selectel) this could then manually be corrected if necessary.
+
+set -euo pipefail
+
+output=""
+
+while getopts ":o:" opt; do
+ case $opt in
+ o)
+ output="$OPTARG"
+ ;;
+ \?)
+ echo "Invalid option: -$OPTARG" >&2
+ exit 1
+ ;;
+ :)
+ echo "Option -$OPTARG requires an argument" >&2
+ exit 1
+ ;;
+ esac
+done
+shift $((OPTIND - 1))
+
+DNSAPI_DIR="${1:-./acme.sh/dnsapi}"
+first=true
+declare -a not_generated=()
+
+json_escape() {
+ printf '%s' "$1" \
+ | sed -E \
+ -e 's/\\/\\\\/g' \
+ -e 's/"/\\"/g' \
+ -e 's/\r/\\r/g' \
+ -e 's/\n/\\n/g' \
+ -e 's/\t/\\t/g'
+}
+
+gen_schema() {
+ echo "{"
+
+ for f in "$DNSAPI_DIR"/dns_*.sh; do
+ [[ -f "$f" ]] || continue
+
+ # Extract dns_xxx_info=' ... '
+ info=$(awk '
+ BEGIN {in_info=0; text=""}
+ /^dns_.*_info='\''/ {
+ in_info=1
+ sub(/^dns_.*_info='\''/, "")
+ text=$0
+ next
+ }
+ in_info {
+ if ($0 ~ /'\''$/) {
+ sub(/'\''$/, "")
+ text=text"\n"$0
+ in_info=0
+ print text
+ } else {
+ text=text"\n"$0
+ }
+ }' "$f")
+
+ provider=$(basename "$f" .sh | sed 's/^dns_//')
+
+ name=$(echo "$info" | head -n 1)
+ # if site information is wanted/needed?
+ # site=$(echo "$info" | grep -E "^Site:" | sed 's/^Site:[[:space:]]*//' || true)
+
+ declare -A fields=()
+ while read -r var desc; do
+ [[ -z "$var" || -z "$desc" ]] && continue
+ fields["$var"]="$desc"
+ done < <(echo "$info" | awk '/^Options:/ {opt=1; next} opt && NF {if ($1=="Issues:"||$1=="Author:") exit; print}')
+
+ # info was not found or malformed, add to list
+ if [ ${#fields[@]} -eq 0 ]; then
+ not_generated+=("$provider")
+ fi
+
+ $first || echo ","
+ first=false
+
+ echo "\"$(json_escape "$provider")\": {"
+ echo "\"fields\": {"
+ first_field=true
+ for k in "${!fields[@]}"; do
+ $first_field || echo ","
+ first_field=false
+ desc=${fields[$k]}
+ echo "\"$(json_escape "$k")\": {"
+ echo "\"description\":\"$(json_escape "$desc")\","
+ echo "\"type\":\"string\""
+ # parse out if a value is optional through the description
+ echo $desc | grep -qi "optional" && echo ",\"optional\": \"1\""
+ # * after 'Default:', rm full stop rm quotation marks
+ # parse out if a value has a default through the description | | |
+ echo $desc | grep -q "Default:" && echo ",\"default\": \"$(echo "$desc" | sed -n 's/.*\bDefault: *//p' | sed 's/.$//' | tr -d '"')\""
+ echo "}"
+ done
+ echo "},"
+ echo "\"name\": \"$(json_escape "$name")\""
+ # echo "\"site\":\"$(json_escape "$site")\""
+ echo "}"
+ done
+
+ echo "}"
+}
+
+if [ -n "$output" ]; then
+ gen_schema > "$output"
+ # normalize schema
+ perl -MJSON -0777 \
+ -E 'print JSON->new->utf8->pretty->canonical->space_before(0)->encode(decode_json(<>))' \
+ "$output" > "$output.tmp"
+ mv "$output.tmp" "$output"
+else
+ # if -o is omitted, it will simply print to stdout
+ gen_schema
+fi
+
+if [ ${#not_generated[@]} -gt 0 ]; then
+ echo "Some entries could not be (fully) generated:"
+fi
+
+for i in "${not_generated[@]}"; do
+ echo " - $i"
+done
--
2.47.3
_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel
next prev parent reply other threads:[~2025-11-03 8:03 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-03 8:03 [pve-devel] [PATCH proxmox-acme 0/2] Automate DNS challenge json schema with script Nicolas Frey
2025-11-03 8:03 ` Nicolas Frey [this message]
2025-11-03 8:03 ` [pve-devel] [PATCH proxmox-acme 2/2] dns: generate schema Nicolas Frey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251103080310.60168-2-n.frey@proxmox.com \
--to=n.frey@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox