Skip to content

Commit 1f24ace

Browse files
authored
fix: change audit descriptions to indicate unsuccessful attempts (#13897)
1 parent a3f40d5 commit 1f24ace

File tree

2 files changed

+91
-12
lines changed

2 files changed

+91
-12
lines changed

coderd/audit.go

+19-12
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,41 @@ 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+
// NOTE: WriteString always returns a nil error, so we never check it
269+
_, _ = b.WriteString("{user} ")
270+
if alog.StatusCode >= 400 {
271+
_, _ = b.WriteString("unsuccessfully attempted to ")
272+
_, _ = b.WriteString(string(alog.Action))
273+
} else {
274+
_, _ = b.WriteString(codersdk.AuditAction(alog.Action).Friendly())
275+
}
269276

270277
// API Key resources (used for authentication) do not have targets and follow the below format:
271278
// "User {logged in | logged out | registered}"
272279
if alog.ResourceType == database.ResourceTypeApiKey &&
273280
(alog.Action == database.AuditActionLogin || alog.Action == database.AuditActionLogout || alog.Action == database.AuditActionRegister) {
274-
return str
281+
return b.String()
275282
}
276283

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

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

288295
if alog.ResourceType == database.ResourceTypeConvertLogin {
289-
str += " to"
296+
_, _ = b.WriteString(" to")
290297
}
291298

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

294-
return str
301+
return b.String()
295302
}
296303

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

coderd/audit_internal_test.go

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

0 commit comments

Comments
 (0)