Merge "Add cap_length helper"

This commit is contained in:
Zuul 2023-05-17 23:20:30 +00:00 committed by Gerrit Code Review
commit 4c7b2e3bb5
6 changed files with 30 additions and 12 deletions

View File

@ -78,7 +78,7 @@ from swift.common.middleware.catch_errors import enforce_byte_count
from swift.common.swob import Request
from swift.common.utils import (get_logger, get_remote_client,
config_true_value, reiterate,
close_if_possible,
close_if_possible, cap_length,
InputProxy, list_from_csv, get_policy_index,
split_path, StrAnonymizer, StrFormatTime,
LogStringFormatter)
@ -200,9 +200,7 @@ class ProxyLoggingMiddleware(object):
env['swift.proxy_access_log_made'] = True
def obscure_sensitive(self, value):
if value and len(value) > self.reveal_sensitive_prefix:
return value[:self.reveal_sensitive_prefix] + '...'
return value
return cap_length(value, self.reveal_sensitive_prefix)
def obscure_req(self, req):
for header in get_sensitive_headers():

View File

@ -5717,6 +5717,15 @@ def strict_b64decode(value, allow_line_breaks=False):
raise ValueError
def cap_length(value, max_length):
if value and len(value) > max_length:
if isinstance(value, bytes):
return value[:max_length] + b'...'
else:
return value[:max_length] + '...'
return value
MD5_BLOCK_READ_BYTES = 4096

View File

@ -388,7 +388,8 @@ class Receiver(object):
raise SsyncClientDisconnected
if line.strip() != b':MISSING_CHECK: START':
raise Exception(
'Looking for :MISSING_CHECK: START got %r' % line[:1024])
'Looking for :MISSING_CHECK: START got %r'
% utils.cap_length(line, 1024))
object_hashes = []
nlines = 0
while True:
@ -450,7 +451,8 @@ class Receiver(object):
# Guess they hung up waiting for us to process the missing check
raise SsyncClientDisconnected
if line.strip() != b':UPDATES: START':
raise Exception('Looking for :UPDATES: START got %r' % line[:1024])
raise Exception('Looking for :UPDATES: START got %r'
% utils.cap_length(line, 1024))
successes = 0
failures = 0
updates = 0

View File

@ -266,7 +266,7 @@ class Sender(object):
self.daemon.node_timeout, 'connect receive'):
response = connection.getresponse()
if response.status != http.HTTP_OK:
err_msg = response.read()[:1024]
err_msg = utils.cap_length(response.read(), 1024)
raise exceptions.ReplicationException(
'Expected status %s; got %s (%s)' %
(http.HTTP_OK, response.status, err_msg))
@ -358,7 +358,7 @@ class Sender(object):
except UnicodeDecodeError:
pass
raise exceptions.ReplicationException(
'Unexpected response: %r' % line[:1024])
'Unexpected response: %r' % utils.cap_length(line, 1024))
while True:
with exceptions.MessageTimeout(
self.daemon.http_timeout, 'missing_check line wait'):
@ -447,7 +447,7 @@ class Sender(object):
except UnicodeDecodeError:
pass
raise exceptions.ReplicationException(
'Unexpected response: %r' % line[:1024])
'Unexpected response: %r' % utils.cap_length(line, 1024))
while True:
with exceptions.MessageTimeout(
self.daemon.http_timeout, 'updates line wait'):
@ -464,7 +464,7 @@ class Sender(object):
except UnicodeDecodeError:
pass
raise exceptions.ReplicationException(
'Unexpected response: %r' % line[:1024])
'Unexpected response: %r' % utils.cap_length(line, 1024))
def send_subrequest(self, connection, method, url_path, headers, df):
msg = [b'%s %s' % (method.encode('ascii'), url_path.encode('utf8'))]

View File

@ -36,7 +36,7 @@ from swift.common.utils import Watchdog, get_logger, \
get_remote_client, split_path, config_true_value, generate_trans_id, \
affinity_key_function, affinity_locality_predicate, list_from_csv, \
parse_prefixed_conf, config_auto_int_value, node_to_string, \
config_request_node_count_value, config_percent_value
config_request_node_count_value, config_percent_value, cap_length
from swift.common.registry import register_swift_info
from swift.common.constraints import check_utf8, valid_api_version
from swift.proxy.controllers import AccountController, ContainerController, \
@ -736,7 +736,7 @@ class Application(object):
else:
fmt = 'ERROR %(status)d %(body)s Trying to %(method)s ' \
'%(path)s From %(type)s Server'
values['body'] = body[:1024]
values['body'] = cap_length(body, 1024)
self.error_occurred(node, fmt % values)
else:
ok = True

View File

@ -3713,6 +3713,15 @@ cluster_dfw1 = http://dfw1.host/v1/
self.fail('Invalid results from pure function:\n%s' %
'\n'.join(failures))
def test_cap_length(self):
self.assertEqual(utils.cap_length(None, 3), None)
self.assertEqual(utils.cap_length('', 3), '')
self.assertEqual(utils.cap_length('asdf', 3), 'asd...')
self.assertEqual(utils.cap_length('asdf', 5), 'asdf')
self.assertEqual(utils.cap_length(b'asdf', 3), b'asd...')
self.assertEqual(utils.cap_length(b'asdf', 5), b'asdf')
def test_get_partition_for_hash(self):
hex_hash = 'af088baea4806dcaba30bf07d9e64c77'
self.assertEqual(43, utils.get_partition_for_hash(hex_hash, 6))