Skip to content

Commit 73eed2c

Browse files
Merge pull request #6 from warpstreamlabs/ra/scan-secrets
Add secret scan workflow
2 parents 6c23469 + 381242f commit 73eed2c

2 files changed

Lines changed: 111 additions & 0 deletions

File tree

.github/workflows/secret-scan.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Secret scanning workflow.
2+
name: Secret Scan
3+
4+
on:
5+
pull_request:
6+
push:
7+
branches:
8+
- master
9+
10+
permissions:
11+
contents: read
12+
13+
jobs:
14+
secret-scan:
15+
name: Secret Scan
16+
runs-on: ubuntu-latest
17+
timeout-minutes: 5
18+
steps:
19+
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1
20+
- uses: actions/setup-go@93397bea11091df50f3d7e59dc26a7711a8bcfbe # v4.1.0
21+
with:
22+
go-version: '1.24.x'
23+
cache: false
24+
- name: Scan for hard-coded secrets
25+
run: go run ./scripts/secret_scan/main.go -files="$(git ls-files | tr '\n' ',')"

scripts/secret_scan/main.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"regexp"
9+
"strings"
10+
)
11+
12+
var (
13+
prefixes = []string{
14+
// API Key.
15+
"aks_[0-9a-f]{20,}",
16+
// Cluster credentials password.
17+
"ccp_[0-9a-f]{20,}",
18+
// Session key secret.
19+
"sks_[0-9a-f]{20,}",
20+
}
21+
22+
allowedKeys = map[string]struct{}{}
23+
)
24+
25+
func main() {
26+
filesFlag := flag.String("files", "", "Comma-separated list of files to scan")
27+
flag.Parse()
28+
29+
if *filesFlag == "" {
30+
fmt.Println("Usage: scanner -files=<comma-separated-files>")
31+
os.Exit(1)
32+
}
33+
34+
var (
35+
files = strings.Split(*filesFlag, ",")
36+
prefixPattern = strings.Join(prefixes, "|")
37+
regex = regexp.MustCompile(prefixPattern)
38+
)
39+
40+
hasIssues := false
41+
for _, file := range files {
42+
if file == "" {
43+
continue
44+
}
45+
46+
f, err := os.Open(file)
47+
if err != nil {
48+
fmt.Printf("Error opening file %s: %v\n", file, err)
49+
os.Exit(1)
50+
}
51+
52+
scanner := bufio.NewScanner(f)
53+
lineNumber := 1
54+
for scanner.Scan() {
55+
line := scanner.Text()
56+
matches := regex.FindAllString(line, -1)
57+
for _, match := range matches {
58+
if _, allowed := allowedKeys[match]; allowed {
59+
continue
60+
}
61+
62+
fmt.Printf("Found illegal prefix (potential secret?) in %s at line %d: %s\n", file, lineNumber, line)
63+
hasIssues = true
64+
}
65+
lineNumber++
66+
}
67+
68+
if err := scanner.Err(); err != nil {
69+
fmt.Printf("Error reading file %s: %v\n", file, err)
70+
if closeErr := f.Close(); closeErr != nil {
71+
fmt.Printf("Error closing file %s: %v\n", file, closeErr)
72+
}
73+
os.Exit(1)
74+
}
75+
76+
if err := f.Close(); err != nil {
77+
fmt.Printf("Error closing file %s: %v\n", file, err)
78+
os.Exit(1)
79+
}
80+
}
81+
82+
if hasIssues {
83+
fmt.Println("Illegal prefixes (potential secret?) found.")
84+
os.Exit(1)
85+
}
86+
}

0 commit comments

Comments
 (0)