Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1147,17 +1147,12 @@ private static boolean delete(File file) {

// ---- naming ----

/** The directory the store for one environment belongs in. */
static File defaultDirectory(File noBackupFilesDir, String mobileKey) {
return new File(new File(noBackupFilesDir, DIRECTORY_NAME), environmentDirectoryName(mobileKey));
}

/**
* Names the directory for one environment, hashed so the mobile key itself is never a filename.
* Not {@link LDUtil#urlSafeBase64Hash(String)}, for the reasons {@link LDUtil#hexDigest} gives.
* The directory the store for one environment belongs in.
*
* @param environmentName used as it is, so it has to be safe as a filename and hold nothing secret
*/
private static String environmentDirectoryName(String mobileKey) {
// Half a digest, which is far more than enough to keep two mobile keys apart.
return LDUtil.hexDigest(mobileKey, 16);
static File defaultDirectory(File noBackupFilesDir, String environmentName) {
return new File(new File(noBackupFilesDir, DIRECTORY_NAME), environmentName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,9 @@ static EventStore create(
boolean persistEvents,
LDLogger logger
) {
return new EventStore(EventDirectory.defaultDirectory(noBackupFilesDir, mobileKey), capacity,
EventProcessorBuilder.DEFAULT_CAPACITY_BYTES, EventProcessorBuilder.DEFAULT_MAX_AGE_MILLIS,
persistEvents, logger);
File directory = EventDirectory.defaultDirectory(noBackupFilesDir, LDUtil.hashedStorageName(mobileKey));
return new EventStore(directory, capacity, EventProcessorBuilder.DEFAULT_CAPACITY_BYTES,
EventProcessorBuilder.DEFAULT_MAX_AGE_MILLIS, persistEvents, logger);
}

/**
Expand All @@ -255,7 +255,8 @@ static EventStore create(
LDLogger logger
) {
EventDirectory.Location location =
() -> EventDirectory.defaultDirectory(platformState.getNoBackupFilesDir(), mobileKey);
() -> EventDirectory.defaultDirectory(platformState.getNoBackupFilesDir(),
LDUtil.hashedStorageName(mobileKey));
return new EventStore(location, capacity, byteCapacity, maxAgeMillis, persistEvents, logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,15 @@ static String hexDigest(String input, int bytes) {
}
}

/**
* A name for what is stored for a mobile key, safe as a filename and not holding the key itself.
* Not {@link #urlSafeBase64Hash(String)}, for the reasons {@link #hexDigest} gives.
*/
static String hashedStorageName(String mobileKey) {
// Half a digest, which is far more than enough to keep two mobile keys apart.
return hexDigest(mobileKey, 16);
}

static String urlSafeBase64(LDContext context) {
return Base64.encodeToString(JsonSerialization.serialize(context).getBytes(),
Base64.URL_SAFE + Base64.NO_WRAP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void anImmediateTrackDoesNotWaitForACommitInProgress() throws Exception {
// summary would be. The track has nothing to wait for it about.
CountDownLatch commitStalled = new CountDownLatch(1);
CountDownLatch commitResumes = new CountDownLatch(1);
EventStore store = new EventStore(EventDirectory.defaultDirectory(eventsDirectory.getRoot(), MOBILE_KEY),
EventStore store = new EventStore(environmentDirectory(),
DEFAULT_CAPACITY, true, logging.logger) {
@Override
void commit() {
Expand Down Expand Up @@ -730,7 +730,7 @@ public void aFlushAfterTheOpenLogVanishedWhileItWasHeldHearsOfTheLoss() throws E
scheduler.submit(() -> { }).get(2, TimeUnit.SECONDS);
store.stage("{\"kind\":\"custom\",\"key\":\"vanished\"}".getBytes(StandardCharsets.UTF_8));
store.commit();
File[] logs = EventDirectory.defaultDirectory(eventsDirectory.getRoot(), MOBILE_KEY)
File[] logs = environmentDirectory()
.listFiles((dir, name) -> name.startsWith(EventDirectory.LOG_PREFIX));
assertNotNull(logs);
assertEquals(1, logs.length);
Expand Down Expand Up @@ -774,14 +774,18 @@ private static File findFile(File directory, String name) {
return null;
}

private File environmentDirectory() {
return EventDirectory.defaultDirectory(eventsDirectory.getRoot(), LDUtil.hashedStorageName(MOBILE_KEY));
}

/** @return how many events of this kind the store holds, read as another process would read it */
/**
* Every event in the environment's directory, read straight off the disk the way a process starting after a
* kill finds them: every log, whoever has it open, and every batch.
*/
private List<byte[]> payloadsOnDisk() throws IOException {
List<byte[]> payloads = new ArrayList<>();
File[] files = EventDirectory.defaultDirectory(eventsDirectory.getRoot(), MOBILE_KEY).listFiles();
File[] files = environmentDirectory().listFiles();
if (files == null) {
return payloads;
}
Expand Down Expand Up @@ -2837,7 +2841,7 @@ public void aFlushFailsWhileTheStoredEventsCannotBeListed() throws Exception {
assumePersisting();
try (HttpServer server = HttpServer.start(Handlers.status(202))) {
EventProcessor eventProcessor = makeEventProcessor(server, eventsBuilder(DEFAULT_CAPACITY), true);
File directory = EventDirectory.defaultDirectory(eventsDirectory.getRoot(), MOBILE_KEY);
File directory = environmentDirectory();
try {
eventProcessor.recordCustomEvent(CONTEXT, "first", LDValue.ofNull(), null);
assertTrue(awaitFlush(eventProcessor, 10, TimeUnit.SECONDS));
Expand Down Expand Up @@ -2988,7 +2992,7 @@ public void eventsRecordedDuringAnOutageAreDeliveredWithTheBatchesRefusedBeforeT
@Test
public void theFirstFlushAfterADamagedLogFromAPreviousRunHearsOfTheLoss() throws Exception {
assumePersisting();
File directory = EventDirectory.defaultDirectory(eventsDirectory.getRoot(), MOBILE_KEY);
File directory = environmentDirectory();
assertTrue(directory.mkdirs() || directory.isDirectory());
// A header and then a frame whose checksum does not match, as storage that damaged it leaves it.
ByteArrayOutputStream log = new ByteArrayOutputStream();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ private static EventStore.Batch closeOne(EventStore store) {
return closed.get(0);
}

// naming

@Test
public void anEnvironmentsEventsAreInADirectoryNamedAsItIsToldWithoutHashingItAgain() {
File directory = EventDirectory.defaultDirectory(new File("root"), "an-environment");
assertEquals("an-environment", directory.getName());
assertEquals("com.launchdarkly.events", directory.getParentFile().getName());
}

// staging

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public void testUrlSafeBase64Hash() {
Assert.assertEquals(expectedOutput, output);
}

@Test
public void testHashedStorageNameDoesNotHoldTheKey() {
String name = LDUtil.hashedStorageName("mob-12345");
Assert.assertEquals("3a4f14b3feba38b362ad80b504238faa", name);
Assert.assertFalse(name.contains("mob-12345"));
}

@Test
public void testValidateStringValue() {
Assert.assertNotNull(LDUtil.validateStringValue(""));
Expand Down
Loading