From 38d5fbf25604abb570c5dbbb3344adb311c4ae61 Mon Sep 17 00:00:00 2001 From: Mael Pedretti Date: Tue, 2 Jul 2024 09:21:14 +0200 Subject: [PATCH] Added support for sub-auth-id and sub-auth-user Implements https://github.com/tokiwinter/cloudns-api/issues/15 --- README.md | 14 +++++++---- bin/cloudns_api.sh | 59 +++++++++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 020aebc..c3ae744 100644 --- a/README.md +++ b/README.md @@ -47,9 +47,12 @@ Usage test - perform an authentication test Environment: - Ensure that the following two environment variables are exported: - CLOUDNS_API_ID - your ClouDNS API ID (auth-id) - CLOUDNS_PASSWORD - your ClouDNS API password (auth-password) + Ensure that the following environment variable + CLOUDNS_PASSWORD - your ClouDNS API password (auth-password) + And that one of the following are exported: + CLOUDNS_API_ID - your ClouDNS API ID (auth-id) + CLOUDNS_API_SUB_ID - your ClouDNS API sub-ID (sub-auth-id) + CLOUDNS_API_SUB_USER - your ClouDNS API sub-user name (sub-auth-user) Environment ----------- @@ -57,8 +60,11 @@ Environment You should ensure that you export the following environment variables correctly. Their existence is tested by the script. - $ export CLOUDNS_API_ID= $ export CLOUDNS_PASSWORD= + # and one of + $ export CLOUDNS_API_ID= + $ export CLOUDNS_API_SUB_ID= + $ export CLOUDNS_API_SUB_USER= The script does not currently support sub-auth-id, etc. See Limitations at the end of this document. This does allow us to operate globally on your account. Ensure you diff --git a/bin/cloudns_api.sh b/bin/cloudns_api.sh index a453195..60580b1 100755 --- a/bin/cloudns_api.sh +++ b/bin/cloudns_api.sh @@ -59,9 +59,12 @@ function print_usage() { builtin echo " test - perform an authentication test" builtin echo "" builtin echo " Environment:" - builtin echo " Ensure that the following two environment variables are exported:" - builtin echo " CLOUDNS_API_ID - your ClouDNS API ID (auth-id)" + builtin echo " Ensure that the following environment variable" builtin echo " CLOUDNS_PASSWORD - your ClouDNS API password (auth-password)" + builtin echo " And that one of the following are exported:" + builtin echo " CLOUDNS_API_ID - your ClouDNS API ID (auth-id)" + builtin echo " CLOUDNS_API_SUB_ID - your ClouDNS API sub-ID (sub-auth-id)" + builtin echo " CLOUDNS_API_SUB_USER - your ClouDNS API sub-user name (sub-auth-user)" } >&2 } @@ -130,22 +133,62 @@ function process_arguments() { function check_environment_variables() { local ERROR_COUNT=0 - local REQUIRED_VARIABLES=( CLOUDNS_API_ID CLOUDNS_PASSWORD ) - for REQUIRED_VARIABLE in ${REQUIRED_VARIABLES[@]}; do - if $( builtin eval test -z \${${REQUIRED_VARIABLE}} ); then - print_error "Environment variable \${${REQUIRED_VARIABLE}} unset" - (( ERROR_COUNT = ERROR_COUNT + 1 )) + + # Check if CLOUDNS_PASSWORD is set + if $(builtin eval test -z \${CLOUDNS_PASSWORD}); then + print_error "Environment variable CLOUDNS_PASSWORD unset" + (( ERROR_COUNT = ERROR_COUNT + 1 )) + fi + + # Check if only one of the required CloudNS identifier is set + local CLOUDNS_API_IDENTIFIERS=( CLOUDNS_API_ID CLOUDNS_API_SUB_ID CLOUDNS_API_SUB_USER ) + local CLOUDNS_API_IDENTIFIERS_COUNT=0 + + for CLOUDNS_API_IDENTIFIER in ${CLOUDNS_API_IDENTIFIERS[@]}; do + if ! $(builtin eval test -z \${${CLOUDNS_API_IDENTIFIER}}); then + (( CLOUDNS_API_IDENTIFIERS_COUNT = CLOUDNS_API_IDENTIFIERS_COUNT + 1 )) fi done + + if [ "${CLOUDNS_API_IDENTIFIERS_COUNT}" -eq "0" ]; then + print_error "None of the environment variables CLOUDNS_API_ID, CLOUDNS_API_SUB_ID, CLOUDNS_API_SUB_USER are set." + (( ERROR_COUNT = ERROR_COUNT + 1 )) + fi + + if [ "${CLOUDNS_API_IDENTIFIERS_COUNT}" -gt "1" ]; then + print_error "Exactly one of the environment variables CLOUDNS_API_ID, CLOUDNS_API_SUB_ID, CLOUDNS_API_SUB_USER must be set, not more." + (( ERROR_COUNT = ERROR_COUNT + 1 )) + fi + if [ "${ERROR_COUNT}" -gt "0" ]; then exit 1 fi } + function set_auth_post_data() { - AUTH_POST_DATA="-d auth-id=${CLOUDNS_API_ID} -d auth-password=${CLOUDNS_PASSWORD}" + local AUTH_TYPE="" + local AUTH_ID="" + + if [ ! -z "${CLOUDNS_API_ID}" ]; then + AUTH_TYPE="auth-id" + AUTH_ID="${CLOUDNS_API_ID}" + elif [ ! -z "${CLOUDNS_API_SUB_ID}" ]; then + AUTH_TYPE="sub-auth-id" + AUTH_ID="${CLOUDNS_API_SUB_ID}" + elif [ ! -z "${CLOUDNS_API_SUB_USER}" ]; then + AUTH_TYPE="sub-auth-user" + AUTH_ID="${CLOUDNS_API_SUB_USER}" + else + print_error "No valid authentication environment variable set" + exit 1 + fi + + AUTH_POST_DATA="-d ${AUTH_TYPE}=${AUTH_ID} -d auth-password=${CLOUDNS_PASSWORD}" } + + function test_api_url() { local HTTP_CODE=$( curl -4qs -o /dev/null -w '%{http_code}' ${API_URL}/login.json ) if [ "${HTTP_CODE}" != "200" ]; then