Search in sources :

Example 1 with JournalEntry

use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.

the class JournalBasedTokenStateService method getTokenExpiration.

@Override
public long getTokenExpiration(final String tokenId, boolean validate) throws UnknownTokenException {
    // Check the in-memory collection first, to avoid file access when possible
    try {
        // return the expiration from there.
        return super.getTokenExpiration(tokenId, validate);
    } catch (UnknownTokenException e) {
    // It's not in memory
    }
    if (validate) {
        validateToken(tokenId);
    }
    // If there is no associated state in the in-memory cache, proceed to check the journal
    long expiration = 0;
    try {
        JournalEntry entry = journal.get(tokenId);
        if (entry == null) {
            throw new UnknownTokenException(tokenId);
        }
        expiration = Long.parseLong(entry.getExpiration());
        super.addToken(tokenId, Long.parseLong(entry.getIssueTime()), expiration, Long.parseLong(entry.getMaxLifetime()));
    } catch (IOException e) {
        log.failedToLoadJournalEntry(e);
    }
    return expiration;
}
Also used : UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) IOException(java.io.IOException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry)

Example 2 with JournalEntry

use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.

the class JournalBasedTokenStateService method getMaxLifetime.

@Override
protected long getMaxLifetime(final String tokenId) {
    long result = super.getMaxLifetime(tokenId);
    // If there is no result from the in-memory collection, proceed to check the journal
    if (result < 1L) {
        try {
            JournalEntry entry = journal.get(tokenId);
            if (entry == null) {
                throw new UnknownTokenException(tokenId);
            }
            result = Long.parseLong(entry.getMaxLifetime());
            super.setMaxLifetime(tokenId, Long.parseLong(entry.getIssueTime()), result);
        } catch (Exception e) {
            log.failedToLoadJournalEntry(e);
        }
    }
    return result;
}
Also used : UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException)

Example 3 with JournalEntry

use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.

the class JournalBasedTokenStateService method init.

@Override
public void init(final GatewayConfig config, final Map<String, String> options) throws ServiceLifecycleException {
    super.init(config, options);
    try {
        // Initialize the token state journal
        journal = TokenStateJournalFactory.create(config);
        // Load any persisted journal entries, and add them to the in-memory collection
        List<JournalEntry> entries = journal.get();
        for (JournalEntry entry : entries) {
            String id = entry.getTokenId();
            try {
                long issueTime = Long.parseLong(entry.getIssueTime());
                long expiration = Long.parseLong(entry.getExpiration());
                long maxLifetime = Long.parseLong(entry.getMaxLifetime());
                // Add the token state to memory
                super.addToken(id, issueTime, expiration, maxLifetime);
            } catch (Exception e) {
                log.failedToLoadJournalEntry(Tokens.getTokenIDDisplayText(id), e);
            }
        }
    } catch (IOException e) {
        throw new ServiceLifecycleException("Failed to load persisted state from the token state journal", e);
    }
}
Also used : ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) UnknownTokenException(org.apache.knox.gateway.services.security.token.UnknownTokenException) ServiceLifecycleException(org.apache.knox.gateway.services.ServiceLifecycleException) IOException(java.io.IOException)

Example 4 with JournalEntry

use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.

the class MultiFileTokenStateJournal method get.

@Override
public JournalEntry get(final String tokenId) throws IOException {
    JournalEntry result = null;
    Path entryFilePath = journalDir.resolve(tokenId + ENTRY_FILE_EXT);
    if (Files.exists(entryFilePath)) {
        try (FileChannel fileChannel = FileChannel.open(entryFilePath, StandardOpenOption.READ)) {
            fileChannel.lock(0L, Long.MAX_VALUE, true);
            List<FileJournalEntry> entries = loadJournal(fileChannel);
            if (entries.isEmpty()) {
                log.journalEntryNotFound(Tokens.getTokenIDDisplayText(tokenId));
            } else {
                result = entries.get(0);
            }
        }
    } else {
        log.journalEntryNotFound(Tokens.getTokenIDDisplayText(tokenId));
    }
    return result;
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry)

Example 5 with JournalEntry

use of org.apache.knox.gateway.services.token.state.JournalEntry in project knox by apache.

the class MultiFileTokenStateJournal method add.

@Override
public void add(final List<JournalEntry> entries) throws IOException {
    // Persist each journal entry as an individual file in the journal directory
    for (JournalEntry entry : entries) {
        final Path entryFile = journalDir.resolve(entry.getTokenId() + ENTRY_FILE_EXT);
        log.persistingJournalEntry(getDisplayableJournalFilepath(entry.getTokenId(), entryFile.toString()));
        try (FileChannel fileChannel = FileChannel.open(entryFile, StandardOpenOption.WRITE, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
            fileChannel.lock();
            try (OutputStream out = Channels.newOutputStream(fileChannel)) {
                BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
                writer.write(entry.toString());
                writer.newLine();
                writer.flush();
            }
            log.addedJournalEntry(Tokens.getTokenIDDisplayText(entry.getTokenId()));
        } catch (IOException e) {
            log.failedToPersistJournalEntry(Tokens.getTokenIDDisplayText(entry.getTokenId()), e);
            throw e;
        }
    }
}
Also used : Path(java.nio.file.Path) FileChannel(java.nio.channels.FileChannel) OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) JournalEntry(org.apache.knox.gateway.services.token.state.JournalEntry) BufferedWriter(java.io.BufferedWriter)

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