Skip to content

Commit

Permalink
[CHANGE] [BREAKING] retracts --strict-tags support (#674)
Browse files Browse the repository at this point in the history
* [CHANGE] retract case-sensitive tag changes from v2.7.0 - the --strict-tags flag has been removed.
This reverts commit 2931e19.
  • Loading branch information
aricart authored Oct 10, 2024
1 parent 26cd02f commit ce4b054
Show file tree
Hide file tree
Showing 11 changed files with 47 additions and 148 deletions.
3 changes: 2 additions & 1 deletion cmd/deleteimport.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package cmd

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -172,7 +173,7 @@ func (p *DeleteImportParams) Validate(ctx ActionCtx) error {
if p.srcAccount != "" {
m = fmt.Sprintf("%s from account %s", m, p.srcAccount)
}
return fmt.Errorf(m)
return errors.New(m)
}

if err = p.SignerParams.Resolve(ctx); err != nil {
Expand Down
1 change: 0 additions & 1 deletion cmd/editaccount.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ func createEditAccount() *cobra.Command {
return RunAction(cmd, args, params)
},
}
cmd.Flags().BoolVarP(&params.strictTags, "strict-tags", "", false, "allow tags to be case-sensitive, default false")
cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
params.conns = -1
Expand Down
41 changes: 8 additions & 33 deletions cmd/editaccount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func Test_EditAccount(t *testing.T) {
{createEditAccount(), []string{"edit", "account"}, nil, []string{"specify an edit option"}, true},
{createEditAccount(), []string{"edit", "account", "--info-url", "http://foo/bar"}, nil, []string{"changed info url to"}, false},
{createEditAccount(), []string{"edit", "account", "--description", "my account is about this"}, nil, []string{"changed description to"}, false},
{createEditAccount(), []string{"edit", "account", "--tag", "a", "--name", "A"}, nil, []string{"edited account \"A\""}, false},
{createEditAccount(), []string{"edit", "account", "--tag", "A", "--name", "A"}, nil, []string{"edited account \"A\""}, false},
}

tests.Run(t, "root", "edit")
Expand All @@ -60,7 +60,7 @@ func Test_EditAccount_Tag(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createEditAccount(), "--tag", "a,b,c")
_, _, err := ExecuteCmd(createEditAccount(), "--tag", "A,B,C")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
Expand All @@ -75,17 +75,17 @@ func Test_EditAccount_RmTag(t *testing.T) {
defer ts.Done(t)

ts.AddAccount(t, "A")
_, _, err := ExecuteCmd(createEditAccount(), "--tag", "A,B,C", "--strict-tags")
_, _, err := ExecuteCmd(createEditAccount(), "--tag", "A,B,C")
require.NoError(t, err)

_, _, err = ExecuteCmd(createEditAccount(), "--rm-tag", "A,B", "--strict-tags")
_, _, err = ExecuteCmd(createEditAccount(), "--rm-tag", "A,B")
require.NoError(t, err)

ac, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)

require.Len(t, ac.Tags, 1)
require.ElementsMatch(t, ac.Tags, []string{"C"})
require.ElementsMatch(t, ac.Tags, []string{"c"})
}

func Test_EditAccount_Times(t *testing.T) {
Expand Down Expand Up @@ -380,17 +380,17 @@ func Test_EditSysAccount(t *testing.T) {
for idx, n := range jsOptions {
flag := fmt.Sprintf("--%s", n)
if idx > 0 {
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "a", flag, "1")
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "A", flag, "1")
require.Error(t, err)
require.Contains(t, err.Error(), flag)
} else {
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "a", flag)
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "A", flag)
require.Error(t, err)
require.Contains(t, err.Error(), flag)
}
}
// defaults are removed automatically
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "a")
_, _, err = ExecuteCmd(createEditAccount(), "SYS", "--tag", "A")
require.NoError(t, err)
}

Expand Down Expand Up @@ -524,28 +524,3 @@ func Test_EnableTierNoOtherFlag(t *testing.T) {
require.Error(t, err)
require.Equal(t, "rm-js-tier is exclusive of all other js options", err.Error())
}

