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.

147 lines
5.9 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. # export PANOS_USER="" # required
  10. # export PANOS_PASS="" # required
  11. # export PANOS_HOST="" # required
  12. # This function is to parse the XML
  13. parse_response() {
  14. type=$2
  15. if [ "$type" = 'keygen' ]; then
  16. status=$(echo "$1" | sed 's/^.*\(['\'']\)\([a-z]*\)'\''.*/\2/g')
  17. if [ "$status" = "success" ]; then
  18. panos_key=$(echo "$1" | sed 's/^.*\(<key>\)\(.*\)<\/key>.*/\2/g')
  19. _panos_key=$panos_key
  20. message='PAN-OS key is set.'
  21. else
  22. message="PAN-OS Key could not be set."
  23. fi
  24. else
  25. status=$(echo "$1" | sed 's/^.*"\([a-z]*\)".*/\1/g')
  26. message=$(echo "$1" | sed 's/^.*<result>\(.*\)<\/result.*/\1/g')
  27. fi
  28. return 0
  29. }
  30. deployer() {
  31. type=$1 # Types are keygen, cert, key, commit
  32. _debug "**** Deploying $type *****"
  33. panos_url="https://$_panos_host/api/"
  34. if [ "$type" = 'keygen' ]; then
  35. _H1="Content-Type: application/x-www-form-urlencoded"
  36. content="type=keygen&user=$_panos_user&password=$_panos_pass"
  37. # 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}"
  38. fi
  39. if [ "$type" = 'cert' ] || [ "$type" = 'key' ]; then
  40. #Generate DEIM
  41. delim="-----MultipartDelimiter$(date "+%s%N")"
  42. nl="\015\012"
  43. #Set Header
  44. export _H1="Content-Type: multipart/form-data; boundary=$delim"
  45. if [ "$type" = 'cert' ]; then
  46. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"type\"\r\n\r\n\r\nimport"
  47. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\n\r\ncertificate"
  48. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n\r\n$_cdomain"
  49. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n\r\n$_panos_key"
  50. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\n\r\npem"
  51. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"file\"; filename=\"$(basename "$_cfullchain")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_cfullchain")"
  52. fi
  53. if [ "$type" = 'key' ]; then
  54. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"type\"\r\n\r\n\r\nimport"
  55. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\n\r\nprivate-key"
  56. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n\r\n$_cdomain"
  57. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n\r\n$_panos_key"
  58. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\n\r\npem"
  59. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"passphrase\"\r\n\r\n\r\nnone"
  60. content="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"file\"; filename=\"$(basename "$_ckey")\"${nl}Content-Type: application/octet-stream${nl}${nl}$(cat "$_ckey")"
  61. fi
  62. #Close multipart
  63. content="$content${nl}--$delim--${nl}"
  64. #Convert CRLF
  65. content=$(printf %b "$content")
  66. fi
  67. if [ "$type" = 'commit' ]; then
  68. export _H1="Content-Type: application/x-www-form-urlencoded"
  69. cmd=$(printf "%s" "<commit><partial><$_panos_user></$_panos_user></partial></commit>" | _url_encode)
  70. content="type=commit&key=$_panos_key&cmd=$cmd"
  71. fi
  72. response=$(_post "$content" "$panos_url" "" "POST")
  73. parse_response "$response" "$type"
  74. # Saving response to variables
  75. response_status=$status
  76. #DEBUG
  77. _debug response_status "$response_status"
  78. if [ "$response_status" = "success" ]; then
  79. _debug "Successfully deployed $type"
  80. return 0
  81. else
  82. _err "Deploy of type $type failed. Try deploying with --debug to troubleshoot."
  83. _debug "$message"
  84. return 1
  85. fi
  86. }
  87. # This is the main function that will call the other functions to deploy everything.
  88. panos_deploy() {
  89. _cdomain="$1"
  90. _ckey="$2"
  91. _cfullchain="$5"
  92. # PANOS HOST is required to make API calls to the PANOS/Panorama
  93. if [ -z "$PANOS_HOST" ]; then
  94. if [ -z "$_panos_host" ]; then
  95. _err "PANOS_HOST not defined."
  96. return 1
  97. fi
  98. else
  99. _debug "PANOS HOST is set. Save to domain conf."
  100. _panos_host="$PANOS_HOST"
  101. _savedomainconf _panos_host "$_panos_host"
  102. fi
  103. # Retrieve stored variables
  104. _panos_user="$(_readaccountconf_mutable PANOS_USER)"
  105. _panos_pass="$(_readaccountconf_mutable PANOS_PASS)"
  106. # PANOS Credentials check
  107. if [ -z "$PANOS_USER" ] || [ -z "$PANOS_PASS" ]; then
  108. _debug "PANOS_USER, PANOS_PASS is not defined"
  109. if [ -z "$_panos_user" ] && [ -z "$_panos_pass" ]; then
  110. _err "No user and pass found in storage. If this is the first time deploying please set PANOS_USER and PANOS_PASS in environment variables."
  111. return 1
  112. else
  113. _debug "ok"
  114. fi
  115. else
  116. _debug "Saving environment variables"
  117. # Encrypt and save user
  118. _saveaccountconf_mutable PANOS_USER "$PANOS_USER"
  119. _saveaccountconf_mutable PANOS_PASS "$PANOS_PASS"
  120. _panos_user="$PANOS_USER"
  121. _panos_pass="$PANOS_PASS"
  122. fi
  123. _debug "Let's use username and pass to generate token."
  124. if [ -z "$_panos_user" ] || [ -z "$_panos_pass" ] || [ -z "$_panos_host" ]; then
  125. _err "Please pass username and password and host as env variables PANOS_USER, PANOS_PASS and PANOS_HOST"
  126. return 1
  127. else
  128. _debug "Getting PANOS KEY"
  129. deployer keygen
  130. if [ -z "$_panos_key" ]; then
  131. _err "Missing host, apikey, user."
  132. return 1
  133. else
  134. deployer cert
  135. deployer key
  136. deployer commit
  137. fi
  138. fi
  139. }