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.

215 lines
8.6 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. # VALID FILE CHECK
  124. if [ ! -f "$_ckey" ] || [ ! -f "$_cfullchain" ]; then
  125. _err "Unable to find a valid key and/or cert. If this is an ECDSA/ECC cert, use the --ecc flag when deploying."
  126. return 1
  127. fi
  128. # PANOS_HOST
  129. if [ "$PANOS_HOST" ]; then
  130. _debug "Detected ENV variable PANOS_HOST. Saving to file."
  131. _savedeployconf PANOS_HOST "$PANOS_HOST" 1
  132. else
  133. _debug "Attempting to load variable PANOS_HOST from file."
  134. _getdeployconf PANOS_HOST
  135. fi
  136. # PANOS USER
  137. if [ "$PANOS_USER" ]; then
  138. _debug "Detected ENV variable PANOS_USER. Saving to file."
  139. _savedeployconf PANOS_USER "$PANOS_USER" 1
  140. else
  141. _debug "Attempting to load variable PANOS_USER from file."
  142. _getdeployconf PANOS_USER
  143. fi
  144. # PANOS_PASS
  145. if [ "$PANOS_PASS" ]; then
  146. _debug "Detected ENV variable PANOS_PASS. Saving to file."
  147. _savedeployconf PANOS_PASS "$PANOS_PASS" 1
  148. else
  149. _debug "Attempting to load variable PANOS_PASS from file."
  150. _getdeployconf PANOS_PASS
  151. fi
  152. # PANOS_KEY
  153. _getdeployconf PANOS_KEY
  154. if [ "$PANOS_KEY" ]; then
  155. _debug "Detected saved key."
  156. _panos_key=$PANOS_KEY
  157. else
  158. _debug "No key detected"
  159. unset _panos_key
  160. fi
  161. #Store variables
  162. _panos_host=$PANOS_HOST
  163. _panos_user=$PANOS_USER
  164. _panos_pass=$PANOS_PASS
  165. #Test API Key if found. If the key is invalid, the variable _panos_key will be unset.
  166. if [ "$_panos_host" ] && [ "$_panos_key" ]; then
  167. _debug "**** Testing API KEY ****"
  168. deployer keytest
  169. fi
  170. # Check for valid variables
  171. if [ -z "$_panos_host" ]; then
  172. _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."
  173. return 1
  174. elif [ -z "$_panos_user" ]; then
  175. _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."
  176. return 1
  177. elif [ -z "$_panos_pass" ]; then
  178. _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."
  179. return 1
  180. else
  181. # Generate a new API key if no valid API key is found
  182. if [ -z "$_panos_key" ]; then
  183. _debug "**** Generating new PANOS API KEY ****"
  184. deployer keygen
  185. _savedeployconf PANOS_KEY "$_panos_key" 1
  186. fi
  187. # Confirm that a valid key was generated
  188. if [ -z "$_panos_key" ]; then
  189. _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"
  190. return 1
  191. else
  192. deployer cert
  193. deployer key
  194. deployer commit
  195. fi
  196. fi
  197. }