Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func (r *ContainerScanReconciler) Reconcile(ctx context.Context, req ctrl.Reques

scanTypesInstalled := r.checkForScanTypes(ctx, pod)
if !scanTypesInstalled {
requeueDuration := r.Config.ContainerAutoDiscovery.PassiveReconcileInterval
requeueDuration := r.Config.ContainerAutoDiscovery.PassiveReconcileInterval.Duration
return ctrl.Result{Requeue: true, RequeueAfter: requeueDuration}, nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (r *ServiceScanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
// Requeue to allow scan to be created when the user installs the scanType
return ctrl.Result{
Requeue: true,
RequeueAfter: Config.ServiceAutoDiscovery.PassiveReconcileInterval,
RequeueAfter: r.Config.ServiceAutoDiscovery.PassiveReconcileInterval.Duration,
}, nil
} else if err != nil {
return ctrl.Result{
Expand Down Expand Up @@ -242,7 +242,7 @@ func (r *ServiceScanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
}
return ctrl.Result{
Requeue: true,
RequeueAfter: r.Config.ServiceAutoDiscovery.PassiveReconcileInterval,
RequeueAfter: r.Config.ServiceAutoDiscovery.PassiveReconcileInterval.Duration,
}, nil
}

Expand Down
100 changes: 2 additions & 98 deletions auto-discovery/kubernetes/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"context"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
Expand All @@ -21,11 +20,8 @@ import (

ctrl "sigs.k8s.io/controller-runtime"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

//+kubebuilder:scaffold:imports

config "github.com/secureCodeBox/secureCodeBox/auto-discovery/kubernetes/pkg/config"
executionv1 "github.com/secureCodeBox/secureCodeBox/operator/apis/execution/v1"
)

Expand Down Expand Up @@ -72,105 +68,13 @@ var _ = BeforeSuite(func() {
})
Expect(err).ToNot(HaveOccurred())

config := config.AutoDiscoveryConfig{
Cluster: config.ClusterConfig{
Name: "test-cluster",
},
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan-0",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
{
Name: "test-scan-1",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
},
},
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
ImagePullSecretConfig: config.ImagePullSecretConfig{
MapImagePullSecretsToEnvironmentVariables: true,
UsernameEnvironmentVariableName: "username",
PasswordNameEnvironmentVariableName: "password",
},
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
},
},
{
Name: "test-scan-two",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
},
},
},
},
ResourceInclusion: config.ResourceInclusionConfig{
Mode: config.EnabledPerResource,
},
}

// working config
err = (&ServiceScanReconciler{
Client: k8sManager.GetClient(),
Scheme: k8sManager.GetScheme(),
Recorder: k8sManager.GetEventRecorderFor("ServiceScanController"),
Log: ctrl.Log.WithName("controllers").WithName("ServiceScanController"),
Config: config,
Config: AutoDiscoveryConfigMock,
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand All @@ -180,7 +84,7 @@ var _ = BeforeSuite(func() {
Scheme: k8sManager.GetScheme(),
Recorder: k8sManager.GetEventRecorderFor("ContainerScanController"),
Log: ctrl.Log.WithName("controllers").WithName("ContainerScanController"),
Config: config,
Config: AutoDiscoveryConfigMock,
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
198 changes: 47 additions & 151 deletions auto-discovery/kubernetes/controllers/suite_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,174 +11,70 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var Config = config.AutoDiscoveryConfig{
Cluster: config.ClusterConfig{
Name: "test-cluster",
},
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan-0",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
{
Name: "test-scan-1",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
// newServiceScanConfigMock creates a mock scan configuration specifically for service auto-discovery
func newServiceScanConfigMock(name string) config.ScanConfig {
return config.ScanConfig{
Name: name,
RepeatInterval: metav1.Duration{Duration: time.Hour},
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
}
}

// newContainerScanConfigMock creates a mock scan configuration specifically for container auto-discovery
func newContainerScanConfigMock(name string) config.ScanConfig {
return config.ScanConfig{
Name: name,
RepeatInterval: metav1.Duration{Duration: time.Hour},
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
},
{
Name: "test-scan-two",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
},
},
ResourceInclusion: config.ResourceInclusionConfig{
Mode: config.EnabledPerResource,
},
}
}

// broken config has two scans (per autodiscovery) defined with the same name which will trigger an error during controller setup
var BrokenConfig = config.AutoDiscoveryConfig{
// AutoDiscoveryConfigMock holds the complete mock configuration
var AutoDiscoveryConfigMock = config.AutoDiscoveryConfig{
Cluster: config.ClusterConfig{
Name: "test-cluster",
},
ServiceAutoDiscovery: config.ServiceAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{},
Labels: map[string]string{},
Parameters: []string{"-p", "{{ .Host.Port }}", "{{ .Service.Name }}.{{ .Service.Namespace }}.svc"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchLabels: map[string]string{
"foo": "bar",
},
},
},
newServiceScanConfigMock("test-scan-0"),
newServiceScanConfigMock("test-scan-1"),
},
},
ContainerAutoDiscovery: config.ContainerAutoDiscoveryConfig{
PassiveReconcileInterval: 1 * time.Second,
PassiveReconcileInterval: metav1.Duration{Duration: 1 * time.Second},
ImagePullSecretConfig: config.ImagePullSecretConfig{
MapImagePullSecretsToEnvironmentVariables: true,
UsernameEnvironmentVariableName: "username",
PasswordNameEnvironmentVariableName: "password",
},
ScanConfigs: []config.ScanConfig{
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
},
},
{
Name: "test-scan",
RepeatInterval: time.Hour,
Annotations: map[string]string{"testAnnotation": "{{ .Namespace.Name }}"},
Labels: map[string]string{"testLabel": "{{ .Namespace.Name }}"},
Parameters: []string{"-p", "{{ .Namespace.Name }}"},
ScanType: "nmap",
HookSelector: metav1.LabelSelector{
MatchExpressions: []metav1.LabelSelectorRequirement{
{
Operator: metav1.LabelSelectorOpIn,
Key: "foo",
Values: []string{"bar", "baz"},
},
{
Operator: metav1.LabelSelectorOpDoesNotExist,
Key: "foo",
},
},
},
},
newContainerScanConfigMock("test-scan"),
newContainerScanConfigMock("test-scan-two"),
},
},
ResourceInclusion: config.ResourceInclusionConfig{
Expand Down
Loading
Loading