Skip to content

Commit

Permalink
Analyzer capitalization (#3188)
Browse files Browse the repository at this point in the history
* capitalization

* Lowercase analyze labels for the subcommand

* Canonicalize input and lowercase when matching command

* add warning

---------

Co-authored-by: Miccah Castorina <[email protected]>
  • Loading branch information
hxnyk and mcastorina authored Aug 7, 2024
1 parent a8777fc commit ab8c843
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
40 changes: 20 additions & 20 deletions pkg/analyzer/analyzers/analyzers.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,26 @@ const (
// selection. TODO: Change slice type to Analyzer interface when all available
// analyzers implement it.
var AvailableAnalyzers = []string{
"airbrake",
"asana",
"bitbucket",
"github",
"gitlab",
"huggingface",
"mailchimp",
"mailgun",
"mysql",
"openai",
"opsgenie",
"postgres",
"postman",
"sendgrid",
"shopify",
"slack",
"sourcegraph",
"square",
"stripe",
"twilio",
"Airbrake",
"Asana",
"Bitbucket",
"GitHub",
"GitLab",
"HuggingFace",
"Mailchimp",
"Mailgun",
"MySQL",
"OpenAI",
"Opsgenie",
"Postgres",
"Postman",
"Sendgrid",
"Shopify",
"Slack",
"Sourcegraph",
"Square",
"Stripe",
"Twilio",
}

type PermissionStatus struct {
Expand Down
9 changes: 7 additions & 2 deletions pkg/analyzer/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ func Command(app *kingpin.Application) *kingpin.CmdClause {
"Type of key to analyze. Omit to interactively choose. Available key types: %s",
strings.Join(analyzers.AvailableAnalyzers, ", "),
)
analyzeKeyType = cli.Arg("key-type", keyTypeHelp).Enum(analyzers.AvailableAnalyzers...)
// Lowercase the available analyzers.
availableAnalyzers := make([]string, len(analyzers.AvailableAnalyzers))
for i, a := range analyzers.AvailableAnalyzers {
availableAnalyzers[i] = strings.ToLower(a)
}
analyzeKeyType = cli.Arg("key-type", keyTypeHelp).Enum(availableAnalyzers...)

return cli
}
Expand All @@ -56,7 +61,7 @@ func Run(cmd string) {
if secretInfo.Cfg == nil {
secretInfo.Cfg = &config.Config{}
}
switch keyType {
switch strings.ToLower(keyType) {
case "github":
github.AnalyzeAndPrintPermissions(secretInfo.Cfg, secretInfo.Parts["key"])
case "sendgrid":
Expand Down
5 changes: 3 additions & 2 deletions pkg/analyzer/tui/form.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tui
import (
"fmt"
"slices"
"strings"

"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
Expand All @@ -21,7 +22,7 @@ type FormPage struct {

func NewFormPage(c *common.Common, keyType string) FormPage {
var inputs []textinputs.InputConfig
switch keyType {
switch strings.ToLower(keyType) {
case "twilio":
inputs = []textinputs.InputConfig{{
Label: "SID",
Expand Down Expand Up @@ -62,7 +63,7 @@ func NewFormPage(c *common.Common, keyType string) FormPage {

form := textinputs.New(inputs).
SetHeader(titleStyle.Render(fmt.Sprintf("Configuring %s analyzer", keyType))).
SetFooter("⚠️ Running TruffleHog Analyze will send a lot of requests ⚠️").
SetFooter("⚠️ Running TruffleHog Analyze will send a lot of requests ⚠️\n\n🚧 Please confirm you have permission to run TruffleHog Analyze against this secret 🚧").
SetSubmitMsg("Run TruffleHog Analyze")
return FormPage{
Common: c,
Expand Down
12 changes: 10 additions & 2 deletions pkg/analyzer/tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tui
import (
"errors"
"fmt"
"slices"
"strings"

tea "github.com/charmbracelet/bubbletea"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
Expand Down Expand Up @@ -31,7 +31,15 @@ var AbortError error = errors.New("command aborted")
func Run(keyType string) (string, *SecretInfo, error) {
// If a keyType is provided, make sure it's in the list of AvailableAnalyzers.
if keyType != "" {
if _, ok := slices.BinarySearch(analyzers.AvailableAnalyzers, keyType); !ok {
var found bool
for _, a := range analyzers.AvailableAnalyzers {
if strings.EqualFold(a, keyType) {
keyType = a
found = true
break
}
}
if !found {
return "", nil, fmt.Errorf("Unrecognized command %q", keyType)
}
}
Expand Down

0 comments on commit ab8c843

Please sign in to comment.