Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ To turn reporting off, any one of these is enough:
Every switch fails closed: `enabled` keeps reporting on only when it is `true` (or the string
`"true"`, `"on"`, `"yes"` or `"1"`), so `"false"`, `"off"`, `null` or a typo turn it off, and so does
`"usage_reporting": false` in place of the block. Collide never rewrites a `usage_reporting` entry
that is already there, so an opt-out stays put.
that is already there, so an opt-out stays put. Other spellings count as off too: a block written as `"usage-reporting"`, `"usageReporting"` or `"UsageReporting"` (or any spelling of "usage reporting") holding `false` or `"enabled": false`, and inside the block `"enable": false`, `"Enabled": false` or `"disabled": true`; the real `usage_reporting` block is then written with `"enabled": false` next to it rather than with reporting on.

The environment variables win over `settings.json`. The `endpoint` and `key` entries in the same
block select where reports go and the key they are sent with. The client is `src/trace_client.py`,
Expand Down
51 changes: 48 additions & 3 deletions src/usage_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,44 @@ def installIdFile():
return os.path.join(base, APPLICATION.lower(), "trace-install-id")


def _normalise(key):
"""A settings key in lower case with everything but letters and digits dropped, so
``usage_reporting``, ``usage-reporting``, ``usageReporting`` and ``UsageReporting`` all
read ``usagereporting``."""
return "".join(c for c in str(key).lower() if c.isascii() and c.isalnum())


def hasNearMissOptOut(settings):
"""Whether ``settings`` holds an attempt to turn usage reporting off that is not written
exactly as ``"usage_reporting": {"enabled": ...}``. Such a setting used to be ignored: the
default block, ``"enabled": true``, was written next to it and reporting stayed on. An
operator who wrote any of these did not ask for reporting, so each counts as off:

* a top-level key that spells "usage reporting" another way (``usage-reporting``,
``usageReporting``, ``UsageReporting``, ...) holding a value instead of a block that is
not clearly on (the client's ``allows_reporting``);
* inside any such block, a key that spells "enabled" another way (``enable``,
``Enabled``, ...) whose value is not clearly on, or one that spells "disabled"
(``disabled``, ``disable``, ...) whose value is on.
"""
for key, value in settings.items():
if "usagereport" not in _normalise(key):
continue
if not isinstance(value, dict):
if not allows_reporting(value):
return True
continue
for child, childValue in value.items():
if key == SETTINGS_SECTION and child == "enabled":
continue # the switch itself, read by buildClient
name = _normalise(child)
if "disabl" in name and allows_reporting(childValue):
return True
if "enabl" in name and not allows_reporting(childValue):
return True
return False


def loadSettings(settingsFile=SETTINGS_FILE, log=print):
"""Read the usage_reporting block from the settings file, writing the default block
(and printing the one-time notice) when the file does not have one yet.
Expand All @@ -96,16 +134,23 @@ def loadSettings(settingsFile=SETTINGS_FILE, log=print):
return None

section = settings.get(SETTINGS_SECTION)
# An opt-out written under another spelling (see hasNearMissOptOut) wins over the block.
nearMissOptOut = hasNearMissOptOut(settings)
if isinstance(section, dict):
return section
return dict(section, enabled=False) if nearMissOptOut else section
if SETTINGS_SECTION in settings:
# "usage_reporting": false (or "off", null, ...) is a setting, not a missing block:
# read it as ``enabled`` and leave the file alone rather than replace it with the
# default block, which would turn reporting back on.
return {"enabled": section}

settings[SETTINGS_SECTION] = defaultSettings()
log(firstRunNotice())
if nearMissOptOut:
# The block is written with the real switch off, next to the near-miss, so the file
# says off in the spelling this program reads; no "reporting is on" notice is shown.
settings[SETTINGS_SECTION] = dict(defaultSettings(), enabled=False)
else:
settings[SETTINGS_SECTION] = defaultSettings()
log(firstRunNotice())
try:
with open(settingsFile, "w") as f:
json.dump(settings, f, indent=2)
Expand Down
54 changes: 54 additions & 0 deletions tests/test_usage_reporting.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,60 @@ def test_a_usage_reporting_value_that_is_not_a_block_is_an_opt_out_and_not_rewri
self.assertEqual([], self.logged)
self.assertEqual({"usage_reporting": value}, self.readSettingsFile())

def test_an_opt_out_written_under_a_near_miss_name_is_an_opt_out(self):
# Each of these used to be ignored: the default block was written next to it with
# "enabled": true and reporting stayed on.
nearMisses = (
{"usage-reporting": {"enabled": False}},
{"usageReporting": {"enabled": "off"}},
{"UsageReporting": {"enabled": "false"}},
{"usage-reporting": False},
{"usageReporting": "no"},
{"usage_reporting": {"enable": False}},
{"usage_reporting": {"Enabled": False}},
{"usage_reporting": {"disabled": True}},
{"usage_reporting": {"enabled": True, "disable": "yes"}},
)
for nearMiss in nearMisses:
with self.subTest(settings=nearMiss):
with open(self.settingsFile, "w") as f:
json.dump(nearMiss, f)
self.logged.clear()

client = buildClient(loadSettings(self.settingsFile, self.log))

self.assertFalse(client.enabled)
self.assertEqual("config", client.disabled_reason)
self.assertEqual([], self.logged)

def test_near_miss_names_that_say_on_are_not_an_opt_out(self):
onCases = (
{"usage-reporting": {"enabled": True}},
{"usageReporting": "on"},
{"usage_reporting": {"enabled": True, "enable": "yes"}},
{"usage_reporting": {"enabled": True, "disabled": False}},
{"usage_reporting": {"enabled": True}, "other": {"enabled": False}},
)
for onCase in onCases:
with self.subTest(settings=onCase):
with open(self.settingsFile, "w") as f:
json.dump(onCase, f)

self.assertTrue(buildClient(loadSettings(self.settingsFile, self.log)).enabled)

def test_a_near_miss_opt_out_writes_the_real_switch_off(self):
with open(self.settingsFile, "w") as f:
json.dump({"usage-reporting": {"enabled": False}}, f)

loadSettings(self.settingsFile, self.log)

written = self.readSettingsFile()
self.assertEqual({"enabled": False}, written["usage-reporting"])
self.assertIs(False, written["usage_reporting"]["enabled"])
self.assertEqual([], self.logged)
# and the next launch, reading the real switch, stays off
self.assertFalse(buildClient(loadSettings(self.settingsFile, self.log)).enabled)

def test_unreadable_settings_file_disables_reporting_without_raising(self):
with open(self.settingsFile, "w") as f:
f.write("{not json")
Expand Down