diff options
author | Zuul <zuul@review.opendev.org> | 2022-08-25 07:38:36 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2022-08-25 07:38:36 +0000 |
commit | 662e530d8d4e8e74ba087733f1f51cb98d5145cc (patch) | |
tree | b281d66873c596213b6a80fb683eb7ef0b1bafa2 /swiftclient | |
parent | 02509acc71e6f609b6a9f3c8055479872bedaf2a (diff) | |
parent | defbb4a8f390c7de73ac6a90fc1ab5009e8105ee (diff) | |
download | python-swiftclient-662e530d8d4e8e74ba087733f1f51cb98d5145cc.tar.gz |
Merge "Allow tempurl times to have units"4.1.0
Diffstat (limited to 'swiftclient')
-rwxr-xr-x | swiftclient/shell.py | 18 | ||||
-rw-r--r-- | swiftclient/utils.py | 89 |
2 files changed, 65 insertions, 42 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py index 4bcb251..882a1c0 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -1388,14 +1388,16 @@ Positional arguments: <method> An HTTP method to allow for this temporary URL. Usually 'GET' or 'PUT'. <time> The amount of time the temporary URL will be - valid. The time can be specified in two ways: - an integer representing the time in seconds or an - ISO 8601 timestamp in a specific format. - If --absolute is passed and time - is an integer, the seconds are intepreted as the Unix - timestamp when the temporary URL will expire. The ISO - 8601 timestamp can be specified in one of following - formats: + valid. The time can be specified in three ways: + an integer representing the time in seconds; + a number with a 's', 'm', 'h', or 'd' suffix to specify + the time in seconds, minutes, hours, or days; or + an ISO 8601 timestamp in a specific format. + If --absolute is passed and time is an integer, the + seconds are intepreted as the Unix timestamp when the + temporary URL will expire. + The ISO 8601 timestamp can be specified in one of + following formats: i) Complete date: YYYY-MM-DD (eg 1997-07-16) diff --git a/swiftclient/utils.py b/swiftclient/utils.py index c865d27..1b80e6d 100644 --- a/swiftclient/utils.py +++ b/swiftclient/utils.py @@ -70,40 +70,7 @@ def prt_bytes(num_bytes, human_flag): return '%.1f%s' % (num, suffix) -def generate_temp_url(path, seconds, key, method, absolute=False, - prefix=False, iso8601=False, ip_range=None, - digest='sha256'): - """Generates a temporary URL that gives unauthenticated access to the - Swift object. - - :param path: The full path to the Swift object or prefix if - a prefix-based temporary URL should be generated. Example: - /v1/AUTH_account/c/o or /v1/AUTH_account/c/prefix. - :param seconds: time in seconds or ISO 8601 timestamp. - If absolute is False and this is the string representation of an - integer, then this specifies the amount of time in seconds for which - the temporary URL will be valid. - If absolute is True then this specifies an absolute time at which the - temporary URL will expire. - :param key: The secret temporary URL key set on the Swift - cluster. To set a key, run 'swift post -m - "Temp-URL-Key: <substitute tempurl key here>"' - :param method: A HTTP method, typically either GET or PUT, to allow - for this temporary URL. - :param absolute: if True then the seconds parameter is interpreted as a - Unix timestamp, if seconds represents an integer. - :param prefix: if True then a prefix-based temporary URL will be generated. - :param iso8601: if True, a URL containing an ISO 8601 UTC timestamp - instead of a UNIX timestamp will be created. - :param ip_range: if a valid ip range, restricts the temporary URL to the - range of ips. - :param digest: digest algorithm to use. Must be one of ``sha1``, - ``sha256``, or ``sha512``. - :raises ValueError: if timestamp or path is not in valid format, - or if digest is not one of ``sha1``, ``sha256``, or - ``sha512``. - :return: the path portion of a temporary URL - """ +def parse_timestamp(seconds, absolute=False): try: try: timestamp = float(seconds) @@ -127,6 +94,20 @@ def generate_temp_url(path, seconds, key, method, absolute=False, absolute = True break + if t is None and not absolute: + for suffix, multiplier in ( + ('s', 1), + ('m', 60), + ('min', 60), + ('h', 60 * 60), + ('hr', 60 * 60), + ('d', 24 * 60 * 60), + ): + if seconds.endswith(suffix): + timestamp = t = int( + multiplier * float(seconds[:-len(suffix)])) + break + if t is None: raise ValueError() else: @@ -137,6 +118,46 @@ def generate_temp_url(path, seconds, key, method, absolute=False, raise ValueError() except ValueError: raise ValueError(TIME_ERRMSG) + return timestamp, absolute + + +def generate_temp_url(path, seconds, key, method, absolute=False, + prefix=False, iso8601=False, ip_range=None, + digest='sha256'): + """Generates a temporary URL that gives unauthenticated access to the + Swift object. + + :param path: The full path to the Swift object or prefix if + a prefix-based temporary URL should be generated. Example: + /v1/AUTH_account/c/o or /v1/AUTH_account/c/prefix. + :param seconds: time in seconds or ISO 8601 timestamp. + If absolute is False and this is the string representation of an + integer, then this specifies the amount of time in seconds for which + the temporary URL will be valid. This may include a suffix to scale + the value: 's' for seconds, 'm' (or 'min') for minutes, + 'h' (or 'hr') for hours, or 'd' for days. + If absolute is True then this specifies an absolute time at which the + temporary URL will expire. + :param key: The secret temporary URL key set on the Swift + cluster. To set a key, run 'swift post -m + "Temp-URL-Key: <substitute tempurl key here>"' + :param method: A HTTP method, typically either GET or PUT, to allow + for this temporary URL. + :param absolute: if True then the seconds parameter is interpreted as a + Unix timestamp, if seconds represents an integer. + :param prefix: if True then a prefix-based temporary URL will be generated. + :param iso8601: if True, a URL containing an ISO 8601 UTC timestamp + instead of a UNIX timestamp will be created. + :param ip_range: if a valid ip range, restricts the temporary URL to the + range of ips. + :param digest: digest algorithm to use. Must be one of ``sha1``, + ``sha256``, or ``sha512``. + :raises ValueError: if timestamp or path is not in valid format, + or if digest is not one of ``sha1``, ``sha256``, or + ``sha512``. + :return: the path portion of a temporary URL + """ + timestamp, absolute = parse_timestamp(seconds, absolute) if isinstance(path, bytes): try: |