Search in sources :

Example 1 with AdministrationException

use of org.apache.nifi.admin.service.AdministrationException in project nifi by apache.

the class JwtService method generateSignedToken.

/**
 * Generates a signed JWT token from the provided (Spring Security) login authentication token.
 *
 * @param authenticationToken an instance of the Spring Security token after login credentials have been verified against the respective information source
 * @return a signed JWT containing the user identity and the identity provider, Base64-encoded
 * @throws JwtException if there is a problem generating the signed token
 */
public String generateSignedToken(final LoginAuthenticationToken authenticationToken) throws JwtException {
    if (authenticationToken == null) {
        throw new IllegalArgumentException("Cannot generate a JWT for a null authentication token");
    }
    // Set expiration from the token
    final Calendar expiration = Calendar.getInstance();
    expiration.setTimeInMillis(authenticationToken.getExpiration());
    final Object principal = authenticationToken.getPrincipal();
    if (principal == null || StringUtils.isEmpty(principal.toString())) {
        final String errorMessage = "Cannot generate a JWT for a token with an empty identity issued by " + authenticationToken.getIssuer();
        logger.error(errorMessage);
        throw new JwtException(errorMessage);
    }
    // Create a JWT with the specified authentication
    final String identity = principal.toString();
    final String username = authenticationToken.getName();
    try {
        // Get/create the key for this user
        final Key key = keyService.getOrCreateKey(identity);
        final byte[] keyBytes = key.getKey().getBytes(StandardCharsets.UTF_8);
        logger.trace("Generating JWT for " + authenticationToken);
        // Build the token
        return Jwts.builder().setSubject(identity).setIssuer(authenticationToken.getIssuer()).setAudience(authenticationToken.getIssuer()).claim(USERNAME_CLAIM, username).claim(KEY_ID_CLAIM, key.getId()).setExpiration(expiration.getTime()).setIssuedAt(Calendar.getInstance().getTime()).signWith(SIGNATURE_ALGORITHM, keyBytes).compact();
    } catch (NullPointerException | AdministrationException e) {
        final String errorMessage = "Could not retrieve the signing key for JWT for " + identity;
        logger.error(errorMessage, e);
        throw new JwtException(errorMessage, e);
    }
}
Also used : Calendar(java.util.Calendar) UnsupportedJwtException(io.jsonwebtoken.UnsupportedJwtException) MalformedJwtException(io.jsonwebtoken.MalformedJwtException) ExpiredJwtException(io.jsonwebtoken.ExpiredJwtException) JwtException(io.jsonwebtoken.JwtException) AdministrationException(org.apache.nifi.admin.service.AdministrationException) Key(org.apache.nifi.key.Key)

Example 2 with AdministrationException

use of org.apache.nifi.admin.service.AdministrationException in project nifi by apache.

the class StandardAuditService method purgeActions.

@Override
public void purgeActions(Date end, Action purgeAction) {
    Transaction transaction = null;
    writeLock.lock();
    try {
        // start the transaction
        transaction = transactionBuilder.start();
        // purge the action database
        PurgeActionsAction purgeActions = new PurgeActionsAction(end, purgeAction);
        transaction.execute(purgeActions);
        // commit the transaction
        transaction.commit();
    } catch (TransactionException | DataAccessException te) {
        rollback(transaction);
        throw new AdministrationException(te);
    } catch (Throwable t) {
        rollback(transaction);
        throw t;
    } finally {
        closeQuietly(transaction);
        writeLock.unlock();
    }
}
Also used : TransactionException(org.apache.nifi.admin.service.transaction.TransactionException) Transaction(org.apache.nifi.admin.service.transaction.Transaction) PurgeActionsAction(org.apache.nifi.admin.service.action.PurgeActionsAction) AdministrationException(org.apache.nifi.admin.service.AdministrationException) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 3 with AdministrationException

use of org.apache.nifi.admin.service.AdministrationException in project nifi by apache.

the class StandardAuditService method getActions.

