Skip to content

Commit

Permalink
Merge pull request #644 from ripienaar/render_userinfo
Browse files Browse the repository at this point in the history
support 2.10.x user info structs
  • Loading branch information
ripienaar authored Dec 2, 2022
2 parents ed146e9 + c661b14 commit 3d1dad1
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
70 changes: 70 additions & 0 deletions cli/account_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/choria-io/fisk"
"github.com/dustin/go-humanize"
Expand Down Expand Up @@ -387,8 +388,41 @@ func (c *actCmd) infoAction(_ *fisk.ParseContext) error {
rtt, _ := nc.RTT()
tlsc, _ := nc.TLSConnectionState()

var ui *server.UserInfo
if serverMinVersion(nc.ConnectedServerVersion(), 2, 10, 0) {
subj := "$SYS.REQ.USER.INFO"
if opts.Trace {
log.Printf(">>> %s: {}\n", subj)
}
resp, err := nc.Request("$SYS.REQ.USER.INFO", nil, time.Second)
if err == nil {
if opts.Trace {
log.Printf("<<< %s", string(resp.Data))
}
var res = struct {
Data *server.UserInfo `json:"data"`
Server server.ServerInfo `json:"server"`
Error *server.ApiError `json:"error"`
}{}

err = json.Unmarshal(resp.Data, &res)
if err == nil && res.Error == nil {
ui = res.Data
}
}
}

fmt.Println("Connection Information:")
fmt.Println()
if ui != nil {
fmt.Printf(" User: %v\n", ui.UserID)
fmt.Printf(" Account: %v\n", ui.Account)
if ui.Expires == 0 {
fmt.Printf(" Expires: never\n")
} else {
fmt.Printf(" Expires: %s\n", humanizeDuration(ui.Expires))
}
}
fmt.Printf(" Client ID: %v\n", id)
fmt.Printf(" Client IP: %v\n", ip)
fmt.Printf(" RTT: %v\n", rtt)
Expand All @@ -403,6 +437,7 @@ func (c *actCmd) infoAction(_ *fisk.ParseContext) error {
if nc.ConnectedServerId() != nc.ConnectedServerName() {
fmt.Printf(" Connected Server Name: %v\n", nc.ConnectedServerName())
}

if tlsc.HandshakeComplete {
version := ""
switch tlsc.Version {
Expand Down Expand Up @@ -430,6 +465,41 @@ func (c *actCmd) infoAction(_ *fisk.ParseContext) error {
}
fmt.Println()

renderPerm := func(title string, p *server.SubjectPermission) {
if p == nil {
return
}
if len(p.Deny) == 0 && len(p.Allow) == 0 {
return
}

fmt.Printf(" %s\n", title)
if len(p.Allow) > 0 {
fmt.Println(" Allow:")
sort.Strings(p.Allow)
for _, perm := range p.Allow {
fmt.Printf(" %s\n", perm)
}
}
if len(p.Deny) > 0 {
fmt.Println()
fmt.Println(" Deny:")
sort.Strings(p.Deny)
for _, perm := range p.Deny {
fmt.Printf(" %s\n", perm)
}
}
}

if ui != nil && ui.Permissions != nil {
fmt.Println("Connection Permissions:")
fmt.Println()
renderPerm("Publish", ui.Permissions.Publish)
fmt.Println()
renderPerm("Subscribe", ui.Permissions.Subscribe)
fmt.Println()
}

info, err := mgr.JetStreamAccountInfo()

fmt.Println("JetStream Account Information:")
Expand Down
31 changes: 31 additions & 0 deletions cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,3 +1158,34 @@ func sliceGroups(input []string, size int, fn func(group []string)) {
fn(chunk)
}
}

var semVerRe = regexp.MustCompile(`\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?`)

func versionComponents(version string) (major, minor, patch int, err error) {
m := semVerRe.FindStringSubmatch(version)
if m == nil {
return 0, 0, 0, errors.New("invalid semver")
}
major, err = strconv.Atoi(m[1])
if err != nil {
return -1, -1, -1, err
}
minor, err = strconv.Atoi(m[2])
if err != nil {
return -1, -1, -1, err
}
patch, err = strconv.Atoi(m[3])
if err != nil {
return -1, -1, -1, err
}
return major, minor, patch, err
}

// Check for minimum server requirement.
func serverMinVersion(version string, major, minor, patch int) bool {
smajor, sminor, spatch, _ := versionComponents(version)
if smajor < major || (smajor == major && sminor < minor) || (smajor == major && sminor == minor && spatch < patch) {
return false
}
return true
}
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klauspost/compress v1.15.12
github.com/nats-io/jsm.go v0.0.36-0.20221202101040-65db8f7ffa60
github.com/nats-io/nats-server/v2 v2.9.8
github.com/nats-io/nats-server/v2 v2.9.9-0.20221128185543-14f40b93b51e
github.com/nats-io/nats.go v1.20.0
github.com/nats-io/nuid v1.0.1
github.com/prometheus/client_golang v1.13.1
Expand All @@ -42,6 +42,7 @@ require (
github.com/nats-io/nkeys v0.3.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
go.uber.org/automaxprocs v1.5.1 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
golang.org/x/time v0.1.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ github.com/nats-io/jwt/v2 v2.3.0 h1:z2mA1a7tIf5ShggOFlR1oBPgd6hGqcDYsISxZByUzdI=
github.com/nats-io/jwt/v2 v2.3.0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k=
github.com/nats-io/nats-server/v2 v2.9.8 h1:jgxZsv+A3Reb3MgwxaINcNq/za8xZInKhDg9Q0cGN1o=
github.com/nats-io/nats-server/v2 v2.9.8/go.mod h1:AB6hAnGZDlYfqb7CTAm66ZKMZy9DpfierY1/PbpvI2g=
github.com/nats-io/nats-server/v2 v2.9.9-0.20221128185543-14f40b93b51e h1:RuZ6tfwWVduB9eoBcyrqhGiLcYHOlHV1vlUn6igC9Es=
github.com/nats-io/nats-server/v2 v2.9.9-0.20221128185543-14f40b93b51e/go.mod h1:AB6hAnGZDlYfqb7CTAm66ZKMZy9DpfierY1/PbpvI2g=
github.com/nats-io/nats.go v1.20.0 h1:T8JJnQfVSdh1CzGiwAOv5hEobYCBho/0EupGznYw0oM=
github.com/nats-io/nats.go v1.20.0/go.mod h1:tLqubohF7t4z3du1QDPYJIQQyhb4wl6DhjxEajSI7UA=
github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8=
Expand Down Expand Up @@ -275,6 +277,8 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/automaxprocs v1.5.1 h1:e1YG66Lrk73dn4qhg8WFSvhF0JuFQF0ERIp4rpuV8Qk=
go.uber.org/automaxprocs v1.5.1/go.mod h1:BF4eumQw0P9GtnuxxovUd06vwm1o18oMzFtK66vU6XU=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down

0 comments on commit 3d1dad1

Please sign in to comment.