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

adds the notion of a connection deadline to User #3580

Merged
merged 1 commit into from Oct 28, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/auth.go
Expand Up @@ -71,6 +71,7 @@ type User struct {
Password string `json:"password"`
Permissions *Permissions `json:"permissions,omitempty"`
Account *Account `json:"account,omitempty"`
ConnectionDeadline time.Time `json:"connection_deadline,omitempty"`
AllowedConnectionTypes map[string]struct{} `json:"connection_types,omitempty"`
}

Expand All @@ -83,6 +84,14 @@ func (u *User) clone() *User {
clone := &User{}
*clone = *u
clone.Permissions = u.Permissions.clone()

if len(u.AllowedConnectionTypes) > 0 {
Copy link
Contributor Author

@ripienaar ripienaar Oct 27, 2022

Choose a reason for hiding this comment

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

dont know if this is needed, just noticed it might not be cloning this map so added it, not related to the PR really

Copy link
Member

Choose a reason for hiding this comment

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

I guess this is good to do.

clone.AllowedConnectionTypes = make(map[string]struct{})
for k, v := range u.AllowedConnectionTypes {
clone.AllowedConnectionTypes[k] = v
}
}

return clone
}

Expand Down
37 changes: 37 additions & 0 deletions server/auth_test.go
Expand Up @@ -14,6 +14,7 @@
package server

import (
"context"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -276,6 +277,42 @@ func TestNoAuthUser(t *testing.T) {
}
}

func TestUserConnectionDeadline(t *testing.T) {
clientAuth := &DummyAuth{
t: t,
register: true,
deadline: time.Now().Add(50 * time.Millisecond),
}

opts := DefaultOptions()
opts.CustomClientAuthentication = clientAuth

s := RunServer(opts)
defer s.Shutdown()

var dcerr error

ctx, cancel := context.WithTimeout(context.Background(), time.Second)

nc, err := nats.Connect(s.ClientURL(), nats.UserInfo("valid", ""), nats.NoReconnect(), nats.ErrorHandler(func(nc *nats.Conn, _ *nats.Subscription, err error) {
dcerr = err
cancel()
}))
if err != nil {
t.Fatalf("Expected client to connect, got: %s", err)
}

<-ctx.Done()

if nc.IsConnected() {
t.Fatalf("Expected to be disconnected")
}

if dcerr == nil || dcerr.Error() != "nats: authentication expired" {
t.Fatalf("Expected a auth expired error: got: %v", dcerr)
}
}

func TestNoAuthUserNoConnectProto(t *testing.T) {
conf := createConfFile(t, []byte(`
listen: "127.0.0.1:-1"
Expand Down
5 changes: 5 additions & 0 deletions server/client.go
Expand Up @@ -840,6 +840,11 @@ func (c *client) RegisterUser(user *User) {
c.opts.Username = user.Username
}

// if a deadline time stamp is set we start a timer to disconnect the user at that time
if !user.ConnectionDeadline.IsZero() {
c.atmr = time.AfterFunc(time.Until(user.ConnectionDeadline), c.authExpired)
}

c.mu.Unlock()
}

Expand Down
20 changes: 18 additions & 2 deletions server/server_test.go
Expand Up @@ -611,6 +611,8 @@ func TestNilMonitoringPort(t *testing.T) {
type DummyAuth struct {
t *testing.T
needNonce bool
deadline time.Time
register bool
}

func (d *DummyAuth) Check(c ClientAuthentication) bool {
Expand All @@ -620,12 +622,26 @@ func (d *DummyAuth) Check(c ClientAuthentication) bool {
d.t.Fatalf("Received a nonce when none was expected")
}

return c.GetOpts().Username == "valid"
if c.GetOpts().Username != "valid" {
return false
}

if !d.register {
return true
}

u := &User{
Username: c.GetOpts().Username,
ConnectionDeadline: d.deadline,
}
c.RegisterUser(u)

return true
}

func TestCustomClientAuthentication(t *testing.T) {
testAuth := func(t *testing.T, nonce bool) {
clientAuth := &DummyAuth{t, nonce}
clientAuth := &DummyAuth{t: t, needNonce: nonce}

opts := DefaultOptions()
opts.CustomClientAuthentication = clientAuth
Expand Down