Repository: aquasecurity/starboard
Pull request #602 (2021-05-25 13:50:00)

refactor: Define PluginConfig object
Signed-off-by: Daniel Pacak <pacak.daniel@gmail.com>

codecov[bot]: # [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) Report
> Merging [#602](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) (a618262) into [main](https://codecov.io/gh/aquasecurity/starboard/commit/ddc4a58d25eb093caf12100d73b074190b72303f?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) (ddc4a58) will **decrease** coverage by `25.66%`.
> The diff coverage is `75.00%`.

[![Impacted file tree graph](https://codecov.io/gh/aquasecurity/starboard/pull/602/graphs/tree.svg?width=650&height=150&src=pr&token=qqTGKAJ0Wb&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)

```diff
@@             Coverage Diff             @@
##             main     #602       +/-   ##
===========================================
- Coverage   71.11%   45.44%   -25.67%     
===========================================
  Files          57       41       -16     
  Lines        4227     3593      -634     
===========================================
- Hits         3006     1633     -1373     
- Misses        882     1863      +981     
+ Partials      339       97      -242     
```


| [Impacted Files](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) | Coverage Δ | |
|---|---|---|
| [pkg/plugin/conftest/plugin.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3BsdWdpbi9jb25mdGVzdC9wbHVnaW4uZ28=) | `87.50% <ø> (ø)` | |
| [pkg/starboard/plugin.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3N0YXJib2FyZC9wbHVnaW4uZ28=) | `77.77% <75.00%> (-3.18%)` | :arrow_down: |
| [pkg/kubehunter/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmVodW50ZXIvc2Nhbm5lci5nbw==) | `0.00% <0.00%> (-80.51%)` | :arrow_down: |
| [pkg/plugin/factory.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3BsdWdpbi9mYWN0b3J5Lmdv) | `0.00% <0.00%> (-80.49%)` | :arrow_down: |
| [pkg/configauditreport/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2NvbmZpZ2F1ZGl0cmVwb3J0L3NjYW5uZXIuZ28=) | `0.00% <0.00%> (-70.77%)` | :arrow_down: |
| [pkg/vulnerabilityreport/scanner.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL3Z1bG5lcmFiaWxpdHlyZXBvcnQvc2Nhbm5lci5nbw==) | `0.00% <0.00%> (-70.54%)` | :arrow_down: |
| [pkg/operator/controller/configauditreport.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvY29uZmlnYXVkaXRyZXBvcnQuZ28=) | `0.00% <0.00%> (-65.75%)` | :arrow_down: |
| [pkg/operator/controller/ciskubebenchreport.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvY2lza3ViZWJlbmNocmVwb3J0Lmdv) | `0.00% <0.00%> (-65.43%)` | :arrow_down: |
| [pkg/operator/controller/vulnerabilityreport.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL29wZXJhdG9yL2NvbnRyb2xsZXIvdnVsbmVyYWJpbGl0eXJlcG9ydC5nbw==) | `0.00% <0.00%> (-65.17%)` | :arrow_down: |
| [pkg/kube/runnable\_job.go](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity#diff-cGtnL2t1YmUvcnVubmFibGVfam9iLmdv) | `0.00% <0.00%> (-64.39%)` | :arrow_down: |
| ... and [31 more](https://codecov.io/gh/aquasecurity/starboard/pull/602/diff?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity). Last update [ddc4a58...a618262](https://codecov.io/gh/aquasecurity/starboard/pull/602?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=aquasecurity).


Patch:
diff --git a/pkg/starboard/plugin.go b/pkg/starboard/plugin.go
index 1afcc6b..ced8c9c 100644
--- a/pkg/starboard/plugin.go
+++ b/pkg/starboard/plugin.go
@@ -6,18 +6,25 @@ import (
 	"strings"
 
 	corev1 "k8s.io/api/core/v1"
+	"k8s.io/apimachinery/pkg/api/errors"
 	"k8s.io/apimachinery/pkg/types"
 	"sigs.k8s.io/controller-runtime/pkg/client"
 )
 
+// PluginConfig holds plugin configuration settings.
+type PluginConfig struct {
+	Data       map[string]string
+	SecretData map[string][]byte
+}
+
 // PluginContext is plugin's execution context within the Starboard toolkit.
 // The context is used to grant access to other methods so that this plugin
 // can interact with the toolkit.
 type PluginContext interface {
 	// GetName returns the name of the plugin.
 	GetName() string
-	// GetConfig returns the v1.ConfigMap object that holds configuration settings of the plugin.
-	GetConfig() (*corev1.ConfigMap, error)
+	// GetConfig returns the PluginConfig object that holds configuration settings of the plugin.
+	GetConfig() (PluginConfig, error)
 	// GetNamespace return the name of the K8s Namespace where Starboard creates Jobs
 	// and other helper objects.
 	GetNamespace() string
@@ -43,13 +50,36 @@ func (p *pluginContext) GetName() string {
 	return p.name
 }
 
-func (p *pluginContext) GetConfig() (*corev1.ConfigMap, error) {
+func (p *pluginContext) GetConfig() (PluginConfig, error) {
 	cm := &corev1.ConfigMap{}
+	secret := &corev1.Secret{}
+
 	err := p.client.Get(context.Background(), types.NamespacedName{
 		Namespace: p.namespace,
 		Name:      fmt.Sprintf("starboard-%s-config", strings.ToLower(p.GetName())),
 	}, cm)
-	return cm.DeepCopy(), err
+	if err != nil {
+		return PluginConfig{}, err
+	}
+
+	err = p.client.Get(context.Background(), types.NamespacedName{
+		Namespace: p.namespace,
+		Name:      fmt.Sprintf("starboard-%s-config", strings.ToLower(p.GetName())),
+	}, secret)
+
+	var secretData map[string][]byte
+	if err == nil {
+		secretData = secret.DeepCopy().Data
+	}
+
+	if err != nil && !errors.IsNotFound(err) {
+		return PluginConfig{}, err
+	}
+
+	return PluginConfig{
+		Data:       cm.DeepCopy().Data,
+		SecretData: secretData,
+	}, nil
 }
 
 func (p *pluginContext) GetNamespace() string {


Test patch:
diff --git a/pkg/plugin/conftest/plugin.go b/pkg/plugin/conftest/plugin.go
index cce1ecb..bf43651 100644
--- a/pkg/plugin/conftest/plugin.go
+++ b/pkg/plugin/conftest/plugin.go
@@ -174,10 +174,10 @@ func (p *plugin) GetScanJobSpec(ctx starboard.PluginContext, obj client.Object)
 		}}, nil
 }
 
-func (p *plugin) getPolicies(cm *corev1.ConfigMap) map[string]string {
+func (p *plugin) getPolicies(config starboard.PluginConfig) map[string]string {
 	policies := make(map[string]string)
 
-	for key, value := range cm.Data {
+	for key, value := range config.Data {
 		if !strings.HasPrefix(key, policyPrefix) {
 			continue
 		}
diff --git a/pkg/starboard/plugin_test.go b/pkg/starboard/plugin_test.go
index bf7fd3a..01f2cdb 100644
--- a/pkg/starboard/plugin_test.go
+++ b/pkg/starboard/plugin_test.go
@@ -17,41 +17,81 @@ func TestGetPluginConfigMapName(t *testing.T) {
 }
 
 func TestPluginContext_GetConfig(t *testing.T) {
-	g := gomega.NewGomegaWithT(t)
 
-	client := fake.NewClientBuilder().
-		WithScheme(starboard.NewScheme()).
-		WithObjects(&corev1.ConfigMap{
-			ObjectMeta: metav1.ObjectMeta{
-				Name:      "starboard-polaris-config",
-				Namespace: "starboard-ns",
-			},
-			Data: map[string]string{
-				"foo": "bar",
-			},
-		}).
-		Build()
-
-	pluginContext := starboard.NewPluginContext().
-		WithName("polaris").
-		WithNamespace("starboard-ns").
-		WithClient(client).
-		Get()
-
-	cm, err := pluginContext.GetConfig()
-
-	g.Expect(err).ToNot(gomega.HaveOccurred())
-	g.Expect(cm).To(gomega.Equal(&corev1.ConfigMap{
-		TypeMeta: metav1.TypeMeta{
-			APIVersion: "v1",
-			Kind:       "ConfigMap",
-		},
-		ObjectMeta: metav1.ObjectMeta{
-			Name:      "starboard-polaris-config",
-			Namespace: "starboard-ns",
-		},
-		Data: map[string]string{
-			"foo": "bar",
-		},
-	}))
+	t.Run("Should return PluginConfig from ConfigMap", func(t *testing.T) {
+		g := gomega.NewGomegaWithT(t)
+
+		client := fake.NewClientBuilder().
+			WithScheme(starboard.NewScheme()).
+			WithObjects(&corev1.ConfigMap{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "starboard-polaris-config",
+					Namespace: "starboard-ns",
+				},
+				Data: map[string]string{
+					"foo": "bar",
+				},
+			}).
+			Build()
+
+		pluginContext := starboard.NewPluginContext().
+			WithName("polaris").
+			WithNamespace("starboard-ns").
+			WithClient(client).
+			Get()
+
+		cm, err := pluginContext.GetConfig()
+
+		g.Expect(err).ToNot(gomega.HaveOccurred())
+		g.Expect(cm).To(gomega.Equal(
+			starboard.PluginConfig{
+				Data: map[string]string{
+					"foo": "bar",
+				},
+			}))
+	})
+
+	t.Run("Should return PluginConfig from ConfigMap and Secret", func(t *testing.T) {
+		g := gomega.NewGomegaWithT(t)
+
+		client := fake.NewClientBuilder().
+			WithScheme(starboard.NewScheme()).
+			WithObjects(&corev1.ConfigMap{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "starboard-polaris-config",
+					Namespace: "starboard-ns",
+				},
+				Data: map[string]string{
+					"foo": "bar",
+				},
+			}, &corev1.Secret{
+				ObjectMeta: metav1.ObjectMeta{
+					Name:      "starboard-polaris-config",
+					Namespace: "starboard-ns",
+				},
+				Data: map[string][]byte{
+					"secret": []byte("pa$$word"),
+				},
+			}).
+			Build()
+
+		pluginContext := starboard.NewPluginContext().
+			WithName("polaris").
+			WithNamespace("starboard-ns").
+			WithClient(client).
+			Get()
+
+		cm, err := pluginContext.GetConfig()
+
+		g.Expect(err).ToNot(gomega.HaveOccurred())
+		g.Expect(cm).To(gomega.Equal(
+			starboard.PluginConfig{
+				Data: map[string]string{
+					"foo": "bar",
+				},
+				SecretData: map[string][]byte{
+					"secret": []byte("pa$$word"),
+				},
+			}))
+	})
 }