From 1181f261264eb863600b6dd8f99d8f36f63e2412 Mon Sep 17 00:00:00 2001 From: Gustavo Bazan Date: Wed, 13 Mar 2024 12:19:08 +0000 Subject: [PATCH] CLOUDP-237474: AtlasCLI fails to support collection names with dots for custom roles (#2767) --- internal/cli/atlas/customdbroles/create.go | 7 ++- internal/convert/custom_db_role.go | 2 +- internal/convert/custom_db_role_test.go | 64 ++++++++++++---------- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/internal/cli/atlas/customdbroles/create.go b/internal/cli/atlas/customdbroles/create.go index ca5c9f858c..17d220cf3d 100644 --- a/internal/cli/atlas/customdbroles/create.go +++ b/internal/cli/atlas/customdbroles/create.go @@ -24,7 +24,6 @@ import ( "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/config" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/convert" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag" - "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/pointer" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store" "github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage" "github.com/spf13/cobra" @@ -62,10 +61,12 @@ func (opts *CreateOpts) Run() error { } func (opts *CreateOpts) newCustomDBRole() *atlasv2.UserCustomDBRole { + actions := convert.BuildAtlasActions(opts.action) + inheritedRoles := convert.BuildAtlasInheritedRoles(opts.inheritedRoles) return &atlasv2.UserCustomDBRole{ RoleName: opts.roleName, - Actions: pointer.Get((convert.BuildAtlasActions(opts.action))), - InheritedRoles: pointer.Get(convert.BuildAtlasInheritedRoles(opts.inheritedRoles)), + Actions: &actions, + InheritedRoles: &inheritedRoles, } } diff --git a/internal/convert/custom_db_role.go b/internal/convert/custom_db_role.go index 6ae1608c44..fb38b4dee8 100644 --- a/internal/convert/custom_db_role.go +++ b/internal/convert/custom_db_role.go @@ -56,7 +56,7 @@ func BuildAtlasActions(a []string) []atlasv2.DatabasePrivilegeAction { resource := strings.Split(action[1], resourceSep) resourceStruct.Db = resource[0] if len(resource) > 1 { - resourceStruct.Collection = resource[1] + resourceStruct.Collection = strings.Join(resource[1:], resourceSep) } } else { resourceStruct.Cluster = true diff --git a/internal/convert/custom_db_role_test.go b/internal/convert/custom_db_role_test.go index 49b80409aa..6e615b341c 100644 --- a/internal/convert/custom_db_role_test.go +++ b/internal/convert/custom_db_role_test.go @@ -24,12 +24,10 @@ import ( ) func TestBuildAtlasInheritedRoles(t *testing.T) { - type test struct { + tests := []struct { input []string want []atlasv2.DatabaseInheritedRole - } - - tests := []test{ + }{ { input: []string{"admin"}, want: []atlasv2.DatabaseInheritedRole{ @@ -69,61 +67,72 @@ func TestBuildAtlasInheritedRoles(t *testing.T) { t.Run("", func(t *testing.T) { t.Parallel() got := BuildAtlasInheritedRoles(input) - if err := deep.Equal(want, got); err != nil { - t.Fatalf("expected: %v, got: %v", want, got) + if diff := deep.Equal(want, got); diff != nil { + t.Error(diff) } }) } } func TestBuildAtlasActions(t *testing.T) { - type test struct { + tests := []struct { + name string input []string want []atlasv2.DatabasePrivilegeAction - } - - cluster := true - - testdb := "testdb" - collection := "collection" - datalake := "DATA_LAKE" - - tests := []test{ + }{ { + name: "role", input: []string{"clusterName"}, want: []atlasv2.DatabasePrivilegeAction{ { Action: "clusterName", Resources: &[]atlasv2.DatabasePermittedNamespaceResource{ { - Cluster: cluster, + Cluster: true, }, }, }, }, }, { + name: "role and fqn", input: []string{"clusterName@testdb.collection"}, want: []atlasv2.DatabasePrivilegeAction{ { Action: "clusterName", Resources: &[]atlasv2.DatabasePermittedNamespaceResource{ { - Db: testdb, - Collection: collection, + Db: "testdb", + Collection: "collection", }, }, }, }, }, { + name: "role and fqn", + input: []string{"clusterName@testdb.collection.with.dots"}, + want: []atlasv2.DatabasePrivilegeAction{ + { + Action: "clusterName", + Resources: &[]atlasv2.DatabasePermittedNamespaceResource{ + { + Db: "testdb", + Collection: "collection.with.dots", + }, + }, + }, + }, + }, + { + name: "role and fqn", input: []string{"clusterName", "name@DATA_LAKE"}, want: []atlasv2.DatabasePrivilegeAction{ { Action: "clusterName", Resources: &[]atlasv2.DatabasePermittedNamespaceResource{ { - Cluster: cluster, + Cluster: true, }, }, }, @@ -131,7 +140,7 @@ func TestBuildAtlasActions(t *testing.T) { Action: "name", Resources: &[]atlasv2.DatabasePermittedNamespaceResource{ { - Db: datalake, + Db: "DATA_LAKE", }, }, }, @@ -139,14 +148,13 @@ func TestBuildAtlasActions(t *testing.T) { }, } - for _, tc := range tests { - input := tc.input - want := tc.want - t.Run("", func(t *testing.T) { + for _, tt := range tests { + tc := tt + t.Run(tc.name, func(t *testing.T) { t.Parallel() - got := BuildAtlasActions(input) - if err := deep.Equal(want, got); err != nil { - t.Fatalf("expected: %v, got: %v", want, got) + got := BuildAtlasActions(tc.input) + if diff := deep.Equal(tc.want, got); diff != nil { + t.Error(diff) } }) }