Skip to content

Commit a9aa60d

Browse files
committed
fix: change audit descriptions to indicate unsuccessful attempts
1 parent a5e4bf3 commit a9aa60d

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

coderd/audit.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net"
99
"net/http"
1010
"net/netip"
11+
"strings"
1112
"time"
1213

1314
"github.com/google/uuid"
@@ -263,35 +264,40 @@ func (api *API) convertAuditLog(ctx context.Context, dblog database.GetAuditLogs
263264
}
264265

265266
func auditLogDescription(alog database.GetAuditLogsOffsetRow) string {
266-
str := fmt.Sprintf("{user} %s",
267-
codersdk.AuditAction(alog.Action).Friendly(),
268-
)
267+
b := strings.Builder{}
268+
_, _ = b.WriteString("{user} ")
269+
if alog.StatusCode >= 400 {
270+
_, _ = b.WriteString("unsuccessfully attempted to ")
271+
_, _ = b.WriteString(string(alog.Action))
272+
} else {
273+
_, _ = b.WriteString(codersdk.AuditAction(alog.Action).Friendly())
274+
}
269275

270276
// API Key resources (used for authentication) do not have targets and follow the below format:
271277
// "User {logged in | logged out | registered}"
272278
if alog.ResourceType == database.ResourceTypeApiKey &&
273279
(alog.Action == database.AuditActionLogin || alog.Action == database.AuditActionLogout || alog.Action == database.AuditActionRegister) {
274-
return str
280+
return b.String()
275281
}
276282

277283
// We don't display the name (target) for git ssh keys. It's fairly long and doesn't
278284
// make too much sense to display.
279285
if alog.ResourceType == database.ResourceTypeGitSshKey {
280-
str += fmt.Sprintf(" the %s",
281-
codersdk.ResourceType(alog.ResourceType).FriendlyString())
282-
return str
286+
_, _ = b.WriteString(" the ")
287+
_, _ = b.WriteString(codersdk.ResourceType(alog.ResourceType).FriendlyString())
288+
return b.String()
283289
}
284290

285-
str += fmt.Sprintf(" %s",
286-
codersdk.ResourceType(alog.ResourceType).FriendlyString())
291+
_, _ = b.WriteString(" ")
292+
_, _ = b.WriteString(codersdk.ResourceType(alog.ResourceType).FriendlyString())
287293

288294
if alog.ResourceType == database.ResourceTypeConvertLogin {
289-
str += " to"
295+
_, _ = b.WriteString(" to")
290296
}
291297

292-
str += " {target}"
298+
_, _ = b.WriteString(" {target}")
293299

294-
return str
300+
return b.String()
295301
}
296302

297303
func (api *API) auditLogIsResourceDeleted(ctx context.Context, alog database.GetAuditLogsOffsetRow) bool {

coderd/audit_internal_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package coderd
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
8+
"github.com/coder/coder/v2/coderd/database"
9+
)
10+
11+
func TestAuditLogDescription(t *testing.T) {
12+
t.Parallel()
13+
// nolint: paralleltest // no longer need to reinitialize loop vars in go 1.22
14+
for _, tc := range []struct {
15+
name string
16+
alog database.GetAuditLogsOffsetRow
17+
want string
18+
}{
19+
{
20+
name: "mainline",
21+
alog: database.GetAuditLogsOffsetRow{
22+
Action: database.AuditActionCreate,
23+
StatusCode: 200,
24+
ResourceType: database.ResourceTypeWorkspace,
25+
},
26+
want: "{user} created workspace {target}",
27+
},
28+
{
29+
name: "unsuccessful",
30+
alog: database.GetAuditLogsOffsetRow{
31+
Action: database.AuditActionCreate,
32+
StatusCode: 400,
33+
ResourceType: database.ResourceTypeWorkspace,
34+
},
35+
want: "{user} unsuccessfully attempted to create workspace {target}",
36+
},
37+
{
38+
name: "login",
39+
alog: database.GetAuditLogsOffsetRow{
40+
Action: database.AuditActionLogin,
41+
StatusCode: 200,
42+
ResourceType: database.ResourceTypeApiKey,
43+
},
44+
want: "{user} logged in",
45+
},
46+
{
47+
name: "unsuccessful_login",
48+
alog: database.GetAuditLogsOffsetRow{
49+
Action: database.AuditActionLogin,
50+
StatusCode: 401,
51+
ResourceType: database.ResourceTypeApiKey,
52+
},
53+
want: "{user} unsuccessfully attempted to login",
54+
},
55+
{
56+
name: "gitsshkey",
57+
alog: database.GetAuditLogsOffsetRow{
58+
Action: database.AuditActionDelete,
59+
StatusCode: 200,
60+
ResourceType: database.ResourceTypeGitSshKey,
61+
},
62+
want: "{user} deleted the git ssh key",
63+
},
64+
} {
65+
t.Run(tc.name, func(t *testing.T) {
66+
t.Parallel()
67+
got := auditLogDescription(tc.alog)
68+
require.Equal(t, tc.want, got)
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)