Skip to content

Commit 9d0e104

Browse files
committed
refactor(auth): unify Dataverse auth handling with header-based approach #11959
Replaced query parameter-based authentication with a standardized header-based mechanism. Centralized logic for configuring Bearer Token and Unblock Key authentication, including support for file-based inputs with error handling.
1 parent b8982b6 commit 9d0e104

File tree

1 file changed

+31
-28
lines changed

1 file changed

+31
-28
lines changed

conf/solr/solr-driver.sh

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ UPGRADE_SOURCE_PATH="${UPGRADE_SOURCE_PATH:-${DEFAULT_UPGRADE_SOURCE_PATH}}"
8989
METADATA_ENDPOINT=""
9090
LOCK_FD=""
9191
SOLR_AUTH_HEADER=""
92-
DATAVERSE_AUTH_PARAM=""
92+
DATAVERSE_AUTH_HEADER=""
9393
SCHEMA_SOURCE_PATH_SET_BY_USER="false"
9494

9595
# Logging functions
@@ -436,16 +436,7 @@ check_solr_status() {
436436
# Check Dataverse API status
437437
check_dataverse_status() {
438438
local status_url="${DATAVERSE_URL}/api/admin/settings"
439-
if [[ -n "${DATAVERSE_AUTH_PARAM}" ]]; then
440-
status_url="${status_url}?${DATAVERSE_AUTH_PARAM}"
441-
fi
442-
443-
local auth_header=""
444-
if [[ -n "${DATAVERSE_BEARER_TOKEN:-}" ]]; then
445-
auth_header="Authorization: Bearer ${DATAVERSE_BEARER_TOKEN}"
446-
fi
447-
448-
check_endpoint "${status_url}" "Dataverse API" "${auth_header}"
439+
check_endpoint "${status_url}" "Dataverse API" "${DATAVERSE_AUTH_HEADER}"
449440
}
450441

451442
# Perform startup checks with configured behavior
@@ -548,18 +539,13 @@ fetch_metadata_fields() {
548539
local output_file="$1"
549540
local url="${METADATA_ENDPOINT}"
550541

551-
# Add query parameters if needed
552-
if [[ -n "${DATAVERSE_AUTH_PARAM}" ]]; then
553-
url="${url}?${DATAVERSE_AUTH_PARAM}"
554-
fi
555-
556542
log_info "Fetching metadata fields from ${METADATA_ENDPOINT}"
557543

558544
local curl_opts=(-sf -o "${output_file}")
559545

560-
# Add bearer token if configured
561-
if [[ -n "${DATAVERSE_BEARER_TOKEN:-}" ]]; then
562-
curl_opts+=(-H "Authorization: Bearer ${DATAVERSE_BEARER_TOKEN}")
546+
# Add authentication header if configured
547+
if [[ -n "${DATAVERSE_AUTH_HEADER}" ]]; then
548+
curl_opts+=(-H "${DATAVERSE_AUTH_HEADER}")
563549
fi
564550

565551
if ! curl "${curl_opts[@]}" "${url}"; then
@@ -1020,19 +1006,36 @@ main() {
10201006
fi
10211007

10221008
# Dataverse authentication
1023-
if [[ -n "${DATAVERSE_BEARER_TOKEN_FILE:-}" && -f "${DATAVERSE_BEARER_TOKEN_FILE}" ]]; then
1024-
DATAVERSE_BEARER_TOKEN=$(cat "${DATAVERSE_BEARER_TOKEN_FILE}")
1025-
fi
1026-
1027-
if [[ -n "${DATAVERSE_UNBLOCK_KEY_FILE:-}" && -f "${DATAVERSE_UNBLOCK_KEY_FILE}" ]]; then
1028-
DATAVERSE_UNBLOCK_KEY=$(cat "${DATAVERSE_UNBLOCK_KEY_FILE}")
1029-
fi
1030-
1009+
# Priority 1: Bearer token (env var or file)
10311010
if [[ -n "${DATAVERSE_BEARER_TOKEN:-}" ]]; then
1011+
# Bearer token already set, use it
1012+
DATAVERSE_AUTH_HEADER="Authorization: Bearer ${DATAVERSE_BEARER_TOKEN}"
10321013
log_info "Dataverse authentication configured (Bearer Token)"
1014+
elif [[ -n "${DATAVERSE_BEARER_TOKEN_FILE:-}" ]]; then
1015+
# Bearer token file specified, try to read it
1016+
if [[ -f "${DATAVERSE_BEARER_TOKEN_FILE}" ]]; then
1017+
DATAVERSE_BEARER_TOKEN=$(cat "${DATAVERSE_BEARER_TOKEN_FILE}")
1018+
DATAVERSE_AUTH_HEADER="Authorization: Bearer ${DATAVERSE_BEARER_TOKEN}"
1019+
log_info "Dataverse authentication configured (Bearer Token from file)"
1020+
else
1021+
log_error "DATAVERSE_BEARER_TOKEN_FILE specified but file not found: ${DATAVERSE_BEARER_TOKEN_FILE}"
1022+
exit 1
1023+
fi
1024+
# Priority 2: Unblock key (only if no bearer token)
10331025
elif [[ -n "${DATAVERSE_UNBLOCK_KEY:-}" ]]; then
1034-
DATAVERSE_AUTH_PARAM="unblock-key=${DATAVERSE_UNBLOCK_KEY}"
1026+
# Unblock key already set, use it
1027+
DATAVERSE_AUTH_HEADER="X-Dataverse-unblock-key: ${DATAVERSE_UNBLOCK_KEY}"
10351028
log_info "Dataverse authentication configured (Unblock Key)"
1029+
elif [[ -n "${DATAVERSE_UNBLOCK_KEY_FILE:-}" ]]; then
1030+
# Unblock key file specified, try to read it
1031+
if [[ -f "${DATAVERSE_UNBLOCK_KEY_FILE}" ]]; then
1032+
DATAVERSE_UNBLOCK_KEY=$(cat "${DATAVERSE_UNBLOCK_KEY_FILE}")
1033+
DATAVERSE_AUTH_HEADER="X-Dataverse-unblock-key: ${DATAVERSE_UNBLOCK_KEY}"
1034+
log_info "Dataverse authentication configured (Unblock Key from file)"
1035+
else
1036+
log_error "DATAVERSE_UNBLOCK_KEY_FILE specified but file not found: ${DATAVERSE_UNBLOCK_KEY_FILE}"
1037+
exit 1
1038+
fi
10361039
fi
10371040

10381041
# Set metadata endpoint based on Dataverse URL

0 commit comments

Comments
 (0)