-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.go
152 lines (132 loc) · 3.65 KB
/
indexer.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
package eks_cert_fingerprint_indexer
import (
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
"log"
"net/url"
"path"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/ssm"
)
var (
errCnt = 0
errInvalidScheme = errors.New("Invalid URL scheme, supported schemes: [https].")
errPartialFailure = errors.New("Indexer experianced failures during run.")
)
func clusters(svc *eks.EKS) ([]string, error) {
var clusters []string
log.Println("listing clusters")
err := svc.ListClustersPages(
&eks.ListClustersInput{},
func(resp *eks.ListClustersOutput, lastPage bool) bool {
for _, cluster := range resp.Clusters {
clusters = append(clusters, *cluster)
}
return !lastPage
},
)
return clusters, err
}
func clusterOidcIssuerURL(cluster string, svc *eks.EKS) (string, error) {
log.Println("describing cluster:", cluster)
response, err := svc.DescribeCluster(
&eks.DescribeClusterInput{
Name: aws.String(cluster),
},
)
if err != nil {
return "", err
}
return *response.Cluster.Identity.Oidc.Issuer, nil
}
func fingerprintTlsCert(cert *x509.Certificate) string {
return fmt.Sprintf("%x", sha1.Sum(cert.Raw))
}
func readTlsCerts(endpointURL string, verifyCertChain bool) ([]*x509.Certificate, error) {
parsedURL, err := url.Parse(endpointURL)
if err != nil {
return nil, err
}
if parsedURL.Scheme != "https" {
return nil, errInvalidScheme
}
if parsedURL.Port() == "" {
parsedURL.Host += ":443"
}
log.Println("reading certificates from endpoint:", endpointURL)
conn, err := tls.Dial(
"tcp",
parsedURL.Host,
&tls.Config{
InsecureSkipVerify: !verifyCertChain,
},
)
if err != nil {
return nil, err
}
defer conn.Close()
return conn.ConnectionState().PeerCertificates, nil
}
func Run(config Config, eksService *eks.EKS, ssmService *ssm.SSM) error {
eksClusters, err := clusters(eksService)
if err != nil {
log.Println("error - unable to list EKS clusters, err:", err)
return err
}
for _, cluster := range eksClusters {
ssm_key := path.Join(config.SSMKeyPrefix, cluster)
if !config.SSMOverwrite {
log.Println("checking for existing SSM parameter at path:", ssm_key)
_, err := ssmService.GetParameter(&ssm.GetParameterInput{
Name: aws.String(ssm_key),
WithDecryption: aws.Bool(false),
})
if err == nil {
log.Println("SSM parameter already exists:", ssm_key)
continue
} else {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() != "ParameterNotFound" {
log.Println("error - unable to get SSM parameter:", ssm_key, "err:", err)
errCnt += 1
continue
}
}
}
}
isserUrl, err := clusterOidcIssuerURL(cluster, eksService)
if err != nil {
log.Println("error - unable to detect cluster OIDC issuer URL for cluster:", cluster)
errCnt += 1
continue
}
certs, err := readTlsCerts(isserUrl, config.VerifyCertChain)
if err != nil {
log.Println("error - unable to read certificates from endpoint:", isserUrl, "err:", err)
errCnt += 1
continue
}
certIndex := len(certs) - config.CertificateIndex - 1
certFingerprint := fingerprintTlsCert(certs[certIndex])
log.Println("setting SSM parameter:", ssm_key)
if _, err := ssmService.PutParameter(&ssm.PutParameterInput{
Name: aws.String(ssm_key),
Overwrite: aws.Bool(config.SSMOverwrite),
Type: aws.String("String"),
Value: aws.String(certFingerprint),
}); err != nil {
log.Println("error - unable to set SSM parameter:", ssm_key, "err:", err)
errCnt += 1
continue
}
}
if errCnt > 0 {
return errPartialFailure
}
return nil
}