Skip to content

Commit

Permalink
Merge pull request #223 from octarinesec/remote-update-advanced-settings
Browse files Browse the repository at this point in the history
Add advanced settings to remote update
  • Loading branch information
tomer-shefler authored Jan 16, 2024
2 parents 4bfe420 + c4c58b3 commit f699280
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
14 changes: 10 additions & 4 deletions cbcontainers/models/remote_configuration_changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 18 additions & 0 deletions cbcontainers/remote_configuration/change_applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
}
}
60 changes: 59 additions & 1 deletion cbcontainers/remote_configuration/change_applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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: &reg,
}
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: &reg,
}}

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) {
Expand Down

0 comments on commit f699280

Please sign in to comment.