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.

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