Skip to content

Commit

Permalink
Merge pull request #1893 from tirthct/ocm-6926-fix-mandatory-env
Browse files Browse the repository at this point in the history
OCM-6926 | fix: Added back the condition to check for empty env flag
  • Loading branch information
openshift-merge-bot[bot] authored Apr 1, 2024
2 parents 136ab0c + 4ee89c6 commit c9f520d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
10 changes: 6 additions & 4 deletions cmd/login/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ const oauthClientId = "ocm-cli"

var reAttempt bool

var env string

var args struct {
tokenURL string
clientID string
Expand Down Expand Up @@ -183,7 +185,7 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
}

// Check mandatory options:
env := args.env
env = args.env

// Fail fast if config is keyring managed and invalid
if keyring, ok := config.IsKeyringManaged(); ok {
Expand Down Expand Up @@ -256,7 +258,7 @@ func runWithRuntime(r *rosa.Runtime, cmd *cobra.Command, argv []string) error {
token := args.token

// Determine if we should be using the FedRAMP environment:
err = CheckAndLogIntoFedramp(fedramp.HasFlag(cmd), fedramp.HasAdminFlag(cmd), cfg, token, env, r)
err = CheckAndLogIntoFedramp(fedramp.HasFlag(cmd), fedramp.HasAdminFlag(cmd), cfg, token, r)
if err != nil {
r.Reporter.Errorf("%s", err.Error())
os.Exit(1)
Expand Down Expand Up @@ -543,7 +545,7 @@ func Call(cmd *cobra.Command, argv []string, reporter *rprtr.Object) error {
return nil
}

func CheckAndLogIntoFedramp(hasFlag, hasAdminFlag bool, cfg *config.Config, token string, env string,
func CheckAndLogIntoFedramp(hasFlag, hasAdminFlag bool, cfg *config.Config, token string,
runtime *rosa.Runtime) error {
if hasFlag ||
(cfg.FedRAMP && token == "") ||
Expand All @@ -557,7 +559,7 @@ func CheckAndLogIntoFedramp(hasFlag, hasAdminFlag bool, cfg *config.Config, toke

fedramp.Enable()
// Always default to prod
if env == sdk.DefaultURL {
if env == sdk.DefaultURL || env == "" {
env = "production"
}
if hasAdminFlag {
Expand Down
24 changes: 20 additions & 4 deletions cmd/login/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var _ = Describe("Validate login command", func() {

Context("login command", func() {
When("logging into FedRAMP", func() {
env = "staging"
It("only 'region' is FedRAMP", func() {
os.Setenv("AWS_REGION", "us-gov-west-1")
// Load the configuration file:
Expand All @@ -53,7 +54,8 @@ var _ = Describe("Validate login command", func() {
if cfg == nil {
cfg = new(config.Config)
}
err = CheckAndLogIntoFedramp(false, false, cfg, "", "staging", rosa.NewRuntime())
env = "staging"
err = CheckAndLogIntoFedramp(false, false, cfg, "", rosa.NewRuntime())
Expect(err).ToNot(HaveOccurred())
})
It("only 'govcloud' flag is true", func() {
Expand All @@ -64,7 +66,7 @@ var _ = Describe("Validate login command", func() {
if cfg == nil {
cfg = new(config.Config)
}
err = CheckAndLogIntoFedramp(true, false, cfg, "", "staging", rosa.NewRuntime())
err = CheckAndLogIntoFedramp(true, false, cfg, "", rosa.NewRuntime())
Expect(err).To(HaveOccurred())
})
It("only 'cfg' has FedRAMP", func() {
Expand All @@ -76,7 +78,7 @@ var _ = Describe("Validate login command", func() {
cfg = new(config.Config)
}
cfg.FedRAMP = true
err = CheckAndLogIntoFedramp(false, false, cfg, "", "staging", rosa.NewRuntime())
err = CheckAndLogIntoFedramp(false, false, cfg, "", rosa.NewRuntime())
Expect(err).To(HaveOccurred())
})
It("'cfg' has FedRAMP and region is govcloud", func() {
Expand All @@ -88,9 +90,23 @@ var _ = Describe("Validate login command", func() {
cfg = new(config.Config)
}
cfg.FedRAMP = true
err = CheckAndLogIntoFedramp(false, false, cfg, "", "staging", rosa.NewRuntime())
err = CheckAndLogIntoFedramp(false, false, cfg, "", rosa.NewRuntime())
Expect(err).ToNot(HaveOccurred())
})
It("env is empty", func() {
os.Setenv("AWS_REGION", "us-gov-east-1")
// Load the configuration file:
cfg, err := config.Load()
Expect(err).ToNot(HaveOccurred())
if cfg == nil {
cfg = new(config.Config)
}
env = ""
cfg.FedRAMP = true
err = CheckAndLogIntoFedramp(false, false, cfg, "", rosa.NewRuntime())
Expect(err).ToNot(HaveOccurred())
Expect(env).To(Equal("production"))
})
})
})
})
Expand Down

0 comments on commit c9f520d

Please sign in to comment.