imbajin commented on code in PR #2945: URL: https://github.com/apache/hugegraph/pull/2945#discussion_r4303523355
########## hugegraph-server/hugegraph-dist/src/assembly/static/bin/arthas.sh: ########## @@ -0,0 +1,201 @@ +#!/bin/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. +# +# Offline attachment to a running Server or Store using the bundled agent. +set -euo pipefail + +fail() { echo "Error: $*" >&2; exit 1; } +usage() { + echo "Usage: $0 [--pid PID] [--telnet-port PORT] [--target-ip ADDRESS] [--command COMMAND]" + echo " [--username USER] [--password PASSWORD]" + echo "Run as the service OS user; defaults to bin/pid and loopback port 3658." + echo "New agents disable HTTP/MCP; use the existing telnet port for an active agent." + echo "Exiting this helper stops Arthas, including after --command or quit; the JVM keeps running." +} + +BIN=$(cd -P "$(dirname "${BASH_SOURCE[0]}")" && pwd) +TOP=$(cd -P "$BIN/.." && pwd) +pid="" +port=3658 +target_ip=127.0.0.1 +username="" +password="" +command="" +boot_args=() +while [ "$#" -gt 0 ]; do + case "$1" in + --pid|--telnet-port|--target-ip|--command|--username|--password) + [ "$#" -ge 2 ] || fail "Missing value for $1" + case "$1" in + --pid) pid=$2 ;; + --telnet-port) port=$2 ;; + --target-ip) target_ip=$2 ;; + --command) command=$2 ;; + --username) username=$2 ;; + --password) password=$2 ;; + esac + shift 2 ;; + -h|--help) usage; exit 0 ;; + *) usage >&2; fail "Unknown option: $1" ;; + esac +done +# Native batch files contain one command per line. +for credential in "$username" "$password"; do + [[ "$credential" != *$'\n'* && "$credential" != *$'\r'* ]] || fail "Credentials must be single-line values" +done +if [ -z "$pid" ]; then + [ -r "$BIN/pid" ] || fail "No bin/pid; start the service or supply --pid PID" + pid=$(cat "$BIN/pid") +fi +[[ "$pid" =~ ^[1-9][0-9]{0,9}$ ]] || fail "PID must be a positive integer" +[[ "$port" =~ ^[1-9][0-9]{0,4}$ ]] && [ "$port" -le 65535 ] || fail "Invalid telnet port" +[ -t 0 ] || [ -n "$command" ] || fail "Interactive use needs a terminal; use --command COMMAND" +case "$target_ip" in + [Ll][Oo][Cc][Aa][Ll][Hh][Oo][Ss][Tt]) target_ip=127.0.0.1 ;; +esac +JAVA=${JAVA_HOME:+$JAVA_HOME/bin/}java +ARTHAS="$TOP/lib/arthas" +[ -f "$ARTHAS/arthas-boot.jar" ] || fail "Bundled Arthas files are missing from lib/arthas" +# Keep the listener within this diagnosis session. Verify its PID before stopping +# it, since an occupied port must never stop a different application's agent. +state_dir=$(mktemp -d) +client_pid="" +attaching=true +connect_ip=$target_ip +quote_argument() { + local value=${1//\'/\'\\\'\'} + printf "'%s'" "$value" +} +write_batch() { + local file=$1 + local commands=$2 + { + if [ -n "$password" ]; then + printf 'auth --username %s %s\n' "$(quote_argument "${username:-arthas}")" "$(quote_argument "$password")" + fi + # Match arthas-client --command's semicolon-separated command syntax. + printf '%s\n' "${commands//;/$'\n'}" + } > "$file" +} +redact_auth_echo() { + local line + while IFS= read -r line || [ -n "$line" ]; do + case "$line" in + *'$ auth --username '*) printf '%s$ auth [credentials omitted]\n' "${line%%'$ auth --username '*}" ;; + *) printf '%s\n' "$line" ;; + esac + done +} +# The native client's -t option does not bound its final response wait. +bounded_client() { + write_batch "$state_dir/cleanup.commands" "$1" + "$JAVA" -jar "$ARTHAS/arthas-client.jar" -t 5000 -f "$state_dir/cleanup.commands" "$connect_ip" "$port" & Review Comment: P2: Keep authentication output out of the session PID parser. This batch now runs auth before session, while stop_agent searches the entire captured output for the first unanchored JAVA_PID match. This does not depend only on terminal echo: Arthas 4.1.5 AuthCommand prints the supplied username in its success response. For requested PID 123, a valid username 'prefix JAVA_PID 123 suffix' followed by the actual session row JAVA_PID 999 makes the helper accept 123 and send stop to the other agent. Conversely, credential text containing another PID can prevent cleanup of the correct agent. Two independent exact-script replay harnesses reproduce both outcomes; Java/agent replies were simulated, not native two-JVM executions. Parse only an unambiguous actual session result row after separating auth output, and fail closed on ambiguous results. Add authenticated two-agent/PID-mismatch tests plus credentials resembling session fields. Upstream response: https://github.com/alibaba/arthas/bl ob/arthas-all-4.1.5/core/src/main/java/com/taobao/arthas/core/command/basic1000/AuthCommand.java#L83-L91 . -- 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]
