Skip to content

Commit

Permalink
✨ implement MockConnect for slack provider
Browse files Browse the repository at this point in the history
Signed-off-by: Dominik Richter <[email protected]>
  • Loading branch information
arlimus committed Sep 26, 2023
1 parent 970e46f commit 7bd2518
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions providers/slack/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ type SlackConnection struct {
teamInfo *slack.TeamInfo
}

func NewMockConnection(id uint32, asset *inventory.Asset, conf *inventory.Config) *SlackConnection {
return &SlackConnection{
Conf: conf,
id: id,
asset: asset,
}
}

func NewSlackConnection(id uint32, asset *inventory.Asset, conf *inventory.Config) (*SlackConnection, error) {
sc := &SlackConnection{
Conf: conf,
Expand Down
31 changes: 30 additions & 1 deletion providers/slack/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,33 @@ func (s *Service) Shutdown(req *plugin.ShutdownReq) (*plugin.ShutdownRes, error)
}

func (s *Service) MockConnect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) {
return nil, errors.New("mock connect not yet implemented")
if req == nil || req.Asset == nil {
return nil, errors.New("no connection data provided")
}

asset := &inventory.Asset{
PlatformIds: req.Asset.PlatformIds,
Platform: req.Asset.Platform,
Connections: []*inventory.Config{{
Type: "mock",
}},
}

conn, err := s.connect(&plugin.ConnectReq{
Features: req.Features,
Upstream: req.Upstream,
Asset: asset,
}, callback)
if err != nil {
return nil, err
}

return &plugin.ConnectRes{
Id: uint32(conn.ID()),
Name: conn.Name(),
Asset: asset,
Inventory: nil,
}, nil
}

func (s *Service) Connect(req *plugin.ConnectReq, callback plugin.ProviderCallback) (*plugin.ConnectRes, error) {
Expand Down Expand Up @@ -108,6 +134,9 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
var err error

switch conf.Type {
case "mock":
s.lastConnectionID++
conn = connection.NewMockConnection(s.lastConnectionID, asset, conf)
default:
s.lastConnectionID++
conn, err = connection.NewSlackConnection(s.lastConnectionID, asset, conf)
Expand Down
4 changes: 4 additions & 0 deletions providers/slack/resources/accesslogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resources

import (
"errors"
"time"

"github.com/slack-go/slack"
Expand All @@ -15,6 +16,9 @@ import (
func (s *mqlSlack) accessLogs() ([]interface{}, error) {
conn := s.MqlRuntime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

accessLogs, _, err := client.GetAccessLogs(slack.AccessLogParameters{
Count: 1000,
Expand Down
7 changes: 7 additions & 0 deletions providers/slack/resources/conversations.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package resources

import (
"errors"
"time"

"github.com/slack-go/slack"
Expand All @@ -16,6 +17,9 @@ import (
func (s *mqlSlack) conversations() ([]interface{}, error) {
conn := s.MqlRuntime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

list := []interface{}{}

Expand Down Expand Up @@ -135,6 +139,9 @@ func (x *mqlSlackConversation) id() (string, error) {
func (s *mqlSlackConversation) members() ([]interface{}, error) {
conn := s.MqlRuntime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

var list []interface{}
isChannel := s.IsChannel.Data
Expand Down
6 changes: 6 additions & 0 deletions providers/slack/resources/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package resources

import (
"errors"

"go.mondoo.com/cnquery/llx"
"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
"go.mondoo.com/cnquery/providers/slack/connection"
Expand All @@ -17,6 +19,10 @@ func (x *mqlSlackTeam) id() (string, error) {
func initSlackTeam(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[string]*llx.RawData, plugin.Resource, error) {
conn := runtime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

teamInfo, err := client.GetTeamInfo()
if err != nil {
return nil, nil, err
Expand Down
4 changes: 4 additions & 0 deletions providers/slack/resources/usergroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package resources

import (
"context"
"errors"

"github.com/slack-go/slack"
"go.mondoo.com/cnquery/llx"
Expand All @@ -15,6 +16,9 @@ import (
func (s *mqlSlack) userGroups() ([]interface{}, error) {
conn := s.MqlRuntime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

// requires usergroups:read scope
ctx := context.Background()
Expand Down
7 changes: 7 additions & 0 deletions providers/slack/resources/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (o *mqlSlackUsers) id() (string, error) {
func (s *mqlSlackUsers) list() ([]interface{}, error) {
conn := s.MqlRuntime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

ctx := context.Background()

// requires users:read scope
Expand Down Expand Up @@ -238,6 +242,9 @@ func initSlackUser(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[s

conn := runtime.Connection.(*connection.SlackConnection)
client := conn.Client()
if client == nil {
return nil, errors.New("cannot retrieve new data while using a mock connection")
}

users, err := client.GetUsersInfo(id)
if err != nil {
Expand Down

0 comments on commit 7bd2518

Please sign in to comment.