bitflicker64 commented on code in PR #3192:
URL: https://github.com/apache/hugegraph/pull/3192#discussion_r4453636610
##########
hugegraph-server/hugegraph-dist/src/assembly/static/bin/enable-auth.sh:
##########
@@ -34,23 +36,334 @@ GREMLIN_SERVER_CONF="gremlin-server.yaml"
REST_SERVER_CONF="rest-server.properties"
GRAPH_CONF="hugegraph.properties"
+fail() {
+ echo "enable-auth.sh: $*" >&2
+ exit 1
+}
+
+# Reading and writing .properties files goes through props.awk, the same helper
+# the docker entrypoint uses, because the keys below can be spelled in every
way
+# java.util.Properties accepts: `=`/`:`/bare-whitespace separators, a form feed
+# as whitespace, `\.` or `\u002e` for the dots, and LF, CRLF or CR line
+# terminators. grep and sed see a different file. A legal
+# `gremlin\u002egraph=org.apache.hugegraph.HugeFactory` matched no pattern at
+# all, so the factory was never wrapped for auth even though both servers were
+# told authentication was on -- and the CR byte that the previous pattern had
+# to be handed a carriage return for is now handled by the reader itself.
+#
+# props.awk is packaged in this same bin/ directory by the release assembly, so
+# it is present in the tarball and in the image. PROPS_AWK lets a caller point
+# at a different copy; the entrypoint reads that same variable for itself.
+for candidate in "${PROPS_AWK:-}" "${BIN}/props.awk" "${TOP}/props.awk"; do
+ if [[ -n "${candidate}" && -f "${candidate}" ]]; then
+ PROPS_AWK="${candidate}"
+ break
+ fi
+done
+[[ -n "${PROPS_AWK:-}" ]] || fail "props.awk not found beside this script"
+
+# The Gremlin half of the decision below has to be made by the same reader the
+# entrypoint uses. yamlscan.awk is not in bin/: the Dockerfile places it in
the
+# install home, one above this script, which is where the image layout is
+# mirrored in the test tree; PROPS_AWK and YAMLSCAN_AWK cover a caller that
+# keeps it elsewhere. The release tarball carries no copy at all, so the
+# fallback below has to answer on its own.
+YAMLSCAN=""
+for candidate in "${YAMLSCAN_AWK:-}" "${TOP}/yamlscan.awk"
"${BIN}/yamlscan.awk"; do
+ if [[ -n "${candidate}" && -f "${candidate}" ]]; then
+ YAMLSCAN="${candidate}"
+ break
+ fi
+done
+
+# props_get is the only reader used here, and it treats any nonzero status from
+# props.awk as an error: 2 means the file could not be read at all, which must
+# not be mistaken for "the key is not there" and answered with a write.
+props_get() {
+ local status=0 value
+ value=$(PROPS_MODE=get PROPS_DECODED=1 PROPS_KEY="$1" PROPS_FILE="$2" \
+ awk -f "${PROPS_AWK}" /dev/null) || status=$?
+ if (( status > 0 )); then
+ fail "cannot read $2"
+ fi
+ printf '%s' "${value}"
+}
+
+# How many logical definitions a key carries, `0` when it has none. Like
+# props_get, a nonzero status means the file could not be read rather than the
+# key being absent, so the status has to survive to the caller instead of
+# collapsing into an empty string that reads as "nothing there".
+props_count() {
+ local status=0 value
+ value=$(PROPS_MODE=count PROPS_KEY="$1" PROPS_FILE="$2" \
+ awk -f "${PROPS_AWK}" /dev/null) || status=$?
+ if (( status > 0 )); then
+ return "${status}"
+ fi
+ printf '%s' "${value}"
+}
+
+# Collapse a key to its first definition, dropping the rest, keeping the bytes
+# the server has already been reading. Going through props_set with a value
+# from props_get is not the same thing: props_get decodes, and a decoded value
+# containing `=`, `:` or a leading blank does not come back off the encoder
+# byte-identical.
+props_keep_first() {
+ PROPS_MODE=set PROPS_KEEP_FIRST=1 PROPS_KEY="$1" PROPS_FILE="$2" \
+ awk -f "${PROPS_AWK}" /dev/null || fail "cannot update $2"
+}
+
+# The character check for every class this script writes. Java class names
need
+# no properties escaping; anything else would have to go through the
entrypoint's
+# encoder first. It is one function rather than a pattern at each call site so
+# the early refusal below and the write cannot drift apart.
+check_class_name() {
+ case "$1" in
+ *[!A-Za-z0-9_\.\$]*) fail "refusing to write an unescaped value: $1" ;;
+ esac
+}
+
+props_set() {
+ check_class_name "$2"
+ PROPS_MODE=set PROPS_KEY="$1" PROPS_VALUE_ENCODED="$2" PROPS_FILE="$3" \
+ awk -f "${PROPS_AWK}" /dev/null || fail "cannot update $3"
+}
+
+# Give `$3` its default `$2` for key `$1`, in place, unless it already has a
+# value. Guarding on the key being present and appending was not the same
+# question:
+# `auth.authenticator=` and a bare `auth.authenticator` line both parse to the
+# empty string (measured against java.util.Properties, which also strips the
+# trailing blanks of `auth.authenticator= `), so a presence check reported
+# them as
+# answered and the append was skipped -- while the entrypoint's
+# check_auth_sides, which asks for the value rather than the key, counted the
+# same file as unconfigured. `loadAuthenticator("")` returns null, so REST
then
+# served without authentication next to a Gremlin that required it.
+#
+# props_set covers both shapes the guard had to split: with a definition
+# present it replaces the first one where it stands (no duplicate for
+# first-definition-wins to bury), with none present it appends.
+#
+# `$4` is the key's current value, read before the first write. Reading it
+# here instead -- `[[ -n "$(props_get "$1" "$3")" ]]` -- discarded the status
of
+# the command substitution, because `[[` only looks at the text, so props_get's
+# refusal of a file props.awk cannot read (an `include` directive) exited the
+# subshell, the guard saw an empty string, and props_set then tried to write
+# that same unreadable file.
+ensure_rest_prop() {
+ if [[ -z "$4" ]]; then
+ props_set "$1" "$2" "$3"
+ return 0
+ fi
+ # `$4` non-empty only ever spoke about the FIRST definition, so an older
+ # image that appended on every restart left the key defined two or three
+ # times and this guard let all of them stand. HugeConfig then reads the
key
+ # as a list, `InitStore` fails with "Invalid value for key
+ # 'auth.authenticator'", and the upgraded container never boots.
Collapsing
+ # to the first definition is the answer that changes nothing the server has
+ # already been reading.
+ local definitions
+ definitions=$(props_count "$1" "$3") || fail "cannot read $3"
+ if (( definitions > 1 )); then
+ props_keep_first "$1" "$3"
Review Comment:
Important: this keeps the first `auth.authenticator` even when the
duplicates disagree, while `Settings.read()` takes the last root
`authentication` mapping, so a mounted config that named `ConfigAuthenticator`
before the base `enable-auth.sh` appended `StandardAuthenticator` now boots
with REST and Gremlin on different authenticators instead of failing in
`InitStore`. Collapse only when every definition has the same value, and fail
naming the key otherwise.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]