Skip to content

Commit 991a57c

Browse files
author
Katrina Owen
authored
Merge pull request #358 from exercism/debug-api-key
Redact API key from debug output.
2 parents c5b1921 + dc9b74c commit 991a57c

2 files changed

Lines changed: 49 additions & 23 deletions

File tree

cmd/debug.go

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"net/http"
77
"os"
88
"runtime"
9+
"strings"
910
"sync"
1011
"time"
1112

12-
"github.com/urfave/cli"
1313
"github.com/exercism/cli/config"
1414
"github.com/exercism/cli/paths"
15+
"github.com/urfave/cli"
1516
)
1617

1718
type pingResult struct {
@@ -53,27 +54,9 @@ func Debug(ctx *cli.Context) error {
5354
log.Fatal(err)
5455
}
5556

56-
configured := true
57-
if _, err = os.Stat(c.File); err != nil {
58-
if os.IsNotExist(err) {
59-
configured = false
60-
} else {
61-
log.Fatal(err)
62-
}
63-
}
64-
65-
if configured {
66-
fmt.Printf("Config file: %s\n", c.File)
67-
if c.APIKey != "" {
68-
fmt.Printf("API Key: %s\n", c.APIKey)
69-
} else {
70-
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
71-
}
72-
} else {
73-
fmt.Printf("Config file: %s (not configured)\n", c.File)
74-
fmt.Println("API Key: Please set your API Key to access all of the CLI features")
57+
if err := printConfigFileData(ctx, c); err != nil {
58+
log.Fatal(err)
7559
}
76-
fmt.Printf("Exercises Directory: %s\n", c.Dir)
7760

7861
fmt.Println("Testing API endpoints reachability")
7962

@@ -131,3 +114,40 @@ func Debug(ctx *cli.Context) error {
131114

132115
return nil
133116
}
117+
118+
func printConfigFileData(ctx *cli.Context, cfg *config.Config) error {
119+
configured := true
120+
if _, err := os.Stat(cfg.File); err != nil {
121+
if os.IsNotExist(err) {
122+
configured = false
123+
} else {
124+
return err
125+
}
126+
}
127+
128+
apiKey := "Please set your API key to access all of the CLI features"
129+
configFile := fmt.Sprintf("%s (not configured)", cfg.File)
130+
131+
if configured {
132+
configFile = cfg.File
133+
if cfg.APIKey != "" {
134+
if ctx.Bool("full-api-key") {
135+
apiKey = cfg.APIKey
136+
} else {
137+
apiKey = redactAPIKey(cfg.APIKey)
138+
}
139+
}
140+
}
141+
142+
fmt.Printf("Config File: %s\n", configFile)
143+
fmt.Printf("API Key: %s\n", apiKey)
144+
fmt.Printf("Exercises Directory: %s\n", cfg.Dir)
145+
146+
return nil
147+
}
148+
149+
func redactAPIKey(apiKey string) string {
150+
str := apiKey[4 : len(apiKey)-3]
151+
redaction := strings.Repeat("*", len(str))
152+
return string(apiKey[:4]) + redaction + string(apiKey[len(apiKey)-3:])
153+
}

exercism/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ func main() {
8585
Action: cmd.Configure,
8686
},
8787
{
88-
Name: "debug",
89-
Usage: descDebug,
88+
Name: "debug",
89+
Usage: descDebug,
90+
Flags: []cli.Flag{
91+
cli.BoolFlag{
92+
Name: "full-api-key",
93+
Usage: "Displays the full API key without obfuscating it",
94+
},
95+
},
9096
Action: cmd.Debug,
9197
},
9298
{

0 commit comments

Comments
 (0)