KubernetesNamespaceFactory { public void checkIfNamespaceIsAllowed(String namespaceName) throws ValidationException { if (allowUserDefinedNamespaces) { return; } String defaultNamespace = evalPlaceholders(defaultNamespaceName, EnvironmentContext.getCurrent().getSubject(), null); if (!namespaceName.equals(defaultNamespace)) { throw new ValidationException( format( "User defined namespaces are not allowed. Only the default namespace '%s' is available.", defaultNamespaceName)); } } @Inject  KubernetesNamespaceFactory(
      @Nullable @Named("che.infra.kubernetes.namespace") String legacyNamespaceName,
      @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.allow_user_defined")
          boolean allowUserDefinedNamespaces,
      @Named("che.infra.kubernetes.namespace.creation_allowed") boolean namespaceCreationAllowed,
      KubernetesClientFactory clientFactory,
      UserManager userManager,
      PreferenceManager preferenceManager,
      KubernetesSharedPool sharedPool); KubernetesNamespace access(String workspaceId, String namespace); void checkIfNamespaceIsAllowed(String namespaceName); List<KubernetesNamespaceMeta> list(); KubernetesNamespace getOrCreate(RuntimeIdentity identity); KubernetesNamespace get(RuntimeIdentity identity); KubernetesNamespace get(Workspace workspace); String evaluateLegacyNamespaceName(NamespaceResolutionContext resolutionCtx); String evaluateNamespaceName(NamespaceResolutionContext resolutionCtx); void deleteIfManaged(Workspace workspace);  }
@Test public void shouldNotThrowExceptionIfDefaultNamespaceIsSpecifiedOnCheckingIfNamespaceIsAllowed() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "legacy", "", "", "defaultNs", false, true, clientFactory, userManager, preferenceManager, pool); namespaceFactory.checkIfNamespaceIsAllowed("defaultNs"); }
@Test public void shouldNotThrowExceptionIfNonDefaultNamespaceIsSpecifiedAndUserDefinedAreAllowedOnCheckingIfNamespaceIsAllowed() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "legacy", "", "", "defaultNs", true, true, clientFactory, userManager, preferenceManager, pool); namespaceFactory.checkIfNamespaceIsAllowed("any-namespace"); }
@Test( expectedExceptions = ValidationException.class, expectedExceptionsMessageRegExp = "User defined namespaces are not allowed. Only the default namespace 'defaultNs' is available.") public void shouldThrowExceptionIfNonDefaultNamespaceIsSpecifiedAndUserDefinedAreNotAllowedOnCheckingIfNamespaceIsAllowed() throws Exception { namespaceFactory = new KubernetesNamespaceFactory( "legacy", "", "", "defaultNs", false, true, clientFactory, userManager, preferenceManager, pool); namespaceFactory.checkIfNamespaceIsAllowed("any-namespace"); }