-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: New Microsoft Teams alert channel (#315)
Includes implementation into the CLI and API packages. Signed-off-by: Darren Murray <[email protected]>
- Loading branch information
1 parent
4b4efbb
commit e414226
Showing
6 changed files
with
473 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
) | ||
|
||
func main() { | ||
lacework, err := api.NewClient("account", api.WithApiKeys("KEY", "SECRET")) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
myTeamsChannel := api.NewMicrosoftTeamsAlertChannel("microsoft-teams-alert-from-golang", | ||
api.MicrosoftTeamsChannelData{ | ||
WebhookURL: "https://outlook.office.com/webhook/api-token", | ||
}, | ||
) | ||
|
||
response, err := lacework.Integrations.CreateMicrosoftTeamsAlertChannel(myTeamsChannel) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
// Output: Microsoft Teams alert channel created: THE-INTEGRATION-GUID | ||
fmt.Printf("Microsoft Teams alert channel created: %s", response.Data[0].IntgGuid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// Author:: Darren Murray(<[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 api | ||
|
||
// NewMicrosoftTeamsAlertChannel returns an instance of MicrosoftTeamsAlertChannel | ||
// with the provided name and data. | ||
// | ||
// Basic usage: Initialize a new MicrosoftTeamsAlertChannel struct, then | ||
// use the new instance to do CRUD operations | ||
// | ||
// client, err := api.NewClient("account") | ||
// if err != nil { | ||
// return err | ||
// } | ||
// | ||
// microsoftTeamsChannel := api.NewMicrosoftTeamsAlertChannel("foo", | ||
// api.MicrosoftTeamsChannelData{ | ||
// WebhookURL: "https://outlook.office.com/webhook/api-token", | ||
// }, | ||
// ) | ||
// | ||
// client.Integrations.CreateMicrosoftTeamsAlertChannel(microsoftTeamsChannel) | ||
// | ||
func NewMicrosoftTeamsAlertChannel(name string, data MicrosoftTeamsChannelData) MicrosoftTeamsAlertChannel { | ||
return MicrosoftTeamsAlertChannel{ | ||
commonIntegrationData: commonIntegrationData{ | ||
Name: name, | ||
Type: MicrosoftTeamsChannelIntegration.String(), | ||
Enabled: 1, | ||
}, | ||
Data: data, | ||
} | ||
} | ||
|
||
// CreateMicrosoftTeamsAlertChannel creates a msTeams alert channel integration on the Lacework Server | ||
func (svc *IntegrationsService) CreateMicrosoftTeamsAlertChannel(integration MicrosoftTeamsAlertChannel) ( | ||
response MicrosoftTeamsAlertChannelResponse, | ||
err error, | ||
) { | ||
err = svc.create(integration, &response) | ||
return | ||
} | ||
|
||
// GetMicrosoftTeamsAlertChannel gets a msTeams alert channel integration that matches with | ||
// the provided integration guid on the Lacework Server | ||
func (svc *IntegrationsService) GetMicrosoftTeamsAlertChannel(guid string) (response MicrosoftTeamsAlertChannelResponse, | ||
err error) { | ||
err = svc.get(guid, &response) | ||
return | ||
} | ||
|
||
// UpdateMicrosoftTeamsAlertChannel updates a single msTeams alert channel integration | ||
func (svc *IntegrationsService) UpdateMicrosoftTeamsAlertChannel(data MicrosoftTeamsAlertChannel) ( | ||
response MicrosoftTeamsAlertChannelResponse, | ||
err error, | ||
) { | ||
err = svc.update(data.IntgGuid, data, &response) | ||
return | ||
} | ||
|
||
// ListMicrosoftTeamsAlertChannel lists the Microsoft Teams external integrations available on the Lacework Server | ||
func (svc *IntegrationsService) ListMicrosoftTeamsAlertChannel() (response MicrosoftTeamsAlertChannelResponse, err error) { | ||
err = svc.listByType(MicrosoftTeamsChannelIntegration, &response) | ||
return | ||
} | ||
|
||
type MicrosoftTeamsAlertChannelResponse struct { | ||
Data []MicrosoftTeamsAlertChannel `json:"data"` | ||
Ok bool `json:"ok"` | ||
Message string `json:"message"` | ||
} | ||
|
||
type MicrosoftTeamsAlertChannel struct { | ||
commonIntegrationData | ||
Data MicrosoftTeamsChannelData `json:"DATA"` | ||
} | ||
|
||
type MicrosoftTeamsChannelData struct { | ||
WebhookURL string `json:"TEAMS_URL" mapstructure:"TEAMS_URL"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,242 @@ | ||
// | ||
// Author:: Darren Murray (<[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 api_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
"github.com/lacework/go-sdk/internal/intgguid" | ||
"github.com/lacework/go-sdk/internal/lacework" | ||
) | ||
|
||
func TestIntegrationsNewMicrosoftTeamsAlertChannel(t *testing.T) { | ||
subject := api.NewMicrosoftTeamsAlertChannel("integration_name", | ||
api.MicrosoftTeamsChannelData{ | ||
WebhookURL: "https://outlook.office.com/webhook/api-token", | ||
}, | ||
) | ||
assert.Equal(t, api.MicrosoftTeamsChannelIntegration.String(), subject.Type) | ||
} | ||
|
||
func TestIntegrationsCreateMicrosoftTeamsAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI("external/integrations", func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "POST", r.Method, "CreateMicrosoftTeamsAlertChannel should be a POST method") | ||
|
||
if assert.NotNil(t, r.Body) { | ||
body := httpBodySniffer(r) | ||
assert.Contains(t, body, "integration_name", "integration name is missing") | ||
assert.Contains(t, body, "MICROSOFT_TEAMS", "wrong integration type") | ||
assert.Contains(t, body, "https://outlook.office.com/webhook/api-token", "wrong microsoft Teams url") | ||
assert.Contains(t, body, "ENABLED\":1", "integration is not enabled") | ||
} | ||
|
||
fmt.Fprintf(w, microsoftTeamsChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
data := api.NewMicrosoftTeamsAlertChannel("integration_name", | ||
api.MicrosoftTeamsChannelData{ | ||
WebhookURL: "https://outlook.office.com/webhook/api-token", | ||
}, | ||
) | ||
assert.Equal(t, "integration_name", data.Name, "MicrosoftTeams integration name mismatch") | ||
assert.Equal(t, "MICROSOFT_TEAMS", data.Type, "a new Microsoft Teams integration should match its type") | ||
assert.Equal(t, 1, data.Enabled, "a new MicrosoftTeams integration should be enabled") | ||
|
||
response, err := c.Integrations.CreateMicrosoftTeamsAlertChannel(data) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
if assert.Equal(t, 1, len(response.Data)) { | ||
resData := response.Data[0] | ||
assert.Equal(t, intgGUID, resData.IntgGuid) | ||
assert.Equal(t, "integration_name", resData.Name) | ||
assert.True(t, resData.State.Ok) | ||
assert.Equal(t, "https://outlook.office.com/webhook/api-token", resData.Data.WebhookURL) | ||
} | ||
} | ||
|
||
func TestIntegrationsGetMicrosoftTeamsAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("external/integrations/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "GET", r.Method, "GetMicrosoftTeamsAlertChannel should be a GET method") | ||
fmt.Fprintf(w, microsoftTeamsChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
response, err := c.Integrations.GetMicrosoftTeamsAlertChannel(intgGUID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
if assert.Equal(t, 1, len(response.Data)) { | ||
resData := response.Data[0] | ||
assert.Equal(t, intgGUID, resData.IntgGuid) | ||
assert.Equal(t, "integration_name", resData.Name) | ||
assert.True(t, resData.State.Ok) | ||
assert.Equal(t, "https://outlook.office.com/webhook/api-token", resData.Data.WebhookURL) | ||
} | ||
} | ||
|
||
func TestIntegrationsUpdateMicrosoftTeamsAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("external/integrations/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "PATCH", r.Method, "UpdateMicrosoftTeamsAlertChannel should be a PATCH method") | ||
|
||
if assert.NotNil(t, r.Body) { | ||
body := httpBodySniffer(r) | ||
assert.Contains(t, body, intgGUID, "INTG_GUID missing") | ||
assert.Contains(t, body, "integration_name", "integration name is missing") | ||
assert.Contains(t, body, "MICROSOFT_TEAMS", "wrong integration type") | ||
assert.Contains(t, body, "https://outlook.office.com/webhook/api-token", "wrong microsoftTeams url") | ||
assert.Contains(t, body, "ENABLED\":1", "integration is not enabled") | ||
} | ||
|
||
fmt.Fprintf(w, microsoftTeamsChannelIntegrationJsonResponse(intgGUID)) | ||
}) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
data := api.NewMicrosoftTeamsAlertChannel("integration_name", | ||
api.MicrosoftTeamsChannelData{ | ||
WebhookURL: "https://outlook.office.com/webhook/api-token", | ||
}, | ||
) | ||
assert.Equal(t, "integration_name", data.Name, "MicrosoftTeams integration name mismatch") | ||
assert.Equal(t, "MICROSOFT_TEAMS", data.Type, "a new MicrosoftTeams integration should match its type") | ||
assert.Equal(t, 1, data.Enabled, "a new MicrosoftTeams integration should be enabled") | ||
data.IntgGuid = intgGUID | ||
|
||
response, err := c.Integrations.UpdateMicrosoftTeamsAlertChannel(data) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.Equal(t, "SUCCESS", response.Message) | ||
assert.Equal(t, 1, len(response.Data)) | ||
assert.Equal(t, intgGUID, response.Data[0].IntgGuid) | ||
} | ||
|
||
func TestIntegrationsListMicrosoftTeamsAlertChannel(t *testing.T) { | ||
var ( | ||
intgGUIDs = []string{intgguid.New(), intgguid.New(), intgguid.New()} | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.MockAPI("external/integrations/type/MICROSOFT_TEAMS", | ||
func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "GET", r.Method, "ListMicrosoftTeamsAlertChannel should be a GET method") | ||
fmt.Fprintf(w, microsoftTeamsChanMultiIntegrationJsonResponse(intgGUIDs)) | ||
}, | ||
) | ||
defer fakeServer.Close() | ||
|
||
c, err := api.NewClient("test", | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
response, err := c.Integrations.ListMicrosoftTeamsAlertChannel() | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.True(t, response.Ok) | ||
assert.Equal(t, len(intgGUIDs), len(response.Data)) | ||
for _, d := range response.Data { | ||
assert.Contains(t, intgGUIDs, d.IntgGuid) | ||
} | ||
} | ||
|
||
func microsoftTeamsChannelIntegrationJsonResponse(intgGUID string) string { | ||
return ` | ||
{ | ||
"data": [` + singleMicrosoftTeamsChanIntegration(intgGUID) + `], | ||
"ok": true, | ||
"message": "SUCCESS" | ||
} | ||
` | ||
} | ||
|
||
func microsoftTeamsChanMultiIntegrationJsonResponse(guids []string) string { | ||
integrations := []string{} | ||
for _, guid := range guids { | ||
integrations = append(integrations, singleMicrosoftTeamsChanIntegration(guid)) | ||
} | ||
return ` | ||
{ | ||
"data": [` + strings.Join(integrations, ", ") + `], | ||
"ok": true, | ||
"message": "SUCCESS" | ||
} | ||
` | ||
} | ||
|
||
func singleMicrosoftTeamsChanIntegration(id string) string { | ||
return ` | ||
{ | ||
"INTG_GUID": "` + id + `", | ||
"CREATED_OR_UPDATED_BY": "[email protected]", | ||
"CREATED_OR_UPDATED_TIME": "2020-Jul-16 19:59:22 UTC", | ||
"DATA": { | ||
"TEAMS_URL": "https://outlook.office.com/webhook/api-token" | ||
}, | ||
"ENABLED": 1, | ||
"IS_ORG": 0, | ||
"NAME": "integration_name", | ||
"STATE": { | ||
"lastSuccessfulTime": "2020-Jul-16 18:26:54 UTC", | ||
"lastUpdatedTime": "2020-Jul-16 18:26:54 UTC", | ||
"ok": true | ||
}, | ||
"TYPE": "MICROSOFT_TEAMS", | ||
"TYPE_NAME": "MICROSOFT_TEAMS" | ||
} | ||
` | ||
} |
Oops, something went wrong.