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.

192 lines
6.9 KiB

  1. #!/usr/local/bin/bash
  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. _getdeployconf DEPLOY_TRUENAS_APIKEY
  34. if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
  35. _err "TrueNAS Api Key is not found, please define DEPLOY_TRUENAS_APIKEY."
  36. return 1
  37. fi
  38. _secure_debug2 DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
  39. # Optional hostname, scheme for TrueNAS
  40. _getdeployconf DEPLOY_TRUENAS_HOSTNAME
  41. _getdeployconf DEPLOY_TRUENAS_SCHEME
  42. # default values for hostname and scheme
  43. [ -n "${DEPLOY_TRUENAS_HOSTNAME}" ] || DEPLOY_TRUENAS_HOSTNAME="localhost"
  44. [ -n "${DEPLOY_TRUENAS_SCHEME}" ] || DEPLOY_TRUENAS_SCHEME="http"
  45. _debug2 DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
  46. _debug2 DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  47. _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
  48. _debug _api_url "$_api_url"
  49. _H1="Authorization: Bearer $DEPLOY_TRUENAS_APIKEY"
  50. _secure_debug3 _H1 "$_H1"
  51. _info "Testing Connection TrueNAS"
  52. _response=$(_get "$_api_url/system/state")
  53. _info "TrueNAS System State: $_response."
  54. if [ -z "$_response" ]; then
  55. _err "Unable to authenticate to $_api_url."
  56. _err 'Check your Connection and set DEPLOY_TRUENAS_HOSTNAME="192.168.178.x".'
  57. _err 'or'
  58. _err 'set DEPLOY_TRUENAS_HOSTNAME="<truenas_dnsname>".'
  59. _err 'Check your Connection and set DEPLOY_TRUENAS_SCHEME="https".'
  60. _err "Check your Api Key."
  61. return 1
  62. fi
  63. _saveaccountconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
  64. _saveaccountconf DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
  65. _saveaccountconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  66. _info "Getting active certificate from TrueNAS"
  67. _response=$(_get "$_api_url/system/general")
  68. _active_cert_id=$(echo "$_response" | grep -B2 '"name":' | grep 'id' | tr -d -- '"id: ,')
  69. _active_cert_name=$(echo "$_response" | grep '"name":' | sed -n 's/.*: "\(.\{1,\}\)",$/\1/p')
  70. _param_httpsredirect=$(echo "$_response" | grep '"ui_httpsredirect":' | sed -n 's/.*": \(.\{1,\}\),$/\1/p')
  71. _debug Active_UI_Certificate_ID "$_active_cert_id"
  72. _debug Active_UI_Certificate_Name "$_active_cert_name"
  73. _debug Active_UI_http_redirect "$_param_httpsredirect"
  74. if [ "$DEPLOY_TRUENAS_SCHEME" = "http" ] && [ "$_param_httpsredirect" = "true" ] ; then
  75. _info "http Redirect active"
  76. _info "Setting DEPLOY_TRUENAS_SCHEME to 'https'"
  77. DEPLOY_TRUENAS_SCHEME="https"
  78. _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
  79. _saveaccountconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  80. fi
  81. _info "Upload new certifikate to TrueNAS"
  82. _date_now() {
  83. date -u "+%Y-%m-%d_%H%M%S"
  84. }
  85. _certname="Letsencrypt_$(_date_now)"
  86. _debug3 _certname "$_certname"
  87. _certData="{\"create_type\": \"CERTIFICATE_CREATE_IMPORTED\", \"name\": \"${_certname}\", \"certificate\": \"$(_json_encode <"$_cfullchain")\", \"privatekey\": \"$(_json_encode <"$_ckey")\"}"
  88. _add_cert_result="$(_post "$_certData" "$_api_url/certificate" "" "POST" "application/json")"
  89. _debug3 _add_cert_result "$_add_cert_result"
  90. _info "Getting Certificate list to get new Cert ID"
  91. _cert_list=$(_get "$_api_url/system/general/ui_certificate_choices")
  92. _cert_id=$(echo "$_cert_list" | grep "$_certname" | sed -n 's/.*"\([0-9]\{1,\}\)".*$/\1/p')
  93. _debug3 _cert_id "$_cert_id"
  94. _info "Activate Certificate ID: $_cert_id"
  95. _activateData="{\"ui_certificate\": \"${_cert_id}\"}"
  96. _activate_result="$(_post "$_activateData" "$_api_url/system/general" "" "PUT" "application/json")"
  97. _debug3 _activate_result "$_activate_result"
  98. _info "Check if WebDAV certificate is the same as the WEB UI"
  99. _webdav_list=$(_get "$_api_url/webdav")
  100. _webdav_cert_id=$(echo "$_webdav_list" | grep '"certssl":' | tr -d -- '"certsl: ,')
  101. if [ "$_webdav_cert_id" = "$_active_cert_id" ]; then
  102. _info "Update the WebDAV Certificate"
  103. _debug _webdav_cert_id "$_webdav_cert_id"
  104. _webdav_data="{\"certssl\": \"${_cert_id}\"}"
  105. _activate_webdav_cert="$(_post "$_webdav_data" "$_api_url/webdav" "" "PUT" "application/json")"
  106. _webdav_new_cert_id=$(echo "$_activate_webdav_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
  107. if [ "$_webdav_new_cert_id" -eq "$_cert_id" ]; then
  108. _info "WebDAV Certificate update successfully"
  109. else
  110. _err "Unable to set WebDAV certificate"
  111. _debug3 _activate_webdav_cert "$_activate_webdav_cert"
  112. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  113. return 1
  114. fi
  115. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  116. else
  117. _info "WebDAV certificate not set or not the same as Web UI"
  118. fi
  119. _info "Check if FTP certificate is the same as the WEB UI"
  120. _ftp_list=$(_get "$_api_url/ftp")
  121. _ftp_cert_id=$(echo "$_ftp_list" | grep '"ssltls_certificate":' | tr -d -- '"certislfa:_ ,')
  122. if [ "$_ftp_cert_id" = "$_active_cert_id" ]; then
  123. _info "Update the FTP Certificate"
  124. _debug _ftp_cert_id "$_ftp_cert_id"
  125. _ftp_data="{\"ssltls_certificate\": \"${_cert_id}\"}"
  126. _activate_ftp_cert="$(_post "$_ftp_data" "$_api_url/ftp" "" "PUT" "application/json")"
  127. _ftp_new_cert_id=$(echo "$_activate_ftp_cert" | _json_decode | sed -n 's/.*: \([0-9]\{1,\}\) }$/\1/p')
  128. if [ "$_ftp_new_cert_id" -eq "$_cert_id" ]; then
  129. _info "FTP Certificate update successfully"
  130. else
  131. _err "Unable to set FTP certificate"
  132. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  133. _debug3 _ftp_new_cert_id "$_ftp_new_cert_id"
  134. return 1
  135. fi
  136. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  137. else
  138. _info "FTP certificate not set or not the same as Web UI"
  139. fi
  140. _info "Delete old Certificate"
  141. _delete_result="$(_post "" "$_api_url/certificate/id/$_active_cert_id" "" "DELETE" "application/json")"
  142. _debug3 _delete_result "$_delete_result"
  143. _info "Reload WebUI from TrueNAS"
  144. curl --silent -L --no-keepalive --user-agent "$USER_AGENT" "$_api_url/system/general/ui_restart"
  145. ret=$?
  146. _debug CURL_RETURN "$ret"
  147. if [ "$ret" != "0" ]; then
  148. _err "Please refer to https://curl.haxx.se/libcurl/c/libcurl-errors.html for error code: $ret"
  149. return 1
  150. fi
  151. return 0
  152. }