Search in sources :

Example 6 with ApiKeyException

use of io.hops.hopsworks.exceptions.ApiKeyException in project hopsworks by logicalclocks.

the class ApiKeyResource method validateScopes.

// For a strange reason the Set of user supplied ApiScope(s) is marshalled
// to String even though it's a Set of ApiScope. We need to explicitly convert
// them to ApiScope
private Set<ApiScope> validateScopes(Users user, Set<ApiScope> scopes) throws ApiKeyException {
    Set<ApiScope> validScopes = getScopesForUser(user);
    Set<ApiScope> validatedScopes = new HashSet<>(scopes.size());
    for (Object scope : scopes) {
        try {
            ApiScope apiScope = ApiScope.fromString((String) scope);
            if (!validScopes.contains(apiScope)) {
                throw new ApiKeyException(RESTCodes.ApiKeyErrorCode.KEY_SCOPE_CONTROL_EXCEPTION, Level.FINE, "User is not allowed to issue token " + apiScope.name(), "User " + user.getUsername() + " tried to generate API key with scope " + apiScope + " but it's role is not allowed to");
            }
            validatedScopes.add(apiScope);
        } catch (IllegalArgumentException iae) {
            throw new WebApplicationException("Scope need to set a valid scope, but found: " + scope, Response.Status.NOT_FOUND);
        }
    }
    return validatedScopes;
}
Also used : ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) WebApplicationException(javax.ws.rs.WebApplicationException) ApiScope(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope) HashSet(java.util.HashSet)

Example 7 with ApiKeyException

use of io.hops.hopsworks.exceptions.ApiKeyException in project hopsworks by logicalclocks.

the class JupyterConfigFilesGenerator method generateConfiguration.

public JupyterPaths generateConfiguration(Project project, String secretConfig, String hdfsUser, Users hopsworksUser, JupyterSettings js, Integer port, String allowOrigin) throws ServiceException, JobException {
    boolean newDir = false;
    JupyterPaths jp = generateJupyterPaths(project, hdfsUser, secretConfig);
    try {
        newDir = createJupyterDirs(jp);
        createConfigFiles(jp, hdfsUser, hopsworksUser, project, port, js, allowOrigin);
    } catch (IOException | ServiceException | ServiceDiscoveryException | ApiKeyException e) {
        if (newDir) {
            // if the folder was newly created delete it
            removeProjectUserDirRecursive(jp);
        }
        LOGGER.log(Level.SEVERE, "Error in initializing JupyterConfig for project: {0}. {1}", new Object[] { project.getName(), e });
        throw new ServiceException(RESTCodes.ServiceErrorCode.JUPYTER_ADD_FAILURE, Level.SEVERE, null, e.getMessage(), e);
    }
    return jp;
}
Also used : ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) ServiceException(io.hops.hopsworks.exceptions.ServiceException) ServiceDiscoveryException(com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException) IOException(java.io.IOException)

Example 8 with ApiKeyException

use of io.hops.hopsworks.exceptions.ApiKeyException 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)

Example 9 with ApiKeyException

use of io.hops.hopsworks.exceptions.ApiKeyException in project hopsworks by logicalclocks.

