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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

- An opt-out written under another property name now counts as off. Spring binds only `usage.reporting.enabled` (and its relaxed forms, such as `USAGE_REPORTING_ENABLED`), so `usage-reporting.enabled=false`, `usage_reporting.enable=false`, `usage.reporting.disabled=true`, `usage.reporting=false` or `USAGE_REPORTING=off` used to be ignored and reporting stayed on. Any property naming usage reporting whose "enabled"-like key is not clearly on, whose "disabled"-like key is on, or which is given a value that is not clearly on, now turns reporting off.
- Usage reporting now stays off once it is turned off. `usage.reporting.enabled` / `USAGE_REPORTING_ENABLED` used to turn reporting off only for `false`: `off`, `no`, `0`, a blank value or a typo all left it on. It now fails closed and is read through the trace client's `allowsReporting` helper: only `true`, `on`, `yes` or `1` (any case) keeps reporting on, and anything else turns it off. `docker-compose.yml` now passes a blank `USAGE_REPORTING_ENABLED` through as blank (off) instead of replacing it with `true`. The vendored `TraceClient` is now trace-client-java 0.8.0, whose `TRACE_USAGE_REPORTING` and `DO_NOT_TRACK` checks fail closed too: `TRACE_USAGE_REPORTING` set to anything but blank or `true`/`on`/`yes`/`1`, `DO_NOT_TRACK` set to anything but blank or `0`/`false`/`off`/`no`, or an environment that cannot be read, turns reporting off

### Added
Expand Down
2 changes: 1 addition & 1 deletion CONFIG.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ java -Dlogging.level.root=INFO -jar target/gh-backup-2.0.0-SNAPSHOT-8-8-2026.jar

**Type:** boolean
**Default:** `true`
**Description:** Whether gh-backup reports that it was used to the maintainers' trace service. When on, two events are sent, both from a background thread that never blocks or delays a backup: `startup` once per process and `backup-completed` when a backup run finishes. Both carry the same two tags and nothing else: `version`, the program version, and `install`, a random installation ID that lets installations be counted rather than events. The installation ID is the value of the `TRACE_INSTALL_ID` environment variable when that is set and not blank; otherwise it is a random UUID written the first time reporting runs to `~/.config/gh-backup/trace-install-id` and reused after that (delete the file to get a new one). Nothing about the users, organizations or repositories being backed up is sent, and no usernames, hostnames, IP addresses or paths. The first time reporting runs on a machine, one `INFO` line saying so is logged, and a marker file (`~/.config/gh-backup/usage-reporting-notice-shown`) keeps it from repeating; delete that file to see the notice again. gh-backup has no settings file of its own, which is why the marker and the installation ID file live there. If the JVM has no usable home directory (`user.home` unset or blank), there is nowhere to keep the marker or the installation ID, so the notice is logged on every run and events carry no `install` tag unless `TRACE_INSTALL_ID` is set. Setting the property to `false` — or to anything but `true`, `on`, `yes` or `1` (any case), blank or a typo included, since it fails closed — sends nothing, logs no notice, writes no marker, and neither reads nor writes the installation ID file. As with every other property, it can also be set in an `application.properties` next to the JAR, and it is read from a `USAGE_REPORTING_ENABLED` environment variable, which is how the Docker image is configured. Independently of this property, the environment variables every trace client honours also turn reporting off and are checked first: `TRACE_USAGE_REPORTING=off` (or any value but blank, `true`, `on`, `yes` or `1`) and `DO_NOT_TRACK=1` (or any value but blank, `0`, `false`, `off` or `no`; see https://consoledonottrack.com). Details on what trace collects and why: https://danielstephenson.dev/usage-reporting
**Description:** Whether gh-backup reports that it was used to the maintainers' trace service. When on, two events are sent, both from a background thread that never blocks or delays a backup: `startup` once per process and `backup-completed` when a backup run finishes. Both carry the same two tags and nothing else: `version`, the program version, and `install`, a random installation ID that lets installations be counted rather than events. The installation ID is the value of the `TRACE_INSTALL_ID` environment variable when that is set and not blank; otherwise it is a random UUID written the first time reporting runs to `~/.config/gh-backup/trace-install-id` and reused after that (delete the file to get a new one). Nothing about the users, organizations or repositories being backed up is sent, and no usernames, hostnames, IP addresses or paths. The first time reporting runs on a machine, one `INFO` line saying so is logged, and a marker file (`~/.config/gh-backup/usage-reporting-notice-shown`) keeps it from repeating; delete that file to see the notice again. gh-backup has no settings file of its own, which is why the marker and the installation ID file live there. If the JVM has no usable home directory (`user.home` unset or blank), there is nowhere to keep the marker or the installation ID, so the notice is logged on every run and events carry no `install` tag unless `TRACE_INSTALL_ID` is set. Setting the property to `false` — or to anything but `true`, `on`, `yes` or `1` (any case), blank or a typo included, since it fails closed — sends nothing, logs no notice, writes no marker, and neither reads nor writes the installation ID file. As with every other property, it can also be set in an `application.properties` next to the JAR, and it is read from a `USAGE_REPORTING_ENABLED` environment variable, which is how the Docker image is configured. Independently of this property, the environment variables every trace client honours also turn reporting off and are checked first: `TRACE_USAGE_REPORTING=off` (or any value but blank, `true`, `on`, `yes` or `1`) and `DO_NOT_TRACK=1` (or any value but blank, `0`, `false`, `off` or `no`; see https://consoledonottrack.com). Other spellings count as off too: `usage-reporting.enabled=false`, `usage_reporting.enable=false`, `usage.reporting.disabled=true`, `usage.reporting=false` or `USAGE_REPORTING=off` turn reporting off even though they are not this property. Details on what trace collects and why: https://danielstephenson.dev/usage-reporting

