Skip to content

Commit

Permalink
exec: spawn new shell on no arguments
Browse files Browse the repository at this point in the history
When no arguments are provided, spawn a fresh shell with the current environment
and the credentials populated.
  • Loading branch information
jacobbednarz committed Sep 16, 2020
1 parent e83887a commit 47eefa1
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"strings"
"syscall"

"os/exec"

Expand Down Expand Up @@ -50,31 +51,40 @@ var execCmd = &cobra.Command{
}

if profileSection.Key("email").String() == "" {
log.Fatal(fmt.Sprintf("no profile matching %q found in the configuration file at %s", profileName, defaultFullConfigPath))
log.Fatalf("no profile matching %q found in the configuration file at %s", profileName, defaultFullConfigPath)
}


command := strings.Split(args[1], " ")
executable := command[0]
argv0, err := exec.LookPath(executable)
if err != nil {
log.Fatalf("couldn't find the executable '%s': %w", executable, err)
// Don't allow nesting of cf-vault sessions, it gets messy.
if os.Getenv("CLOUDFLARE_VAULT_SESSION") != "" {
log.Fatal("cf-vault sessions shouldn't be nested, unset CLOUDFLARE_VAULT_SESSION to continue or open a new shell session")
}

ring, _ := keyring.Open(keyringDefaults)
keychain, _ := ring.Get(fmt.Sprintf("%s-%s", profileName, profileSection.Key("auth_type").String()))

runningCommand := exec.Command(executable, args...)
runningCommand.Env = append(os.Environ(),
cloudflareCreds := []string{
fmt.Sprintf("CLOUDFLARE_VAULT_SESSION=%s", profileName),
fmt.Sprintf("CLOUDFLARE_EMAIL=%s", profileSection.Key("email").String()),
fmt.Sprintf("CLOUDFLARE_%s=%s", strings.ToUpper(profileSection.Key("auth_type").String()), string(keychain.Data)),
)
stdoutStderr, err := runningCommand.CombinedOutput()
}

// Should a command not be provided, drop into a fresh shell with the
// credentials populated alongside the existing env.
if len(args) == 0 {
log.Debug("launching new shell with credentials populated")
envVars := append(syscall.Environ(), cloudflareCreds...)
syscall.Exec(os.Getenv("SHELL"), []string{os.Getenv("SHELL")}, envVars)
}

executable := args[0]
pathtoExec, err := exec.LookPath(executable)
if err != nil {
log.Fatal(err)
log.Fatalf("couldn't find the executable '%s': %w", pathtoExec, err)
}

fmt.Printf("%s\n", stdoutStderr)
log.Debugf("found executable %s", pathtoExec)
log.Debugf("executing command: %s", strings.Join(args, " "))

syscall.Exec(pathtoExec, args, cloudflareCreds)
},
}

0 comments on commit 47eefa1

Please sign in to comment.