diff --git a/cbcontainers/models/remote_configuration_changes.go b/cbcontainers/models/remote_configuration_changes.go index 91211953..41758fd1 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 { + ProxyServer *string `json:"proxy_server,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:"advanced_settings,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..64d4e92a 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(cr, change.AdvancedSettings) resetImageTagsInCR(cr) toggleFeaturesBasedOnCompatibility(cr, *change.AgentVersion, sensorMetadata) } @@ -78,3 +79,20 @@ func toggleFeaturesBasedOnCompatibility(cr *cbcontainersv1.CBContainersAgent, ve cr.Spec.Components.Cndr.Enabled = &falseRef } } + +func applyAdvancedSettings(cr *cbcontainersv1.CBContainersAgent, settings *models.AdvancedSettings) { + if settings == nil { + return + } + + if settings.RegistryServer != nil { + cr.Spec.Components.Settings.DefaultImagesRegistry = *settings.RegistryServer + } + if settings.ProxyServer != nil { + 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) {