Search in sources :

Example 6 with ScmUnauthorizedException

use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project devspaces-images by redhat-developer.

the class DevfileToApiExceptionMapperTest method shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized.

@Test
public void shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized() {
    ScmUnauthorizedException scmUnauthorizedException = new ScmUnauthorizedException("msg", "gitlab", "2.0", "http://gitlab.com/oauth/authenticate");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmUnauthorizedException));
    assertTrue(exception instanceof UnauthorizedException);
    assertEquals(((ExtendedError) exception.getServiceError()).getErrorCode(), 401);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().size(), 3);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_version"), "2.0");
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_authentication_url"), "http://gitlab.com/oauth/authenticate");
}
Also used : ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 7 with ScmUnauthorizedException

use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project che-server by eclipse-che.

the class GithubPersonalAccessTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    OAuthToken oAuthToken;
    if (githubApiClient == null || !githubApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    try {
        oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
        // Find the user associated to the OAuth token by querying the GitHub API.
        GithubUser user = githubApiClient.getUser(oAuthToken.getToken());
        PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getLogin(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
        Optional<Boolean> valid = isValid(token);
        if (valid.isEmpty()) {
            throw new ScmCommunicationException("Unable to verify if current token is a valid GitHub token.  Token's scm-url needs to be '" + GithubApiClient.GITHUB_SERVER + "' and was '" + token.getScmProviderUrl() + "'");
        } else if (!valid.get()) {
            throw new ScmCommunicationException("Current token doesn't have the necessary privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
        }
        return token;
    } catch (UnauthorizedException e) {
        throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
    } catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
        LOG.error(e.getMessage());
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)

Example 8 with ScmUnauthorizedException

use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project che-server by eclipse-che.

the class GitlabOAuthTokenFetcher method fetchPersonalAccessToken.

@Override
public PersonalAccessToken fetchPersonalAccessToken(Subject cheSubject, String scmServerUrl) throws ScmUnauthorizedException, ScmCommunicationException {
    scmServerUrl = StringUtils.trimEnd(scmServerUrl, '/');
    GitlabApiClient gitlabApiClient = getApiClient(scmServerUrl);
    if (gitlabApiClient == null || !gitlabApiClient.isConnected(scmServerUrl)) {
        LOG.debug("not a  valid url {} for current fetcher ", scmServerUrl);
        return null;
    }
    if (oAuthAPI == null) {
        throw new ScmCommunicationException(format("OAuth 2 is not configured for SCM provider [%s]. For details, refer " + "the documentation in section of SCM providers configuration.", OAUTH_PROVIDER_NAME));
    }
    OAuthToken oAuthToken;
    try {
        oAuthToken = oAuthAPI.getToken(OAUTH_PROVIDER_NAME);
        GitlabUser user = gitlabApiClient.getUser(oAuthToken.getToken());
        PersonalAccessToken token = new PersonalAccessToken(scmServerUrl, cheSubject.getUserId(), user.getUsername(), Long.toString(user.getId()), NameGenerator.generate(OAUTH_2_PREFIX, 5), NameGenerator.generate("id-", 5), oAuthToken.getToken());
        Optional<Boolean> valid = isValid(token);
        if (valid.isEmpty() || !valid.get()) {
            throw new ScmCommunicationException("Current token doesn't have the necessary  privileges. Please make sure Che app scopes are correct and containing at least: " + DEFAULT_TOKEN_SCOPES.toString());
        }
        return token;
    } catch (UnauthorizedException e) {
        throw new ScmUnauthorizedException(cheSubject.getUserName() + " is not authorized in " + OAUTH_PROVIDER_NAME + " OAuth provider.", OAUTH_PROVIDER_NAME, "2.0", getLocalAuthenticateUrl());
    } catch (NotFoundException | ServerException | ForbiddenException | BadRequestException | ScmItemNotFoundException | ScmBadRequestException | ConflictException e) {
        LOG.warn(e.getMessage());
        throw new ScmCommunicationException(e.getMessage(), e);
    }
}
Also used : ForbiddenException(org.eclipse.che.api.core.ForbiddenException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ServerException(org.eclipse.che.api.core.ServerException) ConflictException(org.eclipse.che.api.core.ConflictException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) NotFoundException(org.eclipse.che.api.core.NotFoundException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) OAuthToken(org.eclipse.che.api.auth.shared.dto.OAuthToken) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) BadRequestException(org.eclipse.che.api.core.BadRequestException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)

Example 9 with ScmUnauthorizedException

use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project che-server by eclipse-che.

the class DevfileToApiExceptionMapperTest method shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized.

@Test
public void shouldReturnUnauthorizedExceptionIfCauseIsScmUnauthorized() {
    ScmUnauthorizedException scmUnauthorizedException = new ScmUnauthorizedException("msg", "gitlab", "2.0", "http://gitlab.com/oauth/authenticate");
    ApiException exception = DevfileToApiExceptionMapper.toApiException(new DevfileException("text", scmUnauthorizedException));
    assertTrue(exception instanceof UnauthorizedException);
    assertEquals(((ExtendedError) exception.getServiceError()).getErrorCode(), 401);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().size(), 3);
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_version"), "2.0");
    assertEquals(((ExtendedError) exception.getServiceError()).getAttributes().get("oauth_authentication_url"), "http://gitlab.com/oauth/authenticate");
}
Also used : ExtendedError(org.eclipse.che.api.core.rest.shared.dto.ExtendedError) UnauthorizedException(org.eclipse.che.api.core.UnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ApiException(org.eclipse.che.api.core.ApiException) Test(org.testng.annotations.Test)

Example 10 with ScmUnauthorizedException

use of org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException in project che-server by eclipse-che.

the class AuthorizingFileContentProvider method fetchContent.

@Override
public String fetchContent(String fileURL) throws IOException, DevfileException {
    final String requestURL = formatUrl(fileURL);
    try {
        Optional<PersonalAccessToken> token = personalAccessTokenManager.get(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
        if (token.isPresent()) {
            PersonalAccessToken personalAccessToken = token.get();
            String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
            gitCredentialManager.createOrReplace(personalAccessToken);
            return content;
        } else {
            try {
                return urlFetcher.fetch(requestURL);
            } catch (IOException exception) {
                if (exception instanceof SSLException) {
                    ScmCommunicationException cause = new ScmCommunicationException(String.format("Failed to fetch a content from URL %s due to TLS key misconfiguration. Please refer to the docs about how to correctly import it. ", requestURL));
                    throw new DevfileException(exception.getMessage(), cause);
                } else if (exception instanceof FileNotFoundException) {
                    if (isPublicRepository(remoteFactoryUrl)) {
                        // for public repo-s return 404 as-is
                        throw exception;
                    }
                }
                // unable to determine exact cause, so let's just try to authorize...
                try {
                    PersonalAccessToken personalAccessToken = personalAccessTokenManager.fetchAndSave(EnvironmentContext.getCurrent().getSubject(), remoteFactoryUrl.getHostName());
                    String content = urlFetcher.fetch(requestURL, formatAuthorization(personalAccessToken.getToken()));
                    gitCredentialManager.createOrReplace(personalAccessToken);
                    return content;
                } catch (ScmUnauthorizedException | UnknownScmProviderException e) {
                    throw new DevfileException(e.getMessage(), e);
                } catch (ScmCommunicationException e) {
                    throw new IOException(String.format("Failed to fetch a content from URL %s. Make sure the URL" + " is correct. For private repository, make sure authentication is configured." + " Additionally, if you're using " + " relative form, make sure the referenced file are actually stored" + " relative to the devfile on the same host," + " or try to specify URL in absolute form. The current attempt to authenticate" + " request, failed with the following error message: %s", fileURL, e.getMessage()), e);
                }
            }
        }
    } catch (ScmConfigurationPersistenceException | UnsatisfiedScmPreconditionException | ScmUnauthorizedException | ScmCommunicationException e) {
        throw new DevfileException(e.getMessage(), e);
    }
}
Also used : ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) SSLException(javax.net.ssl.SSLException) DevfileException(org.eclipse.che.api.workspace.server.devfile.exception.DevfileException) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) UnsatisfiedScmPreconditionException(org.eclipse.che.api.factory.server.scm.exception.UnsatisfiedScmPreconditionException) ScmConfigurationPersistenceException(org.eclipse.che.api.factory.server.scm.exception.ScmConfigurationPersistenceException)

Aggregations

ScmUnauthorizedException (org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException)12 ScmCommunicationException (org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException)10 ScmBadRequestException (org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)8 ScmItemNotFoundException (org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException)8 UnauthorizedException (org.eclipse.che.api.core.UnauthorizedException)6 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)6 OAuthToken (org.eclipse.che.api.auth.shared.dto.OAuthToken)4 BadRequestException (org.eclipse.che.api.core.BadRequestException)4 ConflictException (org.eclipse.che.api.core.ConflictException)4 ForbiddenException (org.eclipse.che.api.core.ForbiddenException)4 NotFoundException (org.eclipse.che.api.core.NotFoundException)4 ServerException (org.eclipse.che.api.core.ServerException)4 IOException (java.io.IOException)3 List (java.util.List)3 Optional (java.util.Optional)3 Set (java.util.Set)3 Collectors (java.util.stream.Collectors)3 DevfileException (org.eclipse.che.api.workspace.server.devfile.exception.DevfileException)3 EnvironmentContext (org.eclipse.che.commons.env.EnvironmentContext)3 Subject (org.eclipse.che.commons.subject.Subject)3