func TestEditAccountStrictTags(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")

_, _, err := ExecuteCmd(createEditAccount(), "--tag", "a")
require.NoError(t, err)

_, _, err = ExecuteCmd(createEditAccount(), "--rm-tag", "A")
require.Error(t, err)
require.Contains(t, err.Error(), "--rm-tag \"A\" is not lowercased")

_, _, err = ExecuteCmd(createEditAccount(), "--rm-tag", "A", "--strict-tags")
require.Error(t, err)
require.Contains(t, err.Error(), "unable to remove tag: \"A\" - not found")

_, _, err = ExecuteCmd(createEditAccount(), "--tag", "A", "--strict-tags")
require.NoError(t, err)

uc, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)
require.True(t, uc.Tags.Equals(&jwt.TagList{"A", "a"}))
}
1 change: 0 additions & 1 deletion cmd/editoperator.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ func createEditOperatorCmd() *cobra.Command {
}
params.signingKeys.BindFlags("sk", "", nkeys.PrefixByteOperator, cmd)
cmd.Flags().StringSliceVarP(&params.rmSigningKeys, "rm-sk", "", nil, "remove signing key - comma separated list or option can be specified multiple times")
cmd.Flags().BoolVarP(&params.strictTags, "strict-tags", "", false, "allow tags to be case-sensitive, default false")
cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
cmd.Flags().StringVarP(&params.asu, "account-jwt-server-url", "u", "", "set account jwt server url for nsc sync (only http/https or nats service (nats/tls/ws/wss) urls supported if updating with nsc)")
Expand Down
28 changes: 1 addition & 27 deletions cmd/editoperator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func Test_EditOperator(t *testing.T) {
{createEditOperatorCmd(), []string{"edit", "operator", "--sk"}, nil, []string{"flag needs an argument"}, true},
{createEditOperatorCmd(), []string{"edit", "operator", "--sk", "SAADOZRUTPZS6LIXS6CSSSW5GXY3DNMQMSDTVWHQNHQTIBPGNSADSMBPEU"}, nil, []string{"invalid operator signing key"}, true},
{createEditOperatorCmd(), []string{"edit", "operator", "--sk", "OBMWGGURAFWMH3AFDX65TVIH4ZYSL7UKZ3LOH2ZRWIAU7PGZ3IJNR6W5"}, nil, []string{"edited operator"}, false},
{createEditOperatorCmd(), []string{"edit", "operator", "--tag", "o", "--start", "2019-04-13", "--expiry", "2050-01-01"}, nil, []string{"edited operator"}, false},
{createEditOperatorCmd(), []string{"edit", "operator", "--tag", "O", "--start", "2019-04-13", "--expiry", "2050-01-01"}, nil, []string{"edited operator"}, false},
{createEditOperatorCmd(), []string{"edit", "operator", "--require-signing-keys"}, nil, []string{"needs to be issued with a signing key first"}, true},
}

Expand Down Expand Up @@ -420,29 +420,3 @@ func Test_CannotSetRequireSKWithoutSK(t *testing.T) {
require.False(t, oc.StrictSigningKeyUsage)
require.Empty(t, oc.SigningKeys)
}

func TestEditOperatorStrictTags(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

_, _, err := ExecuteCmd(createEditOperatorCmd(), "--tag", "A")
require.Error(t, err)

_, _, err = ExecuteCmd(createEditOperatorCmd(), "--tag", "a")
require.NoError(t, err)

_, _, err = ExecuteCmd(createEditOperatorCmd(), "--rm-tag", "A")
require.Error(t, err)
require.Contains(t, err.Error(), "--rm-tag \"A\" is not lowercased")

_, _, err = ExecuteCmd(createEditOperatorCmd(), "--rm-tag", "A", "--strict-tags")
require.Error(t, err)
require.Contains(t, err.Error(), "unable to remove tag: \"A\" - not found")

_, _, err = ExecuteCmd(createEditOperatorCmd(), "--tag", "A", "--strict-tags")
require.NoError(t, err)

oc, err := ts.Store.ReadOperatorClaim()
require.NoError(t, err)
require.True(t, oc.Tags.Equals(&jwt.TagList{"A", "a"}))
}
1 change: 0 additions & 1 deletion cmd/edituser.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ nsc edit user --name <n> --rm-response-perms
return RunAction(cmd, args, &params)
},
}
cmd.Flags().BoolVarP(&params.strictTags, "strict-tags", "", false, "allow tags to be case-sensitive, default false")
cmd.Flags().StringSliceVarP(&params.tags, "tag", "", nil, "add tags for user - comma separated list or option can be specified multiple times")
cmd.Flags().StringSliceVarP(&params.rmTags, "rm-tag", "", nil, "remove tag - comma separated list or option can be specified multiple times")
cmd.Flags().StringVarP(&params.name, "name", "n", "", "user name")
Expand Down
44 changes: 8 additions & 36 deletions cmd/edituser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
package cmd

