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.0 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
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. # You MUST include the following environment variable when first running
  10. # the sccript (can be deleted afterwards):
  11. #
  12. # REQURED:
  13. # export PANOS_HOST="" # required
  14. #
  15. # AND one of the two authenticiation methods:
  16. #
  17. # Method 1: Username & Password (RECOMMENDED)
  18. # export PANOS_USER=""
  19. # export PANOS_PASS=""
  20. #
  21. # Method 2: API KEY
  22. # export PANOS_KEY=""
  23. #
  24. #
  25. # The Username & Password method will automatically generate a new API key if
  26. # no key is found, or if a saved key has expired or is invalid.
  27. # This function is to parse the XML
  28. parse_response() {
  29. type=$2
  30. if [ "$type" = 'keygen' ]; then
  31. status=$(echo "$1" | sed 's/^.*\(['\'']\)\([a-z]*\)'\''.*/\2/g')
  32. if [ "$status" = "success" ]; then
  33. panos_key=$(echo "$1" | sed 's/^.*\(<key>\)\(.*\)<\/key>.*/\2/g')
  34. _panos_key=$panos_key
  35. else
  36. message="PAN-OS Key could not be set."
  37. fi
  38. else
  39. status=$(echo "$1" | sed 's/^.*"\([a-z]*\)".*/\1/g')
  40. message=$(echo "$1" | sed 's/^.*<result>\(.*\)<\/result.*/\1/g')
  41. if [ "$type" = 'keytest' ] && [ "$status" != "success" ]; then
  42. _debug "**** API Key has EXPIRED or is INVALID ****"
  43. unset _panos_key
  44. fi
  45. fi
  46. return 0
  47. }
  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. content="type=commit&cmd=<commit></commit>&key=$_panos_key"
  57. fi
  58. # Generate API Key
  59. if [ "$type" = 'keygen' ]; then
  60. _debug "**** Generating new API Key ****"
  61. _H1="Content-Type: application/x-www-form-urlencoded"
  62. content="type=keygen&user=$_panos_user&password=$_panos_pass"
  63. # 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}"
  64. fi
  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. if [ "$type" = 'commit' ]; then
  95. _debug "**** Committing changes ****"
  96. export _H1="Content-Type: application/x-www-form-urlencoded"
  97. if [ "$_panos_user" ]; then
  98. _commit_desc=$_panos_user
  99. else
  100. _commit_desc="acmesh"
  101. fi
  102. cmd=$(printf "%s" "<commit><partial><$_commit_desc></$_commit_desc></partial></commit>" | _url_encode)
  103. content="type=commit&key=$_panos_key&cmd=$cmd"
  104. fi
  105. response=$(_post "$content" "$panos_url" "" "POST")
  106. parse_response "$response" "$type"
  107. # Saving response to variables
  108. response_status=$status
  109. #DEBUG
  110. _debug response_status "$response_status"
  111. if [ "$response_status" = "success" ]; then
  112. _debug "Successfully deployed $type"
  113. return 0
  114. else
  115. _err "Deploy of type $type failed. Try deploying with --debug to troubleshoot."
  116. _debug "$message"
  117. return 1
  118. fi
  119. }
  120. # This is the main function that will call the other functions to deploy everything.
  121. panos_deploy() {
  122. _cdomain=$(echo "$1" | sed 's/*/WILDCARD_/g') #Wildcard Safe Filename
  123. _ckey="$2"
  124. _cfullchain="$5"
  125. # VALID ECC KEY CHECK
  126. keysuffix=$(printf '%s' "$_ckey" | tail -c 8)
  127. if [ "$keysuffix" = "_ecc.key" ] && [ ! -f "$_ckey" ]; then
  128. _debug "The ECC key $_ckey doesn't exist. Attempting to strip '_ecc' from the key name"
  129. _ckey=$(echo "$_ckey" | sed 's/\(.*\)_ecc.key$/\1.key/g')
  130. if [ ! -f "$_ckey" ]; then
  131. _err "Unable to find a valid key. Try issuing the certificate using RSA (non-ECC) encryption."
  132. return 1
  133. fi
  134. fi
  135. # Environment Checks
  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_KEY
  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. Please enter a valid host as environment variable PANOS_HOST."
  181. return 1
  182. elif [ -z "$_panos_key" ] && { [ -z "$_panos_user" ] || [ -z "$_panos_pass" ]; }; then
  183. _err "No user and pass OR valid API key found.. If this is the first time deploying please set PANOS_USER and PANOS_PASS -- AND/OR -- PANOS_KEY in environment variables. Delete them after you have succesfully deployed certs."
  184. return 1
  185. else
  186. # Generate a new API key if no valid API key is found
  187. if [ -z "$_panos_key" ]; then
  188. _debug "**** Generating new PANOS API KEY ****"
  189. deployer keygen
  190. _savedeployconf PANOS_KEY "$_panos_key" 1
  191. fi
  192. # Confirm that a valid key was generated
  193. if [ -z "$_panos_key" ]; then
  194. _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 credentials and try again"
  195. return 1
  196. else
  197. deployer cert
  198. deployer key
  199. deployer commit
  200. fi
  201. fi
  202. }