@Override
public History getActions(HistoryQuery query) {
    Transaction transaction = null;
    History history = null;
    readLock.lock();
    try {
        // start the transaction
        transaction = transactionBuilder.start();
        // seed the accounts
        GetActionsAction getActions = new GetActionsAction(query);
        history = transaction.execute(getActions);
        // commit the transaction
        transaction.commit();
    } catch (TransactionException | DataAccessException te) {
        rollback(transaction);
        throw new AdministrationException(te);
    } catch (Throwable t) {
        rollback(transaction);
        throw t;
    } finally {
        closeQuietly(transaction);
        readLock.unlock();
    }
    return history;
}
Also used : TransactionException(org.apache.nifi.admin.service.transaction.TransactionException) Transaction(org.apache.nifi.admin.service.transaction.Transaction) GetActionsAction(org.apache.nifi.admin.service.action.GetActionsAction) History(org.apache.nifi.history.History) AdministrationException(org.apache.nifi.admin.service.AdministrationException) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 4 with AdministrationException

use of org.apache.nifi.admin.service.AdministrationException in project nifi by apache.

the class StandardKeyService method getOrCreateKey.

@Override
public Key getOrCreateKey(String identity) {
    Transaction transaction = null;
    Key key = null;
    writeLock.lock();
    try {
        // start the transaction
        transaction = transactionBuilder.start();
        // get or create a key
        GetOrCreateKeyAction addActions = new GetOrCreateKeyAction(identity);
        key = transaction.execute(addActions);
        // commit the transaction
        transaction.commit();
    } catch (TransactionException | DataAccessException te) {
        rollback(transaction);
        throw new AdministrationException(te);
    } catch (Throwable t) {
        rollback(transaction);
        throw t;
    } finally {
        closeQuietly(transaction);
        writeLock.unlock();
    }
    return key;
}
Also used : TransactionException(org.apache.nifi.admin.service.transaction.TransactionException) Transaction(org.apache.nifi.admin.service.transaction.Transaction) GetOrCreateKeyAction(org.apache.nifi.admin.service.action.GetOrCreateKeyAction) AdministrationException(org.apache.nifi.admin.service.AdministrationException) Key(org.apache.nifi.key.Key) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Example 5 with AdministrationException

use of org.apache.nifi.admin.service.AdministrationException in project nifi by apache.

the class StandardKeyService method getKey.

@Override
public Key getKey(int id) {
    Transaction transaction = null;
    Key key = null;
    readLock.lock();
    try {
        // start the transaction
        transaction = transactionBuilder.start();
        // get the key
        GetKeyByIdAction addActions = new GetKeyByIdAction(id);
        key = transaction.execute(addActions);
        // commit the transaction
        transaction.commit();
    } catch (TransactionException | DataAccessException te) {
        rollback(transaction);
        throw new AdministrationException(te);
    } catch (Throwable t) {
        rollback(transaction);
        throw t;
    } finally {
        closeQuietly(transaction);
        readLock.unlock();
    }
    return key;
}
Also used : TransactionException(org.apache.nifi.admin.service.transaction.TransactionException) Transaction(org.apache.nifi.admin.service.transaction.Transaction) GetKeyByIdAction(org.apache.nifi.admin.service.action.GetKeyByIdAction) AdministrationException(org.apache.nifi.admin.service.AdministrationException) Key(org.apache.nifi.key.Key) DataAccessException(org.apache.nifi.admin.dao.DataAccessException)

Aggregations

AdministrationException (org.apache.nifi.admin.service.AdministrationException)13 DataAccessException (org.apache.nifi.admin.dao.DataAccessException)8 Transaction (org.apache.nifi.admin.service.transaction.Transaction)8 TransactionException (org.apache.nifi.admin.service.transaction.TransactionException)8 Key (org.apache.nifi.key.Key)4 JwtException (io.jsonwebtoken.JwtException)3 ExpiredJwtException (io.jsonwebtoken.ExpiredJwtException)2 MalformedJwtException (io.jsonwebtoken.MalformedJwtException)2 UnsupportedJwtException (io.jsonwebtoken.UnsupportedJwtException)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ApiResponses (io.swagger.annotations.ApiResponses)2 Consumes (javax.ws.rs.Consumes)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 AddActionsAction (org.apache.nifi.admin.service.action.AddActionsAction)2 GetActionsAction (org.apache.nifi.admin.service.action.GetActionsAction)2 PurgeActionsAction (org.apache.nifi.admin.service.action.PurgeActionsAction)2 LoginAuthenticationToken (org.apache.nifi.web.security.token.LoginAuthenticationToken)2 Claims (io.jsonwebtoken.Claims)1 JwsHeader (io.jsonwebtoken.JwsHeader)1