-
Notifications
You must be signed in to change notification settings - Fork 45
/
data_session.go
253 lines (214 loc) · 6.81 KB
/
data_session.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package advisory
import (
"context"
"fmt"
"os"
"slices"
"strings"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/google/go-github/v58/github"
"github.com/google/uuid"
"github.com/wolfi-dev/wolfictl/pkg/configs"
v2 "github.com/wolfi-dev/wolfictl/pkg/configs/advisory/v2"
rwos "github.com/wolfi-dev/wolfictl/pkg/configs/rwfs/os"
"github.com/wolfi-dev/wolfictl/pkg/distro"
wgit "github.com/wolfi-dev/wolfictl/pkg/git"
)
type DataSession struct {
tempDir string
repo *git.Repository
workingBranch string
distro distro.Distro
index *configs.Index[v2.Document]
githubClient *github.Client
modified bool
modifiedPackages []string
}
type DataSessionOptions struct {
Distro distro.Distro
GitHubClient *github.Client
}
// NewDataSession initializes a new advisory data session for the specified
// distro and returns a reference to the session. This call will retrieve the
// data and manage it in a local temp directory until the session is closed. The
// session should be closed by calling Close() when it is no longer needed.
func NewDataSession(ctx context.Context, opts DataSessionOptions) (*DataSession, error) {
// create temp directory
tempDir, err := os.MkdirTemp("", "wolfictl-advisory-data-session-*")
if err != nil {
return nil, fmt.Errorf("creating temp dir: %w", err)
}
ds := &DataSession{
tempDir: tempDir,
distro: opts.Distro,
}
ds.githubClient = opts.GitHubClient
gitURL := opts.Distro.Absolute.AdvisoriesHTTPSCloneURL()
gitAuth, err := wgit.GetGitAuth(gitURL)
if err != nil {
return nil, fmt.Errorf("getting git auth: %w", err)
}
// clone advisories repo
repo, err := git.PlainCloneContext(ctx, tempDir, false, &git.CloneOptions{
URL: gitURL,
Auth: gitAuth,
})
if err != nil {
return nil, fmt.Errorf("cloning advisories repo: %w", err)
}
ds.repo = repo
// checkout a new branch
u := uuid.New()
branchName := fmt.Sprintf("wolfictl-data-session-%s", u)
ds.workingBranch = branchName
wt, err := repo.Worktree()
if err != nil {
return nil, fmt.Errorf("getting worktree: %w", err)
}
err = wt.Checkout(&git.CheckoutOptions{
Branch: plumbing.NewBranchReferenceName(branchName),
Create: true,
})
if err != nil {
return nil, fmt.Errorf("checking out new branch: %w", err)
}
// index advisory documents
index, err := v2.NewIndex(ctx, rwos.DirFS(tempDir))
if err != nil {
return nil, fmt.Errorf("indexing advisory documents: %w", err)
}
ds.index = index
return ds, nil
}
// Close closes the advisory data session and cleans up any temporary data that
// was downloaded.
func (ds DataSession) Close() error {
return os.RemoveAll(ds.tempDir)
}
// Create creates a new advisory within the context of the data session.
func (ds *DataSession) Create(ctx context.Context, req Request) error {
err := Create(ctx, req, CreateOptions{
AdvisoryDocs: ds.index,
})
if err != nil {
return fmt.Errorf("creating advisory: %w", err)
}
err = ds.commit(ctx, req, "create")
if err != nil {
return fmt.Errorf("committing advisory creation: %w", err)
}
ds.modified = true
ds.modifiedPackages = append(ds.modifiedPackages, req.Package)
return nil
}
// Update updates an existing advisory within the context of the data session.
func (ds *DataSession) Update(ctx context.Context, req Request) error {
err := Update(ctx, req, UpdateOptions{
AdvisoryDocs: ds.index,
})
if err != nil {
return fmt.Errorf("updating advisory: %w", err)
}
err = ds.commit(ctx, req, "update")
if err != nil {
return fmt.Errorf("committing advisory update: %w", err)
}
ds.modified = true
ds.modifiedPackages = append(ds.modifiedPackages, req.Package)
return nil
}
// Append creates a new event for an advisory if the advisory already exists, or
// creates a new advisory with the event if the advisory does not already exist.
func (ds *DataSession) Append(ctx context.Context, req Request) error {
packageSelection := ds.index.Select().WhereName(req.Package)
if packageSelection.Len() == 0 {
return ds.Create(ctx, req)
}
if _, exists := packageSelection.Configurations()[0].Advisories.GetByVulnerability(req.VulnerabilityID); !exists {
return ds.Create(ctx, req)
}
return ds.Update(ctx, req)
}
// Dir returns the path to the temporary directory where the session's advisory
// data is currently stored.
func (ds DataSession) Dir() string {
return ds.tempDir
}
// Index returns the index of advisory documents for the session.
func (ds DataSession) Index() *configs.Index[v2.Document] {
return ds.index
}
// Modified returns true if any changes have been made to the advisory data
// during the session.
func (ds DataSession) Modified() bool {
return ds.modified
}
// Push pushes the changes made during the session to the remote advisories
// repository.
func (ds DataSession) Push(ctx context.Context) error {
gitURL := ds.distro.Absolute.AdvisoriesHTTPSCloneURL()
gitAuth, err := wgit.GetGitAuth(gitURL)
if err != nil {
return fmt.Errorf("getting git auth: %w", err)
}
err = ds.repo.PushContext(ctx, &git.PushOptions{
RemoteURL: gitURL,
Auth: gitAuth,
})
if err != nil {
return fmt.Errorf("pushing changes: %w", err)
}
return nil
}
// OpenPullRequest opens a pull request for the changes made during the session.
func (ds DataSession) OpenPullRequest(ctx context.Context) (*PullRequest, error) {
slices.Sort(ds.modifiedPackages)
compact := slices.Compact(ds.modifiedPackages)
newPullRequest := github.NewPullRequest{
Title: github.String(fmt.Sprintf("Add advisory data for %s", strings.Join(compact, ", "))),
Body: github.String(pullRequestBody),
Head: github.String(ds.workingBranch),
Base: github.String("main"),
MaintainerCanModify: github.Bool(true),
}
pullRequest, _, err := ds.githubClient.PullRequests.Create(
ctx,
ds.distro.Absolute.DistroRepoOwner,
ds.distro.Absolute.DistroAdvisoriesRepo,
&newPullRequest,
)
if err != nil {
return nil, fmt.Errorf("creating pull request on GitHub: %w", err)
}
pr := PullRequest{
URL: pullRequest.GetHTMLURL(),
}
return &pr, nil
}
const pullRequestBody = "This PR was created using the `wolfictl adv guide` command."
type PullRequest struct {
URL string
}
// commit creates a commit in the advisory repo for the specified operation.
func (ds DataSession) commit(_ context.Context, req Request, operation string) error {
commitMessage := fmt.Sprintf(
"%s: %s advisory %s",
req.Package,
operation,
req.VulnerabilityID,
)
wt, err := ds.repo.Worktree()
if err != nil {
return fmt.Errorf("getting worktree: %w", err)
}
err = wt.AddGlob(fmt.Sprintf("%s.advisories.yaml", req.Package))
if err != nil {
return fmt.Errorf("staging changes: %w", err)
}
_, err = wt.Commit(commitMessage, &git.CommitOptions{})
if err != nil {
return fmt.Errorf("creating commit: %w", err)
}
return nil
}