import (
"github.com/nats-io/nsc/v2/cmd/store"
"strings"
"testing"
"time"

"github.com/nats-io/nsc/v2/cmd/store"

"github.com/nats-io/nkeys"

cli "github.com/nats-io/cliprompts/v2"
Expand All @@ -39,13 +38,12 @@ 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{"--tag \"A\" is not lowercased"}, true},
{createEditUserCmd(), []string{"edit", "user", "--tag", "a", "--account", "A"}, nil, []string{"edited user \"a\""}, false},
{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", "--conn-type", "LEAFNODE_WS", "--account", "A"}, nil, []string{"added connection type LEAFNODE_WS"}, false},
{createEditUserCmd(), []string{"edit", "user", "--conn-type", "MQTT_WS", "--account", "A"}, nil, []string{"added connection type MQTT_WS"}, 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},
{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},
}

tests.Run(t, "root", "edit")
Expand Down Expand Up @@ -109,25 +107,25 @@ func Test_EditUser_Tag(t *testing.T) {
defer ts.Done(t)

ts.AddUser(t, "A", "a")
_, _, err := ExecuteCmd(createEditUserCmd(), "--tag", "A,B,C", "--strict-tags")
_, _, err := ExecuteCmd(createEditUserCmd(), "--tag", "A,B,C")
require.NoError(t, err)

cc, err := ts.Store.ReadUserClaim("A", "a")
require.NoError(t, err)
require.NotNil(t, cc)

require.Len(t, cc.Tags, 3)
require.ElementsMatch(t, cc.Tags, []string{"A", "B", "C"})
require.ElementsMatch(t, cc.Tags, []string{"a", "b", "c"})

_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-tag", "A,B", "--strict-tags")
_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-tag", "A,B")
require.NoError(t, err)

cc, err = ts.Store.ReadUserClaim("A", "a")
require.NoError(t, err)
require.NotNil(t, cc)

require.Len(t, cc.Tags, 1)
require.ElementsMatch(t, cc.Tags, []string{"C"})
require.ElementsMatch(t, cc.Tags, []string{"c"})

}

Expand Down Expand Up @@ -597,29 +595,3 @@ func Test_EditUserConnectionDeleteCase(t *testing.T) {
require.NoError(t, err)
require.Len(t, claim.AllowedConnectionTypes, 0)
}

