Skip to content

Commit

Permalink
CNS-2801 - Minimize the operator's RBAC access (#163)
Browse files Browse the repository at this point in the history
* Change namespace to be a value on the Controller and StateApplier instead of coming from agentSpec. Modify objects to take the value from their own fields.

* Add env var to pass the operator's namespace into the code and use it for the agent

* Add a description to the Namespace field to indicate it is deprecated and will not have any effect.

* Reduce operator workload permissions to a role instead of ClusterRole

* Move dataplane RBAC to a subfolder to separate operator vs dataplane

* Commit generated file diff

* Add dataplane files to the kustomization

* Remove duplicated Dataplane items

* Keep the old ClusterRoleBinding name to avoid duplicates if redeploying a newer operator. Rename the local RoleBinding instead to avoid conflicts.

* Restrict PriorityClass RBAC

* Restrict webhook RBAC by resource name when possible

* Move dataplane RBAC objects to the operator chart - so the agent chart mimics deploying a CR with secret

* Update Readmes to match the "same-namespace" enforcement. Remove references to repositories that we don't support. Added the missing labeling when pre-creating a namespace.

* Sync operator.yaml in the chart with RBAC changes and some missing items from last releases

* Bump charts version due to breaking changes. Bumped app version there as well to be latest so far.

* Minor comment change

* Added some explanation behind the RBAC setup and how to update it

* Update deployment.yaml to also mount the namespace env var

* Replace namespace var in dataplane_rbac.yaml

* Added deprecation notice as godoc to CRD.Namespace

* Add Namespace to each component's constructor. Don't set the namespace on the k8s object as it has no affect (NamespacedName is what sets the namespace).

* Removed the public registry secret from all dataplane accounts since they shouldn't need it
  • Loading branch information
