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.

267 lines
6.6 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
  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)"
  79. #read -ra ADDR <<< "$temp" #put Ids into array
  80. domain_id=$temp # first element is zone Id
  81. _info "DomainId is: $domain_id"
  82. _info "Consumer key is ok."
  83. return 0
  84. }
  85. ######## Public functions #####################
  86. #Usage: add _acme-challenge.www.domain.com "XKrxpRBosdIKFzxW_CT3KLZNf6q0HG9i01zxXp5CPBs"
  87. dns_aruba_add() {
  88. fulldomain=$1
  89. txtvalue=$2
  90. if ! _initAuth; then
  91. return 1
  92. fi
  93. _debug _domain "$_domain"
  94. _sub_domain="_acme-challenge"
  95. _debug "Check if _acme-challenge record exists in " "$_domain"
  96. if ! _extract_record_id "$_sub_domain.$_domain."; then
  97. _method="POST"
  98. else
  99. _method="PUT"
  100. fi
  101. _payload="{ \"IdDomain\": $domain_id, \"Type\": \"TXT\", \"Name\": \"$_sub_domain\", \"Content\": \"\\\"$txtvalue\\\"\" }"
  102. _info "Adding record"
  103. if _aruba_rest "$_method" "api/domains/dns/record" "$_payload"; then
  104. if _contains "$response" "$txtvalue"; then
  105. _aruba_rest GET "api/domains/dns/$_domain/details"
  106. _debug "Refresh:$response"
  107. _info "Added, sleep 10 seconds."
  108. _sleep 10
  109. return 0
  110. fi
  111. fi
  112. _err "Add txt record error."
  113. return 1
  114. }
  115. #fulldomain
  116. dns_aruba_rm() {
  117. fulldomain=$1
  118. txtvalue=$2
  119. if ! _initAuth; then
  120. return 1
  121. fi
  122. _sub_domain="_acme-challenge"
  123. _debug "Getting TXT record to delete: $_sub_domain.$_domain."
  124. if ! _extract_record_id "$_sub_domain.$_domain"; then
  125. return 1
  126. fi
  127. _debug "Deleting TXT record: $_sub_domain.$_domain"
  128. if ! _aruba_rest DELETE "api/domains/dns/record/$_recordId"; then
  129. return 1
  130. fi
  131. return 0
  132. }
  133. #################### Private functions below ##################################
  134. # returns TXT record and put it in_record_id, if esists
  135. _extract_record_id() {
  136. subdomain="$1"
  137. _arrayid=0
  138. _ids="$(echo $domainData | _egrep_o '"Id": [^,]+' | cut -d : -f 2)"
  139. _debug $ids
  140. #_temp="$(echo $domainData | grep -oP "\"DomainId\":\s\d{1,}," | tr -d ' ')"
  141. #_domainids="$(echo $_temp | tr -d ' ')"
  142. _names="$(echo $domainData | _egrep_o '"Name": [^,]*' | cut -d : -f 2)"
  143. _debug $names
  144. ARRAY_IDS=$(echo $_ids | tr ", " "\n")
  145. ARRAY_NAMES=$_names
  146. j=0
  147. for i in $ARRAY_NAMES;
  148. do
  149. if [ "$i" = "$subdomain" ]; then
  150. _debug printf "%s\t%s\n" "$i"
  151. _arrayname=$i
  152. _arrayId=$j
  153. _info "Found txt record id: $_arrayId"
  154. fi
  155. j=$(_math "$j" + 1)
  156. done
  157. n=0
  158. for i in $ARRAY_IDS;
  159. do
  160. if [ "$n" = "$_arrayId" ]; then
  161. _recordId=$i
  162. _info "recordid found: $_recordId"
  163. return 0
  164. fi
  165. n=$(_math "$n" + 1)
  166. done
  167. return 1
  168. }
  169. _aruba_authentication() {
  170. export _H1="Content-Type: application/x-www-form-urlencoded"
  171. export _H2="Authorization-Key: $ARUBA_TK"
  172. _H3=""
  173. _H4=""
  174. _arubadata="grant_type=password&username=$ARUBA_AK&password=$ARUBA_AS"
  175. response="$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  176. _debug "$(_post "$_arubadata" "$ARUBA_API/auth/token")"
  177. _debug3 response "$response"
  178. access_token="$(echo "$response" | _egrep_o "access_token\":\"[^\"]*\"" | cut -d : -f 2 | tr -d '"')"
  179. if [ -z "$access_token" ]; then
  180. _err "Unable to get access_token"
  181. return 1
  182. fi
  183. _secure_debug access_token "$access_token"
  184. ARUBA_CK="$access_token"
  185. _saveaccountconf ARUBA_CK "$ARUBA_CK"
  186. return 0
  187. }
  188. _aruba_rest() {
  189. m=$1
  190. ep="$2"
  191. data="$3"
  192. _debug "$ep"
  193. _aruba_url="$ARUBA_API/$ep"
  194. _debug2 _aruba_url "$_aruba_url"
  195. export _H1="Content-type: application/json"
  196. export _H2="Accept: application/json"
  197. export _H3="Authorization: Bearer $ARUBA_CK"
  198. export _H4="Authorization-Key: $ARUBA_TK"
  199. export _H5="Accept: application/json"
  200. _debug2 _H3 "$_H3"
  201. _debug2 _H4 "$_H4"
  202. if [ "$data" ] || [ "$m" = "POST" ] || [ "$m" = "PUT" ] || [ "$m" = "DELETE" ]; then
  203. _debug data "$data"
  204. response="$(_post "$data" "$_aruba_url" "" "$m")"
  205. else
  206. response="$(_get "$_aruba_url")"
  207. fi
  208. if [ "$?" != "0" ] || _contains "$response" "wrong credentials" || _contains "$response" "Unprocessable" || _contains "$response" "denied"; then
  209. _err "Response error $response"
  210. return 1
  211. fi
  212. _debug2 response "$response"
  213. return 0
  214. }