-
Notifications
You must be signed in to change notification settings - Fork 24
/
certs.go
76 lines (62 loc) · 1.58 KB
/
certs.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
package gcpjwt
import (
"context"
"crypto/rsa"
"encoding/json"
"io/ioutil"
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/pquerna/cachecontrol"
)
const (
certificateURL = "https://www.googleapis.com/robot/v1/metadata/x509/"
)
// certificates is a map of key id -> public keys
type certificates map[string]*rsa.PublicKey
func getCertificates(ctx context.Context, config *IAMConfig) (certificates, error) {
if config.EnableCache {
if certsResp, ok := getCertsFromCache(config.ServiceAccount); ok {
return certsResp, nil
}
}
// Default config.Client is a http.DefaultClient
client := config.Client
if client == nil {
client = getDefaultClient(ctx)
}
req, err := http.NewRequest(http.MethodGet, certificateURL+config.ServiceAccount, nil)
if err != nil {
return nil, err
}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
certsRaw := make(map[string]string)
err = json.Unmarshal(b, &certsRaw)
if err != nil {
return nil, err
}
_, expires, err := cachecontrol.CachableResponse(req, resp, cachecontrol.Options{PrivateCache: true})
if err != nil && config.CacheExpiration > 0 {
expires = time.Now().Add(config.CacheExpiration)
}
certs := make(certificates)
for key, cert := range certsRaw {
rsaKey, err := jwt.ParseRSAPublicKeyFromPEM([]byte(cert))
if err != nil {
return nil, err
}
certs[key] = rsaKey
}
if config.EnableCache && !expires.IsZero() {
updateCache(config.ServiceAccount, certs, expires)
}
return certs, nil
}