From 4624dfb6524f653f98eae7c87357d99c247e6f6c Mon Sep 17 00:00:00 2001 From: Salim Afiune Maya Date: Mon, 26 Jul 2021 09:03:38 -0500 Subject: [PATCH] feat(cli): manage EmailUser alert channels Signed-off-by: Salim Afiune Maya --- api/integrations.go | 4 +++ cli/cmd/integration.go | 18 ++++++++++ cli/cmd/integration_email.go | 68 ++++++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+) create mode 100644 cli/cmd/integration_email.go diff --git a/api/integrations.go b/api/integrations.go index 76f52e461..c1f139cee 100644 --- a/api/integrations.go +++ b/api/integrations.go @@ -100,6 +100,9 @@ const ( // Jira integration type JiraIntegration + // Email integration type + EmailIntegration + // VictorOps channel integration type VictorOpsChannelIntegration @@ -132,6 +135,7 @@ var IntegrationTypes = map[integrationType]string{ AwsCloudWatchIntegration: "CLOUDWATCH_EB", PagerDutyIntegration: "PAGER_DUTY_API", JiraIntegration: "JIRA", + EmailIntegration: "EMAIL_USER", VictorOpsChannelIntegration: "VICTOR_OPS", WebhookIntegration: "WEBHOOK", } diff --git a/cli/cmd/integration.go b/cli/cmd/integration.go index aef4554d3..f44d898e4 100644 --- a/cli/cmd/integration.go +++ b/cli/cmd/integration.go @@ -20,6 +20,7 @@ package cmd import ( "fmt" + "strings" "github.com/AlecAivazis/survey/v2" "github.com/mitchellh/mapstructure" @@ -200,6 +201,7 @@ func promptCreateIntegration() error { Message: "Choose an integration type to create: ", Options: []string{ "Slack Alert Channel", + "Email Alert Channel", "AWS S3 Alert Channel", "Cisco Webex Alert Channel", "Datadog Alert Channel", @@ -239,6 +241,8 @@ func promptCreateIntegration() error { switch integration { case "Slack Alert Channel": return createSlackAlertChannelIntegration() + case "Email Alert Channel": + return createEmailAlertChannelIntegration() case "GCP PubSub Alert Channel": return createGcpPubSubChannelIntegration() case "Microsoft Teams Alert Channel": @@ -820,6 +824,20 @@ func reflectIntegrationData(raw api.RawIntegration) [][]string { return out + case api.EmailIntegration.String(): + // Use v2 endpoint for Email Alert Channel + emailAlertChan, err := cli.LwApi.V2.AlertChannels.GetEmailUser(raw.IntgGuid) + if err != nil { + cli.Log.Debugw("unable to get EmailUser Alert Channel (v2)", + "error", err.Error(), + ) + break + } + return [][]string{ + {"RECIPIENTS", + strings.Join(emailAlertChan.Data.Data.ChannelProps.Recipients, "\n")}, + } + case api.JiraIntegration.String(): var iData api.JiraAlertChannelData diff --git a/cli/cmd/integration_email.go b/cli/cmd/integration_email.go new file mode 100644 index 000000000..c10cf989d --- /dev/null +++ b/cli/cmd/integration_email.go @@ -0,0 +1,68 @@ +// +// Author:: Salim Afiune Maya () +// Copyright:: Copyright 2021, Lacework Inc. +// License:: Apache License, Version 2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +package cmd + +import ( + "strings" + + "github.com/AlecAivazis/survey/v2" + + "github.com/lacework/go-sdk/api" +) + +func createEmailAlertChannelIntegration() error { + questions := []*survey.Question{ + { + Name: "name", + Prompt: &survey.Input{Message: "Name: "}, + Validate: survey.Required, + }, + { + Name: "recipients", + Prompt: &survey.Multiline{Message: "List of Recipients: "}, + Validate: survey.Required, + }, + } + + answers := struct { + Name string + Recipients string + }{} + + err := survey.Ask(questions, &answers, + survey.WithIcons(promptIconsFunc), + ) + if err != nil { + return err + } + + emailAlertChan := api.NewAlertChannel(answers.Name, + api.EmailUserAlertChannel, + api.EmailUserData{ + ChannelProps: api.EmailUserChannelProps{ + Recipients: strings.Split(answers.Recipients, "\n"), + }, + }, + ) + + cli.StartProgress(" Creating integration...") + _, err = cli.LwApi.V2.AlertChannels.Create(emailAlertChan) + cli.StopProgress() + return err +}