Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url obfuscation #773

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions newrelic/api/web_transaction.py
Expand Up @@ -16,6 +16,7 @@
import time
import logging
import warnings
import re

try:
import urlparse
Expand Down Expand Up @@ -139,6 +140,23 @@ class WebTransaction(Transaction):
unicode_error_reported = False
QUEUE_TIME_HEADERS = ('x-request-start', 'x-queue-start')

@property
def _request_uri(self):
return self.request_uri


@_request_uri.setter
def _request_uri(self, raw_uri):
if self._settings and raw_uri:
if self._settings.url_regex and self._settings.url_regex_rep:
obfuscated_uri = re.sub(str(self._settings.url_regex),str(self._settings.url_regex_rep), str(raw_uri))
self.request_uri = obfuscated_uri
elif self._settings.url_regex:
obfuscated_uri = re.sub(str(self._settings.url_regex),"*****", str(raw_uri))
self.request_uri = obfuscated_uri
else:
self.request_uri = raw_uri

def __init__(self, application, name, group=None,
scheme=None, host=None, port=None, request_method=None,
request_path=None, query_string=None, headers=None,
Expand Down
2 changes: 2 additions & 0 deletions newrelic/config.py
Expand Up @@ -314,6 +314,8 @@ def _process_setting(section, option, getter, mapper):
def _process_configuration(section):
_process_setting(section, "feature_flag", "get", _map_feature_flag)
_process_setting(section, "app_name", "get", None)
_process_setting(section, "url_regex", "get", None)
_process_setting(section, "url_regex_rep", "get", None)
_process_setting(section, "labels", "get", _map_labels)
_process_setting(section, "license_key", "get", _map_default_host_value)
_process_setting(section, "api_key", "get", None)
Expand Down
3 changes: 3 additions & 0 deletions newrelic/core/config.py
Expand Up @@ -570,6 +570,9 @@ def default_host(license_key):
_settings.ca_bundle_path = os.environ.get("NEW_RELIC_CA_BUNDLE_PATH", None)

_settings.app_name = os.environ.get("NEW_RELIC_APP_NAME", "Python Application")
_settings.url_regex = os.environ.get('NEW_RELIC_URL_REGEX', None )
_settings.url_regex_rep = os.environ.get('NEW_RELIC_URL_REGEX_REP', None)

_settings.linked_applications = []

_settings.process_host.display_name = os.environ.get("NEW_RELIC_PROCESS_HOST_DISPLAY_NAME", None)
Expand Down
8 changes: 8 additions & 0 deletions newrelic/newrelic.ini
Expand Up @@ -42,6 +42,14 @@ license_key = *** REPLACE ME ***
# https://docs.newrelic.com/docs/apm/agents/manage-apm-agents/app-naming/use-multiple-names-app/
app_name = Python Application

# If sensitive data needs obfuscating in the URL, set this to regex pattern to search in URL
# Disabled by default
url_regex =

# String to replace with if regex pattern returns match
# Default *****
url_regex_rep =

# When "true", the agent collects performance data about your
# application and reports this data to the New Relic UI at
# newrelic.com. This global switch is normally overridden for
Expand Down