diff --git a/tools/ngcp-api.inc b/tools/ngcp-api.inc index 2752bf6c81..8c6a0e6747 100644 --- a/tools/ngcp-api.inc +++ b/tools/ngcp-api.inc @@ -29,3 +29,20 @@ importcreds () { exit 1 fi } + +validateurl() { + local URL=$1 + + # If we are using the legacy credentials, stop any validation. + if [ -z "${APIREALM}" ]; then + return + fi + + local NGCP_API_HOST + NGCP_API_HOST="$(getent hosts "${NGCP_API_IP}" | awk '{ print $2 }')" + if ! [[ "${URL}" =~ ^https://${NGCP_API_IP}:${NGCP_API_PORT} ]] && + ! [[ "${URL}" =~ ^https://${NGCP_API_HOST}:${NGCP_API_PORT} ]]; then + echo "Error: specified URL does not match expected URL for API credentials (https://${NGCP_API_IP}:${NGCP_API_PORT}/)" >&2 + exit 1 + fi +} diff --git a/tools_bin/ngcp-api-delete b/tools_bin/ngcp-api-delete index 3934f3fac1..9a0bfaa5bd 100755 --- a/tools_bin/ngcp-api-delete +++ b/tools_bin/ngcp-api-delete @@ -25,6 +25,7 @@ if [ -z "${APIUSER}" ]; then if [ -z "${APIUSER}" ]; then importcreds "${SYSCREDS}" 0440 APIUSER="${AUTH_SYSTEM_LOGIN}:${AUTH_SYSTEM_PASSWORD}" + APIREALM=(-H 'NGCP-UserAgent: NGCP::API::Client') fi fi if [ -z "${APIUSER}" ]; then @@ -53,7 +54,9 @@ URL="$1" if [ -z "${URL}" ] ; then usage fi +validateurl "$URL" ${CURL} -i ${VERBOSE} -X DELETE \ + "${APIREALM[@]}" \ -H 'Connection: close' \ "${HEADERS}" --user "${APIUSER}" --insecure "${URL}" diff --git a/tools_bin/ngcp-api-get b/tools_bin/ngcp-api-get index 27fa83fd2a..777a7baed6 100755 --- a/tools_bin/ngcp-api-get +++ b/tools_bin/ngcp-api-get @@ -25,6 +25,7 @@ if [ -z "${APIUSER}" ]; then if [ -z "${APIUSER}" ]; then importcreds "${SYSCREDS}" 0440 APIUSER="${AUTH_SYSTEM_LOGIN}:${AUTH_SYSTEM_PASSWORD}" + APIREALM=(-H 'NGCP-UserAgent: NGCP::API::Client') fi fi if [ -z "${APIUSER}" ]; then @@ -53,7 +54,9 @@ URL="$1" if [ -z "${URL}" ]; then usage fi +validateurl "$URL" ${CURL} -i ${VERBOSE} -X GET \ + "${APIREALM[@]}" \ -H 'Connection: close' \ --user "${APIUSER}" --insecure "${URL}" diff --git a/tools_bin/ngcp-api-patch b/tools_bin/ngcp-api-patch index 05739575ab..9328f8f612 100755 --- a/tools_bin/ngcp-api-patch +++ b/tools_bin/ngcp-api-patch @@ -27,6 +27,7 @@ if [ -z "${APIUSER}" ]; then if [ -z "${APIUSER}" ]; then importcreds "${SYSCREDS}" 0440 APIUSER="${AUTH_SYSTEM_LOGIN}:${AUTH_SYSTEM_PASSWORD}" + APIREALM=(-H 'NGCP-UserAgent: NGCP::API::Client') fi fi if [ -z "${APIUSER}" ]; then @@ -69,8 +70,10 @@ URL="$1" if [ -z "${URL}" ] ; then usage fi +validateurl "$URL" ${CURL} -i ${VERBOSE} -X PATCH \ + "${APIREALM[@]}" -H 'Connection: close' -H "Prefer: return=${PREFER}" \ -H "Content-Type: ${CTYPE}" \ --user "${APIUSER}" --insecure "${FILE_INPUT[@]}" "${URL}" "${INPUT[@]}" diff --git a/tools_bin/ngcp-api-post b/tools_bin/ngcp-api-post index 1d46c5b6de..449ebb01b6 100755 --- a/tools_bin/ngcp-api-post +++ b/tools_bin/ngcp-api-post @@ -26,6 +26,7 @@ if [ -z "${APIUSER}" ]; then if [ -z "${APIUSER}" ]; then importcreds "${SYSCREDS}" 0440 APIUSER="${AUTH_SYSTEM_LOGIN}:${AUTH_SYSTEM_PASSWORD}" + APIREALM=(-H 'NGCP-UserAgent: NGCP::API::Client') fi fi if [ -z "${APIUSER}" ]; then @@ -63,9 +64,11 @@ URL="$1" if [ -z "${URL}" ]; then usage fi +validateurl "$URL" CTYPE=${2:-application/json} ${CURL} -i ${VERBOSE} -X POST \ + "${APIREALM[@]}" \ -H 'Connection: close' -H 'Prefer: return=representation' \ -H "Content-Type: ${CTYPE}" \ --user "${APIUSER}" --insecure "${FILE_INPUT[@]}" "${URL}" "${INPUT[@]}" diff --git a/tools_bin/ngcp-api-put b/tools_bin/ngcp-api-put index 01060f1297..d4120d2fe5 100755 --- a/tools_bin/ngcp-api-put +++ b/tools_bin/ngcp-api-put @@ -26,6 +26,7 @@ if [ -z "${APIUSER}" ]; then if [ -z "${APIUSER}" ]; then importcreds "${SYSCREDS}" 0440 APIUSER="${AUTH_SYSTEM_LOGIN}:${AUTH_SYSTEM_PASSWORD}" + APIREALM=(-H 'NGCP-UserAgent: NGCP::API::Client') fi fi if [ -z "${APIUSER}" ]; then @@ -63,9 +64,11 @@ URL="$1" if [ -z "${URL}" ] ; then usage fi +validateurl "$URL" CTYPE=${2:-application/json} ${CURL} -i ${VERBOSE} -X PUT \ + "${APIREALM[@]}" \ -H 'Connection: close' -H 'Prefer: return=representation' \ -H "Content-Type: ${CTYPE}" \ --user "${APIUSER}" --insecure "${FILE_INPUT[@]}" "${URL}" "${INPUT[@]}"