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.

196 lines
7.6 KiB

  1. #!/usr/bin/env sh
  2. # Here is a scipt to deploy the cert to your TrueNAS using the REST API.
  3. # https://www.truenas.com/docs/hub/additional-topics/api/rest_api.html
  4. #
  5. # Written by Frank Plass github@f-plass.de
  6. # https://github.com/danb35/deploy-freenas/blob/master/deploy_freenas.py
  7. # Thanks to danb35 for your template!
  8. #
  9. # Following environment variables must be set:
  10. #
  11. # export DEPLOY_TRUENAS_APIKEY="<API_KEY_GENERATED_IN_THE_WEB_UI"
  12. #
  13. # The following environmental variables may be set if you don't like their
  14. # default values:
  15. #
  16. # DEPLOY_TRUENAS_HOSTNAME - defaults to localhost
  17. # DEPLOY_TRUENAS_SCHEME - defaults to http, set alternatively to https
  18. #
  19. #returns 0 means success, otherwise error.
  20. ######## Public functions #####################
  21. #domain keyfile certfile cafile fullchain
  22. truenas_deploy() {
  23. _cdomain="$1"
  24. _ckey="$2"
  25. _ccert="$3"
  26. _cca="$4"
  27. _cfullchain="$5"
  28. _debug _cdomain "$_cdomain"
  29. _debug _ckey "$_ckey"
  30. _debug _ccert "$_ccert"
  31. _debug _cca "$_cca"
  32. _debug _cfullchain "$_cfullchain"
  33. if _exists "curl"; then
  34. _debug "curl found, no Message to restartUI error"
  35. else
  36. if _exists "wget"; then
  37. _err "Until Version of TrueNAS is older than TrueNAS-12.0-U2 there are problems with using wget"
  38. _err "There is a bug when using the API Call restartUI with wget"
  39. _err "The API call does not give any response, whit wget the api call restartUI would be called about 20 times"
  40. _err "Please use curl!"
  41. _err "Bug Report at https://jira.ixsystems.com/browse/NAS-109435"
  42. return 1
  43. fi
  44. fi
  45. _getdeployconf DEPLOY_TRUENAS_APIKEY
  46. if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
  47. _err "TrueNAS Api Key is not found, please define DEPLOY_TRUENAS_APIKEY."
  48. return 1
  49. fi
  50. _secure_debug2 DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
  51. # Optional hostname, scheme for TrueNAS
  52. _getdeployconf DEPLOY_TRUENAS_HOSTNAME
  53. _getdeployconf DEPLOY_TRUENAS_SCHEME
  54. # default values for hostname and scheme
  55. [ -n "${DEPLOY_TRUENAS_HOSTNAME}" ] || DEPLOY_TRUENAS_HOSTNAME="localhost"
  56. [ -n "${DEPLOY_TRUENAS_SCHEME}" ] || DEPLOY_TRUENAS_SCHEME="http"
  57. _debug2 DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
  58. _debug2 DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  59. _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
  60. _debug _api_url "$_api_url"
  61. _H1="Authorization: Bearer $DEPLOY_TRUENAS_APIKEY"
  62. _secure_debug3 _H1 "$_H1"
  63. _info "Testing Connection TrueNAS"
  64. _response=$(_get "$_api_url/system/state")
  65. _info "TrueNAS System State: $_response."
  66. if [ -z "$_response" ]; then
  67. _err "Unable to authenticate to $_api_url."
  68. _err 'Check your Connection and set DEPLOY_TRUENAS_HOSTNAME="192.168.178.x".'
  69. _err 'or'
  70. _err 'set DEPLOY_TRUENAS_HOSTNAME="<truenas_dnsname>".'
  71. _err 'Check your Connection and set DEPLOY_TRUENAS_SCHEME="https".'
  72. _err "Check your Api Key."
  73. return 1
  74. fi
  75. _savedeployconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
  76. _savedeployconf DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
  77. _savedeployconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  78. _info "Getting active certificate from TrueNAS"
  79. _response=$(_get "$_api_url/system/general")
  80. _active_cert_id=$(echo "$_response" | grep -B2 '"name":' | grep 'id' | tr -d -- '"id: ,')
  81. _active_cert_name=$(echo "$_response" | grep '"name":' | sed -n 's/.*: "\(.\{1,\}\)",$/\1/p')
  82. _param_httpsredirect=$(echo "$_response" | grep '"ui_httpsredirect":' | sed -n 's/.*": \(.\{1,\}\),$/\1/p')
  83. _debug Active_UI_Certificate_ID "$_active_cert_id"
  84. _debug Active_UI_Certificate_Name "$_active_cert_name"
  85. _debug Active_UI_http_redirect "$_param_httpsredirect"
  86. if [ "$DEPLOY_TRUENAS_SCHEME" = "http" ] && [ "$_param_httpsredirect" = "true" ]; then
  87. _info "http Redirect active"
  88. _info "Setting DEPLOY_TRUENAS_SCHEME to 'https'"
  89. DEPLOY_TRUENAS_SCHEME="https"
  90. _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
  91. _savedeployconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  92. fi
  93. _info "Upload new certifikate to TrueNAS"
  94. _certname="Letsencrypt_$(_utc_date | tr ' ' '_' | tr -d -- ':')"
  95. _debug3 _certname "$_certname"
  96. _certData="{\"create_type\": \"CERTIFICATE_CREATE_IMPORTED\", \"name\": \"${_certname}\", \"certificate\": \"$(_json_encode <"$_cfullchain")\", \"privatekey\": \"$(_json_encode <"$_ckey")\"}"
  97. _add_cert_result="$(_post "$_certData" "$_api_url/certificate" "" "POST" "application/json")"
  98. _debug3 _add_cert_result "$_add_cert_result"
  99. _info "Getting Certificate list to get new Cert ID"
  100. _cert_list=$(_get "$_api_url/system/general/ui_certificate_choices")
  101. _cert_id=$(echo "$_cert_list" | grep "$_certname" | sed -n 's/.*"\([0-9]\{1,\}\)".*$/\1/p')
  102. _debug3 _cert_id "$_cert_id"
  103. _info "Activate Certificate ID: $_cert_id"
  104. _activateData="{\"ui_certificate\": \"${_cert_id}\"}"
  105. _activate_result="$(_post "$_activateData" "$_api_url/system/general" "" "PUT" "application/json")"
  106. _debug3 _activate_result "$_activate_result"
  107. _info "Check if WebDAV certificate is the same as the WEB UI"
  108. _webdav_list=$(_get "$_api_url/webdav")
  109. _webdav_cert_id=$(echo "$_webdav_list" | grep '"certssl":' | tr -d -- '"certsl: ,')
  110. if [ "$_webdav_cert_id" = "$_active_cert_id" ]; then
  111. _info "Update the WebDAV Certificate"
  112. _debug _webdav_cert_id "$_webdav_cert_id"
  113. _webdav_data="{\"certssl\": \"${_cert_id}\"}"
  114. _activate_webdav_cert="$(_post "$_webdav_data" "$_api_url/webdav" "" "PUT" "application/json")"
  115. _webdav_new_cert_id=$(echo "$_activate_webdav_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
  116. if [ "$_webdav_new_cert_id" -eq "$_cert_id" ]; then
  117. _info "WebDAV Certificate update successfully"
  118. else
  119. _err "Unable to set WebDAV certificate"
  120. _debug3 _activate_webdav_cert "$_activate_webdav_cert"
  121. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  122. return 1
  123. fi
  124. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  125. else
  126. _info "WebDAV certificate not set or not the same as Web UI"
  127. fi
  128. _info "Check if FTP certificate is the same as the WEB UI"
  129. _ftp_list=$(_get "$_api_url/ftp")
  130. _ftp_cert_id=$(echo "$_ftp_list" | grep '"ssltls_certificate":' | tr -d -- '"certislfa:_ ,')
  131. if [ "$_ftp_cert_id" = "$_active_cert_id" ]; then
  132. _info "Update the FTP Certificate"
  133. _debug _ftp_cert_id "$_ftp_cert_id"
  134. _ftp_data="{\"ssltls_certificate\": \"${_cert_id}\"}"
  135. _activate_ftp_cert="$(_post "$_ftp_data" "$_api_url/ftp" "" "PUT" "application/json")"
  136. _ftp_new_cert_id=$(echo "$_activate_ftp_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
  137. if [ "$_ftp_new_cert_id" -eq "$_cert_id" ]; then
  138. _info "FTP Certificate update successfully"
  139. else
  140. _err "Unable to set FTP certificate"
  141. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  142. _debug3 _ftp_new_cert_id "$_ftp_new_cert_id"
  143. return 1
  144. fi
  145. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  146. else
  147. _info "FTP certificate not set or not the same as Web UI"
  148. fi
  149. _info "Delete old Certificate"
  150. _delete_result="$(_post "" "$_api_url/certificate/id/$_active_cert_id" "" "DELETE" "application/json")"
  151. _debug3 _delete_result "$_delete_result"
  152. _info "Reload WebUI from TrueNAS"
  153. _restart_UI=$(_get "$_api_url/system/general/ui_restart")
  154. _info "Until Version of TrueNAS is older than TrueNAS-12.0-U3 curl returns error 52"
  155. _info "This is not a problem for tis scipt"
  156. _info "See Bugreport: https://jira.ixsystems.com/browse/NAS-109435"
  157. _debug2 _restart_UI "$_restart_UI"
  158. if [ -n "$_add_cert_result" ] && [ -n "$_activate_result" ] && [ "$_ret" = "52" ]; then
  159. return 0
  160. else
  161. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $_ret"
  162. return 1
  163. fi
  164. }