func TestEditUserStrictTags(t *testing.T) {
ts := NewTestStore(t, "O")
defer ts.Done(t)

ts.AddAccount(t, "A")
ts.AddUser(t, "A", "U")

_, _, err := ExecuteCmd(createEditUserCmd(), "--tag", "a")
require.NoError(t, err)

_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-tag", "A")
require.Error(t, err)
require.Contains(t, err.Error(), "--rm-tag \"A\" is not lowercased")

_, _, err = ExecuteCmd(createEditUserCmd(), "--rm-tag", "A", "--strict-tags")
require.Error(t, err)
require.Contains(t, err.Error(), "unable to remove tag: \"A\" - not found")

_, _, err = ExecuteCmd(createEditUserCmd(), "--tag", "A", "--strict-tags")
require.NoError(t, err)

uc, err := ts.Store.ReadUserClaim("A", "U")
require.NoError(t, err)
require.True(t, uc.Tags.Equals(&jwt.TagList{"A", "a"}))
}
30 changes: 5 additions & 25 deletions cmd/genericclaimparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ import (
// GenericClaimsParams - TimeParams and tags
type GenericClaimsParams struct {
TimeParams
tags []string
rmTags []string
strictTags bool
tags []string
rmTags []string
}

func (sp *GenericClaimsParams) Edit(current []string) error {
Expand Down Expand Up @@ -101,21 +100,6 @@ func (sp *GenericClaimsParams) Valid() error {
if err := sp.TimeParams.Validate(); err != nil {
return err
}
if !sp.strictTags {
for _, t := range sp.tags {
tt := strings.ToLower(t)
if t != tt {
return fmt.Errorf("--tag %q is not lowercased, specify option --strict-tags to honor non-lowercased values", t)
}
}
for _, t := range sp.rmTags {
tt := strings.ToLower(t)
if t != tt {
return fmt.Errorf("--rm-tag %q is not lowercased, specify option --strict-tags", t)
}
}
}

return nil
}

Expand Down Expand Up @@ -161,19 +145,15 @@ func (sp *GenericClaimsParams) Run(ctx ActionCtx, claim jwt.Claims, r *store.Rep
}

tags.Add(sp.tags...)
if err := tags.Remove(sp.rmTags...); err != nil {
return err
}
tags.Remove(sp.rmTags...)
sort.Strings(*tags)

if r != nil {
for _, t := range sp.tags {
t = strings.TrimSpace(t)
r.AddOK("added tag %q", t)
r.AddOK("added tag %q", strings.ToLower(t))
}
for _, t := range sp.rmTags {
t = strings.TrimSpace(t)
r.AddOK("removed tag %q", t)
r.AddOK("removed tag %q", strings.ToLower(t))
}
}
return nil
Expand Down
7 changes: 3 additions & 4 deletions cmd/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func Test_SyncOK(t *testing.T) {
ts.AddAccount(t, "A")

// edit the jwt
_, _, err = ExecuteCmd(createEditAccount(), "--tag", "A", "--strict-tags")
_, _, err = ExecuteCmd(createEditAccount(), "--tag", "A")
require.NoError(t, err)

// sync the store
Expand All @@ -50,8 +50,7 @@ func Test_SyncOK(t *testing.T) {
// verify the tag was stored
ac, err := ts.Store.ReadAccountClaim("A")
require.NoError(t, err)
require.Contains(t, ac.Tags, "A")
require.True(t, ac.Tags.Contains("A"))
require.Contains(t, ac.Tags, "a")
}

func Test_SyncNoURL(t *testing.T) {
Expand Down Expand Up @@ -121,7 +120,7 @@ func Test_SyncManualServer(t *testing.T) {
ts.AddAccount(t, "A")

// edit the jwt
_, _, err = ExecuteCmd(createEditAccount(), "--tag", "a")
_, _, err = ExecuteCmd(createEditAccount(), "--tag", "A")
require.NoError(t, err)

// sync the store
Expand Down
16 changes: 9 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module github.com/nats-io/nsc/v2

go 1.21
go 1.22

toolchain go1.22.5

require (
github.com/blang/semver v3.5.1+incompatible
Expand All @@ -9,7 +11,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/nats-io/cliprompts/v2 v2.0.0-20231014115920-801ca035562a
github.com/nats-io/jsm.go v0.1.2
github.com/nats-io/jwt/v2 v2.7.0
github.com/nats-io/jwt/v2 v2.7.2
github.com/nats-io/nats-server/v2 v2.10.18
github.com/nats-io/nats.go v1.37.0
github.com/nats-io/nkeys v0.4.7
Expand All @@ -19,12 +21,12 @@ require (
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.9.0
github.com/xlab/tablewriter v0.0.0-20160610135559-80b567a11ad5
golang.org/x/text v0.18.0
golang.org/x/text v0.19.0
)

require (
github.com/AlecAivazis/survey/v2 v2.3.7 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/google/go-github/v30 v30.1.0 // indirect
Expand All @@ -43,11 +45,11 @@ require (
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/ulikunitz/xz v0.5.12 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/oauth2 v0.23.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/term v0.24.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/term v0.25.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
Loading

0 comments on commit ce4b054

Please sign in to comment.