Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit 2f3bf5e

Browse files
[Backport 5.1] treat unknown actors as internal for the purpose of audit logs (#54895)
1 parent 98ba21d commit 2f3bf5e

File tree

5 files changed

+15
-2
lines changed

5 files changed

+15
-2
lines changed

cmd/frontend/internal/httpapi/graphql_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/sourcegraph/log/logtest"
99
"github.com/stretchr/testify/assert"
1010

11+
"github.com/sourcegraph/sourcegraph/internal/actor"
1112
"github.com/sourcegraph/sourcegraph/internal/conf"
1213
"github.com/sourcegraph/sourcegraph/schema"
1314
)
@@ -49,7 +50,8 @@ func Test_recordAuditLog(t *testing.T) {
4950

5051
logger, exportLogs := logtest.Captured(t)
5152

52-
recordAuditLog(context.Background(), logger, traceData{
53+
ctx := actor.WithActor(context.Background(), actor.FromUser(1))
54+
recordAuditLog(ctx, logger, traceData{
5355
queryParams: graphQLQueryParams{
5456
Query: `repository(name: "github.com/gorilla/mux") { name }`,
5557
Variables: map[string]any{"param1": "value1"},

cmd/gitserver/server/accesslog/BUILD.bazel

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

cmd/gitserver/server/accesslog/accesslog_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/stretchr/testify/require"
1313
"google.golang.org/grpc"
1414

15+
"github.com/sourcegraph/sourcegraph/internal/actor"
1516
"github.com/sourcegraph/sourcegraph/internal/conf/conftypes"
1617
"github.com/sourcegraph/sourcegraph/internal/requestclient"
1718
"github.com/sourcegraph/sourcegraph/schema"
@@ -121,6 +122,7 @@ func TestHTTPMiddleware(t *testing.T) {
121122
})
122123
rec := httptest.NewRecorder()
123124
req := httptest.NewRequest("GET", "/", nil)
125+
req = req.WithContext(actor.WithActor(context.Background(), actor.FromUser(32)))
124126

125127
// Request with access logging disabled
126128
h.ServeHTTP(rec, req)

internal/audit/audit.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ func Log(ctx context.Context, logger log.Logger, record Record) {
2323

2424
// internal actors add a lot of noise to the audit log
2525
siteConfig := conf.SiteConfig()
26+
// if the actor is internal and internal traffic logging is disabled, do not log
2627
if act.Internal && !IsEnabled(siteConfig, InternalTraffic) {
2728
return
2829
}
2930

3031
client := requestclient.FromContext(ctx)
31-
32+
// if the actor and client ip is unknown, and internal traffic logging is disabled, do not log
33+
// internal actors generate a large volume of logs, and they are generally not useful
34+
if (actorId(act) == "unknown" && ip(client) == "unknown") && !IsEnabled(siteConfig, InternalTraffic) {
35+
return
36+
}
3237
auditId := uuid.New().String()
3338
if record.auditIDGenerator != nil {
3439
auditId = record.auditIDGenerator()

internal/database/security_event_logs_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,19 @@ func TestSecurityEventLogs_ValidInfo(t *testing.T) {
7171
},
7272
{
7373
name: "JustUser",
74+
actor: &actor.Actor{UID: 1}, // if we have a userID, we should have a valid actor UID
7475
event: &SecurityEvent{Name: "test_event", URL: "http://sourcegraph.com", Source: "Web", UserID: 1, AnonymousUserID: ""},
7576
err: "<nil>",
7677
},
7778
{
7879
name: "JustAnonymous",
80+
actor: &actor.Actor{AnonymousUID: "blah"},
7981
event: &SecurityEvent{Name: "test_event", URL: "http://sourcegraph.com", Source: "Web", UserID: 0, AnonymousUserID: "blah"},
8082
err: "<nil>",
8183
},
8284
{
8385
name: "ValidInsert",
86+
actor: &actor.Actor{UID: 1}, // if we have a userID, we should have a valid actor UID
8487
event: &SecurityEvent{Name: "test_event", UserID: 1, URL: "http://sourcegraph.com", Source: "WEB"},
8588
err: "<nil>",
8689
},

0 commit comments

Comments
 (0)