Search in sources :

Example 11 with ScmUnauthorizedException

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

the class HttpBitbucketServerApiClient method getUser.

@Override
public BitbucketUser getUser(Subject cheUser) throws ScmUnauthorizedException, ScmCommunicationException, ScmItemNotFoundException {
    try {
        // Since Bitbucket server API doesn't provide a way to get an account profile currently
        // authenticated user we will try to find it and by iterating over the list available to the
        // current user Bitbucket users and attempting to get their personal access tokens. To speed
        // up this process first of all we will search among users that contain(somewhere in Bitbucket
        // user
        // entity) Che's user username. At the second step, we will search against all visible(to the
        // current Che's user) bitbucket users that are not included in the first list.
        Set<String> usersByName = getUsers(cheUser.getUserName()).stream().map(BitbucketUser::getSlug).collect(Collectors.toSet());
        Optional<BitbucketUser> currentUser = findCurrentUser(usersByName);
        if (currentUser.isPresent()) {
            return currentUser.get();
        }
        Set<String> usersAllExceptByName = getUsers().stream().map(BitbucketUser::getSlug).filter(s -> !usersByName.contains(s)).collect(Collectors.toSet());
        currentUser = findCurrentUser(usersAllExceptByName);
        if (currentUser.isPresent()) {
            return currentUser.get();
        }
    } catch (ScmBadRequestException | ScmItemNotFoundException scmException) {
        throw new ScmCommunicationException(scmException.getMessage(), scmException);
    }
    throw new ScmItemNotFoundException("Current user not found. That is possible only if user are not authorized against " + serverUri);
}
Also used : ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) HTTP_BAD_REQUEST(java.net.HttpURLConnection.HTTP_BAD_REQUEST) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) LoggerFactory(org.slf4j.LoggerFactory) Duration.ofSeconds(java.time.Duration.ofSeconds) Function(java.util.function.Function) OAuthAuthenticator(org.eclipse.che.security.oauth1.OAuthAuthenticator) HttpRequest(java.net.http.HttpRequest) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) EnvironmentContext(org.eclipse.che.commons.env.EnvironmentContext) HttpHeaders(com.google.common.net.HttpHeaders) TypeFactory(com.fasterxml.jackson.databind.type.TypeFactory) CharStreams(com.google.common.io.CharStreams) Subject(org.eclipse.che.commons.subject.Subject) Duration(java.time.Duration) HttpClient(java.net.http.HttpClient) JavaType(com.fasterxml.jackson.databind.JavaType) URI(java.net.URI) LoggingUncaughtExceptionHandler(org.eclipse.che.commons.lang.concurrent.LoggingUncaughtExceptionHandler) HttpResponse(java.net.http.HttpResponse) Charsets(com.google.common.base.Charsets) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException) Logger(org.slf4j.Logger) HTTP_UNAUTHORIZED(java.net.HttpURLConnection.HTTP_UNAUTHORIZED) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) Set(java.util.Set) OAuthAuthenticationException(org.eclipse.che.security.oauth1.OAuthAuthenticationException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ScmUnauthorizedException(org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) MediaType(jakarta.ws.rs.core.MediaType) Optional(java.util.Optional) HTTP_NOT_FOUND(java.net.HttpURLConnection.HTTP_NOT_FOUND) InputStream(java.io.InputStream) ScmItemNotFoundException(org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException) ScmCommunicationException(org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException) ScmBadRequestException(org.eclipse.che.api.factory.server.scm.exception.ScmBadRequestException)

Example 12 with ScmUnauthorizedException

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

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