Skip to content

Commit 03ca8aa

Browse files
hxnykmcastorina
andauthored
Merge analyze tui with trufflehog tui (#3735)
* 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 <[email protected]> * [WIP] Analyze page Co-authored-by: hxnyk <[email protected]> * Add SetSize to KeyTypePage * Use pageHistory stack for proper history traversal Co-authored-by: hxnyk <[email protected]> * Ongoing work - separate analyze pages Co-authored-by: mcastorina <[email protected]> * Set size on list Co-authored-by: mcastorina <[email protected]> * Pass analyzer as message Co-authored-by: mcastorina <[email protected]> * fix args for analyze Co-authored-by: mcastorina <[email protected]> * Actually run the analysis Co-authored-by: hxnyk <[email protected]> * Remove analyze_old Co-authored-by: hxnyk <[email protected]> * remove old comments Co-authored-by: mcastorina <[email protected]> * Remove unused analyzeKeyType --------- Co-authored-by: Miccah Castorina <[email protected]>
1 parent ad8fd36 commit 03ca8aa

File tree

6 files changed

+226
-265
lines changed

6 files changed

+226
-265
lines changed

main.go

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -269,8 +269,8 @@ func init() {
269269
// Support -h for help
270270
cli.HelpFlag.Short('h')
271271

272-
if len(os.Args) <= 1 && isatty.IsTerminal(os.Stdout.Fd()) {
273-
args := tui.Run()
272+
if isatty.IsTerminal(os.Stdout.Fd()) && (len(os.Args) <= 1 || os.Args[1] == analyzeCmd.FullCommand()) {
273+
args := tui.Run(os.Args[1:])
274274
if len(args) == 0 {
275275
os.Exit(0)
276276
}
@@ -520,46 +520,56 @@ func run(state overseer.State) {
520520
return
521521
}
522522

523-
topLevelSubCommand, _, _ := strings.Cut(cmd, " ")
524-
switch topLevelSubCommand {
525-
case analyzeCmd.FullCommand():
526-
analyzer.Run(cmd)
527-
default:
528-
metrics, err := runSingleScan(ctx, cmd, engConf)
529-
if err != nil {
530-
logFatal(err, "error running scan")
531-
}
532-
533-
verificationCacheMetrics := struct {
534-
Hits int32
535-
Misses int32
536-
HitsWasted int32
537-
AttemptsSaved int32
538-
VerificationTimeSpentMS int64
539-
}{
540-
Hits: verificationCacheMetrics.ResultCacheHits.Load(),
541-
Misses: verificationCacheMetrics.ResultCacheMisses.Load(),
542-
HitsWasted: verificationCacheMetrics.ResultCacheHitsWasted.Load(),
543-
AttemptsSaved: verificationCacheMetrics.CredentialVerificationsSaved.Load(),
544-
VerificationTimeSpentMS: verificationCacheMetrics.FromDataVerifyTimeSpentMS.Load(),
545-
}
546-
547-
// Print results.
548-
logger.Info("finished scanning",
549-
"chunks", metrics.ChunksScanned,
550-
"bytes", metrics.BytesScanned,
551-
"verified_secrets", metrics.VerifiedSecretsFound,
552-
"unverified_secrets", metrics.UnverifiedSecretsFound,
553-
"scan_duration", metrics.ScanDuration.String(),
554-
"trufflehog_version", version.BuildVersion,
555-
"verification_caching", verificationCacheMetrics,
556-
)
557-
558-
if metrics.hasFoundResults && *fail {
559-
logger.V(2).Info("exiting with code 183 because results were found")
560-
os.Exit(183)
561-
}
523+
metrics, err := runSingleScan(ctx, cmd, engConf)
524+
if err != nil {
525+
logFatal(err, "error running scan")
526+
}
527+
528+
verificationCacheMetricsSnapshot := struct {
529+
Hits int32
530+
Misses int32
531+
HitsWasted int32
532+
AttemptsSaved int32
533+
VerificationTimeSpentMS int64
534+
}{
535+
Hits: verificationCacheMetrics.ResultCacheHits.Load(),
536+
Misses: verificationCacheMetrics.ResultCacheMisses.Load(),
537+
HitsWasted: verificationCacheMetrics.ResultCacheHitsWasted.Load(),
538+
AttemptsSaved: verificationCacheMetrics.CredentialVerificationsSaved.Load(),
539+
VerificationTimeSpentMS: verificationCacheMetrics.FromDataVerifyTimeSpentMS.Load(),
540+
}
541+
542+
// Print results.
543+
logger.Info("finished scanning",
544+
"chunks", metrics.ChunksScanned,
545+
"bytes", metrics.BytesScanned,
546+
"verified_secrets", metrics.VerifiedSecretsFound,
547+
"unverified_secrets", metrics.UnverifiedSecretsFound,
548+
"scan_duration", metrics.ScanDuration.String(),
549+
"trufflehog_version", version.BuildVersion,
550+
"verification_caching", verificationCacheMetricsSnapshot,
551+
)
552+
553+
if metrics.hasFoundResults && *fail {
554+
logger.V(2).Info("exiting with code 183 because results were found")
555+
os.Exit(183)
562556
}
557+
558+
// Print results.
559+
logger.Info("finished scanning",
560+
"chunks", metrics.ChunksScanned,
561+
"bytes", metrics.BytesScanned,
562+
"verified_secrets", metrics.VerifiedSecretsFound,
563+
"unverified_secrets", metrics.UnverifiedSecretsFound,
564+
"scan_duration", metrics.ScanDuration.String(),
565+
"trufflehog_version", version.BuildVersion,
566+
)
567+
568+
if metrics.hasFoundResults && *fail {
569+
logger.V(2).Info("exiting with code 183 because results were found")
570+
os.Exit(183)
571+
}
572+
563573
}
564574

565575
func compareScans(ctx context.Context, cmd string, cfg engine.Config) error {

pkg/analyzer/cli.go

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package analyzer
22

33
import (
4-
"fmt"
54
"strings"
65

76
"github.com/alecthomas/kingpin/v2"
8-
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers"
97
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/airbrake"
108
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/asana"
119
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/bitbucket"
@@ -28,37 +26,18 @@ import (
2826
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/stripe"
2927
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/analyzers/twilio"
3028
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/config"
31-
"github.com/trufflesecurity/trufflehog/v3/pkg/analyzer/tui"
3229
)
3330

34-
var (
35-
// TODO: Add list of supported key types.
36-
analyzeKeyType *string
37-
)
31+
type SecretInfo struct {
32+
Parts map[string]string
33+
Cfg *config.Config
34+
}
3835

3936
func Command(app *kingpin.Application) *kingpin.CmdClause {
40-
cli := app.Command("analyze", "Analyze API keys for fine-grained permissions information.")
41-
42-
keyTypeHelp := fmt.Sprintf(
43-
"Type of key to analyze. Omit to interactively choose. Available key types: %s",
44-
strings.Join(analyzers.AvailableAnalyzers(), ", "),
45-
)
46-
// Lowercase the available analyzers.
47-
availableAnalyzers := make([]string, len(analyzers.AvailableAnalyzers()))
48-
for i, a := range analyzers.AvailableAnalyzers() {
49-
availableAnalyzers[i] = strings.ToLower(a)
50-
}
51-
analyzeKeyType = cli.Arg("key-type", keyTypeHelp).Enum(availableAnalyzers...)
52-
53-
return cli
37+
return app.Command("analyze", "Analyze API keys for fine-grained permissions information.")
5438
}
5539

56-
func Run(cmd string) {
57-
keyType, secretInfo, err := tui.Run(*analyzeKeyType)
58-
if err != nil {
59-
// TODO: Log error.
60-
return
61-
}
40+
func Run(keyType string, secretInfo SecretInfo) {
6241
if secretInfo.Cfg == nil {
6342
secretInfo.Cfg = &config.Config{}
6443
}

pkg/analyzer/tui/tui.go

Lines changed: 0 additions & 122 deletions
This file was deleted.

0 commit comments

Comments
 (0)