From d27b952133b57f4ffc16307e04eec6e5191ec1ec Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Tue, 30 Apr 2019 11:44:25 -0500 Subject: [PATCH] Added flag to edit leaf node connections (#77) Fixed Operator describer to display standard JWT claims, including issuer. --- cmd/describer.go | 16 ++++++++++++---- cmd/editaccount.go | 13 ++++++++++++- cmd/editaccount_test.go | 5 +++-- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cmd/describer.go b/cmd/describer.go index 375c317b..4953f350 100644 --- a/cmd/describer.go +++ b/cmd/describer.go @@ -62,6 +62,14 @@ func (a *AccountDescriber) Describe() string { table.AddRow("Max Connections", "Unlimited") } + if lim.LeafNodeConn == 0 { + table.AddRow("Max Leaf Node Connections", "Not Allowed") + } else if lim.LeafNodeConn > 0 { + table.AddRow("Max Leaf Node Connections", fmt.Sprintf("%d", lim.LeafNodeConn)) + } else { + table.AddRow("Max Leaf Node Connections", "Unlimited") + } + if lim.Data > -1 { table.AddRow("Max Data", fmt.Sprintf("%s (%d bytes)", humanize.Bytes(uint64(lim.Data)), lim.Data)) } else { @@ -246,6 +254,9 @@ func AddStandardClaimInfo(table *tablewriter.Table, claims jwt.Claims) { issuer = uc.IssuerAccount } } + if _, ok := claims.(*jwt.OperatorClaims); ok { + label = "Operator ID" + } cd := claims.Claims() if cd.Name != "" { @@ -436,10 +447,7 @@ func (o *OperatorDescriber) Describe() string { table := tablewriter.CreateTable() table.UTF8Box() table.AddTitle("Operator Details") - table.AddRow("Name", o.Name) - table.AddRow("Operator ID", ShortCodes(o.Subject)) - table.AddRow("Issued", RenderDate(o.IssuedAt)) - table.AddRow("Expires", RenderDate(o.Expires)) + AddStandardClaimInfo(table, &o.OperatorClaims) if len(o.Identities) > 0 { table.AddSeparator() diff --git a/cmd/editaccount.go b/cmd/editaccount.go index 4d454942..70ca85ef 100644 --- a/cmd/editaccount.go +++ b/cmd/editaccount.go @@ -64,6 +64,7 @@ func createEditAccount() *cobra.Command { cmd.Flags().StringSliceVarP(¶ms.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times") cmd.Flags().StringSliceVarP(¶ms.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times") cmd.Flags().Int64VarP(¶ms.conns.NumberValue, "conns", "", -1, "set maximum active connections for the account (-1 is unlimited)") + cmd.Flags().Int64VarP(¶ms.leafConns.NumberValue, "leaf-conns", "", 0, "set maximum active leaf node connections for the account (-1 is unlimited)") cmd.Flags().StringVarP(¶ms.data.Value, "data", "", "-1", "set maximum data in bytes for the account (-1 is unlimited)") cmd.Flags().Int64VarP(¶ms.exports.NumberValue, "exports", "", -1, "set maximum number of exports for the account (-1 is unlimited)") cmd.Flags().Int64VarP(¶ms.imports.NumberValue, "imports", "", -1, "set maximum number of imports for the account (-1 is unlimited)") @@ -90,6 +91,7 @@ type EditAccountParams struct { claim *jwt.AccountClaims token string conns NumberParams + leafConns NumberParams exports NumberParams exportsWc bool imports NumberParams @@ -106,7 +108,7 @@ func (p *EditAccountParams) SetDefaults(ctx ActionCtx) error { } p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx) - if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag", "conns", "exports", "imports", "subscriptions", "payload", "data", "wildcard-exports", "sk", "rm-sk") { + if !InteractiveFlag && ctx.NothingToDo("start", "expiry", "tag", "rm-tag", "conns", "leaf-conns", "exports", "imports", "subscriptions", "payload", "data", "wildcard-exports", "sk", "rm-sk") { ctx.CurrentCmd().SilenceUsage = false return fmt.Errorf("specify an edit option") } @@ -137,6 +139,10 @@ func (p *EditAccountParams) Load(ctx ActionCtx) error { p.conns.NumberValue = p.claim.Limits.Conn } + if !ctx.CurrentCmd().Flags().Changed("leaf-conns") { + p.leafConns.NumberValue = p.claim.Limits.LeafNodeConn + } + if !ctx.CurrentCmd().Flags().Changed("data") { p.data.Value = fmt.Sprintf("%d", p.claim.Limits.Data) } @@ -170,6 +176,10 @@ func (p *EditAccountParams) PostInteractive(ctx ActionCtx) error { return err } + if err = p.leafConns.Edit("max leaf node connections (-1 unlimited)"); err != nil { + return err + } + if err = p.data.Edit("max data (-1 unlimited)"); err != nil { return err } @@ -227,6 +237,7 @@ func (p *EditAccountParams) Run(ctx ActionCtx) error { } p.claim.Limits.Conn = p.conns.NumberValue + p.claim.Limits.LeafNodeConn = p.leafConns.NumberValue p.claim.Limits.Data, err = p.data.NumberValue() if err != nil { return fmt.Errorf("error parsing %s: %s", "data", p.data.Value) diff --git a/cmd/editaccount_test.go b/cmd/editaccount_test.go index 53c964eb..9a09a0f4 100644 --- a/cmd/editaccount_test.go +++ b/cmd/editaccount_test.go @@ -113,13 +113,14 @@ func Test_EditAccountLimits(t *testing.T) { ts.AddAccount(t, "A") _, _, err := ExecuteCmd(createEditAccount(), "--conns", "5", "--data", "10M", "--exports", "15", - "--imports", "20", "--payload", "1K", "--subscriptions", "30") + "--imports", "20", "--payload", "1K", "--subscriptions", "30", "--leaf-conns", "31") require.NoError(t, err) ac, err := ts.Store.ReadAccountClaim("A") require.NoError(t, err) require.NotNil(t, ac) require.Equal(t, int64(5), ac.Limits.Conn) + require.Equal(t, int64(31), ac.Limits.LeafNodeConn) require.Equal(t, int64(1000*1000*10), ac.Limits.Data) require.Equal(t, int64(15), ac.Limits.Exports) require.Equal(t, int64(20), ac.Limits.Imports) @@ -127,7 +128,7 @@ func Test_EditAccountLimits(t *testing.T) { require.Equal(t, int64(30), ac.Limits.Subs) } -func Test_EditACcountSigningKeys(t *testing.T) { +func Test_EditAccountSigningKeys(t *testing.T) { ts := NewTestStore(t, "edit account") defer ts.Done(t)