239 lines
7.6 KiB
Bash
Executable File
239 lines
7.6 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Verifies the OSS credentials configured in a deployment config file without
|
|
# printing secrets. The default action only needs oss:PutObject permission.
|
|
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
cat <<'EOF'
|
|
Usage:
|
|
bash scripts/test-oss-upload.sh [--env /path/to/.env] [--config /path/to/application-prod.yml] [--key object/key]
|
|
bash scripts/test-oss-upload.sh --cors [--env /path/to/.env] [--config /path/to/application-prod.yml] [--origin https://example.com]
|
|
|
|
Default mode uploads a small text file to oss-connectivity-test/ and keeps it
|
|
there. It proves only that this server can upload to OSS with the selected
|
|
deployment configuration.
|
|
--cors sends the same OPTIONS preflight that a browser uses and checks whether
|
|
the Bucket permits a direct PUT request from the selected web origin.
|
|
|
|
Configuration source, in order:
|
|
1. --env (shell-style KEY=VALUE file)
|
|
2. --config (Spring application-prod.yml)
|
|
3. ./src/main/resources/application-prod.yml, ./application-prod.yml, ./.env
|
|
|
|
Recognized variables in a .env file:
|
|
APP_OSS_ENDPOINT or OSS_ENDPOINT
|
|
APP_OSS_ACCESS_KEY_ID or OSS_ACCESS_KEY_ID
|
|
APP_OSS_ACCESS_KEY_SECRET or OSS_ACCESS_KEY_SECRET
|
|
APP_OSS_BUCKET or OSS_BUCKET
|
|
EOF
|
|
}
|
|
|
|
env_file=""
|
|
config_file=""
|
|
mode="upload"
|
|
origin="https://cadworld.robotquan.com"
|
|
object_key=""
|
|
|
|
while [[ $# -gt 0 ]]; do
|
|
case "$1" in
|
|
--env)
|
|
[[ $# -ge 2 ]] || { echo "--env requires a path" >&2; exit 2; }
|
|
env_file="$2"
|
|
shift 2
|
|
;;
|
|
--key)
|
|
[[ $# -ge 2 ]] || { echo "--key requires an object key" >&2; exit 2; }
|
|
object_key="$2"
|
|
shift 2
|
|
;;
|
|
--config)
|
|
[[ $# -ge 2 ]] || { echo "--config requires a path" >&2; exit 2; }
|
|
config_file="$2"
|
|
shift 2
|
|
;;
|
|
--origin)
|
|
[[ $# -ge 2 ]] || { echo "--origin requires a URL" >&2; exit 2; }
|
|
origin="$2"
|
|
shift 2
|
|
;;
|
|
--cors)
|
|
mode="cors"
|
|
shift
|
|
;;
|
|
--help|-h)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Unknown option: $1" >&2
|
|
usage >&2
|
|
exit 2
|
|
;;
|
|
esac
|
|
done
|
|
|
|
if [[ -n "$env_file" && -n "$config_file" ]]; then
|
|
echo "Use either --env or --config, not both" >&2
|
|
exit 2
|
|
fi
|
|
|
|
if [[ -z "$env_file" && -z "$config_file" ]]; then
|
|
for candidate in src/main/resources/application-prod.yml application-prod.yml .env; do
|
|
if [[ -f "$candidate" ]]; then
|
|
case "$candidate" in
|
|
*.yml|*.yaml) config_file="$candidate" ;;
|
|
*) env_file="$candidate" ;;
|
|
esac
|
|
break
|
|
fi
|
|
done
|
|
fi
|
|
|
|
if [[ -z "$env_file" && -z "$config_file" ]]; then
|
|
echo "Cannot find application-prod.yml or .env. Pass --config or --env explicitly." >&2
|
|
exit 2
|
|
fi
|
|
|
|
if [[ -n "$env_file" ]]; then
|
|
[[ -f "$env_file" ]] || { echo "Cannot find .env file: $env_file" >&2; exit 2; }
|
|
# Deployment .env files are shell-style KEY=VALUE lines. Export them only to
|
|
# this script process so no credentials are placed in shell history or output.
|
|
set -a
|
|
# shellcheck disable=SC1090
|
|
. "$env_file"
|
|
set +a
|
|
fi
|
|
|
|
endpoint="${APP_OSS_ENDPOINT:-${OSS_ENDPOINT:-}}"
|
|
access_key_id="${APP_OSS_ACCESS_KEY_ID:-${OSS_ACCESS_KEY_ID:-}}"
|
|
access_key_secret="${APP_OSS_ACCESS_KEY_SECRET:-${OSS_ACCESS_KEY_SECRET:-}}"
|
|
bucket="${APP_OSS_BUCKET:-${OSS_BUCKET:-}}"
|
|
|
|
read_oss_yaml_value() {
|
|
local key="$1"
|
|
awk -v wanted="$key" '
|
|
/^[[:space:]]*oss:[[:space:]]*(#.*)?$/ { in_oss = 1; next }
|
|
in_oss && /^ [^[:space:]#][^:]*:[[:space:]]*/ { exit }
|
|
in_oss {
|
|
line = $0
|
|
sub(/^[[:space:]]*/, "", line)
|
|
current = line
|
|
sub(/:.*/, "", current)
|
|
if (current == wanted) {
|
|
sub(/^[^:]*:[[:space:]]*/, "", line)
|
|
sub(/[[:space:]]*#.*/, "", line)
|
|
print line
|
|
exit
|
|
}
|
|
}
|
|
' "$config_file"
|
|
}
|
|
|
|
if [[ -n "$config_file" ]]; then
|
|
[[ -f "$config_file" ]] || { echo "Cannot find config file: $config_file" >&2; exit 2; }
|
|
endpoint="$(read_oss_yaml_value endpoint)"
|
|
access_key_id="$(read_oss_yaml_value access-key-id)"
|
|
access_key_secret="$(read_oss_yaml_value access-key-secret)"
|
|
bucket="$(read_oss_yaml_value bucket)"
|
|
|
|
for value_name in endpoint access_key_id access_key_secret bucket; do
|
|
value="${!value_name}"
|
|
value="${value%$'\r'}"
|
|
value="${value#\"}"
|
|
value="${value%\"}"
|
|
value="${value#\'}"
|
|
value="${value%\'}"
|
|
printf -v "$value_name" '%s' "$value"
|
|
done
|
|
fi
|
|
|
|
for value_name in endpoint access_key_id access_key_secret bucket; do
|
|
[[ -n "${!value_name}" ]] || {
|
|
echo "Missing OSS configuration: $value_name" >&2
|
|
exit 2
|
|
}
|
|
done
|
|
|
|
endpoint="${endpoint%/}"
|
|
case "$endpoint" in
|
|
https://*|http://*) ;;
|
|
*) endpoint="https://$endpoint" ;;
|
|
esac
|
|
|
|
endpoint_host="${endpoint#http://}"
|
|
endpoint_host="${endpoint_host#https://}"
|
|
endpoint_host="${endpoint_host%%/*}"
|
|
object_url_base="${endpoint%%://*}://${bucket}.${endpoint_host}"
|
|
|
|
if [[ "$mode" == "cors" ]]; then
|
|
probe_key="oss-connectivity-test/cors-preflight.txt"
|
|
headers_file="$(mktemp)"
|
|
trap 'rm -f "$headers_file"' EXIT
|
|
|
|
status="$(curl --silent --show-error --output /dev/null --dump-header "$headers_file" \
|
|
--write-out '%{http_code}' --request OPTIONS \
|
|
--header "Origin: $origin" \
|
|
--header 'Access-Control-Request-Method: PUT' \
|
|
--header 'Access-Control-Request-Headers: content-type' \
|
|
"$object_url_base/$probe_key")"
|
|
|
|
allowed_origin="$(awk -F': ' 'tolower($1) == "access-control-allow-origin" { gsub("\\r", "", $2); print $2 }' "$headers_file" | tail -n 1)"
|
|
allowed_methods="$(awk -F': ' 'tolower($1) == "access-control-allow-methods" { gsub("\\r", "", $2); print $2 }' "$headers_file" | tail -n 1)"
|
|
|
|
echo "OSS preflight status: $status"
|
|
echo "Access-Control-Allow-Origin: ${allowed_origin:-<missing>}"
|
|
echo "Access-Control-Allow-Methods: ${allowed_methods:-<missing>}"
|
|
|
|
normalized_methods="${allowed_methods//[[:space:]]/}"
|
|
if [[ "$status" =~ ^2 ]] && [[ "$allowed_origin" == "$origin" || "$allowed_origin" == "*" ]] \
|
|
&& [[ ",${normalized_methods^^}," == *",PUT,"* ]]; then
|
|
echo "CORS check passed: browser direct upload from $origin is allowed."
|
|
exit 0
|
|
fi
|
|
|
|
echo "CORS check failed: OSS does not allow browser PUT uploads from $origin." >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ -z "$object_key" ]]; then
|
|
object_key="oss-connectivity-test/momatchengine-$(date -u +%Y%m%dT%H%M%SZ)-$RANDOM.txt"
|
|
fi
|
|
|
|
# A generated key must remain a simple URL path. Keeping it ASCII also makes
|
|
# the canonical OSS resource unambiguous for this connectivity check.
|
|
[[ "$object_key" =~ ^[A-Za-z0-9._/-]+$ ]] || {
|
|
echo "--key may contain only letters, digits, dot, underscore, slash, and hyphen" >&2
|
|
exit 2
|
|
}
|
|
|
|
date_header="$(LC_ALL=C date -u '+%a, %d %b %Y %H:%M:%S GMT')"
|
|
canonical_resource="/$bucket/$object_key"
|
|
string_to_sign="PUT\n\ntext/plain\n$date_header\n$canonical_resource"
|
|
signature="$(printf '%b' "$string_to_sign" | openssl dgst -sha1 -hmac "$access_key_secret" -binary | base64 | tr -d '\n')"
|
|
authorization="OSS $access_key_id:$signature"
|
|
payload_file="$(mktemp)"
|
|
headers_file="$(mktemp)"
|
|
trap 'rm -f "$payload_file" "$headers_file"' EXIT
|
|
printf 'OSS connectivity test generated at %s\n' "$(date -u '+%Y-%m-%dT%H:%M:%SZ')" > "$payload_file"
|
|
|
|
status="$(curl --silent --show-error --output /dev/null --dump-header "$headers_file" \
|
|
--write-out '%{http_code}' --request PUT \
|
|
--header "Date: $date_header" \
|
|
--header 'Content-Type: text/plain' \
|
|
--header "Authorization: $authorization" \
|
|
--upload-file "$payload_file" \
|
|
"$object_url_base/$object_key")"
|
|
|
|
if [[ "$status" =~ ^2 ]]; then
|
|
echo "OSS upload succeeded (HTTP $status)."
|
|
echo "Object key: $object_key"
|
|
echo "The test object was retained because the configured account may only have upload permission."
|
|
exit 0
|
|
fi
|
|
|
|
echo "OSS upload failed (HTTP $status). Response headers:" >&2
|
|
sed -n '1,40p' "$headers_file" >&2
|
|
exit 1
|