-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
142 lines (121 loc) · 3.63 KB
/
utils.go
File metadata and controls
142 lines (121 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2025 variHQ OÜ
// SPDX-License-Identifier: BSD-3-Clause
package spark
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"sort"
"strings"
"time"
)
var (
// ErrEmptyAccountID is returned when the AWS caller identity has no account ID.
ErrEmptyAccountID = errors.New("failed to get caller identity, empty account ID")
// ErrCtxCancelled indicates the scan was canceled via context.
ErrCtxCancelled = errors.New("scan was cancelled")
// ErrEmptyCheck is returned when no resource types are specified for scanning.
ErrEmptyCheck = errors.New(
"no resource types specified; use -list-scanners, -scan <type>, or -scan-all",
)
// ErrEmptyRegion is returned when no AWS regions are specified.
ErrEmptyRegion = errors.New("no AWS regions specified; use -region <name> or -region-all")
// ErrEmptyTarget indicates a missing target AWS account ID.
ErrEmptyTarget = errors.New("empty target AWS account ID")
)
// GetLogger returns a slog.Logger configured with the given output and log level.
// If verbose is true, the log level is set to debug; otherwise, it defaults to info.
func GetLogger(output io.Writer, verbose *bool) *slog.Logger {
logLevel := slog.LevelInfo
if verbose != nil && *verbose {
logLevel = slog.LevelDebug
}
logger := slog.New(
slog.NewTextHandler(output, &slog.HandlerOptions{
AddSource: false,
Level: logLevel,
ReplaceAttr: nil,
}),
)
return logger
}
// PrepareOutput returns a pretty-printed JSON byte slice from a list of Result objects.
func PrepareOutput(output []Result) ([]byte, error) {
marshal, err := json.MarshalIndent(struct {
Results []Result `json:"results"`
}{
Results: output,
}, "", " ")
if err != nil {
return nil, fmt.Errorf("failed to marshal output, %w", err)
}
return marshal, nil
}
// GetSupportedScanners returns supported AWS scanner names.
func GetSupportedScanners() []string {
return []string{
ImageAMI.String(),
SnapshotEBS.String(),
DocumentSSM.String(),
SnapshotRDS.String(),
}
}
// GetRunners maps input strings to unique RunnerType values, ignoring case and invalid entries.
func GetRunners(input []string) []RunnerType {
uniq := make(map[RunnerType]struct{})
for _, scan := range input {
switch {
case strings.EqualFold(scan, ImageAMI.String()):
uniq[ImageAMI] = struct{}{}
case strings.EqualFold(scan, SnapshotEBS.String()):
uniq[SnapshotEBS] = struct{}{}
case strings.EqualFold(scan, DocumentSSM.String()):
uniq[DocumentSSM] = struct{}{}
case strings.EqualFold(scan, SnapshotRDS.String()):
uniq[SnapshotRDS] = struct{}{}
default:
slog.Debug("invalid scan type", slog.String("type", scan))
}
}
output := make([]RunnerType, 0, len(uniq))
for scan := range uniq {
output = append(output, scan)
}
sort.Slice(output, func(left, right int) bool {
return output[left].String() < output[right].String()
})
return output
}
func uniqRegions(input []string) []string {
uniq := make(map[string]struct{})
for _, region := range input {
uniq[region] = struct{}{}
}
output := make([]string, 0, len(uniq))
for region := range uniq {
output = append(output, region)
}
sort.Strings(output)
return output
}
// Spinner writes a rotating animation to the writer, updating on each tick until the context is canceled.
func Spinner(ctx context.Context, writer io.Writer, ticker <-chan time.Time) {
spinChars := []rune{
'|',
'/',
'-',
'\\',
}
for position := 0; ; position = (position + 1) % len(spinChars) {
select {
case <-ctx.Done():
_, _ = fmt.Fprintln(writer, "\r\033[K")
return
case <-ticker:
_, _ = fmt.Fprintf(writer, "\r\033[K%s %c", "scanning...", spinChars[position])
}
}
}