Skip to content

Commit

Permalink
Moved NewConversationID to utils package
Browse files Browse the repository at this point in the history
- Created new utils package
  • Loading branch information
kian99 committed Sep 12, 2023
1 parent af6bf59 commit 6f09789
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
15 changes: 3 additions & 12 deletions internal/jimm/audit_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ package jimm

import (
"context"
"crypto/rand"
"encoding/hex"
"encoding/json"
"time"

Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -112,7 +103,7 @@ type recorder struct {
func NewRecorder(logger DbAuditLogger) recorder {
return recorder{
start: time.Now(),
conversationId: NewConversationID(),
conversationId: utils.NewConversationID(),
logger: logger,
}
}
Expand Down
4 changes: 2 additions & 2 deletions internal/rpc/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 6f09789

Please sign in to comment.