Search in sources :

Example 41 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldFailToProvisionIfNotAbleToFindNamespace.

@Test(expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Not able to find namespace jondoe-cha-cha-cha")
public void shouldFailToProvisionIfNotAbleToFindNamespace() throws InfrastructureException {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-cha-cha-cha", 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-cha-cha-cha");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    KubernetesNamespaceMetaImpl namespaceMeta = new KubernetesNamespaceMetaImpl("jondoe-cha-cha-cha", ImmutableMap.of("phase", "active", "default", "true"));
    doReturn(empty()).when(namespaceFactory).fetchNamespace(eq("jondoe-cha-cha-cha"));
    // when
    NamespaceResolutionContext context = new NamespaceResolutionContext("workspace123", "user123", "jondoe");
    testProvisioning(context);
    // then
    fail("should not reach this point since exception has to be thrown");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) Test(org.testng.annotations.Test)

Example 42 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class KubernetesNamespaceFactoryTest method shouldFail2ProvisionIfNotAbleToFindNamespace.

@Test(expectedExceptions = InfrastructureException.class, expectedExceptionsMessageRegExp = "Error occurred when tried to fetch default namespace")
public void shouldFail2ProvisionIfNotAbleToFindNamespace() throws InfrastructureException {
    // given
    namespaceFactory = spy(new KubernetesNamespaceFactory("<username>-cha-cha-cha", 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-cha-cha-cha");
    doReturn(toReturnNamespace).when(namespaceFactory).doCreateNamespaceAccess(any(), any());
    KubernetesNamespaceMetaImpl namespaceMeta = new KubernetesNamespaceMetaImpl("jondoe-cha-cha-cha", ImmutableMap.of("phase", "active", "default", "true"));
    doThrow(new InfrastructureException("Error occurred when tried to fetch default namespace")).when(namespaceFactory).fetchNamespace(eq("jondoe-cha-cha-cha"));
    // when
    NamespaceResolutionContext context = new NamespaceResolutionContext("workspace123", "user123", "jondoe");
    testProvisioning(context);
    // then
    fail("should not reach this point since exception has to be thrown");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) KubernetesNamespaceMetaImpl(org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException) Test(org.testng.annotations.Test)

Example 43 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class KubernetesNamespaceFactory method checkIfNamespaceIsAllowed.

/**
 * Checks if the current user is able to use the specified namespace for their new workspaces.
 *
 * @param namespaceName namespace name to check
 * @throws ValidationException if the specified namespace is not permitted for the current user
 */
public void checkIfNamespaceIsAllowed(String namespaceName) throws ValidationException {
    NamespaceResolutionContext context = new NamespaceResolutionContext(EnvironmentContext.getCurrent().getSubject());
    final String defaultNamespace = findStoredNamespace(context).orElse(evalPlaceholders(defaultNamespaceName, context));
    if (!namespaceName.equals(defaultNamespace)) {
        try {
            List<KubernetesNamespaceMeta> labeledNamespaces = findPreparedNamespaces(context);
            if (labeledNamespaces.stream().noneMatch(n -> n.getName().equals(namespaceName))) {
                throw new ValidationException(format("User defined namespaces are not allowed. Only the default namespace '%s' is available.", defaultNamespace));
            }
        } catch (InfrastructureException e) {
            throw new ValidationException("Some infrastructure failure caused failed validation.", e);
        }
    }
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) ValidationException(org.eclipse.che.api.core.ValidationException) KubernetesNamespaceMeta(org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 44 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class KubernetesNamespaceFactory method canCreateNamespace.

/**
 * Tells the caller whether the namespace that is being prepared for the provided workspace
 * runtime identity can be created or is expected to already be present.
 *
 * <p>Note that this method cannot be reduced to merely checking if user-defined namespaces are
 * allowed or not (and depending on prior validation using the {@link
 * #checkIfNamespaceIsAllowed(String)} method during the workspace creation) because workspace
 * start is a) async from workspace creation and the underlying namespaces might have disappeared
 * and b) can be called during workspace recovery, where we don't even have the current user in
 * the context.
 *
 * @param identity the identity of the workspace runtime
 * @return true if the namespace can be created, false if the namespace is expected to already
 *     exist
 * @throws InfrastructureException on failure
 */
protected boolean canCreateNamespace(RuntimeIdentity identity) throws InfrastructureException {
    if (!namespaceCreationAllowed) {
        return false;
    }
    // we need to make sure that the provided namespace is indeed the one provided by our
    // configuration
    User owner;
    try {
        owner = userManager.getById(identity.getOwnerId());
    } catch (NotFoundException | ServerException e) {
        throw new InfrastructureException("Failed to resolve workspace owner. Cause: " + e.getMessage(), e);
    }
    String requiredNamespace = identity.getInfrastructureNamespace();
    NamespaceResolutionContext resolutionContext = new NamespaceResolutionContext(identity.getWorkspaceId(), identity.getOwnerId(), owner.getName());
    String resolvedDefaultNamespace = evaluateNamespaceName(resolutionContext);
    return resolvedDefaultNamespace.equals(requiredNamespace);
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) User(org.eclipse.che.api.core.model.user.User) ServerException(org.eclipse.che.api.core.ServerException) NotFoundException(org.eclipse.che.api.core.NotFoundException) InfrastructureException(org.eclipse.che.api.workspace.server.spi.InfrastructureException)

Example 45 with NamespaceResolutionContext

use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.

the class OpenShiftProjectFactoryTest method testEvalNamespaceNameWhenPreparedNamespacesFound.

@Test
public void testEvalNamespaceNameWhenPreparedNamespacesFound() throws InfrastructureException {
    List<Project> projects = Arrays.asList(createProject("ns1", "project1", "desc1", "Active", Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")), createProject("ns3", "project3", "desc3", "Active", Map.of(NAMESPACE_ANNOTATION_NAME, "some_other_user")), createProject("ns2", "project2", "desc2", "Active", Map.of(NAMESPACE_ANNOTATION_NAME, "jondoe")));
    doReturn(projects).when(projectList).getItems();
    projectFactory = new OpenShiftProjectFactory("<userid>-che", true, true, true, NAMESPACE_LABELS, NAMESPACE_ANNOTATIONS, true, emptySet(), clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER);
    String namespace = projectFactory.evaluateNamespaceName(new NamespaceResolutionContext("workspace123", "user123", "jondoe"));
    assertEquals(namespace, "ns1");
}
Also used : NamespaceResolutionContext(org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext) Project(io.fabric8.openshift.api.model.Project) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.testng.annotations.Test)

Aggregations

NamespaceResolutionContext (org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext)56 Test (org.testng.annotations.Test)30 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 KubernetesServer (io.fabric8.kubernetes.client.server.mock.KubernetesServer)14 BeforeMethod (org.testng.annotations.BeforeMethod)14 HashMap (java.util.HashMap)8 KubernetesNamespaceMetaImpl (org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl)8 WorkspaceImpl (org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl)6 InfrastructureException (org.eclipse.che.api.workspace.server.spi.InfrastructureException)6 KubernetesNamespaceMeta (org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta)6 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)4 RuntimeIdentity (org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity)4 UserImpl (org.eclipse.che.api.user.server.model.impl.UserImpl)4 RuntimeIdentityImpl (org.eclipse.che.api.workspace.server.model.impl.RuntimeIdentityImpl)4 DevfileImpl (org.eclipse.che.api.workspace.server.model.impl.devfile.DevfileImpl)4 SubjectImpl (org.eclipse.che.commons.subject.SubjectImpl)4 NamespaceConfigurator (org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator)4 Namespace (io.fabric8.kubernetes.api.model.Namespace)2 NamespaceBuilder (io.fabric8.kubernetes.api.model.NamespaceBuilder)2 Project (io.fabric8.openshift.api.model.Project)2