Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cmd

import (
"os"
"os/exec"
"syscall"

"github.com/netflix/weep/creds"
"github.com/spf13/cobra"
)

var (
runRole string
)

func init() {
runCmd.PersistentFlags().BoolVarP(&noIpRestrict, "no-ip", "n", false, "remove IP restrictions")
runCmd.PersistentFlags().StringVarP(&runRole, "role", "r", "", "role to use when running command")
rootCmd.AddCommand(runCmd)
}

var runCmd = &cobra.Command{
Use: "run [command]",
Short: "Run a CLI command with Weep credentials",
Args: cobra.MinimumNArgs(1),
RunE: runRun,
}

func runRun(cmd *cobra.Command, args []string) error {
binary, err := exec.LookPath(args[0])
if err != nil {
return err
}
credentials, err := creds.GetCredentials(runRole, noIpRestrict, assumeRole)
if err != nil {
return err
}
env := os.Environ()

env = append(env, "AWS_ACCESS_KEY_ID="+credentials.AccessKeyId)
env = append(env, "AWS_SECRET_ACCESS_KEY="+credentials.SecretAccessKey)
env = append(env, "AWS_SESSION_TOKEN="+credentials.SessionToken)

if err := syscall.Exec(binary, args[0:], env); err != nil {
return err
}
return nil
}