From 7bd25186f2c0984f677978ba94ac72df62a0b8fc Mon Sep 17 00:00:00 2001 From: Dominik Richter Date: Tue, 26 Sep 2023 00:17:15 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20implement=20MockConnect=20for=20sla?= =?UTF-8?q?ck=20provider?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Dominik Richter --- providers/slack/connection/connection.go | 8 ++++++ providers/slack/provider/provider.go | 31 +++++++++++++++++++++- providers/slack/resources/accesslogs.go | 4 +++ providers/slack/resources/conversations.go | 7 +++++ providers/slack/resources/team.go | 6 +++++ providers/slack/resources/usergroups.go | 4 +++ providers/slack/resources/users.go | 7 +++++ 7 files changed, 66 insertions(+), 1 deletion(-) diff --git a/providers/slack/connection/connection.go b/providers/slack/connection/connection.go index 8d1d206d6a..6edc12a78e 100644 --- a/providers/slack/connection/connection.go +++ b/providers/slack/connection/connection.go @@ -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, diff --git a/providers/slack/provider/provider.go b/providers/slack/provider/provider.go index 670332f175..f4b3a1c17b 100644 --- a/providers/slack/provider/provider.go +++ b/providers/slack/provider/provider.go @@ -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) { @@ -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) diff --git a/providers/slack/resources/accesslogs.go b/providers/slack/resources/accesslogs.go index 91c5503cb9..a36e794f83 100644 --- a/providers/slack/resources/accesslogs.go +++ b/providers/slack/resources/accesslogs.go @@ -4,6 +4,7 @@ package resources import ( + "errors" "time" "github.com/slack-go/slack" @@ -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, diff --git a/providers/slack/resources/conversations.go b/providers/slack/resources/conversations.go index d224029dc7..6c6f871ace 100644 --- a/providers/slack/resources/conversations.go +++ b/providers/slack/resources/conversations.go @@ -4,6 +4,7 @@ package resources import ( + "errors" "time" "github.com/slack-go/slack" @@ -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{}{} @@ -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 diff --git a/providers/slack/resources/team.go b/providers/slack/resources/team.go index cb8832504a..4133d92632 100644 --- a/providers/slack/resources/team.go +++ b/providers/slack/resources/team.go @@ -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" @@ -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 diff --git a/providers/slack/resources/usergroups.go b/providers/slack/resources/usergroups.go index 223eeaeef9..12ba549bd3 100644 --- a/providers/slack/resources/usergroups.go +++ b/providers/slack/resources/usergroups.go @@ -5,6 +5,7 @@ package resources import ( "context" + "errors" "github.com/slack-go/slack" "go.mondoo.com/cnquery/llx" @@ -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() diff --git a/providers/slack/resources/users.go b/providers/slack/resources/users.go index 78a1c0ff34..04d601da1c 100644 --- a/providers/slack/resources/users.go +++ b/providers/slack/resources/users.go @@ -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 @@ -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 {