Skip to content

Commit 869b568

Browse files
Merge pull request #11 from nais/remove-component
Remove component name from audit logs
2 parents 8bebba7 + 7916792 commit 869b568

18 files changed

+54
-156
lines changed

internal/auditlogger/logger.go

+10-14
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/nais/api/internal/auditlogger/audittype"
99
"github.com/nais/api/internal/auth/authz"
1010
"github.com/nais/api/internal/database"
11-
"github.com/nais/api/internal/logger"
1211
"github.com/nais/api/internal/slug"
1312
"github.com/sirupsen/logrus"
1413
"k8s.io/utils/ptr"
@@ -19,9 +18,8 @@ type AuditLogger interface {
1918
}
2019

2120
type auditLogger struct {
22-
componentName logger.ComponentName
23-
db database.Database
24-
log logrus.FieldLogger
21+
db database.Database
22+
log logrus.FieldLogger
2523
}
2624

2725
type auditLoggerForTesting struct {
@@ -46,11 +44,10 @@ type Entry struct {
4644
Message string
4745
}
4846

49-
func New(db database.Database, componentName logger.ComponentName, log logrus.FieldLogger) AuditLogger {
47+
func New(db database.Database, log logrus.FieldLogger) AuditLogger {
5048
return &auditLogger{
51-
componentName: componentName,
52-
db: db,
53-
log: log.WithField("component", componentName),
49+
db: db,
50+
log: log,
5451
}
5552
}
5653

@@ -96,7 +93,6 @@ func (l *auditLogger) Logf(ctx context.Context, targets []Target, fields Fields,
9693
err := l.db.CreateAuditLogEntry(
9794
ctx,
9895
fields.CorrelationID,
99-
l.componentName,
10096
actor,
10197
target.Type,
10298
target.Identifier,
@@ -135,6 +131,10 @@ func (l *auditLogger) Logf(ctx context.Context, targets []Target, fields Fields,
135131
}
136132
}
137133

134+
func SystemTarget(systemName string) Target {
135+
return Target{Type: audittype.AuditLogsTargetTypeSystem, Identifier: systemName}
136+
}
137+
138138
func UserTarget(email string) Target {
139139
return Target{Type: audittype.AuditLogsTargetTypeUser, Identifier: email}
140140
}
@@ -144,9 +144,5 @@ func TeamTarget(slug slug.Slug) Target {
144144
}
145145

146146
func ReconcilerTarget(name string) Target {
147-
return Target{Type: audittype.AuditLogsTargetTypeReconciler, Identifier: string(name)}
148-
}
149-
150-
func ComponentTarget(name logger.ComponentName) Target {
151-
return Target{Type: audittype.AuditLogsTargetTypeSystem, Identifier: string(name)}
147+
return Target{Type: audittype.AuditLogsTargetTypeReconciler, Identifier: name}
152148
}

internal/auditlogger/logger_test.go

+11-16
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/nais/api/internal/auditlogger/audittype"
1212
"github.com/nais/api/internal/auth/authz"
1313
"github.com/nais/api/internal/database"
14-
"github.com/nais/api/internal/logger"
1514
"github.com/nais/api/internal/slug"
1615
"github.com/sirupsen/logrus"
1716
"github.com/sirupsen/logrus/hooks/test"
@@ -35,19 +34,18 @@ func Test_Logf(t *testing.T) {
3534
ctx := context.Background()
3635
db := database.NewMockDatabase(t)
3736
msg := "some message"
38-
componentName := logger.ComponentNameGraphqlApi
3937

4038
t.Run("missing audit action", func(t *testing.T) {
4139
testLogger, hook := test.NewNullLogger()
4240

4341
auditlogger.
44-
New(db, componentName, testLogger).
42+
New(db, testLogger).
4543
Logf(ctx, []auditlogger.Target{}, auditlogger.Fields{}, msg)
4644

4745
want := []*logrus.Entry{
4846
{
4947
Message: "unable to create auditlog entry: missing or invalid audit action",
50-
Data: logrus.Fields{"component": componentName},
48+
Data: logrus.Fields{},
5149
Level: logrus.ErrorLevel,
5250
},
5351
}
@@ -63,7 +61,7 @@ func Test_Logf(t *testing.T) {
6361
Action: audittype.AuditActionAzureGroupAddMember,
6462
}
6563
auditlogger.
66-
New(db, componentName, log).
64+
New(db, log).
6765
Logf(ctx, []auditlogger.Target{}, fields, msg)
6866
})
6967

@@ -73,7 +71,7 @@ func Test_Logf(t *testing.T) {
7371
userEmail := "[email protected]"
7472
teamSlug := slug.Slug("team-slug")
7573
reconcilerName := "github:teams"
76-
componentName := logger.ComponentName("github:teams")
74+
systemName := "some:system"
7775
actorIdentity := "actor"
7876
action := audittype.AuditActionAzureGroupAddMember
7977

@@ -82,7 +80,7 @@ func Test_Logf(t *testing.T) {
8280
auditlogger.UserTarget(userEmail),
8381
auditlogger.TeamTarget(teamSlug),
8482
auditlogger.ReconcilerTarget(reconcilerName),
85-
auditlogger.ComponentTarget(componentName),
83+
auditlogger.SystemTarget(systemName),
8684
}
8785

8886
authenticatedUser := authz.NewMockAuthenticatedUser(t)
@@ -97,19 +95,18 @@ func Test_Logf(t *testing.T) {
9795
}
9896

9997
db := database.NewMockDatabase(t)
100-
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeUser, userEmail, action, msg).Return(nil).Once()
101-
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeTeam, teamSlug.String(), action, msg).Return(nil).Once()
102-
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeReconciler, reconcilerName, action, msg).Return(nil).Once()
103-
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeSystem, string(componentName), action, msg).Return(nil).Once()
98+
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeUser, userEmail, action, msg).Return(nil).Once()
99+
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeTeam, teamSlug.String(), action, msg).Return(nil).Once()
100+
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeReconciler, reconcilerName, action, msg).Return(nil).Once()
101+
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeSystem, systemName, action, msg).Return(nil).Once()
104102

