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.

272 lines
12 KiB

2 days ago
2 days ago
  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. _getdeployconf DEPLOY_TRUENAS_APIKEY
  34. if [ -z "$DEPLOY_TRUENAS_APIKEY" ]; then
  35. _err "TrueNAS API key not found, please set the DEPLOY_TRUENAS_APIKEY environment variable."
  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. _info "Getting TrueNAS version"
  55. _response=$(_get "$_api_url/system/version")
  56. if echo "$_response" | grep -q "SCALE"; then
  57. _truenas_os=$(echo "$_response" | cut -d '-' -f 2)
  58. _truenas_version=$(echo "$_response" | cut -d '-' -f 3 | tr -d '"' | cut -d '.' -f 1,2)
  59. else
  60. _truenas_os="unknown"
  61. _truenas_version="unknown"
  62. fi
  63. _info "Detected TrueNAS system os: $_truenas_os"
  64. _info "Detected TrueNAS system version: $_truenas_version"
  65. if [ -z "$_response" ]; then
  66. _err "Unable to authenticate to $_api_url."
  67. _err 'Check your connection settings are correct, e.g.'
  68. _err 'DEPLOY_TRUENAS_HOSTNAME="192.168.x.y" or DEPLOY_TRUENAS_HOSTNAME="truenas.example.com".'
  69. _err 'DEPLOY_TRUENAS_SCHEME="https" or DEPLOY_TRUENAS_SCHEME="http".'
  70. _err "Verify your TrueNAS API key is valid and set correctly, e.g. DEPLOY_TRUENAS_APIKEY=xxxx...."
  71. return 1
  72. fi
  73. _savedeployconf DEPLOY_TRUENAS_APIKEY "$DEPLOY_TRUENAS_APIKEY"
  74. _savedeployconf DEPLOY_TRUENAS_HOSTNAME "$DEPLOY_TRUENAS_HOSTNAME"
  75. _savedeployconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  76. _info "Getting current active certificate from TrueNAS"
  77. _response=$(_get "$_api_url/system/general")
  78. _active_cert_id=$(echo "$_response" | grep -B2 '"name":' | grep 'id' | tr -d -- '"id: ,')
  79. _active_cert_name=$(echo "$_response" | grep '"name":' | sed -n 's/.*: "\(.\{1,\}\)",$/\1/p')
  80. _param_httpsredirect=$(echo "$_response" | grep '"ui_httpsredirect":' | sed -n 's/.*": \(.\{1,\}\),$/\1/p')
  81. _debug Active_UI_Certificate_ID "$_active_cert_id"
  82. _debug Active_UI_Certificate_Name "$_active_cert_name"
  83. _debug Active_UI_http_redirect "$_param_httpsredirect"
  84. if [ "$DEPLOY_TRUENAS_SCHEME" = "http" ] && [ "$_param_httpsredirect" = "true" ]; then
  85. _info "HTTP->HTTPS redirection is enabled"
  86. _info "Setting DEPLOY_TRUENAS_SCHEME to 'https'"
  87. DEPLOY_TRUENAS_SCHEME="https"
  88. _api_url="$DEPLOY_TRUENAS_SCHEME://$DEPLOY_TRUENAS_HOSTNAME/api/v2.0"
  89. _savedeployconf DEPLOY_TRUENAS_SCHEME "$DEPLOY_TRUENAS_SCHEME"
  90. fi
  91. _info "Uploading new certificate to TrueNAS"
  92. _certname="Letsencrypt_$(_utc_date | tr ' ' '_' | tr -d -- ':')"
  93. _debug3 _certname "$_certname"
  94. _certData="{\"create_type\": \"CERTIFICATE_CREATE_IMPORTED\", \"name\": \"${_certname}\", \"certificate\": \"$(_json_encode <"$_cfullchain")\", \"privatekey\": \"$(_json_encode <"$_ckey")\"}"
  95. _add_cert_result="$(_post "$_certData" "$_api_url/certificate" "" "POST" "application/json")"
  96. _debug3 _add_cert_result "$_add_cert_result"
  97. _info "Fetching list of installed certificates"
  98. _cert_list=$(_get "$_api_url/system/general/ui_certificate_choices")
  99. _cert_id=$(echo "$_cert_list" | grep "$_certname" | sed -n 's/.*"\([0-9]\{1,\}\)".*$/\1/p')
  100. _debug3 _cert_id "$_cert_id"
  101. _info "Current activate certificate ID: $_cert_id"
  102. _activateData="{\"ui_certificate\": \"${_cert_id}\"}"
  103. _activate_result="$(_post "$_activateData" "$_api_url/system/general" "" "PUT" "application/json")"
  104. _debug3 _activate_result "$_activate_result"
  105. _truenas_version_23_10="23.10"
  106. _truenas_version_24_10="24.10"
  107. _check_version=$(printf "%s\n%s" "$_truenas_version_23_10" "$_truenas_version" | sort -V | head -n 1)
  108. if [ "$_truenas_os" != "SCALE" ] || [ "$_check_version" != "$_truenas_version_23_10" ]; then
  109. _info "Checking if WebDAV certificate is the same as the TrueNAS web UI"
  110. _webdav_list=$(_get "$_api_url/webdav")
  111. _webdav_cert_id=$(echo "$_webdav_list" | grep '"certssl":' | tr -d -- '"certsl: ,')
  112. if [ "$_webdav_cert_id" = "$_active_cert_id" ]; then
  113. _info "Updating the WebDAV certificate"
  114. _debug _webdav_cert_id "$_webdav_cert_id"
  115. _webdav_data="{\"certssl\": \"${_cert_id}\"}"
  116. _activate_webdav_cert="$(_post "$_webdav_data" "$_api_url/webdav" "" "PUT" "application/json")"
  117. _webdav_new_cert_id=$(echo "$_activate_webdav_cert" | _json_decode | grep '"certssl":' | sed -n 's/.*: \([0-9]\{1,\}\),\{0,1\}$/\1/p')
  118. if [ "$_webdav_new_cert_id" -eq "$_cert_id" ]; then
  119. _info "WebDAV certificate updated successfully"
  120. else
  121. _err "Unable to set WebDAV certificate"
  122. _debug3 _activate_webdav_cert "$_activate_webdav_cert"
  123. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  124. return 1
  125. fi
  126. _debug3 _webdav_new_cert_id "$_webdav_new_cert_id"
  127. else
  128. _info "WebDAV certificate is not configured or is not the same as TrueNAS web UI"
  129. fi
  130. _info "Checking if S3 certificate is the same as the TrueNAS web UI"
  131. _s3_list=$(_get "$_api_url/s3")
  132. _s3_cert_id=$(echo "$_s3_list" | grep '"certificate":' | tr -d -- '"certifa:_ ,')
  133. if [ "$_s3_cert_id" = "$_active_cert_id" ]; then
  134. _info "Updating the S3 certificate"
  135. _debug _s3_cert_id "$_s3_cert_id"
  136. _s3_data="{\"certificate\": \"${_cert_id}\"}"
  137. _activate_s3_cert="$(_post "$_s3_data" "$_api_url/s3" "" "PUT" "application/json")"
  138. _s3_new_cert_id=$(echo "$_activate_s3_cert" | _json_decode | grep '"certificate":' | sed -n 's/.*: \([0-9]\{1,\}\),\{0,1\}$/\1/p')
  139. if [ "$_s3_new_cert_id" -eq "$_cert_id" ]; then
  140. _info "S3 certificate updated successfully"
  141. else
  142. _err "Unable to set S3 certificate"
  143. _debug3 _activate_s3_cert "$_activate_s3_cert"
  144. _debug3 _s3_new_cert_id "$_s3_new_cert_id"
  145. return 1
  146. fi
  147. _debug3 _activate_s3_cert "$_activate_s3_cert"
  148. else
  149. _info "S3 certificate is not configured or is not the same as TrueNAS web UI"
  150. fi
  151. fi
  152. if [ "$_truenas_os" = "SCALE" ]; then
  153. _check_version=$(printf "%s\n%s" "$_truenas_version_24_10" "$_truenas_version" | sort -V | head -n 1)
  154. if [ "$_check_version" != "$_truenas_version_24_10" ]; then
  155. _info "Checking if any chart release Apps is using the same certificate as TrueNAS web UI. Tool 'jq' is required"
  156. if _exists jq; then
  157. _info "Query all chart release"
  158. _release_list=$(_get "$_api_url/chart/release")
  159. _related_name_list=$(printf "%s" "$_release_list" | jq -r "[.[] | {name,certId: .config.ingress?.main.tls[]?.scaleCert} | select(.certId==$_active_cert_id) | .name ] | unique")
  160. _release_length=$(printf "%s" "$_related_name_list" | jq -r "length")
  161. _info "Found $_release_length related chart release in list: $_related_name_list"
  162. for i in $(seq 0 $((_release_length - 1))); do
  163. _release_name=$(echo "$_related_name_list" | jq -r ".[$i]")
  164. _info "Updating certificate from $_active_cert_id to $_cert_id for chart release: $_release_name"
  165. #Read the chart release configuration
  166. _chart_config=$(printf "%s" "$_release_list" | jq -r ".[] | select(.name==\"$_release_name\")")
  167. #Replace the old certificate id with the new one in path .config.ingress.main.tls[].scaleCert. Then update .config.ingress
  168. _updated_chart_config=$(printf "%s" "$_chart_config" | jq "(.config.ingress?.main.tls[]? | select(.scaleCert==$_active_cert_id) | .scaleCert ) |= $_cert_id | .config.ingress ")
  169. _update_chart_result="$(_post "{\"values\" : { \"ingress\" : $_updated_chart_config } }" "$_api_url/chart/release/id/$_release_name" "" "PUT" "application/json")"
  170. _debug3 _update_chart_result "$_update_chart_result"
  171. done
  172. else
  173. _info "Tool 'jq' does not exists, skip chart release checking"
  174. fi
  175. else
  176. _info "Checking if any app is using the same certificate as TrueNAS web UI. Tool 'jq' is required"
  177. if _exists jq; then
  178. _info "Query all apps"
  179. _app_list=$(_get "$_api_url/app")
  180. _app_id_list=$(printf "%s" "$_app_list" | jq -r '.[].name')
  181. _app_length=$(echo "$_app_id_list" | wc -l)
  182. _info "Found $_app_length apps"
  183. _info "Checking for each app if an update is needed"
  184. for i in $(seq 1 "$_app_length"); do
  185. _app_id=$(echo "$_app_id_list" | sed -n "${i}p")
  186. _app_config="$(_post "\"$_app_id\"" "$_api_url/app/config" "" "POST" "application/json")"
  187. # Check if the app use the same certificate TrueNAS web UI
  188. _app_active_cert_config=$(echo "$_app_config" | _json_decode | jq -r ".ix_certificates[\"$_active_cert_id\"]")
  189. if [ "$_app_active_cert_config" != "null" ]; then
  190. _info "Updating certificate from $_active_cert_id to $_cert_id for app: $_app_id"
  191. #Replace the old certificate id with the new one in path
  192. _update_app_result="$(_post "{\"values\" : { \"network\": { \"certificate_id\": $_cert_id } } }" "$_api_url/app/id/$_app_id" "" "PUT" "application/json")"
  193. _debug3 _update_app_result "$_update_app_result"
  194. fi
  195. done
  196. else
  197. _info "Tool 'jq' does not exists, skip app checking"
  198. fi
  199. fi
  200. fi
  201. _info "Checking if FTP certificate is the same as the TrueNAS web UI"
  202. _ftp_list=$(_get "$_api_url/ftp")
  203. _ftp_cert_id=$(echo "$_ftp_list" | grep '"ssltls_certificate":' | tr -d -- '"certislfa:_ ,')
  204. if [ "$_ftp_cert_id" = "$_active_cert_id" ]; then
  205. _info "Updating the FTP certificate"
  206. _debug _ftp_cert_id "$_ftp_cert_id"
  207. _ftp_data="{\"ssltls_certificate\": \"${_cert_id}\"}"
  208. _activate_ftp_cert="$(_post "$_ftp_data" "$_api_url/ftp" "" "PUT" "application/json")"
  209. _ftp_new_cert_id=$(echo "$_activate_ftp_cert" | _json_decode | grep '"ssltls_certificate":' | sed -n 's/.*: \([0-9]\{1,\}\),\{0,1\}$/\1/p')
  210. if [ "$_ftp_new_cert_id" -eq "$_cert_id" ]; then
  211. _info "FTP certificate updated successfully"
  212. else
  213. _err "Unable to set FTP certificate"
  214. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  215. _debug3 _ftp_new_cert_id "$_ftp_new_cert_id"
  216. return 1
  217. fi
  218. _debug3 _activate_ftp_cert "$_activate_ftp_cert"
  219. else
  220. _info "FTP certificate is not configured or is not the same as TrueNAS web UI"
  221. fi
  222. _info "Deleting old certificate"
  223. _delete_result="$(_post "" "$_api_url/certificate/id/$_active_cert_id" "" "DELETE" "application/json")"
  224. _debug3 _delete_result "$_delete_result"
  225. _info "Reloading TrueNAS web UI"
  226. _restart_UI=$(_get "$_api_url/system/general/ui_restart")
  227. _debug2 _restart_UI "$_restart_UI"
  228. if [ -n "$_add_cert_result" ] && [ -n "$_activate_result" ]; then
  229. return 0
  230. else
  231. _err "Certificate update was not succesful, please try again with --debug"
  232. return 1
  233. fi
  234. }