Skip to content

Commit

Permalink
[Added] support for connection types in user jwt
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Hanel <mh@synadia.com>
  • Loading branch information
matthiashanel committed Dec 7, 2020
1 parent d12f4b7 commit 6a90bb2
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
5 changes: 5 additions & 0 deletions cmd/describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ func (u *UserDescriber) Describe() string {
AddListValues(table, "Tags", u.Tags)
}

if len(u.Tags) > 0 {
table.AddSeparator()
AddListValues(table, "Allowed Connection Types", u.AllowedConnectionTypes)
}

return table.Render()
}

Expand Down
50 changes: 42 additions & 8 deletions cmd/edituser.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ nsc edit user --name <n> --rm-response-perms
cmd.Flags().Int64VarP(&params.payload.Number, "payload", "", -1, "set maximum message payload in bytes for the account (-1 is unlimited)")

cmd.Flags().StringVarP(&params.name, "name", "n", "", "user name")
cmd.Flags().StringSliceVarP(&params.connTypes, "conn-type", "", nil,
fmt.Sprintf("add connection types: %s %s %s %s - comma separated list or option can be specified multiple times",
jwt.ConnectionTypeLeafnode, jwt.ConnectionTypeMqtt, jwt.ConnectionTypeStandard, jwt.ConnectionTypeWebsocket))
cmd.Flags().StringSliceVarP(&params.rmConnTypes, "rm-conn-type", "", nil, "remove connection types - comma separated list or option can be specified multiple times")

cmd.Flags().BoolVarP(&params.bearer, "bearer", "", false, "no connect challenge required for user")

Expand All @@ -104,12 +108,14 @@ type EditUserParams struct {
token string
credsFilePath string

rmSrc []string
src []string
times timeSlice
rmTimes []string
payload DataParams
bearer bool
rmSrc []string
src []string
times timeSlice
rmTimes []string
payload DataParams
bearer bool
connTypes []string
rmConnTypes []string
}

func (p *EditUserParams) SetDefaults(ctx ActionCtx) error {
Expand All @@ -119,7 +125,7 @@ func (p *EditUserParams) SetDefaults(ctx ActionCtx) error {

if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "rm", "allow-pub", "allow-sub", "allow-pubsub",
"deny-pub", "deny-sub", "deny-pubsub", "tag", "rm-tag", "source-network", "rm-source-network", "payload",
"rm-response-perms", "max-responses", "response-ttl", "allow-pub-response", "bearer", "rm-time", "time") {
"rm-response-perms", "max-responses", "response-ttl", "allow-pub-response", "bearer", "rm-time", "time", "conn-type", "rm-conn-type") {
ctx.CurrentCmd().SilenceUsage = false
return fmt.Errorf("specify an edit option")
}
Expand Down Expand Up @@ -205,6 +211,22 @@ func (p *EditUserParams) PostInteractive(ctx ActionCtx) error {
func (p *EditUserParams) Validate(ctx ActionCtx) error {
var err error

connTypes := make([]string, len(p.connTypes))
for i, k := range p.connTypes {
u := strings.ToUpper(k)
switch u {
case jwt.ConnectionTypeLeafnode, jwt.ConnectionTypeMqtt, jwt.ConnectionTypeStandard, jwt.ConnectionTypeWebsocket:
default:
return fmt.Errorf("unknown connection type %s", k)
}
connTypes[i] = u
}
rmConnTypes := make([]string, len(p.rmConnTypes))
for i, k := range p.rmConnTypes {
rmConnTypes[i] = strings.ToUpper(k)
}
p.rmConnTypes = rmConnTypes

_, err = p.payload.NumberValue()
if err != nil {
return fmt.Errorf("error parsing %s: %s", "payload", p.payload.Value)
Expand Down Expand Up @@ -244,10 +266,22 @@ func (p *EditUserParams) Run(ctx ActionCtx) (store.Status, error) {
r.AddOK("changed bearer to %t", p.bearer)
}

var connTypes jwt.StringList
connTypes.Add(p.claim.AllowedConnectionTypes...)
connTypes.Add(p.connTypes...)
for _, v := range p.connTypes {
r.AddOK("added connection type %s", v)
}
connTypes.Remove(p.rmConnTypes...)
for _, v := range p.rmConnTypes {
r.AddOK("removed connection type %s", v)
}
p.claim.AllowedConnectionTypes = connTypes

var srcList jwt.CIDRList
srcList.Add(p.claim.Src...)
srcList.Add(p.src...)
for _, v := range p.claim.Src {
for _, v := range p.src {
r.AddOK("added src network %s", v)
}
srcList.Remove(p.rmSrc...)
Expand Down
1 change: 1 addition & 0 deletions cmd/edituser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func Test_EditUser(t *testing.T) {
tests := CmdTests{
{createEditUserCmd(), []string{"edit", "user"}, nil, []string{"specify an edit option"}, true},
{createEditUserCmd(), []string{"edit", "user", "--tag", "A", "--account", "A"}, nil, []string{"edited user \"a\""}, false},
{createEditUserCmd(), []string{"edit", "user", "--conn-type", "MQTT", "--rm-conn-type", "LEAFNODE", "--account", "A"}, nil, []string{"added connection type MQTT", "added connection type MQTT"}, false},
{createEditUserCmd(), []string{"edit", "user", "--tag", "B", "--account", "B"}, nil, []string{"user name is required"}, true},
{createEditUserCmd(), []string{"edit", "user", "--tag", "B", "--account", "B", "--name", "bb"}, nil, []string{"edited user \"bb\""}, false},
}
Expand Down

0 comments on commit 6a90bb2

Please sign in to comment.