diff --git a/internal/jimm/audit_log.go b/internal/jimm/audit_log.go index 504016bd0..4694d816c 100644 --- a/internal/jimm/audit_log.go +++ b/internal/jimm/audit_log.go @@ -4,8 +4,6 @@ package jimm import ( "context" - "crypto/rand" - "encoding/hex" "encoding/json" "time" @@ -18,6 +16,7 @@ import ( "github.com/canonical/jimm/internal/db" "github.com/canonical/jimm/internal/dbmodel" "github.com/canonical/jimm/internal/servermon" + "github.com/canonical/jimm/internal/utils" ) type DbAuditLogger struct { @@ -26,19 +25,11 @@ type DbAuditLogger struct { getUser func() names.UserTag } -// NewConversationID generates a unique ID that is used for the -// lifetime of a websocket connection. -func NewConversationID() string { - buf := make([]byte, 8) - rand.Read(buf) // Can't fail - return hex.EncodeToString(buf) -} - // NewDbAuditLogger returns a new audit logger that logs to the database. func NewDbAuditLogger(j *JIMM, getUserFunc func() names.UserTag) DbAuditLogger { logger := DbAuditLogger{ jimm: j, - conversationId: NewConversationID(), + conversationId: utils.NewConversationID(), getUser: getUserFunc, } return logger @@ -112,7 +103,7 @@ type recorder struct { func NewRecorder(logger DbAuditLogger) recorder { return recorder{ start: time.Now(), - conversationId: NewConversationID(), + conversationId: utils.NewConversationID(), logger: logger, } } diff --git a/internal/rpc/proxy.go b/internal/rpc/proxy.go index 2f1b8cae6..6c332f357 100644 --- a/internal/rpc/proxy.go +++ b/internal/rpc/proxy.go @@ -17,7 +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" + "github.com/canonical/jimm/internal/utils" ) // TokenGenerator authenticates a user and generates a JWT token. @@ -464,7 +464,7 @@ func ProxySockets(ctx context.Context, helpers ProxyHelpers) error { msgs: &msgInFlight, tokenGen: helpers.TokenGen, auditLog: helpers.AuditLog, - conversationId: jimm.NewConversationID(), + conversationId: utils.NewConversationID(), }, errChan: errChan, createControllerConn: helpers.ConnectController, diff --git a/internal/utils/utils.go b/internal/utils/utils.go new file mode 100644 index 000000000..4652cc3a8 --- /dev/null +++ b/internal/utils/utils.go @@ -0,0 +1,14 @@ +package utils + +import ( + "crypto/rand" + "encoding/hex" +) + +// NewConversationID generates a unique ID that is used for the +// lifetime of a websocket connection. +func NewConversationID() string { + buf := make([]byte, 8) + rand.Read(buf) // Can't fail + return hex.EncodeToString(buf) +} diff --git a/internal/utils/utils_test.go b/internal/utils/utils_test.go new file mode 100644 index 000000000..8b842aab9 --- /dev/null +++ b/internal/utils/utils_test.go @@ -0,0 +1,15 @@ +package utils_test + +import ( + "testing" + + qt "github.com/frankban/quicktest" + + "github.com/canonical/jimm/internal/utils" +) + +func TestNewConversationID(t *testing.T) { + c := qt.New(t) + res := utils.NewConversationID() + c.Assert(res, qt.HasLen, 16) +}