use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.
the class OpenShiftProjectFactoryTest method testAllConfiguratorsAreCalledWhenCreatingProject.
@Test
public void testAllConfiguratorsAreCalledWhenCreatingProject() throws InfrastructureException {
// given
String projectName = "testprojectname";
NamespaceConfigurator configurator1 = Mockito.mock(NamespaceConfigurator.class);
NamespaceConfigurator configurator2 = Mockito.mock(NamespaceConfigurator.class);
Set<NamespaceConfigurator> namespaceConfigurators = Set.of(configurator1, configurator2);
projectFactory = spy(new OpenShiftProjectFactory("<username>-che", true, true, true, NAMESPACE_LABELS, "try_placeholder_here=<username>", true, namespaceConfigurators, clientFactory, cheClientFactory, cheServerOpenshiftClientFactory, userManager, preferenceManager, pool, NO_OAUTH_IDENTITY_PROVIDER));
EnvironmentContext.getCurrent().setSubject(new SubjectImpl("jondoe", "123", null, false));
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
when(toReturnProject.getName()).thenReturn(projectName);
RuntimeIdentity identity = new RuntimeIdentityImpl("workspace123", null, USER_ID, "old-che");
doReturn(toReturnProject).when(projectFactory).get(identity);
// when
OpenShiftProject project = projectFactory.getOrCreate(identity);
// then
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext("workspace123", "123", "jondoe");
verify(configurator1).configure(resolutionCtx, projectName);
verify(configurator2).configure(resolutionCtx, projectName);
assertEquals(project, toReturnProject);
}
use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.
the class OpenShiftProjectFactory method getOrCreate.
public OpenShiftProject getOrCreate(RuntimeIdentity identity) throws InfrastructureException {
OpenShiftProject osProject = get(identity);
var subject = EnvironmentContext.getCurrent().getSubject();
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(identity.getWorkspaceId(), subject.getUserId(), subject.getUserName());
Map<String, String> namespaceAnnotationsEvaluated = evaluateAnnotationPlaceholders(resolutionCtx);
osProject.prepare(canCreateNamespace(identity), initWithCheServerSa && !isNullOrEmpty(oAuthIdentityProvider), labelNamespaces ? namespaceLabels : emptyMap(), annotateNamespaces ? namespaceAnnotationsEvaluated : emptyMap());
configureNamespace(resolutionCtx, osProject.getName());
return osProject;
}
use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.
the class KubernetesNamespaceFactory method list.
/**
* Returns list of k8s namespaces names where a user is able to run workspaces.
*/
public List<KubernetesNamespaceMeta> list() throws InfrastructureException {
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(EnvironmentContext.getCurrent().getSubject());
List<KubernetesNamespaceMeta> labeledNamespaces = findPreparedNamespaces(resolutionCtx);
if (!labeledNamespaces.isEmpty()) {
return labeledNamespaces;
} else {
return singletonList(getDefaultNamespace(resolutionCtx));
}
}
use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.
the class KubernetesNamespaceFactory method getNamespaceName.
/**
* Returns a namespace name where workspace is assigned to.
*/
protected String getNamespaceName(Workspace workspace) throws InfrastructureException {
String namespace = workspace.getAttributes().get(WORKSPACE_INFRASTRUCTURE_NAMESPACE_ATTRIBUTE);
if (namespace == null) {
// it seems to be legacy workspace since the namespace is not stored in workspace attributes
// it's needed to evaluate that with current user and workspace id
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(workspace.getId(), EnvironmentContext.getCurrent().getSubject().getUserId(), EnvironmentContext.getCurrent().getSubject().getUserName());
namespace = evaluateNamespaceName(resolutionCtx);
LOG.warn("Workspace '{}' doesn't have an explicit namespace assigned." + " The legacy namespace resolution resolved it to '{}'.", workspace.getId(), namespace);
}
if (!NamespaceNameValidator.isValid(namespace)) {
// At a certain unfortunate past version of Che, we stored invalid namespace names.
// At this point in time, we're trying to work with an existing workspace that never could
// started OR has been running since before that unfortunate version. In both cases, going
// back to the default namespace name is the most safe bet we can make.
// but of course, our attempt will be futile if we're running in a context that doesn't know
// the current user.
Subject subj = EnvironmentContext.getCurrent().getSubject();
if (!subj.isAnonymous()) {
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(workspace.getId(), subj.getUserId(), subj.getUserName());
String defaultNamespace = evaluateNamespaceName(resolutionCtx);
LOG.warn("The namespace '{}' of the workspace '{}' is not valid. Trying to recover" + " from this situation using a default namespace which resolved to '{}'.", namespace, workspace.getId(), defaultNamespace);
namespace = defaultNamespace;
} else {
// log a warning including a stacktrace to be able to figure out from where we got here...
LOG.warn("The namespace '{}' of the workspace '{}' is not valid but we currently don't have" + " an active user to try an recover from this situation. We're letting the parent" + " workflow continue, but it may fail at some later point in time because of" + " the incorrect namespace name in use.", namespace, workspace.getId(), new Throwable());
}
// ok, we tried to recover the namespace but nothing helped.
}
return namespace;
}
use of org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext in project che-server by eclipse-che.
the class KubernetesNamespaceFactory method getOrCreate.
public KubernetesNamespace getOrCreate(RuntimeIdentity identity) throws InfrastructureException {
KubernetesNamespace namespace = get(identity);
var subject = EnvironmentContext.getCurrent().getSubject();
NamespaceResolutionContext resolutionCtx = new NamespaceResolutionContext(identity.getWorkspaceId(), subject.getUserId(), subject.getUserName());
Map<String, String> namespaceAnnotationsEvaluated = evaluateAnnotationPlaceholders(resolutionCtx);
namespace.prepare(canCreateNamespace(identity), labelNamespaces ? namespaceLabels : emptyMap(), annotateNamespaces ? namespaceAnnotationsEvaluated : emptyMap());
configureNamespace(resolutionCtx, namespace.getName());
return namespace;
}
Aggregations