Skip to content

Commit

Permalink
fix slack 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 May 9, 2024
1 parent c54286b commit 169a068
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 92 deletions.
3 changes: 1 addition & 2 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,9 @@ func (f *TargetFactory) createSlackClient(config, parent *Target[SlackOptions])
ResultFilter: f.createResultFilter(config.Filter, config.MinimumPriority, config.Sources),
ReportFilter: createReportFilter(config.Filter),
},
Webhook: config.Config.Webhook,
Channel: config.Config.Channel,
CustomFields: config.CustomFields,
HTTPClient: http.NewClient("", false),
HTTPClient: slack.NewAPIClient(config.Config.Webhook, http.NewClient("", false)),
})
}

Expand Down
14 changes: 8 additions & 6 deletions pkg/config/target_factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func newFakeClient() v1.SecretInterface {
"host": []byte("http://localhost:9200"),
"username": []byte("username"),
"password": []byte("password"),
"channel": []byte("general"),
"apiKey": []byte("apiKey"),
"webhook": []byte("http://localhost:9200/webhook"),
"accountId": []byte("accountID"),
Expand All @@ -50,6 +51,7 @@ func mountSecret() {
secretValues := secrets.Values{
Host: "http://localhost:9200",
Webhook: "http://localhost:9200/webhook",
Channel: "general",
Username: "username",
Password: "password",
APIKey: "apiKey",
Expand Down Expand Up @@ -376,9 +378,9 @@ func Test_GetValuesFromSecret(t *testing.T) {
t.Run("Get Slack values from Secret", func(t *testing.T) {
client := reflect.ValueOf(clients[2]).Elem()

webhook := client.FieldByName("webhook").String()
if webhook != "http://localhost:9200/webhook" {
t.Errorf("Expected webhook from secret, got %s", webhook)
webhook := client.FieldByName("channel").String()
if webhook != "general" {
t.Errorf("Expected channel from secret, got %s", webhook)
}
})

Expand Down Expand Up @@ -745,9 +747,9 @@ func Test_GetValuesFromMountedSecret(t *testing.T) {
t.Run("Get Slack values from Secret", func(t *testing.T) {
client := reflect.ValueOf(clients[2]).Elem()

webhook := client.FieldByName("webhook").String()
if webhook != "http://localhost:9200/webhook" {
t.Errorf("Expected webhook from secret, got %s", webhook)
webhook := client.FieldByName("channel").String()
if webhook != "general" {
t.Errorf("Expected channel from secret, got %s", webhook)
}
})

Expand Down
36 changes: 36 additions & 0 deletions pkg/fixtures/policy_reports.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ var DefaultPolicyReport = &v1alpha2.PolicyReport{
},
}

var ScopePolicyReport = &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "policy-report",
Namespace: "test",
},
Summary: v1alpha2.PolicyReportSummary{
Pass: 0,
Skip: 0,
Warn: 0,
Fail: 3,
Error: 0,
},
Scope: &corev1.ObjectReference{
APIVersion: "v1",
Kind: "Deployment",
Name: "nginx",
Namespace: "test",
UID: "dfd57c50-f30c-4729-b63f-b1954d8988d1",
},
Results: []v1alpha2.PolicyReportResult{
{
ID: "12348",
Message: "message",
Result: v1alpha2.StatusFail,
Scored: true,
Policy: "required-label",
Rule: "app-label-required",
Timestamp: v1.Timestamp{Seconds: 1614093000},
Source: "test",
Category: "test",
Severity: v1alpha2.SeverityHigh,
Properties: map[string]string{"version": "1.2.0"},
},
},
}

var MultiResourcePolicyReport = &v1alpha2.PolicyReport{
ObjectMeta: v1.ObjectMeta{
Name: "policy-report",
Expand Down
24 changes: 24 additions & 0 deletions pkg/target/slack/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package slack

import (
"net/http"

"github.com/slack-go/slack"
)

type APIClient interface {
PostMessage(*slack.WebhookMessage) error
}

type apiClient struct {
webhook string
client *http.Client
}

func (c *apiClient) PostMessage(message *slack.WebhookMessage) error {
return slack.PostWebhookCustomHTTP(c.webhook, c.client, message)
}

func NewAPIClient(webhook string, client *http.Client) APIClient {
return &apiClient{webhook: webhook, client: client}
}
23 changes: 11 additions & 12 deletions pkg/target/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package slack
import (
"context"
"fmt"
"net/http"
"strings"

"github.com/slack-go/slack"
Expand All @@ -13,23 +12,20 @@ import (
"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/helper"
"github.com/kyverno/policy-reporter/pkg/target"
rest "github.com/kyverno/policy-reporter/pkg/target/http"
)

// Options to configure the Slack target
type Options struct {
target.ClientOptions
Webhook string
Channel string
CustomFields map[string]string
HTTPClient rest.Client
HTTPClient APIClient
}

type client struct {
target.BaseClient
webhook string
channel string
client rest.Client
client APIClient
customFields map[string]string
}

Expand Down Expand Up @@ -271,17 +267,21 @@ func (s *client) batchMessage(polr v1alpha2.ReportInterface, results []v1alpha2.
}

func (s *client) Send(result v1alpha2.PolicyReportResult) {
client := s.client.(*http.Client)

if err := slack.PostWebhookCustomHTTP(s.webhook, client, s.message(result)); err != nil {
if err := s.client.PostMessage(s.message(result)); err != nil {
zap.L().Error(s.Name()+": PUSH FAILED", zap.Error(err))
}
}

func (s *client) BatchSend(report v1alpha2.ReportInterface, results []v1alpha2.PolicyReportResult) {
client := s.client.(*http.Client)
if report.GetScope() == nil {
for _, result := range results {
s.Send(result)
}

return
}

if err := slack.PostWebhookCustomHTTP(s.webhook, client, s.batchMessage(report, results)); err != nil {
if err := s.client.PostMessage(s.batchMessage(report, results)); err != nil {
zap.L().Error(s.Name()+": BATCH PUSH FAILED", zap.Error(err))
}
}
Expand All @@ -296,7 +296,6 @@ func (s *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {}
func NewClient(options Options) target.Client {
return &client{
target.NewBaseClient(options.ClientOptions),
options.Webhook,
options.Channel,
options.HTTPClient,
options.CustomFields,
Expand Down
Loading

0 comments on commit 169a068

Please sign in to comment.