the class FlinkProxyServlet method service.

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws ServletException, IOException {
    Users user;
    if (servletRequest.getUserPrincipal() == null) {
        // Check if API key is provided
        String authorizationHeader = servletRequest.getHeader("Authorization");
        if (Strings.isNullOrEmpty(authorizationHeader)) {
            servletResponse.sendError(401, "API key was not provided");
            return;
        } else {
            try {
                String key = authorizationHeader.substring(ApiKeyFilter.API_KEY.length()).trim();
                ApiKey apiKey = apiKeyController.getApiKey(key);
                user = apiKey.getUser();
            } catch (ApiKeyException e) {
                servletResponse.sendError(401, "Could not validate API key");
                return;
            }
        }
    } else {
        user = userFacade.findByEmail(servletRequest.getUserPrincipal().getName());
    }
    String uri = servletRequest.getRequestURI();
    Pattern appPattern = Pattern.compile("(application_.*?_\\d*)");
    Matcher appMatcher = appPattern.matcher(uri);
    String appId;
    String flinkMasterURL;
    if (appMatcher.find()) {
        appId = appMatcher.group(1);
        // Validate user is authorized to access to this yarn app
        YarnApplicationstate appState = yarnApplicationstateFacade.findByAppId(appId);
        // If job is not running, show relevant message
        if (!Strings.isNullOrEmpty(appState.getAppsmstate()) && (YarnApplicationState.valueOf(appState.getAppsmstate()) == YarnApplicationState.FAILED || YarnApplicationState.valueOf(appState.getAppsmstate()) == YarnApplicationState.FINISHED || YarnApplicationState.valueOf(appState.getAppsmstate()) == YarnApplicationState.KILLED)) {
            servletResponse.sendError(404, "This Flink cluster is not running. You can navigate to YARN and Logs for historical information on this " + "Flink cluster.");
            return;
        }
        HdfsUsers hdfsUser = hdfsUsersFacade.findByName(appState.getAppuser());
        if (!projectTeamFacade.isUserMemberOfProject(projectFacade.findByName(hdfsUser.getProject()), user)) {
            servletResponse.sendError(403, "You are not authorized to access this Flink cluster");
        }
        // Is this user member of the project?
        flinkMasterURL = flinkMasterAddrCache.get(appId);
        if (Strings.isNullOrEmpty(flinkMasterURL)) {
            servletResponse.sendError(404, "This Flink cluster is not running. You can navigate to YARN and Logs for historical information on this" + " Flink cluster.");
            return;
        }
        String theHost = "http://" + flinkMasterURL;
        URI targetUriHost;
        targetUri = theHost;
        try {
            targetUriObj = new URI(targetUri);
            targetUriHost = new URI(theHost);
        } catch (Exception e) {
            LOGGER.log(Level.INFO, "An error occurred serving the request", e);
            return;
        }
        targetHost = URIUtils.extractHost(targetUriHost);
        servletRequest.setAttribute(ATTR_TARGET_URI, targetUri);
        servletRequest.setAttribute(ATTR_TARGET_HOST, targetHost);
        servletRequest.setAttribute(ATTR_HOST_PORT, flinkMasterURL);
        super.service(servletRequest, servletResponse);
    } else {
        servletResponse.sendError(404, "This Flink cluster is not running. You can navigate to YARN and Logs for historical information on this " + "Flink cluster.");
    }
}
Also used : ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) Pattern(java.util.regex.Pattern) ApiKey(io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey) Matcher(java.util.regex.Matcher) YarnApplicationstate(io.hops.hopsworks.persistence.entity.jobs.history.YarnApplicationstate) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) Users(io.hops.hopsworks.persistence.entity.user.Users) URI(java.net.URI) HdfsUsers(io.hops.hopsworks.persistence.entity.hdfs.user.HdfsUsers) ServletException(javax.servlet.ServletException) ApiKeyException(io.hops.hopsworks.exceptions.ApiKeyException) IOException(java.io.IOException)

Aggregations

ApiKeyException (io.hops.hopsworks.exceptions.ApiKeyException)9 ApiKey (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKey)5 Secret (io.hops.hopsworks.common.security.utils.Secret)3 ApiScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiScope)3 Users (io.hops.hopsworks.persistence.entity.user.Users)2 ApiKeyScope (io.hops.hopsworks.persistence.entity.user.security.apiKey.ApiKeyScope)2 IOException (java.io.IOException)2 Date (java.util.Date)2 ServiceDiscoveryException (com.logicalclocks.servicediscoverclient.exceptions.ServiceDiscoveryException)1 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 InferenceLogger (io.hops.hopsworks.common.serving.inference.logger.InferenceLogger)1 InferenceException (io.hops.hopsworks.exceptions.InferenceException)1 ServiceException (io.hops.hopsworks.exceptions.ServiceException)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 Serving (io.hops.hopsworks.persistence.entity.serving.Serving)1 JsonResponse (io.hops.hopsworks.restutils.JsonResponse)1