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

CSS-7044 Added mock authenticator #1160

Merged
merged 5 commits into from
Feb 20, 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
11 changes: 9 additions & 2 deletions internal/auth/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,19 @@ func (as *AuthenticationService) MintSessionToken(email string, secretKey string
return base64.StdEncoding.EncodeToString(freshToken), nil
}

// VerifySessionToken calls the exported VerifySessionToken function.
func (as *AuthenticationService) VerifySessionToken(token string, secretKey string) (jwt.Token, error) {
return VerifySessionToken(token, secretKey)
}

// VerifySessionToken symmetrically verifies the validty of the signature on the
// access token JWT, returning the parsed token.
//
// The subject of the token contains the user's email and can be used
// for user object creation.
func (as *AuthenticationService) VerifySessionToken(token string, secretKey string) (jwt.Token, error) {
// for user object creation
kian99 marked this conversation as resolved.
Show resolved Hide resolved
//
// This method is exported for use by the mock authenticator.
func VerifySessionToken(token string, secretKey string) (jwt.Token, error) {
kian99 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure i understand why is this a separate function and not directly inside func (as *AuthenticationService) VerifySessionToken

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in other words, why do we need a wrapper and a function, and not only the function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. As a method it's basically a class method, as a function it's like a static method namespaced to the auth package. It probably shouldn't have been added as a method on the struct, but I can't remove it without also changing the interface (this method implements the OAuth interface). So I'll keep it like this until Alex is back and we can discuss changing the interface.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha, good point

const op = errors.Op("auth.AuthenticationService.VerifySessionToken")

if len(token) == 0 {
Expand Down
18 changes: 18 additions & 0 deletions internal/jimmtest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import (
"context"

jujuparams "github.com/juju/juju/rpc/params"
"github.com/lestrrat-go/jwx/v2/jwt"

"github.com/canonical/jimm/internal/auth"
"github.com/canonical/jimm/internal/jimm"
"github.com/canonical/jimm/internal/openfga"
)

Expand All @@ -21,3 +24,18 @@ type Authenticator struct {
func (a Authenticator) Authenticate(_ context.Context, _ *jujuparams.LoginRequest) (*openfga.User, error) {
return a.User, a.Err
}

type MockOAuthAuthenticator struct {
jimm.OAuthAuthenticator
secretKey string
}

func NewMockOAuthAuthenticator(secretKey string) MockOAuthAuthenticator {
return MockOAuthAuthenticator{secretKey: secretKey}
}

// VerifySessionToken provides the mock implementation for verifying session tokens.
// Allowing JIMM tests to create their own session tokens that will always be accepted.
func (m MockOAuthAuthenticator) VerifySessionToken(token string, secretKey string) (jwt.Token, error) {
return auth.VerifySessionToken(token, m.secretKey)
}
3 changes: 3 additions & 0 deletions internal/jimmtest/keycloak.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,18 @@ func CreateRandomKeycloakUser() (*KeycloakUser, error) {
}

if err := addKeycloakUser(adminCLIToken, email, username); err != nil {
zapctx.Error(context.Background(), "failed to add keycloak user", zap.Error(err))
return nil, errors.E(err, fmt.Sprintf("failed to add keycloak user (%q, %q)", email, username))
}

id, err := getKeycloakUserId(adminCLIToken, username)
if err != nil {
zapctx.Error(context.Background(), "failed to get keycloak user ID", zap.Error(err))
return nil, errors.E(err, fmt.Sprintf("failed to retrieve ID for newly added keycloak user (%q, %q)", email, username))
}

if err := setKeycloakUserPassword(adminCLIToken, id, password); err != nil {
zapctx.Error(context.Background(), "failed to set keycloak user password", zap.Error(err))
return nil, errors.E(err, fmt.Sprintf("failed to set password for newly added keycloak user (%q, %q, %q)", email, username, password))
}
return &KeycloakUser{
Expand Down
13 changes: 2 additions & 11 deletions internal/jimmtest/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/canonical/candid/candidtest"
cofga "github.com/canonical/ofga"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/go-chi/chi/v5"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery/identchecker"
Expand Down Expand Up @@ -87,16 +86,8 @@ func (s *JIMMSuite) SetUpTest(c *gc.C) {
ctx, cancel := context.WithCancel(context.Background())
s.cancel = cancel

// Connects to a pre-configured keycloak realm
authSvc, err := auth.NewAuthenticationService(ctx, auth.AuthenticationServiceParams{
IssuerURL: "http://localhost:8082/realms/jimm",
ClientID: "jimm-device",
ClientSecret: "SwjDofnbDzJDm9iyfUhEp67FfUFMY8L4",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
SessionTokenExpiry: time.Hour,
})
c.Assert(err, gc.Equals, nil)
s.JIMM.OAuthAuthenticator = authSvc
// Note that the secret key here must match what is used in tests.
s.JIMM.OAuthAuthenticator = NewMockOAuthAuthenticator("test-key")
babakks marked this conversation as resolved.
Show resolved Hide resolved

err = s.JIMM.Database.Migrate(ctx, false)
c.Assert(err, gc.Equals, nil)
Expand Down
19 changes: 19 additions & 0 deletions internal/jujuapi/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ import (
"net/url"
"regexp"
"strings"
"time"

"github.com/canonical/jimm/api/params"
"github.com/canonical/jimm/internal/auth"
"github.com/canonical/jimm/internal/dbmodel"
"github.com/canonical/jimm/internal/jimmtest"
"github.com/coreos/go-oidc/v3/oidc"
"github.com/juju/juju/api"
jujuparams "github.com/juju/juju/rpc/params"
gc "gopkg.in/check.v1"
Expand All @@ -26,6 +29,22 @@ type adminSuite struct {
websocketSuite
}

func (s *adminSuite) SetUpTest(c *gc.C) {
s.websocketSuite.SetUpTest(c)
ctx := context.Background()
// Replace JIMM's mock authenticator with a real one here
// for testing the login flows.
authSvc, err := auth.NewAuthenticationService(ctx, auth.AuthenticationServiceParams{
IssuerURL: "http://localhost:8082/realms/jimm",
ClientID: "jimm-device",
ClientSecret: "SwjDofnbDzJDm9iyfUhEp67FfUFMY8L4",
Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
SessionTokenExpiry: time.Hour,
})
c.Assert(err, gc.Equals, nil)
s.JIMM.OAuthAuthenticator = authSvc
}

var _ = gc.Suite(&adminSuite{})

func (s *adminSuite) TestLoginToController(c *gc.C) {
Expand Down
Loading