-
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(api): SlackChannel Alert Channel (v2)
Signed-off-by: Salim Afiune Maya <[email protected]>
- Loading branch information
Showing
2 changed files
with
146 additions
and
3 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,51 @@ | ||
// | ||
// Author:: Salim Afiune Maya (<[email protected]>) | ||
// 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 api | ||
|
||
// GetSlackChannel gets a single SlackChannel alert channel matching the | ||
// provided integration guid | ||
func (svc *AlertChannelsService) GetSlackChannel(guid string) ( | ||
response SlackChannelAlertChannelResponseV2, | ||
err error, | ||
) { | ||
err = svc.get(guid, &response) | ||
return | ||
} | ||
|
||
// UpdateSlackChannel updates a single SlackChannel integration on the Lacework Server | ||
func (svc *AlertChannelsService) UpdateSlackChannel(data AlertChannel) ( | ||
response SlackChannelAlertChannelResponseV2, | ||
err error, | ||
) { | ||
err = svc.update(data.ID(), data, &response) | ||
return | ||
} | ||
|
||
type SlackChannelAlertChannelResponseV2 struct { | ||
Data SlackChannelAlertChannelV2 `json:"data"` | ||
} | ||
|
||
type SlackChannelAlertChannelV2 struct { | ||
v2CommonIntegrationData | ||
Data SlackChannelDataV2 `json:"data"` | ||
} | ||
|
||
type SlackChannelDataV2 struct { | ||
SlackUrl string `json:"slackUrl"` | ||
} |
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 |
---|---|---|
|
@@ -18,10 +18,103 @@ | |
|
||
package api_test | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"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 TestAlertChannelsGetSlackChannel(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("AlertChannels/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.UseApiV2() | ||
fakeServer.MockToken("TOKEN") | ||
defer fakeServer.Close() | ||
|
||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "GET", r.Method, "GetSlackChannel() should be a GET method") | ||
fmt.Fprintf(w, generateAlertChannelResponse(singleSlackChannelAlertChannel(intgGUID))) | ||
}) | ||
|
||
c, err := api.NewClient("test", | ||
api.WithApiV2(), | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
response, err := c.V2.AlertChannels.GetSlackChannel(intgGUID) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.Equal(t, intgGUID, response.Data.IntgGuid) | ||
assert.Equal(t, "integration_name", response.Data.Name) | ||
assert.True(t, response.Data.State.Ok) | ||
assert.Contains(t, response.Data.Data.SlackUrl, "https://hooks.slack.com/services/abc/foo/secret123") | ||
} | ||
|
||
func TestAlertChannelsSlackChannelUpdate(t *testing.T) { | ||
var ( | ||
intgGUID = intgguid.New() | ||
apiPath = fmt.Sprintf("AlertChannels/%s", intgGUID) | ||
fakeServer = lacework.MockServer() | ||
) | ||
fakeServer.UseApiV2() | ||
fakeServer.MockToken("TOKEN") | ||
defer fakeServer.Close() | ||
|
||
fakeServer.MockAPI(apiPath, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "PATCH", r.Method, "UpdateSlackChannel() 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", "cloud account name is missing") | ||
assert.Contains(t, body, "SlackChannel", "wrong cloud account type") | ||
assert.Contains(t, body, "https://hooks.slack.com/services/abc/foo/secret123", "missing slack url") | ||
assert.Contains(t, body, "enabled\":1", "cloud account is not enabled") | ||
} | ||
|
||
fmt.Fprintf(w, generateAlertChannelResponse(singleSlackChannelAlertChannel(intgGUID))) | ||
}) | ||
|
||
c, err := api.NewClient("test", | ||
api.WithApiV2(), | ||
api.WithToken("TOKEN"), | ||
api.WithURL(fakeServer.URL()), | ||
) | ||
assert.Nil(t, err) | ||
|
||
emailAlertChan := api.NewAlertChannel("integration_name", | ||
api.SlackChannelAlertChannelType, | ||
api.SlackChannelData{ | ||
SlackUrl: "https://hooks.slack.com/services/abc/foo/secret123", | ||
}, | ||
) | ||
assert.Equal(t, "integration_name", emailAlertChan.Name, "SlackChannel cloud account name mismatch") | ||
assert.Equal(t, "SlackChannel", emailAlertChan.Type, "a new SlackChannel cloud account should match its type") | ||
assert.Equal(t, 1, emailAlertChan.Enabled, "a new SlackChannel cloud account should be enabled") | ||
emailAlertChan.IntgGuid = intgGUID | ||
|
||
response, err := c.V2.AlertChannels.UpdateSlackChannel(emailAlertChan) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, response) | ||
assert.Equal(t, intgGUID, response.Data.IntgGuid) | ||
assert.True(t, response.Data.State.Ok) | ||
assert.Contains(t, response.Data.Data.SlackUrl, "https://hooks.slack.com/services/abc/foo/secret123") | ||
} | ||
|
||
func singleSlackChannelAlertChannel(id string) string { | ||
return ` | ||
{ | ||
"data": { | ||
"createdOrUpdatedBy": "[email protected]", | ||
"createdOrUpdatedTime": "2021-06-01T18:10:40.745Z", | ||
"data": { | ||
|
@@ -30,15 +123,14 @@ func singleSlackChannelAlertChannel(id string) string { | |
"enabled": 1, | ||
"intgGuid": "` + id + `", | ||
"isOrg": 0, | ||
"name": "#tech-ally-notify", | ||
"name": "integration_name", | ||
"state": { | ||
"details": {}, | ||
"lastSuccessfulTime": 1627895573122, | ||
"lastUpdatedTime": 1627895573122, | ||
"ok": true | ||
}, | ||
"type": "SlackChannel" | ||
} | ||
} | ||
` | ||
} |