Skip to content

Commit b98babb

Browse files
authored
Move bitbucket name check to GitCredentialsManager (#825) (#827)
Signed-off-by: Ihor Vinokur <[email protected]>
1 parent ecb06f2 commit b98babb

File tree

5 files changed

+46
-46
lines changed

5 files changed

+46
-46
lines changed

assembly/assembly-wsmaster-war/src/main/webapp/WEB-INF/classes/che/che.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#
2-
# Copyright (c) 2012-2024 Red Hat, Inc.
2+
# Copyright (c) 2012-2025 Red Hat, Inc.
33
# This program and the accompanying materials are made
44
# available under the terms of the Eclipse Public License 2.0
55
# which is available at https://www.eclipse.org/legal/epl-2.0/

infrastructures/infrastructure-factory/src/main/java/org/eclipse/che/api/factory/server/scm/kubernetes/KubernetesGitCredentialManager.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012-2023 Red Hat, Inc.
2+
* Copyright (c) 2012-2025 Red Hat, Inc.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -177,6 +177,11 @@ private String getUsernameSegment(PersonalAccessToken personalAccessToken) {
177177
// Special characters are not allowed in URL username segment, so we need to escape them.
178178
PercentEscaper percentEscaper = new PercentEscaper("", false);
179179
return personalAccessToken.getScmTokenName().startsWith(OAUTH_2_PREFIX)
180+
// Most of the git providers work with git credentials with OAuth token in format
181+
// "ouath2:<oauth token>"
182+
// but bitbucket requires username to be explicitly set: "<username>:<oauth token>
183+
// TODO: needs to be moved to the specific bitbucket implementation.
184+
&& !personalAccessToken.getScmProviderName().equals("bitbucket")
180185
? "oauth2"
181186
: isNullOrEmpty(personalAccessToken.getScmOrganization())
182187
? percentEscaper.escape(personalAccessToken.getScmUserName())

infrastructures/infrastructure-factory/src/test/java/org/eclipse/che/api/factory/server/scm/kubernetes/KubernetesGitCredentialManagerTest.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012-2024 Red Hat, Inc.
2+
* Copyright (c) 2012-2025 Red Hat, Inc.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -200,6 +200,42 @@ public void testCreateAndSaveNewOAuthGitCredential() throws Exception {
200200
assertFalse(createdSecret.getMetadata().getName().contains(token.getScmUserName()));
201201
}
202202

203+
@Test
204+
public void testCreateAndSaveNewBitbucketOAuthGitCredential() throws Exception {
205+
KubernetesNamespaceMeta meta = new KubernetesNamespaceMetaImpl("test");
206+
when(namespaceFactory.list()).thenReturn(Collections.singletonList(meta));
207+
208+
when(cheServerKubernetesClientFactory.create()).thenReturn(kubeClient);
209+
when(kubeClient.secrets()).thenReturn(secretsMixedOperation);
210+
when(secretsMixedOperation.inNamespace(eq(meta.getName()))).thenReturn(nonNamespaceOperation);
211+
when(nonNamespaceOperation.withLabels(anyMap())).thenReturn(filterWatchDeletable);
212+
when(filterWatchDeletable.list()).thenReturn(secretList);
213+
when(secretList.getItems()).thenReturn(emptyList());
214+
ArgumentCaptor<Secret> captor = ArgumentCaptor.forClass(Secret.class);
215+
216+
PersonalAccessToken token =
217+
new PersonalAccessToken(
218+
"https://bitbucket.com",
219+
"bitbucket",
220+
"cheUser",
221+
"username",
222+
"oauth2-token-name",
223+
"tid-23434",
224+
"token123");
225+
226+
// when
227+
kubernetesGitCredentialManager.createOrReplace(token);
228+
// then
229+
verify(nonNamespaceOperation).createOrReplace(captor.capture());
230+
Secret createdSecret = captor.getValue();
231+
assertNotNull(createdSecret);
232+
assertEquals(
233+
new String(Base64.getDecoder().decode(createdSecret.getData().get("credentials"))),
234+
"https://username:[email protected]");
235+
assertTrue(createdSecret.getMetadata().getName().startsWith(NAME_PATTERN));
236+
assertFalse(createdSecret.getMetadata().getName().contains(token.getScmUserName()));
237+
}
238+
203239
@Test
204240
public void testUpdateTokenInExistingCredential() throws Exception {
205241
KubernetesNamespaceMeta namespaceMeta = new KubernetesNamespaceMetaImpl("test");

wsmaster/che-core-api-auth/src/main/java/org/eclipse/che/security/oauth/EmbeddedOAuthAPI.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Response callback(UriInfo uriInfo, @Nullable List<String> errorValues)
114114
EnvironmentContext.getCurrent().getSubject().getUserId(),
115115
null,
116116
null,
117-
generateTokenName(providerName),
117+
NameGenerator.generate(OAUTH_2_PREFIX, 5),
118118
NameGenerator.generate("id-", 5),
119119
token));
120120
} catch (OAuthAuthenticationException e) {
@@ -161,18 +161,6 @@ public static String getRedirectAfterLoginUrl(
161161
return redirectAfterLogin;
162162
}
163163

164-
/*
165-
* This value is used for generating git credentials. Most of the git providers work with git
166-
* credentials with OAuth token in format "ouath2:<oauth token>" but bitbucket requires username
167-
* to be explicitly set: "<username>:<oauth token>, see {@link
168-
* GitCredentialManager#createOrReplace}
169-
* TODO: needs to be moved to the specific bitbucket implementation.
170-
*/
171-
private String generateTokenName(String providerName) {
172-
return NameGenerator.generate(
173-
"bitbucket".equals(providerName) ? providerName + "-" : OAUTH_2_PREFIX, 5);
174-
}
175-
176164
/**
177165
* Encode the redirect URL query parameters to avoid the error when the redirect URL contains
178166
* JSON, as a query parameter. This prevents passing unsupported characters, like '{' and '}' to

wsmaster/che-core-api-auth/src/test/java/org/eclipse/che/security/oauth/EmbeddedOAuthAPITest.java

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012-2024 Red Hat, Inc.
2+
* Copyright (c) 2012-2025 Red Hat, Inc.
33
* This program and the accompanying materials are made
44
* available under the terms of the Eclipse Public License 2.0
55
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -170,35 +170,6 @@ public void shouldStoreTokenOnCallback() throws Exception {
170170
assertEquals(token.getToken(), "token");
171171
}
172172

173-
@Test
174-
public void shouldStoreBitbucketTokenOnCallback() throws Exception {
175-
// given
176-
UriInfo uriInfo = mock(UriInfo.class);
177-
OAuthAuthenticator authenticator = mock(OAuthAuthenticator.class);
178-
when(authenticator.getEndpointUrl()).thenReturn("http://eclipse.che");
179-
when(authenticator.callback(any(URL.class), anyList())).thenReturn("token");
180-
when(uriInfo.getRequestUri())
181-
.thenReturn(
182-
new URI(
183-
"http://eclipse.che?state=oauth_provider%3Dbitbucket%26redirect_after_login%3DredirectUrl"));
184-
when(oauth2Providers.getAuthenticator("bitbucket")).thenReturn(authenticator);
185-
ArgumentCaptor<PersonalAccessToken> tokenCapture =
186-
ArgumentCaptor.forClass(PersonalAccessToken.class);
187-
188-
// when
189-
embeddedOAuthAPI.callback(uriInfo, emptyList());
190-
191-
// then
192-
verify(personalAccessTokenManager).store(tokenCapture.capture());
193-
PersonalAccessToken token = tokenCapture.getValue();
194-
assertEquals(token.getScmProviderUrl(), "http://eclipse.che");
195-
assertEquals(token.getScmProviderName(), "bitbucket");
196-
assertEquals(token.getCheUserId(), "0000-00-0000");
197-
assertTrue(token.getScmTokenId().startsWith("id-"));
198-
assertTrue(token.getScmTokenName().startsWith("bitbucket-"));
199-
assertEquals(token.getToken(), "token");
200-
}
201-
202173
@Test
203174
public void shouldEncodeRedirectUrl() throws Exception {
204175
// given

0 commit comments

Comments
 (0)