Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not validate Bitbucket Server by Api request #757

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2024 Red Hat, Inc.
* Copyright (c) 2012-2025 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -30,14 +30,11 @@
import org.eclipse.che.api.factory.server.scm.PersonalAccessTokenManager;
import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException;
import org.eclipse.che.api.factory.server.scm.exception.ScmConfigurationPersistenceException;
import org.eclipse.che.api.factory.server.scm.exception.ScmItemNotFoundException;
import org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException;
import org.eclipse.che.api.factory.server.urlfactory.DevfileFilenamesProvider;
import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.lang.StringUtils;
import org.eclipse.che.security.oauth.OAuthAPI;
import org.eclipse.che.security.oauth1.BitbucketServerOAuthAuthenticator;

/**
* Parser of String Bitbucket Server URLs and provide {@link BitbucketServerUrl} objects.
Expand Down Expand Up @@ -112,31 +109,10 @@ public boolean isValid(@NotNull String url) {
return
// If Bitbucket server URL is not configured try to find it in a manually added user namespace
// token.
isUserTokenPresent(url)
// Try to call an API request to see if the URL matches Bitbucket.
|| isApiRequestRelevant(url);
isUserTokenPresent(url);
}
}

private boolean isApiRequestRelevant(String repositoryUrl) {
try {
HttpBitbucketServerApiClient bitbucketServerApiClient =
new HttpBitbucketServerApiClient(
getServerUrl(repositoryUrl),
new BitbucketServerOAuthAuthenticator("", "", "", ""),
oAuthAPI,
"");
// If the user request catches the unauthorised error, it means that the provided url
// belongs to Bitbucket.
bitbucketServerApiClient.getUser();
} catch (ScmItemNotFoundException | ScmCommunicationException e) {
return false;
} catch (ScmUnauthorizedException e) {
return true;
}
return false;
}

private String getServerUrl(String repositoryUrl) {
if (repositoryUrl.startsWith("ssh://git@")) {
String substring = repositoryUrl.substring(10);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012-2024 Red Hat, Inc.
* Copyright (c) 2012-2025 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
Expand Down Expand Up @@ -109,24 +109,6 @@ public void shouldThrowExceptionWhenURLDintMatchAnyConfiguredServer() {
bitbucketURLParser.parse("https://github.com/org/repo");
}

@Test
public void shouldValidateUrlByApiRequest() {
// given
bitbucketURLParser =
new BitbucketServerURLParser(
null, devfileFilenamesProvider, oAuthAPI, mock(PersonalAccessTokenManager.class));
String url = wireMockServer.url("/users/user/repos/repo");
stubFor(
get(urlEqualTo("/plugins/servlet/applinks/whoami"))
.willReturn(aResponse().withStatus(401)));

// when
boolean result = bitbucketURLParser.isValid(url);

// then
assertTrue(result);
}

@Test
public void shouldNotValidateUrlByApiRequest() {
// given
Expand Down
Loading