Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove component name from audit logs #11

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions internal/auditlogger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/nais/api/internal/auditlogger/audittype"
"github.com/nais/api/internal/auth/authz"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/logger"
"github.com/nais/api/internal/slug"
"github.com/sirupsen/logrus"
"k8s.io/utils/ptr"
Expand All @@ -19,9 +18,8 @@ type AuditLogger interface {
}

type auditLogger struct {
componentName logger.ComponentName
db database.Database
log logrus.FieldLogger
db database.Database
log logrus.FieldLogger
}

type auditLoggerForTesting struct {
Expand All @@ -46,11 +44,10 @@ type Entry struct {
Message string
}

func New(db database.Database, componentName logger.ComponentName, log logrus.FieldLogger) AuditLogger {
func New(db database.Database, log logrus.FieldLogger) AuditLogger {
return &auditLogger{
componentName: componentName,
db: db,
log: log.WithField("component", componentName),
db: db,
log: log,
}
}

Expand Down Expand Up @@ -96,7 +93,6 @@ func (l *auditLogger) Logf(ctx context.Context, targets []Target, fields Fields,
err := l.db.CreateAuditLogEntry(
ctx,
fields.CorrelationID,
l.componentName,
actor,
target.Type,
target.Identifier,
Expand Down Expand Up @@ -135,6 +131,10 @@ func (l *auditLogger) Logf(ctx context.Context, targets []Target, fields Fields,
}
}

func SystemTarget(systemName string) Target {
return Target{Type: audittype.AuditLogsTargetTypeSystem, Identifier: systemName}
}

func UserTarget(email string) Target {
return Target{Type: audittype.AuditLogsTargetTypeUser, Identifier: email}
}
Expand All @@ -144,9 +144,5 @@ func TeamTarget(slug slug.Slug) Target {
}

func ReconcilerTarget(name string) Target {
return Target{Type: audittype.AuditLogsTargetTypeReconciler, Identifier: string(name)}
}

func ComponentTarget(name logger.ComponentName) Target {
return Target{Type: audittype.AuditLogsTargetTypeSystem, Identifier: string(name)}
return Target{Type: audittype.AuditLogsTargetTypeReconciler, Identifier: name}
}
27 changes: 11 additions & 16 deletions internal/auditlogger/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/nais/api/internal/auditlogger/audittype"
"github.com/nais/api/internal/auth/authz"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/logger"
"github.com/nais/api/internal/slug"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"
Expand All @@ -35,19 +34,18 @@ func Test_Logf(t *testing.T) {
ctx := context.Background()
db := database.NewMockDatabase(t)
msg := "some message"
componentName := logger.ComponentNameGraphqlApi

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

auditlogger.
New(db, componentName, testLogger).
New(db, testLogger).
Logf(ctx, []auditlogger.Target{}, auditlogger.Fields{}, msg)

want := []*logrus.Entry{
{
Message: "unable to create auditlog entry: missing or invalid audit action",
Data: logrus.Fields{"component": componentName},
Data: logrus.Fields{},
Level: logrus.ErrorLevel,
},
}
Expand All @@ -63,7 +61,7 @@ func Test_Logf(t *testing.T) {
Action: audittype.AuditActionAzureGroupAddMember,
}
auditlogger.
New(db, componentName, log).
New(db, log).
Logf(ctx, []auditlogger.Target{}, fields, msg)
})

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

Expand All @@ -82,7 +80,7 @@ func Test_Logf(t *testing.T) {
auditlogger.UserTarget(userEmail),
auditlogger.TeamTarget(teamSlug),
auditlogger.ReconcilerTarget(reconcilerName),
auditlogger.ComponentTarget(componentName),
auditlogger.SystemTarget(systemName),
}

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

db := database.NewMockDatabase(t)
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeUser, userEmail, action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeTeam, teamSlug.String(), action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeReconciler, reconcilerName, action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, componentName, &actorIdentity, audittype.AuditLogsTargetTypeSystem, string(componentName), action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeUser, userEmail, action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeTeam, teamSlug.String(), action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeReconciler, reconcilerName, action, msg).Return(nil).Once()
db.EXPECT().CreateAuditLogEntry(ctx, correlationID, &actorIdentity, audittype.AuditLogsTargetTypeSystem, systemName, action, msg).Return(nil).Once()

auditlogger.
New(db, componentName, testLogger).
New(db, testLogger).
Logf(ctx, targets, fields, msg)

want := []*logrus.Entry{
{
Data: logrus.Fields{
"component": componentName,
"action": action,
"actor": actorIdentity,
"correlation_id": correlationID.String(),
Expand All @@ -121,7 +118,6 @@ func Test_Logf(t *testing.T) {
},
{
Data: logrus.Fields{
"component": componentName,
"action": action,
"actor": actorIdentity,
"correlation_id": correlationID.String(),
Expand All @@ -133,7 +129,6 @@ func Test_Logf(t *testing.T) {
},
{
Data: logrus.Fields{
"component": componentName,
"action": action,
"actor": actorIdentity,
"correlation_id": correlationID.String(),
Expand All @@ -145,7 +140,7 @@ func Test_Logf(t *testing.T) {
},
{
Data: logrus.Fields{
"component": componentName,
"system": systemName,
"action": action,
"actor": actorIdentity,
"correlation_id": correlationID.String(),
Expand Down
3 changes: 1 addition & 2 deletions internal/auth/authn/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/coreos/go-oidc/v3/oidc"
"github.com/google/uuid"
"github.com/nais/api/internal/database"
"github.com/nais/api/internal/logger"
"github.com/sirupsen/logrus"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -47,7 +46,7 @@ func New(oauth2Config OAuth2, db database.Database, log logrus.FieldLogger) Hand
return &handler{
db: db,
oauth2Config: oauth2Config,
log: log.WithField("component", logger.ComponentNameAuthn),
log: log,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func run(ctx context.Context, cfg *Config, log logrus.FieldLogger) error {
return fmt.Errorf("unable to create k8s client: %w", err)
}

auditLogger := auditlogger.New(db, logger.ComponentNameGraphqlApi, log)
auditLogger := auditlogger.New(db, log)
userSync := make(chan uuid.UUID, 1)

pubsubClient, err := pubsub.NewClient(ctx, cfg.GoogleManagementProjectID)
Expand Down
6 changes: 2 additions & 4 deletions internal/database/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import (
"github.com/google/uuid"
"github.com/nais/api/internal/auditlogger/audittype"
"github.com/nais/api/internal/database/gensql"
"github.com/nais/api/internal/logger"
"github.com/nais/api/internal/slug"
)

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

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 {
func (d *database) CreateAuditLogEntry(ctx context.Context, correlationID uuid.UUID, actor *string, targetType audittype.AuditLogsTargetType, targetIdentifier string, action audittype.AuditAction, message string) error {
return d.querier.CreateAuditLog(ctx, gensql.CreateAuditLogParams{
CorrelationID: correlationID,
Actor: actor,
ComponentName: string(componentName),
TargetType: string(targetType),
TargetIdentifier: targetIdentifier,
Action: string(action),
Expand Down
15 changes: 5 additions & 10 deletions internal/database/gensql/audit_logs.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion internal/database/gensql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- +goose Up

ALTER TABLE audit_logs DROP COLUMN component_name;

-- +goose Down

ALTER TABLE audit_logs ADD COLUMN component_name text NOT NULL DEFAULT 'unknown';
23 changes: 10 additions & 13 deletions internal/database/mock_database.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/database/queries/audit_logs.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-- name: CreateAuditLog :exec
INSERT INTO audit_logs (correlation_id, actor, component_name, target_type, target_identifier, action, message)
VALUES (@correlation_id, @actor, @component_name, @target_type, @target_identifier, @action, @message);
INSERT INTO audit_logs (correlation_id, actor, target_type, target_identifier, action, message)
VALUES (@correlation_id, @actor, @target_type, @target_identifier, @action, @message);

-- name: GetAuditLogsForTeam :many
SELECT * FROM audit_logs
Expand Down
1 change: 0 additions & 1 deletion internal/graph/auditlogs.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ func toGraphAuditLogs(logs []*database.AuditLog) []*model.AuditLog {
ID: scalar.AuditLogIdent(log.ID),
Action: log.Action,
Actor: log.Actor,
ComponentName: log.ComponentName,
TargetType: log.TargetType,
CorrelationID: log.CorrelationID.String(),
TargetIdentifier: log.TargetIdentifier,
Expand Down
Loading