Skip to content

Commit

Permalink
Merge pull request #274 from cycloidio/273-fix-login
Browse files Browse the repository at this point in the history
#273 fix login logic and api_key handling.
  • Loading branch information
fhacloid authored Jul 4, 2024
2 parents bd05e9a + 3b6e093 commit ad0559f
Show file tree
Hide file tree
Showing 103 changed files with 304 additions and 309 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ ecr-connect: ## Login to ecr, requires aws cli installed
start-local-be: ## Starts local BE instance. Note! Only for cycloid developers
@if [ ! -d ${LOCAL_BE_GIT_PATH} ]; then echo "Unable to find BE at LOCAL_BE_GIT_PATH"; exit 1; fi;
@if [ -z "$$API_LICENCE_KEY" ]; then echo "API_LICENCE_KEY is not set"; exit 1; fi; \
@echo "Starting Local BE..."
echo "Starting Local BE..."
@echo "Generating fake data to be used in the tests..."
@cd $(LOCAL_BE_GIT_PATH) && sed -i '/cost-explorer-es/d' config.yml
@cd $(LOCAL_BE_GIT_PATH) && YD_API_TAG=${YD_API_TAG} API_LICENCE_KEY=${API_LICENCE_KEY} \
Expand All @@ -159,4 +159,3 @@ delete-local-be: ## Creates local BE instance and starts e2e tests. Note! Only f
new-changelog-entry: ## Create a new entry for unreleased element
@echo ${PATH}
docker run -it -v $(CURDIR):/cycloid-cli -w /cycloid-cli cycloid/cycloid-toolkit changie new

9 changes: 9 additions & 0 deletions changelog/unreleased/CLI-BREAKING-20240702-154137.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component: CLI
kind: BREAKING
body: Env var TOKEN is replaced by CY_API_KEY for giving token via env var.
time: 2024-07-02T15:41:37.096071763Z
custom:
DETAILS: The old `TOKEN` env var still works but emits a warning. It will be removed
in a futur release.
PR: "274"
TYPE: CLI
9 changes: 9 additions & 0 deletions changelog/unreleased/CLI-CHANGED-20240702-153738.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component: CLI
kind: CHANGED
body: Allow to use CY_ORG env var to define the current org instead of --org flag
time: 2024-07-02T15:37:38.698172633Z
custom:
DETAILS: You can still use the --org flag, the CLI flag will have precedence over
the env var.
PR: "274"
TYPE: CLI
9 changes: 9 additions & 0 deletions changelog/unreleased/CLI-FIXED-20240702-154027.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
component: CLI
kind: FIXED
body: --org flag is now global
time: 2024-07-02T15:40:27.815286561Z
custom:
DETAILS: This fixes inconsistency between commands, you can also set the org via
env vars now.
PR: "274"
TYPE: CLI
8 changes: 8 additions & 0 deletions changelog/unreleased/CLI-SECURITY-20240702-153824.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
component: CLI
kind: SECURITY
body: Make the configuration write as 0600 permissions in user home config
time: 2024-07-02T15:38:24.286288523Z
custom:
DETAILS: ""
PR: "274"
TYPE: CLI
10 changes: 10 additions & 0 deletions changelog/unreleased/CLI-SECURITY-20240702-153935.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
component: CLI
kind: SECURITY
body: Allow to login using CY_API_TOKEN instead of providing the token via --api-token
flag
time: 2024-07-02T15:39:35.1456052Z
custom:
DETAILS: This will become the default way, using the flag will be deprecated in
the future.
PR: "274"
TYPE: CLI
13 changes: 12 additions & 1 deletion cmd/cycloid.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ func NewRootCommand() *cobra.Command {
SilenceUsage: false,
Use: "cy",
Short: "Cycloid CLI",
Long: `Cy is a CLI for Cycloid framework. Learn more at https://www.cycloid.io/.`,
Long: `CLI tool to interact with Cycloid API.
Documentation at https://docs.beta.cycloid.io/reference/cli/
Environment:
CY_API_URL -> Specify the HTTP url of Cycloid API to use, default https://http-api.cycloid.io
CY_ORG -> Set the current organization
CY_API_TOKEN -> Set the current API TOKEN
CY_VERBOSITY -> Set the verbosity level (debug, info, warning, error), default warning `,
}

