Skip to content

Commit f1fa007

Browse files
committed
feat: add new -workers flag to configure number of workers during scanning
1 parent cbec81d commit f1fa007

File tree

4 files changed

+39
-12
lines changed

4 files changed

+39
-12
lines changed

Readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Usage spark:
2525
verbose log output
2626
-version
2727
show version
28+
-workers int
29+
number of workers used for scanning (default 2)
2830

2931

3032
$ spark -list-scanners

app.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,26 @@ import (
99
"log/slog"
1010
"strings"
1111

12+
"github.com/aws/aws-sdk-go-v2/aws"
1213
"github.com/aws/aws-sdk-go-v2/config"
1314
"github.com/aws/aws-sdk-go-v2/service/sts"
1415
"golang.org/x/sync/errgroup"
1516
)
1617

1718
// App represents a struct that provides functionality for interacting with the AWS services.
1819
type App struct {
19-
accountID string
20-
runners []runner
21-
stsClient stsClient
20+
accountID string
21+
runners []runner
22+
stsClient stsClient
23+
workerLimit int
2224
}
2325

24-
func newApp(ctx context.Context, check []runnerType, regions []string) (*App, error) {
26+
func newApp(
27+
ctx context.Context,
28+
check []runnerType,
29+
regions []string,
30+
workerLimit int,
31+
) (*App, error) {
2532
if len(regions) == 0 {
2633
return nil, errEmptyRegion
2734
}
@@ -32,6 +39,10 @@ func newApp(ctx context.Context, check []runnerType, regions []string) (*App, er
3239

3340
regions = uniqRegions(regions)
3441

42+
if workerLimit < 1 {
43+
workerLimit = 1
44+
}
45+
3546
baseCfg, err := config.LoadDefaultConfig(ctx)
3647
if err != nil {
3748
return nil, fmt.Errorf("failed to load aws config, %w", err)
@@ -40,6 +51,18 @@ func newApp(ctx context.Context, check []runnerType, regions []string) (*App, er
4051
stsCfg := baseCfg.Copy()
4152
stsCfg.Region = regions[0]
4253

54+
runners := setUpRunners(baseCfg, check, regions)
55+
56+
return &App{
57+
accountID: "",
58+
runners: runners,
59+
stsClient: sts.NewFromConfig(stsCfg),
60+
workerLimit: workerLimit,
61+
}, nil
62+
}
63+
64+
// setUpRunners initializes and returns a list of runners based on the specified configuration, checks, and regions.
65+
func setUpRunners(baseCfg aws.Config, check []runnerType, regions []string) []runner {
4366
runners := make([]runner, 0)
4467

4568
for _, region := range regions {
@@ -64,18 +87,14 @@ func newApp(ctx context.Context, check []runnerType, regions []string) (*App, er
6487
}
6588
}
6689

67-
return &App{
68-
accountID: "",
69-
runners: runners,
70-
stsClient: sts.NewFromConfig(stsCfg),
71-
}, nil
90+
return runners
7291
}
7392

7493
// Run executes the scan process on the target using all available runners,
7594
// and returns the collected results or an error.
7695
func (a *App) Run(ctx context.Context, target string) ([]Result, error) {
7796
group, gCtx := errgroup.WithContext(ctx)
78-
group.SetLimit(1)
97+
group.SetLimit(a.workerLimit)
7998

8099
if strings.EqualFold(target, "self") {
81100
slog.Debug("replacing self with account ID",

app_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func Test_newApp(t *testing.T) {
1818
name string
1919
check []runnerType
2020
regions []string
21+
workerLimit int
2122
wantApp bool
2223
wantErr bool
2324
wantWorkers []runnerType
@@ -83,7 +84,7 @@ func Test_newApp(t *testing.T) {
8384
t.Run(tt.name, func(t *testing.T) {
8485
t.Parallel()
8586

86-
got, err := newApp(t.Context(), tt.check, tt.regions)
87+
got, err := newApp(t.Context(), tt.check, tt.regions, tt.workerLimit)
8788
if (err != nil) != tt.wantErr {
8889
t.Errorf("newApp() error = %v, wantErr %v", err, tt.wantErr)
8990

@@ -164,7 +165,8 @@ func TestApp_Run(t *testing.T) {
164165
t.Parallel()
165166

166167
a := &App{
167-
runners: tt.runners,
168+
runners: tt.runners,
169+
workerLimit: 1,
168170
}
169171

170172
got, err := a.Run(tt.ctx, tt.target)

main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,16 @@ import (
1616
var version = "dev"
1717

1818
func main() { //nolint:cyclop
19+
const numberOfWorkers = 2
20+
1921
var (
2022
target = flag.String("target", "self", "target AWS account ID")
2123
listScanners = flag.Bool("list-scanners", false, "list available resource types")
2224
showVersion = flag.Bool("version", false, "show version")
2325
verbose = flag.Bool("verbose", false, "verbose log output")
2426
scanAllRegions = flag.Bool("region-all", false, "scan all regions")
2527
scannersAll = flag.Bool("scan-all", false, "scan all resource types")
28+
workerCount = flag.Int("workers", numberOfWorkers, "number of workers used for scanning")
2629
regionVars StringSlice
2730
scannersVars StringSlice
2831
)
@@ -92,6 +95,7 @@ func main() { //nolint:cyclop
9295
ctx,
9396
getRunners(scannersVars),
9497
regionVars,
98+
*workerCount,
9599
)
96100
if err != nil {
97101
slog.Error("failed to initialize app", slog.String("error", err.Error()))

0 commit comments

Comments
 (0)