Skip to content

Commit d36a95e

Browse files
authored
CLOUDP-61711: Validate API keys presence (#142)
1 parent 2c3d49a commit d36a95e

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

internal/cli/atlas.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ package cli
1616

1717
import (
1818
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/mongodb/mongocli/internal/validate"
1920
"github.com/spf13/cobra"
2021
)
2122

2223
func AtlasBuilder() *cobra.Command {
2324
cmd := &cobra.Command{
2425
Use: "atlas",
2526
Short: description.Atlas,
27+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
28+
return validate.Credentials()
29+
},
2630
}
2731
cmd.AddCommand(AtlasClustersBuilder())
2832
cmd.AddCommand(AtlasDBUsersBuilder())

internal/cli/cloud_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cli
1616

1717
import (
1818
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/mongodb/mongocli/internal/validate"
1920
"github.com/spf13/cobra"
2021
)
2122

@@ -24,6 +25,9 @@ func CloudManagerBuilder() *cobra.Command {
2425
Use: "cloud-manager",
2526
Aliases: []string{"cm"},
2627
Short: description.CloudManager,
28+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
29+
return validate.Credentials()
30+
},
2731
}
2832

2933
cmd.AddCommand(OpsManagerClustersBuilder())

internal/cli/iam.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ package cli
1616

1717
import (
1818
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/mongodb/mongocli/internal/validate"
1920
"github.com/spf13/cobra"
2021
)
2122

2223
func IAMBuilder() *cobra.Command {
2324
cmd := &cobra.Command{
24-
Use: "iam",
25+
Use: "iam",
26+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
27+
return validate.Credentials()
28+
},
2529
Short: description.IAM,
2630
}
2731
cmd.AddCommand(IAMProjectsBuilder())

internal/cli/ops_manager.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package cli
1616

1717
import (
1818
"github.com/mongodb/mongocli/internal/description"
19+
"github.com/mongodb/mongocli/internal/validate"
1920
"github.com/spf13/cobra"
2021
)
2122

@@ -24,6 +25,9 @@ func OpsManagerBuilder() *cobra.Command {
2425
Use: "ops-manager",
2526
Aliases: []string{"om"},
2627
Short: description.OpsManager,
28+
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
29+
return validate.Credentials()
30+
},
2731
}
2832

2933
cmd.AddCommand(OpsManagerClustersBuilder())

internal/validate/validate.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ package validate
1616

1717
import (
1818
"encoding/hex"
19+
"errors"
1920
"fmt"
2021
"net/url"
2122
"strings"
23+
24+
"github.com/mongodb/mongocli/internal/config"
2225
)
2326

2427
func URL(val interface{}) error {
@@ -45,3 +48,10 @@ func ObjectID(s string) error {
4548
}
4649
return nil
4750
}
51+
52+
func Credentials() error {
53+
if config.PrivateAPIKey() == "" || config.PublicAPIKey() == "" {
54+
return errors.New("missing credentials")
55+
}
56+
return nil
57+
}

internal/validate/validate_test.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414

1515
package validate
1616

17-
import "testing"
17+
import (
18+
"os"
19+
"testing"
20+
21+
"github.com/spf13/viper"
22+
)
1823

1924
func TestURL(t *testing.T) {
2025
t.Run("Valid URL", func(t *testing.T) {
@@ -57,3 +62,24 @@ func TestObjectID(t *testing.T) {
5762
}
5863
})
5964
}
65+
66+
func TestCredentials(t *testing.T) {
67+
t.Run("no credentials", func(t *testing.T) {
68+
err := Credentials()
69+
if err == nil {
70+
t.Fatal("Credentials() expected an error\n")
71+
}
72+
})
73+
t.Run("with credentials", func(t *testing.T) {
74+
// this function depends on the global config (globals are bad I know)
75+
// the easiest way we have to test it is via ENV vars
76+
viper.AutomaticEnv()
77+
_ = os.Setenv("PUBLIC_API_KEY", "test")
78+
_ = os.Setenv("PRIVATE_API_KEY", "test")
79+
80+
err := Credentials()
81+
if err != nil {
82+
t.Fatalf("Credentials() unexpected error %v\n", err)
83+
}
84+
})
85+
}

0 commit comments

Comments
 (0)