Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-237474: AtlasCLI fails to support collection names with dots for custom roles #2767

Merged
merged 2 commits into from Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions internal/cli/atlas/customdbroles/create.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/convert/custom_db_role.go
Expand Up @@ -56,7 +56,7 @@ func BuildAtlasActions(a []string) []atlasv2.DatabasePrivilegeAction {
resource := strings.Split(action[1], resourceSep)
resourceStruct.Db = resource[0]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this works if db names don't have dots, which is indeed a MongoDB restriction https://www.mongodb.com/docs/manual/reference/limits/#naming-restrictions

I'm going to update the task and PR titles (support collection names with dots)

if len(resource) > 1 {
resourceStruct.Collection = resource[1]
resourceStruct.Collection = strings.Join(resource[1:], resourceSep)
}
} else {
resourceStruct.Cluster = true
Expand Down
64 changes: 36 additions & 28 deletions internal/convert/custom_db_role_test.go
Expand Up @@ -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{
Expand Down Expand Up @@ -69,84 +67,94 @@ 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,
},
},
},
{
Action: "name",
Resources: &[]atlasv2.DatabasePermittedNamespaceResource{
{
Db: datalake,
Db: "DATA_LAKE",
},
},
},
},
},
}

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)
}
})
}
Expand Down