|
| 1 | +// Copyright 2025 variHQ OÜ |
| 2 | +// SPDX-License-Identifier: BSD-3-Clause |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "log/slog" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/aws/aws-sdk-go-v2/config" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 14 | + "golang.org/x/sync/errgroup" |
| 15 | +) |
| 16 | + |
| 17 | +// App represents a struct that provides functionality for interacting with the AWS services. |
| 18 | +type App struct { |
| 19 | + accountID string |
| 20 | + runners []runner |
| 21 | + stsClient stsClient |
| 22 | +} |
| 23 | + |
| 24 | +func newApp(ctx context.Context, check []runnerType, regions []string) (*App, error) { |
| 25 | + if len(regions) == 0 { |
| 26 | + return nil, errEmptyRegion |
| 27 | + } |
| 28 | + |
| 29 | + if len(check) == 0 { |
| 30 | + return nil, errEmptyCheck |
| 31 | + } |
| 32 | + |
| 33 | + regions = uniqRegions(regions) |
| 34 | + |
| 35 | + baseCfg, err := config.LoadDefaultConfig(ctx) |
| 36 | + if err != nil { |
| 37 | + return nil, fmt.Errorf("failed to load aws config, %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + stsCfg := baseCfg.Copy() |
| 41 | + stsCfg.Region = regions[0] |
| 42 | + |
| 43 | + runners := make([]runner, 0) |
| 44 | + |
| 45 | + for _, region := range regions { |
| 46 | + cfg := baseCfg.Copy() |
| 47 | + cfg.Region = region |
| 48 | + |
| 49 | + for _, scan := range check { |
| 50 | + switch scan { |
| 51 | + case amiImage: |
| 52 | + runners = append(runners, newAMIImageScan(cfg)) |
| 53 | + case ebsSnapshot: |
| 54 | + runners = append(runners, newEBSSnapshotRunner(cfg)) |
| 55 | + case rdsSnapshot: |
| 56 | + runners = append(runners, newRDSSnapshotRunner(cfg)) |
| 57 | + case ssmDocument: |
| 58 | + runners = append(runners, newSSMDocumentScan(cfg)) |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return &App{ |
| 64 | + accountID: "", |
| 65 | + runners: runners, |
| 66 | + stsClient: sts.NewFromConfig(stsCfg), |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +// Run executes the scan process on the target using all available runners, |
| 71 | +// and returns the collected results or an error. |
| 72 | +func (a *App) Run(ctx context.Context, target string) ([]Result, error) { |
| 73 | + group, gCtx := errgroup.WithContext(ctx) |
| 74 | + group.SetLimit(1) |
| 75 | + |
| 76 | + if strings.EqualFold(target, "self") { |
| 77 | + slog.Debug("replacing self with account ID", |
| 78 | + slog.String("accountID", a.accountID), |
| 79 | + ) |
| 80 | + |
| 81 | + target = a.accountID |
| 82 | + } |
| 83 | + |
| 84 | + buffer := make(chan []Result, len(a.runners)) |
| 85 | + |
| 86 | + for _, scanRunner := range a.runners { |
| 87 | + group.Go(func() error { |
| 88 | + select { |
| 89 | + case <-gCtx.Done(): |
| 90 | + return gCtx.Err() |
| 91 | + default: |
| 92 | + scanResults, err := scanRunner.scan(ctx, target) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + buffer <- scanResults |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + if err := group.Wait(); err != nil { |
| 105 | + return nil, fmt.Errorf("failed to run all checks, %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + close(buffer) |
| 109 | + |
| 110 | + var output []Result |
| 111 | + for item := range buffer { |
| 112 | + output = append(output, item...) |
| 113 | + } |
| 114 | + |
| 115 | + return output, nil |
| 116 | +} |
| 117 | + |
| 118 | +func (a *App) getAccountID(ctx context.Context) error { |
| 119 | + output, err := a.stsClient.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) |
| 120 | + if err != nil { |
| 121 | + return fmt.Errorf("failed to get caller identity, %w", err) |
| 122 | + } |
| 123 | + |
| 124 | + if *output.Account == "" { |
| 125 | + return errEmptyAccountID |
| 126 | + } |
| 127 | + |
| 128 | + a.accountID = *output.Account |
| 129 | + |
| 130 | + return nil |
| 131 | +} |
0 commit comments