You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.4 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. #Original Author: Gerardo Trotta <gerardo.trotta@euronet.aero>
  3. #Application username
  4. #ARUBA_AK="xxxxx"
  5. #
  6. #Application password
  7. #ARUBA_AS="xxxxxx"
  8. #
  9. #API key
  10. #ARUBA_TK="xxxxxxxx"
  11. #
  12. #Consumer Key
  13. #ARUBA_CK="sdfsdfsdfsdfsdfdsf"
  14. #ARUBA_END_POINT=aruba-it
  15. #'aruba-business-it'
  16. ARUBA_BUSINESS_IT='https://api.arubabusiness.it'
  17. _aruba_get_api() {
  18. _ogaep="$1"
  19. case "${_ogaep}" in
  20. aruba-b-it | arubabit)
  21. printf "%s" $ARUBA_BUSINESS_IT
  22. return
  23. ;;
  24. *)
  25. _err "Unknown parameter : $1"
  26. return 1
  27. ;;
  28. esac
  29. }
  30. _initAuth() {
  31. ARUBA_AK="${ARUBA_AK:-$(_readaccountconf_mutable ARUBA_AK)}"
  32. ARUBA_AS="${ARUBA_AS:-$(_readaccountconf_mutable ARUBA_AS)}"
  33. ARUBA_TK="${ARUBA_TK:-$(_readaccountconf_mutable ARUBA_TK)}"
  34. if [ -z "$ARUBA_AK" ] || [ -z "$ARUBA_AS" ] || [ -z "$ARUBA_TK" ]; then
  35. ARUBA_AK=""
  36. ARUBA_AS=""
  37. ARUBA_TK=""
  38. _err "You don't specify ARUBA application key and application secret yet."
  39. _err "Please create you key and try again."
  40. return 1
  41. fi
  42. if [ "$ARUBA_TK" != "$(_readaccountconf ARUBA_TK)" ]; then
  43. _info "It seems that your aruba key is changed, let's clear consumer key first."
  44. _clearaccountconf ARUBA_TK
  45. _clearaccountconf ARUBA_CK
  46. fi
  47. _saveaccountconf_mutable ARUBA_AK "$ARUBA_AK"
  48. _saveaccountconf_mutable ARUBA_AS "$ARUBA_AS"
  49. _saveaccountconf_mutable ARUBA_TK "$ARUBA_TK"
  50. ARUBA_END_POINT="${ARUBA_END_POINT:-$(_readaccountconf_mutable ARUBA_END_POINT)}"
  51. if [ -z "$ARUBA_END_POINT" ]; then
  52. ARUBA_END_POINT="aruba-it"
  53. fi
  54. _info "Using ARUBA endpoint: $ARUBA_END_POINT"
  55. if [ "$ARUBA_END_POINT" != "aruba-it" ]; then
  56. _saveaccountconf_mutable ARUBA_END_POINT "$ARUBA_END_POINT"
  57. fi
  58. ARUBA_API="$(_aruba_get_api $ARUBA_END_POINT)"
  59. _debug ARUBA_API "$ARUBA_API"
  60. ARUBA_CK="${ARUBA_CK:-$(_readaccountconf_mutable ARUBA_CK)}"
  61. if [ -z "$ARUBA_CK" ]; then
  62. _info "ARUBA consumer key is empty, Let's get one:"
  63. if ! _aruba_authentication; then
  64. _err "Can not get consumer key."
  65. #return and wait for retry.
  66. return 1
  67. fi
  68. fi
  69. _info "Checking authentication and get domain details"
  70. if ! _aruba_rest GET "api/domains/dns/$_domain/details" || _contains "$response" "error" || _contains "$response" "denied"; then
  71. _err "The consumer key is invalid: $ARUBA_CK"
  72. _err "Please retry to create a new one."
  73. _clearaccountconf ARUBA_CK
  74. return 1
  75. fi
  76. domainData=$(echo "$response" | tr -d '\r')
  77. # get all Ids and peek only values
  78. temp="$(echo "$domainData" | _egrep_o "Id\": [^,]*" | cut -d : -f 2 | head -1)" # first element is zone Id
  79. domain_id=$temp
  80. _info "DomainId is: $domain_id"
  81. _info "Consumer key is ok."
  82. return 0
  83. }
  84. ######## Public functions #####################
  85. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  86. dns_aruba_add() {
  87. #fulldomain=$1
  88. txtvalue=$2
  89. _debug _domain "$_domain"
  90. _sub_domain="_acme-challenge"
  91. if ! _initAuth; then
  92. return 1
  93. fi
  94. _debug "Check if _acme-challenge record exists in " "$_domain"
  95. if ! _extract_record_id "$_sub_domain.$_domain."; then
  96. _method="POST"
  97. else
  98. _method="PUT"
  99. fi
  100. _payload="{ \"IdDomain\": $domain_id, \"Type\": \"TXT\", \"Name\": \"$_sub_domain\", \"Content\": \"\\\"$txtvalue\\\"\" }"
  101. _info "Adding record"
  102. if _aruba_rest "$_method" "api/domains/dns/record" "$_payload"; then
  103. if _contains "$response" "$txtvalue"; then
  104. _aruba_rest GET "api/domains/dns/$_domain/details"
  105. _debug "Refresh:$response"
  106. _info "Added, sleep 10 seconds."
  107. _sleep 10
  108. return 0
  109. fi
  110. fi
  111. _err "Add txt record error."
  112. return 1
  113. }
  114. #fulldomain
  115. dns_aruba_rm() {
  116. #fulldomain=$1
  117. txtvalue=$2
  118. if ! _initAuth; then
  119. return 1
  120. fi
  121. _sub_domain="_acme-challenge"
  122. _debug "Getting TXT record to delete: $_sub_domain.$_domain."
  123. if ! _extract_record_id "$_sub_domain.$_domain"; then
  124. return 1
  125. fi
  126. _debug "Deleting TXT record: $_sub_domain.$_domain"
  127. if ! _aruba_rest DELETE "api/domains/dns/record/$_recordId"; then
  128. return 1
  129. fi
  130. return 0
  131. }
  132. #################### Private functions below ##################################
  133. # returns TXT record and put it in_record_id, if esists
  134. _extract_record_id() {
  135. subdomain="$1"
  136. _ids="$(echo "$domainData" | _egrep_o '"Id": [^,]+' | cut -d : -f 2)"
  137. _debug "$_ids"
  138. #_temp="$(echo $domainData | grep -oP "\"DomainId\":\s\d{1,}," | tr -d ' ')"
  139. #_domainids="$(echo $_temp | tr -d ' ')"
  140. _names="$(echo "$domainData" | _egrep_o '"Name": [^,]*' | cut -d : -f 2)"
  141. _debug "$_names"
  142. ARRAY_IDS=$(echo "$_ids" | tr ", " "\n")
  143. ARRAY_NAMES=$_names
  144. j=0
  145. for i in $ARRAY_NAMES; do
  146. if [ "$i" = "$subdomain" ]; then
  147. _debug printf "%s\t%s\n" "$i"
  148. #_arrayname=$i
  149. _arrayId=$j
  150. _info "Found txt record id: $_arrayId"
  151. fi
  152. j=$(_math "$j" + 1)
  153. done
  154. n=0
  155. for i in $ARRAY_IDS; do
  156. if [ "$n" = "$_arrayId" ]; then
  157. _recordId=$i
  158. _info "recordid found: $_recordId"
  159. return 0
  160. fi
  161. n=$(_math "$n" + 1)
  162. done
  163. return 1
  164. }
  165. _aruba_authentication() {
  166. export _H1="Content-Type: application/x-www-form-urlencoded"
  167. export _H2="Authorization-Key: $ARUBA_TK"
  168. _H3=""
  169. _H4=""
  170. _arubadata="grant_type=password&username=$ARUBA_AK&password=$ARUBA_AS"
  171. response="$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  172. _debug "$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  173. _debug3 response "$response"
  174. access_token="$(echo "$response" | _egrep_o "access_token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  175. if [ -z "$access_token" ]; then
  176. _err "Unable to get access_token"
  177. return 1
  178. fi
  179. _secure_debug access_token "$access_token"
  180. ARUBA_CK="$access_token"
  181. _saveaccountconf ARUBA_CK "$ARUBA_CK"
  182. return 0
  183. }
  184. _aruba_rest() {
  185. m=$1
  186. ep="$2"
  187. data="$3"
  188. _debug "$ep"
  189. _aruba_url="$ARUBA_API/$ep"
  190. _debug2 _aruba_url "$_aruba_url"
  191. export _H1="Content-type: application/json"
  192. export _H2="Accept: application/json"
  193. export _H3="Authorization: Bearer $ARUBA_CK"
  194. export _H4="Authorization-Key: $ARUBA_TK"
  195. export _H5="Accept: application/json"
  196. _debug2 _H3 "$_H3"
  197. _debug2 _H4 "$_H4"
  198. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  199. _debug data "$data"
  200. response="$(_post "$data" "$_aruba_url" "" "$m")"
  201. else
  202. response="$(_get "$_aruba_url")"
  203. fi
  204. if [ "$?" != "0" ] || _contains "$response" "wrong credentials" || _contains "$response" "Unprocessable" || _contains "$response" "denied"; then
  205. _err "Response error $response"
  206. return 1
  207. fi
  208. _debug2 response "$response"
  209. return 0
  210. }