Fixup for oauth2 usernames

Signed-off-by: Max Shaposhnik <mshaposh@redhat.com>
pull/189/head
Max Shaposhnik 2021-12-01 19:48:15 +02:00 committed by GitHub
parent d38d4391c1
commit 6a7c640b9c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 2 deletions

View File

@ -13,6 +13,7 @@ package org.eclipse.che.api.factory.server.scm.kubernetes;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.eclipse.che.api.factory.server.scm.PersonalAccessTokenFetcher.OAUTH_2_PREFIX;
import static org.eclipse.che.workspace.infrastructure.kubernetes.provision.secret.KubernetesSecretAnnotationNames.ANNOTATION_AUTOMOUNT;
import static org.eclipse.che.workspace.infrastructure.kubernetes.provision.secret.KubernetesSecretAnnotationNames.ANNOTATION_DEV_WORKSPACE_MOUNT_PATH;
import static org.eclipse.che.workspace.infrastructure.kubernetes.provision.secret.KubernetesSecretAnnotationNames.ANNOTATION_GIT_CREDENTIALS;
@ -150,7 +151,9 @@ public class KubernetesGitCredentialManager implements GitCredentialManager {
format(
"%s://%s:%s@%s%s",
scmUrl.getProtocol(),
personalAccessToken.getScmUserName(),
personalAccessToken.getScmTokenName().startsWith(OAUTH_2_PREFIX)
? "oauth2"
: personalAccessToken.getScmUserName(),
URLEncoder.encode(personalAccessToken.getToken(), UTF_8),
scmUrl.getHost(),
scmUrl.getPort() != 80 && scmUrl.getPort() != -1

View File

@ -79,7 +79,7 @@ public class KubernetesGitCredentialManagerTest {
}
@Test
public void testCreateAndSaveNewGitCredential() throws Exception {
public void testCreateAndSaveNewPATGitCredential() throws Exception {
KubernetesNamespaceMeta meta = new KubernetesNamespaceMetaImpl("test");
when(namespaceFactory.list()).thenReturn(Collections.singletonList(meta));
@ -114,6 +114,42 @@ public class KubernetesGitCredentialManagerTest {
assertFalse(createdSecret.getMetadata().getName().contains(token.getScmUserName()));
}
@Test
public void testCreateAndSaveNewOAuthGitCredential() throws Exception {
KubernetesNamespaceMeta meta = new KubernetesNamespaceMetaImpl("test");
when(namespaceFactory.list()).thenReturn(Collections.singletonList(meta));
when(clientFactory.create()).thenReturn(kubeClient);
when(kubeClient.secrets()).thenReturn(secretsMixedOperation);
when(secretsMixedOperation.inNamespace(eq(meta.getName()))).thenReturn(nonNamespaceOperation);
when(nonNamespaceOperation.withLabels(anyMap())).thenReturn(filterWatchDeletable);
when(filterWatchDeletable.list()).thenReturn(secretList);
when(secretList.getItems()).thenReturn(emptyList());
ArgumentCaptor<Secret> captor = ArgumentCaptor.forClass(Secret.class);
PersonalAccessToken token =
new PersonalAccessToken(
"https://bitbucket.com",
"cheUser",
"username",
"userId",
"oauth2-token-name",
"tid-23434",
"token123");
// when
kubernetesGitCredentialManager.createOrReplace(token);
// then
verify(nonNamespaceOperation).createOrReplace(captor.capture());
Secret createdSecret = captor.getValue();
assertNotNull(createdSecret);
assertEquals(
new String(Base64.getDecoder().decode(createdSecret.getData().get("credentials"))),
"https://oauth2:token123@bitbucket.com");
assertTrue(createdSecret.getMetadata().getName().startsWith(NAME_PATTERN));
assertFalse(createdSecret.getMetadata().getName().contains(token.getScmUserName()));
}
@Test
public void testUpdateTokenInExistingCredential() throws Exception {
KubernetesNamespaceMeta namespaceMeta = new KubernetesNamespaceMetaImpl("test");