mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-17 11:24:44 -07:00
Add in leafnode bound account events for accounting
Signed-off-by: Derek Collison <derek@nats.io>
This commit is contained in:
@@ -636,6 +636,9 @@ func (s *Server) isLeafNodeAuthorized(c *client) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
// Generate an event if we have a system account.
|
||||
s.accountConnectEvent(c)
|
||||
|
||||
// Check if we need to set an auth timer if the user jwt expires.
|
||||
c.checkExpiration(juc.Claims())
|
||||
return true
|
||||
|
||||
@@ -2739,8 +2739,8 @@ func (c *client) clearConnection(reason ClosedState) {
|
||||
// Do this always to also kick out any IO writes.
|
||||
nc.SetWriteDeadline(time.Time{})
|
||||
|
||||
// Save off the connection if its a client.
|
||||
if c.kind == CLIENT {
|
||||
// Save off the connection if its a client or leafnode.
|
||||
if c.kind == CLIENT || c.kind == LEAF {
|
||||
go srv.saveClosedClient(c, nc, reason)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ var (
|
||||
|
||||
const (
|
||||
// VERSION is the current version for the server.
|
||||
VERSION = "2.0.0-RC16"
|
||||
VERSION = "2.0.0-RC17"
|
||||
|
||||
// PROTO is the currently supported protocol.
|
||||
// 0 was the original
|
||||
|
||||
@@ -15,6 +15,7 @@ package test
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/url"
|
||||
@@ -2077,3 +2078,69 @@ func TestLeafNodeServiceImportLikeNGS(t *testing.T) {
|
||||
t.Fatalf("Did not receive response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLeafNodeSendsAccountingEvents(t *testing.T) {
|
||||
s, opts, conf := runLeafNodeOperatorServer(t)
|
||||
defer os.Remove(conf)
|
||||
defer s.Shutdown()
|
||||
|
||||
// System account
|
||||
acc, akp := createAccount(t, s)
|
||||
if err := s.SetSystemAccount(acc.Name); err != nil {
|
||||
t.Fatalf("Expected this succeed, got %v", err)
|
||||
}
|
||||
|
||||
// Leafnode Account
|
||||
lacc, lakp := createAccount(t, s)
|
||||
|
||||
// Create a system account user and connect a client to listen for the events.
|
||||
url := fmt.Sprintf("nats://%s:%d", opts.Host, opts.Port)
|
||||
nc, err := nats.Connect(url, createUserCreds(t, s, akp))
|
||||
if err != nil {
|
||||
t.Fatalf("Error on connect: %v", err)
|
||||
}
|
||||
defer nc.Close()
|
||||
|
||||
// Watch only for our leaf node account.
|
||||
cSub, _ := nc.SubscribeSync(fmt.Sprintf("$SYS.ACCOUNT.%s.CONNECT", lacc.Name))
|
||||
dSub, _ := nc.SubscribeSync(fmt.Sprintf("$SYS.ACCOUNT.%s.DISCONNECT", lacc.Name))
|
||||
nc.Flush()
|
||||
|
||||
// Now create creds for the leafnode and connect the server.
|
||||
kp, _ := nkeys.CreateUser()
|
||||
pub, _ := kp.PublicKey()
|
||||
nuc := jwt.NewUserClaims(pub)
|
||||
ujwt, err := nuc.Encode(lakp)
|
||||
if err != nil {
|
||||
t.Fatalf("Error generating user JWT: %v", err)
|
||||
}
|
||||
seed, _ := kp.Seed()
|
||||
mycreds := genCredsFile(t, ujwt, seed)
|
||||
defer os.Remove(mycreds)
|
||||
|
||||
sl, _, lnconf := runSolicitWithCredentials(t, opts, mycreds)
|
||||
defer os.Remove(lnconf)
|
||||
defer sl.Shutdown()
|
||||
|
||||
// Wait for connect event
|
||||
msg, err := cSub.NextMsg(time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("Error waiting for account connect event: %v", err)
|
||||
}
|
||||
m := server.ConnectEventMsg{}
|
||||
if err := json.Unmarshal(msg.Data, &m); err != nil {
|
||||
t.Fatal("Did not get correctly formatted event")
|
||||
}
|
||||
|
||||
// Shutdown leafnode to generate disconnect event.
|
||||
sl.Shutdown()
|
||||
|
||||
msg, err = dSub.NextMsg(time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("Error waiting for account disconnect event: %v", err)
|
||||
}
|
||||
dm := server.DisconnectEventMsg{}
|
||||
if err := json.Unmarshal(msg.Data, &dm); err != nil {
|
||||
t.Fatal("Did not get correctly formatted event")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user