org.eclipse.che.core
che-core-api-dto
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftClientFactory.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftClientFactory.java
index 73805fc824..4ce931f2ab 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftClientFactory.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftClientFactory.java
@@ -57,8 +57,6 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
private static final String BEFORE_TOKEN = "access_token=";
private static final String AFTER_TOKEN = "&expires";
- private final KubernetesClientConfigFactory configBuilder;
-
@Inject
public OpenShiftClientFactory(
KubernetesClientConfigFactory configBuilder,
@@ -72,6 +70,7 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
int connectionPoolKeepAlive,
EventListener eventListener) {
super(
+ configBuilder,
masterUrl,
doTrustCerts,
maxConcurrentRequests,
@@ -79,7 +78,6 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
maxIdleConnections,
connectionPoolKeepAlive,
eventListener);
- this.configBuilder = configBuilder;
}
/**
@@ -96,7 +94,7 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
*/
public OpenShiftClient createOC(String workspaceId) throws InfrastructureException {
Config configForWorkspace = buildConfig(getDefaultConfig(), workspaceId);
- return createOC(configForWorkspace);
+ return create(configForWorkspace);
}
/**
@@ -114,23 +112,13 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
* @throws InfrastructureException if any error occurs on client instance creation.
*/
public OpenShiftClient createOC() throws InfrastructureException {
- return createOC(buildConfig(getDefaultConfig(), null));
+ return create(buildConfig(getDefaultConfig(), null));
}
public OpenShiftClient createAuthenticatedClient(String token) {
Config config = getDefaultConfig();
config.setOauthToken(token);
- return createOC(config);
- }
-
- @Override
- public OkHttpClient getAuthenticatedHttpClient() throws InfrastructureException {
- if (!configBuilder.isPersonalized()) {
- throw new InfrastructureException(
- "Not able to construct impersonating openshift API client.");
- }
- // Ensure to get OkHttpClient with all necessary interceptors.
- return createOC(buildConfig(getDefaultConfig(), null)).getHttpClient();
+ return create(config);
}
@Override
@@ -147,19 +135,6 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
return configBuilder.build();
}
- /**
- * Builds the Openshift {@link Config} object based on a provided {@link Config} object and an
- * optional workspace ID.
- *
- * This method overrides the one in the Kubernetes infrastructure to introduce an additional
- * extension level by delegating to an {@link KubernetesClientConfigFactory}
- */
- @Override
- protected Config buildConfig(Config config, @Nullable String workspaceId)
- throws InfrastructureException {
- return configBuilder.buildConfig(config, workspaceId);
- }
-
@Override
protected Interceptor buildKubernetesInterceptor(Config config) {
final String oauthToken;
@@ -223,7 +198,7 @@ public class OpenShiftClientFactory extends KubernetesClientFactory {
};
}
- private DefaultOpenShiftClient createOC(Config config) {
+ protected DefaultOpenShiftClient create(Config config) {
return new UnclosableOpenShiftClient(
clientForConfig(config), config, this::initializeRequestTracing);
}
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftInfraModule.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftInfraModule.java
index 0bfd6c80f5..17e6932c5e 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftInfraModule.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/OpenShiftInfraModule.java
@@ -53,7 +53,9 @@ import org.eclipse.che.workspace.infrastructure.kubernetes.devfile.KubernetesDev
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironmentFactory;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.CredentialsSecretConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.UserPreferencesConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.UserProfileConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.pvc.CommonPVCStrategy;
@@ -94,6 +96,8 @@ import org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftE
import org.eclipse.che.workspace.infrastructure.openshift.environment.OpenShiftEnvironmentFactory;
import org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftProjectFactory;
import org.eclipse.che.workspace.infrastructure.openshift.project.RemoveProjectOnWorkspaceRemove;
+import org.eclipse.che.workspace.infrastructure.openshift.project.configurator.OpenShiftStopWorkspaceRoleConfigurator;
+import org.eclipse.che.workspace.infrastructure.openshift.project.configurator.OpenShiftWorkspaceServiceAccountConfigurator;
import org.eclipse.che.workspace.infrastructure.openshift.provision.OpenShiftPreviewUrlCommandProvisioner;
import org.eclipse.che.workspace.infrastructure.openshift.provision.OpenshiftTrustedCAProvisioner;
import org.eclipse.che.workspace.infrastructure.openshift.provision.RouteTlsProvisioner;
@@ -117,6 +121,10 @@ public class OpenShiftInfraModule extends AbstractModule {
Multibinder.newSetBinder(binder(), NamespaceConfigurator.class);
namespaceConfigurators.addBinding().to(UserProfileConfigurator.class);
namespaceConfigurators.addBinding().to(UserPreferencesConfigurator.class);
+ namespaceConfigurators.addBinding().to(CredentialsSecretConfigurator.class);
+ namespaceConfigurators.addBinding().to(PreferencesConfigMapConfigurator.class);
+ namespaceConfigurators.addBinding().to(OpenShiftWorkspaceServiceAccountConfigurator.class);
+ namespaceConfigurators.addBinding().to(OpenShiftStopWorkspaceRoleConfigurator.class);
bind(KubernetesNamespaceService.class);
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactory.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactory.java
index 4d957a38de..87813445c7 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactory.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactory.java
@@ -11,9 +11,9 @@
*/
package org.eclipse.che.workspace.infrastructure.openshift.multiuser.oauth;
-import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.CLIENT_ID_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING;
+import static org.eclipse.che.multiuser.oidc.OIDCInfoProvider.AUTH_SERVER_URL_SETTING;
import com.google.inject.Provider;
import io.fabric8.kubernetes.client.Config;
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilter.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilter.java
index 9d4862df58..8aa42afefc 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilter.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilter.java
@@ -16,15 +16,6 @@ import static com.google.common.base.MoreObjects.firstNonNull;
import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.client.OpenShiftClient;
-import jakarta.servlet.FilterChain;
-import jakarta.servlet.FilterConfig;
-import jakarta.servlet.ServletException;
-import jakarta.servlet.ServletRequest;
-import jakarta.servlet.ServletResponse;
-import jakarta.servlet.http.HttpServletRequest;
-import java.io.IOException;
-import java.util.Collections;
-import java.util.List;
import java.util.Optional;
import javax.inject.Inject;
import javax.inject.Singleton;
@@ -46,9 +37,6 @@ import org.slf4j.LoggerFactory;
/**
* This filter uses given token directly. It's used for native OpenShift user authentication.
* Requests without token or with invalid token are rejected.
- *
- *
{@link OpenshiftTokenInitializationFilter#UNAUTHORIZED_ENDPOINT_PATHS} is list of
- * unauthenticated paths, that are allowed without token.
*/
@Singleton
public class OpenshiftTokenInitializationFilter
@@ -57,9 +45,6 @@ public class OpenshiftTokenInitializationFilter
private static final Logger LOG =
LoggerFactory.getLogger(OpenshiftTokenInitializationFilter.class);
- private static final List UNAUTHORIZED_ENDPOINT_PATHS =
- Collections.singletonList("/system/state");
-
private final PermissionChecker permissionChecker;
private final OpenShiftClientFactory clientFactory;
@@ -121,38 +106,4 @@ public class OpenshiftTokenInitializationFilter
// we can use fake email, but probably we will need to find better solution.
return userMeta.getName() + "@che";
}
-
- /**
- * If request path is in {@link OpenshiftTokenInitializationFilter#UNAUTHORIZED_ENDPOINT_PATHS},
- * the request is allowed. All other requests are rejected with error code 401.
- */
- @Override
- protected void handleMissingToken(
- ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException {
-
- // if request path is in unauthorized endpoints, continue
- if (request instanceof HttpServletRequest) {
- HttpServletRequest httpRequest = (HttpServletRequest) request;
- String path = httpRequest.getServletPath();
- if (UNAUTHORIZED_ENDPOINT_PATHS.contains(path)) {
- LOG.debug("Allowing request to '{}' without authorization header.", path);
- chain.doFilter(request, response);
- return;
- }
- }
-
- LOG.error("Rejecting the request due to missing/expired token in Authorization header.");
- sendError(response, 401, "Authorization token is missing or expired");
- }
-
- @Override
- public void init(FilterConfig filterConfig) {
- LOG.trace("OpenshiftTokenInitializationFilter#init({})", filterConfig);
- }
-
- @Override
- public void destroy() {
- LOG.trace("OpenshiftTokenInitializationFilter#destroy()");
- }
}
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProject.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProject.java
index 9d202ade4d..9009ea35d3 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProject.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProject.java
@@ -50,7 +50,6 @@ public class OpenShiftProject extends KubernetesNamespace {
private final OpenShiftRoutes routes;
private final OpenShiftClientFactory clientFactory;
- private final KubernetesClientFactory cheClientFactory;
private final CheServerOpenshiftClientFactory cheServerOpenshiftClientFactory;
@VisibleForTesting
@@ -78,7 +77,6 @@ public class OpenShiftProject extends KubernetesNamespace {
ingresses,
secrets,
configMaps);
- this.cheClientFactory = cheClientFactory;
this.clientFactory = clientFactory;
this.routes = routes;
this.cheServerOpenshiftClientFactory = cheServerOpenshiftClientFactory;
@@ -93,7 +91,6 @@ public class OpenShiftProject extends KubernetesNamespace {
String workspaceId) {
super(clientFactory, cheClientFactory, executor, name, workspaceId);
this.clientFactory = clientFactory;
- this.cheClientFactory = cheClientFactory;
this.routes = new OpenShiftRoutes(name, workspaceId, clientFactory);
this.cheServerOpenshiftClientFactory = cheServerOpenshiftClientFactory;
}
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactory.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactory.java
index 604c011dd8..673ccb3b81 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactory.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactory.java
@@ -16,23 +16,18 @@ import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.PHASE_ATTRIBUTE;
-import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.CREDENTIALS_SECRET_NAME;
-import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.PREFERENCES_CONFIGMAP_NAME;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Inject;
import com.google.inject.Singleton;
-import io.fabric8.kubernetes.api.model.ConfigMap;
-import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
import io.fabric8.kubernetes.api.model.ObjectMeta;
-import io.fabric8.kubernetes.api.model.Secret;
-import io.fabric8.kubernetes.api.model.SecretBuilder;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.api.model.Project;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
+import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Named;
import org.eclipse.che.api.core.model.workspace.Workspace;
@@ -47,11 +42,11 @@ import org.eclipse.che.workspace.infrastructure.kubernetes.CheServerKubernetesCl
import org.eclipse.che.workspace.infrastructure.kubernetes.api.server.impls.KubernetesNamespaceMetaImpl;
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesNamespaceFactory;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSharedPool;
import org.eclipse.che.workspace.infrastructure.openshift.CheServerOpenshiftClientFactory;
import org.eclipse.che.workspace.infrastructure.openshift.Constants;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
-import org.eclipse.che.workspace.infrastructure.openshift.provision.OpenShiftStopWorkspaceRoleProvisioner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -67,14 +62,11 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
private final boolean initWithCheServerSa;
private final OpenShiftClientFactory clientFactory;
private final CheServerOpenshiftClientFactory cheOpenShiftClientFactory;
- private final OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisioner;
private final String oAuthIdentityProvider;
@Inject
public OpenShiftProjectFactory(
- @Nullable @Named("che.infra.kubernetes.service_account_name") String serviceAccountName,
- @Nullable @Named("che.infra.kubernetes.workspace_sa_cluster_roles") String clusterRoleNames,
@Nullable @Named("che.infra.kubernetes.namespace.default") String defaultNamespaceName,
@Named("che.infra.kubernetes.namespace.creation_allowed") boolean namespaceCreationAllowed,
@Named("che.infra.kubernetes.namespace.label") boolean labelProjects,
@@ -82,24 +74,23 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
@Named("che.infra.kubernetes.namespace.labels") String projectLabels,
@Named("che.infra.kubernetes.namespace.annotations") String projectAnnotations,
@Named("che.infra.openshift.project.init_with_server_sa") boolean initWithCheServerSa,
+ Set namespaceConfigurators,
OpenShiftClientFactory clientFactory,
CheServerKubernetesClientFactory cheClientFactory,
CheServerOpenshiftClientFactory cheOpenShiftClientFactory,
- OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisioner,
UserManager userManager,
PreferenceManager preferenceManager,
KubernetesSharedPool sharedPool,
@Nullable @Named("che.infra.openshift.oauth_identity_provider")
String oAuthIdentityProvider) {
super(
- serviceAccountName,
- clusterRoleNames,
defaultNamespaceName,
namespaceCreationAllowed,
labelProjects,
annotateProjects,
projectLabels,
projectAnnotations,
+ namespaceConfigurators,
clientFactory,
cheClientFactory,
userManager,
@@ -108,15 +99,16 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
this.initWithCheServerSa = initWithCheServerSa;
this.clientFactory = clientFactory;
this.cheOpenShiftClientFactory = cheOpenShiftClientFactory;
- this.stopWorkspaceRoleProvisioner = stopWorkspaceRoleProvisioner;
this.oAuthIdentityProvider = oAuthIdentityProvider;
}
public OpenShiftProject getOrCreate(RuntimeIdentity identity) throws InfrastructureException {
OpenShiftProject osProject = get(identity);
+ var subject = EnvironmentContext.getCurrent().getSubject();
NamespaceResolutionContext resolutionCtx =
- new NamespaceResolutionContext(EnvironmentContext.getCurrent().getSubject());
+ new NamespaceResolutionContext(
+ identity.getWorkspaceId(), subject.getUserId(), subject.getUserName());
Map namespaceAnnotationsEvaluated =
evaluateAnnotationPlaceholders(resolutionCtx);
@@ -126,50 +118,8 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
labelNamespaces ? namespaceLabels : emptyMap(),
annotateNamespaces ? namespaceAnnotationsEvaluated : emptyMap());
- // create credentials secret
- if (osProject
- .secrets()
- .get()
- .stream()
- .noneMatch(s -> s.getMetadata().getName().equals(CREDENTIALS_SECRET_NAME))) {
- Secret secret =
- new SecretBuilder()
- .withType("opaque")
- .withNewMetadata()
- .withName(CREDENTIALS_SECRET_NAME)
- .endMetadata()
- .build();
- clientFactory
- .createOC()
- .secrets()
- .inNamespace(identity.getInfrastructureNamespace())
- .create(secret);
- }
+ configureNamespace(resolutionCtx, osProject.getName());
- // create preferences configmap
- if (osProject.configMaps().get(PREFERENCES_CONFIGMAP_NAME).isEmpty()) {
- ConfigMap configMap =
- new ConfigMapBuilder()
- .withNewMetadata()
- .withName(PREFERENCES_CONFIGMAP_NAME)
- .endMetadata()
- .build();
- clientFactory
- .createOC()
- .configMaps()
- .inNamespace(identity.getInfrastructureNamespace())
- .create(configMap);
- }
-
- if (!isNullOrEmpty(getServiceAccountName())) {
- OpenShiftWorkspaceServiceAccount osWorkspaceServiceAccount =
- doCreateServiceAccount(osProject.getWorkspaceId(), osProject.getName());
- osWorkspaceServiceAccount.prepare();
- }
-
- if (!isNullOrEmpty(oAuthIdentityProvider)) {
- stopWorkspaceRoleProvisioner.provision(osProject.getName());
- }
return osProject;
}
@@ -190,11 +140,6 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
}
}
- @Override
- protected boolean checkNamespaceExists(String namespaceName) throws InfrastructureException {
- return fetchNamespaceObject(namespaceName).isPresent();
- }
-
/**
* Creates a kubernetes namespace for the specified workspace.
*
@@ -218,12 +163,6 @@ public class OpenShiftProjectFactory extends KubernetesNamespaceFactory {
workspaceId);
}
- @VisibleForTesting
- OpenShiftWorkspaceServiceAccount doCreateServiceAccount(String workspaceId, String projectName) {
- return new OpenShiftWorkspaceServiceAccount(
- workspaceId, projectName, getServiceAccountName(), getClusterRoleNames(), clientFactory);
- }
-
@Override
public Optional fetchNamespace(String name)
throws InfrastructureException {
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftWorkspaceServiceAccount.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftWorkspaceServiceAccount.java
index 7870a2d299..2933fc0af0 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftWorkspaceServiceAccount.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftWorkspaceServiceAccount.java
@@ -33,10 +33,10 @@ import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory
* @see
* org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesWorkspaceServiceAccount
*/
-class OpenShiftWorkspaceServiceAccount
+public class OpenShiftWorkspaceServiceAccount
extends AbstractWorkspaceServiceAccount {
- OpenShiftWorkspaceServiceAccount(
+ public OpenShiftWorkspaceServiceAccount(
String workspaceId,
String projectName,
String serviceAccountName,
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisioner.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfigurator.java
similarity index 80%
rename from infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisioner.java
rename to infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfigurator.java
index 0db4b86d60..91d09ea080 100644
--- a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisioner.java
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfigurator.java
@@ -9,7 +9,9 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
-package org.eclipse.che.workspace.infrastructure.openshift.provision;
+package org.eclipse.che.workspace.infrastructure.openshift.project.configurator;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder;
import io.fabric8.openshift.api.model.PolicyRuleBuilder;
@@ -20,8 +22,12 @@ import io.fabric8.openshift.api.model.RoleBuilder;
import io.fabric8.openshift.client.OpenShiftClient;
import javax.inject.Inject;
import javax.inject.Named;
+import javax.inject.Singleton;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
+import org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext;
+import org.eclipse.che.commons.annotation.Nullable;
import org.eclipse.che.workspace.infrastructure.kubernetes.environment.CheInstallationLocation;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -32,27 +38,37 @@ import org.slf4j.LoggerFactory;
*
* @author Tom George
*/
-public class OpenShiftStopWorkspaceRoleProvisioner {
+@Singleton
+public class OpenShiftStopWorkspaceRoleConfigurator implements NamespaceConfigurator {
private final OpenShiftClientFactory clientFactory;
private final String installationLocation;
private final boolean stopWorkspaceRoleEnabled;
+ private final String oAuthIdentityProvider;
private static final Logger LOG =
- LoggerFactory.getLogger(OpenShiftStopWorkspaceRoleProvisioner.class);
+ LoggerFactory.getLogger(OpenShiftStopWorkspaceRoleConfigurator.class);
@Inject
- public OpenShiftStopWorkspaceRoleProvisioner(
+ public OpenShiftStopWorkspaceRoleConfigurator(
OpenShiftClientFactory clientFactory,
CheInstallationLocation installationLocation,
- @Named("che.workspace.stop.role.enabled") boolean stopWorkspaceRoleEnabled)
+ @Named("che.workspace.stop.role.enabled") boolean stopWorkspaceRoleEnabled,
+ @Nullable @Named("che.infra.openshift.oauth_identity_provider") String oAuthIdentityProvider)
throws InfrastructureException {
this.clientFactory = clientFactory;
this.installationLocation = installationLocation.getInstallationLocationNamespace();
this.stopWorkspaceRoleEnabled = stopWorkspaceRoleEnabled;
+ this.oAuthIdentityProvider = oAuthIdentityProvider;
}
- public void provision(String projectName) throws InfrastructureException {
+ @Override
+ public void configure(NamespaceResolutionContext namespaceResolutionContext, String projectName)
+ throws InfrastructureException {
+ if (isNullOrEmpty(oAuthIdentityProvider)) {
+ return;
+ }
+
if (stopWorkspaceRoleEnabled && installationLocation != null) {
OpenShiftClient osClient = clientFactory.createOC();
String stopWorkspacesRoleName = "workspace-stop";
diff --git a/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfigurator.java b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfigurator.java
new file mode 100644
index 0000000000..5c18666e20
--- /dev/null
+++ b/infrastructures/openshift/src/main/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfigurator.java
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2012-2021 Red Hat, Inc.
+ * This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License 2.0
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ */
+package org.eclipse.che.workspace.infrastructure.openshift.project.configurator;
+
+import static com.google.common.base.Strings.isNullOrEmpty;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Splitter;
+import com.google.common.collect.Sets;
+import java.util.Collections;
+import java.util.Set;
+import javax.inject.Inject;
+import javax.inject.Named;
+import javax.inject.Singleton;
+import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
+import org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext;
+import org.eclipse.che.commons.annotation.Nullable;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
+import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
+import org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftWorkspaceServiceAccount;
+
+/**
+ * This {@link NamespaceConfigurator} ensures that workspace ServiceAccount with proper ClusterRole
+ * is set in Workspace project.
+ */
+@Singleton
+public class OpenShiftWorkspaceServiceAccountConfigurator implements NamespaceConfigurator {
+
+ private final OpenShiftClientFactory clientFactory;
+
+ private final String serviceAccountName;
+ private final Set clusterRoleNames;
+
+ @Inject
+ public OpenShiftWorkspaceServiceAccountConfigurator(
+ @Nullable @Named("che.infra.kubernetes.service_account_name") String serviceAccountName,
+ @Nullable @Named("che.infra.kubernetes.workspace_sa_cluster_roles") String clusterRoleNames,
+ OpenShiftClientFactory clientFactory) {
+ this.clientFactory = clientFactory;
+ this.serviceAccountName = serviceAccountName;
+ if (!isNullOrEmpty(clusterRoleNames)) {
+ this.clusterRoleNames =
+ Sets.newHashSet(
+ Splitter.on(",").trimResults().omitEmptyStrings().split(clusterRoleNames));
+ } else {
+ this.clusterRoleNames = Collections.emptySet();
+ }
+ }
+
+ @Override
+ public void configure(NamespaceResolutionContext namespaceResolutionContext, String namespaceName)
+ throws InfrastructureException {
+ if (!isNullOrEmpty(serviceAccountName)) {
+ OpenShiftWorkspaceServiceAccount osWorkspaceServiceAccount =
+ createServiceAccount(namespaceResolutionContext.getWorkspaceId(), namespaceName);
+ osWorkspaceServiceAccount.prepare();
+ }
+ }
+
+ @VisibleForTesting
+ public OpenShiftWorkspaceServiceAccount createServiceAccount(String wsId, String namespaceName) {
+ return new OpenShiftWorkspaceServiceAccount(
+ wsId, namespaceName, serviceAccountName, clusterRoleNames, clientFactory);
+ }
+}
diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactoryTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactoryTest.java
index 501c6bcf2c..d85784d030 100644
--- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactoryTest.java
+++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/KeycloakProviderConfigFactoryTest.java
@@ -11,9 +11,9 @@
*/
package org.eclipse.che.workspace.infrastructure.openshift.multiuser.oauth;
-import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.AUTH_SERVER_URL_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.CLIENT_ID_SETTING;
import static org.eclipse.che.multiuser.keycloak.shared.KeycloakConstants.REALM_SETTING;
+import static org.eclipse.che.multiuser.oidc.OIDCInfoProvider.AUTH_SERVER_URL_SETTING;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilterTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilterTest.java
index a649dde5af..547c37f419 100644
--- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilterTest.java
+++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/multiuser/oauth/OpenshiftTokenInitializationFilterTest.java
@@ -11,8 +11,6 @@
*/
package org.eclipse.che.workspace.infrastructure.openshift.multiuser.oauth;
-import static org.mockito.ArgumentMatchers.anyString;
-import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.*;
@@ -21,11 +19,6 @@ import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.openshift.api.model.User;
import io.fabric8.openshift.client.OpenShiftClient;
-import jakarta.servlet.FilterChain;
-import jakarta.servlet.ServletException;
-import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
-import java.io.IOException;
import java.util.Optional;
import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.ServerException;
@@ -55,10 +48,6 @@ public class OpenshiftTokenInitializationFilterTest {
@Mock private User openshiftUser;
@Mock private ObjectMeta openshiftUserMeta;
- @Mock private HttpServletRequest servletRequest;
- @Mock private HttpServletResponse servletResponse;
- @Mock private FilterChain filterChain;
-
private static final String TOKEN = "touken";
private static final String USER_UID = "almost-certainly-unique-id";
private static final String USERNAME = "test_username";
@@ -111,7 +100,7 @@ public class OpenshiftTokenInitializationFilterTest {
@Test
public void extractSubjectCreatesSubjectWithCurrentlyAuthenticatedUser()
- throws InfrastructureException, ServerException, ConflictException {
+ throws ServerException, ConflictException {
when(openShiftClientFactory.createAuthenticatedClient(TOKEN)).thenReturn(openShiftClient);
when(openShiftClient.currentUser()).thenReturn(openshiftUser);
when(openshiftUser.getMetadata()).thenReturn(openshiftUserMeta);
@@ -128,27 +117,6 @@ public class OpenshiftTokenInitializationFilterTest {
assertEquals(subject.getUserName(), USERNAME);
}
- @Test
- public void handleMissingTokenShouldAllowUnauthorizedEndpoint()
- throws ServletException, IOException {
- when(servletRequest.getServletPath()).thenReturn("/system/state");
-
- openshiftTokenInitializationFilter.handleMissingToken(
- servletRequest, servletResponse, filterChain);
-
- verify(filterChain).doFilter(servletRequest, servletResponse);
- }
-
- @Test
- public void handleMissingTokenShouldRejectRequest() throws ServletException, IOException {
- when(servletRequest.getServletPath()).thenReturn("blabol");
-
- openshiftTokenInitializationFilter.handleMissingToken(
- servletRequest, servletResponse, filterChain);
-
- verify(servletResponse).sendError(eq(401), anyString());
- }
-
@Test
public void invalidTokenShouldBeHandledAsMissing() throws Exception {
when(openShiftClientFactory.createAuthenticatedClient(TOKEN)).thenReturn(openShiftClient);
diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactoryTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactoryTest.java
index e7dfa27233..5182d2c945 100644
--- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactoryTest.java
+++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/OpenShiftProjectFactoryTest.java
@@ -13,8 +13,8 @@ package org.eclipse.che.workspace.infrastructure.openshift.project;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
+import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
-import static java.util.Optional.empty;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.DEFAULT_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta.PHASE_ATTRIBUTE;
import static org.eclipse.che.workspace.infrastructure.kubernetes.namespace.AbstractWorkspaceServiceAccount.CREDENTIALS_SECRET_NAME;
@@ -32,7 +32,6 @@ import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
@@ -58,7 +57,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
+import java.util.Set;
import org.eclipse.che.api.core.ValidationException;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.user.server.PreferenceManager;
@@ -76,12 +75,16 @@ import org.eclipse.che.workspace.infrastructure.kubernetes.CheServerKubernetesCl
import org.eclipse.che.workspace.infrastructure.kubernetes.api.shared.KubernetesNamespaceMeta;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesConfigsMaps;
import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.KubernetesSecrets;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.CredentialsSecretConfigurator;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.NamespaceConfigurator;
+import org.eclipse.che.workspace.infrastructure.kubernetes.namespace.configurator.PreferencesConfigMapConfigurator;
import org.eclipse.che.workspace.infrastructure.kubernetes.util.KubernetesSharedPool;
import org.eclipse.che.workspace.infrastructure.openshift.CheServerOpenshiftClientFactory;
import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
-import org.eclipse.che.workspace.infrastructure.openshift.provision.OpenShiftStopWorkspaceRoleProvisioner;
+import org.eclipse.che.workspace.infrastructure.openshift.project.configurator.OpenShiftWorkspaceServiceAccountConfigurator;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
+import org.mockito.Mockito;
import org.mockito.testng.MockitoTestNGListener;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
@@ -110,7 +113,6 @@ public class OpenShiftProjectFactoryTest {
@Mock private OpenShiftClientFactory clientFactory;
@Mock private CheServerKubernetesClientFactory cheClientFactory;
@Mock private CheServerOpenshiftClientFactory cheServerOpenshiftClientFactory;
- @Mock private OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisioner;
@Mock private WorkspaceManager workspaceManager;
@Mock private UserManager userManager;
@Mock private PreferenceManager preferenceManager;
@@ -131,6 +133,7 @@ public class OpenShiftProjectFactoryTest {
@BeforeMethod
public void setUp() throws Exception {
lenient().when(clientFactory.createOC()).thenReturn(osClient);
+ lenient().when(clientFactory.create()).thenReturn(osClient);
lenient().when(osClient.projects()).thenReturn(projectOperation);
lenient()
@@ -162,8 +165,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -171,10 +172,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -195,8 +196,6 @@ public class OpenShiftProjectFactoryTest {
System.out.println("2--------");
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -204,10 +203,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -228,8 +227,6 @@ public class OpenShiftProjectFactoryTest {
throws Exception {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
null,
true,
true,
@@ -237,10 +234,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -266,8 +263,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- "",
"-che",
true,
true,
@@ -275,10 +270,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -305,8 +300,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- "",
"-che",
true,
true,
@@ -314,10 +307,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -340,8 +333,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- "",
"-che",
true,
true,
@@ -349,10 +340,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -385,8 +376,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -394,10 +383,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -424,8 +413,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -433,10 +420,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -463,8 +450,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -472,10 +457,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -492,8 +477,6 @@ public class OpenShiftProjectFactoryTest {
throwOnTryToGetProjectsList(new KubernetesClientException("connection refused"));
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -501,10 +484,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -526,8 +509,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -535,10 +516,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -554,7 +535,6 @@ public class OpenShiftProjectFactoryTest {
// then
assertEquals(toReturnProject, project);
- verify(projectFactory, never()).doCreateServiceAccount(any(), any());
verify(toReturnProject).prepare(eq(false), eq(false), any(), any());
}
@@ -564,8 +544,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -573,30 +551,28 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ Set.of(new CredentialsSecretConfigurator(clientFactory)),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
NO_OAUTH_IDENTITY_PROVIDER));
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
+ when(toReturnProject.getName()).thenReturn("namespace123");
NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
MixedOperation mixedOperation = mock(MixedOperation.class);
- KubernetesSecrets secrets = mock(KubernetesSecrets.class);
- KubernetesConfigsMaps configsMaps = mock(KubernetesConfigsMaps.class);
- when(toReturnProject.secrets()).thenReturn(secrets);
- when(toReturnProject.configMaps()).thenReturn(configsMaps);
- when(secrets.get()).thenReturn(Collections.emptyList());
- when(configsMaps.get(anyString())).thenReturn(Optional.of(mock(ConfigMap.class)));
- lenient().when(osClient.secrets()).thenReturn(mixedOperation);
- lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ when(osClient.secrets()).thenReturn(mixedOperation);
+ when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ Resource nullSecret = mock(Resource.class);
+ when(namespaceOperation.withName(CREDENTIALS_SECRET_NAME)).thenReturn(nullSecret);
+ when(nullSecret.get()).thenReturn(null);
// when
RuntimeIdentity identity =
- new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
+ new RuntimeIdentityImpl("workspace123", null, USER_ID, "namespace123");
projectFactory.getOrCreate(identity);
// then
@@ -613,8 +589,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -622,31 +596,23 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ Set.of(new PreferencesConfigMapConfigurator(clientFactory)),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
NO_OAUTH_IDENTITY_PROVIDER));
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
+ when(toReturnProject.getName()).thenReturn("namespace123");
NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
MixedOperation mixedOperation = mock(MixedOperation.class);
- KubernetesSecrets secrets = mock(KubernetesSecrets.class);
- Secret secret = mock(Secret.class);
- ObjectMeta objectMeta = mock(ObjectMeta.class);
- when(secret.getMetadata()).thenReturn(objectMeta);
- when(objectMeta.getName()).thenReturn(CREDENTIALS_SECRET_NAME);
- when(toReturnProject.secrets()).thenReturn(secrets);
- when(secrets.get()).thenReturn(singletonList(secret));
- lenient().when(osClient.secrets()).thenReturn(mixedOperation);
- KubernetesConfigsMaps configsMaps = mock(KubernetesConfigsMaps.class);
- when(toReturnProject.configMaps()).thenReturn(configsMaps);
- when(configsMaps.get(eq(PREFERENCES_CONFIGMAP_NAME))).thenReturn(empty());
- lenient().when(osClient.configMaps()).thenReturn(mixedOperation);
- lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ when(osClient.configMaps()).thenReturn(mixedOperation);
+ when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ Resource nullCm = mock(Resource.class);
+ when(namespaceOperation.withName(PREFERENCES_CONFIGMAP_NAME)).thenReturn(nullCm);
// when
RuntimeIdentity identity =
@@ -666,8 +632,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -675,10 +639,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ Set.of(new CredentialsSecretConfigurator(clientFactory)),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -686,10 +650,14 @@ public class OpenShiftProjectFactoryTest {
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
prepareProject(toReturnProject);
doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
+ when(toReturnProject.getName()).thenReturn("namespace123");
NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
MixedOperation mixedOperation = mock(MixedOperation.class);
- lenient().when(osClient.secrets()).thenReturn(mixedOperation);
- lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ when(osClient.secrets()).thenReturn(mixedOperation);
+ when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ Resource secretResource = mock(Resource.class);
+ when(namespaceOperation.withName(CREDENTIALS_SECRET_NAME)).thenReturn(secretResource);
+ when(secretResource.get()).thenReturn(mock(Secret.class));
// when
RuntimeIdentity identity =
@@ -706,8 +674,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -715,10 +681,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ Set.of(new PreferencesConfigMapConfigurator(clientFactory)),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -726,10 +692,14 @@ public class OpenShiftProjectFactoryTest {
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
prepareProject(toReturnProject);
doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
+ when(toReturnProject.getName()).thenReturn("namespace123");
NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
MixedOperation mixedOperation = mock(MixedOperation.class);
- lenient().when(osClient.configMaps()).thenReturn(mixedOperation);
- lenient().when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ when(osClient.configMaps()).thenReturn(mixedOperation);
+ when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
+ Resource cmResource = mock(Resource.class);
+ when(namespaceOperation.withName(PREFERENCES_CONFIGMAP_NAME)).thenReturn(cmResource);
+ when(cmResource.get()).thenReturn(mock(ConfigMap.class));
// when
RuntimeIdentity identity =
@@ -740,56 +710,13 @@ public class OpenShiftProjectFactoryTest {
verify(namespaceOperation, never()).create(any());
}
- @Test
- public void shouldPrepareWorkspaceServiceAccountIfItIsConfiguredAndProjectIsNotPredefined()
- throws Exception {
- // given
- projectFactory =
- spy(
- new OpenShiftProjectFactory(
- "serviceAccount",
- null,
- "-che",
- true,
- true,
- true,
- NAMESPACE_LABELS,
- NAMESPACE_ANNOTATIONS,
- true,
- clientFactory,
- cheClientFactory,
- cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
- userManager,
- preferenceManager,
- pool,
- NO_OAUTH_IDENTITY_PROVIDER));
- OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
- prepareProject(toReturnProject);
- when(toReturnProject.getWorkspaceId()).thenReturn("workspace123");
- when(toReturnProject.getName()).thenReturn("workspace123");
- doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
-
- OpenShiftWorkspaceServiceAccount serviceAccount = mock(OpenShiftWorkspaceServiceAccount.class);
- doReturn(serviceAccount).when(projectFactory).doCreateServiceAccount(any(), any());
-
- // when
- RuntimeIdentity identity =
- new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
- projectFactory.getOrCreate(identity);
-
- // then
- verify(projectFactory).doCreateServiceAccount("workspace123", "workspace123");
- verify(serviceAccount).prepare();
- }
-
@Test
public void shouldCallStopWorkspaceRoleProvisionWhenIdentityProviderIsDefined() throws Exception {
+ var saConf =
+ spy(new OpenShiftWorkspaceServiceAccountConfigurator("serviceAccount", "", clientFactory));
projectFactory =
spy(
new OpenShiftProjectFactory(
- "serviceAccount",
- null,
"-che",
true,
true,
@@ -797,22 +724,21 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ Set.of(saConf),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
OAUTH_IDENTITY_PROVIDER));
OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
- when(toReturnProject.getWorkspaceId()).thenReturn("workspace123");
when(toReturnProject.getName()).thenReturn("workspace123");
prepareProject(toReturnProject);
doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
OpenShiftWorkspaceServiceAccount serviceAccount = mock(OpenShiftWorkspaceServiceAccount.class);
- doReturn(serviceAccount).when(projectFactory).doCreateServiceAccount(any(), any());
+ doReturn(serviceAccount).when(saConf).createServiceAccount("workspace123", "workspace123");
// when
RuntimeIdentity identity =
@@ -820,52 +746,7 @@ public class OpenShiftProjectFactoryTest {
projectFactory.getOrCreate(identity);
// then
- verify(projectFactory).doCreateServiceAccount("workspace123", "workspace123");
verify(serviceAccount).prepare();
- verify(stopWorkspaceRoleProvisioner, times(1)).provision("workspace123");
- }
-
- @Test
- public void shouldNotCallStopWorkspaceRoleProvisionWhenIdentityProviderIsDefined()
- throws Exception {
- projectFactory =
- spy(
- new OpenShiftProjectFactory(
- "serviceAccount",
- null,
- "-che",
- true,
- true,
- true,
- NAMESPACE_LABELS,
- NAMESPACE_ANNOTATIONS,
- true,
- clientFactory,
- cheClientFactory,
- cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
- userManager,
- preferenceManager,
- pool,
- NO_OAUTH_IDENTITY_PROVIDER));
- OpenShiftProject toReturnProject = mock(OpenShiftProject.class);
- prepareProject(toReturnProject);
- when(toReturnProject.getWorkspaceId()).thenReturn("workspace123");
- when(toReturnProject.getName()).thenReturn("workspace123");
- doReturn(toReturnProject).when(projectFactory).doCreateProjectAccess(any(), any());
-
- OpenShiftWorkspaceServiceAccount serviceAccount = mock(OpenShiftWorkspaceServiceAccount.class);
- doReturn(serviceAccount).when(projectFactory).doCreateServiceAccount(any(), any());
-
- // when
- RuntimeIdentity identity =
- new RuntimeIdentityImpl("workspace123", null, USER_ID, "workspace123");
- projectFactory.getOrCreate(identity);
-
- // then
- verify(projectFactory).doCreateServiceAccount("workspace123", "workspace123");
- verify(serviceAccount).prepare();
- verify(stopWorkspaceRoleProvisioner, times(0)).provision("workspace123");
}
@Test
@@ -886,8 +767,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- "",
"-che",
true,
true,
@@ -895,10 +774,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -921,8 +800,6 @@ public class OpenShiftProjectFactoryTest {
projectFactory =
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -930,10 +807,10 @@ public class OpenShiftProjectFactoryTest {
"try_placeholder_here=",
NAMESPACE_ANNOTATIONS,
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -946,13 +823,10 @@ public class OpenShiftProjectFactoryTest {
@Test
public void testUsernamePlaceholderInAnnotationsIsEvaluated() throws InfrastructureException {
-
// given
projectFactory =
spy(
new OpenShiftProjectFactory(
- "",
- null,
"-che",
true,
true,
@@ -960,10 +834,10 @@ public class OpenShiftProjectFactoryTest {
NAMESPACE_LABELS,
"try_placeholder_here=",
true,
+ emptySet(),
clientFactory,
cheClientFactory,
cheServerOpenshiftClientFactory,
- stopWorkspaceRoleProvisioner,
userManager,
preferenceManager,
pool,
@@ -983,6 +857,51 @@ public class OpenShiftProjectFactoryTest {
.prepare(eq(false), eq(false), any(), eq(Map.of("try_placeholder_here", "jondoe")));
}
+ @Test
+ public void testAllConfiguratorsAreCalledWhenCreatingProject() throws InfrastructureException {
+ // given
+ String projectName = "testprojectname";
+ NamespaceConfigurator configurator1 = Mockito.mock(NamespaceConfigurator.class);
+ NamespaceConfigurator configurator2 = Mockito.mock(NamespaceConfigurator.class);
+ Set namespaceConfigurators = Set.of(configurator1, configurator2);
+
+ projectFactory =
+ spy(
+ new OpenShiftProjectFactory(
+ "-che",
+ true,
+ true,
+ true,
+ NAMESPACE_LABELS,
+ "try_placeholder_here=",
+ 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);
+ }
+
private void prepareNamespaceToBeFoundByName(String name, Project project) throws Exception {
@SuppressWarnings("unchecked")
Resource getProjectByNameOperation = mock(Resource.class);
@@ -1010,15 +929,13 @@ public class OpenShiftProjectFactoryTest {
private void prepareProject(OpenShiftProject project) throws InfrastructureException {
KubernetesSecrets secrets = mock(KubernetesSecrets.class);
+ lenient().when(project.secrets()).thenReturn(secrets);
KubernetesConfigsMaps configsMaps = mock(KubernetesConfigsMaps.class);
- when(project.secrets()).thenReturn(secrets);
- when(project.configMaps()).thenReturn(configsMaps);
- when(configsMaps.get(anyString())).thenReturn(Optional.of(mock(ConfigMap.class)));
Secret secretMock = mock(Secret.class);
ObjectMeta objectMeta = mock(ObjectMeta.class);
- when(objectMeta.getName()).thenReturn(CREDENTIALS_SECRET_NAME);
- when(secretMock.getMetadata()).thenReturn(objectMeta);
- when(secrets.get()).thenReturn(singletonList(secretMock));
+ lenient().when(objectMeta.getName()).thenReturn(CREDENTIALS_SECRET_NAME);
+ lenient().when(secretMock.getMetadata()).thenReturn(objectMeta);
+ lenient().when(secrets.get()).thenReturn(Collections.singletonList(secretMock));
}
private void throwOnTryToGetProjectsList(Throwable e) throws Exception {
diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisionerTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfiguratorTest.java
similarity index 82%
rename from infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisionerTest.java
rename to infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfiguratorTest.java
index f9694ab164..e59f0888bc 100644
--- a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/provision/OpenShiftStopWorkspaceRoleProvisionerTest.java
+++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftStopWorkspaceRoleConfiguratorTest.java
@@ -9,7 +9,7 @@
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
-package org.eclipse.che.workspace.infrastructure.openshift.provision;
+package org.eclipse.che.workspace.infrastructure.openshift.project.configurator;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyString;
@@ -17,6 +17,7 @@ import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import io.fabric8.kubernetes.api.model.ObjectReferenceBuilder;
@@ -42,15 +43,16 @@ import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
/**
- * Test for {@link OpenShiftStopWorkspaceRoleProvisioner}
+ * Test for {@link
+ * org.eclipse.che.workspace.infrastructure.openshift.project.configurator.OpenShiftStopWorkspaceRoleConfigurator}
*
* #author Tom George
*/
@Listeners(MockitoTestNGListener.class)
-public class OpenShiftStopWorkspaceRoleProvisionerTest {
+public class OpenShiftStopWorkspaceRoleConfiguratorTest {
@Mock private CheInstallationLocation cheInstallationLocation;
- private OpenShiftStopWorkspaceRoleProvisioner stopWorkspaceRoleProvisioner;
+ private OpenShiftStopWorkspaceRoleConfigurator stopWorkspaceRoleProvisioner;
@Mock private OpenShiftClientFactory clientFactory;
@Mock private OpenShiftClient osClient;
@@ -123,7 +125,8 @@ public class OpenShiftStopWorkspaceRoleProvisionerTest {
public void setUp() throws Exception {
lenient().when(cheInstallationLocation.getInstallationLocationNamespace()).thenReturn("che");
stopWorkspaceRoleProvisioner =
- new OpenShiftStopWorkspaceRoleProvisioner(clientFactory, cheInstallationLocation, true);
+ new OpenShiftStopWorkspaceRoleConfigurator(
+ clientFactory, cheInstallationLocation, true, "yes");
lenient().when(clientFactory.createOC()).thenReturn(osClient);
lenient().when(osClient.roles()).thenReturn(mixedRoleOperation);
lenient().when(osClient.roleBindings()).thenReturn(mixedRoleBindingOperation);
@@ -160,7 +163,7 @@ public class OpenShiftStopWorkspaceRoleProvisionerTest {
@Test
public void shouldCreateRoleAndRoleBindingWhenRoleDoesNotYetExist()
throws InfrastructureException {
- stopWorkspaceRoleProvisioner.provision("developer-che");
+ stopWorkspaceRoleProvisioner.configure(null, "developer-che");
verify(osClient, times(2)).roles();
verify(osClient.roles(), times(2)).inNamespace("developer-che");
verify(osClient.roles().inNamespace("developer-che")).withName("workspace-stop");
@@ -174,7 +177,7 @@ public class OpenShiftStopWorkspaceRoleProvisionerTest {
@Test
public void shouldCreateRoleBindingWhenRoleAlreadyExists() throws InfrastructureException {
lenient().when(roleResource.get()).thenReturn(expectedRole);
- stopWorkspaceRoleProvisioner.provision("developer-che");
+ stopWorkspaceRoleProvisioner.configure(null, "developer-che");
verify(osClient, times(1)).roles();
verify(osClient).roleBindings();
verify(osClient.roleBindings()).inNamespace("developer-che");
@@ -185,9 +188,10 @@ public class OpenShiftStopWorkspaceRoleProvisionerTest {
@Test
public void shouldNotCreateRoleBindingWhenStopWorkspaceRolePropertyIsDisabled()
throws InfrastructureException {
- OpenShiftStopWorkspaceRoleProvisioner disabledStopWorkspaceRoleProvisioner =
- new OpenShiftStopWorkspaceRoleProvisioner(clientFactory, cheInstallationLocation, false);
- disabledStopWorkspaceRoleProvisioner.provision("developer-che");
+ OpenShiftStopWorkspaceRoleConfigurator disabledStopWorkspaceRoleProvisioner =
+ new OpenShiftStopWorkspaceRoleConfigurator(
+ clientFactory, cheInstallationLocation, false, "yes");
+ disabledStopWorkspaceRoleProvisioner.configure(null, "developer-che");
verify(osClient, never()).roles();
verify(osClient, never()).roleBindings();
verify(osClient.roleBindings(), never()).inNamespace("developer-che");
@@ -197,12 +201,26 @@ public class OpenShiftStopWorkspaceRoleProvisionerTest {
public void shouldNotCreateRoleBindingWhenInstallationLocationIsNull()
throws InfrastructureException {
lenient().when(cheInstallationLocation.getInstallationLocationNamespace()).thenReturn(null);
- OpenShiftStopWorkspaceRoleProvisioner
+ OpenShiftStopWorkspaceRoleConfigurator
stopWorkspaceRoleProvisionerWithoutValidInstallationLocation =
- new OpenShiftStopWorkspaceRoleProvisioner(clientFactory, cheInstallationLocation, true);
- stopWorkspaceRoleProvisionerWithoutValidInstallationLocation.provision("developer-che");
+ new OpenShiftStopWorkspaceRoleConfigurator(
+ clientFactory, cheInstallationLocation, true, "yes");
+ stopWorkspaceRoleProvisionerWithoutValidInstallationLocation.configure(null, "developer-che");
verify(osClient, never()).roles();
verify(osClient, never()).roleBindings();
verify(osClient.roleBindings(), never()).inNamespace("developer-che");
}
+
+ @Test
+ public void shouldNotCallStopWorkspaceRoleProvisionWhenIdentityProviderIsDefined()
+ throws Exception {
+ when(cheInstallationLocation.getInstallationLocationNamespace()).thenReturn("something");
+ OpenShiftStopWorkspaceRoleConfigurator configurator =
+ new OpenShiftStopWorkspaceRoleConfigurator(
+ clientFactory, cheInstallationLocation, true, null);
+
+ configurator.configure(null, "something");
+
+ verify(clientFactory, times(0)).createOC();
+ }
}
diff --git a/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfiguratorTest.java b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfiguratorTest.java
new file mode 100644
index 0000000000..fef482236b
--- /dev/null
+++ b/infrastructures/openshift/src/test/java/org/eclipse/che/workspace/infrastructure/openshift/project/configurator/OpenShiftWorkspaceServiceAccountConfiguratorTest.java
@@ -0,0 +1,77 @@
+/*
+ * Copyright (c) 2012-2021 Red Hat, Inc.
+ * This program and the accompanying materials are made
+ * available under the terms of the Eclipse Public License 2.0
+ * which is available at https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ */
+package org.eclipse.che.workspace.infrastructure.openshift.project.configurator;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.testng.Assert.*;
+
+import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
+import org.eclipse.che.api.workspace.server.spi.NamespaceResolutionContext;
+import org.eclipse.che.workspace.infrastructure.openshift.OpenShiftClientFactory;
+import org.eclipse.che.workspace.infrastructure.openshift.project.OpenShiftWorkspaceServiceAccount;
+import org.mockito.Mock;
+import org.mockito.testng.MockitoTestNGListener;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Listeners;
+import org.testng.annotations.Test;
+
+@Listeners(MockitoTestNGListener.class)
+public class OpenShiftWorkspaceServiceAccountConfiguratorTest {
+ private final String SA_NAME = "test-serviceaccout";
+ private final String CLUSTER_ROLES = "role1, role2";
+
+ private final String WS_ID = "ws123";
+ private final String USER_ID = "user123";
+ private final String USERNAME = "user-che";
+
+ private final String NS_NAME = "namespace-che";
+
+ private NamespaceResolutionContext nsContext;
+
+ @Mock private OpenShiftClientFactory clientFactory;
+
+ private OpenShiftWorkspaceServiceAccountConfigurator saConfigurator;
+
+ @BeforeMethod
+ public void setUp() {
+ nsContext = new NamespaceResolutionContext(WS_ID, USER_ID, USERNAME);
+ }
+
+ @Test
+ public void testPreparesServiceAccount() throws InfrastructureException {
+ saConfigurator =
+ spy(
+ new OpenShiftWorkspaceServiceAccountConfigurator(
+ SA_NAME, CLUSTER_ROLES, clientFactory));
+ OpenShiftWorkspaceServiceAccount serviceAccount = mock(OpenShiftWorkspaceServiceAccount.class);
+ doReturn(serviceAccount).when(saConfigurator).createServiceAccount(WS_ID, NS_NAME);
+
+ saConfigurator.configure(nsContext, NS_NAME);
+
+ verify(serviceAccount).prepare();
+ }
+
+ @Test
+ public void testDoNothingWhenServiceAccountNotSet() throws InfrastructureException {
+ saConfigurator =
+ spy(new OpenShiftWorkspaceServiceAccountConfigurator(null, CLUSTER_ROLES, clientFactory));
+
+ saConfigurator.configure(nsContext, NS_NAME);
+
+ verify(saConfigurator, times(0)).createServiceAccount(any(), any());
+ }
+}
diff --git a/multiuser/api/che-multiuser-api-authentication-commons/src/main/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilter.java b/multiuser/api/che-multiuser-api-authentication-commons/src/main/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilter.java
index c100be3417..85206f81c4 100644
--- a/multiuser/api/che-multiuser-api-authentication-commons/src/main/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilter.java
+++ b/multiuser/api/che-multiuser-api-authentication-commons/src/main/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilter.java
@@ -23,6 +23,8 @@ import jakarta.servlet.http.HttpServletRequestWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.IOException;
+import java.util.Collections;
+import java.util.List;
import java.util.Optional;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.subject.Subject;
@@ -43,6 +45,9 @@ import org.slf4j.LoggerFactory;
*
Set subject for current request into {@link EnvironmentContext}
*
*
+ * {@link MultiUserEnvironmentInitializationFilter#UNAUTHORIZED_ENDPOINT_PATHS} is list of
+ * unauthenticated paths, that are allowed without token.
+ *
* @param the type of intermediary type used for conversion from a string token to a Subject
* @author Max Shaposhnyk (mshaposh@redhat.com)
*/
@@ -51,6 +56,9 @@ public abstract class MultiUserEnvironmentInitializationFilter implements Fil
private static final Logger LOG =
LoggerFactory.getLogger(MultiUserEnvironmentInitializationFilter.class);
+ private static final List UNAUTHORIZED_ENDPOINT_PATHS =
+ Collections.singletonList("/system/state");
+
private final SessionStore sessionStore;
private final RequestTokenExtractor tokenExtractor;
@@ -197,9 +205,23 @@ public abstract class MultiUserEnvironmentInitializationFilter implements Fil
* @throws IOException inherited from {@link FilterChain#doFilter}
* @throws ServletException inherited from {@link FilterChain#doFilter}
*/
- protected abstract void handleMissingToken(
+ protected void handleMissingToken(
ServletRequest request, ServletResponse response, FilterChain chain)
- throws IOException, ServletException;
+ throws IOException, ServletException {
+ // if request path is in unauthorized endpoints, continue
+ if (request instanceof HttpServletRequest) {
+ HttpServletRequest httpRequest = (HttpServletRequest) request;
+ String path = httpRequest.getServletPath();
+ if (UNAUTHORIZED_ENDPOINT_PATHS.contains(path)) {
+ LOG.debug("Allowing request to '{}' without authorization header.", path);
+ chain.doFilter(request, response);
+ return;
+ }
+ }
+
+ LOG.error("Rejecting the request due to missing/expired token in Authorization header.");
+ sendError(response, 401, "Authorization token is missing or expired");
+ }
/**
* Sends appropriate error status code and message into response.
diff --git a/multiuser/api/che-multiuser-api-authentication-commons/src/test/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilterTest.java b/multiuser/api/che-multiuser-api-authentication-commons/src/test/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilterTest.java
index 62847c8e7c..10c94ddc94 100644
--- a/multiuser/api/che-multiuser-api-authentication-commons/src/test/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilterTest.java
+++ b/multiuser/api/che-multiuser-api-authentication-commons/src/test/java/org/eclipse/che/multiuser/api/authentication/commons/filter/MultiUserEnvironmentInitializationFilterTest.java
@@ -25,9 +25,11 @@ import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;
import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
+import java.io.IOException;
import java.util.Optional;
import org.eclipse.che.commons.env.EnvironmentContext;
import org.eclipse.che.commons.subject.Subject;
@@ -83,6 +85,7 @@ public class MultiUserEnvironmentInitializationFilterTest {
// then
verify(tokenExtractor).getToken(eq(request));
verify(filter).handleMissingToken(eq(request), eq(response), eq(chain));
+ verify(request).getServletPath();
verifyNoMoreInteractions(request);
verify(filter, never()).getUserId(any());
verify(filter, never()).extractSubject(anyString(), any());
@@ -100,6 +103,7 @@ public class MultiUserEnvironmentInitializationFilterTest {
// then
verify(tokenExtractor).getToken(eq(request));
verify(filter).handleMissingToken(eq(request), eq(response), eq(chain));
+ verify(request).getServletPath();
verifyNoMoreInteractions(request);
verify(filter, never()).getUserId(any());
verify(filter, never()).extractSubject(anyString(), any());
@@ -168,4 +172,23 @@ public class MultiUserEnvironmentInitializationFilterTest {
// then
verify(context).setSubject(eq(subject));
}
+
+ @Test
+ public void handleMissingTokenShouldAllowUnauthorizedEndpoint()
+ throws ServletException, IOException {
+ when(request.getServletPath()).thenReturn("/system/state");
+
+ filter.handleMissingToken(request, response, chain);
+
+ verify(chain).doFilter(request, response);
+ }
+
+ @Test
+ public void handleMissingTokenShouldRejectRequest() throws ServletException, IOException {
+ when(request.getServletPath()).thenReturn("blabol");
+
+ filter.handleMissingToken(request, response, chain);
+
+ verify(response).sendError(eq(401), anyString());
+ }
}
diff --git a/multiuser/keycloak/che-multiuser-keycloak-server/pom.xml b/multiuser/keycloak/che-multiuser-keycloak-server/pom.xml
index efc65be2bf..e1dfe7620e 100644
--- a/multiuser/keycloak/che-multiuser-keycloak-server/pom.xml
+++ b/multiuser/keycloak/che-multiuser-keycloak-server/pom.xml
@@ -30,14 +30,6 @@
com.auth0
jwks-rsa
-