From a2f7b30a0fd6d9baf93e1e9bdd7a7f785da35bd2 Mon Sep 17 00:00:00 2001 From: Kian Parvin Date: Tue, 17 Dec 2024 16:34:56 +0200 Subject: [PATCH] chore: rename jwtgenerator to jujuauth --- .../{jwtgenerator => jujuauth}/jwtgenerator.go | 18 +++++++++--------- .../jwtgenerator_test.go | 8 ++++---- internal/jujuapi/websocket.go | 6 +++--- 3 files changed, 16 insertions(+), 16 deletions(-) rename internal/jimm/{jwtgenerator => jujuauth}/jwtgenerator.go (90%) rename internal/jimm/{jwtgenerator => jujuauth}/jwtgenerator_test.go (98%) diff --git a/internal/jimm/jwtgenerator/jwtgenerator.go b/internal/jimm/jujuauth/jwtgenerator.go similarity index 90% rename from internal/jimm/jwtgenerator/jwtgenerator.go rename to internal/jimm/jujuauth/jwtgenerator.go index 9f07a41ac..fd648b4d6 100644 --- a/internal/jimm/jwtgenerator/jwtgenerator.go +++ b/internal/jimm/jujuauth/jwtgenerator.go @@ -6,7 +6,7 @@ // JWT token generator as it crafts Juju specific // permissions that are added as claims to the JWT // and therefore exists in JIMM's business logic layer. -package jwtgenerator +package jujuauth import ( "context" @@ -42,8 +42,8 @@ type jwtService interface { NewJWT(context.Context, jimmjwx.JWTParams) ([]byte, error) } -// JWTGenerator provides the necessary state and methods to authorize a user and generate JWT tokens. -type JWTGenerator struct { +// TokenGenerator provides the necessary state and methods to authorize a user and generate JWT tokens. +type TokenGenerator struct { database generatorDatabase accessChecker generatorAccessChecker jwtService jwtService @@ -57,8 +57,8 @@ type JWTGenerator struct { } // New returns a new JWTGenerator. -func New(database generatorDatabase, accessChecker generatorAccessChecker, jwtService jwtService) JWTGenerator { - return JWTGenerator{ +func New(database generatorDatabase, accessChecker generatorAccessChecker, jwtService jwtService) TokenGenerator { + return TokenGenerator{ database: database, accessChecker: accessChecker, jwtService: jwtService, @@ -66,13 +66,13 @@ func New(database generatorDatabase, accessChecker generatorAccessChecker, jwtSe } // SetTags implements TokenGenerator. -func (auth *JWTGenerator) SetTags(mt names.ModelTag, ct names.ControllerTag) { +func (auth *TokenGenerator) SetTags(mt names.ModelTag, ct names.ControllerTag) { auth.mt = mt auth.ct = ct } // SetTags implements TokenGenerator. -func (auth *JWTGenerator) GetUser() names.UserTag { +func (auth *TokenGenerator) GetUser() names.UserTag { if auth.user != nil { return auth.user.ResourceTag() } @@ -82,7 +82,7 @@ func (auth *JWTGenerator) GetUser() names.UserTag { // MakeLoginToken authorizes the user based on the provided login requests and returns // a JWT containing claims about user's access to the controller, model (if applicable) // and all clouds that the controller knows about. -func (auth *JWTGenerator) MakeLoginToken(ctx context.Context, user *openfga.User) ([]byte, error) { +func (auth *TokenGenerator) MakeLoginToken(ctx context.Context, user *openfga.User) ([]byte, error) { const op = errors.Op("jimm.MakeLoginToken") auth.mu.Lock() @@ -148,7 +148,7 @@ func (auth *JWTGenerator) MakeLoginToken(ctx context.Context, user *openfga.User // MakeToken assumes MakeLoginToken has already been called and checks the permissions // specified in the permissionMap. If the logged in user has all those permissions // a JWT will be returned with assertions confirming all those permissions. -func (auth *JWTGenerator) MakeToken(ctx context.Context, permissionMap map[string]interface{}) ([]byte, error) { +func (auth *TokenGenerator) MakeToken(ctx context.Context, permissionMap map[string]interface{}) ([]byte, error) { const op = errors.Op("jimm.MakeToken") auth.mu.Lock() diff --git a/internal/jimm/jwtgenerator/jwtgenerator_test.go b/internal/jimm/jujuauth/jwtgenerator_test.go similarity index 98% rename from internal/jimm/jwtgenerator/jwtgenerator_test.go rename to internal/jimm/jujuauth/jwtgenerator_test.go index f60a11035..f04519486 100644 --- a/internal/jimm/jwtgenerator/jwtgenerator_test.go +++ b/internal/jimm/jujuauth/jwtgenerator_test.go @@ -1,6 +1,6 @@ // Copyright 2024 Canonical. -package jwtgenerator_test +package jujuauth_test import ( "context" @@ -12,7 +12,7 @@ import ( "github.com/canonical/jimm/v3/internal/dbmodel" "github.com/canonical/jimm/v3/internal/errors" - "github.com/canonical/jimm/v3/internal/jimm/jwtgenerator" + "github.com/canonical/jimm/v3/internal/jimm/jujuauth" "github.com/canonical/jimm/v3/internal/jimmjwx" "github.com/canonical/jimm/v3/internal/openfga" ) @@ -238,7 +238,7 @@ func TestJWTGeneratorMakeLoginToken(t *testing.T) { }} for _, test := range tests { - generator := jwtgenerator.New(test.database, test.accessChecker, test.jwtService) + generator := jujuauth.New(test.database, test.accessChecker, test.jwtService) generator.SetTags(mt, ct) i, err := dbmodel.NewIdentity(test.username) @@ -311,7 +311,7 @@ func TestJWTGeneratorMakeToken(t *testing.T) { }} for _, test := range tests { - generator := jwtgenerator.New( + generator := jujuauth.New( &testDatabase{ ctl: dbmodel.Controller{ CloudRegions: []dbmodel.CloudRegionControllerPriority{{ diff --git a/internal/jujuapi/websocket.go b/internal/jujuapi/websocket.go index 95461c8cd..3f8d70efa 100644 --- a/internal/jujuapi/websocket.go +++ b/internal/jujuapi/websocket.go @@ -21,7 +21,7 @@ import ( "github.com/canonical/jimm/v3/internal/dbmodel" "github.com/canonical/jimm/v3/internal/errors" "github.com/canonical/jimm/v3/internal/jimm" - "github.com/canonical/jimm/v3/internal/jimm/jwtgenerator" + "github.com/canonical/jimm/v3/internal/jimm/jujuauth" "github.com/canonical/jimm/v3/internal/jimmhttp" jimmRPC "github.com/canonical/jimm/v3/internal/rpc" ) @@ -173,7 +173,7 @@ func modelInfoFromPath(path string) (uuid string, finalPath string, err error) { // We act as a proxier, handling auth on requests before forwarding the // requests to the appropriate Juju controller. func (s apiProxier) ServeWS(ctx context.Context, clientConn *websocket.Conn) { - jwtGenerator := jwtgenerator.New(s.jimm.Database, s.jimm, s.jimm.JWTService) + jwtGenerator := jujuauth.New(s.jimm.Database, s.jimm, s.jimm.JWTService) connectionFunc := controllerConnectionFunc(s, &jwtGenerator) zapctx.Debug(ctx, "Starting proxier") auditLogger := s.jimm.AddAuditLogEntry @@ -192,7 +192,7 @@ func (s apiProxier) ServeWS(ctx context.Context, clientConn *websocket.Conn) { // controllerConnectionFunc returns a function that will be used to // connect to a controller when a client makes a request. -func controllerConnectionFunc(s apiProxier, jwtGenerator *jwtgenerator.JWTGenerator) func(context.Context) (jimmRPC.WebsocketConnectionWithMetadata, error) { +func controllerConnectionFunc(s apiProxier, jwtGenerator *jujuauth.TokenGenerator) func(context.Context) (jimmRPC.WebsocketConnectionWithMetadata, error) { return func(ctx context.Context) (jimmRPC.WebsocketConnectionWithMetadata, error) { const op = errors.Op("proxy.controllerConnectionFunc") path := jimmhttp.PathElementFromContext(ctx, "path")