Skip to content

Commit

Permalink
feat(cli): Create Jira Alert Channels 🚨
Browse files Browse the repository at this point in the history
Users now can create Jira Alert Channel Integrations via the CLI:
```
$ lacework integration create
? Choose an integration type to create:  Jira Cloud Alert Channel
â–¸ Name:  example-jira-alert
â–¸ Jira URL:  mycompany.atlassian.net
â–¸ Issue Type:  Bug
â–¸ Project Key:  EXAMPLE
â–¸ Username:  [email protected]
â–¸ API Token:  **********************
â–¸ Alert Severity Level:  High and above
The integration was created.
```

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune committed Aug 3, 2020
1 parent 0cdb2a4 commit 6ca8cef
Show file tree
Hide file tree
Showing 2 changed files with 212 additions and 0 deletions.
30 changes: 30 additions & 0 deletions cli/cmd/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,8 @@ func promptCreateIntegration() error {
"Slack Alert Channel",
"PagerDuty Alert Channel",
"AWS CloudWatch Alert Channel",
"Jira Cloud Alert Channel",
"Jira Server Alert Channel",
"Docker Hub",
"AWS Config",
"AWS CloudTrail",
Expand All @@ -219,6 +221,10 @@ func promptCreateIntegration() error {
return createPagerDutyAlertChannelIntegration()
case "AWS CloudWatch Alert Channel":
return createAwsCloudWatchAlertChannelIntegration()
case "Jira Cloud Alert Channel":
return createJiraCloudAlertChannelIntegration()
case "Jira Server Alert Channel":
return createJiraServerAlertChannelIntegration()
case "Docker Hub":
return createDockerHubIntegration()
case "AWS Config":
Expand Down Expand Up @@ -446,6 +452,30 @@ func reflectIntegrationData(raw api.RawIntegration) [][]string {

return out

case api.JiraIntegration.String():

var iData api.JiraAlertChannelData
err := mapstructure.Decode(raw.Data, &iData)
if err != nil {
cli.Log.Debugw("unable to decode integration data",
"integration_type", raw.Type,
"raw_data", raw.Data,
"error", err,
)
break
}
out := [][]string{
[]string{"JIRA INTEGRATION TYPE", iData.JiraType},
[]string{"JIRA URL", iData.JiraUrl},
[]string{"PROJECT KEY", iData.ProjectID},
[]string{"USERNAME", iData.Username},
[]string{"ISSUE TYPE", iData.IssueType},
[]string{"ISSUE GROUPING", iData.IssueGrouping},
[]string{"ALERT ON SEVERITY", iData.MinAlertSeverity.String()},
}

return out

default:
out := [][]string{}
for key, value := range deepKeyValueExtract(raw.Data) {
Expand Down
182 changes: 182 additions & 0 deletions cli/cmd/integration_jira.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
//
// Author:: Salim Afiune Maya (<[email protected]>)
// Copyright:: Copyright 2020, 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 (
"github.com/AlecAivazis/survey/v2"

"github.com/lacework/go-sdk/api"
)

type jiraAlertChannelIntegrationSurvey struct {
Name string
Url string
Issue string
Project string
Username string
Token string
Password string
AlertSeverity string `survey:"alert_severity_level"`
}

func createJiraCloudAlertChannelIntegration() error {
questions := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Name: "},
Validate: survey.Required,
},
{
Name: "url",
Prompt: &survey.Input{Message: "Jira URL: "},
Validate: survey.Required,
},
{
Name: "issue",
Prompt: &survey.Input{Message: "Issue Type: "},
Validate: survey.Required,
},
{
Name: "project",
Prompt: &survey.Input{Message: "Project Key: "},
Validate: survey.Required,
},
{
Name: "username",
Prompt: &survey.Input{Message: "Username: "},
Validate: survey.Required,
},
{
Name: "token",
Prompt: &survey.Password{Message: "API Token: "},
Validate: survey.Required,
},
{
Name: "alert_severity_level",
Prompt: &survey.Select{
Message: "Alert Severity Level: ",
Options: []string{
"Critical",
"High and above",
"Medium and above",
"Low and above",
"All",
},
},
Validate: survey.Required,
},
}

var answers jiraAlertChannelIntegrationSurvey
err := survey.Ask(questions, &answers,
survey.WithIcons(promptIconsFunc),
)
if err != nil {
return err
}

jira := api.NewJiraCloudAlertChannel(answers.Name,
api.JiraAlertChannelData{
JiraUrl: answers.Url,
IssueType: answers.Issue,
ProjectID: answers.Project,
Username: answers.Username,
ApiToken: answers.Token,
MinAlertSeverity: alertSeverityToEnum(answers.AlertSeverity),
},
)

return createJiraAlertChannelIntegration(jira)
}

func createJiraServerAlertChannelIntegration() error {
questions := []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "Name: "},
Validate: survey.Required,
},
{
Name: "url",
Prompt: &survey.Input{Message: "Jira URL: "},
Validate: survey.Required,
},
{
Name: "issue",
Prompt: &survey.Input{Message: "Issue Type: "},
Validate: survey.Required,
},
{
Name: "project",
Prompt: &survey.Input{Message: "Project Key: "},
Validate: survey.Required,
},
{
Name: "username",
Prompt: &survey.Input{Message: "Username: "},
Validate: survey.Required,
},
{
Name: "password",
Prompt: &survey.Password{Message: "Password: "},
Validate: survey.Required,
},
{
Name: "alert_severity_level",
Prompt: &survey.Select{
Message: "Alert Severity Level: ",
Options: []string{
"Critical",
"High and above",
"Medium and above",
"Low and above",
"All",
},
},
Validate: survey.Required,
},
}

var answers jiraAlertChannelIntegrationSurvey
err := survey.Ask(questions, &answers,
survey.WithIcons(promptIconsFunc),
)
if err != nil {
return err
}

jira := api.NewJiraServerAlertChannel(answers.Name,
api.JiraAlertChannelData{
JiraUrl: answers.Url,
IssueType: answers.Issue,
ProjectID: answers.Project,
Username: answers.Username,
Password: answers.Password,
MinAlertSeverity: alertSeverityToEnum(answers.AlertSeverity),
},
)
return createJiraAlertChannelIntegration(jira)
}

func createJiraAlertChannelIntegration(jira api.JiraAlertChannel) error {
cli.StartProgress(" Creating integration...")
_, err := cli.LwApi.Integrations.CreateJiraAlertChannel(jira)
cli.StopProgress()
return err
}

0 comments on commit 6ca8cef

Please sign in to comment.