**Override at runtime:**
```bash
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/com/github/backup/NearMissOptOut.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.github.backup;

import com.github.backup.trace.TraceClient;
import org.springframework.boot.context.properties.source.ConfigurationProperty;
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.context.properties.source.IterableConfigurationPropertySource;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;

import java.util.Locale;

/**
* Finds an attempt to turn usage reporting off that is not written as the real switch,
* {@code usage.reporting.enabled}. Spring binds only that name (and its relaxed forms, such as
* {@code USAGE_REPORTING_ENABLED}), so anything else used to be ignored and reporting stayed on. An operator
* who wrote any of these did not ask for reporting, so each counts as off:
* <ul>
* <li>a property that spells "enabled" another way under any spelling of "usage reporting"
* ({@code usage_reporting.enable}, {@code usageReporting.Enabled}, ...) whose value is not
* clearly on ({@link TraceClient#allowsReporting(Object)});</li>
* <li>one that spells "disabled" ({@code usage-reporting.disabled}, {@code USAGE_REPORTING_DISABLE},
* ...) whose value is on;</li>
* <li>a value given to "usage reporting" itself ({@code usage-reporting=false},
* {@code USAGE_REPORTING=off}, ...) that is not clearly on.</li>
* </ul>
* {@code TRACE_USAGE_REPORTING} is left to the client, which reads it itself.
*/
final class NearMissOptOut {

private NearMissOptOut() {
}

/** The near-miss property that turns reporting off, or null when there is none. */
static String find(Environment environment, String realSwitch) {
if (!(environment instanceof ConfigurableEnvironment)) {
return null;
}
ConfigurationPropertyName switchName = ConfigurationPropertyName.of(realSwitch);
for (ConfigurationPropertySource source
: ConfigurationPropertySources.from(((ConfigurableEnvironment) environment).getPropertySources())) {
if (!(source instanceof IterableConfigurationPropertySource)) {
continue;
}
for (ConfigurationPropertyName name : (IterableConfigurationPropertySource) source) {
if (name.equals(switchName)) {
continue; // the switch itself, which Spring resolves
}
String key = normalise(name.toString());
if (!key.contains("usagereport") || key.equals("traceusagereporting")) {
continue;
}
ConfigurationProperty property = source.getConfigurationProperty(name);
Object value = property == null ? null : property.getValue();
if (isOptOut(key, value)) {
return name.toString();
}
}
}
return null;
}

static boolean isOptOut(String normalisedKey, Object value) {
if (normalisedKey.contains("disabl")) {
return TraceClient.allowsReporting(value);
}
if (normalisedKey.contains("enabl") || normalisedKey.endsWith("usagereporting")) {
return !TraceClient.allowsReporting(value);
}
return false;
}

/** The key in lower case with everything but letters and digits dropped. */
static String normalise(String key) {
return key.toLowerCase(Locale.ROOT).replaceAll("[^a-z0-9]", "");
}
}
22 changes: 22 additions & 0 deletions src/main/java/com/github/backup/UsageReportingService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

import java.io.File;
Expand Down Expand Up @@ -61,19 +62,40 @@ public class UsageReportingService {
static final String DETAILS_URL = "https://danielstephenson.dev/usage-reporting";
/** Sent as the version when the build did not supply one. */
static final String UNKNOWN_VERSION = "unknown";
/** The program's own switch, as Spring binds it. */
static final String SWITCH = "usage.reporting.enabled";

private final TraceClient client;
private final Path noticeMarker;

@Autowired
public UsageReportingService(
Environment environment,
@Value("${usage.reporting.enabled:true}") String enabled,
@Value("${usage.reporting.endpoint:https://trace.danielstephenson.dev}") String endpoint,
@Value("${usage.reporting.key:}") String key,
@Value("${spring.application.version:}") String version) {
this(switchOrNearMissOptOut(enabled, environment), endpoint, key, version, defaultNoticeMarker());
}

/** As above, reading only the switch itself (no near-miss check). */
public UsageReportingService(String enabled, String endpoint, String key, String version) {
this(enabled, endpoint, key, version, defaultNoticeMarker());
}

/**
* The switch as Spring resolved it, or "false" when an opt-out is written under another name
* (see {@link NearMissOptOut}), which Spring would not bind to the switch at all.
*/
static String switchOrNearMissOptOut(String enabled, Environment environment) {
String nearMiss = NearMissOptOut.find(environment, SWITCH);
if (nearMiss != null) {
log.debug("Usage reporting is off: {} is read as an opt-out.", nearMiss);
return "false";
}
return enabled;
}

UsageReportingService(String enabled, String endpoint, String key, String version, Path noticeMarker) {
this.client = buildClient(enabled, endpoint, key, versionOrUnknown(version), installIdFile(noticeMarker));
this.noticeMarker = noticeMarker;
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/com/github/backup/NearMissOptOutTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.github.backup;

import org.junit.jupiter.api.Test;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.StandardEnvironment;
import org.springframework.core.env.SystemEnvironmentPropertySource;

import java.util.LinkedHashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

/**
* An opt-out written under a name Spring does not bind to {@value UsageReportingService#SWITCH} used to be
* ignored, and reporting stayed on.
*/
class NearMissOptOutTest {

/** An environment holding only the shipped defaults plus the given properties. */
private static StandardEnvironment environment(Map<String, Object> file, Map<String, Object> env) {
StandardEnvironment environment = new StandardEnvironment();
environment.getPropertySources().remove(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME);
environment.getPropertySources().remove(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME);
environment.getPropertySources().addLast(new SystemEnvironmentPropertySource("env", env));
environment.getPropertySources().addLast(new MapPropertySource("file", file));
environment.getPropertySources().addLast(new MapPropertySource("defaults",
Map.<String, Object>of(UsageReportingService.SWITCH, "true", "usage.reporting.endpoint", "http://127.0.0.1:9")));
return environment;
}

private static Map<String, Object> one(String key, Object value) {
Map<String, Object> map = new LinkedHashMap<>();
map.put(key, value);
return map;
}

@Test
void anOptOutWrittenUnderANearMissNameIsAnOptOut() {
Object[][] fileNearMisses = {
{"usage-reporting.enabled", false},
{"usageReporting.enabled", "off"},
{"Usage_Reporting.Enabled", "false"},
{"usage-reporting.enable", false},
{"usage-reporting.disabled", true},
{"usage-reporting.disable", "yes"},
{"usage-reporting", false},
{"usage_reporting", "no"},
};
for (Object[] nearMiss : fileNearMisses) {
StandardEnvironment environment = environment(one((String) nearMiss[0], nearMiss[1]), Map.of());
assertNotNull(NearMissOptOut.find(environment, UsageReportingService.SWITCH), nearMiss[0] + "=" + nearMiss[1]);
assertEquals("false", UsageReportingService.switchOrNearMissOptOut("true", environment), nearMiss[0] + "=" + nearMiss[1]);
}
String[][] envNearMisses = {
{"USAGE_REPORTING_DISABLED", "true"},
{"USAGE_REPORTING_ENABLE", "false"},
{"USAGE_REPORTING", "off"},
};
for (String[] nearMiss : envNearMisses) {
StandardEnvironment environment = environment(Map.of(), one(nearMiss[0], nearMiss[1]));
assertNotNull(NearMissOptOut.find(environment, UsageReportingService.SWITCH), nearMiss[0] + "=" + nearMiss[1]);
}
}

@Test
void nearMissNamesThatSayOnAreNotAnOptOut() {
Object[][] onSettings = {
{"usage-reporting.enable", true},
{"usage-reporting.disabled", false},
{"usageReporting", "on"},
{"usage.reporting.endpoint", "http://127.0.0.1:9"},
{"management.endpoints.enabled", false},
};
for (Object[] on : onSettings) {
StandardEnvironment environment = environment(one((String) on[0], on[1]), Map.of());
assertNull(NearMissOptOut.find(environment, UsageReportingService.SWITCH), on[0] + "=" + on[1]);
assertEquals("true", UsageReportingService.switchOrNearMissOptOut("true", environment), on[0] + "=" + on[1]);
}
// The real switch is left to Spring, and the client reads TRACE_USAGE_REPORTING itself
// (a blank one, as docker-compose passes through, is not an opt-out).
assertNull(NearMissOptOut.find(environment(one(UsageReportingService.SWITCH, "true"),
one("USAGE_REPORTING_ENABLED", "true")), UsageReportingService.SWITCH));
assertNull(NearMissOptOut.find(environment(Map.of(), one("TRACE_USAGE_REPORTING", "")), UsageReportingService.SWITCH));
assertNull(NearMissOptOut.find(environment(Map.of(), Map.of()), UsageReportingService.SWITCH));
}

@Test
void theRealSwitchIsStillReadAsBefore() {
StandardEnvironment environment = environment(Map.of(), Map.of());
assertEquals("false", UsageReportingService.switchOrNearMissOptOut("false", environment));
assertEquals("off", UsageReportingService.switchOrNearMissOptOut("off", environment));
assertEquals("true", UsageReportingService.switchOrNearMissOptOut("true", environment));
}
}