Search in sources :

Example 1 with ApiKey

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

the class ApiKeyController method getApiKey.

/**
 * @param key
 * @return
 * @throws ApiKeyException
 */
public ApiKey getApiKey(String key) throws ApiKeyException {
    String[] parts = key.split(Secret.KEY_ID_SEPARATOR_REGEX);
    if (parts.length < 2) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
    }
    ApiKey apiKey = apiKeyFacade.findByPrefix(parts[0]);
    if (apiKey == null) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
    }
    // ___MinLength can be set to 0 b/c no validation is needed if the key was in db
    Secret secret = new Secret(parts[0], parts[1], apiKey.getSalt());
    if (!secret.getSha256HexDigest().equals(apiKey.getSecret())) {
        throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_NOT_FOUND, Level.FINE);
    }
    return apiKey;
}
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)

Example 2 with ApiKey

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey 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 3 with ApiKey

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey 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 4 with ApiKey

use of io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey 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 5 with ApiKey

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

the class ApiKeyController method deleteAll.

/**
 * @param user
 */
public void deleteAll(Users user) throws ApiKeyException {
    List<ApiKey> keys = apiKeyFacade.findByUser(user);
    for (ApiKey key : keys) {
        // run delete handlers
        ApiKeyHandler.runApiKeyDeleteHandlers(apiKeyHandlers, key);
        apiKeyFacade.remove(key);
    }
    sendDeletedAllEmail(user);
}
Also used : ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey)

Aggregations

ApiKey (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey)12 ApiKeyException (io.hops.hopsworks.exceptions.ApiKeyException)5 ApiScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope)5 ApiKeyScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope)4 Date (java.util.Date)4 Users (io.hops.hopsworks.persistence.entity.user.Users)3 ResourceRequest (io.hops.hopsworks.common.api.ResourceRequest)2 Secret (io.hops.hopsworks.common.security.utils.Secret)2 JWTRequired (io.hops.hopsworks.jwt.annotation.JWTRequired)2 ApiOperation (io.swagger.annotations.ApiOperation)2 ArrayList (java.util.ArrayList)2 Produces (javax.ws.rs.Produces)2 HopsworksSecurityContext (io.hops.hopsworks.api.filter.util.HopsworksSecurityContext)1 Subject (io.hops.hopsworks.api.filter.util.Subject)1 RESTApiJsonResponse (io.hops.hopsworks.api.util.RESTApiJsonResponse)1 UserException (io.hops.hopsworks.exceptions.UserException)1 HdfsUsers (io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers)1 YarnApplicationstate (io.hops.hopsworks.persistence.entity.jobs.history.YarnApplicationstate)1 JsonResponse (io.hops.hopsworks.restutils.JsonResponse)1 IOException (java.io.IOException)1