-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (167 loc) · 4.85 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// SPDX-FileCopyrightText: Copyright 2023 Stacklok
// SPDX-License-Identifier: Apache-2.0
// Package main provides a command-line tool to scan for secrets in text / files.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"io"
"os"
"strings"
"github.com/stackloklabs/secret-scanning-api/patterns"
"github.com/stackloklabs/secret-scanning-api/scanner"
)
type scanFilters struct {
enablePasswords bool
enableAPIKeys bool
enablePrivateKeys bool
}
func main() {
var (
file string
text string
showHelp bool
entropyOnly bool
maskSecrets bool
filters scanFilters
)
// File and general flags
flag.StringVar(&file, "file", "", "File to scan for secrets")
flag.StringVar(&text, "text", "", "Text to scan for secrets")
flag.BoolVar(&entropyOnly, "entropy-only", false, "Use only entropy-based detection")
flag.BoolVar(&maskSecrets, "mask", true, "Mask secrets in output")
flag.BoolVar(&showHelp, "help", false, "Show help message")
// Pattern type flags
flag.BoolVar(&filters.enablePasswords, "passwords", true, "Enable password detection")
flag.BoolVar(&filters.enableAPIKeys, "apikeys", true, "Enable API key detection")
flag.BoolVar(&filters.enablePrivateKeys, "privatekeys", true, "Enable private key detection")
flag.Parse()
if showHelp {
printUsage()
return
}
// Initialize scanner
s := scanner.New()
// Add patterns unless entropy-only mode is enabled
if !entropyOnly {
addPatternsWithFilters(s, filters)
}
var input string
var err error
switch {
case file != "":
input, err = readFile(file)
case text != "":
input = text
default:
input, err = readStdin()
}
if err != nil {
fmt.Fprintf(os.Stderr, "Error reading input: %v\n", err)
os.Exit(1)
}
// Perform scan
results, err := s.Scan(context.Background(), input)
if err != nil {
fmt.Fprintf(os.Stderr, "Error scanning: %v\n", err)
os.Exit(1)
}
// Print results
if len(results) == 0 {
fmt.Println("No secrets detected")
return
}
fmt.Printf("Found %d potential secrets:\n\n", len(results))
for i, result := range results {
fmt.Printf("%d. Type: %s\n", i+1, result.Type)
fmt.Printf(" Description: %s\n", result.Description)
fmt.Printf(" Confidence: %.2f\n", result.Confidence)
fmt.Printf(" Value: %s\n", scanner.MaskSecret(result.Value, 2)) // Updated to include exposeCount
fmt.Printf(" Position: %d-%d\n", result.StartIndex, result.EndIndex)
fmt.Printf(" Line Number: %d\n", result.LineNumber)
fmt.Println()
}
}
func addPatternsWithFilters(s *scanner.Scanner, filters scanFilters) {
if filters.enableAPIKeys {
for name, pattern := range patterns.CommonAPIPatterns {
if err := s.AddPattern(name, pattern); err != nil {
fmt.Fprintf(os.Stderr, "Error adding API pattern %s: %v\n", name, err)
}
}
}
if filters.enablePasswords {
for name, pattern := range patterns.PasswordPatterns {
if err := s.AddPattern(name, pattern); err != nil {
fmt.Fprintf(os.Stderr, "Error adding password pattern %s: %v\n", name, err)
}
}
}
if filters.enablePrivateKeys {
for name, pattern := range patterns.PrivateKeyPatterns {
if err := s.AddPattern(name, pattern); err != nil {
fmt.Fprintf(os.Stderr, "Error adding private key pattern %s: %v\n", name, err)
}
}
}
}
func readFile(path string) (string, error) {
content, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("failed to read file: %w", err)
}
return string(content), nil
}
func readStdin() (string, error) {
var builder strings.Builder
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
return "", fmt.Errorf("failed to read from stdin: %w", err)
}
builder.WriteString(line)
}
return builder.String(), nil
}
func printUsage() {
fmt.Println(`Secret Scanner - Detect potential secrets in text
Usage:
secret-scanner [options]
Options:
-file string
File to scan for secrets
-text string
Text to scan for secrets
-entropy-only
Use only entropy-based detection
-mask
Mask secrets in output (default: true)
-passwords
Enable password detection (default: true)
-apikeys
Enable API key detection (default: true)
-privatekeys
Enable private key detection (default: true)
-help
Show this help message
Examples:
# Scan a file
secret-scanner -file config.json
# Scan without password detection
secret-scanner -file config.json -passwords=false
# Scan only for API keys
secret-scanner -file config.json -passwords=false -privatekeys=false
# Scan text directly without masking
secret-scanner -text "api_key=1234567890abcdef" -mask=false
# Scan from stdin
cat config.json | secret-scanner
# Use only entropy-based detection
secret-scanner -entropy-only -file config.json
Note: Boolean flags require the '=' operator, e.g., -mask=false instead of -mask false`)
}