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.

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