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.

234 lines
9.4 KiB

6 months ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 months ago
5 years ago
5 years ago
5 years ago
5 years ago
1 year 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=""
  11. # export PANOS_USER="" #User *MUST* have Commit and Import Permissions in XML API for Admin Role
  12. # export PANOS_PASS=""
  13. #
  14. # OPTIONAL
  15. # export PANOS_TEMPLATE="" #Template Name of panorama managed devices
  16. #
  17. # The script will automatically generate a new API key if
  18. # no key is found, or if a saved key has expired or is invalid.
  19. # This function is to parse the XML response from the firewall
  20. parse_response() {
  21. type=$2
  22. if [ "$type" = 'keygen' ]; then
  23. status=$(echo "$1" | sed 's/^.*\(['\'']\)\([a-z]*\)'\''.*/\2/g')
  24. if [ "$status" = "success" ]; then
  25. panos_key=$(echo "$1" | sed 's/^.*\(<key>\)\(.*\)<\/key>.*/\2/g')
  26. _panos_key=$panos_key
  27. else
  28. message="PAN-OS Key could not be set."
  29. fi
  30. else
  31. status=$(echo "$1" | tr -d '\n' | sed 's/^.*"\([a-z]*\)".*/\1/g')
  32. message=$(echo "$1" | tr -d '\n' | sed 's/.*\(<result>\|<msg>\|<line>\)\([^<]*\).*/\2/g')
  33. _debug "Firewall message: $message"
  34. if [ "$type" = 'keytest' ] && [ "$status" != "success" ]; then
  35. _debug "**** API Key has EXPIRED or is INVALID ****"
  36. unset _panos_key
  37. fi
  38. fi
  39. return 0
  40. }
  41. #This function is used to deploy to the firewall
  42. deployer() {
  43. content=""
  44. type=$1 # Types are keytest, keygen, cert, key, commit
  45. panos_url="https://$_panos_host/api/"
  46. #Test API Key by performing a lookup
  47. if [ "$type" = 'keytest' ]; then
  48. _debug "**** Testing saved API Key ****"
  49. _H1="Content-Type: application/x-www-form-urlencoded"
  50. # Get Version Info to test key
  51. content="type=version&key=$_panos_key"
  52. ## Exclude all scopes for the empty commit
  53. #_exclude_scope="<policy-and-objects>exclude</policy-and-objects><device-and-network>exclude</device-and-network><shared-object>exclude</shared-object>"
  54. #content="type=commit&action=partial&key=$_panos_key&cmd=<commit><partial>$_exclude_scope<admin><member>acmekeytest</member></admin></partial></commit>"
  55. fi
  56. # Generate API Key
  57. if [ "$type" = 'keygen' ]; then
  58. _debug "**** Generating new API Key ****"
  59. _H1="Content-Type: application/x-www-form-urlencoded"
  60. content="type=keygen&user=$_panos_user&password=$_panos_pass"
  61. # 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}"
  62. fi
  63. # Deploy Cert or Key
  64. if [ "$type" = 'cert' ] || [ "$type" = 'key' ]; then
  65. _debug "**** Deploying $type ****"
  66. #Generate DELIM
  67. delim="-----MultipartDelimiter$(date "+%s%N")"
  68. nl="\015\012"
  69. #Set Header
  70. export _H1="Content-Type: multipart/form-data; boundary=$delim"
  71. if [ "$type" = 'cert' ]; then
  72. panos_url="${panos_url}?type=import"
  73. content="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\ncertificate"
  74. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
  75. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
  76. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
  77. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"file\"; filename=\"$(basename "$_cfullchain")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cfullchain")"
  78. if [ "$_panos_template" ]; then
  79. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"target-tpl\"\r\n\r\n$_panos_template"
  80. fi
  81. fi
  82. if [ "$type" = 'key' ]; then
  83. panos_url="${panos_url}?type=import"
  84. content="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\nprivate-key"
  85. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
  86. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
  87. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
  88. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"passphrase\"\r\n\r\n123456"
  89. 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")"
  90. if [ "$_panos_template" ]; then
  91. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"target-tpl\"\r\n\r\n$_panos_template"
  92. fi
  93. fi
  94. #Close multipart
  95. content="$content${nl}--$delim--${nl}${nl}"
  96. #Convert CRLF
  97. content=$(printf %b "$content")
  98. fi
  99. # Commit changes
  100. if [ "$type" = 'commit' ]; then
  101. _debug "**** Committing changes ****"
  102. export _H1="Content-Type: application/x-www-form-urlencoded"
  103. #Check for force commit - will commit ALL uncommited changes to the firewall. Use with caution!
  104. if [ "$FORCE" ]; then
  105. _debug "Force switch detected. Committing ALL changes to the firewall."
  106. cmd=$(printf "%s" "<commit><partial><force><admin><member>$_panos_user</member></admin></force></partial></commit>" | _url_encode)
  107. else
  108. _exclude_scope="<policy-and-objects>exclude</policy-and-objects><device-and-network>exclude</device-and-network>"
  109. cmd=$(printf "%s" "<commit><partial>$_exclude_scope<admin><member>$_panos_user</member></admin></partial></commit>" | _url_encode)
  110. fi
  111. content="type=commit&action=partial&key=$_panos_key&cmd=$cmd"
  112. fi
  113. response=$(_post "$content" "$panos_url" "" "POST")
  114. parse_response "$response" "$type"
  115. # Saving response to variables
  116. response_status=$status
  117. _debug response_status "$response_status"
  118. if [ "$response_status" = "success" ]; then
  119. _debug "Successfully deployed $type"
  120. return 0
  121. else
  122. _err "Deploy of type $type failed. Try deploying with --debug to troubleshoot."
  123. _debug "$message"
  124. return 1
  125. fi
  126. }
  127. # This is the main function that will call the other functions to deploy everything.
  128. panos_deploy() {
  129. _cdomain=$(echo "$1" | sed 's/*/WILDCARD_/g') #Wildcard Safe Filename
  130. _ckey="$2"
  131. _cfullchain="$5"
  132. # VALID FILE CHECK
  133. if [ ! -f "$_ckey" ] || [ ! -f "$_cfullchain" ]; then
  134. _err "Unable to find a valid key and/or cert. If this is an ECDSA/ECC cert, use the --ecc flag when deploying."
  135. return 1
  136. fi
  137. # PANOS_HOST
  138. if [ "$PANOS_HOST" ]; then
  139. _debug "Detected ENV variable PANOS_HOST. Saving to file."
  140. _savedeployconf PANOS_HOST "$PANOS_HOST" 1
  141. else
  142. _debug "Attempting to load variable PANOS_HOST from file."
  143. _getdeployconf PANOS_HOST
  144. fi
  145. # PANOS USER
  146. if [ "$PANOS_USER" ]; then
  147. _debug "Detected ENV variable PANOS_USER. Saving to file."
  148. _savedeployconf PANOS_USER "$PANOS_USER" 1
  149. else
  150. _debug "Attempting to load variable PANOS_USER from file."
  151. _getdeployconf PANOS_USER
  152. fi
  153. # PANOS_PASS
  154. if [ "$PANOS_PASS" ]; then
  155. _debug "Detected ENV variable PANOS_PASS. Saving to file."
  156. _savedeployconf PANOS_PASS "$PANOS_PASS" 1
  157. else
  158. _debug "Attempting to load variable PANOS_PASS from file."
  159. _getdeployconf PANOS_PASS
  160. fi
  161. # PANOS_KEY
  162. _getdeployconf PANOS_KEY
  163. if [ "$PANOS_KEY" ]; then
  164. _debug "Detected saved key."
  165. _panos_key=$PANOS_KEY
  166. else
  167. _debug "No key detected"
  168. unset _panos_key
  169. fi
  170. # PANOS_TEMPLATE
  171. if [ "$PANOS_TEMPLATE" ]; then
  172. _debug "Detected ENV variable PANOS_TEMPLATE. Saving to file."
  173. _savedeployconf PANOS_TEMPLATE "$PANOS_TEMPLATE" 1
  174. else
  175. _debug "Attempting to load variable PANOS_TEMPLATE from file."
  176. _getdeployconf PANOS_TEMPLATE
  177. fi
  178. #Store variables
  179. _panos_host=$PANOS_HOST
  180. _panos_user=$PANOS_USER
  181. _panos_pass=$PANOS_PASS
  182. _panos_template=$PANOS_TEMPLATE
  183. #Test API Key if found. If the key is invalid, the variable _panos_key will be unset.
  184. if [ "$_panos_host" ] && [ "$_panos_key" ]; then
  185. _debug "**** Testing API KEY ****"
  186. deployer keytest
  187. fi
  188. # Check for valid variables
  189. if [ -z "$_panos_host" ]; then
  190. _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."
  191. return 1
  192. elif [ -z "$_panos_user" ]; then
  193. _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."
  194. return 1
  195. elif [ -z "$_panos_pass" ]; then
  196. _err "No password found. If this is your first time deploying, please set PANOS_PASS in ENV variables. You can delete it after you have successfully deployed the certs."
  197. return 1
  198. else
  199. # Generate a new API key if no valid API key is found
  200. if [ -z "$_panos_key" ]; then
  201. _debug "**** Generating new PANOS API KEY ****"
  202. deployer keygen
  203. _savedeployconf PANOS_KEY "$_panos_key" 1
  204. fi
  205. # Confirm that a valid key was generated
  206. if [ -z "$_panos_key" ]; then
  207. _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"
  208. return 1
  209. else
  210. deployer cert
  211. deployer key
  212. deployer commit
  213. fi
  214. fi
  215. }