Skip to content

Commit

Permalink
[fix] homedir not parsed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
bitte-ein-bit committed Jul 12, 2024
1 parent 77eb63e commit a6d1ba3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
4 changes: 0 additions & 4 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ func validateSection(cfg *ini.File, section string) error {
// it should not have any of source_profile, role_arn, mfa_serial, external_id, or credential_source
for _, key := range []string{"source_profile", "role_arn", "mfa_serial", "external_id", "credential_source", "credential_process"} {
if s.HasKey(key) {
log.WithFields(log.Fields{
"section": section,
"key": key,
}).Errorf("Profile contains key %s, which indicates, it should not be used by clisso", key)
return fmt.Errorf(errCannotBeUsed, section, key)
}
}
Expand Down
23 changes: 19 additions & 4 deletions cmd/credential_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/allcloud-io/clisso/aws"
"github.com/allcloud-io/clisso/log"
homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand Down Expand Up @@ -59,7 +60,7 @@ var configureCmd = &cobra.Command{
Use: "configure",
Short: "Configure the credential_process functionality",
Run: func(cmd *cobra.Command, args []string) {
err := configureCredentialProcess()
err := configureCredentialProcess(cmd)
if err != nil {
log.Fatal("Failed to configure credential_process:", err)
}
Expand All @@ -68,12 +69,22 @@ var configureCmd = &cobra.Command{
}

func init() {
cmdCredentialProcess.AddCommand(unlockCmd, lockCmd, lockStatusCmd, configureCmd)
RootCmd.AddCommand(cmdCredentialProcess)
cmdCredentialProcess.AddCommand(unlockCmd, lockCmd, lockStatusCmd, configureCmd)


configureCmd.Flags().StringVarP(
&output, "output", "o", defaultOutput, "where to configure credentials_process profiles",
)
// here for backward compatibility
configureCmd.Flags().StringVarP(
&writeToFile, "write-to-file", "w", defaultOutput, "Write credentials to this file instead of the default",
)
err := configureCmd.Flags().MarkDeprecated("write-to-file", "please use output instead.")
if err != nil {
// we don't have a logger yet, so we can't use it but need to print the error to the console
fmt.Printf("Error marking flag as deprecated: %v", err)
}
}

func enableCredentialProcess() error {
Expand Down Expand Up @@ -105,12 +116,16 @@ func checkCredentialProcessActive(printToCredentialProcess bool) {
}
}

func configureCredentialProcess() error {
o := preferredOutput(cmdCredentialProcess, "")
func configureCredentialProcess(cmd *cobra.Command) error {
o := preferredOutput(cmd, "")
// check if output is set to credential_process or environment
if o == "credential_process" || o == "environment" {
return fmt.Errorf("output flag cannot be set to '%s' when configuring credential_process", o)
}
o, err := homedir.Expand(o)
if err != nil {
return err
}
// configure all apps as AWS profiles
apps := viper.GetStringMap("apps")
for app := range apps {
Expand Down
6 changes: 4 additions & 2 deletions cmd/credential_process_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/allcloud-io/clisso/log"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -101,12 +102,13 @@ func TestConfigureCredentialProcess(t *testing.T) {
viper.SetConfigFile("TestConfigureCredentialProcess.yaml")

// test default values
err := configureCredentialProcess()
cmd := &cobra.Command{}
err := configureCredentialProcess(cmd)
assert.Nil(err, "Expected no error, but got: %v", err)

for _, o := range []string{"credential_process", "environment"} {
viper.Set("global.output", o)
err = configureCredentialProcess()
err = configureCredentialProcess(cmd)
assert.NotNil(err, "Expected an error, but got nil")
assert.EqualError(err, "output flag cannot be set to '"+o+"' when configuring credential_process")
}
Expand Down

0 comments on commit a6d1ba3

Please sign in to comment.