Skip to content

Commit

Permalink
Do not validate Bitbucket Server by Api request
Browse files Browse the repository at this point in the history
  • Loading branch information
vinokurig committed Jan 23, 2025
1 parent e3c3c88 commit 5c2963a
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 45 deletions.
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

0 comments on commit 5c2963a

Please sign in to comment.