Search in sources :

Example 11 with JournalEntry

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;
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) ArrayList(java.util.ArrayList) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry)

Example 12 with JournalEntry

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));
}
Also used : TokenStateJournal(org.apache.knox.gateway.services.token.state.TokenStateJournal) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 13 with JournalEntry

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));
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) TokenStateJournal(org.apache.knox.gateway.services.token.state.TokenStateJournal) HashMap(java.util.HashMap) Map(java.util.Map) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Aggregations

JournalEntry (org.apache.knox.gateway.services.token.state.JournalEntry)13 IOException (java.io.IOException)6 UnknownTokenException (org.apache.knox.gateway.services.security.token.UnknownTokenException)5 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)4 TokenStateJournal (org.apache.knox.gateway.services.token.state.TokenStateJournal)4 Test (org.junit.Test)4 FileChannel (java.nio.channels.FileChannel)3 Path (java.nio.file.Path)3 ServiceLifecycleException (org.apache.knox.gateway.services.ServiceLifecycleException)3 ArrayList (java.util.ArrayList)2 BufferedWriter (java.io.BufferedWriter)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 AliasServiceException (org.apache.knox.gateway.services.security.AliasServiceException)1