Skip to content

Commit

Permalink
MS Teams BatchSend
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 10, 2024
1 parent 169a068 commit 4dd1d7d
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 225 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.22.1

require (
cloud.google.com/go/storage v1.40.0
github.com/atc0005/go-teams-notify/v2 v2.10.0
github.com/aws/aws-sdk-go-v2 v1.26.1
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/credentials v1.17.11
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ cloud.google.com/go/storage v1.40.0/go.mod h1:Rrj7/hKlG87BLqDJYtwR0fbPld8uJPbQ2u
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/atc0005/go-teams-notify/v2 v2.10.0 h1:eQvRIkyESQgBvlUdQ/iPol/lj3QcRyrdEQM3+c/nXhM=
github.com/atc0005/go-teams-notify/v2 v2.10.0/go.mod h1:SIeE1UfCcVRYMqP5b+r1ZteHyA/2UAjzWF5COnZ8q0w=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.2 h1:x6xsQXGSmW6frevwDA+vi/wqhp1ct18mVXYN08/93to=
Expand Down
3 changes: 1 addition & 2 deletions pkg/config/target_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,9 +273,8 @@ func (f *TargetFactory) createTeamsClient(config, parent *Target[WebhookOptions]
ResultFilter: f.createResultFilter(config.Filter, config.MinimumPriority, config.Sources),
ReportFilter: createReportFilter(config.Filter),
},
Webhook: config.Config.Webhook,
CustomFields: config.CustomFields,
HTTPClient: http.NewClient(config.Config.Certificate, config.Config.SkipTLS),
HTTPClient: teams.NewAPIClient(config.Config.Webhook, http.NewClient(config.Config.Certificate, config.Config.SkipTLS)),
})
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/target/formatting/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package formatting

import (
"fmt"
"strings"

corev1 "k8s.io/api/core/v1"
)

func ResourceString(res *corev1.ObjectReference) string {
var resource string
if res.Namespace == "" {
resource = fmt.Sprintf("%s/%s: %s", res.APIVersion, res.Kind, res.Name)
} else {
resource = fmt.Sprintf("%s/%s: %s/%s", res.APIVersion, res.Kind, res.Namespace, res.Name)
}

return strings.Trim(resource, "/")
}
4 changes: 1 addition & 3 deletions pkg/target/securityhub/securityhub.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ import (
"github.com/kyverno/policy-reporter/pkg/target"
)

var (
schema = toPointer("2018-10-08")
)
var schema = toPointer("2018-10-08")

type HubClient interface {
BatchImportFindings(ctx context.Context, params *hub.BatchImportFindingsInput, optFns ...func(*hub.Options)) (*hub.BatchImportFindingsOutput, error)
Expand Down
18 changes: 3 additions & 15 deletions pkg/target/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package slack
import (
"context"
"fmt"
"strings"

"github.com/slack-go/slack"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"

"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/formatting"
)

// Options to configure the Slack target
Expand Down Expand Up @@ -167,7 +166,7 @@ func (s *client) message(result v1alpha2.PolicyReportResult) *slack.WebhookMessa

func (s *client) batchMessage(polr v1alpha2.ReportInterface, results []v1alpha2.PolicyReportResult) *slack.WebhookMessage {
scope := polr.GetScope()
resource := ResourceString(scope)
resource := formatting.ResourceString(scope)

p := &slack.WebhookMessage{
Attachments: make([]slack.Attachment, 0, 1),
Expand Down Expand Up @@ -196,7 +195,7 @@ func (s *client) batchMessage(polr v1alpha2.ReportInterface, results []v1alpha2.
var propBlock *slack.SectionBlock
for property, value := range s.customFields {
if i%2 == 0 {
propBlock = slack.NewSectionBlock(nil, make([]*slack.TextBlockObject, 2), nil)
propBlock = slack.NewSectionBlock(nil, make([]*slack.TextBlockObject, 0, 2), nil)
att.Blocks.BlockSet = append(att.Blocks.BlockSet, propBlock)
}

Expand Down Expand Up @@ -301,14 +300,3 @@ func NewClient(options Options) target.Client {
options.CustomFields,
}
}

func ResourceString(res *corev1.ObjectReference) string {
var resource string
if res.Namespace == "" {
resource = fmt.Sprintf("%s/%s: %s", res.APIVersion, res.Kind, res.Name)
} else {
resource = fmt.Sprintf("%s/%s: %s/%s", res.APIVersion, res.Kind, res.Namespace, res.Name)
}

return strings.Trim(resource, "/")
}
54 changes: 54 additions & 0 deletions pkg/target/teams/card.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package teams

import (
"github.com/atc0005/go-teams-notify/v2/adaptivecard"
"github.com/kyverno/policy-reporter/pkg/helper"
)

func newFactSet() adaptivecard.Element {
factSet := adaptivecard.Element{
Type: adaptivecard.TypeElementFactSet,
}

return factSet
}

func newFactSetPointer() *adaptivecard.Element {
factSet := newFactSet()

return &factSet
}

func newSubTitle(title string) adaptivecard.Element {
text := adaptivecard.NewTextBlock(title, true)
text.Weight = adaptivecard.WeightBolder
text.IsSubtle = true

return text
}

func MapToColumnSet(list map[string]string) adaptivecard.Element {
i := 0

first := adaptivecard.NewColumn()
first.Items = append(first.Items, newFactSetPointer())

second := adaptivecard.NewColumn()
second.Items = append(second.Items, newFactSetPointer())

propBlock := adaptivecard.NewColumnSet()
propBlock.Columns = []adaptivecard.Column{first, second}

for property, value := range list {
index := i % 2

propBlock.Columns[index].Items[0].Facts = append(propBlock.Columns[index].Items[0].Facts, adaptivecard.Fact{
Title: helper.Title(property),
Value: value,
})

i++
}

return propBlock
}
29 changes: 29 additions & 0 deletions pkg/target/teams/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package teams

import (
"net/http"

teams "github.com/atc0005/go-teams-notify/v2"
"github.com/atc0005/go-teams-notify/v2/adaptivecard"
)

type APIClient interface {
PostMessage(*adaptivecard.Message) error
}

type apiClient struct {
webhook string
client *teams.TeamsClient
}

func (c *apiClient) PostMessage(message *adaptivecard.Message) error {
return c.client.Send(c.webhook, message)
}

func NewAPIClient(webhook string, client *http.Client) APIClient {
msTeams := teams.NewTeamsClient()
msTeams.SetHTTPClient(client)
msTeams.SetUserAgent("Policy-Reporter")

return &apiClient{webhook: webhook, client: msTeams}
}
16 changes: 16 additions & 0 deletions pkg/target/teams/client_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package teams_test

import (
"net/http"
"testing"

"github.com/stretchr/testify/assert"

"github.com/kyverno/policy-reporter/pkg/target/teams"
)

func TestNewAPI(t *testing.T) {
h := &http.Client{}

assert.NotNil(t, teams.NewAPIClient("http://webhook:8080", h))
}
Loading

0 comments on commit 4dd1d7d

Please sign in to comment.