ltsonov-cb authored Jul 11, 2023
1 parent 8d296ec commit b83d0be
Show file tree
Hide file tree
Showing 42 changed files with 603 additions and 439 deletions.
6 changes: 6 additions & 0 deletions api/v1/cbcontainersagent_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ type CBContainersAgentSpec struct {
ClusterName string `json:"clusterName,required"`
Version string `json:"version,required"`
Gateways CBContainersGatewaysSpec `json:"gateways,required"`
// The field below remains to avoid moving the CRD from v1 to v2.
// It MUST not be used as agent namespace should be controlled outside the operator itself.
// This is because a custom namespace in the CRD requires high privileges by the operator across the whole cluster to be able to "switch" namespaces on demand.

// +kubebuilder:default:="cbcontainers-dataplane"
// Namespace is deprecated and the value has no effect. Do not use.
// Deprecated: The operator and agent always run in the same namespace. See documentation for ways to customize this namespace.
Namespace string `json:"namespace,omitempty"`
// +kubebuilder:default:="cbcontainers-access-token"
AccessTokenSecretName string `json:"accessTokenSecretName,omitempty"`
Expand Down
7 changes: 3 additions & 4 deletions cbcontainers/state/components/cluster_configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ type ConfigurationK8sObject struct {
Namespace string
}

func NewConfigurationK8sObject() *ConfigurationK8sObject {
func NewConfigurationK8sObject(namespace string) *ConfigurationK8sObject {
return &ConfigurationK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -35,12 +35,11 @@ func (obj *ConfigurationK8sObject) MutateK8sObject(k8sObject client.Object, agen
return fmt.Errorf("expected ConfigMap K8s object")
}

configMap.Namespace = agentSpec.Namespace
configMap.Data = map[string]string{
commonState.DataPlaneConfigmapAccountKey: agentSpec.Account,
commonState.DataPlaneConfigmapClusterKey: agentSpec.ClusterName,
commonState.DataPlaneConfigmapAgentVersionKey: agentSpec.Version,
commonState.DataPlaneConfigmapDataplaneNamespaceKey: agentSpec.Namespace,
commonState.DataPlaneConfigmapDataplaneNamespaceKey: obj.Namespace,
commonState.DataPlaneConfigmapApiSchemeKey: agentSpec.Gateways.ApiGateway.Scheme,
commonState.DataPlaneConfigmapApiHostKey: agentSpec.Gateways.ApiGateway.Host,
commonState.DataPlaneConfigmapApiPortKey: strconv.Itoa(agentSpec.Gateways.ApiGateway.Port),
Expand Down
6 changes: 2 additions & 4 deletions cbcontainers/state/components/enforcer_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ type EnforcerDeploymentK8sObject struct {
Namespace string
}

func NewEnforcerDeploymentK8sObject() *EnforcerDeploymentK8sObject {
func NewEnforcerDeploymentK8sObject(namespace string) *EnforcerDeploymentK8sObject {
return &EnforcerDeploymentK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand Down Expand Up @@ -84,8 +84,6 @@ func (obj *EnforcerDeploymentK8sObject) MutateK8sObject(k8sObject client.Object,
if objectsDiffer(deployment.Spec.Template.Spec.ImagePullSecrets, desiredImagePullSecrets) {
deployment.Spec.Template.Spec.ImagePullSecrets = desiredImagePullSecrets
}
obj.Namespace = agentSpec.Namespace
deployment.Namespace = agentSpec.Namespace
obj.mutateAnnotations(deployment, enforcer)
obj.mutateVolumes(&deployment.Spec.Template.Spec)
obj.mutateAffinityAndNodeSelector(&deployment.Spec.Template.Spec, enforcer)
Expand Down
14 changes: 7 additions & 7 deletions cbcontainers/state/components/enforcer_mutating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ type EnforcerMutatingWebhookK8sObject struct {
ServiceNamespace string
}

func NewEnforcerMutatingWebhookK8sObject(kubeletVersion string) *EnforcerMutatingWebhookK8sObject {
func NewEnforcerMutatingWebhookK8sObject(serviceNamespace, kubeletVersion string) *EnforcerMutatingWebhookK8sObject {
return &EnforcerMutatingWebhookK8sObject{
kubeletVersion: kubeletVersion,
ServiceNamespace: commonState.DataPlaneNamespaceName,
ServiceNamespace: serviceNamespace,
}
}

Expand Down Expand Up @@ -65,10 +65,10 @@ func (obj *EnforcerMutatingWebhookK8sObject) MutateK8sObject(k8sObject client.Ob

enforcer := &agentSpec.Components.Basic.Enforcer
obj.mutateWebhookConfigurationLabels(webhookConfiguration, enforcer)
return obj.mutateWebhooks(webhookConfiguration, enforcer, agentSpec.Namespace)
return obj.mutateWebhooks(webhookConfiguration, enforcer)
}

func (obj *EnforcerMutatingWebhookK8sObject) mutateWebhooks(webhookConfiguration adapters.WebhookConfigurationAdapter, enforcer *cbcontainersv1.CBContainersEnforcerSpec, serviceNamespace string) error {
func (obj *EnforcerMutatingWebhookK8sObject) mutateWebhooks(webhookConfiguration adapters.WebhookConfigurationAdapter, enforcer *cbcontainersv1.CBContainersEnforcerSpec) error {
var resourcesWebhookObj adapters.WebhookAdapter

initializeWebhooks := false
Expand All @@ -93,7 +93,7 @@ func (obj *EnforcerMutatingWebhookK8sObject) mutateWebhooks(webhookConfiguration
resourcesWebhookObj = updatedWebhooks[0]
}

obj.mutateResourcesWebhook(resourcesWebhookObj, enforcer.WebhookTimeoutSeconds, enforcer.FailurePolicy, serviceNamespace)
obj.mutateResourcesWebhook(resourcesWebhookObj, enforcer.WebhookTimeoutSeconds, enforcer.FailurePolicy)
return nil
}

Expand All @@ -107,7 +107,7 @@ func (obj *EnforcerMutatingWebhookK8sObject) findWebhookByName(webhooks []adapte
return nil, false
}

func (obj *EnforcerMutatingWebhookK8sObject) mutateResourcesWebhook(resourcesWebhook adapters.WebhookAdapter, timeoutSeconds int32, failurePolicy, serviceNamespace string) {
func (obj *EnforcerMutatingWebhookK8sObject) mutateResourcesWebhook(resourcesWebhook adapters.WebhookAdapter, timeoutSeconds int32, failurePolicy string) {
resourcesWebhook.SetName(MutatingWebhookName)
resourcesWebhook.SetFailurePolicy(failurePolicy)
resourcesWebhook.SetSideEffects(MutatingWebhookSideEffect)
Expand All @@ -123,7 +123,7 @@ func (obj *EnforcerMutatingWebhookK8sObject) mutateResourcesWebhook(resourcesWeb
}
resourcesWebhook.SetCABundle(obj.tlsSecretValues.CaCert)
resourcesWebhook.SetServiceName(EnforcerName)
resourcesWebhook.SetServiceNamespace(serviceNamespace)
resourcesWebhook.SetServiceNamespace(obj.ServiceNamespace)
resourcesWebhook.SetServicePath(&MutatingWebhookPath)
}

Expand Down
6 changes: 2 additions & 4 deletions cbcontainers/state/components/enforcer_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

cbcontainersv1 "github.com/vmware/cbcontainers-operator/api/v1"
commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -21,9 +20,9 @@ type EnforcerServiceK8sObject struct {
Namespace string
}

func NewEnforcerServiceK8sObject() *EnforcerServiceK8sObject {
func NewEnforcerServiceK8sObject(namespace string) *EnforcerServiceK8sObject {
return &EnforcerServiceK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -45,7 +44,6 @@ func (obj *EnforcerServiceK8sObject) MutateK8sObject(k8sObject client.Object, ag

service.Labels = enforcer.Labels
service.Spec.Type = coreV1.ServiceTypeClusterIP
service.Namespace = agentSpec.Namespace
service.Spec.Selector = map[string]string{
EnforcerLabelKey: EnforcerName,
}
Expand Down
6 changes: 2 additions & 4 deletions cbcontainers/state/components/enforcer_tls_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

cbcontainersv1 "github.com/vmware/cbcontainers-operator/api/v1"
"github.com/vmware/cbcontainers-operator/cbcontainers/models"
commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand All @@ -26,10 +25,10 @@ type EnforcerTlsK8sObject struct {
Namespace string
}

func NewEnforcerTlsK8sObject(tlsSecretsValuesCreator TlsSecretsValuesCreator) *EnforcerTlsK8sObject {
func NewEnforcerTlsK8sObject(namespace string, tlsSecretsValuesCreator TlsSecretsValuesCreator) *EnforcerTlsK8sObject {
return &EnforcerTlsK8sObject{
tlsSecretsValuesCreator: tlsSecretsValuesCreator,
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -47,7 +46,6 @@ func (obj *EnforcerTlsK8sObject) MutateK8sObject(k8sObject client.Object, spec *
return fmt.Errorf("expected Secret K8s object")
}

secret.Namespace = spec.Namespace
tlsSecretValues, err := obj.tlsSecretsValuesCreator.CreateTlsSecretsValues(types.NamespacedName{Name: EnforcerName, Namespace: obj.Namespace})
if err != nil {
return err
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/enforcer_validating_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
cbcontainersv1 "github.com/vmware/cbcontainers-operator/api/v1"
"github.com/vmware/cbcontainers-operator/cbcontainers/models"
"github.com/vmware/cbcontainers-operator/cbcontainers/state/adapters"
commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
"github.com/vmware/cbcontainers-operator/cbcontainers/utils"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -36,10 +35,10 @@ type EnforcerValidatingWebhookK8sObject struct {
ServiceNamespace string
}

func NewEnforcerValidatingWebhookK8sObject(kubeletVersion string) *EnforcerValidatingWebhookK8sObject {
func NewEnforcerValidatingWebhookK8sObject(serviceNamespace, kubeletVersion string) *EnforcerValidatingWebhookK8sObject {
return &EnforcerValidatingWebhookK8sObject{
kubeletVersion: kubeletVersion,
ServiceNamespace: commonState.DataPlaneNamespaceName,
ServiceNamespace: serviceNamespace,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ type ImageScanningReporterDeploymentK8sObject struct {
Namespace string
}

func NewImageScanningReporterDeploymentK8sObject() *ImageScanningReporterDeploymentK8sObject {
func NewImageScanningReporterDeploymentK8sObject(namespace string) *ImageScanningReporterDeploymentK8sObject {
return &ImageScanningReporterDeploymentK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -55,7 +55,6 @@ func (obj *ImageScanningReporterDeploymentK8sObject) MutateK8sObject(k8sObject c
}

clusterScanning := &agentSpec.Components.ClusterScanning
deployment.Namespace = agentSpec.Namespace
imageScanningReporter := &clusterScanning.ImageScanningReporter
obj.initiateDeployment(deployment, agentSpec)
obj.mutateLabels(deployment, imageScanningReporter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

cbcontainersv1 "github.com/vmware/cbcontainers-operator/api/v1"
commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -21,9 +20,9 @@ type ImageScanningReporterServiceK8sObject struct {
Namespace string
}

func NewImageScanningReporterServiceK8sObject() *ImageScanningReporterServiceK8sObject {
func NewImageScanningReporterServiceK8sObject(namespace string) *ImageScanningReporterServiceK8sObject {
return &ImageScanningReporterServiceK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -43,7 +42,6 @@ func (obj *ImageScanningReporterServiceK8sObject) MutateK8sObject(k8sObject clie

imageScanningReporter := &agentSpec.Components.ClusterScanning.ImageScanningReporter

service.Namespace = agentSpec.Namespace
service.Labels = imageScanningReporter.Labels
service.Spec.Type = coreV1.ServiceTypeClusterIP
service.Spec.Selector = map[string]string{
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/monitor_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type MonitorDeploymentK8sObject struct {
Namespace string
}

func NewMonitorDeploymentK8sObject() *MonitorDeploymentK8sObject {
func NewMonitorDeploymentK8sObject(namespace string) *MonitorDeploymentK8sObject {
return &MonitorDeploymentK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand Down Expand Up @@ -76,7 +76,6 @@ func (obj *MonitorDeploymentK8sObject) MutateK8sObject(k8sObject client.Object,
deployment.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
}

deployment.Namespace = agentSpec.Namespace
deployment.Spec.Replicas = &MonitorReplicas
deployment.ObjectMeta.Labels = desiredLabels
deployment.Spec.Selector.MatchLabels = desiredLabels
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/registry_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type RegistrySecretK8sObject struct {
Namespace string
}

func NewRegistrySecretK8sObject() *RegistrySecretK8sObject {
func NewRegistrySecretK8sObject(namespace string) *RegistrySecretK8sObject {
return &RegistrySecretK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -46,7 +46,6 @@ func (obj *RegistrySecretK8sObject) MutateK8sObject(k8sObject client.Object, spe

secret.Type = obj.registrySecretValues.Type
secret.Data = obj.registrySecretValues.Data
secret.Namespace = spec.Namespace

return nil
}
6 changes: 2 additions & 4 deletions cbcontainers/state/components/resolver_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"

cbcontainersv1 "github.com/vmware/cbcontainers-operator/api/v1"
commonState "github.com/vmware/cbcontainers-operator/cbcontainers/state/common"
coreV1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand All @@ -20,9 +19,9 @@ type ResolverServiceK8sObject struct {
Namespace string
}

func NewResolverServiceK8sObject() *ResolverServiceK8sObject {
func NewResolverServiceK8sObject(namespace string) *ResolverServiceK8sObject {
return &ResolverServiceK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand All @@ -46,7 +45,6 @@ func (obj *ResolverServiceK8sObject) MutateK8sObject(k8sObject client.Object, ag
service.Spec.Type = coreV1.ServiceTypeClusterIP
service.Spec.ClusterIP = coreV1.ClusterIPNone

service.Namespace = agentSpec.Namespace
service.Spec.Selector = map[string]string{
resolverLabelKey: ResolverName,
}
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/runtime_resolver_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ type ResolverDeploymentK8sObject struct {
APIReader client.Reader
}

func NewResolverDeploymentK8sObject(apiReader client.Reader) *ResolverDeploymentK8sObject {
func NewResolverDeploymentK8sObject(namespace string, apiReader client.Reader) *ResolverDeploymentK8sObject {
return &ResolverDeploymentK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
APIReader: apiReader,
}
}
Expand Down Expand Up @@ -89,7 +89,6 @@ func (obj *ResolverDeploymentK8sObject) MutateK8sObject(k8sObject client.Object,
}
}

deployment.Namespace = agentSpec.Namespace
deployment.Spec.Replicas = replicasCount
deployment.ObjectMeta.Labels = desiredLabels
deployment.Spec.Selector.MatchLabels = desiredLabels
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/sensor_daemon_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ type SensorDaemonSetK8sObject struct {
Namespace string
}

func NewSensorDaemonSetK8sObject() *SensorDaemonSetK8sObject {
func NewSensorDaemonSetK8sObject(namespace string) *SensorDaemonSetK8sObject {
return &SensorDaemonSetK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand Down Expand Up @@ -131,7 +131,6 @@ func (obj *SensorDaemonSetK8sObject) MutateK8sObject(k8sObject client.Object, ag
daemonSet.Spec.Template.Spec.HostPID = false
}

daemonSet.Namespace = agentSpec.Namespace
obj.mutateLabels(daemonSet, agentSpec)
obj.mutateAnnotations(daemonSet, agentSpec)
obj.mutateVolumes(daemonSet, agentSpec)
Expand Down
5 changes: 2 additions & 3 deletions cbcontainers/state/components/state_reporter_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ type StateReporterDeploymentK8sObject struct {
Namespace string
}

func NewStateReporterDeploymentK8sObject() *StateReporterDeploymentK8sObject {
func NewStateReporterDeploymentK8sObject(namespace string) *StateReporterDeploymentK8sObject {
return &StateReporterDeploymentK8sObject{
Namespace: commonState.DataPlaneNamespaceName,
Namespace: namespace,
}
}

Expand Down Expand Up @@ -72,7 +72,6 @@ func (obj *StateReporterDeploymentK8sObject) MutateK8sObject(k8sObject client.Ob
deployment.Spec.Template.ObjectMeta.Annotations = make(map[string]string)
}

deployment.Namespace = agentSpec.Namespace
deployment.Spec.Replicas = &StateReporterReplicas
deployment.ObjectMeta.Labels = desiredLabels
deployment.Spec.Selector.MatchLabels = desiredLabels
Expand Down
Loading

0 comments on commit b83d0be

Please sign in to comment.