Skip to content

Commit

Permalink
add util tests
Browse files Browse the repository at this point in the history
Signed-off-by: Frank Jogeleit <frank.jogeleit@lovoo.com>
  • Loading branch information
Frank Jogeleit committed Apr 28, 2024
1 parent 48d5c0f commit 76797be
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 52 deletions.
12 changes: 7 additions & 5 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/kyverno/policy-reporter/pkg/target/http"
"github.com/kyverno/policy-reporter/pkg/target/kinesis"
"github.com/kyverno/policy-reporter/pkg/target/loki"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
gs "github.com/kyverno/policy-reporter/pkg/target/provider/gcs"
"github.com/kyverno/policy-reporter/pkg/target/s3"
"github.com/kyverno/policy-reporter/pkg/target/securityhub"
"github.com/kyverno/policy-reporter/pkg/target/slack"
Expand Down Expand Up @@ -431,14 +433,14 @@ func (f *TargetFactory) createS3Client(config, parent *Target[S3Options]) target

config.MapBaseParent(parent)

s3Client := helper.NewS3Client(
s3Client := aws.NewS3Client(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
config.Config.Endpoint,
config.Config.Bucket,
config.Config.PathStyle,
helper.WithKMS(config.Config.BucketKeyEnabled, &config.Config.KmsKeyID, &config.Config.ServerSideEncryption),
aws.WithKMS(config.Config.BucketKeyEnabled, &config.Config.KmsKeyID, &config.Config.ServerSideEncryption),
)

sugar.Infof("%s configured", config.Name)
Expand Down Expand Up @@ -485,7 +487,7 @@ func (f *TargetFactory) createKinesisClient(config, parent *Target[KinesisOption

config.MapBaseParent(parent)

kinesisClient := helper.NewKinesisClient(
kinesisClient := aws.NewKinesisClient(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
Expand Down Expand Up @@ -533,7 +535,7 @@ func (f *TargetFactory) createSecurityHub(config, parent *Target[SecurityHubOpti

setInt(&config.Config.DelayInSeconds, parent.Config.DelayInSeconds)

client := helper.NewHubClient(
client := aws.NewHubClient(
config.Config.AccessKeyID,
config.Config.SecretAccessKey,
config.Config.Region,
Expand Down Expand Up @@ -584,7 +586,7 @@ func (f *TargetFactory) createGCSClient(config, parent *Target[GCSOptions]) targ

config.MapBaseParent(parent)

gcsClient := helper.NewGCSClient(
gcsClient := gs.NewClient(
context.Background(),
config.Config.Credentials,
config.Config.Bucket,
Expand Down
42 changes: 42 additions & 0 deletions pkg/helper/http_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package helper_test

import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/stretchr/testify/assert"
)

func TestSendJSONResponse(t *testing.T) {
t.Run("success response", func(t *testing.T) {
w := httptest.NewRecorder()

helper.SendJSONResponse(w, []string{"default", "user"}, nil)

assert.Equal(t, http.StatusOK, w.Code)

resp := make([]string, 0, 2)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, []string{"default", "user"}, resp)
})

t.Run("error response", func(t *testing.T) {
w := httptest.NewRecorder()

helper.SendJSONResponse(w, nil, errors.New("error"))

assert.Equal(t, http.StatusInternalServerError, w.Code)

resp := make(map[string]string, 0)

json.NewDecoder(w.Body).Decode(&resp)

assert.Equal(t, map[string]string{"message": "error"}, resp)
})
}
29 changes: 4 additions & 25 deletions pkg/helper/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package helper

import (
"encoding/json"
"strings"
)

Expand All @@ -24,19 +23,6 @@ func ToList[T any, R comparable](mapping map[R]T) []T {
return list
}

func Merge[T comparable, R any](first, second map[T]R) map[T]R {
merged := make(map[T]R, len(first)+len(second))

for k, v := range first {
merged[k] = v
}
for k, v := range second {
merged[k] = v
}

return merged
}

func Map[T any, R any](source []T, cb func(T) R) []R {
list := make([]R, 0, len(source))
for _, i := range source {
Expand All @@ -46,17 +32,6 @@ func Map[T any, R any](source []T, cb func(T) R) []R {
return list
}

func ConvertJSONToMap(s string) map[string]string {
m := make(map[string]string)
if s == "" {
return m
}

_ = json.Unmarshal([]byte(s), &m)

return m
}

func ConvertMap(m map[string]any) map[string]string {
n := make(map[string]string, len(m))
for k, v := range m {
Expand All @@ -75,3 +50,7 @@ func Defaults(s, f string) string {

return f
}

func ToPointer[T any](s T) *T {
return &s
}
48 changes: 48 additions & 0 deletions pkg/helper/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package helper_test

import (
"strings"
"testing"

"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/stretchr/testify/assert"
)

func TestContains(t *testing.T) {
assert.True(t, helper.Contains("kyverno", []string{"test", "kyverno", "trivy"}))
assert.False(t, helper.Contains("kube-bench", []string{"test", "kyverno", "trivy"}))
}

func TestToList(t *testing.T) {
assert.Equal(t, []string{"kyverno", "trivy"}, helper.ToList(map[string]string{
"first": "kyverno",
"second": "trivy",
}))
}

func TestMap(t *testing.T) {
assert.Equal(t, []string{"kyverno", "trivy"}, helper.Map([]string{"source_kyverno", "source_trivy"}, func(value string) string {
return strings.TrimPrefix(value, "source_")
}))
}

func TestConvertMap(t *testing.T) {
assert.Equal(t, map[string]string{"first": "kyverno", "second": "trivy"}, helper.ConvertMap(map[string]any{
"first": "kyverno",
"second": "trivy",
"third": 3,
}))
}

func TestDetauls(t *testing.T) {
assert.Equal(t, "fallback", helper.Defaults("", "fallback"))
assert.Equal(t, "value", helper.Defaults("value", "fallback"))
}

func TestToPointer(t *testing.T) {
value := "test"
number := 5

assert.Equal(t, &value, helper.ToPointer(value))
assert.Equal(t, &number, helper.ToPointer(number))
}
6 changes: 3 additions & 3 deletions pkg/target/gcs/gcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"go.uber.org/zap"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target"
"github.com/kyverno/policy-reporter/pkg/target/http"
"github.com/kyverno/policy-reporter/pkg/target/provider/gcs"
)

// Options to configure the GCS target
type Options struct {
target.ClientOptions
CustomFields map[string]string
Client helper.GCPClient
Client gcs.Client
Prefix string
}

type client struct {
target.BaseClient
customFields map[string]string
client helper.GCPClient
client gcs.Client
prefix string
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/target/kinesis/kinesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
"go.uber.org/zap"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target"
"github.com/kyverno/policy-reporter/pkg/target/http"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
)

// Options to configure the Kinesis target
type Options struct {
target.ClientOptions
CustomFields map[string]string
Kinesis helper.AWSClient
Kinesis aws.Client
}

type client struct {
target.BaseClient
customFields map[string]string
kinesis helper.AWSClient
kinesis aws.Client
}

func (c *client) Send(result v1alpha2.PolicyReportResult) {
Expand Down
8 changes: 4 additions & 4 deletions pkg/helper/aws.go → pkg/target/provider/aws/aws.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper
package aws

import (
"bytes"
Expand All @@ -21,7 +21,7 @@ import (

var enable = true

type AWSClient interface {
type Client interface {
// Upload given Data the configured AWS storage
Upload(body *bytes.Buffer, key string) error
}
Expand Down Expand Up @@ -62,7 +62,7 @@ func (s *s3Client) Upload(body *bytes.Buffer, key string) error {
}

// NewS3Client creates a new S3.client to send Results to S3
func NewS3Client(accessKeyID, secretAccessKey, region, endpoint, bucket string, pathStyle bool, opts ...Options) AWSClient {
func NewS3Client(accessKeyID, secretAccessKey, region, endpoint, bucket string, pathStyle bool, opts ...Options) Client {
config, err := createConfig(accessKeyID, secretAccessKey, region)
if err != nil {
zap.L().Error("error while creating config", zap.Error(err))
Expand Down Expand Up @@ -111,7 +111,7 @@ func (k *kinesisClient) Upload(body *bytes.Buffer, key string) error {
}

// NewKinesisClient creates a new S3.client to send Results to S3
func NewKinesisClient(accessKeyID, secretAccessKey, region, endpoint, streamName string) AWSClient {
func NewKinesisClient(accessKeyID, secretAccessKey, region, endpoint, streamName string) Client {
config, err := createConfig(accessKeyID, secretAccessKey, region)
if err != nil {
zap.L().Error("error while creating config", zap.Error(err))
Expand Down
27 changes: 27 additions & 0 deletions pkg/target/provider/aws/aws_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package aws_test

import (
"testing"

"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
"github.com/stretchr/testify/assert"
)

func TestS3Client(t *testing.T) {
client := aws.NewS3Client("access", "secret", "eu-central-1", "http://s3.aws.com", "policy-reporter", false, aws.WithKMS(true, helper.ToPointer("kms"), helper.ToPointer("encryption")))

assert.NotNil(t, client)
}

func TestKinesisClient(t *testing.T) {
client := aws.NewKinesisClient("access", "secret", "eu-central-1", "http://kinesis.aws.com", "policy-reporter")

assert.NotNil(t, client)
}

func TestSecurityHubClient(t *testing.T) {
client := aws.NewHubClient("access", "secret", "eu-central-1", "http://securityhub.aws.com")

assert.NotNil(t, client)
}
18 changes: 9 additions & 9 deletions pkg/helper/gcp.go → pkg/target/provider/gcs/gcs.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package helper
package gcs

import (
"bytes"
Expand All @@ -10,17 +10,17 @@ import (
"google.golang.org/api/option"
)

type GCPClient interface {
type Client interface {
// Upload given Data the configured AWS storage
Upload(body *bytes.Buffer, key string) error
}

type gcsClient struct {
type client struct {
bucket string
client *storage.Client
}

func (c *gcsClient) Upload(body *bytes.Buffer, key string) error {
func (c *client) Upload(body *bytes.Buffer, key string) error {
object := c.client.Bucket(c.bucket).Object(key)

writer := object.NewWriter(context.Background())
Expand All @@ -34,22 +34,22 @@ func (c *gcsClient) Upload(body *bytes.Buffer, key string) error {
return writer.Close()
}

// NewGCSClient creates a new GCS.client to send Results to GCS Bucket
func NewGCSClient(ctx context.Context, credentials, bucket string) GCPClient {
// NewClient creates a new GCS.client to send Results to GCS Bucket
func NewClient(ctx context.Context, credentials, bucket string) Client {
cred, err := google.CredentialsFromJSON(ctx, []byte(credentials), storage.ScopeReadWrite)
if err != nil {
zap.L().Error("error while creating GCS credentials", zap.Error(err))
return nil
}

client, err := storage.NewClient(ctx, option.WithCredentials(cred))
baseClient, err := storage.NewClient(ctx, option.WithCredentials(cred))
if err != nil {
zap.L().Error("error while creating GCS client", zap.Error(err))
return nil
}

return &gcsClient{
return &client{
bucket,
client,
baseClient,
}
}
6 changes: 3 additions & 3 deletions pkg/target/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ import (
"go.uber.org/zap"

"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target"
"github.com/kyverno/policy-reporter/pkg/target/http"
"github.com/kyverno/policy-reporter/pkg/target/provider/aws"
)

// Options to configure the S3 target
type Options struct {
target.ClientOptions
CustomFields map[string]string
S3 helper.AWSClient
S3 aws.Client
Prefix string
}

type client struct {
target.BaseClient
customFields map[string]string
s3 helper.AWSClient
s3 aws.Client
prefix string
}

Expand Down

0 comments on commit 76797be

Please sign in to comment.