From 5c2963a3d0591d2c462ef143c6a518a314dff3a3 Mon Sep 17 00:00:00 2001 From: ivinokur Date: Thu, 23 Jan 2025 17:05:14 +0200 Subject: [PATCH] Do not validate Bitbucket Server by Api request --- .../bitbucket/BitbucketServerURLParser.java | 28 ++----------------- .../BitbucketServerURLParserTest.java | 20 +------------ 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/wsmaster/che-core-api-factory-bitbucket-server/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParser.java b/wsmaster/che-core-api-factory-bitbucket-server/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParser.java index 93e84f6c79..b9fc09dd13 100644 --- a/wsmaster/che-core-api-factory-bitbucket-server/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParser.java +++ b/wsmaster/che-core-api-factory-bitbucket-server/src/main/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParser.java @@ -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/ @@ -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. @@ -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); diff --git a/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParserTest.java b/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParserTest.java index c8598467fa..255d3a030a 100644 --- a/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParserTest.java +++ b/wsmaster/che-core-api-factory-bitbucket-server/src/test/java/org/eclipse/che/api/factory/server/bitbucket/BitbucketServerURLParserTest.java @@ -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/ @@ -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