From bc6e4c440a986938d67d6a6f90efcbbcfd975718 Mon Sep 17 00:00:00 2001 From: Edward Dowling Date: Fri, 26 Jul 2024 11:57:32 +0100 Subject: [PATCH] Opsgenie Plugin support schedules/teams ID and Name (#44381) * Opsgenie Plugin support schedules/teams ID and Name Signed-off-by: Edward Dowling * Replace regex by UUID parser Signed-off-by: Edward Dowling --------- Signed-off-by: Edward Dowling Co-authored-by: Carlos Castro --- integrations/access/opsgenie/client.go | 27 +++++++++++++-------- integrations/access/opsgenie/client_test.go | 10 +++++--- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/integrations/access/opsgenie/client.go b/integrations/access/opsgenie/client.go index d44a892029232..9591d26834002 100644 --- a/integrations/access/opsgenie/client.go +++ b/integrations/access/opsgenie/client.go @@ -29,6 +29,7 @@ import ( "github.com/aws/aws-sdk-go/aws/defaults" "github.com/go-resty/resty/v2" + "github.com/google/uuid" "github.com/gravitational/trace" "github.com/jonboulle/clockwork" @@ -219,22 +220,28 @@ func (og Client) getResponders(reqData RequestData) []Responder { } responders := make([]Responder, 0, len(schedules)+len(teams)) for _, s := range schedules { - responders = append(responders, Responder{ - Type: ResponderTypeSchedule, - ID: s, - Name: s, - }) + responders = append(responders, createResponder(ResponderTypeSchedule, s)) } for _, t := range teams { - responders = append(responders, Responder{ - Type: ResponderTypeTeam, - ID: t, - Name: t, - }) + responders = append(responders, createResponder(ResponderTypeTeam, t)) } return responders } +// Check if the responder is a UUID. If it is, then it is an ID; otherwise, it is a name. +func createResponder(responderType string, value string) Responder { + if _, err := uuid.Parse(value); err == nil { + return Responder{ + Type: responderType, + ID: value, + } + } + return Responder{ + Type: responderType, + Name: value, + } +} + // PostReviewNote posts a note once a new request review appears. func (og Client) PostReviewNote(ctx context.Context, alertID string, review types.AccessReview) error { note, err := buildReviewNoteBody(review) diff --git a/integrations/access/opsgenie/client_test.go b/integrations/access/opsgenie/client_test.go index e61e5004c0bcf..d03871731b14f 100644 --- a/integrations/access/opsgenie/client_test.go +++ b/integrations/access/opsgenie/client_test.go @@ -59,8 +59,8 @@ func TestCreateAlert(t *testing.T) { Roles: []string{"role1", "role2"}, RequestReason: "someReason", SystemAnnotations: types.Labels{ - types.TeleportNamespace + types.ReqAnnotationNotifySchedulesLabel: {"responder@example.com"}, - types.TeleportNamespace + types.ReqAnnotationTeamsLabel: {"MyOpsGenieTeam"}, + types.TeleportNamespace + types.ReqAnnotationNotifySchedulesLabel: {"responder@example.com", "bb4d9938-c3c2-455d-aaab-727aa701c0d8"}, + types.TeleportNamespace + types.ReqAnnotationTeamsLabel: {"MyOpsGenieTeam", "aee8a0de-c80f-4515-a232-501c0bc9d715"}, }, }) assert.NoError(t, err) @@ -70,8 +70,10 @@ func TestCreateAlert(t *testing.T) { Alias: "teleport-access-request/someRequestID", Description: "someUser requested permissions for roles role1, role2 on Teleport at 01 Jan 01 00:00 UTC.\nReason: someReason\n\n", Responders: []Responder{ - {Type: "schedule", Name: "responder@example.com", ID: "responder@example.com"}, - {Type: "team", Name: "MyOpsGenieTeam", ID: "MyOpsGenieTeam"}, + {Type: "schedule", Name: "responder@example.com"}, + {Type: "schedule", ID: "bb4d9938-c3c2-455d-aaab-727aa701c0d8"}, + {Type: "team", Name: "MyOpsGenieTeam"}, + {Type: "team", ID: "aee8a0de-c80f-4515-a232-501c0bc9d715"}, }, Priority: "somePriority", }