Search in sources :

Example 1 with ApiKeyScope

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.

the class ApiKeyController method addScope.

/**
 * @param user
 * @param keyName
 * @param scopes
 * @return
 * @throws ApiKeyException
 */
public ApiKey addScope(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
    ApiKey apiKey = validate(user, keyName, scopes);
    List<ApiKeyScope> newScopes;
    Set<ApiScope> oldScopes = toApiScope(apiKey.getApiKeyScopeCollection());
    scopes.removeAll(oldScopes);
    if (!scopes.isEmpty()) {
        newScopes = getKeyScopes(scopes, apiKey);
        apiKey.getApiKeyScopeCollection().addAll(newScopes);
        apiKey.setModified(new Date());
        apiKey = apiKeyFacade.update(apiKey);
        // run api key update handlers
        ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey);
    }
    return apiKey;
}
Also used : ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey) ApiKeyScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope) ApiScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope) Date(java.util.Date)

Example 2 with ApiKeyScope

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.

the class ApiKeyController method update.

/**
 * @param user
 * @param keyName
 * @param scopes
 * @return
 * @throws ApiKeyException
 */
public ApiKey update(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
    ApiKey apiKey = validate(user, keyName, scopes);
    Collection<ApiKeyScope> oldScopes = apiKey.getApiKeyScopeCollection();
    List<ApiKeyScope> toKeep = new ArrayList<>();
    List<ApiKeyScope> toAdd = new ArrayList<>();
    boolean exist;
    boolean added = false;
    for (ApiScope scope : scopes) {
        exist = false;
        for (ApiKeyScope apiKeyScope : oldScopes) {
            if (apiKeyScope.getScope().equals(scope)) {
                toKeep.add(apiKeyScope);
                exist = true;
                break;
            }
        }
        if (!exist) {
            added = true;
            toAdd.add(new ApiKeyScope(scope, apiKey));
        }
    }
    boolean update = false;
    oldScopes.removeAll(toKeep);
    if (!oldScopes.isEmpty()) {
        for (ApiKeyScope apiKeyScope : oldScopes) {
            apiKeyScopeFacade.remove(apiKeyScope);
        }
        update = true;
    }
    if (added) {
        toKeep.addAll(toAdd);
        update = true;
    }
    if (update) {
        // make a copy of toRemove scopes
        Collection<ApiKeyScope> toRemove = new ArrayList<>(oldScopes);
        apiKey.setApiKeyScopeCollection(toKeep);
        apiKey.setModified(new Date());
        apiKey = apiKeyFacade.update(apiKey);
        // run api key handlers
        ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey, toAdd);
        ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, apiKey, toRemove);
    }
    return apiKey;
}
Also used : ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey) ApiKeyScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope) ArrayList(java.util.ArrayList) ApiScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope) Date(java.util.Date)

Example 3 with ApiKeyScope

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.

the class ApiKeyController method removeScope.

/**
 * @param user
 * @param keyName
 * @param scopes
 * @return
 * @throws ApiKeyException
 */
public ApiKey removeScope(Users user, String keyName, Set<ApiScope> scopes) throws ApiKeyException {
    ApiKey apiKey = validate(user, keyName, scopes);
    Collection<ApiKeyScope> oldScopes = apiKey.getApiKeyScopeCollection();
    List<ApiKeyScope> toRemove = new ArrayList<>();
    for (ApiScope scope : scopes) {
        for (ApiKeyScope apiKeyScope : oldScopes) {
            if (apiKeyScope.getScope().equals(scope)) {
                toRemove.add(apiKeyScope);
                break;
            }
        }
    }
    boolean removed = apiKey.getApiKeyScopeCollection().removeAll(toRemove);
    if (removed && !apiKey.getApiKeyScopeCollection().isEmpty()) {
        // this should not be necessary
        for (ApiKeyScope apiKeyScope : toRemove) {
            apiKeyScopeFacade.remove(apiKeyScope);
        }
        apiKey.setModified(new Date());
        apiKey = apiKeyFacade.update(apiKey);
        // run api key update handlers
        ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, apiKey, toRemove);
    } else if (removed && apiKey.getApiKeyScopeCollection().isEmpty()) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_SCOPE_EMPTY, Level.FINE);
    }
    return apiKey;
}
Also used : ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey) ApiKeyScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope) ArrayList(java.util.ArrayList) ApiScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope) Date(java.util.Date)

Example 4 with ApiKeyScope

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope in project hopsworks by logicalclocks.

the class ApiKeyController method createNewKey.

/**
 * Create new key for the give user with the given key name and scopes.
 * @param user
 * @param keyName
 * @param scopes
 * @throws UserException
 * @throws ApiKeyException
 * @return
 */
public String createNewKey(Users user, String keyName, Set<ApiScope> scopes, Boolean reserved) throws UserException, ApiKeyException {
    if (user == null) {
        throw new UserException(RESTCodes.UserErrorCode.USER_WAS_NOT_FOUND, Level.FINE);
    }
    if (keyName == null || keyName.isEmpty()) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_NOT_SPECIFIED, Level.FINE);
    }
    if (keyName.length() > 45) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_NOT_VALID, Level.FINE);
    }
    if (scopes == null || scopes.isEmpty()) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_SCOPE_NOT_SPECIFIED, Level.FINE);
    }
    ApiKey apiKey = apiKeyFacade.findByUserAndName(user, keyName);
    if (apiKey != null) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NAME_EXIST, Level.FINE);
    }
    Secret secret = generateApiKey();
    Date date = new Date();
    apiKey = new ApiKey(user, secret.getPrefix(), secret.getSha256HexDigest(), secret.getSalt(), date, date, keyName, reserved);
    List<ApiKeyScope> keyScopes = getKeyScopes(scopes, apiKey);
    apiKey.setApiKeyScopeCollection(keyScopes);
    apiKeyFacade.save(apiKey);
    // run create handlers
    ApiKeyHandler.runApiKeyCreateHandlers(apiKeyHandlers, apiKey);
    sendCreatedEmail(user, keyName, date, scopes);
    return secret.getPrefixPlusSecret();
}
Also used : ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) Secret(io.hops.hopsworks.common.security.utils.Secret) ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey) ApiKeyScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope) UserException(io.hops.hopsworks.exceptions.UserException) Date(java.util.Date)

Aggregations

ApiKey (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey)4 ApiKeyScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope)4 Date (java.util.Date)4 ApiScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope)3 ApiKeyException (io.hops.hopsworks.exceptions.ApiKeyException)2 ArrayList (java.util.ArrayList)2 Secret (io.hops.hopsworks.common.security.utils.Secret)1 UserException (io.hops.hopsworks.exceptions.UserException)1