Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (f userFlags) MarshalJSON() ([]byte, error) {
BrowserURL: f.BrowserURL,
Suites: f.Suites,
Include: f.Include,
APIKey: f.APIKey,
APIKey: obfuscateAPIKey(f.APIKey),
Region: f.Region,
Script: f.Script,
ScriptFlags: f.ScriptFlags,
Expand Down Expand Up @@ -376,3 +376,16 @@ func stringToRegion(region string) Region {
}
return NoRegion
}

// obfuscateAPIKey - returns an obfuscated version of an API key showing only the first 6 characters
func obfuscateAPIKey(apiKey string) string {
if apiKey == "" {
return ""
}
keyLen := len(apiKey)
if keyLen <= 6 {
return strings.Repeat("*", keyLen)
}

return apiKey[:6] + strings.Repeat("*", keyLen-6)
}
40 changes: 40 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,43 @@ func Test_parseRegionFlagAndEnv(t *testing.T) {
})
}
}

func Test_obfuscateAPIKey(t *testing.T) {
type args struct {
apiKey string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Empty string returns empty string",
args: args{
apiKey: "",
},
want: "",
},
{
name: "Short key returns all asterisks",
args: args{
apiKey: "abcd",
},
want: "****",
},
{
name: "Standard 32-char API key shows first 6 + 26 asterisks",
args: args{
apiKey: "THIS-ISNTAREALAPIKEYOKAYTHANKYOU",
},
want: "THIS-I**************************",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := obfuscateAPIKey(tt.args.apiKey); got != tt.want {
t.Errorf("obfuscateAPIKey() = %v, want %v", got, tt.want)
}
})
}
}
Loading