From f8665698b3dc53dc8c989fcde62a49cd161d5ace Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Thu, 3 Dec 2026 01:01:09 +0530 Subject: [PATCH] fix: resolve workspace-referenced DWOC only from the operator namespace The controller.devfile.io/devworkspace-config attribute let a DevWorkspace reference a DevWorkspaceOperatorConfig in any namespace, which was then merged over the global config. Operator configuration is meant to be administrator-controlled, so it should only be read from a trusted location. Resolve the referenced config's name only from the operator installation namespace, ignoring the namespace supplied in the attribute. The attribute is retained for backward compatibility so existing workspace manifests stay valid; its namespace field is now optional, and a config in any other namespace (including the workspace's own) is no longer applied. Requires the che-operator counterpart to be rolled out first: https://github.com/eclipse-che/che-operator/pull/2291 Assisted-by: Claude Code Signed-off-by: Rohan Kumar --- docs/additional-configuration.adoc | 8 +-- docs/dwo-configuration.md | 6 +- pkg/config/common_test.go | 2 +- pkg/config/sync.go | 14 ++-- pkg/config/sync_test.go | 102 ++++++++++++++++++++++++++++- pkg/constants/attributes.go | 8 +-- 6 files changed, 119 insertions(+), 21 deletions(-) diff --git a/docs/additional-configuration.adoc b/docs/additional-configuration.adoc index 9df10c1ba..e2f603e1f 100644 --- a/docs/additional-configuration.adoc +++ b/docs/additional-configuration.adoc @@ -336,11 +336,10 @@ This will mount the files in the `git-ssh-key` secret to `/etc/ssh/`, creating f ## Setting an alternate configuration for a workspace It is possible to configure a workspace to use an alternate DevWorkspaceOperatorConfig. -In order to do so, the alternate DevWorkspaceOperatorConfig must exist on the cluster, and the `controller.devfile.io/devworkspace-config` workspace attribute must be set. -The `controller.devfile.io/devworkspace-config` attribute takes two string fields: `name` and `namespace`. +The alternate DevWorkspaceOperatorConfig must exist in the namespace where the DevWorkspace Operator is installed, and the `controller.devfile.io/devworkspace-config` workspace attribute must be set. +The `controller.devfile.io/devworkspace-config` attribute takes one string field: `name`. -* `name`: the `metadata.name` of the alternate DevWorkspaceOperatorConfig. -* `namespace`: the `metadata.namespace` of the alternate DevWorkspaceOperatorConfig. +* `name`: the `metadata.name` of the alternate DevWorkspaceOperatorConfig. The operator reads that object from its own installation namespace. A DevWorkspaceOperatorConfig in any other namespace, including the workspace namespace, is not applied. [source,yaml] ---- @@ -353,7 +352,6 @@ spec: attributes: controller.devfile.io/devworkspace-config: name: - namespace: ---- *Note:* the alternate DevWorkspaceOperatorConfig will be diff --git a/docs/dwo-configuration.md b/docs/dwo-configuration.md index 77f283766..787e1b4ce 100644 --- a/docs/dwo-configuration.md +++ b/docs/dwo-configuration.md @@ -31,7 +31,8 @@ config: ### DevWorkspace specific configuration To apply a configuration to a specific `DevWorkspace` instead of globally, an existing `DevWorkspaceOperatorConfig` can -be referenced in a `DevWorkspace`'s attributes: +be referenced in a `DevWorkspace`'s attributes. The named object is always read from the operator installation +namespace. ```yaml apiVersion: workspace.devfile.io/v1alpha2 kind: DevWorkspace @@ -41,8 +42,7 @@ spec: template: attributes: controller.devfile.io/devworkspace-config: - name: - namespace: + name: ``` Configuration specified as above will be merged into the default global configuration, overriding any values present. Note that fields set in the global configuration cannot be cleared (reset to unset) by a workspace-specific diff --git a/pkg/config/common_test.go b/pkg/config/common_test.go index 07565f4f5..dd924b81c 100644 --- a/pkg/config/common_test.go +++ b/pkg/config/common_test.go @@ -80,7 +80,7 @@ func buildExternalConfig(config *v1alpha1.OperatorConfiguration) *v1alpha1.DevWo return &v1alpha1.DevWorkspaceOperatorConfig{ ObjectMeta: metav1.ObjectMeta{ Name: externalConfigName, - Namespace: externalConfigNamespace, + Namespace: testNamespace, }, Config: config, } diff --git a/pkg/config/sync.go b/pkg/config/sync.go index 1faf0baa6..0b0515dd1 100644 --- a/pkg/config/sync.go +++ b/pkg/config/sync.go @@ -60,7 +60,11 @@ func GetGlobalConfig() *controller.OperatorConfiguration { // DevWorkspaceOperatorConfig specified by the optional workspace attribute `controller.devfile.io/devworkspace-config`. // If the `controller.devfile.io/devworkspace-config` is not set, the global DevWorkspaceOperatorConfig is returned. // If the `controller.devfile.io/devworkspace-config` attribute is incorrectly set, or the specified DevWorkspaceOperatorConfig -// does not exist on the cluster, an error is returned. +// does not exist in the operator namespace, an error is returned. +// +// The attribute's name is honored. Its namespace is optional and ignored: the named config is always read from the +// operator installation namespace. +// See adr/2026-12-02-trusted-devworkspace-config-resolution.md. func ResolveConfigForWorkspace(workspace *dw.DevWorkspace, client crclient.Client) (*controller.OperatorConfiguration, error) { if !workspace.Spec.Template.Attributes.Exists(constants.ExternalDevWorkspaceConfiguration) { return GetGlobalConfig(), nil @@ -76,14 +80,16 @@ func ResolveConfigForWorkspace(workspace *dw.DevWorkspace, client crclient.Clien return nil, fmt.Errorf("'name' must be set for attribute %s in DevWorkspace attributes", constants.ExternalDevWorkspaceConfiguration) } - if namespacedName.Namespace == "" { - return nil, fmt.Errorf("'namespace' must be set for attribute %s in DevWorkspace attributes", constants.ExternalDevWorkspaceConfiguration) + if configNamespace == "" { + return nil, fmt.Errorf("failed to resolve attribute %s: operator namespace is not configured", constants.ExternalDevWorkspaceConfiguration) } + attributeNamespace := namespacedName.Namespace + namespacedName.Namespace = configNamespace externalDWOC := &controller.DevWorkspaceOperatorConfig{} err = client.Get(context.TODO(), namespacedName, externalDWOC) if err != nil { - return nil, fmt.Errorf("could not fetch external DWOC with name %s in namespace %s: %w", namespacedName.Name, namespacedName.Namespace, err) + return nil, fmt.Errorf("could not fetch DevWorkspaceOperatorConfig %q from operator namespace %q (attribute namespace %q is ignored): %w", namespacedName.Name, configNamespace, attributeNamespace, err) } return getMergedConfig(externalDWOC.Config, internalConfig), nil } diff --git a/pkg/config/sync_test.go b/pkg/config/sync_test.go index 82e875796..01d4084b9 100644 --- a/pkg/config/sync_test.go +++ b/pkg/config/sync_test.go @@ -1,5 +1,5 @@ // -// Copyright (c) 2019-2025 Red Hat, Inc. +// Copyright (c) 2019-2026 Red Hat, Inc. // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -29,6 +29,7 @@ import ( appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" networkingv1 "k8s.io/api/networking/v1" + k8sErrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" @@ -155,6 +156,95 @@ func TestCatchesNonExistentExternalDWOC(t *testing.T) { assert.Equal(t, resolvedConfig, internalConfig, "Internal/Global DWOC should be used as fallback if external DWOC could not be resolved") } +func TestResolveConfigForWorkspaceIgnoresAttributeNamespace(t *testing.T) { + setupForTest(t) + + workspace := &dw.DevWorkspace{} + workspaceAttributes := attributes.Attributes{} + workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, types.NamespacedName{ + Name: externalConfigName, + Namespace: externalConfigNamespace, + }, nil) + workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{ + Attributes: workspaceAttributes, + } + + tenantConfig := buildExternalConfig(defaultConfig.DeepCopy()) + tenantConfig.Namespace = externalConfigNamespace + tenantConfig.Config.Workspace.ImagePullPolicy = "Never" + + client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(tenantConfig).Build() + resolvedConfig, err := ResolveConfigForWorkspace(workspace, client) + if assert.Error(t, err, "A DevWorkspaceOperatorConfig that exists only in the attribute namespace must not be applied") { + assert.True(t, k8sErrors.IsNotFound(err), "Lookup should miss in the operator namespace, got: %s", err) + } + assert.Nil(t, resolvedConfig) + + operatorConfig := buildExternalConfig(defaultConfig.DeepCopy()) + operatorConfig.Config.Workspace.ImagePullPolicy = "Always" + err = client.Create(context.Background(), operatorConfig) + if !assert.NoError(t, err) { + return + } + + SetGlobalConfigForTesting(defaultConfig.DeepCopy()) + resolvedConfig, err = ResolveConfigForWorkspace(workspace, client) + if !assert.NoError(t, err, "The same name in the operator namespace should resolve") { + return + } + assert.Equal(t, "Always", resolvedConfig.Workspace.ImagePullPolicy) +} + +func TestResolveConfigForWorkspaceWithoutAttributeNamespace(t *testing.T) { + setupForTest(t) + + workspace := &dw.DevWorkspace{} + workspaceAttributes := attributes.Attributes{} + workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, map[string]string{ + "name": externalConfigName, + }, nil) + workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{ + Attributes: workspaceAttributes, + } + + operatorConfig := buildExternalConfig(defaultConfig.DeepCopy()) + operatorConfig.Config.Workspace.ImagePullPolicy = "Always" + client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(operatorConfig).Build() + + SetGlobalConfigForTesting(defaultConfig.DeepCopy()) + resolvedConfig, err := ResolveConfigForWorkspace(workspace, client) + if !assert.NoError(t, err, "The attribute namespace is ignored, so omitting it should not be an error") { + return + } + assert.Equal(t, "Always", resolvedConfig.Workspace.ImagePullPolicy) +} + +func TestResolveConfigForWorkspaceFailsWithoutOperatorNamespace(t *testing.T) { + setupForTest(t) + configNamespace = "" + + workspace := &dw.DevWorkspace{} + workspaceAttributes := attributes.Attributes{} + workspaceAttributes.Put(constants.ExternalDevWorkspaceConfiguration, types.NamespacedName{ + Name: externalConfigName, + Namespace: externalConfigNamespace, + }, nil) + workspace.Spec.Template.DevWorkspaceTemplateSpecContent = dw.DevWorkspaceTemplateSpecContent{ + Attributes: workspaceAttributes, + } + + // Present in the attribute namespace: must not be reachable when the operator namespace is unknown. + tenantConfig := buildExternalConfig(defaultConfig.DeepCopy()) + tenantConfig.Namespace = externalConfigNamespace + client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(tenantConfig).Build() + + resolvedConfig, err := ResolveConfigForWorkspace(workspace, client) + if assert.Error(t, err, "Resolution must fail when the operator namespace is not configured") { + assert.Contains(t, err.Error(), "operator namespace is not configured") + } + assert.Nil(t, resolvedConfig) +} + func TestMergeExternalConfig(t *testing.T) { setupForTest(t) @@ -181,7 +271,13 @@ func TestMergeExternalConfig(t *testing.T) { } clusterConfig := buildConfig(defaultConfig.DeepCopy()) - client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, externalConfig).Build() + // Same name in the attribute's namespace. Must not be merged: resolution is forced + // into the operator namespace. + decoyConfig := externalConfig.DeepCopy() + decoyConfig.Namespace = externalConfigNamespace + decoyConfig.Config.Workspace.ImagePullPolicy = "Never" + decoyConfig.Config.Workspace.PVCName = "tenant-controlled-pvc" + client := fake.NewClientBuilder().WithScheme(scheme).WithObjects(clusterConfig, externalConfig, decoyConfig).Build() err := SetupControllerConfig(client) if !assert.NoError(t, err, "Should not return error") { return @@ -716,7 +812,7 @@ func TestResolveConfigForWorkspaceWithNetworkPolicy(t *testing.T) { externalDwoc := &v1alpha1.DevWorkspaceOperatorConfig{ ObjectMeta: metav1.ObjectMeta{ Name: externalConfigName, - Namespace: externalConfigNamespace, + Namespace: configNamespace, }, Config: &v1alpha1.OperatorConfiguration{ Workspace: &v1alpha1.WorkspaceConfig{ diff --git a/pkg/constants/attributes.go b/pkg/constants/attributes.go index fb03df339..de4b1865f 100644 --- a/pkg/constants/attributes.go +++ b/pkg/constants/attributes.go @@ -29,18 +29,16 @@ const ( DevWorkspaceStorageTypeAttribute = "controller.devfile.io/storage-type" // ExternalDevWorkspaceConfiguration is an attribute that allows for specifying an (optional) external DevWorkspaceOperatorConfig - // which will merged with the internal/global DevWorkspaceOperatorConfig. The DevWorkspaceOperatorConfig resulting from the merge will be used for the workspace. + // which will be merged with the internal/global DevWorkspaceOperatorConfig. The DevWorkspaceOperatorConfig resulting from the merge will be used for the workspace. // The fields which are set in the external DevWorkspaceOperatorConfig will overwrite those existing in the // internal/global DevWorkspaceOperatorConfig during the merge. - // The structure of the attribute value should contain two strings: name and namespace. - // 'name' specifies the metadata.name of the external operator configuration. - // 'namespace' specifies the metadata.namespace of the external operator configuration . + // 'name' specifies the metadata.name of the external operator configuration, which is always read from the + // operator installation namespace. A 'namespace' field may be present in existing manifests; it is ignored. // For example: // // attributes: // controller.devfile.io/devworkspace-config: // name: external-dwoc-name - // namespace: some-namespace ExternalDevWorkspaceConfiguration = "controller.devfile.io/devworkspace-config" // RuntimeClassNameAttribute is an attribute added to a DevWorkspace to specify a runtimeClassName for container