rootCmd.PersistentFlags().StringVarP(&userOutput, "output", "o", "table", "The formatting style for command output: json|yaml|table")
Expand All @@ -63,6 +71,9 @@ func NewRootCommand() *cobra.Command {
rootCmd.PersistentFlags().Bool("insecure", false, "Decide to skip or not TLS verification")
viper.BindPFlag("insecure", rootCmd.PersistentFlags().Lookup("insecure"))

rootCmd.PersistentFlags().String("org", "", "Specify the org to use. override CY_ORG env var. Required for all Org scoped endpoint.")
viper.BindPFlag("org", rootCmd.PersistentFlags().Lookup("org"))

// Remove usage on error, this is annoying in scripting
rootCmd.SilenceUsage = true

Expand Down
3 changes: 0 additions & 3 deletions cmd/cycloid/apikey/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package apikey

import (
"github.com/spf13/cobra"

"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
)

var (
Expand All @@ -24,7 +22,6 @@ func NewCommands() *cobra.Command {
Example: example,
Short: short,
}
common.RequiredPersistentFlag(common.WithFlagOrg, cmd)

cmd.AddCommand(
NewDeleteCommand(),
Expand Down
31 changes: 15 additions & 16 deletions cmd/cycloid/apikey/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@ func NewDeleteCommand() *cobra.Command {
# delete the API key 'my-key' in the org my-org
cy api-key delete --org my-org --canonical my-key
`,
RunE: func(cmd *cobra.Command, args []string) error {
org, err := cmd.Flags().GetString("org")
if err != nil {
return fmt.Errorf("unable to get org flag: %w", err)
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}
canonical, err := cmd.Flags().GetString("canonical")
if err != nil {
return fmt.Errorf("unable to get canonical flag: %w", err)
}
return remove(cmd, org, canonical, output)
},
RunE: remove,
}

WithFlagCanonical(cmd)
Expand All @@ -46,7 +32,20 @@ func NewDeleteCommand() *cobra.Command {

// remove will send the DELETE request to the API in order to
// delete a generated token
func remove(cmd *cobra.Command, org, canonical, output string) error {
func remove(cmd *cobra.Command, args []string) error {
org, err := common.GetOrg(cmd)
if err != nil {
return fmt.Errorf("unable to get org flag: %w", err)
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}
canonical, err := cmd.Flags().GetString("canonical")
if err != nil {
return fmt.Errorf("unable to get canonical flag: %w", err)
}

api := common.NewAPI()
m := middleware.NewMiddleware(api)

Expand Down
31 changes: 15 additions & 16 deletions cmd/cycloid/apikey/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,7 @@ func NewGetCommand() *cobra.Command {
# get API key 'my-key' in the org my-org
cy api-key get --org my-org --canonical my-key
`,
RunE: func(cmd *cobra.Command, args []string) error {
org, err := cmd.Flags().GetString("org")
if err != nil {
return fmt.Errorf("unable to get org flag: %w", err)
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}
canonical, err := cmd.Flags().GetString("canonical")
if err != nil {
return fmt.Errorf("unable to get canonical flag: %w", err)
}
return get(cmd, org, canonical, output)
},
RunE: get,
}

WithFlagCanonical(cmd)
Expand All @@ -46,7 +32,20 @@ func NewGetCommand() *cobra.Command {

// get will send the GET request to the API in order to
// get the generated token
func get(cmd *cobra.Command, org, canonical, output string) error {
func get(cmd *cobra.Command, args []string) error {
org, err := common.GetOrg(cmd)
if err != nil {
return fmt.Errorf("unable to get org flag: %w", err)
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}
canonical, err := cmd.Flags().GetString("canonical")
if err != nil {
return fmt.Errorf("unable to get canonical flag: %w", err)
}

api := common.NewAPI()
m := middleware.NewMiddleware(api)

Expand Down
30 changes: 15 additions & 15 deletions cmd/cycloid/apikey/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,35 +19,35 @@ func NewListCommand() *cobra.Command {
Use: "list",
Short: "list API keys",
Example: `
# list API keys in the org my-org
cy api-key list --org my-org
# list API keys in the org my-org
cy api-key list --org my-org
`,
RunE: func(cmd *cobra.Command, args []string) error {
org, err := cmd.Flags().GetString("org")
if err != nil {
return fmt.Errorf("unable to get org flag: %w", err)
}
output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}
return list(cmd, org, output)
},
RunE: list,
}
return cmd
}

// list will send the GET request to the API in order to
// list the generated tokens
func list(cmd *cobra.Command, org, output string) error {
func list(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
if err != nil {
return err
}

output, err := cmd.Flags().GetString("output")
if err != nil {
return fmt.Errorf("unable to get output flag: %w", err)
}

// fetch the printer from the factory
p, err := factory.GetPrinter(output)
if err != nil {
return errors.Wrap(err, "unable to get printer")
}

keys, err := m.ListAPIKey(org)
return printer.SmartPrint(p, keys, err, "unable to list API keys", printer.Options{}, cmd.OutOrStdout())
}
2 changes: 0 additions & 2 deletions cmd/cycloid/catalog-repositories/cmd.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package catalogRepositories

import (
"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
"github.com/spf13/cobra"
)

Expand All @@ -15,7 +14,6 @@ func NewCommands() *cobra.Command {
},
Short: "Manage the catalog repositories",
}
common.RequiredPersistentFlag(common.WithFlagOrg, cmd)

cmd.AddCommand(NewCreateCommand(),
NewUpdateCommand(),
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func createCatalogRepository(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func deleteCatalogRepository(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func getCatalogRepository(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func listCatalogRepositories(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func refreshCatalogRepository(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cycloid/catalog-repositories/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func updateCatalogRepository(cmd *cobra.Command, args []string) error {
api := common.NewAPI()
m := middleware.NewMiddleware(api)

org, err := cmd.Flags().GetString("org")
org, err := common.GetOrg(cmd)
if err != nil {
return err
}
Expand Down
20 changes: 16 additions & 4 deletions cmd/cycloid/common/flags.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
package common

import (
"fmt"
"os"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var (
projectFlag, envFlag, orgFlag, credFlag, canFlag string
idFlag uint32
)

func WithFlagOrg(cmd *cobra.Command) string {
flagName := "org"
cmd.PersistentFlags().StringVar(&orgFlag, flagName, "", "Org cannonical name")
return flagName
func GetOrg(cmd *cobra.Command) (org string, err error) {
org = viper.GetString("org")
if org == "" {
return "", errors.New("org is not set, use --org flag or CY_ORG env var")
}

if viper.GetString("verbosity") == "debug" {
fmt.Fprintln(os.Stderr, "\033[1;34mdebug:\033[0m using org:", org)
}

return org, nil
}

func WithFlagProject(cmd *cobra.Command) string {
Expand Down
12 changes: 9 additions & 3 deletions cmd/cycloid/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,16 @@ func (a *APIClient) Credentials(org *string) runtime.ClientAuthInfoWriter {
var token = a.Config.Token
if token == "" {
// we first try to get the token from the env variable
token = os.Getenv("TOKEN")
var ok bool
token, ok = os.LookupEnv("CY_API_TOKEN")
if !ok {
token, ok = os.LookupEnv("TOKEN")
if ok {
fmt.Println("TOKEN env var is deprecated, please use CY_API_TOKEN instead")
}
}
}

// if the token is not set with env variable we try to fetch
// him from the config (if the user is logged)
if len(token) == 0 {
Expand Down Expand Up @@ -332,8 +340,6 @@ func ParseFormsConfig(form *models.FormUseCase, getCurrent bool) (vars map[strin
vars[*varEntity.Key] = value
}

// We have to strings.ToLower() the groups and sections names
// otherwise, it will not be recognized as input for a create-env
groups[strings.ToLower(*group.Name)] = vars
}

Expand Down
3 changes: 0 additions & 3 deletions cmd/cycloid/config-repositories/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package configRepositories

import (
"github.com/spf13/cobra"

"github.com/cycloidio/cycloid-cli/cmd/cycloid/common"
)

func NewCommands() *cobra.Command {
Expand All @@ -15,7 +13,6 @@ func NewCommands() *cobra.Command {
},
Short: "Manage the catalog repositories",
}
common.RequiredPersistentFlag(common.WithFlagOrg, cmd)

cmd.AddCommand(NewCreateCommand(),
NewUpdateCommand(),
Expand Down
Loading

0 comments on commit ad0559f

Please sign in to comment.