Skip to content

Commit 4f5a2f0

Browse files
authored
feat: add backend for jfrog xray support (#11829)
1 parent 46d92da commit 4f5a2f0

25 files changed

+944
-2
lines changed

coderd/apidoc/docs.go

Lines changed: 103 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbauthz/dbauthz.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1111,6 +1111,13 @@ func (q *querier) GetHungProvisionerJobs(ctx context.Context, hungSince time.Tim
11111111
return q.db.GetHungProvisionerJobs(ctx, hungSince)
11121112
}
11131113

1114+
func (q *querier) GetJFrogXrayScanByWorkspaceAndAgentID(ctx context.Context, arg database.GetJFrogXrayScanByWorkspaceAndAgentIDParams) (database.JfrogXrayScan, error) {
1115+
if _, err := fetch(q.log, q.auth, q.db.GetWorkspaceByID)(ctx, arg.WorkspaceID); err != nil {
1116+
return database.JfrogXrayScan{}, err
1117+
}
1118+
return q.db.GetJFrogXrayScanByWorkspaceAndAgentID(ctx, arg)
1119+
}
1120+
11141121
func (q *querier) GetLastUpdateCheck(ctx context.Context) (string, error) {
11151122
if err := q.authorizeContext(ctx, rbac.ActionRead, rbac.ResourceSystem); err != nil {
11161123
return "", err
@@ -3153,6 +3160,27 @@ func (q *querier) UpsertHealthSettings(ctx context.Context, value string) error
31533160
return q.db.UpsertHealthSettings(ctx, value)
31543161
}
31553162

3163+
func (q *querier) UpsertJFrogXrayScanByWorkspaceAndAgentID(ctx context.Context, arg database.UpsertJFrogXrayScanByWorkspaceAndAgentIDParams) error {
3164+
// TODO: Having to do all this extra querying makes me a sad panda.
3165+
workspace, err := q.db.GetWorkspaceByID(ctx, arg.WorkspaceID)
3166+
if err != nil {
3167+
return xerrors.Errorf("get workspace by id: %w", err)
3168+
}
3169+
3170+
template, err := q.db.GetTemplateByID(ctx, workspace.TemplateID)
3171+
if err != nil {
3172+
return xerrors.Errorf("get template by id: %w", err)
3173+
}
3174+
3175+
// Only template admins should be able to write JFrog Xray scans to a workspace.
3176+
// We don't want this to be a workspace-level permission because then users
3177+
// could overwrite their own results.
3178+
if err := q.authorizeContext(ctx, rbac.ActionCreate, template); err != nil {
3179+
return err
3180+
}
3181+
return q.db.UpsertJFrogXrayScanByWorkspaceAndAgentID(ctx, arg)
3182+
}
3183+
31563184
func (q *querier) UpsertLastUpdateCheck(ctx context.Context, value string) error {
31573185
if err := q.authorizeContext(ctx, rbac.ActionUpdate, rbac.ResourceSystem); err != nil {
31583186
return err

coderd/database/dbauthz/dbauthz_test.go

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ func (s *MethodTestSuite) TestGroup() {
364364
}))
365365
}
366366

367-
func (s *MethodTestSuite) TestProvsionerJob() {
367+
func (s *MethodTestSuite) TestProvisionerJob() {
368368
s.Run("ArchiveUnusedTemplateVersions", s.Subtest(func(db database.Store, check *expects) {
369369
j := dbgen.ProvisionerJob(s.T(), db, nil, database.ProvisionerJob{
370370
Type: database.ProvisionerJobTypeTemplateVersionImport,
@@ -2216,6 +2216,44 @@ func (s *MethodTestSuite) TestSystemFunctions() {
22162216
s.Run("GetUserLinksByUserID", s.Subtest(func(db database.Store, check *expects) {
22172217
check.Args(uuid.New()).Asserts(rbac.ResourceSystem, rbac.ActionRead)
22182218
}))
2219+
s.Run("GetJFrogXrayScanByWorkspaceAndAgentID", s.Subtest(func(db database.Store, check *expects) {
2220+
ws := dbgen.Workspace(s.T(), db, database.Workspace{})
2221+
agent := dbgen.WorkspaceAgent(s.T(), db, database.WorkspaceAgent{})
2222+
2223+
err := db.UpsertJFrogXrayScanByWorkspaceAndAgentID(context.Background(), database.UpsertJFrogXrayScanByWorkspaceAndAgentIDParams{
2224+
AgentID: agent.ID,
2225+
WorkspaceID: ws.ID,
2226+
Critical: 1,
2227+
High: 12,
2228+
Medium: 14,
2229+
ResultsUrl: "http://hello",
2230+
})
2231+
require.NoError(s.T(), err)
2232+
2233+
expect := database.JfrogXrayScan{
2234+
WorkspaceID: ws.ID,
2235+
AgentID: agent.ID,
2236+
Critical: 1,
2237+
High: 12,
2238+
Medium: 14,
2239+
ResultsUrl: "http://hello",
2240+
}
2241+
2242+
check.Args(database.GetJFrogXrayScanByWorkspaceAndAgentIDParams{
2243+
WorkspaceID: ws.ID,
2244+
AgentID: agent.ID,
2245+
}).Asserts(ws, rbac.ActionRead).Returns(expect)
2246+
}))
2247+
s.Run("UpsertJFrogXrayScanByWorkspaceAndAgentID", s.Subtest(func(db database.Store, check *expects) {
2248+
tpl := dbgen.Template(s.T(), db, database.Template{})
2249+
ws := dbgen.Workspace(s.T(), db, database.Workspace{
2250+
TemplateID: tpl.ID,
2251+
})
2252+
check.Args(database.UpsertJFrogXrayScanByWorkspaceAndAgentIDParams{
2253+
WorkspaceID: ws.ID,
2254+
AgentID: uuid.New(),
2255+
}).Asserts(tpl, rbac.ActionCreate)
2256+
}))
22192257
}
22202258

22212259
func (s *MethodTestSuite) TestOAuth2ProviderApps() {

coderd/database/dbmem/dbmem.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ type data struct {
129129
gitSSHKey []database.GitSSHKey
130130
groupMembers []database.GroupMember
131131
groups []database.Group
132+
jfrogXRayScans []database.JfrogXrayScan
132133
licenses []database.License
133134
oauth2ProviderApps []database.OAuth2ProviderApp
134135
oauth2ProviderAppSecrets []database.OAuth2ProviderAppSecret
@@ -1986,6 +1987,24 @@ func (q *FakeQuerier) GetHungProvisionerJobs(_ context.Context, hungSince time.T
19861987
return hungJobs, nil
19871988
}
19881989

1990+
func (q *FakeQuerier) GetJFrogXrayScanByWorkspaceAndAgentID(_ context.Context, arg database.GetJFrogXrayScanByWorkspaceAndAgentIDParams) (database.JfrogXrayScan, error) {
1991+
err := validateDatabaseType(arg)
1992+
if err != nil {
1993+
return database.JfrogXrayScan{}, err
1994+
}
1995+
1996+
q.mutex.RLock()
1997+
defer q.mutex.RUnlock()
1998+
1999+
for _, scan := range q.jfrogXRayScans {
2000+
if scan.AgentID == arg.AgentID && scan.WorkspaceID == arg.WorkspaceID {
2001+
return scan, nil
2002+
}
2003+
}
2004+
2005+
return database.JfrogXrayScan{}, sql.ErrNoRows
2006+
}
2007+
19892008
func (q *FakeQuerier) GetLastUpdateCheck(_ context.Context) (string, error) {
19902009
q.mutex.RLock()
19912010
defer q.mutex.RUnlock()
@@ -7292,6 +7311,39 @@ func (q *FakeQuerier) UpsertHealthSettings(_ context.Context, data string) error
72927311
return nil
72937312
}
72947313

7314+
func (q *FakeQuerier) UpsertJFrogXrayScanByWorkspaceAndAgentID(_ context.Context, arg database.UpsertJFrogXrayScanByWorkspaceAndAgentIDParams) error {
7315+
err := validateDatabaseType(arg)
7316+
if err != nil {
7317+
return err
7318+
}
7319+
7320+
q.mutex.Lock()
7321+
defer q.mutex.Unlock()
7322+
7323+
for i, scan := range q.jfrogXRayScans {
7324+
if scan.AgentID == arg.AgentID && scan.WorkspaceID == arg.WorkspaceID {
7325+
scan.Critical = arg.Critical
7326+
scan.High = arg.High
7327+
scan.Medium = arg.Medium
7328+
scan.ResultsUrl = arg.ResultsUrl
7329+
q.jfrogXRayScans[i] = scan
7330+
return nil
7331+
}
7332+
}
7333+
7334+
//nolint:gosimple
7335+
q.jfrogXRayScans = append(q.jfrogXRayScans, database.JfrogXrayScan{
7336+
WorkspaceID: arg.WorkspaceID,
7337+
AgentID: arg.AgentID,
7338+
Critical: arg.Critical,
7339+
High: arg.High,
7340+
Medium: arg.Medium,
7341+
ResultsUrl: arg.ResultsUrl,
7342+
})
7343+
7344+
return nil
7345+
}
7346+
72957347
func (q *FakeQuerier) UpsertLastUpdateCheck(_ context.Context, data string) error {
72967348
q.mutex.Lock()
72977349
defer q.mutex.Unlock()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy