Skip to content

Commit

Permalink
Merge analyze tui with trufflehog tui (#3735)
Browse files Browse the repository at this point in the history
* Move pkg/analyzer/tui to pkg/tui/analyzer

* [WIP] Wire up arguments into the TUI package

* WIP - moving analyze inside tui

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* [WIP] Analyze page

Co-authored-by: hxnyk <8292703+hxnyk@users.noreply.github.com>

* Add SetSize to KeyTypePage

* Use pageHistory stack for proper history traversal

Co-authored-by: hxnyk <8292703+hxnyk@users.noreply.github.com>

* Ongoing work - separate analyze pages

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* Set size on list

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* Pass analyzer as message

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* fix args for analyze

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* Actually run the analysis

Co-authored-by: hxnyk <8292703+hxnyk@users.noreply.github.com>

* Remove analyze_old

Co-authored-by: hxnyk <8292703+hxnyk@users.noreply.github.com>

* remove old comments

Co-authored-by: mcastorina <m.castorina93@gmail.com>

* Remove unused analyzeKeyType

---------

Co-authored-by: Miccah Castorina <m.castorina93@gmail.com>
  • Loading branch information
hxnyk and mcastorina authored Feb 7, 2025
1 parent ad8fd36 commit 03ca8aa
Show file tree
Hide file tree
Showing 6 changed files with 226 additions and 265 deletions.
92 changes: 51 additions & 41 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ func init() {
// Support -h for help
cli.HelpFlag.Short('h')

if len(os.Args) <= 1 && isatty.IsTerminal(os.Stdout.Fd()) {
args := tui.Run()
if isatty.IsTerminal(os.Stdout.Fd()) && (len(os.Args) <= 1 || os.Args[1] == analyzeCmd.FullCommand()) {
args := tui.Run(os.Args[1:])
if len(args) == 0 {
os.Exit(0)
}
Expand Down Expand Up @@ -520,46 +520,56 @@ func run(state overseer.State) {
return
}

topLevelSubCommand, _, _ := strings.Cut(cmd, " ")
switch topLevelSubCommand {
case analyzeCmd.FullCommand():
analyzer.Run(cmd)
default:
metrics, err := runSingleScan(ctx, cmd, engConf)
if err != nil {
logFatal(err, "error running scan")
}

verificationCacheMetrics := struct {
Hits int32
Misses int32
HitsWasted int32
AttemptsSaved int32
VerificationTimeSpentMS int64
}{
Hits: verificationCacheMetrics.ResultCacheHits.Load(),
Misses: verificationCacheMetrics.ResultCacheMisses.Load(),
HitsWasted: verificationCacheMetrics.ResultCacheHitsWasted.Load(),
AttemptsSaved: verificationCacheMetrics.CredentialVerificationsSaved.Load(),
VerificationTimeSpentMS: verificationCacheMetrics.FromDataVerifyTimeSpentMS.Load(),
}

// Print results.
logger.Info("finished scanning",
"chunks", metrics.ChunksScanned,
"bytes", metrics.BytesScanned,
"verified_secrets", metrics.VerifiedSecretsFound,
"unverified_secrets", metrics.UnverifiedSecretsFound,
"scan_duration", metrics.ScanDuration.String(),
"trufflehog_version", version.BuildVersion,
"verification_caching", verificationCacheMetrics,
)

if metrics.hasFoundResults && *fail {
logger.V(2).Info("exiting with code 183 because results were found")
os.Exit(183)
}
metrics, err := runSingleScan(ctx, cmd, engConf)
if err != nil {
logFatal(err, "error running scan")
}

verificationCacheMetricsSnapshot := struct {
Hits int32
Misses int32
HitsWasted int32
AttemptsSaved int32
VerificationTimeSpentMS int64
}{
Hits: verificationCacheMetrics.ResultCacheHits.Load(),
Misses: verificationCacheMetrics.ResultCacheMisses.Load(),
HitsWasted: verificationCacheMetrics.ResultCacheHitsWasted.Load(),
AttemptsSaved: verificationCacheMetrics.CredentialVerificationsSaved.Load(),
VerificationTimeSpentMS: verificationCacheMetrics.FromDataVerifyTimeSpentMS.Load(),
}

// Print results.
logger.Info("finished scanning",
"chunks", metrics.ChunksScanned,
"bytes", metrics.BytesScanned,
"verified_secrets", metrics.VerifiedSecretsFound,
"unverified_secrets", metrics.UnverifiedSecretsFound,
"scan_duration", metrics.ScanDuration.String(),
"trufflehog_version", version.BuildVersion,
"verification_caching", verificationCacheMetricsSnapshot,
)

if metrics.hasFoundResults && *fail {
logger.V(2).Info("exiting with code 183 because results were found")
os.Exit(183)
}

// Print results.
logger.Info("finished scanning",
"chunks", metrics.ChunksScanned,
"bytes", metrics.BytesScanned,
"verified_secrets", metrics.VerifiedSecretsFound,
"unverified_secrets", metrics.UnverifiedSecretsFound,
"scan_duration", metrics.ScanDuration.String(),
"trufflehog_version", version.BuildVersion,
)

if metrics.hasFoundResults && *fail {
logger.V(2).Info("exiting with code 183 because results were found")
os.Exit(183)
}

}

func compareScans(ctx context.Context, cmd string, cfg engine.Config) error {
Expand Down
33 changes: 6 additions & 27 deletions pkg/analyzer/cli.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package analyzer

import (
"fmt"
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/airbrake"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/asana"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/bitbucket"
Expand All @@ -28,37 +26,18 @@ import (
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/stripe"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/twilio"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config"
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/tui"
)

var (
// TODO: Add list of supported key types.
analyzeKeyType *string
)
type SecretInfo struct {
Parts map[string]string
Cfg *config.Config
}

func Command(app *kingpin.Application) *kingpin.CmdClause {
cli := app.Command("analyze", "Analyze API keys for fine-grained permissions information.")

keyTypeHelp := fmt.Sprintf(
"Type of key to analyze. Omit to interactively choose. Available key types: %s",
strings.Join(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
return app.Command("analyze", "Analyze API keys for fine-grained permissions information.")
}

func Run(cmd string) {
keyType, secretInfo, err := tui.Run(*analyzeKeyType)
if err != nil {
// TODO: Log error.
return
}
func Run(keyType string, secretInfo SecretInfo) {
if secretInfo.Cfg == nil {
secretInfo.Cfg = &config.Config{}
}
Expand Down
122 changes: 0 additions & 122 deletions pkg/analyzer/tui/tui.go

This file was deleted.

Loading

0 comments on commit 03ca8aa

Please # to comment.