Skip to content

Commit a00d197

Browse files
committed
simplify logic
1 parent ff496e8 commit a00d197

File tree

8 files changed

+29
-29
lines changed

8 files changed

+29
-29
lines changed

coderd/database/dbauthz/dbauthz.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4615,9 +4615,9 @@ func (q *querier) UpsertWorkspaceAgentPortShare(ctx context.Context, arg databas
46154615
return q.db.UpsertWorkspaceAgentPortShare(ctx, arg)
46164616
}
46174617

4618-
func (q *querier) UpsertWorkspaceAppAuditSession(ctx context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (uuid.UUID, error) {
4618+
func (q *querier) UpsertWorkspaceAppAuditSession(ctx context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
46194619
if err := q.authorizeContext(ctx, policy.ActionUpdate, rbac.ResourceSystem); err != nil {
4620-
return uuid.Nil, err
4620+
return false, err
46214621
}
46224622
return q.db.UpsertWorkspaceAppAuditSession(ctx, arg)
46234623
}

coderd/database/dbmem/dbmem.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12283,7 +12283,7 @@ func (q *FakeQuerier) UpsertWorkspaceAgentPortShare(_ context.Context, arg datab
1228312283
return psl, nil
1228412284
}
1228512285

12286-
func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (uuid.UUID, error) {
12286+
func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg database.UpsertWorkspaceAppAuditSessionParams) (bool, error) {
1228712287
err := validateDatabaseType(arg)
1228812288
if err != nil {
1228912289
return uuid.Nil, err
@@ -12322,9 +12322,9 @@ func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg data
1232212322
if !fresh {
1232312323
q.workspaceAppAuditSessions[i].ID = arg.ID
1232412324
q.workspaceAppAuditSessions[i].StartedAt = arg.StartedAt
12325-
return arg.ID, nil
12325+
return true, nil
1232612326
}
12327-
return s.ID, nil
12327+
return false, nil
1232812328
}
1232912329

1233012330
q.workspaceAppAuditSessions = append(q.workspaceAppAuditSessions, database.WorkspaceAppAuditSession{
@@ -12338,7 +12338,7 @@ func (q *FakeQuerier) UpsertWorkspaceAppAuditSession(_ context.Context, arg data
1233812338
StartedAt: arg.StartedAt,
1233912339
UpdatedAt: arg.UpdatedAt,
1234012340
})
12341-
return arg.ID, nil
12341+
return true, nil
1234212342
}
1234312343

1234412344
func (q *FakeQuerier) GetAuthorizedTemplates(ctx context.Context, arg database.GetTemplatesWithFilterParams, prepared rbac.PreparedAuthorized) ([]database.Template, error) {

coderd/database/dbmetrics/querymetrics.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/database/dbmock/dbmock.go

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

coderd/database/querier.go

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

coderd/database/queries.sql.go

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

coderd/database/queries/workspaceappaudit.sql

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
-- name: UpsertWorkspaceAppAuditSession :one
22
--
3-
-- Insert a new workspace app audit session or update an existing one, if
4-
-- started_at is updated, it means the session has been restarted.
3+
-- The returned boolean, new_or_stale, can be used to deduce if a new session
4+
-- was started. This means that a new row was inserted (no previous session) or
5+
-- the updated_at is older than stale interval.
56
INSERT INTO
67
workspace_app_audit_sessions (
78
id,
@@ -46,4 +47,4 @@ DO
4647
END,
4748
updated_at = EXCLUDED.updated_at
4849
RETURNING
49-
id;
50+
id = $1 AS new_or_stale;

coderd/workspaceapps/db.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -447,20 +447,17 @@ func (p *DBTokenProvider) auditInitRequest(ctx context.Context, w http.ResponseW
447447
slog.F("status_code", statusCode),
448448
)
449449

450-
var (
451-
refreshID = uuid.New()
452-
upsertRefreshID uuid.UUID
453-
)
450+
var newOrStale bool
454451
err := p.Database.InTx(func(tx database.Store) (err error) {
455452
// nolint:gocritic // System context is needed to write audit sessions.
456453
dangerousSystemCtx := dbauthz.AsSystemRestricted(ctx)
457454

458-
upsertRefreshID, err = tx.UpsertWorkspaceAppAuditSession(dangerousSystemCtx, database.UpsertWorkspaceAppAuditSessionParams{
455+
newOrStale, err = tx.UpsertWorkspaceAppAuditSession(dangerousSystemCtx, database.UpsertWorkspaceAppAuditSessionParams{
459456
// Config.
460457
StaleIntervalMS: p.WorkspaceAppAuditSessionTimeout.Milliseconds(),
461458

462459
// Data.
463-
ID: refreshID,
460+
ID: uuid.New(),
464461
AgentID: aReq.dbReq.Agent.ID,
465462
AppID: aReq.dbReq.App.ID, // Can be unset, in which case uuid.Nil is fine.
466463
UserID: userID, // Can be unset, in which case uuid.Nil is fine.
@@ -485,7 +482,7 @@ func (p *DBTokenProvider) auditInitRequest(ctx context.Context, w http.ResponseW
485482
return
486483
}
487484

488-
if sessionExistsOrIsActive := refreshID != upsertRefreshID; sessionExistsOrIsActive {
485+
if !newOrStale {
489486
// We either didn't insert a new session, or the session
490487
// didn't timeout due to inactivity.
491488
return

0 commit comments

Comments
 (0)