3 # Script to deploy certificates to Palo Alto Networks PANOS via API
4 # Note PANOS API KEY and IP address needs to be set prior to running.
5 # The following variables exported from environment will be used.
6 # If not set then values previously saved in domain.conf file are used.
8 # Firewall admin with superuser and IP address is required.
11 # export PANOS_HOST=""
12 # export PANOS_USER="" #User *MUST* have Commit and Import Permissions in XML API for Admin Role
13 # export PANOS_PASS=""
15 # The script will automatically generate a new API key if
16 # no key is found, or if a saved key has expired or is invalid.
18 # This function is to parse the XML response from the firewall
21 if [ "$type" = 'keygen' ]; then
22 status
=$
(echo "$1" |
sed 's/^.*\(['\'']\)\([a-z]*\)'\''.*/\2/g')
23 if [ "$status" = "success" ]; then
24 panos_key
=$
(echo "$1" |
sed 's/^.*\(<key>\)\(.*\)<\/key>.*/\2/g')
27 message
="PAN-OS Key could not be set."
30 status
=$
(echo "$1" |
tr -d '\n' |
sed 's/^.*"\([a-z]*\)".*/\1/g')
31 message
=$
(echo "$1" |
tr -d '\n' |
sed 's/.*\(<result>\|<msg>\|<line>\)\([^<]*\).*/\2/g')
32 _debug
"Firewall message: $message"
33 if [ "$type" = 'keytest' ] && [ "$status" != "success" ]; then
34 _debug
"**** API Key has EXPIRED or is INVALID ****"
41 #This function is used to deploy to the firewall
44 type=$1 # Types are keytest, keygen, cert, key, commit
45 panos_url
="https://$_panos_host/api/"
47 #Test API Key by performing a lookup
48 if [ "$type" = 'keytest' ]; then
49 _debug
"**** Testing saved API Key ****"
50 _H1
="Content-Type: application/x-www-form-urlencoded"
51 # Get Version Info to test key
52 content
="type=version&key=$_panos_key"
53 ## Exclude all scopes for the empty commit
54 #_exclude_scope="<policy-and-objects>exclude</policy-and-objects><device-and-network>exclude</device-and-network><shared-object>exclude</shared-object>"
55 #content="type=commit&action=partial&key=$_panos_key&cmd=<commit><partial>$_exclude_scope<admin><member>acmekeytest</member></admin></partial></commit>"
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}"
67 if [ "$type" = 'cert' ] ||
[ "$type" = 'key' ]; then
68 _debug
"**** Deploying $type ****"
70 delim
="-----MultipartDelimiter$(date "+%s
%N
")"
73 export _H1
="Content-Type: multipart/form-data; boundary=$delim"
74 if [ "$type" = 'cert' ]; then
75 panos_url
="${panos_url}?type=import"
76 content
="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\ncertificate"
77 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
78 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
79 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
80 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 if [ "$type" = 'key' ]; then
83 panos_url
="${panos_url}?type=import"
84 content
="--$delim${nl}Content-Disposition: form-data; name=\"category\"\r\n\r\nprivate-key"
85 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"certificate-name\"\r\n\r\n$_cdomain"
86 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"key\"\r\n\r\n$_panos_key"
87 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"format\"\r\n\r\npem"
88 content
="$content${nl}--$delim${nl}Content-Disposition: form-data; name=\"passphrase\"\r\n\r\n123456"
89 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")"
92 content
="$content${nl}--$delim--${nl}${nl}"
94 content
=$
(printf %b
"$content")
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
)
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
)
109 content
="type=commit&action=partial&key=$_panos_key&cmd=$cmd"
112 response
=$
(_post
"$content" "$panos_url" "" "POST")
113 parse_response
"$response" "$type"
114 # Saving response to variables
115 response_status
=$status
116 _debug response_status
"$response_status"
117 if [ "$response_status" = "success" ]; then
118 _debug
"Successfully deployed $type"
121 _err
"Deploy of type $type failed. Try deploying with --debug to troubleshoot."
127 # This is the main function that will call the other functions to deploy everything.
129 _cdomain
=$
(echo "$1" |
sed 's/*/WILDCARD_/g') #Wildcard Safe Filename
134 if [ ! -f "$_ckey" ] ||
[ ! -f "$_cfullchain" ]; then
135 _err
"Unable to find a valid key and/or cert. If this is an ECDSA/ECC cert, use the --ecc flag when deploying."
140 if [ "$PANOS_HOST" ]; then
141 _debug
"Detected ENV variable PANOS_HOST. Saving to file."
142 _savedeployconf PANOS_HOST
"$PANOS_HOST" 1
144 _debug
"Attempting to load variable PANOS_HOST from file."
145 _getdeployconf PANOS_HOST
149 if [ "$PANOS_USER" ]; then
150 _debug
"Detected ENV variable PANOS_USER. Saving to file."
151 _savedeployconf PANOS_USER
"$PANOS_USER" 1
153 _debug
"Attempting to load variable PANOS_USER from file."
154 _getdeployconf PANOS_USER
158 if [ "$PANOS_PASS" ]; then
159 _debug
"Detected ENV variable PANOS_PASS. Saving to file."
160 _savedeployconf PANOS_PASS
"$PANOS_PASS" 1
162 _debug
"Attempting to load variable PANOS_PASS from file."
163 _getdeployconf PANOS_PASS
167 _getdeployconf PANOS_KEY
168 if [ "$PANOS_KEY" ]; then
169 _debug
"Detected saved key."
170 _panos_key
=$PANOS_KEY
172 _debug
"No key detected"
177 _panos_host
=$PANOS_HOST
178 _panos_user
=$PANOS_USER
179 _panos_pass
=$PANOS_PASS
181 #Test API Key if found. If the key is invalid, the variable _panos_key will be unset.
182 if [ "$_panos_host" ] && [ "$_panos_key" ]; then
183 _debug
"**** Testing API KEY ****"
187 # Check for valid variables
188 if [ -z "$_panos_host" ]; then
189 _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."
191 elif [ -z "$_panos_user" ]; then
192 _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."
194 elif [ -z "$_panos_pass" ]; then
195 _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."
198 # Generate a new API key if no valid API key is found
199 if [ -z "$_panos_key" ]; then
200 _debug
"**** Generating new PANOS API KEY ****"
202 _savedeployconf PANOS_KEY
"$_panos_key" 1
205 # Confirm that a valid key was generated
206 if [ -z "$_panos_key" ]; then
207 _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"