Skip to content

Commit

Permalink
[TEST] Cleanup FileUserPasswdStoreTests (#35329)
Browse files Browse the repository at this point in the history
The file realm has not supported custom filenames/locations since at
least 5.0, but this test still tried to configure them.

Remove all configuration of file locations, and cleaned up a few other
warnings and deprecations
  • Loading branch information
tvernum committed Nov 8, 2018
1 parent a4d979c commit 566979c
Showing 1 changed file with 15 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,32 @@ public void init() {
}

@After
public void shutdown() throws InterruptedException {
public void shutdown() {
terminate(threadPool);
}

public void testStore_ConfiguredWithUnreadableFile() throws Exception {
Path xpackConf = env.configFile();
Files.createDirectories(xpackConf);
Path file = xpackConf.resolve("users");
Path configDir = env.configFile();
Files.createDirectories(configDir);
Path file = configDir.resolve("users");

// writing in utf_16 should cause a parsing error as we try to read the file in utf_8
Files.write(file, Collections.singletonList("aldlfkjldjdflkjd"), StandardCharsets.UTF_16);

Settings fileSettings = randomBoolean() ? Settings.EMPTY : Settings.builder().put("files.users", file.toAbsolutePath()).build();
RealmConfig config = getRealmConfig(fileSettings);
RealmConfig config = getRealmConfig();
ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool);
FileUserPasswdStore store = new FileUserPasswdStore(config, watcherService);
assertThat(store.usersCount(), is(0));
}

public void testStore_AutoReload() throws Exception {
Path users = getDataPath("users");
Path xpackConf = env.configFile();
Files.createDirectories(xpackConf);
Path file = xpackConf.resolve("users");
Path configDir = env.configFile();
Files.createDirectories(configDir);
Path file = configDir.resolve("users");
Files.copy(users, file, StandardCopyOption.REPLACE_EXISTING);
final Hasher hasher = Hasher.resolve(settings.get("xpack.security.authc.password_hashing.algorithm"));
Settings fileSettings = randomBoolean() ? Settings.EMPTY : Settings.builder().put("files.users", file.toAbsolutePath()).build();
RealmConfig config = getRealmConfig(fileSettings);
RealmConfig config = getRealmConfig();
ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool);
final CountDownLatch latch = new CountDownLatch(1);

Expand Down Expand Up @@ -120,23 +118,19 @@ public void testStore_AutoReload() throws Exception {
assertThat(result.getUser(), is(user));
}

private RealmConfig getRealmConfig(Settings fileSettings) {
private RealmConfig getRealmConfig() {
final RealmConfig.RealmIdentifier identifier = new RealmConfig.RealmIdentifier("file", "file-test");
return new RealmConfig(identifier, settings, env, threadPool.getThreadContext());
}

public void testStore_AutoReload_WithParseFailures() throws Exception {
Path users = getDataPath("users");
Path xpackConf = env.configFile();
Files.createDirectories(xpackConf);
Path testUsers = xpackConf.resolve("users");
Path confDir = env.configFile();
Files.createDirectories(confDir);
Path testUsers = confDir.resolve("users");
Files.copy(users, testUsers, StandardCopyOption.REPLACE_EXISTING);

Settings fileSettings = Settings.builder()
.put("files.users", testUsers.toAbsolutePath())
.build();

RealmConfig config = getRealmConfig(fileSettings);
RealmConfig config = getRealmConfig();
ResourceWatcherService watcherService = new ResourceWatcherService(settings, threadPool);
final CountDownLatch latch = new CountDownLatch(1);

Expand Down Expand Up @@ -192,6 +186,7 @@ public void testParseFile_Empty() throws Exception {
Path empty = createTempFile();
Logger logger = CapturingLogger.newCapturingLogger(Level.DEBUG, null);
Map<String, char[]> users = FileUserPasswdStore.parseFile(empty, logger, Settings.EMPTY);
assertThat(users, notNullValue());
assertThat(users.isEmpty(), is(true));
List<String> events = CapturingLogger.output(logger.getName(), Level.DEBUG);
assertThat(events.size(), is(1));
Expand Down

0 comments on commit 566979c

Please sign in to comment.