From 6827eac77a9c74bc88d18c2d88871c6078e5021f Mon Sep 17 00:00:00 2001 From: Brian Marroquin Date: Mon, 15 Apr 2024 21:58:44 -0700 Subject: [PATCH 1/2] adds httpPath variable for Bitbucket DC --- .../BitbucketHelperTest.cs | 39 +++++++++++++++++++ .../Atlassian.Bitbucket/BitbucketHelper.cs | 24 +++++++++++- .../DataCenter/BitbucketOAuth2Client.cs | 10 +---- .../DataCenter/BitbucketRestApi.cs | 8 +--- .../DataCenter/DataCenterConstants.cs | 2 + 5 files changed, 67 insertions(+), 16 deletions(-) diff --git a/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs b/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs index cad371431..2402eb53f 100644 --- a/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs +++ b/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs @@ -1,3 +1,7 @@ +using Atlassian.Bitbucket.DataCenter; +using GitCredentialManager; +using Moq; +using Newtonsoft.Json.Linq; using System; using Xunit; @@ -5,6 +9,10 @@ namespace Atlassian.Bitbucket.Tests { public class BitbucketHelperTest { + + + private Mock settings = new Mock(MockBehavior.Loose); + [Theory] [InlineData(null, false)] [InlineData("", false)] @@ -56,5 +64,36 @@ public void BitbucketHelper_IsBitbucketOrg_Uri(string str, bool expected) bool actual = BitbucketHelper.IsBitbucketOrg(new Uri(str)); Assert.Equal(expected, actual); } + + [Theory] + // old behavior + [InlineData("http://bitbucket.org", null, "http://bitbucket.org:80")] + [InlineData("https://bitbucket.org", null, "https://bitbucket.org:443")] + [InlineData("https://bitbucket.org/project/repo.git", null, "https://bitbucket.org:443/project")] + // with http path + [InlineData("http://bitbucket.org", "/bitbucket", "http://bitbucket.org:80/bitbucket")] + [InlineData("https://bitbucket.org", "/bitbucket", "https://bitbucket.org:443/bitbucket")] + // usehttppath takes preference over httpPath + [InlineData("https://bitbucket.org/project/repo.git", "/bitbucket", "https://bitbucket.org:443/project")] + public void BitbucketHelper_GetBaseUri(string uri, string httpPath, string expected) + { + + settings.Setup(s => s.RemoteUri).Returns(new Uri(uri)); + if(httpPath != null) + { + MockHttpPath(httpPath); + } + var actual = BitbucketHelper.GetBaseUri(settings.Object); + Assert.Equal(expected, actual); + } + + private string MockHttpPath(string value) + { + settings.Setup(s => s.TryGetSetting( + DataCenterConstants.EnvironmentVariables.HttpPath, + Constants.GitConfiguration.Credential.SectionName, DataCenterConstants.GitConfiguration.Credential.HttpPath, + out value)).Returns(true); + return value; + } } } \ No newline at end of file diff --git a/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs b/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs index 3624627f0..1765dbed9 100644 --- a/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs +++ b/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs @@ -1,17 +1,39 @@ using System; +using System.Net.NetworkInformation; using Atlassian.Bitbucket.Cloud; +using Atlassian.Bitbucket.DataCenter; using GitCredentialManager; namespace Atlassian.Bitbucket { public static class BitbucketHelper { - public static string GetBaseUri(Uri remoteUri) + + private static bool TryGetHttpPath(ISettings settings, out string httpPath) + { + return settings.TryGetSetting( + DataCenterConstants.EnvironmentVariables.HttpPath, + Constants.GitConfiguration.Credential.SectionName, DataCenterConstants.GitConfiguration.Credential.HttpPath, + out httpPath); + } + + public static string GetBaseUri(ISettings settings) { + var remoteUri = settings?.RemoteUri; + if (remoteUri == null) + { + throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC Rest/OAuth endpoints"); + } + var pathParts = remoteUri.PathAndQuery.Split('/'); var pathPart = remoteUri.PathAndQuery.StartsWith("/") ? pathParts[1] : pathParts[0]; var path = !string.IsNullOrWhiteSpace(pathPart) ? "/" + pathPart : null; + if(path == null && TryGetHttpPath(settings, out string httpPath) && !string.IsNullOrEmpty(httpPath)) + { + path = httpPath; + } return $"{remoteUri.Scheme}://{remoteUri.Host}:{remoteUri.Port}{path}"; + } public static bool IsBitbucketOrg(InputArguments input) diff --git a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs index 97abd533c..7da561dd4 100644 --- a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs +++ b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs @@ -68,15 +68,9 @@ private static string GetClientSecret(ISettings settings) private static OAuth2ServerEndpoints GetEndpoints(ISettings settings) { - var remoteUri = settings.RemoteUri; - if (remoteUri == null) - { - throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC OAuth2 endpoint Urls"); - } - return new OAuth2ServerEndpoints( - new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/oauth2/latest/authorize"), - new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/oauth2/latest/token") + new Uri(BitbucketHelper.GetBaseUri(settings) + "/rest/oauth2/latest/authorize"), + new Uri(BitbucketHelper.GetBaseUri(settings) + "/rest/oauth2/latest/token") ); } } diff --git a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketRestApi.cs b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketRestApi.cs index 159229885..19e8ad929 100644 --- a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketRestApi.cs +++ b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketRestApi.cs @@ -141,13 +141,7 @@ private Uri ApiUri { get { - var remoteUri = _context.Settings?.RemoteUri; - if (remoteUri == null) - { - throw new ArgumentException("RemoteUri must be defined to generate Bitbucket DC OAuth2 endpoint Urls"); - } - - return new Uri(BitbucketHelper.GetBaseUri(remoteUri) + "/rest/"); + return new Uri(BitbucketHelper.GetBaseUri(_context.Settings) + "/rest/"); } } } diff --git a/src/shared/Atlassian.Bitbucket/DataCenter/DataCenterConstants.cs b/src/shared/Atlassian.Bitbucket/DataCenter/DataCenterConstants.cs index 526db40c0..876bd765a 100644 --- a/src/shared/Atlassian.Bitbucket/DataCenter/DataCenterConstants.cs +++ b/src/shared/Atlassian.Bitbucket/DataCenter/DataCenterConstants.cs @@ -30,6 +30,7 @@ public static class EnvironmentVariables public const string OAuthClientId = "GCM_BITBUCKET_DATACENTER_CLIENTID"; public const string OAuthClientSecret = "GCM_BITBUCKET_DATACENTER_CLIENTSECRET"; public const string OAuthRedirectUri = "GCM_BITBUCKET_DATACENTER_OAUTH_REDIRECTURI"; + public const string HttpPath = "GCM_BITBUCKET_DATACENTER_HTTP_PATH"; } public static class GitConfiguration @@ -39,6 +40,7 @@ public static class Credential public const string OAuthClientId = "bitbucketDataCenterOAuthClientId"; public const string OAuthClientSecret = "bitbucketDataCenterOAuthClientSecret"; public const string OAuthRedirectUri = "bitbucketDataCenterOauthRedirectUri"; + public const string HttpPath = "bitbucketDataCenterHttpPath"; } } } From 13001e0d58f9471343178998f4b3d877157cc8a2 Mon Sep 17 00:00:00 2001 From: Brian Marroquin Date: Mon, 15 Apr 2024 22:17:54 -0700 Subject: [PATCH 2/2] removes unused imports --- src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs | 1 - src/shared/Atlassian.Bitbucket/BitbucketHelper.cs | 1 - .../Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs | 2 -- 3 files changed, 4 deletions(-) diff --git a/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs b/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs index 2402eb53f..b2bae24aa 100644 --- a/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs +++ b/src/shared/Atlassian.Bitbucket.Tests/BitbucketHelperTest.cs @@ -1,7 +1,6 @@ using Atlassian.Bitbucket.DataCenter; using GitCredentialManager; using Moq; -using Newtonsoft.Json.Linq; using System; using Xunit; diff --git a/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs b/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs index 1765dbed9..42defcfef 100644 --- a/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs +++ b/src/shared/Atlassian.Bitbucket/BitbucketHelper.cs @@ -1,5 +1,4 @@ using System; -using System.Net.NetworkInformation; using Atlassian.Bitbucket.Cloud; using Atlassian.Bitbucket.DataCenter; using GitCredentialManager; diff --git a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs index 7da561dd4..e764a7558 100644 --- a/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs +++ b/src/shared/Atlassian.Bitbucket/DataCenter/BitbucketOAuth2Client.cs @@ -3,8 +3,6 @@ using System; using System.Collections.Generic; using System.Net.Http; -using System.Threading; -using System.Threading.Tasks; using GitCredentialManager; using GitCredentialManager.Authentication.OAuth;