From a3b4b62d8144ce95daefcf90011bbef104cf3beb Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Sat, 21 Sep 2024 04:15:36 +0000 Subject: [PATCH] update: video record standalone with GraphQL endpoint needs basic auth Signed-off-by: Viet Nguyen Duc --- Video/Dockerfile | 2 +- Video/validate_endpoint.sh | 26 ++++++++++++++++++++++++++ Video/video.sh | 16 +++++++--------- Video/video_graphQLQuery.sh | 6 ++---- 4 files changed, 36 insertions(+), 14 deletions(-) create mode 100755 Video/validate_endpoint.sh diff --git a/Video/Dockerfile b/Video/Dockerfile index 33ecfe7e0..456432dc6 100755 --- a/Video/Dockerfile +++ b/Video/Dockerfile @@ -73,7 +73,7 @@ RUN --mount=type=secret,id=SEL_PASSWD \ # Add Supervisor configuration files #====================================== COPY supervisord.conf /etc -COPY --chown="${SEL_UID}:${SEL_GID}" entry_point.sh video.sh video_ready.py video_graphQLQuery.sh video_gridUrl.sh /opt/bin/ +COPY --chown="${SEL_UID}:${SEL_GID}" entry_point.sh validate_endpoint.sh video.sh video_ready.py video_graphQLQuery.sh video_gridUrl.sh /opt/bin/ #====================================== # Add RCLONE for uploading videos diff --git a/Video/validate_endpoint.sh b/Video/validate_endpoint.sh new file mode 100755 index 000000000..e9c969748 --- /dev/null +++ b/Video/validate_endpoint.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +endpoint=$1 +graphql_endpoint=${2:-false} +max_time=1 +process_name="endpoint.checks" + +BASIC_AUTH="$(echo -n "${SE_ROUTER_USERNAME}:${SE_ROUTER_PASSWORD}" | base64)" + +if [ "${graphql_endpoint}" = "true" ]; then + endpoint_checks=$(curl --noproxy "*" -m ${max_time} -k -X POST \ + -H "Content-Type: application/json" \ + -H "Authorization: Basic ${BASIC_AUTH}" \ + --data '{"query":"{ grid { sessionCount } }"}' \ + -s "${endpoint}" -o /dev/null -w "%{http_code}") +else + endpoint_checks=$(curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -m ${max_time} -s -k -o /dev/null -w "%{http_code}" "${endpoint}") +fi + +if [[ "$endpoint_checks" = "404" ]]; then + echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} is not found - status code: ${endpoint_checks}" +elif [[ "$endpoint_checks" = "401" ]]; then + echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} requires authentication - status code: ${endpoint_checks}. Please provide valid credentials via SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD environment variables." +elif [[ "$endpoint_checks" != "200" ]]; then + echo "$(date +%FT%T%Z) [${process_name}] - Endpoint ${endpoint} is not available - status code: ${endpoint_checks}" +fi diff --git a/Video/video.sh b/Video/video.sh index 97d98b497..27ae6246b 100755 --- a/Video/video.sh +++ b/Video/video.sh @@ -28,6 +28,9 @@ else NODE_STATUS_ENDPOINT="${SE_SERVER_PROTOCOL}://${DISPLAY_CONTAINER_NAME}:${SE_NODE_PORT}/status" fi +/opt/bin/validate_endpoint.sh "${NODE_STATUS_ENDPOINT}" +BASIC_AUTH="$(echo -n "${SE_ROUTER_USERNAME}:${SE_ROUTER_PASSWORD}" | base64)" + if [ -d "${VIDEO_FOLDER}" ]; then echo "$(date +%FT%T%Z) [${process_name}] - Video folder exists: ${VIDEO_FOLDER}" else @@ -70,7 +73,7 @@ function wait_for_display() { } function check_if_api_respond() { - endpoint_checks=$(curl --noproxy "*" -sk -o /dev/null -w "%{http_code}" "${NODE_STATUS_ENDPOINT}") + endpoint_checks=$(curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -sk -o /dev/null -w "%{http_code}" "${NODE_STATUS_ENDPOINT}") if [[ "${endpoint_checks}" != "200" ]]; then return 1 fi @@ -198,20 +201,15 @@ else recorded_count=0 wait_for_api_respond - while curl --noproxy "*" -sk --request GET ${NODE_STATUS_ENDPOINT} >/tmp/status.json; do + while curl --noproxy "*" -H "Authorization: Basic ${BASIC_AUTH}" -sk --request GET ${NODE_STATUS_ENDPOINT} >/tmp/status.json; do session_id=$(jq -r "${JQ_SESSION_ID_QUERY}" /tmp/status.json) if [[ "$session_id" != "null" && "$session_id" != "" && "$session_id" != "reserved" && "$recording_started" = "false" ]]; then echo "$(date +%FT%T%Z) [${process_name}] - Session: $session_id is created" return_list=($(bash ${VIDEO_CONFIG_DIRECTORY}/video_graphQLQuery.sh "$session_id")) caps_se_video_record="${return_list[0]}" video_file_name="${return_list[1]}.mp4" - endpoint_status="${return_list[2]}" - endpoint_url="${return_list[3]}" - if [[ "${endpoint_status}" = "401" ]]; then - echo "$(date +%FT%T%Z) [${process_name}] - GraphQL endpoint requires authentication, please set env variables SE_ROUTER_USERNAME and SE_ROUTER_PASSWORD" - elif [[ "${endpoint_status}" = "404" ]]; then - echo "$(date +%FT%T%Z) [${process_name}] - GraphQL endpoint could not be found, please check the endpoint ${endpoint_url}" - fi + endpoint_url="${return_list[2]}" + /opt/bin/validate_endpoint.sh "${endpoint_url}" "true" echo "$(date +%FT%T%Z) [${process_name}] - Start recording: $caps_se_video_record, video file name: $video_file_name" log_node_response fi diff --git a/Video/video_graphQLQuery.sh b/Video/video_graphQLQuery.sh index cc2986add..bf877a5da 100755 --- a/Video/video_graphQLQuery.sh +++ b/Video/video_graphQLQuery.sh @@ -34,9 +34,7 @@ if [ -n "${GRAPHQL_ENDPOINT}" ]; then -s "${GRAPHQL_ENDPOINT}" -o "/tmp/graphQL_${SESSION_ID}.json" -w "%{http_code}") current_check=$((current_check + 1)) # Check if the response contains "capabilities" - if [[ "$endpoint_checks" = "404" ]] || [[ $current_check -eq $retry_time ]]; then - break - elif [[ "$endpoint_checks" = "401" ]] || [[ $current_check -eq $retry_time ]]; then + if [[ $current_check -eq $retry_time ]]; then break elif [[ "$endpoint_checks" = "200" ]] && [[ $(jq -e '.data.session.capabilities | fromjson | ."'se:vncEnabled'"' /tmp/graphQL_${SESSION_ID}.json >/dev/null) -eq 0 ]]; then break @@ -78,7 +76,7 @@ fi # Normalize the video file name TEST_NAME="$(echo "${TEST_NAME}" | tr ' ' '_' | tr -dc "${VIDEO_FILE_NAME_TRIM}" | cut -c 1-251)" -return_array=("${RECORD_VIDEO}" "${TEST_NAME}" "${endpoint_checks}" "${GRAPHQL_ENDPOINT}") +return_array=("${RECORD_VIDEO}" "${TEST_NAME}" "${GRAPHQL_ENDPOINT}") # stdout the values for other scripts consuming echo "${return_array[@]}"