Skip to content

Commit

Permalink
add check for GNUPG1 pubring file and check that the config file has …
Browse files Browse the repository at this point in the history
…profiles set
  • Loading branch information
esilva-everbridge committed Feb 13, 2019
1 parent 666909b commit 62e6708
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
46 changes: 32 additions & 14 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/mattes/go-expand-tilde.v1"
)

var logger = logrus.New()
Expand Down Expand Up @@ -118,6 +119,17 @@ func init() {
privateKeyRing = fmt.Sprintf("%s/secring.gpg", gpgHome)
}

// check for GNUPG1 pubring file
filePath, err := tilde.Expand(publicKeyRing)
if err != nil {
logger.Fatalf("Error with GNUPG pubring path: %s", err)
}
if _, err = os.Stat(filepath.Clean(filePath)); os.IsNotExist(err) {
if err != nil {
logger.Fatalf("Error finding GNUPG pubring file: %s", err)
}
}

rootCmd.PersistentFlags().BoolP("version", "v", false, "print the version")
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.config/generate-secure-pillar/config.yaml)")
rootCmd.PersistentFlags().StringVar(&profile, "profile", "", "config file (default is $HOME/.config/generate-secure-pillar/config.yaml)")
Expand Down Expand Up @@ -146,6 +158,10 @@ func initConfig() {
if err != nil {
logger.Fatalf("error creating config file path: %s", err)
}
_, err = os.OpenFile(dir+"/config.yaml", os.O_RDONLY|os.O_CREATE, 0660)
if err != nil {
logger.Fatalf("Error creating config file: %s", err)
}

// set config in "~/.config/generate-secure-pillar/config.yaml".
viper.AddConfigPath(configPath)
Expand All @@ -168,20 +184,22 @@ func getPki() pki.Pki {
}

func readProfile() {
profiles := viper.Get("profiles")
profName := rootCmd.Flag("profile").Value.String()

if profName != "" || pgpKeyName == "" {
for _, prof := range profiles.([]interface{}) {
p := prof.(map[interface{}]interface{})
if p["default"] == true || profName == p["name"] {
gpgHome := p["gnupg_home"].(string)
if gpgHome != "" {
publicKeyRing = fmt.Sprintf("%s/pubring.gpg", gpgHome)
privateKeyRing = fmt.Sprintf("%s/secring.gpg", gpgHome)
}
if p["default_key"] != nil {
pgpKeyName = p["default_key"].(string)
if viper.IsSet("profiles") {
profiles := viper.Get("profiles")
profName := rootCmd.Flag("profile").Value.String()

if profName != "" || pgpKeyName == "" {
for _, prof := range profiles.([]interface{}) {
p := prof.(map[interface{}]interface{})
if p["default"] == true || profName == p["name"] {
gpgHome := p["gnupg_home"].(string)
if gpgHome != "" {
publicKeyRing = fmt.Sprintf("%s/pubring.gpg", gpgHome)
privateKeyRing = fmt.Sprintf("%s/secring.gpg", gpgHome)
}
if p["default_key"] != nil {
pgpKeyName = p["default_key"].(string)
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ require (
github.com/spf13/viper v1.3.1
github.com/y0ssar1an/q v1.0.6
golang.org/x/tools v0.0.0-20190107155254-e063def13b29 // indirect
gopkg.in/mattes/go-expand-tilde.v1 v1.0.0-20150330173918-cb884138e64c
gopkg.in/yaml.v2 v2.2.2
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 h1:OAj3g0cR6Dx/R07QgQe8wkA9RNjB2u4i700xBkIT4e0=
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2/go.mod h1:Xk6kEKp8OKb+X14hQBKWaSkCsqBpgog8nAV2xsGOxlo=
gopkg.in/mattes/go-expand-tilde.v1 v1.0.0-20150330173918-cb884138e64c h1:/Onz8dZtKBCmB8P0JU7+WSCfMekXry7BflVO0SQQrCU=
gopkg.in/mattes/go-expand-tilde.v1 v1.0.0-20150330173918-cb884138e64c/go.mod h1:j6QavCO5cYWud1+2/PFTXL1y6tjjkhSs+qcWgibOIc0=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
Expand Down

0 comments on commit 62e6708

Please sign in to comment.