Skip to content

Commit c6d96f6

Browse files
committed
Add organization details to audit log queries
1 parent 6d2c427 commit c6d96f6

File tree

4 files changed

+88
-64
lines changed

4 files changed

+88
-64
lines changed

coderd/database/dbmem/dbmem.go

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,16 @@ func (q *FakeQuerier) getLatestWorkspaceAppByTemplateIDUserIDSlugNoLock(ctx cont
928928
return database.WorkspaceApp{}, sql.ErrNoRows
929929
}
930930

931+
// getOrganizationByIDNoLock is used by other functions in the database fake.
932+
func (q *FakeQuerier) getOrganizationByIDNoLock(id uuid.UUID) (database.Organization, error) {
933+
for _, organization := range q.organizations {
934+
if organization.ID == id {
935+
return organization, nil
936+
}
937+
}
938+
return database.Organization{}, sql.ErrNoRows
939+
}
940+
931941
func (*FakeQuerier) AcquireLock(_ context.Context, _ int64) error {
932942
return xerrors.New("AcquireLock must only be called within a transaction")
933943
}
@@ -2146,34 +2156,39 @@ func (q *FakeQuerier) GetAuditLogsOffset(_ context.Context, arg database.GetAudi
21462156
user, err := q.getUserByIDNoLock(alog.UserID)
21472157
userValid := err == nil
21482158

2159+
org, _ := q.getOrganizationByIDNoLock(alog.OrganizationID)
2160+
21492161
logs = append(logs, database.GetAuditLogsOffsetRow{
2150-
ID: alog.ID,
2151-
RequestID: alog.RequestID,
2152-
OrganizationID: alog.OrganizationID,
2153-
Ip: alog.Ip,
2154-
UserAgent: alog.UserAgent,
2155-
ResourceType: alog.ResourceType,
2156-
ResourceID: alog.ResourceID,
2157-
ResourceTarget: alog.ResourceTarget,
2158-
ResourceIcon: alog.ResourceIcon,
2159-
Action: alog.Action,
2160-
Diff: alog.Diff,
2161-
StatusCode: alog.StatusCode,
2162-
AdditionalFields: alog.AdditionalFields,
2163-
UserID: alog.UserID,
2164-
UserUsername: sql.NullString{String: user.Username, Valid: userValid},
2165-
UserName: sql.NullString{String: user.Name, Valid: userValid},
2166-
UserEmail: sql.NullString{String: user.Email, Valid: userValid},
2167-
UserCreatedAt: sql.NullTime{Time: user.CreatedAt, Valid: userValid},
2168-
UserUpdatedAt: sql.NullTime{Time: user.UpdatedAt, Valid: userValid},
2169-
UserLastSeenAt: sql.NullTime{Time: user.LastSeenAt, Valid: userValid},
2170-
UserLoginType: database.NullLoginType{LoginType: user.LoginType, Valid: userValid},
2171-
UserDeleted: sql.NullBool{Bool: user.Deleted, Valid: userValid},
2172-
UserThemePreference: sql.NullString{String: user.ThemePreference, Valid: userValid},
2173-
UserQuietHoursSchedule: sql.NullString{String: user.QuietHoursSchedule, Valid: userValid},
2174-
UserStatus: database.NullUserStatus{UserStatus: user.Status, Valid: userValid},
2175-
UserRoles: user.RBACRoles,
2176-
Count: 0,
2162+
ID: alog.ID,
2163+
RequestID: alog.RequestID,
2164+
OrganizationID: alog.OrganizationID,
2165+
OrganizationName: org.Name,
2166+
OrganizationDisplayName: org.DisplayName,
2167+
OrganizationIcon: org.Icon,
2168+
Ip: alog.Ip,
2169+
UserAgent: alog.UserAgent,
2170+
ResourceType: alog.ResourceType,
2171+
ResourceID: alog.ResourceID,
2172+
ResourceTarget: alog.ResourceTarget,
2173+
ResourceIcon: alog.ResourceIcon,
2174+
Action: alog.Action,
2175+
Diff: alog.Diff,
2176+
StatusCode: alog.StatusCode,
2177+
AdditionalFields: alog.AdditionalFields,
2178+
UserID: alog.UserID,
2179+
UserUsername: sql.NullString{String: user.Username, Valid: userValid},
2180+
UserName: sql.NullString{String: user.Name, Valid: userValid},
2181+
UserEmail: sql.NullString{String: user.Email, Valid: userValid},
2182+
UserCreatedAt: sql.NullTime{Time: user.CreatedAt, Valid: userValid},
2183+
UserUpdatedAt: sql.NullTime{Time: user.UpdatedAt, Valid: userValid},
2184+
UserLastSeenAt: sql.NullTime{Time: user.LastSeenAt, Valid: userValid},
2185+
UserLoginType: database.NullLoginType{LoginType: user.LoginType, Valid: userValid},
2186+
UserDeleted: sql.NullBool{Bool: user.Deleted, Valid: userValid},
2187+
UserThemePreference: sql.NullString{String: user.ThemePreference, Valid: userValid},
2188+
UserQuietHoursSchedule: sql.NullString{String: user.QuietHoursSchedule, Valid: userValid},
2189+
UserStatus: database.NullUserStatus{UserStatus: user.Status, Valid: userValid},
2190+
UserRoles: user.RBACRoles,
2191+
Count: 0,
21772192
})
21782193

21792194
if len(logs) >= int(arg.LimitOpt) {
@@ -2969,12 +2984,7 @@ func (q *FakeQuerier) GetOrganizationByID(_ context.Context, id uuid.UUID) (data
29692984
q.mutex.RLock()
29702985
defer q.mutex.RUnlock()
29712986

2972-
for _, organization := range q.organizations {
2973-
if organization.ID == id {
2974-
return organization, nil
2975-
}
2976-
}
2977-
return database.Organization{}, sql.ErrNoRows
2987+
return q.getOrganizationByIDNoLock(id)
29782988
}
29792989

29802990
func (q *FakeQuerier) GetOrganizationByName(_ context.Context, name string) (database.Organization, error) {

coderd/database/queries.sql.go

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

coderd/database/queries/auditlogs.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ SELECT
1818
users.deleted AS user_deleted,
1919
users.theme_preference AS user_theme_preference,
2020
users.quiet_hours_schedule AS user_quiet_hours_schedule,
21+
COALESCE(organizations.name, '') AS organization_name,
22+
COALESCE(organizations.display_name, '') AS organization_display_name,
23+
COALESCE(organizations.icon, '') AS organization_icon,
2124
COUNT(audit_logs.*) OVER () AS count
2225
FROM
2326
audit_logs
@@ -46,6 +49,7 @@ FROM
4649
workspaces.id = workspace_builds.workspace_id AND
4750
workspace_builds.build_number = 1
4851
)
52+
LEFT JOIN organizations ON audit_logs.organization_id = organizations.id
4953
WHERE
5054
-- Filter resource_type
5155
CASE

enterprise/cli/provisionerdaemons.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
114114
return xerrors.New("must provide a pre-shared key when not authenticated as a user")
115115
}
116116

117-
org = codersdk.Organization{ID: uuid.Nil}
117+
org = codersdk.Organization{MinimalOrganization: codersdk.MinimalOrganization{ID: uuid.Nil}}
118118
if orgContext.FlagSelect != "" {
119119
// If we are using PSK, we can't fetch the organization
120120
// to validate org name so we need the user to provide
@@ -123,7 +123,7 @@ func (r *RootCmd) provisionerDaemonStart() *serpent.Command {
123123
if err != nil {
124124
return xerrors.New("must provide an org ID when not authenticated as a user and organization is specified")
125125
}
126-
org = codersdk.Organization{ID: orgID}
126+
org = codersdk.Organization{MinimalOrganization: codersdk.MinimalOrganization{ID: orgID}}
127127
}
128128
}
129129

0 commit comments

Comments
 (0)