|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 | +# contributor license agreements. See the NOTICE file distributed with |
| 5 | +# this work for additional information regarding copyright ownership. |
| 6 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 | +# (the "License"); you may not use this file except in compliance with |
| 8 | +# the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, software |
| 13 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +# See the License for the specific language governing permissions and |
| 16 | +# limitations under the License. |
| 17 | + |
| 18 | +set -euo pipefail |
| 19 | + |
| 20 | +SERVER_ROOT_INPUT="${1:?Usage: $0 PATH_TO_SERVER_DIST}" |
| 21 | +SERVER_ROOT=$(cd "$SERVER_ROOT_INPUT" && pwd) |
| 22 | +SERVER_SCRIPT="${SERVER_ROOT}/bin/hugegraph-server.sh" |
| 23 | +CONF="${SERVER_ROOT}/conf" |
| 24 | +SECURITY_PROPERTIES="${CONF}/java-security.properties" |
| 25 | + |
| 26 | +fail() { |
| 27 | + echo "FAIL: $1" >&2 |
| 28 | + exit 1 |
| 29 | +} |
| 30 | + |
| 31 | +assert_argument() { |
| 32 | + local argument="$1" |
| 33 | + local capture="$2" |
| 34 | + grep -Fxq -- "$argument" "$capture" || \ |
| 35 | + fail "missing JVM argument: $argument" |
| 36 | +} |
| 37 | + |
| 38 | +assert_no_argument() { |
| 39 | + local pattern="$1" |
| 40 | + local capture="$2" |
| 41 | + if grep -Eq -- "$pattern" "$capture"; then |
| 42 | + fail "unexpected JVM argument matching: $pattern" |
| 43 | + fi |
| 44 | +} |
| 45 | + |
| 46 | +if [[ ! -x "$SERVER_SCRIPT" ]]; then |
| 47 | + fail "server script is not executable: $SERVER_SCRIPT" |
| 48 | +fi |
| 49 | +if [[ ! -f "$SECURITY_PROPERTIES" ]]; then |
| 50 | + fail "security properties file is missing: $SECURITY_PROPERTIES" |
| 51 | +fi |
| 52 | + |
| 53 | +if [[ -n "${JAVA_HOME:-}" ]]; then |
| 54 | + JAVA_BIN="${JAVA_HOME}/bin/java" |
| 55 | +else |
| 56 | + JAVA_BIN="java" |
| 57 | +fi |
| 58 | + |
| 59 | +TEMP_DIR=$(mktemp -d) |
| 60 | +trap 'rm -rf "$TEMP_DIR"' EXIT |
| 61 | + |
| 62 | +CHECK_SOURCE="${TEMP_DIR}/ReadDnsCacheTtl.java" |
| 63 | +cat > "$CHECK_SOURCE" <<'JAVA' |
| 64 | +import java.security.Security; |
| 65 | +
|
| 66 | +public class ReadDnsCacheTtl { |
| 67 | + public static void main(String[] args) { |
| 68 | + System.out.print(Security.getProperty("networkaddress.cache.ttl")); |
| 69 | + } |
| 70 | +} |
| 71 | +JAVA |
| 72 | + |
| 73 | +ACTUAL_TTL=$("$JAVA_BIN" \ |
| 74 | + -Djava.security.properties="$SECURITY_PROPERTIES" "$CHECK_SOURCE") |
| 75 | +if [[ "$ACTUAL_TTL" != "30" ]]; then |
| 76 | + fail "expected security property TTL 30, got: $ACTUAL_TTL" |
| 77 | +fi |
| 78 | + |
| 79 | +SYSTEM_PROPERTY_TTL=$("$JAVA_BIN" \ |
| 80 | + -Dnetworkaddress.cache.ttl=99 \ |
| 81 | + -Djava.security.properties="$SECURITY_PROPERTIES" "$CHECK_SOURCE") |
| 82 | +if [[ "$SYSTEM_PROPERTY_TTL" != "30" ]]; then |
| 83 | + fail "ordinary -D property unexpectedly changed the security property" |
| 84 | +fi |
| 85 | + |
| 86 | +OPERATOR_PROPERTIES="${TEMP_DIR}/operator-security.properties" |
| 87 | +echo "networkaddress.cache.ttl=45" > "$OPERATOR_PROPERTIES" |
| 88 | +OPERATOR_TTL=$("$JAVA_BIN" \ |
| 89 | + -Djava.security.properties="$SECURITY_PROPERTIES" \ |
| 90 | + -Djava.security.properties="$OPERATOR_PROPERTIES" "$CHECK_SOURCE") |
| 91 | +if [[ "$OPERATOR_TTL" != "45" ]]; then |
| 92 | + fail "operator security properties override was not honored" |
| 93 | +fi |
| 94 | + |
| 95 | +MOCK_JAVA_HOME="${TEMP_DIR}/mock-java-home" |
| 96 | +mkdir -p "${MOCK_JAVA_HOME}/bin" |
| 97 | +cat > "${MOCK_JAVA_HOME}/bin/java" <<'MOCK' |
| 98 | +#!/bin/bash |
| 99 | +if [[ " $* " == *" -version "* ]]; then |
| 100 | + echo 'openjdk version "11.0.0"' >&2 |
| 101 | + exit 0 |
| 102 | +fi |
| 103 | +printf '%s\n' "$@" > "$CAPTURE_FILE" |
| 104 | +MOCK |
| 105 | +chmod +x "${MOCK_JAVA_HOME}/bin/java" |
| 106 | + |
| 107 | +ENABLED_CAPTURE="${TEMP_DIR}/enabled.args" |
| 108 | +CAPTURE_FILE="$ENABLED_CAPTURE" JAVA_HOME="$MOCK_JAVA_HOME" \ |
| 109 | + STDOUT_MODE=true "$SERVER_SCRIPT" \ |
| 110 | + "${CONF}/gremlin-server.yaml" "${CONF}/rest-server.properties" true \ |
| 111 | + "-Doperator.marker=preserved" >/dev/null |
| 112 | + |
| 113 | +assert_argument \ |
| 114 | + "-Djava.security.properties=${SECURITY_PROPERTIES}" "$ENABLED_CAPTURE" |
| 115 | +assert_argument \ |
| 116 | + "-Djava.security.manager=org.apache.hugegraph.security.HugeSecurityManager" \ |
| 117 | + "$ENABLED_CAPTURE" |
| 118 | +assert_argument "-Doperator.marker=preserved" "$ENABLED_CAPTURE" |
| 119 | +assert_no_argument '^-D(networkaddress\.cache\.ttl|sun\.net\.inetaddr\.ttl)=' \ |
| 120 | + "$ENABLED_CAPTURE" |
| 121 | + |
| 122 | +DISABLED_CAPTURE="${TEMP_DIR}/disabled.args" |
| 123 | +CAPTURE_FILE="$DISABLED_CAPTURE" JAVA_HOME="$MOCK_JAVA_HOME" \ |
| 124 | + STDOUT_MODE=true "$SERVER_SCRIPT" \ |
| 125 | + "${CONF}/gremlin-server.yaml" "${CONF}/rest-server.properties" false \ |
| 126 | + "-Doperator.marker=preserved" >/dev/null |
| 127 | + |
| 128 | +assert_no_argument '^-Djava\.security\.properties=' "$DISABLED_CAPTURE" |
| 129 | +assert_no_argument '^-Djava\.security\.manager=' "$DISABLED_CAPTURE" |
| 130 | +assert_argument "-Doperator.marker=preserved" "$DISABLED_CAPTURE" |
| 131 | + |
| 132 | +OVERRIDE_CAPTURE="${TEMP_DIR}/override.args" |
| 133 | +CAPTURE_FILE="$OVERRIDE_CAPTURE" JAVA_HOME="$MOCK_JAVA_HOME" \ |
| 134 | + STDOUT_MODE=true "$SERVER_SCRIPT" \ |
| 135 | + "${CONF}/gremlin-server.yaml" "${CONF}/rest-server.properties" true \ |
| 136 | + "-Djava.security.properties=${OPERATOR_PROPERTIES}" >/dev/null |
| 137 | + |
| 138 | +LAST_SECURITY_ARGUMENT=$(grep -E '^-Djava\.security\.properties=' \ |
| 139 | + "$OVERRIDE_CAPTURE" | tail -n 1) |
| 140 | +if [[ "$LAST_SECURITY_ARGUMENT" != \ |
| 141 | + "-Djava.security.properties=${OPERATOR_PROPERTIES}" ]]; then |
| 142 | + fail "operator security properties argument was overwritten" |
| 143 | +fi |
| 144 | + |
| 145 | +echo "PASS: Java security properties and startup wiring" |
0 commit comments