Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ implement MockConnect for slack provider #1933

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, 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, nil, errors.New("cannot retrieve new data while using a mock connection")
}

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