bitflicker64 commented on code in PR #3189: URL: https://github.com/apache/hugegraph/pull/3189#discussion_r3957217662
########## hugegraph-store/docs/deployment-guide.md: ########## @@ -678,6 +678,13 @@ For a production-like 3-node distributed deployment, use the compose file at `do ```bash cd docker +# The PD REST secret is required; the Compose file refuses to start without +# it. Generate it once and keep it, every PD node and PD client needs the +# same value (docker/README.md has the full .env recipe). Review Comment: @a-3954361458.md ########## docker/set-hubble-pd-password.sh: ########## @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# Generate the Hubble properties file a Compose topology mounts, with PD's +# REST secret written in as operations.pd.password. +# +# usage: set-hubble-pd-password.sh <hstore|hstore-ha> [secret] +# +# Reads conf/hubble/<name>.properties.example (tracked) and writes +# conf/hubble/<name>.local.properties (ignored by git), so the secret never +# lands in a tracked file. The secret defaults to $HG_PD_AUTH_SECRET_KEY. The +# value never goes through a sed replacement, where & # and backslash are +# special, and backslashes are doubled for the .properties format. Run this +# before `docker compose up`: if the target is missing Docker creates an empty +# directory at the bind path and Hubble starts with no configuration. +set -euo pipefail + +name=${1:?usage: $0 <hstore|hstore-ha> [secret]} +secret=${2:-${HG_PD_AUTH_SECRET_KEY:-}} +dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/conf/hubble" +example="${dir}/${name}.properties.example" +out="${dir}/${name}.local.properties" + +[[ -f "$example" ]] || { echo "no such topology: ${name} (expected ${example})" >&2; exit 1; } +[[ -n "$secret" ]] || { echo "secret is empty; load .env first (set -a; . ./.env; set +a)" >&2; exit 1; } +case "$secret" in + *$'\n'*|*$'\r'*) echo "secret contains a line break, which a .properties value cannot hold" >&2; exit 1 ;; Review Comment: @a-3954361461.md ########## hugegraph-pd/hg-pd-test/src/main/java/org/apache/hugegraph/pd/rest/RestApiTest.java: ########## @@ -137,11 +137,89 @@ public void testQueryShards() throws URISyntaxException, IOException, Interrupte String url = pdRestAddr + "/v1/shards"; HttpRequest request = HttpRequest.newBuilder() .uri(new URI(url)) - .header("Authorization", "Basic c3RvcmU6MTIz") + .header(AUTH_HEADER, VALID_AUTH) .GET() .build(); HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); JSONObject obj = new JSONObject(response.body()); assert obj.getInt("status") == 0; } + + @Test + public void testMissingCredentialGets401() throws URISyntaxException, IOException, + InterruptedException { + String url = pdRestAddr + "/v1/members"; + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(url)) + .GET() + .build(); + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assert response.statusCode() == 401; + } + + @Test + public void testWrongPasswordGets401() throws URISyntaxException, IOException, + InterruptedException { + String url = pdRestAddr + "/v1/members"; + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(url)) + .header(AUTH_HEADER, basicAuth("store", "wrong-password")) + .GET() + .build(); + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assert response.statusCode() == 401; + } + + @Test + public void testEmptyPasswordGets401() throws URISyntaxException, IOException, + InterruptedException { + String url = pdRestAddr + "/v1/members"; + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(url)) + .header(AUTH_HEADER, basicAuth("hg", "")) + .GET() + .build(); + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString()); + assert response.statusCode() == 401; + } + + private int statusWithoutCredential(String path) throws URISyntaxException, IOException, + InterruptedException { + HttpRequest request = HttpRequest.newBuilder() + .uri(new URI(pdRestAddr + path)) + .GET() + .build(); + return client.send(request, HttpResponse.BodyHandlers.ofString()).statusCode(); + } + + @Test + public void testProbePathsNeedNoCredential() throws URISyntaxException, IOException, + InterruptedException { + assert statusWithoutCredential("/v1/health") == 200; + assert statusWithoutCredential("/actuator/health") == 200; Review Comment: @a-3954361462.md ########## hugegraph-pd/hg-pd-dist/docker/docker-entrypoint.sh: ########## @@ -51,14 +72,31 @@ require_env "HG_PD_GRPC_HOST" require_env "HG_PD_RAFT_ADDRESS" require_env "HG_PD_RAFT_PEERS_LIST" require_env "HG_PD_INITIAL_STORE_LIST" +# The REST API refuses every authenticated request without this, and the image +# ships no default because a published secret is not a secret. +require_env "HG_PD_AUTH_SECRET_KEY" : "${HG_PD_GRPC_PORT:=8686}" : "${HG_PD_REST_PORT:=8620}" : "${HG_PD_DATA_PATH:=/hugegraph-pd/pd_data}" : "${HG_PD_INITIAL_STORE_COUNT:=1}" +# Secret for REST Basic authentication (auth.secret-key). Required above and +# never logged. +AUTH_JSON="\"auth\": { \"secret-key\": \"$(json_escape "${HG_PD_AUTH_SECRET_KEY}")\" }," + +# The secret above lands in SPRING_APPLICATION_JSON, and actuator's /env +# sanitizer keys off the property name: it redacts auth.secret-key but returns +# the SPRING_APPLICATION_JSON environment entry verbatim, secret included. The +# image's own conf/application.yml already narrows the exposure, but a +# bind-mounted pre-1.8 config brings back include: "*". SPRING_APPLICATION_JSON +# outranks the config file, so pin the allowlist here too. +MANAGEMENT_JSON="\"management\": { \"endpoints\": { \"web\": { \"exposure\": { \"include\": \"health,metrics,prometheus\" } } } }," Review Comment: @a-3954361464.md ########## hugegraph-server/hugegraph-dist/src/assembly/static/bin/wait-storage.sh: ########## @@ -93,19 +116,34 @@ if env | grep '^hugegraph\.' > /dev/null; then export PD_REST_LIST log "PD REST peers = $PD_REST_LIST" + # Only worth saying where PD is actually polled: topologies without + # pd.peers never send this credential anywhere. + if [ -z "${PD_AUTH_PASSWORD}" ]; then + log "WARN: PD_AUTH_PASSWORD is empty; PD will answer 401 unless it runs without auth" + fi log "Timeout = ${WAIT_STORAGE_TIMEOUT_S}s" timeout "${WAIT_STORAGE_TIMEOUT_S}s" bash -c " log() { echo '[wait-storage] '\"\$1\"; } + # curl stays out of the grep pipeline so its status code is + # readable: a 401 is a wrong secret, not a storage problem, and + # retrying it for 300s only hides that. check_any_pd_stores() { for peer in \$(echo \"\$PD_REST_LIST\" | tr ',' ' '); do - if curl ${PD_AUTH_ARGS} -f -s \ - --connect-timeout ${WAIT_STORAGE_PD_CONNECT_TIMEOUT_S} \ - --max-time ${WAIT_STORAGE_PD_MAX_TIMEOUT_S} \ - http://\${peer}/v1/stores 2>/dev/null | \ - grep -qi '\"state\"[[:space:]]*:[[:space:]]*\"Up\"'; then + body=\$(printf 'user = \"%s:%s\"\n' \ + \"\$PD_AUTH_CURL_USER\" \"\$PD_AUTH_CURL_PASSWORD\" | \ + curl -K - -s -w '\n%{http_code}' \ + --connect-timeout ${WAIT_STORAGE_PD_CONNECT_TIMEOUT_S} \ + --max-time ${WAIT_STORAGE_PD_MAX_TIMEOUT_S} \ + \"http://\${peer}/v1/stores\" 2>/dev/null) + code=\${body##*\$'\n'} + if [ \"\$code\" = 401 ]; then + log \"ERROR: PD at \${peer} refused the credential (401): PD_AUTH_PASSWORD must match PD's auth.secret-key\" >&2 + return 2 Review Comment: @a-3956307914.md ########## hugegraph-pd/hg-pd-dist/docker/docker-entrypoint.sh: ########## @@ -26,10 +26,31 @@ require_env() { fi } +# Escape a value for use inside a JSON string: backslash and quote, then every +# remaining C0 control character as \uXXXX. Dropping only LF, as an earlier +# version did, left CR and TAB to produce invalid JSON and a container that +# failed before startup. json_escape() { - local s="$1" - s=${s//\\/\\\\}; s=${s//\"/\\\"}; s=${s//$'\n'/} - printf "%s" "$s" + local s="$1" out="" i c Review Comment: @a-3956307921.md -- 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]
