-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce7803c
commit 9b8eee7
Showing
7 changed files
with
596 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package cmd | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/99designs/keyring" | ||
"github.com/mitchellh/go-homedir" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
var addCmd = &cobra.Command{ | ||
Use: "add [profile]", | ||
Short: "Add a new profile to your configuration and keychain", | ||
Long: "", | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("requires a profile argument") | ||
} | ||
return nil | ||
}, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if verbose { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
profileName := strings.TrimSpace(args[0]) | ||
|
||
reader := bufio.NewReader(os.Stdin) | ||
fmt.Print("Email address: ") | ||
emailAddress, _ := reader.ReadString('\n') | ||
emailAddress = strings.TrimSpace(emailAddress) | ||
|
||
fmt.Print("Authentication type (api_token or api_key): ") | ||
authType, _ := reader.ReadString('\n') | ||
authType = strings.TrimSpace(authType) | ||
|
||
fmt.Print("Authentication value: ") | ||
authValue, _ := reader.ReadString('\n') | ||
authValue = strings.TrimSpace(authValue) | ||
|
||
home, err := homedir.Dir() | ||
if err != nil { | ||
log.Fatal("unable to find home directory: ", err) | ||
} | ||
|
||
os.MkdirAll(home+defaultConfigDirectory, 0700) | ||
if _, err := os.Stat(home + defaultFullConfigPath); os.IsNotExist(err) { | ||
file, err := os.Create(home + defaultFullConfigPath) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer file.Close() | ||
} | ||
|
||
cfg, _ := ini.Load(home + defaultFullConfigPath) | ||
cfg.Section(fmt.Sprintf("profile %s", profileName)).NewKey("email", emailAddress) | ||
cfg.Section(fmt.Sprintf("profile %s", profileName)).NewKey("auth_type", authType) | ||
cfg.SaveTo(home + defaultFullConfigPath) | ||
|
||
ring, _ := keyring.Open(keyring.Config{ | ||
FileDir: "~/.cf-vault/keys/", | ||
ServiceName: projectName, | ||
}) | ||
|
||
_ = ring.Set(keyring.Item{ | ||
Key: fmt.Sprintf("%s-%s", profileName, authType), | ||
Data: []byte(authValue), | ||
}) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,75 @@ | ||
package cmd | ||
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"os/exec" | ||
"strings" | ||
|
||
"github.com/99designs/keyring" | ||
"github.com/mitchellh/go-homedir" | ||
log "github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
"gopkg.in/ini.v1" | ||
) | ||
|
||
func init() { | ||
rootCmd.AddCommand(execCmd) | ||
} | ||
|
||
var execCmd = &cobra.Command{ | ||
Use: "exec", | ||
Use: "exec [profile]", | ||
Short: "Execute a command with Cloudflare credentials populated", | ||
Long: "", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New("requires a profile argument") | ||
} | ||
return nil | ||
}, | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
if verbose { | ||
log.SetLevel(log.DebugLevel) | ||
} | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
profileName := args[0] | ||
log.Debug("using profile: ", profileName) | ||
|
||
home, err := homedir.Dir() | ||
if err != nil { | ||
log.Fatal("unable to find home directory: ", err) | ||
} | ||
cfg, err := ini.Load(home + defaultFullConfigPath) | ||
profileSection, err := cfg.GetSection("profile " + profileName) | ||
if err != nil { | ||
log.Fatal("unable to create profile section: ", err) | ||
} | ||
|
||
if profileSection.Key("email").String() == "" { | ||
log.Fatal(fmt.Sprintf("no profile matching %q found in the configuration file at %s", profileName, defaultFullConfigPath)) | ||
} | ||
|
||
ring, _ := keyring.Open(keyring.Config{ | ||
FileDir: "~/.cf-vault/keys/", | ||
ServiceName: projectName, | ||
}) | ||
|
||
i, _ := ring.Get(fmt.Sprintf("%s-%s", profileName, profileSection.Key("auth_type").String())) | ||
|
||
fmt.Printf("%v", string(i.Data)) | ||
|
||
log.Debug("environment is populated with credentials") | ||
|
||
command := exec.Command(args[1]) | ||
command.Env = append(os.Environ(), | ||
fmt.Sprintf("CLOUDFLARE_EMAIL=%s", profileSection.Key("email").String()), | ||
fmt.Sprintf("CLOUDFLARE_%s=%s", strings.ToUpper(profileSection.Key("auth_type").String()), string(i.Data)), | ||
) | ||
commandOutput := &bytes.Buffer{} | ||
command.Stdout = commandOutput | ||
err = command.Run() | ||
if err != nil { | ||
os.Stderr.WriteString(err.Error()) | ||
} | ||
fmt.Print(string(commandOutput.Bytes())) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.