-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (128 loc) · 3.58 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
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sts"
)
func appendIfNotExists(slice []string, item string) []string {
for _, el := range slice {
if el == item {
return slice
}
}
return append(slice, item)
}
func main() {
// Get repository link from command-line arguments
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <repository link>")
os.Exit(1)
}
repoLink := os.Args[1]
// Clone repository
dir, err := filepath.Abs(filepath.Base(repoLink))
if err != nil {
fmt.Println("Error: Failed to get absolute path for directory")
os.Exit(1)
}
cmd := exec.Command("git", "clone", repoLink, dir)
if err := cmd.Run(); err != nil {
fmt.Printf("Error: Failed to clone repository %s\n", repoLink)
os.Exit(1)
}
// Navigate into directory
if err := os.Chdir(dir); err != nil {
fmt.Println("Error: Failed to navigate to directory")
os.Exit(1)
}
// Use git log -p output
cmd = exec.Command("git", "log", "-p")
out, err := cmd.Output()
if err != nil {
fmt.Println("Error: Failed to run git log -p")
os.Exit(1)
}
access_regex := regexp.MustCompile("AKIA[0-9A-Z]{16}")
access_key_matches := make([]string, 0)
secret_regex := regexp.MustCompile("[0-9a-zA-Z/+]{40}")
secret_key_matches := make([]string, 0)
reader := bytes.NewReader(out)
scanner := bufio.NewScanner(reader)
// Scan for access keys and secret keys
for scanner.Scan() {
line := scanner.Text()
matches := access_regex.FindAllString(line, -1)
for _, match := range matches {
access_key_matches = appendIfNotExists(access_key_matches, match)
}
matches = secret_regex.FindAllString(line, -1)
for _, match := range matches {
secret_key_matches = appendIfNotExists(secret_key_matches, match)
}
}
fmt.Print("Printing access keys and secret keys, if any...\n\n")
// Check all combinations of access keys and secret keys and print the valid ones
if len(access_key_matches) > 0 && len(secret_key_matches) > 0 {
for _, match := range access_key_matches {
for _, match2 := range secret_key_matches {
checkKeys(match, match2)
}
}
}
fmt.Print("\nPrinting access keys, if any...\n\n")
// Print the valid access keys
if len(access_key_matches) > 0 {
for _, match := range access_key_matches {
checkAccessKeys(match)
}
}
}
// Func to check if access key is valid
func checkAccessKeys(accessKeyID string) {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
})
if err != nil {
fmt.Println("Error creating AWS session: ", err)
return
}
svc := sts.New(sess)
// Call GetAccessKeyInfo API
input := &sts.GetAccessKeyInfoInput{
AccessKeyId: &accessKeyID,
}
_, err = svc.GetAccessKeyInfo(input)
if err != nil {
return
}
fmt.Println(accessKeyID, "is an access key present in the repository")
}
// Func to check if key pair is valid
func checkKeys(accessKeyID string, secretToken string) {
creds := credentials.NewStaticCredentials(accessKeyID, secretToken, "")
// Create a new session with AWS credentials
sess, err := session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
Credentials: creds,
})
if err != nil {
fmt.Println("Error creating AWS session: ", err)
return
}
// Create a new STS client
svc := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
_, err = svc.GetCallerIdentity(input)
if err != nil {
return
}
fmt.Println(accessKeyID, "and", secretToken, "is a key pair present in the repository")
}