fix(test): retain safe inbound resource fault diagnostics
This commit is contained in:
@@ -18,11 +18,37 @@ import sys
|
||||
import time
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
import urllib.error
|
||||
from pathlib import Path
|
||||
|
||||
TARGET = 'firstleg-test-lastleg'
|
||||
UPSTREAM = 'firstleg-test-warehouse-overseas'
|
||||
PROPERTY = 'baiyue.warehouse.base-url'
|
||||
STAGE = 'INITIALIZE'
|
||||
|
||||
|
||||
def safe_diagnostic(error):
|
||||
"""Never return exception messages, URLs, bodies, environment or identities."""
|
||||
stage, status = STAGE, None
|
||||
if isinstance(error, urllib.error.HTTPError):
|
||||
endpoint = urllib.parse.urlsplit(error.geturl()).path
|
||||
stage = {'/nacos/v3/auth/user/login': 'NACOS_LOGIN',
|
||||
'/nacos/v3/admin/cs/config': 'NACOS_CONFIG_READ'}.get(endpoint, stage)
|
||||
status = error.code if type(error.code) is int and 100 <= error.code <= 599 else None
|
||||
kind = type(error).__name__
|
||||
diagnostic = {'stage': stage, 'errorType': kind if re.fullmatch(r'[A-Za-z_]{1,80}', kind) else 'Exception'}
|
||||
if isinstance(error, urllib.error.URLError) and not isinstance(error, urllib.error.HTTPError):
|
||||
reason = error.reason
|
||||
reason_type = type(reason).__name__
|
||||
if re.fullmatch(r'[A-Za-z_]{1,80}', reason_type): diagnostic['transportErrorType'] = reason_type
|
||||
number = getattr(reason, 'errno', None)
|
||||
if type(number) is int and -10000 <= number <= 10000: diagnostic['errno'] = number
|
||||
if type(status) is int and 100 <= status <= 599: diagnostic['httpStatus'] = status
|
||||
message = str(error)
|
||||
code = message if re.fullmatch(r'[A-Z_0-9]{1,90}', message) else 'INBOUND_RESOURCE_ACTION_FAILED'
|
||||
if status is not None: code = stage + '_HTTP_' + str(status)
|
||||
elif isinstance(error, urllib.error.URLError): code = stage + '_TRANSPORT_FAILED'
|
||||
return {'ok': False, 'error': code, 'diagnostic': diagnostic}
|
||||
|
||||
|
||||
def validate_scope(scope):
|
||||
@@ -183,15 +209,25 @@ def runtime_class(base_source):
|
||||
|
||||
|
||||
def execute(config):
|
||||
global STAGE
|
||||
STAGE = 'LOAD_REVIEWED_PRIMITIVES'
|
||||
Runtime, namespace = runtime_class(base64.b64decode(config['baseSource']).decode())
|
||||
runtime = Runtime(config['scope'], namespace['common_tools'](base64.b64decode(config['commonSource']).decode()))
|
||||
action = config['action']
|
||||
if action == 'preflight':
|
||||
STAGE = 'LASTLEG_DOCKER_READ'
|
||||
current = runtime.container()
|
||||
STAGE = 'WAREHOUSE_UPSTREAM_IDENTITY'
|
||||
target = runtime.verified_upstream(current, config['routing'], runtime.tools['env_of'](current).get('INTERNAL_SERVICE_TOKEN', ''))
|
||||
return {'originalImageId': current['Image'], 'originalFingerprint': runtime.tools['fingerprint'](current),
|
||||
'sourceSha256': runtime.config_fingerprint(current, config['routing']), 'runtimeChanged': False,
|
||||
'upstreamImageId': target['Image'], 'upstreamFingerprint': runtime.tools['fingerprint'](target)}
|
||||
STAGE = 'LASTLEG_FULL_FINGERPRINT'
|
||||
original_fingerprint = runtime.tools['fingerprint'](current)
|
||||
STAGE = 'NACOS_SOURCE_READ'
|
||||
source_hash = runtime.config_fingerprint(current, config['routing'])
|
||||
STAGE = 'WAREHOUSE_FULL_FINGERPRINT'
|
||||
upstream_fingerprint = runtime.tools['fingerprint'](target)
|
||||
return {'originalImageId': current['Image'], 'originalFingerprint': original_fingerprint,
|
||||
'sourceSha256': source_hash, 'runtimeChanged': False,
|
||||
'upstreamImageId': target['Image'], 'upstreamFingerprint': upstream_fingerprint}
|
||||
if action == 'prepare': return runtime.prepare(config)
|
||||
state = runtime.load()
|
||||
if action == 'status':
|
||||
@@ -234,5 +270,4 @@ def main(config):
|
||||
if __name__ == '__main__':
|
||||
try: print('INBOUND_RESOURCE_JSON=' + json.dumps({'ok': True, 'data': main(json.loads(base64.b64decode(sys.argv[1])))}))
|
||||
except Exception as error:
|
||||
message = str(error)
|
||||
print('INBOUND_RESOURCE_JSON=' + json.dumps({'ok': False, 'error': message if re.fullmatch(r'[A-Z_0-9]{1,90}', message) else 'INBOUND_RESOURCE_ACTION_FAILED'}))
|
||||
print('INBOUND_RESOURCE_JSON=' + json.dumps(safe_diagnostic(error)))
|
||||
|
||||
Reference in New Issue
Block a user