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.

204 lines
6.3 KiB

  1. #!/usr/bin/env sh
  2. # Here is a script to deploy certificates to CloudHub V2 using Anypoint Platform REST APIs via curl
  3. # A TLS Context is created and the certificates deployed on it
  4. # (https://docs.mulesoft.com/cloudhub-2/ps-config-domains/)
  5. #
  6. # This script will use Connected Apps - Client Credentials
  7. # The App must have "Cloudhub Network Administrator" or "Cloudhub Organization Admin" scope
  8. # (https://docs.mulesoft.com/access-management/connected-apps-developers#developers)
  9. #
  10. # It requires following environment variables:
  11. #
  12. # CLIENT_ID - Connected App Client ID
  13. # CLIENT_SECRET - Connected App Client Secret
  14. # PRIVATE_SPACE_ID - Private Space ID where the TLS Context will be created
  15. #
  16. #
  17. #returns 0 means success, otherwise error.
  18. ######## Public functions #####################
  19. #!/usr/bin/env sh
  20. #Here is a sample custom api script.
  21. #This file name is "myapi.sh"
  22. #So, here must be a method myapi_deploy()
  23. #Which will be called by acme.sh to deploy the cert
  24. #returns 0 means success, otherwise error.
  25. ######## Public functions #####################
  26. #domain keyfile certfile cafile fullchain
  27. cloudhub_v2_deploy() {
  28. _cdomain="$1"
  29. _ckey="$2"
  30. _ccert="$3"
  31. _cca="$4"
  32. _cfullchain="$5"
  33. _debug _cdomain "$_cdomain"
  34. _debug _ckey "$_ckey"
  35. _debug _ccert "$_ccert"
  36. _debug _cca "$_cca"
  37. _debug _cfullchain "$_cfullchain"
  38. _getdeployconf CH2_CLIENT_ID
  39. _getdeployconf CH2_CLIENT_SECRET
  40. _getdeployconf ORGANIZATION_ID
  41. _getdeployconf CH2_PRIVATE_SPACE_ID
  42. # Validate required env vars
  43. if [ -z "$CH2_CLIENT_ID" ]; then
  44. _err "Connected App CH2_CLIENT_ID not defined."
  45. return 1
  46. fi
  47. if [ -z "$CH2_CLIENT_SECRET" ]; then
  48. _err "Connected App CH2_CLIENT_SECRET not defined."
  49. return 1
  50. fi
  51. if [ -z "$ORGANIZATION_ID" ]; then
  52. _err "ORGANIZATION_ID not defined."
  53. return 1
  54. fi
  55. if [ -z "$CH2_PRIVATE_SPACE_ID" ]; then
  56. _err "CH2_PRIVATE_SPACE_ID not defined."
  57. return 1
  58. fi
  59. # Set Anypoint Platform URL
  60. if [ -z "$ANYPOINT_URL" ]; then
  61. _debug "ANYPOINT_URL Not set, using default https://anypoint.mulesoft.com"
  62. ANYPOINT_URL="https://anypoint.mulesoft.com"
  63. fi
  64. _savedeployconf CH2_CLIENT_ID "$CH2_CLIENT_ID"
  65. _savedeployconf CH2_CLIENT_SECRET "$CH2_CLIENT_SECRET"
  66. _savedeployconf ORGANIZATION_ID "$ORGANIZATION_ID"
  67. _savedeployconf CH2_PRIVATE_SPACE_ID "$CH2_PRIVATE_SPACE_ID"
  68. _savedeployconf ANYPOINT_URL "$ANYPOINT_URL"
  69. # Anypoint Platform access token
  70. _info "Obtaining a Anypoint Platform access token"
  71. token_data="{\"grant_type\": \"client_credentials\", \"client_id\": \"${CH2_CLIENT_ID}\", \"client_secret\": \"${CH2_CLIENT_SECRET}\"}"
  72. _debug token_data "$token_data"
  73. token_response="$(_cloudhub_rest "POST" "/accounts/api/v2/oauth2/token" "$token_data")"
  74. _ret="$?"
  75. if [ "$_ret" != 0 ]; then
  76. _err "Error while creating token"
  77. return 1
  78. fi
  79. regex_token=".*\"access_token\":\"\([-._0-9A-Za-z]*\)\".*$"
  80. _debug regex_token "$regex_token"
  81. access_token=$(echo "$token_response" | _json_decode | sed -n "s/$regex_token/\1/p")
  82. _debug access_token "$access_token"
  83. export _H1="Authorization: Bearer ${access_token}"
  84. # Get TLS-Context
  85. tls_context_name=$(echo "$_cdomain" | tr '.' '-' | tr '*' 'x')
  86. tls_context_id=$(_get_tls_context_id "$tls_context_name")
  87. _ret="$?"
  88. if [ "$_ret" != 0 ]; then
  89. _err "Error while retrieving TLS-Context"
  90. return 1
  91. fi
  92. _debug tls_context_id "$tls_context_id"
  93. cert_data="{\"name\":\"$tls_context_name\", \"tlsConfig\": {\"keyStore\":{\"source\":\"PEM\",\"certificate\":\"$(_json_encode <"$_ccert")\", \"key\":\"$(_json_encode <"$_ckey")\", \"capath\":\"$(_json_encode <"$_cca")\"}}}"
  94. if [ -z "$tls_context_id" ]; then
  95. #Post certificate to Private Space
  96. _info "Creating a new TLS-Context with name: $tls_context_name"
  97. cert_response="$(_cloudhub_rest "POST" "/runtimefabric/api/organizations/$ORGANIZATION_ID/privatespaces/$CH2_PRIVATE_SPACE_ID/tlsContexts" "$cert_data")"
  98. else
  99. #Patch certificate to Private Space
  100. _info "Updating TLS-Context with name: $tls_context_name and id: $tls_context_id"
  101. cert_response="$(_cloudhub_rest "PATCH" "/runtimefabric/api/organizations/$ORGANIZATION_ID/privatespaces/$CH2_PRIVATE_SPACE_ID/tlsContexts/$tls_context_id" "$cert_data")"
  102. fi
  103. _debug cert_response "$cert_response"
  104. _ret="$?"
  105. if [ "$_ret" != 0 ]; then
  106. _err "Error while creating/updating TLS-Context"
  107. return 1
  108. fi
  109. _info "Certificate deployed!"
  110. }
  111. #################### Private functions below ##################################
  112. # Retrieve TLS Context If from Private Space
  113. #returns
  114. # tls_context_id
  115. _get_tls_context_id() {
  116. _domain=$1
  117. # Get Tls-Context
  118. tls_context_response="$(_cloudhub_rest "GET" "/runtimefabric/api/organizations/$ORGANIZATION_ID/privatespaces/$CH2_PRIVATE_SPACE_ID/tlsContexts" | _normalizeJson)"
  119. _ret="$?"
  120. if [ "$_ret" != 0 ]; then
  121. return 1
  122. fi
  123. if _contains "$tls_context_response" "\"name\":\"$_domain\"" >/dev/null; then
  124. tlscontext_list=$(echo "$tls_context_response" | _egrep_o "\"id\":\".*\",\"name\":\"$_domain\"")
  125. if [ "$tlscontext_list" ]; then
  126. regex_id=".*\"id\":\"\([-._0-9A-Za-z]*\)\".*$"
  127. tls_context_id=$(echo "$tlscontext_list" | sed -n "s/$regex_id/\1/p")
  128. if [ "$tls_context_id" ]; then
  129. _debug "TLS-Context id: $tls_context_id found! The script will update it."
  130. printf "%s" "$tls_context_id"
  131. return 0
  132. fi
  133. _err "Can't extract TLS-Context id from: $tlscontext_list"
  134. return 1
  135. fi
  136. fi
  137. return 0
  138. }
  139. _cloudhub_rest() {
  140. _method=$1
  141. _path="$2"
  142. _data="$3"
  143. # clear headers from previous request to avoid getting wrong http code
  144. : >"$HTTP_HEADER"
  145. _debug data "$_data"
  146. if [ "$_method" != "GET" ]; then
  147. response="$(_post "$_data" "$ANYPOINT_URL""$_path" "" "$_method" "application/json")"
  148. else
  149. response="$(_get "$ANYPOINT_URL""$_path")"
  150. fi
  151. _ret="$?"
  152. http_code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\\r\\n")"
  153. _debug "HTTP status $http_code"
  154. _debug response "$response"
  155. _debug _ret "$_ret"
  156. if [ "$_ret" = "0" ] && { [ "$http_code" -ge 200 ] && [ "$http_code" -le 299 ];}; then
  157. printf "%s" "$response"
  158. return 0
  159. else
  160. _err "Error sending request to $_path"
  161. _err "HTTP Status $http_code"
  162. _err "Response $response"
  163. return 1
  164. fi
  165. }