Skip to content

Commit

Permalink
feat: new Microsoft Teams alert channel resource (#68)
Browse files Browse the repository at this point in the history
* feat: new Microsoft Teams alert channel resource

Signed-off-by: Darren Murray <darren.murray@lacework.net>
  • Loading branch information
dmurray-lacework committed May 27, 2021
1 parent 428fe6f commit 1f467c6
Show file tree
Hide file tree
Showing 12 changed files with 542 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
provider "lacework" {}

resource "lacework_alert_channel_microsoft_teams" "example" {
name = "Microsoft Teams Channel Alert Example"
webhook_url = "https://outlook.office.com/webhook/api-token"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/hashicorp/terraform-plugin-test v1.4.3 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136 // indirect
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/lacework/go-sdk v0.2.18-0.20210205172138-8298022367d9
github.com/lacework/go-sdk v0.2.19-0.20210211001048-e41422624b52
github.com/mattn/go-colorable v0.1.7 // indirect
github.com/mitchellh/cli v1.1.1 // indirect
github.com/mitchellh/go-homedir v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ github.com/lacework/go-sdk v0.2.17 h1:YfM4CwlmLE4ijArTKmnHv+x1JZdb00o9ptqvMr6FNr
github.com/lacework/go-sdk v0.2.17/go.mod h1:yiEjWVHT4TjkZZ1pa9eS8fIfnnoVuzj1VNTIVEIsSKE=
github.com/lacework/go-sdk v0.2.18-0.20210205172138-8298022367d9 h1:MPyhrKjrilbcc7NzWx8u3sg3jf1QIotCtkJN6WU5MOM=
github.com/lacework/go-sdk v0.2.18-0.20210205172138-8298022367d9/go.mod h1:yiEjWVHT4TjkZZ1pa9eS8fIfnnoVuzj1VNTIVEIsSKE=
github.com/lacework/go-sdk v0.2.19-0.20210211001048-e41422624b52 h1:XgcQ1sTXFRqqVQTGKWOK+7H0bs0u5dMnJEwszzOCRFw=
github.com/lacework/go-sdk v0.2.19-0.20210211001048-e41422624b52/go.mod h1:yiEjWVHT4TjkZZ1pa9eS8fIfnnoVuzj1VNTIVEIsSKE=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
Expand Down
45 changes: 23 additions & 22 deletions lacework/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,28 +47,29 @@ func Provider() terraform.ResourceProvider {
},

ResourcesMap: map[string]*schema.Resource{
"lacework_agent_access_token": resourceLaceworkAgentAccessToken(),
"lacework_alert_channel_aws_cloudwatch": resourceLaceworkAlertChannelAwsCloudWatch(),
"lacework_alert_channel_aws_s3": resourceLaceworkAlertChannelAwsS3(),
"lacework_alert_channel_datadog": resourceLaceworkAlertChannelDatadog(),
"lacework_alert_channel_gcp_pub_sub": resourceLaceworkAlertChannelGcpPubSub(),
"lacework_alert_channel_jira_cloud": resourceLaceworkAlertChannelJiraCloud(),
"lacework_alert_channel_jira_server": resourceLaceworkAlertChannelJiraServer(),
"lacework_alert_channel_pagerduty": resourceLaceworkAlertChannelPagerDuty(),
"lacework_alert_channel_slack": resourceLaceworkAlertChannelSlack(),
"lacework_alert_channel_splunk": resourceLaceworkAlertChannelSplunk(),
"lacework_alert_channel_service_now": resourceLaceworkAlertChannelServiceNow(),
"lacework_alert_channel_webhook": resourceLaceworkAlertChannelWebhook(),
"lacework_integration_aws_cfg": resourceLaceworkIntegrationAwsCfg(),
"lacework_integration_aws_ct": resourceLaceworkIntegrationAwsCloudTrail(),
"lacework_integration_azure_cfg": resourceLaceworkIntegrationAzureCfg(),
"lacework_integration_azure_al": resourceLaceworkIntegrationAzureActivityLog(),
"lacework_integration_docker_hub": resourceLaceworkIntegrationDockerHub(),
"lacework_integration_docker_v2": resourceLaceworkIntegrationDockerV2(),
"lacework_integration_ecr": resourceLaceworkIntegrationEcr(),
"lacework_integration_gcp_cfg": resourceLaceworkIntegrationGcpCfg(),
"lacework_integration_gcp_at": resourceLaceworkIntegrationGcpAt(),
"lacework_integration_gcr": resourceLaceworkIntegrationGcr(),
"lacework_agent_access_token": resourceLaceworkAgentAccessToken(),
"lacework_alert_channel_aws_cloudwatch": resourceLaceworkAlertChannelAwsCloudWatch(),
"lacework_alert_channel_aws_s3": resourceLaceworkAlertChannelAwsS3(),
"lacework_alert_channel_datadog": resourceLaceworkAlertChannelDatadog(),
"lacework_alert_channel_gcp_pub_sub": resourceLaceworkAlertChannelGcpPubSub(),
"lacework_alert_channel_jira_cloud": resourceLaceworkAlertChannelJiraCloud(),
"lacework_alert_channel_jira_server": resourceLaceworkAlertChannelJiraServer(),
"lacework_alert_channel_pagerduty": resourceLaceworkAlertChannelPagerDuty(),
"lacework_alert_channel_microsoft_teams": resourceLaceworkAlertChannelMicrosoftTeams(),
"lacework_alert_channel_slack": resourceLaceworkAlertChannelSlack(),
"lacework_alert_channel_splunk": resourceLaceworkAlertChannelSplunk(),
"lacework_alert_channel_service_now": resourceLaceworkAlertChannelServiceNow(),
"lacework_alert_channel_webhook": resourceLaceworkAlertChannelWebhook(),
"lacework_integration_aws_cfg": resourceLaceworkIntegrationAwsCfg(),
"lacework_integration_aws_ct": resourceLaceworkIntegrationAwsCloudTrail(),
"lacework_integration_azure_cfg": resourceLaceworkIntegrationAzureCfg(),
"lacework_integration_azure_al": resourceLaceworkIntegrationAzureActivityLog(),
"lacework_integration_docker_hub": resourceLaceworkIntegrationDockerHub(),
"lacework_integration_docker_v2": resourceLaceworkIntegrationDockerV2(),
"lacework_integration_ecr": resourceLaceworkIntegrationEcr(),
"lacework_integration_gcp_cfg": resourceLaceworkIntegrationGcpCfg(),
"lacework_integration_gcp_at": resourceLaceworkIntegrationGcpAt(),
"lacework_integration_gcr": resourceLaceworkIntegrationGcr(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down
208 changes: 208 additions & 0 deletions lacework/resource_lacework_alert_channel_microsoft_teams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
package lacework

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/lacework/go-sdk/api"
)

func resourceLaceworkAlertChannelMicrosoftTeams() *schema.Resource {
return &schema.Resource{
Create: resourceLaceworkAlertChannelMicrosoftTeamsCreate,
Read: resourceLaceworkAlertChannelMicrosoftTeamsRead,
Update: resourceLaceworkAlertChannelMicrosoftTeamsUpdate,
Delete: resourceLaceworkAlertChannelMicrosoftTeamsDelete,

Importer: &schema.ResourceImporter{
State: importLaceworkIntegration,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"intg_guid": {
Type: schema.TypeString,
Computed: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"webhook_url": {
Type: schema.TypeString,
Required: true,
},
"created_or_updated_time": {
Type: schema.TypeString,
Computed: true,
},
"created_or_updated_by": {
Type: schema.TypeString,
Computed: true,
},
"type_name": {
Type: schema.TypeString,
Computed: true,
},
"org_level": {
Type: schema.TypeBool,
Computed: true,
},
},
}
}

func resourceLaceworkAlertChannelMicrosoftTeamsCreate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
WebhookURL: d.Get("webhook_url").(string),
},
)
)
if !d.Get("enabled").(bool) {
microsoftTeams.Enabled = 0
}

log.Printf("[INFO] Creating %s integration with data:\n%+v\n", api.MicrosoftTeamsChannelIntegration, microsoftTeams)
response, err := lacework.Integrations.CreateMicrosoftTeamsAlertChannel(microsoftTeams)
if err != nil {
return err
}

log.Println("[INFO] Verifying server response data")
err = validateMicrosoftTeamsAlertChannelResponse(&response)
if err != nil {
return err
}

integration := response.Data[0]
d.SetId(integration.IntgGuid)
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)

