-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathawsValidator.go
65 lines (50 loc) · 1.61 KB
/
awsValidator.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
package main
import (
"fmt"
"regexp"
"strings"
"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/iam"
)
const awsKeyPattern = `(?m)(?i)AKIA[0-9A-Z]{16}\s+\S{40}|AWS[0-9A-Z]{38}\s+?\S{40}`
// Struct
type awsValidator struct{}
// Implementatig Match method
func (a awsValidator) Match(content string) ([]CloudCredentials, error) {
res := []CloudCredentials{}
regex := regexp.MustCompile(awsKeyPattern)
matches := regex.FindAllString(string(content), -1)
for _, match := range matches {
matchArr := regexp.MustCompile(`[^\S]+`).Split(match, 2)
res = append(res, CloudCredentials{
Id: matchArr[0],
Secret: matchArr[1],
})
}
return res, nil
}
// Implemenation of Validate
func (a awsValidator) Validate(c CloudCredentials) bool {
return validateIAMKeys(c.Id, c.Secret)
}
// Implemenation of ValidateIAM keys
func validateIAMKeys(accessKeyID, secretAccessKey string) bool {
// Create a new AWS session with the IAM keys
sess, _ := session.NewSession(&aws.Config{
Region: aws.String("ap-south-1"),
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""),
})
// Create a new iam service client using the session
svc := iam.New(sess)
// Basic API call to check the IAM keys' validity
d, err := svc.ListGroups(&iam.ListGroupsInput{})
if err != nil {
// InvalidClientTokenId error occurs for invalid keys.
return !strings.Contains(err.Error(), "InvalidClientTokenId")
}
fmt.Print(d)
// IAM keys are valid and the role has permission to list groups
return true
}