本文整理汇总了Python中sentry.interfaces.base.get_interface函数的典型用法代码示例。如果您正苦于以下问题:Python get_interface函数的具体用法?Python get_interface怎么用?Python get_interface使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_interface函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: post
def post(self, request, project, helper, **kwargs):
json_body = helper.safely_load_json_string(request.body)
report_type = self.security_report_type(json_body)
if report_type is None:
raise APIError('Unrecognized security report type')
interface = get_interface(report_type)
try:
instance = interface.from_raw(json_body)
except jsonschema.ValidationError as e:
raise APIError('Invalid security report: %s' % str(e).splitlines()[0])
# Do origin check based on the `document-uri` key as explained in `_dispatch`.
origin = instance.get_origin()
if not is_valid_origin(origin, project):
if project:
tsdb.incr(tsdb.models.project_total_received_cors, project.id)
raise APIForbidden('Invalid origin')
data = {
'interface': interface.path,
'report': instance,
'release': request.GET.get('sentry_release'),
'environment': request.GET.get('sentry_environment'),
}
response_or_event_id = self.process(
request, project=project, helper=helper, data=data, **kwargs
)
if isinstance(response_or_event_id, HttpResponse):
return response_or_event_id
return HttpResponse(content_type='application/javascript', status=201)
示例2: should_filter
def should_filter(self, project, data, ip_address=None):
for name in self.report_interfaces:
if name in data:
interface = get_interface(name)
if interface.to_python(data[name]).should_filter(project):
return (True, FilterStatKeys.INVALID_CSP)
return super(SecurityApiHelper, self).should_filter(project, data, ip_address)
示例3: process_csp_report
def process_csp_report(self):
"""Only called from the CSP report endpoint."""
data = self._data
try:
interface = get_interface(data.pop('interface'))
report = data.pop('report')
except KeyError:
raise APIForbidden('No report or interface data')
# To support testing, we can either accept a built interface instance, or the raw data in
# which case we build the instance ourselves
try:
instance = (
report if isinstance(report, interface) else interface.from_raw(report)
)
except jsonschema.ValidationError as e:
raise APIError('Invalid security report: %s' % str(e).splitlines()[0])
def clean(d):
return dict(filter(lambda x: x[1], d.items()))
data.update(
{
'logger': 'csp',
'message': instance.get_message(),
'culprit': instance.get_culprit(),
instance.path: instance.to_json(),
'tags': instance.get_tags(),
'errors': [],
'user': {'ip_address': self._client_ip},
# Construct a faux Http interface based on the little information we have
# This is a bit weird, since we don't have nearly enough
# information to create an Http interface, but
# this automatically will pick up tags for the User-Agent
# which is actually important here for CSP
'request': {
'url': instance.get_origin(),
'headers': clean(
{
'User-Agent': self._user_agent,
'Referer': instance.get_referrer(),
}
),
},
}
)
self._data = data
示例4: interfaces
def interfaces(self):
result = []
for key, data in self.data.iteritems():
try:
cls = get_interface(key)
except ValueError:
continue
value = safe_execute(cls.to_python, data)
if not value:
continue
result.append((key, value))
return SortedDict((k, v) for k, v in sorted(result, key=lambda x: x[1].get_score(), reverse=True))
示例5: post
def post(self, request, project, helper, key, **kwargs):
json_body = safely_load_json_string(request.body)
report_type = self.security_report_type(json_body)
if report_type is None:
track_outcome(
project.organization_id,
project.id,
key.id,
Outcome.INVALID,
"security_report_type")
raise APIError('Unrecognized security report type')
interface = get_interface(report_type)
try:
instance = interface.from_raw(json_body)
except jsonschema.ValidationError as e:
track_outcome(
project.organization_id,
project.id,
key.id,
Outcome.INVALID,
"security_report")
raise APIError('Invalid security report: %s' % str(e).splitlines()[0])
# Do origin check based on the `document-uri` key as explained in `_dispatch`.
origin = instance.get_origin()
if not is_valid_origin(origin, project):
if project:
track_outcome(
project.organization_id,
project.id,
key.id,
Outcome.INVALID,
FilterStatKeys.CORS)
raise APIForbidden('Invalid origin')
data = {
'interface': interface.path,
'report': instance,
'release': request.GET.get('sentry_release'),
'environment': request.GET.get('sentry_environment'),
}
self.process(request, project=project, helper=helper, data=data, key=key, **kwargs)
return HttpResponse(content_type='application/javascript', status=201)
示例6: should_filter
def should_filter(self):
'''
returns (result: bool, reason: string or None)
Result is True if an event should be filtered
The reason for filtering is passed along as a string
so that we can store it in metrics
'''
for name in SECURITY_REPORT_INTERFACES:
if name in self._data:
interface = get_interface(name)
if interface.to_python(self._data[name]).should_filter(self._project):
return (True, FilterStatKeys.INVALID_CSP)
if self._client_ip and not is_valid_ip(self._project, self._client_ip):
return (True, FilterStatKeys.IP_ADDRESS)
release = self._data.get('release')
if release and not is_valid_release(self._project, release):
return (True, FilterStatKeys.RELEASE_VERSION)
error_message = get_path(self._data, 'logentry', 'formatted') \
or get_path(self._data, 'logentry', 'message') \
or ''
if error_message and not is_valid_error_message(self._project, error_message):
return (True, FilterStatKeys.ERROR_MESSAGE)
for exc in get_path(self._data, 'exception', 'values', filter=True, default=[]):
message = u': '.join(
filter(None, map(exc.get, ['type', 'value']))
)
if message and not is_valid_error_message(self._project, message):
return (True, FilterStatKeys.ERROR_MESSAGE)
for filter_cls in filters.all():
filter_obj = filter_cls(self._project)
if filter_obj.is_enabled() and filter_obj.test(self._data):
return (True, six.text_type(filter_obj.id))
return (False, None)
示例7: normalize
#.........这里部分代码省略.........
if get_path(data, ['sentry.interfaces.Http', 'env', 'REMOTE_ADDR']) == '{{auto}}':
data['sentry.interfaces.Http']['env']['REMOTE_ADDR'] = client_ip
if get_path(data, ['request', 'env', 'REMOTE_ADDR']) == '{{auto}}':
data['request']['env']['REMOTE_ADDR'] = client_ip
if get_path(data, ['sentry.interfaces.User', 'ip_address']) == '{{auto}}':
data['sentry.interfaces.User']['ip_address'] = client_ip
if get_path(data, ['user', 'ip_address']) == '{{auto}}':
data['user']['ip_address'] = client_ip
# Validate main event body and tags against schema.
# XXX(ja): jsonschema does not like CanonicalKeyDict, so we need to pass
# in the inner data dict.
is_valid, event_errors = validate_and_default_interface(data.data, 'event')
errors.extend(event_errors)
if 'tags' in data:
is_valid, tag_errors = validate_and_default_interface(data['tags'], 'tags', name='tags')
errors.extend(tag_errors)
# Validate interfaces
for k in list(iter(data)):
if k in CLIENT_RESERVED_ATTRS:
continue
value = data.pop(k)
if not value:
self.logger.debug('Ignored empty interface value: %s', k)
continue
try:
interface = get_interface(k)
except ValueError:
self.logger.debug('Ignored unknown attribute: %s', k)
errors.append({'type': EventError.INVALID_ATTRIBUTE, 'name': k})
continue
try:
inst = interface.to_python(value)
data[inst.get_path()] = inst.to_json()
except Exception as e:
log = self.logger.debug if isinstance(
e, InterfaceValidationError) else self.logger.error
log('Discarded invalid value for interface: %s (%r)', k, value, exc_info=True)
errors.append({'type': EventError.INVALID_DATA, 'name': k, 'value': value})
# Additional data coercion and defaulting
level = data.get('level') or DEFAULT_LOG_LEVEL
if isinstance(level, int) or (isinstance(level, six.string_types) and level.isdigit()):
level = LOG_LEVELS.get(int(level), DEFAULT_LOG_LEVEL)
data['level'] = LOG_LEVELS_MAP.get(level, LOG_LEVELS_MAP[DEFAULT_LOG_LEVEL])
if data.get('dist') and not data.get('release'):
data['dist'] = None
timestamp = data.get('timestamp')
if not timestamp:
timestamp = timezone.now()
# TODO (alex) can this all be replaced by utcnow?
# it looks like the only time that this would even be hit is when timestamp
# is not defined, as the earlier process_timestamp already converts existing
# timestamps to floats.
if isinstance(timestamp, datetime):
示例8: normalize
def normalize(self):
# TODO(dcramer): store http.env.REMOTE_ADDR as user.ip
# First we pull out our top-level (non-data attr) kwargs
data = self.data
if not isinstance(data.get('level'), (six.string_types, int)):
data['level'] = logging.ERROR
elif data['level'] not in LOG_LEVELS:
data['level'] = logging.ERROR
if not data.get('logger'):
data['logger'] = DEFAULT_LOGGER_NAME
else:
logger = trim(data['logger'].strip(), 64)
if TagKey.is_valid_key(logger):
data['logger'] = logger
else:
data['logger'] = DEFAULT_LOGGER_NAME
if data.get('platform'):
data['platform'] = trim(data['platform'], 64)
timestamp = data.get('timestamp')
if not timestamp:
timestamp = timezone.now()
if isinstance(timestamp, datetime):
# We must convert date to local time so Django doesn't mess it up
# based on TIME_ZONE
if settings.TIME_ZONE:
if not timezone.is_aware(timestamp):
timestamp = timestamp.replace(tzinfo=timezone.utc)
elif timezone.is_aware(timestamp):
timestamp = timestamp.replace(tzinfo=None)
timestamp = float(timestamp.strftime('%s'))
data['timestamp'] = timestamp
if not data.get('event_id'):
data['event_id'] = uuid4().hex
data.setdefault('message', '')
data.setdefault('culprit', None)
data.setdefault('time_spent', None)
data.setdefault('server_name', None)
data.setdefault('site', None)
data.setdefault('checksum', None)
data.setdefault('fingerprint', None)
data.setdefault('platform', None)
data.setdefault('environment', None)
data.setdefault('extra', {})
data.setdefault('errors', [])
tags = data.get('tags')
if not tags:
tags = []
# full support for dict syntax
elif isinstance(tags, dict):
tags = tags.items()
# prevent [tag, tag, tag] (invalid) syntax
elif not all(len(t) == 2 for t in tags):
tags = []
else:
tags = list(tags)
data['tags'] = []
for key, value in tags:
key = six.text_type(key).strip()
value = six.text_type(value).strip()
if not (key and value):
continue
data['tags'].append((key, value))
if not isinstance(data['extra'], dict):
# throw it away
data['extra'] = {}
trim_dict(
data['extra'], max_size=settings.SENTRY_MAX_EXTRA_VARIABLE_SIZE)
# TODO(dcramer): more of validate data needs stuffed into the manager
for key in data.keys():
if key in CLIENT_RESERVED_ATTRS:
continue
value = data.pop(key)
try:
interface = get_interface(key)()
except ValueError:
continue
try:
inst = interface.to_python(value)
data[inst.get_path()] = inst.to_json()
except Exception:
pass
data['version'] = self.version
#.........这里部分代码省略.........
示例9: validate_data
def validate_data(project, data, client=None):
# TODO(dcramer): move project out of the data packet
data["project"] = project.id
if not data.get("message"):
data["message"] = "<no message value>"
elif not isinstance(data["message"], six.string_types):
raise APIError("Invalid value for message")
if data.get("culprit"):
if not isinstance(data["culprit"], six.string_types):
raise APIError("Invalid value for culprit")
if not data.get("event_id"):
data["event_id"] = uuid.uuid4().hex
elif not isinstance(data["event_id"], six.string_types):
raise APIError("Invalid value for event_id")
if len(data["event_id"]) > 32:
logger.info(
"Discarded value for event_id due to length (%d chars)",
len(data["event_id"]),
**client_metadata(client, project)
)
data["event_id"] = uuid.uuid4().hex
if "timestamp" in data:
try:
process_data_timestamp(data)
except InvalidTimestamp as e:
# Log the error, remove the timestamp, and continue
logger.info(
"Discarded invalid value for timestamp: %r",
data["timestamp"],
**client_metadata(client, project, exception=e)
)
del data["timestamp"]
if data.get("modules") and type(data["modules"]) != dict:
logger.info("Discarded invalid type for modules: %s", type(data["modules"]), **client_metadata(client, project))
del data["modules"]
if data.get("extra") is not None and type(data["extra"]) != dict:
logger.info("Discarded invalid type for extra: %s", type(data["extra"]), **client_metadata(client, project))
del data["extra"]
if data.get("tags") is not None:
if type(data["tags"]) == dict:
data["tags"] = data["tags"].items()
elif not isinstance(data["tags"], (list, tuple)):
logger.info("Discarded invalid type for tags: %s", type(data["tags"]), **client_metadata(client, project))
del data["tags"]
if data.get("tags"):
# remove any values which are over 32 characters
tags = []
for pair in data["tags"]:
try:
k, v = pair
except ValueError:
logger.info("Discarded invalid tag value: %r", pair, **client_metadata(client, project))
continue
if not isinstance(k, six.string_types):
try:
k = six.text_type(k)
except Exception:
logger.info("Discarded invalid tag key: %r", type(k), **client_metadata(client, project))
continue
if not isinstance(v, six.string_types):
try:
v = six.text_type(v)
except Exception:
logger.info("Discarded invalid tag value: %s=%r", k, type(v), **client_metadata(client, project))
continue
if len(k) > MAX_TAG_KEY_LENGTH or len(v) > MAX_TAG_VALUE_LENGTH:
logger.info("Discarded invalid tag: %s=%s", k, v, **client_metadata(client, project))
continue
tags.append((k, v))
data["tags"] = tags
for k in data.keys():
if k in RESERVED_FIELDS:
continue
value = data.pop(k)
if not value:
logger.info("Ignored empty interface value: %s", k, **client_metadata(client, project))
continue
try:
interface = get_interface(k)
except ValueError:
logger.info("Ignored unknown attribute: %s", k, **client_metadata(client, project))
continue
if type(value) != dict:
# HACK(dcramer): the exception interface supports a list as the
# value. We should change this in a new protocol version.
#.........这里部分代码省略.........
示例10: validate_data
#.........这里部分代码省略.........
if not TagKey.is_valid_key(k):
self.log.debug('Discarded invalid tag key: %s', k)
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': 'tags',
'value': pair,
})
continue
if not TagValue.is_valid_value(v):
self.log.debug('Discard invalid tag value: %s', v)
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': 'tags',
'value': pair,
})
continue
tags.append((k, v))
data['tags'] = tags
for k in list(iter(data)):
if k in CLIENT_RESERVED_ATTRS:
continue
value = data.pop(k)
if not value:
self.log.debug('Ignored empty interface value: %s', k)
continue
try:
interface = get_interface(k)
except ValueError:
self.log.debug('Ignored unknown attribute: %s', k)
data['errors'].append({
'type': EventError.INVALID_ATTRIBUTE,
'name': k,
})
continue
if type(value) != dict:
# HACK(dcramer): the exception/breadcrumbs interface supports a
# list as the value. We should change this in a new protocol
# version.
if type(value) in (list, tuple):
value = {'values': value}
else:
self.log.debug(
'Invalid parameter for value: %s (%r)', k, type(value))
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': k,
'value': value,
})
continue
try:
inst = interface.to_python(value)
data[inst.get_path()] = inst.to_json()
except Exception as e:
if isinstance(e, InterfaceValidationError):
log = self.log.debug
else:
log = self.log.error
示例11: validate_data
#.........这里部分代码省略.........
if TagKey.is_reserved_key(k):
self.log.info('Discarding reserved tag key: %s', k)
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': 'tags',
'value': pair,
})
continue
if not TagKey.is_valid_key(k):
self.log.info('Discarded invalid tag key: %s', k)
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': 'tags',
'value': pair,
})
continue
tags.append((k, v))
data['tags'] = tags
for k in data.keys():
if k in CLIENT_RESERVED_ATTRS:
continue
value = data.pop(k)
if not value:
self.log.info('Ignored empty interface value: %s', k)
continue
try:
interface = get_interface(k)
except ValueError:
self.log.info('Ignored unknown attribute: %s', k)
data['errors'].append({
'type': EventError.INVALID_ATTRIBUTE,
'name': k,
})
continue
if type(value) != dict:
# HACK(dcramer): the exception/breadcrumbs interface supports a
# list as the value. We should change this in a new protocol
# version.
if type(value) in (list, tuple):
value = {'values': value}
else:
self.log.info(
'Invalid parameter for value: %s (%r)', k, type(value))
data['errors'].append({
'type': EventError.INVALID_DATA,
'name': k,
'value': value,
})
continue
try:
inst = interface.to_python(value)
data[inst.get_path()] = inst.to_json()
except Exception as e:
if isinstance(e, InterfaceValidationError):
log = self.log.info
else:
log = self.log.error
示例12: validate_data
#.........这里部分代码省略.........
del data["tags"]
if data.get("tags"):
# remove any values which are over 32 characters
tags = []
for pair in data["tags"]:
try:
k, v = pair
except ValueError:
self.log.info("Discarded invalid tag value: %r", pair)
data["errors"].append({"type": EventError.INVALID_DATA, "name": "tags", "value": pair})
continue
if not isinstance(k, six.string_types):
try:
k = six.text_type(k)
except Exception:
self.log.info("Discarded invalid tag key: %r", type(k))
data["errors"].append({"type": EventError.INVALID_DATA, "name": "tags", "value": pair})
continue
if not isinstance(v, six.string_types):
try:
v = six.text_type(v)
except Exception:
self.log.info("Discarded invalid tag value: %s=%r", k, type(v))
data["errors"].append({"type": EventError.INVALID_DATA, "name": "tags", "value": pair})
continue
if len(k) > MAX_TAG_KEY_LENGTH or len(v) > MAX_TAG_VALUE_LENGTH:
self.log.info("Discarded invalid tag: %s=%s", k, v)
data["errors"].append({"type": EventError.INVALID_DATA, "name": "tags", "value": pair})
continue
# support tags with spaces by converting them
k = k.replace(" ", "-")
if not TagKey.is_valid_key(k):
self.log.info("Discarded invalid tag key: %s", k)
data["errors"].append({"type": EventError.INVALID_DATA, "name": "tags", "value": pair})
continue
tags.append((k, v))
data["tags"] = tags
for k in data.keys():
if k in CLIENT_RESERVED_ATTRS:
continue
value = data.pop(k)
if not value:
self.log.info("Ignored empty interface value: %s", k)
continue
try:
interface = get_interface(k)
except ValueError:
self.log.info("Ignored unknown attribute: %s", k)
data["errors"].append({"type": EventError.INVALID_ATTRIBUTE, "name": k})
continue
if type(value) != dict:
# HACK(dcramer): the exception interface supports a list as the
# value. We should change this in a new protocol version.
if type(value) in (list, tuple):
value = {"values": value}
else:
self.log.info("Invalid parameter for value: %s (%r)", k, type(value))
data["errors"].append({"type": EventError.INVALID_DATA, "name": k, "value": value})
continue
try:
inst = interface.to_python(value)
data[inst.get_path()] = inst.to_json()
except Exception as e:
if isinstance(e, InterfaceValidationError):
log = self.log.info
else:
log = self.log.error
log("Discarded invalid value for interface: %s (%r)", k, value, exc_info=True)
data["errors"].append({"type": EventError.INVALID_DATA, "name": k, "value": value})
level = data.get("level") or DEFAULT_LOG_LEVEL
if isinstance(level, six.string_types) and not level.isdigit():
# assume it's something like 'warning'
try:
data["level"] = LOG_LEVEL_REVERSE_MAP[level]
except KeyError as e:
self.log.info("Discarded invalid logger value: %s", level)
data["errors"].append({"type": EventError.INVALID_DATA, "name": "level", "value": level})
data["level"] = LOG_LEVEL_REVERSE_MAP.get(DEFAULT_LOG_LEVEL, DEFAULT_LOG_LEVEL)
if data.get("release"):
data["release"] = unicode(data["release"])
if len(data["release"]) > 64:
data["errors"].append({"type": EventError.VALUE_TOO_LONG, "name": "release", "value": data["release"]})
del data["release"]
return data
示例13: test_allows_http
def test_allows_http(self):
from sentry.interfaces.http import Http
result = get_interface('sentry.interfaces.Http')
assert result is Http
result = get_interface('request')
assert result is Http
示例14: test_does_not_let_through_disallowed_name
def test_does_not_let_through_disallowed_name(self):
with self.assertRaises(ValueError):
get_interface('subprocess')
示例15: test_get_interface_does_not_let_through_disallowed_name
def test_get_interface_does_not_let_through_disallowed_name():
with pytest.raises(ValueError):
get_interface('subprocess')