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.

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