Search in sources :

Example 1 with KeyEntityRowMapper

use of org.apache.nifi.registry.db.mapper.KeyEntityRowMapper in project nifi-registry by apache.

the class DatabaseKeyService method getKey.

@Override
public Key getKey(String id) {
    if (id == null) {
        throw new IllegalArgumentException("Id cannot be null");
    }
    Key key = null;
    readLock.lock();
    try {
        final String sql = "SELECT * FROM signing_key WHERE id = ?";
        KeyEntity keyEntity;
        try {
            keyEntity = jdbcTemplate.queryForObject(sql, new KeyEntityRowMapper(), id);
        } catch (EmptyResultDataAccessException e) {
            keyEntity = null;
        }
        if (keyEntity != null) {
            key = DataModelMapper.map(keyEntity);
        } else {
            logger.debug("No signing key found with id='" + id + "'");
        }
    } finally {
        readLock.unlock();
    }
    return key;
}
Also used : KeyEntityRowMapper(org.apache.nifi.registry.db.mapper.KeyEntityRowMapper) KeyEntity(org.apache.nifi.registry.db.entity.KeyEntity) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Key(org.apache.nifi.registry.security.key.Key)

Example 2 with KeyEntityRowMapper

use of org.apache.nifi.registry.db.mapper.KeyEntityRowMapper in project nifi-registry by apache.

the class DatabaseKeyService method getOrCreateKey.

@Override
public Key getOrCreateKey(String tenantIdentity) {
    if (tenantIdentity == null) {
        throw new IllegalArgumentException("Identity cannot be null");
    }
    Key key;
    writeLock.lock();
    try {
        final String selectSql = "SELECT * FROM signing_key WHERE tenant_identity = ?";
        KeyEntity existingKeyEntity;
        try {
            existingKeyEntity = jdbcTemplate.queryForObject(selectSql, new KeyEntityRowMapper(), tenantIdentity);
        } catch (EmptyResultDataAccessException e) {
            existingKeyEntity = null;
        }
        if (existingKeyEntity == null) {
            logger.debug("No key found with identity='" + tenantIdentity + "'. Creating new key.");
            final KeyEntity newKeyEntity = new KeyEntity();
            newKeyEntity.setId(UUID.randomUUID().toString());
            newKeyEntity.setTenantIdentity(tenantIdentity);
            newKeyEntity.setKeyValue(UUID.randomUUID().toString());
            final String insertSql = "INSERT INTO signing_key (ID, TENANT_IDENTITY, KEY_VALUE) VALUES (?, ?, ?)";
            jdbcTemplate.update(insertSql, newKeyEntity.getId(), newKeyEntity.getTenantIdentity(), newKeyEntity.getKeyValue());
            key = DataModelMapper.map(newKeyEntity);
        } else {
            key = DataModelMapper.map(existingKeyEntity);
        }
    } finally {
        writeLock.unlock();
    }
    return key;
}
Also used : KeyEntityRowMapper(org.apache.nifi.registry.db.mapper.KeyEntityRowMapper) KeyEntity(org.apache.nifi.registry.db.entity.KeyEntity) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Key(org.apache.nifi.registry.security.key.Key)

Aggregations

KeyEntity (org.apache.nifi.registry.db.entity.KeyEntity)2 KeyEntityRowMapper (org.apache.nifi.registry.db.mapper.KeyEntityRowMapper)2 Key (org.apache.nifi.registry.security.key.Key)2 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)2