Skip to content

Commit

Permalink
Backwards compatibility v2 (#108)
Browse files Browse the repository at this point in the history
* Add refresh command

* Credentials file
  • Loading branch information
jaydhulia authored Dec 6, 2021
1 parent acd1c5e commit 818a107
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func runFile(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Printf("Profile %s for role \"%s\" updated\n", profileName, role)
fmt.Printf("Credentials written to %s\n", destination)
if autoRefresh {
logging.Log.WithFields(logrus.Fields{"role": role}).Infoln("Starting automatic file refresh")
fmt.Printf("starting automatic file refresh for %s", role)
Expand Down
65 changes: 65 additions & 0 deletions cmd/refresh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2020 Netflix, 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 cmd

import (
"fmt"

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

func init() {
refreshCmd.PersistentFlags().StringVarP(&roleRefreshARN, "role", "z", "", "role")
rootCmd.AddCommand(refreshCmd)
}

var refreshCmd = &cobra.Command{
Use: "refresh [profile]",
Short: "Refresh AWS credentials for profiles",
Hidden: true,
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
profileName = args[0]
}

if roleRefreshARN == "" {
// roleRefreshARN is not present, have to go through aws-profiles to see if a role matches
awsProfiles := viper.GetStringMapString("aws-profiles")
for name, role := range awsProfiles {
if name == profileName {
roleRefreshARN = role
break
}
}
}
if roleRefreshARN == "" {
return fmt.Errorf("unable to find profile %s in 'aws-profiles' property. You can also run with -r role_name <optional_profile_name>", profileName)
}

awsCredentialsFile := viper.GetString("aws-credential-file")
if awsCredentialsFile != "" {
destination = awsCredentialsFile
}

argsPass := []string{roleRefreshARN}
// explicit refresh command means force overwrite the profile
force = true
return fileCmd.RunE(fileCmd, argsPass)
},
}
1 change: 1 addition & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
noOpen bool
profileName string
region string
roleRefreshARN string
shortInfo bool
showAll bool
showConfiguredProfilesOnly bool
Expand Down

0 comments on commit 818a107

Please sign in to comment.