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.

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