Skip to content

CLOUDP-61290: Validate base url #124

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

Merged
merged 6 commits into from
Apr 24, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ addlicense:
.PHONY: gen-mocks
gen-mocks: ## Generate mocks
@echo "==> Generating mocks"
mockgen -source=internal/config/profile.go -destination=internal/mocks/mock_profile.go -package=mocks
mockgen -source=internal/store/alert_configuration.go -destination=internal/mocks/mock_alert_configuration.go -package=mocks
mockgen -source=internal/store/automation.go -destination=internal/mocks/mock_automation.go -package=mocks
mockgen -source=internal/store/clusters.go -destination=internal/mocks/mock_clusters.go -package=mocks
Expand Down
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
var (
rootCmd = &cobra.Command{
Version: version.Version,
Use: config.Name,
Use: config.ToolName,
Short: "CLI tool to manage your MongoDB Cloud",
Long: fmt.Sprintf("Use %s command help for information on a specific command", config.Name),
Long: fmt.Sprintf("Use %s command help for information on a specific command", config.ToolName),
}
)

Expand Down
12 changes: 2 additions & 10 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package cli

import (
"encoding/hex"
"errors"
"fmt"
"strconv"
Expand All @@ -25,6 +24,7 @@ import (
atlas "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/prompts"
"github.com/mongodb/mongocli/internal/validate"
)

const (
Expand All @@ -37,14 +37,6 @@ type globalOpts struct {
projectID string
}

func validateObjectID(s string) error {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

moved to its own package

b, err := hex.DecodeString(s)
if err != nil || len(b) != 12 {
return fmt.Errorf("the provided value '%s' is not a valid ObjectID", s)
}
return nil
}

func deploymentStatus(baseURL, projectID string) string {
return fmt.Sprintf("Changes are being applied, please check %s/v2/%s#deployment/topology for status\n", baseURL, projectID)
}
Expand All @@ -67,7 +59,7 @@ func (opts *globalOpts) PreRunE(cbs ...cmdOpt) error {
if opts.ProjectID() == "" {
return errMissingProjectID
}
if err := validateObjectID(opts.ProjectID()); err != nil {
if err := validate.ObjectID(opts.ProjectID()); err != nil {
return err
}
for _, f := range cbs {
Expand Down
11 changes: 6 additions & 5 deletions internal/cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/flags"
"github.com/mongodb/mongocli/internal/usage"
"github.com/mongodb/mongocli/internal/validators"
"github.com/mongodb/mongocli/internal/validate"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -66,7 +66,7 @@ func (opts *configOpts) Run() error {
helpLink = "https://docs.opsmanager.mongodb.com/current/tutorial/configure-public-api-access/"
}

var defaultQuestions = []*survey.Question{
defaultQuestions := []*survey.Question{
{
Name: "publicAPIKey",
Prompt: &survey.Input{
Expand All @@ -89,11 +89,11 @@ func (opts *configOpts) Run() error {
{
Name: "opsManagerURL",
Prompt: &survey.Input{
Message: "Ops Manager Base URL:",
Message: "URL to Access Ops Manage:",
Default: config.OpsManagerURL(),
Help: "Ops Manager host URL",
Help: "https://docs.opsmanager.mongodb.com/current/reference/config/ui-settings/#URL-to-Access-Ops-Manager",
},
Validate: validators.ValidURL,
Validate: validate.URL,
},
}
defaultQuestions = append(opsManagerQuestions, defaultQuestions...)
Expand All @@ -112,6 +112,7 @@ func ConfigBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "config",
Short: description.ConfigDescription,
Long: description.ConfigLongDescription,
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
Expand Down
33 changes: 24 additions & 9 deletions internal/cli/config_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,54 @@ package cli

import (
"fmt"
"strings"

"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/description"
"github.com/mongodb/mongocli/internal/search"
"github.com/mongodb/mongocli/internal/validate"
"github.com/spf13/cobra"
)

type configSetOpts struct {
globalOpts
prop string
val string
prop string
val string
store config.ProfileSetSaver
}

func (opts *configSetOpts) Run() error {
config.Set(opts.prop, opts.val)
if err := config.Save(); err != nil {
if strings.HasSuffix(opts.prop, "_url") {
if err := validate.URL(opts.val); err != nil {
return err
}
} else if strings.HasSuffix(opts.prop, "_id") {
if err := validate.ObjectID(opts.val); err != nil {
return err
}
}
opts.store.Set(opts.prop, opts.val)
if err := opts.store.Save(); err != nil {
return err
}
fmt.Printf("Updated prop '%s'\n", opts.prop)
fmt.Printf("Updated property '%s'\n", opts.prop)
return nil
}

func ConfigSetBuilder() *cobra.Command {
opts := &configSetOpts{}
opts := &configSetOpts{
store: config.Config(),
}
cmd := &cobra.Command{
Use: "set [prop] [val]",
Short: description.SetConfig,
Use: "set [property] [value]",
Short: description.ConfigSetDescription,
Long: fmt.Sprintf(description.ConfigSetLongDescription, config.Properties()),
Args: func(cmd *cobra.Command, args []string) error {
if len(args) != 2 {
return fmt.Errorf("accepts %d arg(s), received %d", 2, len(args))
}
if !search.StringInSlice(cmd.ValidArgs, args[0]) {
return fmt.Errorf("invalid prop %q", args[0])
return fmt.Errorf("invalid property: %q", args[0])
}
return nil
},
Expand Down
188 changes: 188 additions & 0 deletions internal/cli/config_set_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cli

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/mocks"
)

func TestConfigSet_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockProfileSetSaver(ctrl)
defer ctrl.Finish()

t.Run("valid prop", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "test",
val: "something",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(1)

mockStore.
EXPECT().
Save().Return(nil).
Times(1)

err := setOpts.Run()

if err != nil {
t.Fatalf("Run() unexpected error: %v\n", err)
}
})

t.Run("valid base_url", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "base_url",
val: "http://test:9080/",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(1)

mockStore.
EXPECT().
Save().Return(nil).
Times(1)

err := setOpts.Run()

if err != nil {
t.Fatalf("Run() unexpected error: %v\n", err)
}
})

t.Run("invalid base_url", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "base_url",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(0)

mockStore.
EXPECT().
Save().Return(nil).
Times(0)

err := setOpts.Run()

if err == nil {
t.Fatal("Run() expected an error but got none\n")
}
})

t.Run("valid org_id", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "org_id",
val: "5e9f088b4797476aa0a5d56a",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(1)

mockStore.
EXPECT().
Save().Return(nil).
Times(1)

err := setOpts.Run()

if err != nil {
t.Fatalf("Run() unexpected error: %v\n", err)
}
})

t.Run("invalid org_id", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "org_id",
val: "1",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(0)

mockStore.
EXPECT().
Save().Return(nil).
Times(0)

err := setOpts.Run()

if err == nil {
t.Fatal("Run() expected an error but got none\n")
}
})

t.Run("valid project_id", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "project_id",
val: "5e9f088b4797476aa0a5d56a",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(1)

mockStore.
EXPECT().
Save().Return(nil).
Times(1)

err := setOpts.Run()

if err != nil {
t.Fatalf("Run() unexpected error: %v\n", err)
}
})

t.Run("invalid project_id", func(t *testing.T) {
setOpts := &configSetOpts{
store: mockStore,
prop: "project_id",
val: "1",
}
mockStore.
EXPECT().
Set(setOpts.prop, setOpts.val).
Times(0)

mockStore.
EXPECT().
Save().Return(nil).
Times(0)

err := setOpts.Run()

if err == nil {
t.Fatal("Run() expected an error but got none\n")
}
})
}
2 changes: 1 addition & 1 deletion internal/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package config

const (
Name = "mongocli" // Name of the CLI
ToolName = "mongocli" // ToolName of the CLI
EnvPrefix = "mcli" // Prefix for ENV variables
DefaultProfile = "default" // DefaultProfile default
CloudService = "cloud" // CloudService setting when using Atlas API
Expand Down
Loading