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

GetUser check required for jwt auth in files mode #339

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 4 additions & 3 deletions backends/jwt_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewFilesJWTChecker(authOpts map[string]string, logLevel log.Level, hasher h

/* We could ask for a file listing available users with no password, but that gives very little value
versus just assuming users in the ACL file are valid ones, while general rules apply to any user.
Thus, padswords file makes no sense for JWT, we only need to check ACLs.
Thus, passwords file makes no sense for JWT, we only need to check ACLs.
*/
aclPath, ok := authOpts["jwt_acl_path"]
if !ok || aclPath == "" {
Expand All @@ -36,7 +36,8 @@ func NewFilesJWTChecker(authOpts map[string]string, logLevel log.Level, hasher h
}

func (o *filesJWTChecker) GetUser(token string) (bool, error) {
return false, nil
_, err := getUsernameForToken(o.options, token, o.options.skipUserExpiration)
Copy link
Owner

Choose a reason for hiding this comment

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

If there's an actual error maybe we should return it?

Copy link
Author

@december1981 december1981 Sep 28, 2024

Choose a reason for hiding this comment

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

I tried that somewhat naively - but discovered if this propagates an error by returning it, it causes a general failure in the plugin and unresponsive behaviour. As far as I can tell, getUsernameForToken returning an error just means an invalid user, for whatever reason, not a general/unexpected failure, so returning err == nil, nil seemed to be the most fitting return format.

Copy link
Owner

Choose a reason for hiding this comment

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

Oh, ok, I'll take a deeper look into then, thanks.

return err == nil, nil
}

func (o *filesJWTChecker) GetSuperuser(token string) (bool, error) {
Expand All @@ -48,7 +49,7 @@ func (o *filesJWTChecker) CheckAcl(token, topic, clientid string, acc int32) (bo

if err != nil {
log.Printf("jwt get user error: %s", err)
return false, err
return false, nil
}

return o.checker.CheckAcl(username, topic, clientid, acc)
Expand Down
53 changes: 53 additions & 0 deletions backends/jwt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,59 @@ func TestFilesJWTChecker(t *testing.T) {
token, err := notPresentJwtToken.SignedString([]byte(jwtSecret))
So(err, ShouldBeNil)

invalidToken, err := notPresentJwtToken.SignedString([]byte("badsecret"))
So(err, ShouldBeNil)

expiredToken, err := expiredToken.SignedString([]byte(jwtSecret))
So(err, ShouldBeNil)

Convey("Given a valid token, it should correctly authenticate it", func() {
authenticated, err := filesChecker.GetUser(token)

So(err, ShouldBeNil)
So(authenticated, ShouldBeTrue)
})

Convey("Given an invalid token, it should not authenticate it", func() {
authenticated, err := filesChecker.GetUser(invalidToken)

So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})

Convey("Given an expired token, it should not authenticate it", func() {
authenticated, err := filesChecker.GetUser(expiredToken)

So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})

Convey("Given an expired token with skip user expiration, it should anyway authenticate it", func() {
skipExpirationOptions := tkOptions
skipExpirationOptions.skipUserExpiration = true
filesChecker, err := NewFilesJWTChecker(authOpts, logLevel, hasher, skipExpirationOptions)
So(err, ShouldBeNil)

authenticated, err := filesChecker.GetUser(expiredToken)

So(err, ShouldBeNil)
So(authenticated, ShouldBeTrue)
})

Convey("Given a plain non-token format valid username, it should not authenticate it", func() {
authenticated, err := filesChecker.GetUser(username)

So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})

Convey("Given a plain non-token format random username, it should not authenticate it", func() {
authenticated, err := filesChecker.GetUser("somerandomuser")

So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})

Convey("Access should be granted for ACL mentioned users", func() {
tt, err := filesChecker.CheckAcl(token, "test/not_present", "id", 1)

Expand Down