use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class MultiFileTokenStateJournal method loadJournal.
@Override
protected List<JournalEntry> loadJournal() throws IOException {
List<JournalEntry> entries = new ArrayList<>();
// List all the journal entry files in the directory, and create journal entries for them
if (Files.exists(journalDir)) {
log.loadingPersistedJournalEntries();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(journalDir, ENTRY_FILE_EXT_FILTER)) {
for (Path entryFilePath : stream) {
try (FileChannel fileChannel = FileChannel.open(entryFilePath, StandardOpenOption.READ)) {
fileChannel.lock(0L, Long.MAX_VALUE, true);
entries.addAll(loadJournal(fileChannel));
if (entries.isEmpty()) {
log.emptyJournalEntry(getDisplayableJournalFilepath(entryFilePath.toString()));
} else {
// Should only be a single entry for this implementation
log.loadedPersistedJournalEntry(Tokens.getTokenIDDisplayText(entries.get(0).getTokenId()));
}
}
}
}
}
return entries;
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class AbstractFileTokenStateJournalTest method testSingleTokenRoundTrip.
@Test
public void testSingleTokenRoundTrip() throws Exception {
GatewayConfig config = getGatewayConfig();
TokenStateJournal journal = createTokenStateJournal(config);
final String tokenId = String.valueOf(UUID.randomUUID());
// Verify that the token state has not yet been journaled
assertNull(journal.get(tokenId));
long issueTime = System.currentTimeMillis();
long expiration = issueTime + TimeUnit.MINUTES.toMillis(5);
long maxLifetime = issueTime + (5 * TimeUnit.MINUTES.toMillis(5));
journal.add(tokenId, issueTime, expiration, maxLifetime, null);
// Get the token state from the journal, and validate its contents
JournalEntry entry = journal.get(tokenId);
assertNotNull(entry);
assertEquals(tokenId, entry.getTokenId());
assertEquals(issueTime, Long.parseLong(entry.getIssueTime()));
assertEquals(expiration, Long.parseLong(entry.getExpiration()));
assertEquals(maxLifetime, Long.parseLong(entry.getMaxLifetime()));
journal.remove(tokenId);
// Verify that the token state can no longer be gotten from the journal
assertNull(journal.get(tokenId));
}
use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.
the class AbstractFileTokenStateJournalTest method testMultipleTokensRoundTrip.
@Test
public void testMultipleTokensRoundTrip() throws Exception {
GatewayConfig config = getGatewayConfig();
TokenStateJournal journal = createTokenStateJournal(config);
final List<String> tokenIds = new ArrayList<>();
for (int i = 0; i < 10; i++) {
tokenIds.add(String.valueOf(UUID.randomUUID()));
}
Map<String, JournalEntry> journalEntries = new HashMap<>();
// Verify that the token state has not yet been journaled, and create a JournalEntry for it
for (String tokenId : tokenIds) {
assertNull(journal.get(tokenId));
long issueTime = System.currentTimeMillis();
long expiration = issueTime + TimeUnit.MINUTES.toMillis(5);
long maxLifetime = issueTime + (5 * TimeUnit.MINUTES.toMillis(5));
journalEntries.put(tokenId, createTestJournalEntry(tokenId, issueTime, expiration, maxLifetime));
}
for (JournalEntry entry : journalEntries.values()) {
journal.add(entry);
}
for (Map.Entry<String, JournalEntry> journalEntry : journalEntries.entrySet()) {
final String tokenId = journalEntry.getKey();
// Get the token state from the journal, and validate its contents
JournalEntry entry = journal.get(tokenId);
assertNotNull(entry);
JournalEntry original = journalEntry.getValue();
assertEquals(original.getTokenId(), entry.getTokenId());
assertEquals(original.getIssueTime(), entry.getIssueTime());
assertEquals(original.getExpiration(), entry.getExpiration());
assertEquals(original.getMaxLifetime(), entry.getMaxLifetime());
}
// Test loading of persisted token state
List<JournalEntry> loadedEntries = journal.get();
assertNotNull(loadedEntries);
assertFalse(loadedEntries.isEmpty());
assertEquals(10, loadedEntries.size());
for (JournalEntry loaded : loadedEntries) {
JournalEntry original = journalEntries.get(loaded.getTokenId());
assertNotNull(original);
assertEquals(original.getTokenId(), loaded.getTokenId());
assertEquals(original.getIssueTime(), loaded.getIssueTime());
assertEquals(original.getExpiration(), loaded.getExpiration());
assertEquals(original.getMaxLifetime(), loaded.getMaxLifetime());
}
for (String tokenId : tokenIds) {
journal.remove(tokenId);
// Verify that the token state can no longer be gotten from the journal
assertNull(journal.get(tokenId));
}
}
Aggregations