log.Printf("[INFO] Created %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, integration.IntgGuid)
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsRead(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Reading %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, d.Id())
response, err := lacework.Integrations.GetMicrosoftTeamsAlertChannel(d.Id())
if err != nil {
return err
}

for _, integration := range response.Data {
if integration.IntgGuid == d.Id() {
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)
d.Set("webhook_url", integration.Data.WebhookURL)

log.Printf("[INFO] Read %s integration with guid: %v\n",
api.MicrosoftTeamsChannelIntegration, integration.IntgGuid)
return nil
}
}

d.SetId("")
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsUpdate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
microsoftTeams = api.NewMicrosoftTeamsAlertChannel(d.Get("name").(string),
api.MicrosoftTeamsChannelData{
WebhookURL: d.Get("webhook_url").(string),
},
)
)

if !d.Get("enabled").(bool) {
microsoftTeams.Enabled = 0
}

microsoftTeams.IntgGuid = d.Id()

log.Printf("[INFO] Updating %s integration with data:\n%+v\n", api.MicrosoftTeamsChannelIntegration, microsoftTeams)
response, err := lacework.Integrations.UpdateMicrosoftTeamsAlertChannel(microsoftTeams)
if err != nil {
return err
}

log.Println("[INFO] Verifying server response data")
err = validateMicrosoftTeamsAlertChannelResponse(&response)
if err != nil {
return err
}

integration := response.Data[0]
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)

log.Printf("[INFO] Updated %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, d.Id())
return nil
}

func resourceLaceworkAlertChannelMicrosoftTeamsDelete(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Deleting %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, d.Id())
_, err := lacework.Integrations.Delete(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleted %s integration with guid: %v\n", api.MicrosoftTeamsChannelIntegration, d.Id())
return nil
}

func validateMicrosoftTeamsAlertChannelResponse(response *api.MicrosoftTeamsAlertChannelResponse) error {
if len(response.Data) == 0 {
msg := `
Unable to read sever response data. (empty 'data' field)
This was an unexpected behavior, verify that your integration has been
created successfully and report this issue to support@lacework.net
`
return fmt.Errorf(msg)
}

if len(response.Data) > 1 {
msg := `
There is more that one integration inside the server response data.
List of integrations:
`
for _, integration := range response.Data {
msg = msg + fmt.Sprintf("\t%s: %s\n", integration.IntgGuid, integration.Name)
}
msg = msg + unexpectedBehaviorMsg()
return fmt.Errorf(msg)
}

return nil
}
Loading

0 comments on commit 1f467c6

Please sign in to comment.