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.

223 lines
8.5 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
2 years ago
5 years ago
5 years ago
  1. #!/usr/bin/env sh
  2. # Script to deploy certificates to Palo Alto Networks PANOS via API
  3. # Note PANOS API KEY and IP address needs to be set prior to running.
  4. # The following variables exported from environment will be used.
  5. # If not set then values previously saved in domain.conf file are used.
  6. #
  7. # Firewall admin with superuser and IP address is required.
  8. #
  9. # REQURED:
  10. # export PANOS_HOST="" # required
  11. # export PANOS_USER="" # required
  12. #
  13. # AND one of the two authenticiation methods:
  14. #
  15. # Method 1: Password (RECOMMENDED)
  16. # export PANOS_PASS=""
  17. #
  18. # Method 2: API KEY
  19. # export PANOS_KEY=""
  20. #
  21. #
  22. # The Password method will automatically generate a new API key if
  23. # no key is found, or if a saved key has expired or is invalid.
  24. #
  25. # This function is to parse the XML response from the firewall
  26. parse_response() {
  27. type=$2
  28. if [ "$type" = 'keygen' ]; then
  29. status=$(echo "$1" | sed 's/^.*\(['\'']\)\([a-z]*\)'\''.*/\2/g')
  30. if [ "$status" = "success" ]; then
  31. panos_key=$(echo "$1" | sed 's/^.*\(<key>\)\(.*\)<\/key>.*/\2/g')
  32. _panos_key=$panos_key
  33. else
  34. message="PAN-OS Key could not be set."
  35. fi
  36. else
  37. status=$(echo "$1" | sed 's/^.*"\([a-z]*\)".*/\1/g')
  38. message=$(echo "$1" | sed 's/^.*<result>\(.*\)<\/result.*/\1/g')
  39. if [ "$type" = 'keytest' ] && [ "$status" != "success" ]; then
  40. _debug "**** API Key has EXPIRED or is INVALID ****"
  41. unset _panos_key
  42. fi
  43. fi
  44. return 0
  45. }
  46. #This function is used to deploy to the firewall
  47. deployer() {
  48. content=""
  49. type=$1 # Types are keytest, keygen, cert, key, commit
  50. panos_url="https://$_panos_host/api/"
  51. #Test API Key by performing an empty commit.
  52. if [ "$type" = 'keytest' ]; then
  53. _debug "**** Testing saved API Key ****"
  54. _H1="Content-Type: application/x-www-form-urlencoded"
  55. content="type=commit&key=$_panos_key&action=partial&cmd=<commit><partial><admin><member>acmekeytest</member></admin></partial></commit>"
  56. fi
  57. # Generate API Key
  58. if [ "$type" = 'keygen' ]; then
  59. _debug "**** Generating new API Key ****"
  60. _H1="Content-Type: application/x-www-form-urlencoded"
  61. content="type=keygen&user=$_panos_user&password=$_panos_pass"
  62. # content="$content${nl}--$delim${nl}Content-Disposition: form-data; type=\"keygen\"; user=\"$_panos_user\"; password=\"$_panos_pass\"${nl}Content-Type: application/octet-stream${nl}${nl}"
  63. fi
  64. # Deploy Cert or Key
  65. if [ "$type" = 'cert' ] || [ "$type" = 'key' ]; then
  66. _debug "**** Deploying $type ****"
  67. #Generate DELIM
  68. delim="-----MultipartDelimiter$(date "+%s%N")"
  69. nl="\015\012"
  70. #Set Header
  71. export _H1="Content-Type: multipart/form-data; boundary=$delim"
  72. if [ "$type" = 'cert' ]; then
  73. panos_url="${panos_url}?type=import"
  74. content="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\ncertificate"
  75. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
  76. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
  77. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
  78. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"file\"; filename=\"$(basename "$_cfullchain")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cfullchain")"
  79. fi
  80. if [ "$type" = 'key' ]; then
  81. panos_url="${panos_url}?type=import"
  82. content="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\nprivate-key"
  83. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
  84. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
  85. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
  86. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"passphrase\"\r\n\r\n123456"
  87. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"file\"; filename=\"$(basename "$_cdomain.key")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")"
  88. fi
  89. #Close multipart
  90. content="$content${nl}--$delim--${nl}${nl}"
  91. #Convert CRLF
  92. content=$(printf %b "$content")
  93. fi
  94. # Commit changes
  95. if [ "$type" = 'commit' ]; then
  96. _debug "**** Committing changes ****"
  97. export _H1="Content-Type: application/x-www-form-urlencoded"
  98. #Check for force commit
  99. if [ "$FORCE" ]; then
  100. cmd=$(printf "%s" "<commit><partial><force></force><$_panos_user></$_panos_user></partial></commit>" | _url_encode)
  101. else
  102. cmd=$(printf "%s" "<commit><partial><$_panos_user></$_panos_user></partial></commit>" | _url_encode)
  103. fi
  104. content="type=commit&key=$_panos_key&cmd=$cmd"
  105. fi
  106. response=$(_post "$content" "$panos_url" "" "POST")
  107. parse_response "$response" "$type"
  108. # Saving response to variables
  109. response_status=$status
  110. #DEBUG
  111. _debug response_status "$response_status"
  112. if [ "$response_status" = "success" ]; then
  113. _debug "Successfully deployed $type"
  114. return 0
  115. else
  116. _err "Deploy of type $type failed. Try deploying with --debug to troubleshoot."
  117. _debug "$message"
  118. return 1
  119. fi
  120. }
  121. # This is the main function that will call the other functions to deploy everything.
  122. panos_deploy() {
  123. _cdomain=$(echo "$1" | sed 's/*/WILDCARD_/g') #Wildcard Safe Filename
  124. _ckey="$2"
  125. _cfullchain="$5"
  126. # VALID ECC KEY CHECK
  127. keysuffix=$(printf '%s' "$_ckey" | tail -c 8)
  128. if [ "$keysuffix" = "_ecc.key" ] && [ ! -f "$_ckey" ]; then
  129. _debug "The ECC key $_ckey doesn't exist. Attempting to strip '_ecc' from the key name"
  130. _ckey=$(echo "$_ckey" | sed 's/\(.*\)_ecc.key$/\1.key/g')
  131. if [ ! -f "$_ckey" ]; then
  132. _err "Unable to find a valid key. Try issuing the certificate using RSA (non-ECC) encryption."
  133. return 1
  134. fi
  135. fi
  136. # PANOS_HOST
  137. if [ "$PANOS_HOST" ]; then
  138. _debug "Detected ENV variable PANOS_HOST. Saving to file."
  139. _savedeployconf PANOS_HOST "$PANOS_HOST" 1
  140. else
  141. _debug "Attempting to load variable PANOS_HOST from file."
  142. _getdeployconf PANOS_HOST
  143. fi
  144. # PANOS USER
  145. if [ "$PANOS_USER" ]; then
  146. _debug "Detected ENV variable PANOS_USER. Saving to file."
  147. _savedeployconf PANOS_USER "$PANOS_USER" 1
  148. else
  149. _debug "Attempting to load variable PANOS_USER from file."
  150. _getdeployconf PANOS_USER
  151. fi
  152. # PANOS_PASS
  153. if [ "$PANOS_PASS" ]; then
  154. _debug "Detected ENV variable PANOS_PASS. Saving to file."
  155. _savedeployconf PANOS_PASS "$PANOS_PASS" 1
  156. else
  157. _debug "Attempting to load variable PANOS_PASS from file."
  158. _getdeployconf PANOS_PASS
  159. fi
  160. # PANOS_KEY
  161. if [ "$PANOS_KEY" ]; then
  162. _debug "Detected ENV variable PANOS_KEY. Saving to file."
  163. _savedeployconf PANOS_KEY "$PANOS_KEY" 1
  164. else
  165. _debug "Attempting to load variable PANOS_KEY from file."
  166. _getdeployconf PANOS_KEY
  167. fi
  168. #Store variables
  169. _panos_host=$PANOS_HOST
  170. _panos_key=$PANOS_KEY
  171. _panos_user=$PANOS_USER
  172. _panos_pass=$PANOS_PASS
  173. #Test API Key if found. If the key is invalid, the variable panos_key will be unset.
  174. if [ "$_panos_host" ] && [ "$_panos_key" ]; then
  175. _debug "**** Testing API KEY ****"
  176. deployer keytest
  177. fi
  178. # Check for valid variables
  179. if [ -z "$_panos_host" ]; then
  180. _err "No host found. If this is your first time deploying, please set PANOS_HOST in ENV variables. You can delete it after you have successfully deployed the certs."
  181. return 1
  182. elif [ -z "$_panos_user" ]; then
  183. _err "No user found. If this is your first time deploying, please set PANOS_USER in ENV variables. You can delete it after you have successfully deployed the certs."
  184. return 1
  185. elif [ -z "$_panos_key" ] && { [ -z "$_panos_user" ] || [ -z "$_panos_pass" ]; }; then
  186. _err "No pass OR valid API key found. If this is your first time deploying please set PANOS_PASS and/or PANOS_KEY in ENV variables. You can delete them after you have succesfully deployed the certs."
  187. return 1
  188. else
  189. # Generate a new API key if no valid API key is found
  190. if [ -z "$_panos_key" ]; then
  191. _debug "**** Generating new PANOS API KEY ****"
  192. deployer keygen
  193. _savedeployconf PANOS_KEY "$_panos_key" 1
  194. fi
  195. # Confirm that a valid key was generated
  196. if [ -z "$_panos_key" ]; then
  197. _err "Unable to generate an API key. The user and pass may be invalid or not authorized to generate a new key. Please check the PANOS_USER and PANOS_PASS credentials and try again"
  198. return 1
  199. else
  200. deployer cert
  201. deployer key
  202. deployer commit
  203. fi
  204. fi
  205. }