|
| 1 | +#!/usr/bin/env sh |
| 2 | + |
| 3 | +keyhelp_api_deploy() { |
| 4 | + _cdomain="$1" |
| 5 | + _ckey="$2" |
| 6 | + _ccert="$3" |
| 7 | + _cca="$4" |
| 8 | + |
| 9 | + _debug _cdomain "$_cdomain" |
| 10 | + _debug _ckey "$_ckey" |
| 11 | + _debug _ccert "$_ccert" |
| 12 | + _debug _cca "$_cca" |
| 13 | + |
| 14 | + # Read config from saved values or env |
| 15 | + _getdeployconf DEPLOY_KEYHELP_HOST |
| 16 | + _getdeployconf DEPLOY_KEYHELP_API_KEY |
| 17 | + |
| 18 | + _debug DEPLOY_KEYHELP_HOST "$DEPLOY_KEYHELP_HOST" |
| 19 | + _secure_debug DEPLOY_KEYHELP_API_KEY "$DEPLOY_KEYHELP_API_KEY" |
| 20 | + |
| 21 | + if [ -z "$DEPLOY_KEYHELP_HOST" ]; then |
| 22 | + _err "KeyHelp host not found, please define DEPLOY_KEYHELP_HOST." |
| 23 | + return 1 |
| 24 | + fi |
| 25 | + if [ -z "$DEPLOY_KEYHELP_API_KEY" ]; then |
| 26 | + _err "KeyHelp api key not found, please define DEPLOY_KEYHELP_API_KEY." |
| 27 | + return 1 |
| 28 | + fi |
| 29 | + |
| 30 | + # Save current values |
| 31 | + _savedeployconf DEPLOY_KEYHELP_HOST "$DEPLOY_KEYHELP_HOST" |
| 32 | + _savedeployconf DEPLOY_KEYHELP_API_KEY "$DEPLOY_KEYHELP_API_KEY" |
| 33 | + |
| 34 | + _request_key="$(tr '\n' ':' <"$_ckey" | sed 's/:/\\n/g')" |
| 35 | + _request_cert="$(tr '\n' ':' <"$_ccert" | sed 's/:/\\n/g')" |
| 36 | + _request_ca="$(tr '\n' ':' <"$_cca" | sed 's/:/\\n/g')" |
| 37 | + |
| 38 | + _request_body="{ |
| 39 | + \"name\": \"$_cdomain\", |
| 40 | + \"components\": { |
| 41 | + \"private_key\": \"$_request_key\", |
| 42 | + \"certificate\": \"$_request_cert\", |
| 43 | + \"ca_certificate\": \"$_request_ca\" |
| 44 | + } |
| 45 | + }" |
| 46 | + |
| 47 | + _hosts="$(echo "$DEPLOY_KEYHELP_HOST" | tr "," " ")" |
| 48 | + _keys="$(echo "$DEPLOY_KEYHELP_API_KEY" | tr "," " ")" |
| 49 | + _i=1 |
| 50 | + |
| 51 | + for _host in $_hosts; do |
| 52 | + _key="$(_getfield "$_keys" "$_i" " ")" |
| 53 | + _i="$(_math "$_i" + 1)" |
| 54 | + |
| 55 | + export _H1="X-API-Key: $_key" |
| 56 | + |
| 57 | + _put_url="$_host/api/v2/certificates/name/$_cdomain" |
| 58 | + if _post "$_request_body" "$_put_url" "" "PUT" "application/json" >/dev/null; then |
| 59 | + _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")" |
| 60 | + else |
| 61 | + _err "Cannot make PUT request to $_put_url" |
| 62 | + return 1 |
| 63 | + fi |
| 64 | + |
| 65 | + if [ "$_code" = "404" ]; then |
| 66 | + _info "$_cdomain not found, creating new entry at $_host" |
| 67 | + |
| 68 | + _post_url="$_host/api/v2/certificates" |
| 69 | + if _post "$_request_body" "$_post_url" "" "POST" "application/json" >/dev/null; then |
| 70 | + _code="$(grep "^HTTP" "$HTTP_HEADER" | _tail_n 1 | cut -d " " -f 2 | tr -d "\r\n")" |
| 71 | + else |
| 72 | + _err "Cannot make POST request to $_post_url" |
| 73 | + return 1 |
| 74 | + fi |
| 75 | + fi |
| 76 | + |
| 77 | + if _startswith "$_code" "2"; then |
| 78 | + _info "$_cdomain set at $_host" |
| 79 | + else |
| 80 | + _err "HTTP status code is $_code" |
| 81 | + return 1 |
| 82 | + fi |
| 83 | + done |
| 84 | + |
| 85 | + return 0 |
| 86 | +} |
0 commit comments