Skip to content

Commit

Permalink
Revert livemode storage feature (#935)
Browse files Browse the repository at this point in the history
* Revert livemode storage feature

* backwards compatible with redacted livemode key
  • Loading branch information
etsai-stripe committed Aug 8, 2022
1 parent 8966e19 commit ccdecd4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 82 deletions.
2 changes: 1 addition & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *Config) InitConfig() {
})

// redact livemode values for existing configs
c.Profile.redactAllLivemodeValues()
// c.Profile.redactAllLivemodeValues()
}

// EditConfig opens the configuration file in the default editor.
Expand Down
51 changes: 32 additions & 19 deletions pkg/config/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,18 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) {
key = viper.GetString(p.GetConfigField(TestModeAPIKeyName))
}
} else {
p.redactAllLivemodeValues()
key, err = p.retrieveLivemodeValue(LiveModeAPIKeyName)
if err != nil {
return "", err
// p.redactAllLivemodeValues()
// key, err = p.retrieveLivemodeValue(LiveModeAPIKeyName)
// if err != nil {
// return "", err
// }

if err := viper.ReadInConfig(); err == nil {
key = viper.GetString(p.GetConfigField(LiveModeAPIKeyName))
}

if isRedactedAPIKey(key) {
return "", validators.ErrAPIKeyNotConfigured
}
}

Expand All @@ -160,13 +168,14 @@ func (p *Profile) GetAPIKey(livemode bool) (string, error) {
// GetExpiresAt returns the API key expirary date
func (p *Profile) GetExpiresAt(livemode bool) (time.Time, error) {
var timeString string
var err error
// var err error

if livemode {
timeString, err = p.retrieveLivemodeValue(LiveModeKeyExpiresAtName)
if err != nil {
return time.Time{}, err
}
// timeString, err = p.retrieveLivemodeValue(LiveModeKeyExpiresAtName)
// if err != nil {
// return time.Time{}, err
// }
timeString = viper.GetString(p.GetConfigField(LiveModeKeyExpiresAtName))
} else {
timeString = viper.GetString(p.GetConfigField(TestModeKeyExpiresAtName))
}
Expand Down Expand Up @@ -258,9 +267,9 @@ func (p *Profile) DeleteConfigField(field string) error {
}

// delete livemode redacted values from config and full values from keyring
if field == LiveModeAPIKeyName || field == LiveModePubKeyName || field == LiveModeKeyExpiresAtName {
p.deleteLivemodeValue(field)
}
// if field == LiveModeAPIKeyName || field == LiveModePubKeyName || field == LiveModeKeyExpiresAtName {
// p.deleteLivemodeValue(field)
// }

return p.writeProfile(v)
}
Expand All @@ -278,15 +287,19 @@ func (p *Profile) writeProfile(runtimeViper *viper.Viper) error {
}

if p.LiveModeAPIKey != "" {
expiresAt := getKeyExpiresAt()
// comment out livemode storage until bugs are ironed out
// expiresAt := getKeyExpiresAt()

// // store redacted key in config
// runtimeViper.Set(p.GetConfigField(LiveModeAPIKeyName), RedactAPIKey(strings.TrimSpace(p.LiveModeAPIKey)))
// runtimeViper.Set(p.GetConfigField(LiveModeKeyExpiresAtName), expiresAt)

// store redacted key in config
runtimeViper.Set(p.GetConfigField(LiveModeAPIKeyName), RedactAPIKey(strings.TrimSpace(p.LiveModeAPIKey)))
runtimeViper.Set(p.GetConfigField(LiveModeKeyExpiresAtName), expiresAt)
// // store actual key in secure keyring
// p.saveLivemodeValue(LiveModeAPIKeyName, strings.TrimSpace(p.LiveModeAPIKey), "Live mode API key")
// p.saveLivemodeValue(LiveModeKeyExpiresAtName, expiresAt, "Live mode API key expirary")

// store actual key in secure keyring
p.saveLivemodeValue(LiveModeAPIKeyName, strings.TrimSpace(p.LiveModeAPIKey), "Live mode API key")
p.saveLivemodeValue(LiveModeKeyExpiresAtName, expiresAt, "Live mode API key expirary")
runtimeViper.Set(p.GetConfigField(LiveModeAPIKeyName), strings.TrimSpace(p.LiveModeAPIKey))
runtimeViper.Set(p.GetConfigField(LiveModeKeyExpiresAtName), getKeyExpiresAt())
}

if p.LiveModePublishableKey != "" {
Expand Down
118 changes: 56 additions & 62 deletions pkg/config/profile_livemode.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package config

import (
"fmt"
"os"
"strings"

"github.com/99designs/keyring"
"github.com/spf13/viper"

"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/validators"
)

// DateStringFormat ...
Expand All @@ -22,68 +16,68 @@ const KeyValidInDays = 90
var KeyRing keyring.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,
})
}
// 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
}
// 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
}
// 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
// }

// redactAllLivemodeValues redacts all livemode values in the local config file
func (p *Profile) redactAllLivemodeValues() {
color := ansi.Color(os.Stdout)

if err := viper.ReadInConfig(); err == nil {
// if the config file has expires at date, then it is using the new livemode key storage
if viper.IsSet(p.GetConfigField(LiveModeAPIKeyName)) {
key := viper.GetString(p.GetConfigField(LiveModeAPIKeyName))
if !isRedactedAPIKey(key) {
fmt.Println(color.Yellow(`
(!) Livemode value found for the field '` + LiveModeAPIKeyName + `' in your config file.
Livemode values from the config file will be redacted and will not be used.`))

p.WriteConfigField(LiveModeAPIKeyName, RedactAPIKey(key))
}
}
}
}
// func (p *Profile) redactAllLivemodeValues() {
// color := ansi.Color(os.Stdout)

// if err := viper.ReadInConfig(); err == nil {
// // if the config file has expires at date, then it is using the new livemode key storage
// if viper.IsSet(p.GetConfigField(LiveModeAPIKeyName)) {
// key := viper.GetString(p.GetConfigField(LiveModeAPIKeyName))
// if !isRedactedAPIKey(key) {
// fmt.Println(color.Yellow(`
// (!) Livemode value found for the field '` + LiveModeAPIKeyName + `' in your config file.
// Livemode values from the config file will be redacted and will not be used.`))

// p.WriteConfigField(LiveModeAPIKeyName, RedactAPIKey(key))
// }
// }
// }
// }

// RedactAPIKey returns a redacted version of API keys. The first 8 and last 4
// characters are not redacted, everything else is replaced by "*" characters.
Expand Down

0 comments on commit ccdecd4

Please sign in to comment.