From 26446354c54169bdad4ae5613d2611a373d50596 Mon Sep 17 00:00:00 2001 From: Tomer Shefler Date: Sun, 14 Jan 2024 15:12:31 +0200 Subject: [PATCH 1/3] Add advanced settings to remote update --- .../models/remote_configuration_changes.go | 14 ++++++++++---- .../remote_configuration/change_applier.go | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/cbcontainers/models/remote_configuration_changes.go b/cbcontainers/models/remote_configuration_changes.go index 91211953..a2f4e64c 100644 --- a/cbcontainers/models/remote_configuration_changes.go +++ b/cbcontainers/models/remote_configuration_changes.go @@ -8,11 +8,17 @@ var ( ChangeStatusFailed RemoteChangeStatus = "FAILED" ) +type AdvancedSettings struct { + ProxyVersion *string `json:"proxy_version,omitempty"` + RegistryServer *string `json:"registry_server,omitempty"` +} + type ConfigurationChange struct { - ID string `json:"id"` - Status RemoteChangeStatus `json:"status"` - AgentVersion *string `json:"agent_version"` - Timestamp string `json:"timestamp"` + ID string `json:"id"` + Status RemoteChangeStatus `json:"status"` + AgentVersion *string `json:"agent_version"` + AdvancedSettings *AdvancedSettings `json:"advancedSettings,omitempty"` + Timestamp string `json:"timestamp"` } type ConfigurationChangeStatusUpdate struct { diff --git a/cbcontainers/remote_configuration/change_applier.go b/cbcontainers/remote_configuration/change_applier.go index e26d37f5..e503bc30 100644 --- a/cbcontainers/remote_configuration/change_applier.go +++ b/cbcontainers/remote_configuration/change_applier.go @@ -11,6 +11,7 @@ func ApplyConfigChangeToCR(change models.ConfigurationChange, cr *cbcontainersv1 if change.AgentVersion != nil { cr.Spec.Version = *change.AgentVersion + applyAdvancedSettings(change.AdvancedSettings, cr) resetImageTagsInCR(cr) toggleFeaturesBasedOnCompatibility(cr, *change.AgentVersion, sensorMetadata) } @@ -78,3 +79,17 @@ func toggleFeaturesBasedOnCompatibility(cr *cbcontainersv1.CBContainersAgent, ve cr.Spec.Components.Cndr.Enabled = &falseRef } } + +func applyAdvancedSettings(settings *models.AdvancedSettings, cr *cbcontainersv1.CBContainersAgent) { + if settings == nil { + return + } + + if settings.RegistryServer != nil { + cr.Spec.Components.Settings.DefaultImagesRegistry = *settings.RegistryServer + } + if settings.ProxyVersion != nil { + cr.Spec.Components.Settings.Proxy.HttpsProxy = settings.ProxyVersion + cr.Spec.Components.Settings.Proxy.HttpProxy = settings.ProxyVersion + } +} From 97f8ebe5781b79ed01d8622c44bbd089c2c41fb3 Mon Sep 17 00:00:00 2001 From: Tomer Shefler Date: Mon, 15 Jan 2024 09:53:51 +0200 Subject: [PATCH 2/3] Fix advanced settings Camel case json. and fix proxy_server from `proxy_version` --- cbcontainers/models/remote_configuration_changes.go | 4 ++-- cbcontainers/remote_configuration/change_applier.go | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cbcontainers/models/remote_configuration_changes.go b/cbcontainers/models/remote_configuration_changes.go index a2f4e64c..41758fd1 100644 --- a/cbcontainers/models/remote_configuration_changes.go +++ b/cbcontainers/models/remote_configuration_changes.go @@ -9,7 +9,7 @@ var ( ) type AdvancedSettings struct { - ProxyVersion *string `json:"proxy_version,omitempty"` + ProxyServer *string `json:"proxy_server,omitempty"` RegistryServer *string `json:"registry_server,omitempty"` } @@ -17,7 +17,7 @@ type ConfigurationChange struct { ID string `json:"id"` Status RemoteChangeStatus `json:"status"` AgentVersion *string `json:"agent_version"` - AdvancedSettings *AdvancedSettings `json:"advancedSettings,omitempty"` + AdvancedSettings *AdvancedSettings `json:"advanced_settings,omitempty"` Timestamp string `json:"timestamp"` } diff --git a/cbcontainers/remote_configuration/change_applier.go b/cbcontainers/remote_configuration/change_applier.go index e503bc30..67c68e35 100644 --- a/cbcontainers/remote_configuration/change_applier.go +++ b/cbcontainers/remote_configuration/change_applier.go @@ -88,8 +88,8 @@ func applyAdvancedSettings(settings *models.AdvancedSettings, cr *cbcontainersv1 if settings.RegistryServer != nil { cr.Spec.Components.Settings.DefaultImagesRegistry = *settings.RegistryServer } - if settings.ProxyVersion != nil { - cr.Spec.Components.Settings.Proxy.HttpsProxy = settings.ProxyVersion - cr.Spec.Components.Settings.Proxy.HttpProxy = settings.ProxyVersion + if settings.ProxyServer != nil { + cr.Spec.Components.Settings.Proxy.HttpsProxy = settings.ProxyServer + cr.Spec.Components.Settings.Proxy.HttpProxy = settings.ProxyServer } } From c4c58b32589b489a8a891dda12ccc37fb1250619 Mon Sep 17 00:00:00 2001 From: Tomer Shefler Date: Mon, 15 Jan 2024 15:09:19 +0200 Subject: [PATCH 3/3] Add unittests --- .../remote_configuration/change_applier.go | 9 ++- .../change_applier_test.go | 60 ++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/cbcontainers/remote_configuration/change_applier.go b/cbcontainers/remote_configuration/change_applier.go index 67c68e35..64d4e92a 100644 --- a/cbcontainers/remote_configuration/change_applier.go +++ b/cbcontainers/remote_configuration/change_applier.go @@ -11,7 +11,7 @@ func ApplyConfigChangeToCR(change models.ConfigurationChange, cr *cbcontainersv1 if change.AgentVersion != nil { cr.Spec.Version = *change.AgentVersion - applyAdvancedSettings(change.AdvancedSettings, cr) + applyAdvancedSettings(cr, change.AdvancedSettings) resetImageTagsInCR(cr) toggleFeaturesBasedOnCompatibility(cr, *change.AgentVersion, sensorMetadata) } @@ -80,7 +80,7 @@ func toggleFeaturesBasedOnCompatibility(cr *cbcontainersv1.CBContainersAgent, ve } } -func applyAdvancedSettings(settings *models.AdvancedSettings, cr *cbcontainersv1.CBContainersAgent) { +func applyAdvancedSettings(cr *cbcontainersv1.CBContainersAgent, settings *models.AdvancedSettings) { if settings == nil { return } @@ -89,7 +89,10 @@ func applyAdvancedSettings(settings *models.AdvancedSettings, cr *cbcontainersv1 cr.Spec.Components.Settings.DefaultImagesRegistry = *settings.RegistryServer } if settings.ProxyServer != nil { - cr.Spec.Components.Settings.Proxy.HttpsProxy = settings.ProxyServer + if cr.Spec.Components.Settings.Proxy == nil { + cr.Spec.Components.Settings.Proxy = &cbcontainersv1.CBContainersProxySettings{} + } cr.Spec.Components.Settings.Proxy.HttpProxy = settings.ProxyServer + cr.Spec.Components.Settings.Proxy.HttpsProxy = settings.ProxyServer } } diff --git a/cbcontainers/remote_configuration/change_applier_test.go b/cbcontainers/remote_configuration/change_applier_test.go index a317a3d4..40161838 100644 --- a/cbcontainers/remote_configuration/change_applier_test.go +++ b/cbcontainers/remote_configuration/change_applier_test.go @@ -15,7 +15,7 @@ func TestVersionIsAppliedCorrectly(t *testing.T) { originalVersion := "my-version-42" newVersion := "new-version" cr := cbcontainersv1.CBContainersAgent{Spec: cbcontainersv1.CBContainersAgentSpec{Version: originalVersion}} - change := models.ConfigurationChange{AgentVersion: &newVersion} + change := models.ConfigurationChange{AgentVersion: &newVersion, AdvancedSettings: nil} remote_configuration.ApplyConfigChangeToCR(change, &cr, nil) assert.Equal(t, newVersion, cr.Spec.Version) @@ -28,7 +28,65 @@ func TestMissingVersionDoesNotModifyCR(t *testing.T) { remote_configuration.ApplyConfigChangeToCR(change, &cr, nil) assert.Equal(t, originalVersion, cr.Spec.Version) +} + +func TestAdvancedSettingsAppliedCorrectly(t *testing.T) { + proxy := "https://proxy.com" + reg := "dockerhub.com" + version := "3.0.0" + + advancedSettings := &models.AdvancedSettings{ + ProxyServer: &proxy, + RegistryServer: ®, + } + cr := cbcontainersv1.CBContainersAgent{Spec: cbcontainersv1.CBContainersAgentSpec{Version: version}} + change := models.ConfigurationChange{AgentVersion: &version, AdvancedSettings: advancedSettings} + + remote_configuration.ApplyConfigChangeToCR(change, &cr, nil) + assert.Equal(t, version, cr.Spec.Version) + assert.Equal(t, reg, cr.Spec.Components.Settings.DefaultImagesRegistry) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpsProxy) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpProxy) +} + +func TestAdvancedSettingsNoChange(t *testing.T) { + proxy := "https://proxy.com" + reg := "dockerhub.com" + version := "3.0.0" + cr := cbcontainersv1.CBContainersAgent{Spec: cbcontainersv1.CBContainersAgentSpec{Version: version}} + cr.Spec.Components.Settings.DefaultImagesRegistry = reg + cr.Spec.Components.Settings.Proxy = &cbcontainersv1.CBContainersProxySettings{ + HttpProxy: &proxy, HttpsProxy: &proxy, + } + change := models.ConfigurationChange{AgentVersion: &version, AdvancedSettings: nil} + + remote_configuration.ApplyConfigChangeToCR(change, &cr, nil) + assert.Equal(t, version, cr.Spec.Version) + assert.Equal(t, reg, cr.Spec.Components.Settings.DefaultImagesRegistry) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpsProxy) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpProxy) +} + +func TestAdvancedSettingsOnlyReg(t *testing.T) { + proxy := "https://proxy.com" + reg := "dockerhub.com" + version := "3.0.0" + + cr := cbcontainersv1.CBContainersAgent{Spec: cbcontainersv1.CBContainersAgentSpec{Version: version}} + cr.Spec.Components.Settings.Proxy = &cbcontainersv1.CBContainersProxySettings{ + HttpProxy: &proxy, HttpsProxy: &proxy, + } + change := models.ConfigurationChange{AgentVersion: &version, AdvancedSettings: &models.AdvancedSettings{ + ProxyServer: nil, + RegistryServer: ®, + }} + + remote_configuration.ApplyConfigChangeToCR(change, &cr, nil) + assert.Equal(t, version, cr.Spec.Version) + assert.Equal(t, reg, cr.Spec.Components.Settings.DefaultImagesRegistry) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpsProxy) + assert.Equal(t, proxy, *cr.Spec.Components.Settings.Proxy.HttpProxy) } func TestVersionOverwritesCustomTagsByRemovingThem(t *testing.T) {