Skip to content

Commit

Permalink
Make following redirects opt in
Browse files Browse the repository at this point in the history
  • Loading branch information
Chad Nelson committed Jun 22, 2021
1 parent f60961b commit 7011c9a
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions awscurl/awscurl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import datetime
import hashlib
import hmac
import json
import os
import pprint
import sys
Expand Down Expand Up @@ -65,7 +66,8 @@ def make_request(method,
secret_key,
security_token,
data_binary,
verify=True):
verify=True,
redirects=False):
"""
# Make HTTP request with AWS Version 4 signing
Expand All @@ -82,6 +84,7 @@ def make_request(method,
:param security_token: str
:param data_binary: bool
:param verify: bool
:param redirects: false
See also: http://docs.aws.amazon.com/general/latest/gr/sigv4_signing.html
"""
Expand Down Expand Up @@ -132,9 +135,9 @@ def make_request(method,
headers.update(auth_headers)

if data_binary:
return __send_request(uri, data, headers, method, verify)
return __send_request(uri, data, headers, method, verify, redirects)
else:
return __send_request(uri, data.encode('utf-8'), headers, method, verify)
return __send_request(uri, data.encode('utf-8'), headers, method, verify, redirects)


# pylint: disable=too-many-arguments,too-many-locals
Expand Down Expand Up @@ -320,14 +323,14 @@ def __now():
return datetime.datetime.utcnow()


def __send_request(uri, data, headers, method, verify):
def __send_request(uri, data, headers, method, verify, redirects):
__log('\nHEADERS++++++++++++++++++++++++++++++++++++')
__log(headers)

__log('\nBEGIN REQUEST++++++++++++++++++++++++++++++++++++')
__log('Request URL = ' + uri)

response = requests.request(method, uri, headers=headers, data=data, verify=verify)
response = requests.request(method, uri, headers=headers, data=data, verify=verify, allow_redirects=redirects)

__log('\nRESPONSE++++++++++++++++++++++++++++++++++++')
__log('Response code: %d\n' % response.status_code)
Expand Down Expand Up @@ -433,6 +436,8 @@ def inner_main(argv):
# https://github.com/boto/botocore/blob/c76553d3158b083d818f88c898d8f6d7918478fd/botocore/credentials.py#L260-262
parser.add_argument('--security_token', env_var='AWS_SECURITY_TOKEN')
parser.add_argument('--session_token', env_var='AWS_SESSION_TOKEN')
parser.add_argument('-f', '--follow-redirects', action='store_true', default=False,
help="Request should follow redirects in responses")

parser.add_argument('uri')

Expand Down Expand Up @@ -484,10 +489,11 @@ def inner_main(argv):
args.secret_key,
args.session_token,
args.data_binary,
verify=not args.insecure)
verify=not args.insecure,
redirects=args.follow_redirects)

if args.include or IS_VERBOSE:
print(response.headers, end='\n\n')
print(json.dumps(dict(response.headers)), end='\n\n')
print(response.text)

response.raise_for_status()
Expand Down

0 comments on commit 7011c9a

Please sign in to comment.