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.

242 lines
6.9 KiB

  1. #!/usr/bin/env sh
  2. #
  3. # Acme.sh DNS API plugin for Oracle Cloud Infrastructure
  4. # Copyright (c) 2021, Oracle and/or its affiliates
  5. #
  6. # Required OCI CLI environment variables:
  7. # - OCI_CLI_TENANCY : OCID of tenancy that contains the target DNS zone
  8. # - OCI_CLI_USER : OCID of user with permission to add/remove records from zones
  9. # - OCI_CLI_REGION : Should point to the tenancy home region
  10. #
  11. # One of the following two variables is required:
  12. # - OCI_CLI_KEY_FILE: Path to private API signing key file in PEM format; or
  13. # - OCI_CLI_KEY : The private API signing key in PEM format
  14. #
  15. # NOTE: using an encrypted private key that needs a passphrase is not supported.
  16. #
  17. dns_oci_add() {
  18. _fqdn="$1"
  19. _rdata="$2"
  20. if _oci_config; then
  21. if [ "$_sub_domain" ] && [ "$_domain" ]; then
  22. _add_record_body="{\"items\":[{\"domain\":\"${_sub_domain}.${_domain}\",\"rdata\":\"$_rdata\",\"rtype\":\"TXT\",\"ttl\": 30,\"operation\":\"ADD\"}]}"
  23. response=$(_signed_request "PATCH" "/20180115/zones/${_domain}/records" "$_add_record_body")
  24. if [ "$response" ]; then
  25. _info "Success: added TXT record for ${_sub_domain}.${_domain}."
  26. else
  27. _err "Error: failed to add TXT record for ${_sub_domain}.${_domain}."
  28. return 1
  29. fi
  30. fi
  31. else
  32. return 1
  33. fi
  34. }
  35. dns_oci_rm() {
  36. _fqdn="$1"
  37. _rdata="$2"
  38. if _oci_config; then
  39. if [ "$_sub_domain" ] && [ "$_domain" ]; then
  40. _remove_record_body="{\"items\":[{\"domain\":\"${_sub_domain}.${_domain}\",\"rdata\":\"$_rdata\",\"rtype\":\"TXT\",\"operation\":\"REMOVE\"}]}"
  41. response=$(_signed_request "PATCH" "/20180115/zones/${_domain}/records" "$_remove_record_body")
  42. if [ "$response" ]; then
  43. _info "Success: removed TXT record for ${_sub_domain}.${_domain}."
  44. else
  45. _err "Error: failed to remove TXT record for ${_sub_domain}.${_domain}."
  46. return 1
  47. fi
  48. fi
  49. else
  50. return 1
  51. fi
  52. }
  53. #################### Private functions below ##################################
  54. _oci_config() {
  55. OCI_CLI_TENANCY="${OCI_CLI_TENANCY:-$(_readaccountconf_mutable OCI_CLI_TENANCY)}"
  56. OCI_CLI_USER="${OCI_CLI_USER:-$(_readaccountconf_mutable OCI_CLI_USER)}"
  57. OCI_CLI_KEY="${OCI_CLI_KEY:-$(_readaccountconf_mutable OCI_CLI_KEY)}"
  58. OCI_CLI_REGION="${OCI_CLI_REGION:-$(_readaccountconf_mutable OCI_CLI_REGION)}"
  59. _not_set=""
  60. _ret=0
  61. if [ -z "$OCI_CLI_KEY_FILE" ] && [ -z "$OCI_CLI_KEY" ]; then
  62. _err "Fatal: you must provide a value for either OCI_CLI_KEY_FILE or OCI_CLI_KEY."
  63. return 1
  64. fi
  65. if [ "$OCI_CLI_KEY_FILE" ] && [ -z "$OCI_CLI_KEY" ]; then
  66. if [ -f "$OCI_CLI_KEY_FILE" ]; then
  67. OCI_CLI_KEY=$(_base64 <"$OCI_CLI_KEY_FILE")
  68. else
  69. _err "Fatal: unable to read $OCI_CLI_KEY_FILE."
  70. return 1
  71. fi
  72. fi
  73. if [ -z "$OCI_CLI_TENANCY" ]; then
  74. _not_set="${_not_set}OCI_CLI_TENANCY "
  75. fi
  76. if [ -z "$OCI_CLI_USER" ]; then
  77. _not_set="${_not_set}OCI_CLI_USER "
  78. fi
  79. if [ -z "$OCI_CLI_REGION" ]; then
  80. _not_set="${_not_set}OCI_CLI_REGION "
  81. fi
  82. if [ "$_not_set" ]; then
  83. _err "Fatal: required environment variable(s): ${_not_set} not set."
  84. _ret=1
  85. else
  86. _saveaccountconf_mutable OCI_CLI_TENANCY "$OCI_CLI_TENANCY"
  87. _saveaccountconf_mutable OCI_CLI_USER "$OCI_CLI_USER"
  88. _saveaccountconf_mutable OCI_CLI_KEY "$OCI_CLI_KEY"
  89. _saveaccountconf_mutable OCI_CLI_REGION "$OCI_CLI_REGION"
  90. fi
  91. if ! _contains "PRIVATE KEY" "$OCI_CLI_KEY"; then
  92. OCI_CLI_KEY=$(printf "%s" "$OCI_CLI_KEY" | _dbase64 multiline)
  93. fi
  94. if ! _get_zone "$_fqdn"; then
  95. _err "Error: DNS Zone not found for $_fqdn."
  96. _ret=1
  97. fi
  98. return $_ret
  99. }
  100. # _get_zone(): retrieves the Zone name and OCID
  101. #
  102. # _sub_domain=_acme-challenge.www
  103. # _domain=domain.com
  104. # _domain_ociid=ocid1.dns-zone.oc1..
  105. _get_zone() {
  106. domain=$1
  107. i=1
  108. p=1
  109. while true; do
  110. h=$(printf "%s" "$domain" | cut -d . -f $i-100)
  111. _debug h "$h"
  112. if [ -z "$h" ]; then
  113. # not valid
  114. return 1
  115. fi
  116. _domain_id=$(_signed_request "GET" "/20180115/zones/$h" "" "id")
  117. if [ "$_domain_id" ]; then
  118. _sub_domain=$(printf "%s" "$domain" | cut -d . -f 1-$p)
  119. _domain=$h
  120. _debug _domain_id "$_domain_id"
  121. _debug _sub_domain "$_sub_domain"
  122. _debug _domain "$_domain"
  123. return 0
  124. fi
  125. p=$i
  126. i=$(_math "$i" + 1)
  127. done
  128. return 1
  129. }
  130. #Usage: privatekey
  131. #Output MD5 fingerprint
  132. _fingerprint() {
  133. pkey="$1"
  134. if [ -z "$pkey" ]; then
  135. _usage "Usage: _fingerprint privkey"
  136. return 1
  137. fi
  138. printf "%s" "$pkey" | ${ACME_OPENSSL_BIN:-openssl} rsa -pubout -outform DER 2>/dev/null | ${ACME_OPENSSL_BIN:-openssl} md5 -c | cut -d = -f 2 | tr -d ' '
  139. }
  140. _signed_request() {
  141. _sig_method="$1"
  142. _sig_target="$2"
  143. _sig_body="$3"
  144. _return_field="$4"
  145. _key_fingerprint=$(_fingerprint "$OCI_CLI_KEY")
  146. _sig_host="dns.$OCI_CLI_REGION.oraclecloud.com"
  147. _sig_keyId="$OCI_CLI_TENANCY/$OCI_CLI_USER/$_key_fingerprint"
  148. _sig_alg="rsa-sha256"
  149. _sig_version="1"
  150. _sig_now="$(LC_ALL=C \date -u "+%a, %d %h %Y %H:%M:%S GMT")"
  151. _request_method=$(printf %s "$_sig_method" | _lower_case)
  152. _curl_method=$(printf %s "$_sig_method" | _upper_case)
  153. _request_target="(request-target): $_request_method $_sig_target"
  154. _date_header="date: $_sig_now"
  155. _host_header="host: $_sig_host"
  156. _string_to_sign="$_request_target\n$_date_header\n$_host_header"
  157. _sig_headers="(request-target) date host"
  158. if [ "$_sig_body" ]; then
  159. _secure_debug3 _sig_body "$_sig_body"
  160. _sig_body_sha256="x-content-sha256: $(printf %s "$_sig_body" | _digest sha256)"
  161. _sig_body_type="content-type: application/json"
  162. _sig_body_length="content-length: ${#_sig_body}"
  163. _string_to_sign="$_string_to_sign\n$_sig_body_sha256\n$_sig_body_type\n$_sig_body_length"
  164. _sig_headers="$_sig_headers x-content-sha256 content-type content-length"
  165. fi
  166. _tmp_file=$(_mktemp)
  167. if [ -f "$_tmp_file" ]; then
  168. printf '%s' "$OCI_CLI_KEY" >"$_tmp_file"
  169. _signature=$(printf '%b' "$_string_to_sign" | _sign "$_tmp_file" sha256 | tr -d '\r\n')
  170. rm -f "$_tmp_file"
  171. fi
  172. _signed_header="Authorization: Signature version=\"$_sig_version\",keyId=\"$_sig_keyId\",algorithm=\"$_sig_alg\",headers=\"$_sig_headers\",signature=\"$_signature\""
  173. _secure_debug3 _signed_header "$_signed_header"
  174. if [ "$_curl_method" = "GET" ]; then
  175. export _H1="$_date_header"
  176. export _H2="$_signed_header"
  177. _response="$(_get "https://${_sig_host}${_sig_target}")"
  178. elif [ "$_curl_method" = "PATCH" ]; then
  179. export _H1="$_date_header"
  180. export _H2="$_sig_body_sha256"
  181. export _H3="$_sig_body_type"
  182. export _H4="$_sig_body_length"
  183. export _H5="$_signed_header"
  184. _response="$(_post "$_sig_body" "https://${_sig_host}${_sig_target}" "" "PATCH")"
  185. else
  186. _err "Unable to process method: $_curl_method."
  187. fi
  188. _ret="$?"
  189. if [ "$_return_field" ]; then
  190. _response="$(echo "$_response" | sed 's/\\\"//g'))"
  191. _return=$(echo "${_response}" | _egrep_o "\"$_return_field\"\\s*:\\s*\"[^\"]*\"" | _head_n 1 | cut -d : -f 2 | tr -d "\"")
  192. else
  193. _return="$_response"
  194. fi
  195. printf "%s" "$_return"
  196. return $_ret
  197. }