-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
219 lines (190 loc) · 6.63 KB
/
main.go
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package main
import (
"context"
"flag"
"fmt"
"net"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"time"
"github.com/DependencyTrack/client-go"
"github.com/peterbourgon/ff/v3"
"github.com/peterbourgon/ff/v3/ffcli"
"github.com/peterbourgon/ff/v3/ffyaml"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"golang.org/x/sync/errgroup"
"github.com/nscuro/dtapac/internal/analysis"
"github.com/nscuro/dtapac/internal/api"
"github.com/nscuro/dtapac/internal/apply"
"github.com/nscuro/dtapac/internal/audit"
"github.com/nscuro/dtapac/internal/opa"
)
func main() {
fs := flag.NewFlagSet("dtapac", flag.ContinueOnError)
fs.String("config", "", "Path to config file")
var opts options
fs.StringVar(&opts.host, "host", "0.0.0.0", "Host to listen on")
fs.UintVar(&opts.port, "port", 8080, "Port to listen on")
fs.StringVar(&opts.dtURL, "dtrack-url", "", "Dependency-Track API server URL")
fs.StringVar(&opts.dtAPIKey, "dtrack-apikey", "", "Dependency-Track API key")
fs.DurationVar(&opts.dtTimeout, "dtrack-timeout", dtrack.DefaultTimeout, "Dependency-Track request timeout")
fs.StringVar(&opts.opaURL, "opa-url", "", "Open Policy Agent URL")
fs.DurationVar(&opts.opaTimeout, "opa-timeout", 5*time.Second, "OPA request timeout")
fs.StringVar(&opts.watchBundle, "watch-bundle", "", "OPA bundle to watch")
fs.StringVar(&opts.findingPolicyPath, "finding-policy-path", "", "Policy path for finding analysis")
fs.StringVar(&opts.violationPolicyPath, "violation-policy-path", "", "Policy path for violation analysis")
fs.BoolVar(&opts.dryRun, "dry-run", false, "Only log analyses but don't apply them")
fs.StringVar(&opts.logLevel, "log-level", zerolog.LevelInfoValue, "Log level")
fs.BoolVar(&opts.logJSON, "log-json", false, "Output log in JSON format")
cmd := ffcli.Command{
Name: "dtapac",
ShortUsage: "dtapac [FLAGS...]",
LongHelp: `Audit Dependency-Track findings and policy violations via policy as code.`,
Options: []ff.Option{
ff.WithEnvVarNoPrefix(),
ff.WithConfigFileFlag("config"),
ff.WithConfigFileParser(ffyaml.Parser),
ff.WithAllowMissingConfigFile(true),
},
FlagSet: fs,
Exec: func(ctx context.Context, _ []string) error {
return exec(ctx, opts)
},
}
err := cmd.ParseAndRun(context.Background(), os.Args[1:])
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
type options struct {
host string
port uint
dtURL string
dtAPIKey string
dtTimeout time.Duration
opaURL string
opaTimeout time.Duration
watchBundle string
findingPolicyPath string
violationPolicyPath string
dryRun bool
logLevel string
logJSON bool
}
func exec(ctx context.Context, opts options) error {
logger := log.Logger
if !opts.logJSON {
logger = log.Output(zerolog.ConsoleWriter{
Out: os.Stderr,
})
}
if lvl, err := zerolog.ParseLevel(opts.logLevel); err == nil {
logger = logger.Level(lvl)
} else {
logger.Error().Err(err).Msg("failed to parse log level")
}
dtClient, err := dtrack.NewClient(opts.dtURL, dtrack.WithAPIKey(opts.dtAPIKey), dtrack.WithTimeout(opts.dtTimeout))
if err != nil {
return fmt.Errorf("failed to setup dtrack client: %w", err)
}
opaClient, err := opa.NewClient(opts.opaURL, opts.opaTimeout)
if err != nil {
return fmt.Errorf("failed to setup opa client: %w", err)
}
auditor, err := audit.NewOPAAuditor(opaClient, opts.findingPolicyPath, opts.violationPolicyPath, serviceLogger("auditor", logger))
if err != nil {
return fmt.Errorf("failed to setup auditor: %w", err)
}
apiServerAddr := net.JoinHostPort(opts.host, strconv.FormatUint(uint64(opts.port), 10))
apiServer, err := api.NewServer(apiServerAddr, dtClient, auditor, serviceLogger("apiServer", logger))
if err != nil {
return fmt.Errorf("failed to setup api server: %w", err)
}
bundleWatcher, err := opa.NewBundleWatcher(opts.watchBundle, apiServer.OPAStatusChan(), serviceLogger("bundleWatcher", logger))
if err != nil {
return fmt.Errorf("failed to setup bundle watcher: %w", err)
}
portfolioAnalyzer, err := analysis.NewPortfolioAnalyzer(dtClient, auditor, serviceLogger("portfolioAnalyzer", logger))
if err != nil {
return fmt.Errorf("failed to setup portfolio analyzer: %w", err)
}
applier := apply.NewApplier(dtClient.Analysis, dtClient.ViolationAnalysis, serviceLogger("applier", logger))
applier.SetDryRun(opts.dryRun)
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(apiServer.Start)
eg.Go(func() error {
return bundleWatcher.Start(egCtx)
})
// Trigger a portfolio analysis every time the watched bundle has been updated.
portfolioAnalysisTriggerChan := make(chan struct{}, 1)
eg.Go(func() error {
defer close(portfolioAnalysisTriggerChan)
for {
select {
case _, open := <-bundleWatcher.UpdateChan():
if !open {
return nil
}
portfolioAnalysisTriggerChan <- struct{}{}
case <-egCtx.Done():
return egCtx.Err()
}
}
})
eg.Go(func() error {
return portfolioAnalyzer.Start(egCtx, portfolioAnalysisTriggerChan)
})
// Merge all channels that emit audit results into one, so the applier can consume them.
auditResultChan := merge(apiServer.AuditResultChan(), portfolioAnalyzer.AuditResultChan())
eg.Go(func() error {
return applier.Start(egCtx, auditResultChan)
})
termChan := make(chan os.Signal, 1)
signal.Notify(termChan, os.Interrupt, syscall.SIGTERM)
select {
case <-termChan:
logger.Debug().Str("reason", "shutdown requested").Msg("shutting down")
case <-egCtx.Done():
logger.Debug().AnErr("reason", egCtx.Err()).Msg("shutting down")
}
// Stopping the API server will close all its channels, draining all channels
// further down the processing pipeline and ultimately closing them too.
err = apiServer.Stop()
if err != nil {
logger.Error().Err(err).Msg("failed to shutdown api server")
}
return eg.Wait()
}
func serviceLogger(name string, parent zerolog.Logger) zerolog.Logger {
return parent.With().Str("svc", name).Logger()
}
// merge converts a list of channels to a single channel, implementing a fan-in operation.
// This code snippet was taken from https://go.dev/blog/pipelines
func merge(cs ...<-chan any) <-chan any {
var wg sync.WaitGroup
out := make(chan any, 1)
// Start an output goroutine for each input channel in cs. output
// copies values from c to out until c is closed, then calls wg.Done.
output := func(c <-chan any) {
for n := range c {
out <- n
}
wg.Done()
}
wg.Add(len(cs))
for _, c := range cs {
go output(c)
}
// Start a goroutine to close out once all the output goroutines are
// done. This must start after the wg.Add call.
go func() {
wg.Wait()
close(out)
}()
return out
}