Skip to content

Commit

Permalink
Audit log fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kian99 committed Sep 7, 2023
1 parent 48fff0e commit 4115909
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion cmd/jimmctl/cmd/listauditevents.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *listAuditEventsCommand) SetFlags(f *gnuflag.FlagSet) {
f.StringVar(&c.args.UserTag, "user-tag", "", "display events performed by authenticated user")
f.StringVar(&c.args.Method, "method", "", "display events for a specific method call")
f.StringVar(&c.args.Model, "model", "", "display events for a specific model (model name is controller/model)")
f.IntVar(&c.args.Limit, "offset", 0, "offset the set of returned audit events")
f.IntVar(&c.args.Offset, "offset", 0, "offset the set of returned audit events")
f.IntVar(&c.args.Limit, "limit", 0, "limit the maximum number of returned audit events")
f.BoolVar(&c.args.SortTime, "reverse", false, "reverse the order of logs, showing the most recent first")

Expand Down
4 changes: 3 additions & 1 deletion cmd/jimmctl/cmd/purge_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ const purgeLogsDoc = `

// NewPurgeLogsCommand returns a command to purge logs.
func NewPurgeLogsCommand() cmd.Command {
cmd := &purgeLogsCommand{}
cmd := &purgeLogsCommand{
store: jujuclient.NewFileClientStore(),
}
return modelcmd.WrapBase(cmd)
}

Expand Down
10 changes: 5 additions & 5 deletions internal/jimm/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ type DbAuditLogger struct {
getUser func() names.UserTag
}

// newConversationID generates a unique ID that is used for the
// NewConversationID generates a unique ID that is used for the
// lifetime of a websocket connection.
func newConversationID() string {
func NewConversationID() string {
buf := make([]byte, 8)
rand.Read(buf) // Can't fail
return hex.EncodeToString(buf)
Expand All @@ -38,7 +38,7 @@ func newConversationID() string {
func NewDbAuditLogger(j *JIMM, getUserFunc func() names.UserTag) DbAuditLogger {
logger := DbAuditLogger{
jimm: j,
conversationId: newConversationID(),
conversationId: NewConversationID(),
getUser: getUserFunc,
}
return logger
Expand Down Expand Up @@ -112,7 +112,7 @@ type recorder struct {
func NewRecorder(logger DbAuditLogger) recorder {
return recorder{
start: time.Now(),
conversationId: newConversationID(),
conversationId: NewConversationID(),
logger: logger,
}
}
Expand Down Expand Up @@ -166,11 +166,11 @@ func (a *auditLogCleanupService) Start(ctx context.Context) {
// from the service's context. It calculates the poll duration at 9am each day
// UTC.
func (a *auditLogCleanupService) poll(ctx context.Context) {
retentionDate := time.Now().AddDate(0, 0, -(a.auditLogRetentionPeriodInDays))

for {
select {
case <-time.After(calculateNextPollDuration(time.Now().UTC())):
retentionDate := time.Now().AddDate(0, 0, -(a.auditLogRetentionPeriodInDays))
deleted, err := a.db.DeleteAuditLogsBefore(ctx, retentionDate)
if err != nil {
zapctx.Error(ctx, "failed to cleanup audit logs", zap.Error(err))
Expand Down
11 changes: 6 additions & 5 deletions internal/rpc/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/canonical/jimm/internal/auth"
"github.com/canonical/jimm/internal/dbmodel"
"github.com/canonical/jimm/internal/errors"
"github.com/canonical/jimm/internal/jimm"
)

// TokenGenerator authenticates a user and generates a JWT token.
Expand Down Expand Up @@ -286,7 +287,6 @@ func (p *controllerProxy) start(ctx context.Context) error {
zapctx.Debug(ctx, "Reading on controller connection")
msg := new(message)
if err := p.src.readJson(msg); err != nil {
zapctx.Error(ctx, "controllerProxy error reading from src", zap.Error(err))
// Error reading on the socket implies it is closed, simply return.
return err
}
Expand Down Expand Up @@ -460,10 +460,11 @@ func ProxySockets(ctx context.Context, helpers ProxyHelpers) error {
// after the first message has been received so that any errors can be properly sent back to the client.
clProxy := clientProxy{
modelProxy: modelProxy{
src: &client,
msgs: &msgInFlight,
tokenGen: helpers.TokenGen,
auditLog: helpers.AuditLog,
src: &client,
msgs: &msgInFlight,
tokenGen: helpers.TokenGen,
auditLog: helpers.AuditLog,
conversationId: jimm.NewConversationID(),
},
errChan: errChan,
createControllerConn: helpers.ConnectController,
Expand Down

0 comments on commit 4115909

Please sign in to comment.