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");
}
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");
}
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);
}
}
}
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);
}
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");
}
Aggregations