diff --git a/.goreleaser/mac.yml b/.goreleaser/mac.yml index 5c7c56e6..407d3f63 100644 --- a/.goreleaser/mac.yml +++ b/.goreleaser/mac.yml @@ -21,6 +21,8 @@ builds: ldflags: - -s -w -X github.com/stripe/stripe-cli/pkg/version.Version={{.Version}} binary: stripe + env: + - CGO_ENABLED=1 main: ./cmd/stripe/main.go goos: - darwin diff --git a/go.mod b/go.mod index 357815ef..85690fa0 100644 --- a/go.mod +++ b/go.mod @@ -47,7 +47,6 @@ require ( ) require ( - github.com/alessio/shellescape v1.4.1 github.com/hashicorp/go-hclog v1.2.2 github.com/hashicorp/go-plugin v1.4.4 github.com/joho/godotenv v1.4.0 diff --git a/go.sum b/go.sum index ee51c9da..391e7068 100644 --- a/go.sum +++ b/go.sum @@ -49,8 +49,6 @@ github.com/Microsoft/go-winio v0.5.2 h1:a9IhgEQBCUEk6QCdml9CiJGhAws+YwffDHEMp1VM github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 h1:uSoVVbwJiQipAclBbw+8quDsfcvFjOpI5iCf4p/cqCs= github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7/go.mod h1:6zEj6s6u/ghQa61ZWa/C2Aw3RkjiTBOix7dkqa1VLIs= -github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= -github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239 h1:kFOfPq6dUM1hTo4JG6LR5AXSUEsOjtdm0kw0FtQtMJA= github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= diff --git a/pkg/config/config.go b/pkg/config/config.go index 6371b24d..bec8845b 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -253,6 +253,21 @@ func (c *Config) RemoveAllProfiles() error { return syncConfig(runtimeViper) } +func deleteLivemodeKey(key string, profile string) error { + fieldID := profile + "." + key + existingKeys, err := KeyRing.Keys() + if err != nil { + return err + } + for _, item := range existingKeys { + if item == fieldID { + KeyRing.Remove(fieldID) + return nil + } + } + return nil +} + // isProfile identifies whether a value in the config pertains to a profile. func isProfile(value interface{}) bool { // TODO: ianjabour - ideally find a better way to identify projects in config diff --git a/pkg/config/config_livemode.go b/pkg/config/config_livemode.go deleted file mode 100644 index 62616fea..00000000 --- a/pkg/config/config_livemode.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build !arm64 -// +build !arm64 - -package config - -func deleteLivemodeKey(key string, profile string) error { - fieldID := profile + "." + key - existingKeys, err := KeyRing.Keys() - if err != nil { - return err - } - for _, item := range existingKeys { - if item == fieldID { - KeyRing.Remove(fieldID) - return nil - } - } - return nil -} diff --git a/pkg/config/config_livemode_arm64.go b/pkg/config/config_livemode_arm64.go deleted file mode 100644 index 1011c3a7..00000000 --- a/pkg/config/config_livemode_arm64.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build arm64 -// +build arm64 - -package config - -import exec "golang.org/x/sys/execabs" - -func deleteLivemodeKey(key string, profile string) error { - fieldID := profile + "." + key - _, err := exec.Command( - execPathKeychain, - "delete-generic-password", - "-s", fieldID, - "-a", KeyManagementService).CombinedOutput() - if err != nil { - return err - } - return nil -} diff --git a/pkg/config/profile.go b/pkg/config/profile.go index 80516aa1..cb9dd2b0 100644 --- a/pkg/config/profile.go +++ b/pkg/config/profile.go @@ -431,6 +431,51 @@ func getKeyExpiresAt() string { return time.Now().AddDate(0, 0, KeyValidInDays).UTC().Format(DateStringFormat) } +// saveLivemodeValue saves livemode value of given key in keyring +func (p *Profile) saveLivemodeValue(field, value, description string) { + fieldID := p.GetConfigField(field) + _ = KeyRing.Set(keyring.Item{ + Key: fieldID, + Data: []byte(value), + Description: description, + Label: fieldID, + }) +} + +// retrieveLivemodeValue retrieves livemode value of given key in keyring +func (p *Profile) retrieveLivemodeValue(key string) (string, error) { + fieldID := p.GetConfigField(key) + existingKeys, err := KeyRing.Keys() + if err != nil { + return "", err + } + + for _, item := range existingKeys { + if item == fieldID { + value, _ := KeyRing.Get(fieldID) + return string(value.Data), nil + } + } + + return "", validators.ErrAPIKeyNotConfigured +} + +// deleteLivemodeValue deletes livemode value of given key in keyring +func (p *Profile) deleteLivemodeValue(key string) error { + fieldID := p.GetConfigField(key) + existingKeys, err := KeyRing.Keys() + if err != nil { + return err + } + for _, item := range existingKeys { + if item == fieldID { + KeyRing.Remove(fieldID) + return nil + } + } + return nil +} + // ExperimentalFields are currently only used for request signing type ExperimentalFields struct { ContextualName string diff --git a/pkg/config/profile_livemode.go b/pkg/config/profile_livemode.go deleted file mode 100644 index b44f4ea1..00000000 --- a/pkg/config/profile_livemode.go +++ /dev/null @@ -1,55 +0,0 @@ -//go:build !arm64 -// +build !arm64 - -package config - -import ( - "github.com/stripe/stripe-cli/pkg/validators" - - "github.com/99designs/keyring" -) - -// saveLivemodeValue saves livemode value of given key in keyring -func (p *Profile) saveLivemodeValue(field, value, description string) { - fieldID := p.GetConfigField(field) - _ = KeyRing.Set(keyring.Item{ - Key: fieldID, - Data: []byte(value), - Description: description, - Label: fieldID, - }) -} - -// retrieveLivemodeValue retrieves livemode value of given key in keyring -func (p *Profile) retrieveLivemodeValue(key string) (string, error) { - fieldID := p.GetConfigField(key) - existingKeys, err := KeyRing.Keys() - if err != nil { - return "", err - } - - for _, item := range existingKeys { - if item == fieldID { - value, _ := KeyRing.Get(fieldID) - return string(value.Data), nil - } - } - - return "", validators.ErrAPIKeyNotConfigured -} - -// deleteLivemodeValue deletes livemode value of given key in keyring -func (p *Profile) deleteLivemodeValue(key string) error { - fieldID := p.GetConfigField(key) - existingKeys, err := KeyRing.Keys() - if err != nil { - return err - } - for _, item := range existingKeys { - if item == fieldID { - KeyRing.Remove(fieldID) - return nil - } - } - return nil -} diff --git a/pkg/config/profile_livemode_arm64.go b/pkg/config/profile_livemode_arm64.go deleted file mode 100644 index c2205432..00000000 --- a/pkg/config/profile_livemode_arm64.go +++ /dev/null @@ -1,88 +0,0 @@ -//go:build arm64 -// +build arm64 - -package config - -import ( - "encoding/hex" - "fmt" - "io" - "strings" - - "github.com/alessio/shellescape" - exec "golang.org/x/sys/execabs" - - "github.com/stripe/stripe-cli/pkg/validators" -) - -const ( - // execPathKeychain is the path to the keychain binary - execPathKeychain = "/usr/bin/security" - - // encodingPrefix is a well-known prefix added to strings encoded by Set. - encodingPrefix = "go-keyring-encoded:" -) - -// saveLivemodeValue saves livemode value of given key in keyring -func (p *Profile) saveLivemodeValue(key, value, description string) { - fieldID := p.GetConfigField(key) - value = encodingPrefix + hex.EncodeToString([]byte(value)) - - cmd := exec.Command(execPathKeychain, "-i") - stdIn, _ := cmd.StdinPipe() - cmd.Start() - - command := fmt.Sprintf( - "add-generic-password -U -s %s -a %s -w %s\n", - shellescape.Quote(fieldID), - shellescape.Quote(KeyManagementService), - shellescape.Quote(value), - ) - - io.WriteString(stdIn, command) - stdIn.Close() - cmd.Wait() -} - -// retrieveLivemodeValue retrieves livemode value of given key in keyring -func (p *Profile) retrieveLivemodeValue(key string) (string, error) { - fieldID := p.GetConfigField(key) - - out, err := exec.Command( - execPathKeychain, - "find-generic-password", - "-s", fieldID, - "-wa", KeyManagementService).CombinedOutput() - if err != nil { - if strings.Contains(string(out), "could not be found") { - return "", validators.ErrAPIKeyNotConfigured - } - } - - value := strings.TrimSpace(string(out)) - // if the string has the well-known prefix, assume it's encoded - if strings.HasPrefix(value, encodingPrefix) { - dec, err := hex.DecodeString(value[len(encodingPrefix):]) - return string(dec), err - } - - if value != "" { - return value, nil - } - - return "", validators.ErrAPIKeyNotConfigured -} - -// deleteLivemodeValue deletes livemode value of given key in keyring -func (p *Profile) deleteLivemodeValue(key string) error { - fieldID := p.GetConfigField(key) - _, err := exec.Command( - execPathKeychain, - "delete-generic-password", - "-s", fieldID, - "-a", KeyManagementService).CombinedOutput() - if err != nil { - return err - } - return nil -}