Skip to content

Commit

Permalink
🧹 introduce a new --team-id argument for slack provider (#3853)
Browse files Browse the repository at this point in the history
This change is required to support org tokens which supports accessing multiple teams.
  • Loading branch information
chris-rock authored Apr 25, 2024
1 parent fbebdcc commit 2012988
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
6 changes: 6 additions & 0 deletions providers/slack/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ var Config = plugin.Provider{
Default: "",
Desc: "Slack API token",
},
{
Long: "team-id",
Type: plugin.FlagType_String,
Default: "",
Desc: "Team ID (required for org token)",
},
},
},
},
Expand Down
25 changes: 18 additions & 7 deletions providers/slack/connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package connection

import (
"context"
"errors"
"os"

Expand Down Expand Up @@ -61,11 +62,25 @@ func NewSlackConnection(id uint32, asset *inventory.Asset, conf *inventory.Confi
retryClient.RetryMax = 5
retryClient.Logger = &zeroLogAdapter{}
client := slack.New(token, slack.OptionHTTPClient(retryClient.StandardClient()))
teamInfo, err := client.GetTeamInfo()

teamID := conf.Options["team-id"]
ctx := context.Background()

var teamInfo *slack.TeamInfo
var err error
if teamID != "" {
teamInfo, err = client.GetOtherTeamInfoContext(ctx, teamID)
} else {
teamInfo, err = client.GetTeamInfoContext(ctx)
}
if err != nil {
return nil, err
}

if teamInfo == nil {
return nil, errors.New("could not retrieve team info")
}

sc.client = client
sc.teamInfo = teamInfo
sc.asset.Name = "Slack team " + teamInfo.Name
Expand All @@ -84,12 +99,8 @@ func (s *SlackConnection) Client() *slack.Client {
return s.client
}

func (p *SlackConnection) TeamID() string {
return p.teamInfo.ID
}

func (p *SlackConnection) TeamName() string {
return p.teamInfo.Name
func (p *SlackConnection) TeamInfo() *slack.TeamInfo {
return p.teamInfo
}

// zeroLogAdapter is the adapter for retryablehttp is outputting debug messages
Expand Down
17 changes: 14 additions & 3 deletions providers/slack/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,16 @@ func (s *Service) ParseCLI(req *plugin.ParseCLIReq) (*plugin.ParseCLIRes, error)
}

conf := &inventory.Config{
Type: req.Connector,
Type: req.Connector,
Options: map[string]string{},
}

teamID := ""
if x, ok := flags["team-id"]; ok && len(x.Value) != 0 {
teamID = string(x.Value)
}
if teamID != "" {
conf.Options["team-id"] = teamID
}

token := ""
Expand Down Expand Up @@ -162,14 +171,16 @@ func (s *Service) connect(req *plugin.ConnectReq, callback plugin.ProviderCallba
}

func (s *Service) detect(asset *inventory.Asset, conn *connection.SlackConnection) error {
asset.Name = "Slack team " + conn.TeamName()
teamInfo := conn.TeamInfo()

asset.Name = "Slack team " + teamInfo.Name
asset.Platform = &inventory.Platform{
Name: "slack-team",
Family: []string{"slack"},
Kind: "api",
Title: "Slack Team",
Runtime: "slack",
}
asset.PlatformIds = []string{"//platformid.api.mondoo.app/runtime/slack/team/" + conn.TeamID()}
asset.PlatformIds = []string{"//platformid.api.mondoo.app/runtime/slack/team/" + teamInfo.ID}
return nil
}
6 changes: 1 addition & 5 deletions providers/slack/resources/team.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ func initSlackTeam(runtime *plugin.Runtime, args map[string]*llx.RawData) (map[s
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
}

teamInfo := conn.TeamInfo()
args["id"] = llx.StringData(teamInfo.ID)
args["name"] = llx.StringData(teamInfo.Name)
args["domain"] = llx.StringData(teamInfo.Domain)
Expand Down
8 changes: 1 addition & 7 deletions providers/slack/resources/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,8 @@ func (s *mqlSlackUsers) list() ([]interface{}, error) {

ctx := context.Background()

// get team id
teamInfo, err := client.GetTeamInfo()
if err != nil {
return nil, err
}

// requires users:read scope
users, err := client.GetUsersContext(ctx, slack.GetUsersOptionTeamID(teamInfo.ID), slack.GetUsersOptionLimit(999))
users, err := client.GetUsersContext(ctx, slack.GetUsersOptionTeamID(conn.TeamInfo().ID), slack.GetUsersOptionLimit(999))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2012988

Please sign in to comment.