Search in sources :

Example 21 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project devspaces-images by redhat-developer.

the class KubernetesNamespaceFactoryTest method shouldReturnPreparedNamespacesWhenFound.

@Test
public void shouldReturnPreparedNamespacesWhenFound() throws InfrastructureException {
    // given
    List<Namespace> namespaces = Arrays.asList(new NamespaceBuilder().withNewMetadata().withName("ns1").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build(), new NamespaceBuilder().withNewMetadata().withName("ns2").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build(), new NamespaceBuilder().withNewMetadata().withName("ns3").withAnnotations(Map.of(NAMESPACE_ANNOTATION_NAME, "some_other_user")).endMetadata().withNewStatus().withNewPhase("Active").endStatus().build());
    doReturn(namespaces).when(namespaceList).getItems();
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
    // when
    List<KubernetesNamespaceMeta> availableNamespaces = namespaceFactory.list();
    // then
    assertEquals(availableNamespaces.size(), 2);
    verify(namespaceOperation).withLabels(Map.of(NAMESPACE_LABEL_NAME, "workspace"));
    assertEquals(availableNamespaces.get(0).getName(), "ns1");
    assertEquals(availableNamespaces.get(1).getName(), "ns2");
}
Also used : KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) SubjectImpl(org.eclipse.che.commons.subject.SubjectImpl) Namespace(io.fabric8.kubernetes.api.model.Namespace) NamespaceBuilder(io.fabric8.kubernetes.api.model.NamespaceBuilder) Test(org.testng.annotations.Test)

Example 22 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesPersonalAccessTokenManager method get.

@Override
public Optional<PersonalAccessToken> get(Subject cheUser, String scmServerUrl) throws ScmConfigurationPersistenceException, ScmUnauthorizedException, ScmCommunicationException {
    try {
        for (KubernetesNamespaceMeta namespaceMeta : namespaceFactory.list()) {
            List<Secret> secrets = namespaceFactory.access(null, namespaceMeta.getName()).secrets().get(KUBERNETES_PERSONAL_ACCESS_TOKEN_LABEL_SELECTOR);
            for (Secret secret : secrets) {
                Map<String, String> annotations = secret.getMetadata().getAnnotations();
                String trimmedUrl = StringUtils.trimEnd(annotations.get(ANNOTATION_SCM_URL), '/');
                if (annotations.get(ANNOTATION_CHE_USERID).equals(cheUser.getUserId()) && trimmedUrl.equals(StringUtils.trimEnd(scmServerUrl, '/'))) {
                    PersonalAccessToken token = new PersonalAccessToken(trimmedUrl, annotations.get(ANNOTATION_CHE_USERID), annotations.get(ANNOTATION_SCM_USERNAME), annotations.get(ANNOTATION_SCM_USERID), annotations.get(ANNOTATION_SCM_PERSONAL_ACCESS_TOKEN_NAME), annotations.get(ANNOTATION_SCM_PERSONAL_ACCESS_TOKEN_ID), new String(Base64.getDecoder().decode(secret.getData().get("token"))));
                    if (scmPersonalAccessTokenFetcher.isValid(token)) {
                        return Optional.of(token);
                    } else {
                        // Removing token that is no longer valid. If several tokens exist the next one could
                        // be valid. If no valid token can be found, the caller should react in the same way
                        // as it reacts if no token exists. Usually, that means that process of new token
                        // retrieval would be initiated.
                        clientFactory.create().secrets().inNamespace(namespaceMeta.getName()).delete(secret);
                    }
                }
            }
        }
    } catch (InfrastructureException | UnknownScmProviderException e) {
        throw new ScmConfigurationPersistenceException(e.getMessage(), e);
    }
    return Optional.empty();
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) UnknownScmProviderException(org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) ScmConfigurationPersistenceException(org.eclipse.che.api.factory.server.scm.exception.ScmConfigurationPersistenceException)

Example 23 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesGitCredentialManagerTest method testCreateAndSaveNewOAuthGitCredential.

@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()));
}
Also used : Secret(io.fabric8.kubernetes.api.model.Secret) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) PersonalAccessToken(org.eclipse.che.api.factory.server.scm.PersonalAccessToken) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) Test(org.testng.annotations.Test)

Example 24 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldHandleProvision.

@Test
public void shouldHandleProvision() throws InfrastructureException {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-che", false, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool));
    KubernetesNamespace toReturnNamespace = mock(KubernetesNamespace.class);
    prepareNamespace(toReturnNamespace);
    when(toReturnNamespace.getName()).thenReturn("jondoe-che");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    KubernetesNamespaceMetaImpl namespaceMeta = new KubernetesNamespaceMetaImpl("jondoe-che", ImmutableMap.of("phase", "active", "default", "true"));
    doReturn(Optional.of(namespaceMeta)).when(namespaceFactory).fetchNamespace(eq("jondoe-che"));
    // when
    NamespaceResolutionContext context = new NamespaceResolutionContext("workspace123", "user123", "jondoe");
    KubernetesNamespaceMeta actual = testProvisioning(context);
    // then
    assertEquals(actual.getName(), "jondoe-che");
    assertEquals(actual.getAttributes(), ImmutableMap.of("phase", "active", "default", "true"));
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) Test(org.testng.annotations.Test)

Example 25 with KubernetesNamespaceMeta

use of org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldReturnDefaultNamespaceWhenItDoesNotExistAndUserDefinedIsNotAllowed.

@Test
public void shouldReturnDefaultNamespaceWhenItDoesNotExistAndUserDefinedIsNotAllowed() throws Exception {
    prepareNamespaceToBeFoundByName("jondoe-che", null);
    namespaceFactory = new KubernetesNamespaceFactory("<username>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, emptySet(), clientFactory, cheClientFactory, userManager, preferenceManager, pool);
    List<KubernetesNamespaceMeta> availableNamespaces = namespaceFactory.list();
    assertEquals(availableNamespaces.size(), 1);
    KubernetesNamespaceMeta defaultNamespace = availableNamespaces.get(0);
    assertEquals(defaultNamespace.getName(), "jondoe-che");
    assertEquals(defaultNamespace.getAttributes().get(DEFAULT_ATTRIBUTE), "true");
    assertNull(defaultNamespace.getAttributes().get(// no phase - means such namespace does not exist
    PHASE_ATTRIBUTE));
}
Also used : KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) Test(org.testng.annotations.Test)

Aggregations

KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)42 Test (org.testng.annotations.Test)34 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)20 Secret (io.fabric8.kubernetes.api.model.Secret)18 PersonalAccessToken (org.eclipse.che.api.factory.server.scm.PersonalAccessToken)18 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)16 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)10 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)10 SecretBuilder (io.fabric8.kubernetes.api.model.SecretBuilder)10 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)8 KubernetesNamespace (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespace)8 KubernetesSecrets (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesSecrets)8 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)6 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)6 NamespaceResolutionContext (org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)6 Namespace (io.fabric8.kubernetes.api.model.Namespace)4 Status (io.fabric8.kubernetes.api.model.Status)4 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)4 Project (io.fabric8.openshift.api.model.Project)3 SecretList (io.fabric8.kubernetes.api.model.SecretList)2