Skip to content

Commit

Permalink
Adds a command to generate credential process configuration (#10)
Browse files Browse the repository at this point in the history
* Adds a command to generate credential process configuration

* handle error
  • Loading branch information
castrapel authored Oct 20, 2020
1 parent caa1db8 commit 6f7aeb4
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/credential_process.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,25 @@ import (
"fmt"
"time"

"github.com/netflix/weep/util"
ini "gopkg.in/ini.v1"

"github.com/netflix/weep/consoleme"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

func init() {
CredentialProcessCmd.PersistentFlags().BoolVarP(&noIpRestrict, "no-ip", "n", false, "remove IP restrictions")
GenerateCredentialProcessCmd.PersistentFlags().StringVarP(&destinationConfig, "output", "o", getDefaultAwsConfigFile(), "output file for AWS config")
rootCmd.AddCommand(CredentialProcessCmd)
rootCmd.AddCommand(GenerateCredentialProcessCmd)
}

var GenerateCredentialProcessCmd = &cobra.Command{
Use: "generate_credential_process_config",
Short: "Writes all of your eligible roles as profiles in your AWS Config to source credentials from Weep",
RunE: runGenerateCredentialProcessConfig,
}

var CredentialProcessCmd = &cobra.Command{
Expand All @@ -22,6 +33,52 @@ var CredentialProcessCmd = &cobra.Command{
RunE: runCredentialProcess,
}

func writeConfigFile(roles []string) error {
var configINI *ini.File
var err error

// Disable pretty format, but still put spaces around `=`
ini.PrettyFormat = false
ini.PrettyEqual = true

if util.FileExists(destination) {
configINI, err = ini.Load(destination)
if err != nil {
return err
}
} else {
configINI = ini.Empty()
}

for _, r := range roles {
profileName := fmt.Sprintf("profile %s", r)
command := fmt.Sprintf("weep credential_process %s", r)
configINI.Section(profileName).Key("credential_process").SetValue(command)
}
err = configINI.SaveTo(destinationConfig)
if err != nil {
return err
}

return nil
}

func runGenerateCredentialProcessConfig(cmd *cobra.Command, args []string) error {
client, err := consoleme.GetClient()
if err != nil {
return err
}
roles, err := client.Roles()
if err != nil {
return err
}
err = writeConfigFile(roles)
if err != nil {
return err
}
return nil
}

func runCredentialProcess(cmd *cobra.Command, args []string) error {
role = args[0]
client, err := consoleme.GetClient()
Expand Down
9 changes: 9 additions & 0 deletions cmd/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ func getDefaultCredentialsFile() string {
return path.Join(home, ".aws", "credentials")
}

func getDefaultAwsConfigFile() string {
home, err := homedir.Dir()
if err != nil {
fmt.Printf("couldn't get default directory!")
os.Exit(1)
}
return path.Join(home, ".aws", "config")
}

func writeCredentialsFile(credentials consoleme.AwsCredentials) error {
var credentialsINI *ini.File
var err error
Expand Down
1 change: 1 addition & 0 deletions cmd/vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var (
role string
profileName string
destination string
destinationConfig string
noIpRestrict bool
metadataRegion string
metadataListenAddr string
Expand Down

0 comments on commit 6f7aeb4

Please sign in to comment.