105103
auditlogger.
106-
New(db, componentName, testLogger).
104+
New(db, testLogger).
107105
Logf(ctx, targets, fields, msg)
108106

109107
want := []*logrus.Entry{
110108
{
111109
Data: logrus.Fields{
112-
"component": componentName,
113110
"action": action,
114111
"actor": actorIdentity,
115112
"correlation_id": correlationID.String(),
@@ -121,7 +118,6 @@ func Test_Logf(t *testing.T) {
121118
},
122119
{
123120
Data: logrus.Fields{
124-
"component": componentName,
125121
"action": action,
126122
"actor": actorIdentity,
127123
"correlation_id": correlationID.String(),
@@ -133,7 +129,6 @@ func Test_Logf(t *testing.T) {
133129
},
134130
{
135131
Data: logrus.Fields{
136-
"component": componentName,
137132
"action": action,
138133
"actor": actorIdentity,
139134
"correlation_id": correlationID.String(),
@@ -145,7 +140,7 @@ func Test_Logf(t *testing.T) {
145140
},
146141
{
147142
Data: logrus.Fields{
148-
"component": componentName,
143+
"system": systemName,
149144
"action": action,
150145
"actor": actorIdentity,
151146
"correlation_id": correlationID.String(),

internal/auth/authn/handler.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/coreos/go-oidc/v3/oidc"
1212
"github.com/google/uuid"
1313
"github.com/nais/api/internal/database"
14-
"github.com/nais/api/internal/logger"
1514
"github.com/sirupsen/logrus"
1615
"golang.org/x/oauth2"
1716
)
@@ -47,7 +46,7 @@ func New(oauth2Config OAuth2, db database.Database, log logrus.FieldLogger) Hand
4746
return &handler{
4847
db: db,
4948
oauth2Config: oauth2Config,
50-
log: log.WithField("component", logger.ComponentNameAuthn),
49+
log: log,
5150
}
5251
}
5352

internal/cmd/api/api.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
140140
return fmt.Errorf("unable to create k8s client: %w", err)
141141
}
142142

143-
auditLogger := auditlogger.New(db, logger.ComponentNameGraphqlApi, log)
143+
auditLogger := auditlogger.New(db, log)
144144
userSync := make(chan uuid.UUID, 1)
145145

146146
pubsubClient, err := pubsub.NewClient(ctx, cfg.GoogleManagementProjectID)

internal/database/audit_log.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ import (
66
"github.com/google/uuid"
77
"github.com/nais/api/internal/auditlogger/audittype"
88
"github.com/nais/api/internal/database/gensql"
9-
"github.com/nais/api/internal/logger"
109
"github.com/nais/api/internal/slug"
1110
)
1211

1312
type AuditLogsRepo interface {
14-
CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, componentName logger.ComponentName, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error
13+
CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error
1514
GetAuditLogsForCorrelationID(ctx context.Context, correlationID uuid.UUID, p Page) ([]*AuditLog, int, error)
1615
GetAuditLogsForReconciler(ctx context.Context, reconcilerName string, p Page) ([]*AuditLog, int, error)
1716
GetAuditLogsForTeam(ctx context.Context, teamSlug slug.Slug, p Page) ([]*AuditLog, int, error)
@@ -69,11 +68,10 @@ func (d *database) GetAuditLogsForReconciler(ctx context.Context, reconcilerName
6968
return entries, int(total), nil
7069
}
7170

72-
func (d *database) CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, componentName logger.ComponentName, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error {
71+
func (d *database) CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error {
7372
return d.querier.CreateAuditLog(ctx, gensql.CreateAuditLogParams{
7473
CorrelationID: correlationID,
7574
Actor: actor,
76-
ComponentName: string(componentName),
7775
TargetType: string(targetType),
7876
TargetIdentifier: targetIdentifier,
7977
Action: string(action),

internal/database/gensql/audit_logs.sql.go

+5-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/gensql/models.go

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- +goose Up
2+
3+
ALTER TABLE audit_logs DROP COLUMN component_name;
4+
5+
-- +goose Down
6+
7+
ALTER TABLE audit_logs ADD COLUMN component_name text NOT NULL DEFAULT 'unknown';

internal/database/mock_database.go

+10-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/database/queries/audit_logs.sql

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
-- name: CreateAuditLog :exec
2-
INSERT INTO audit_logs (correlation_id, actor, component_name, target_type, target_identifier, action, message)
3-
VALUES (@correlation_id, @actor, @component_name, @target_type, @target_identifier, @action, @message);
2+
INSERT INTO audit_logs (correlation_id, actor, target_type, target_identifier, action, message)
3+
VALUES (@correlation_id, @actor, @target_type, @target_identifier, @action, @message);
44

55
-- name: GetAuditLogsForTeam :many
66
SELECT * FROM audit_logs

internal/graph/auditlogs.go

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func toGraphAuditLogs(logs []*database.AuditLog) []*model.AuditLog {
1313
ID: scalar.AuditLogIdent(log.ID),
1414
Action: log.Action,
1515
Actor: log.Actor,
16-
ComponentName: log.ComponentName,
1716
TargetType: log.TargetType,
1817
CorrelationID: log.CorrelationID.String(),
1918
TargetIdentifier: log.TargetIdentifier,

0 commit comments

Comments
 (0)