-
Notifications
You must be signed in to change notification settings - Fork 389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
show warning for old livemode values #919
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
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" | ||
) | ||
|
||
|
@@ -17,8 +21,8 @@ const KeyValidInDays = 90 | |
// KeyRing ... | ||
var KeyRing keyring.Keyring | ||
|
||
// storeLivemodeValue | ||
func (p *Profile) storeLivemodeValue(field, value, description string) { | ||
// 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, | ||
|
@@ -28,8 +32,8 @@ func (p *Profile) storeLivemodeValue(field, value, description string) { | |
}) | ||
} | ||
|
||
// RetrieveLivemodeValue ... | ||
func (p *Profile) RetrieveLivemodeValue(key string) (string, error) { | ||
// 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 { | ||
|
@@ -46,8 +50,8 @@ func (p *Profile) RetrieveLivemodeValue(key string) (string, error) { | |
return "", validators.ErrAPIKeyNotConfigured | ||
} | ||
|
||
// DeleteLivemodeValue ... | ||
func (p *Profile) DeleteLivemodeValue(key string) error { | ||
// 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 { | ||
|
@@ -62,6 +66,25 @@ func (p *Profile) DeleteLivemodeValue(key string) error { | |
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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we redact LiveModePubKeyName too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i did that at first but tomer said we should leave out publishable key since its public facing, which makes sense |
||
} | ||
} | ||
} | ||
} | ||
|
||
// RedactAPIKey returns a redacted version of API keys. The first 8 and last 4 | ||
// characters are not redacted, everything else is replaced by "*" characters. | ||
// | ||
|
@@ -76,8 +99,8 @@ func RedactAPIKey(apiKey string) string { | |
return b.String() | ||
} | ||
|
||
// IsRedactedAPIKey ... | ||
func IsRedactedAPIKey(apiKey string) bool { | ||
// isRedactedAPIKey checks if the input string is a refacted api key | ||
func isRedactedAPIKey(apiKey string) bool { | ||
keyParts := strings.Split(apiKey, "_") | ||
if len(keyParts) < 3 { | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the values already be redacted by this point? Is this call defensive?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes its a defensive check in case user manually edits the config tom file again