-
Notifications
You must be signed in to change notification settings - Fork 14
/
refresh.go
289 lines (261 loc) · 7.98 KB
/
refresh.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2020 Google LLC
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// https://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package alloydb
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"time"
"cloud.google.com/go/alloydbconn/errtype"
"cloud.google.com/go/alloydbconn/internal/alloydbapi"
"cloud.google.com/go/alloydbconn/internal/trace"
)
type connectInfo struct {
// ipAddr is the instance's IP addresss
ipAddr string
// uid is the instance UID
uid string
}
// fetchMetadata uses the AlloyDB Admin APIs get method to retreive the
// information about an AlloyDB instance that is used to create secure
// connections.
func fetchMetadata(ctx context.Context, cl *alloydbapi.Client, inst instanceURI) (i connectInfo, err error) {
var end trace.EndSpanFunc
ctx, end = trace.StartSpan(ctx, "cloud.google.com/go/alloydbconn/internal.FetchMetadata")
defer func() { end(err) }()
resp, err := cl.ConnectionInfo(ctx, inst.project, inst.region, inst.cluster, inst.name)
if err != nil {
return connectInfo{}, errtype.NewRefreshError("failed to get instance metadata", inst.String(), err)
}
return connectInfo{ipAddr: resp.IPAddress, uid: resp.InstanceUID}, nil
}
var errInvalidPEM = errors.New("certificate is not a valid PEM")
func parseCert(cert string) (*x509.Certificate, error) {
b, _ := pem.Decode([]byte(cert))
if b == nil {
return nil, errInvalidPEM
}
return x509.ParseCertificate(b.Bytes)
}
// fetchEphemeralCert uses the AlloyDB Admin API's generateClientCertificate
// method to create a signed TLS certificate that authorized to connect via the
// AlloyDB instance's serverside proxy. The cert is valid for twenty four hours.
func fetchEphemeralCert(
ctx context.Context,
cl *alloydbapi.Client,
inst instanceURI,
key *rsa.PrivateKey,
) (cc certChain, err error) {
var end trace.EndSpanFunc
ctx, end = trace.StartSpan(ctx, "cloud.google.com/go/alloydbconn/internal.FetchEphemeralCert")
defer func() { end(err) }()
subj := pkix.Name{
CommonName: "alloydb-proxy",
Country: []string{"US"},
Province: []string{"CA"},
Locality: []string{"Sunnyvale"},
Organization: []string{"Google LLC"},
OrganizationalUnit: []string{"Cloud"},
}
tmpl := x509.CertificateRequest{
Subject: subj,
SignatureAlgorithm: x509.SHA256WithRSA,
}
csrBytes, err := x509.CreateCertificateRequest(rand.Reader, &tmpl, key)
if err != nil {
return certChain{}, err
}
buf := &bytes.Buffer{}
pem.Encode(buf, &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrBytes})
resp, err := cl.GenerateClientCert(ctx, inst.project, inst.region, inst.cluster, buf.Bytes())
if err != nil {
return certChain{}, errtype.NewRefreshError(
"create ephemeral cert failed",
inst.String(),
err,
)
}
// There should always be two certs in the chain. If this fails, the API has
// broken its contract with the client.
if len(resp.PemCertificateChain) != 2 {
return certChain{}, errtype.NewRefreshError(
"missing instance and root certificates",
inst.String(),
err,
)
}
rc, err := parseCert(resp.PemCertificateChain[1]) // root cert
if err != nil {
return certChain{}, errtype.NewRefreshError(
"failed to parse root cert",
inst.String(),
err,
)
}
ic, err := parseCert(resp.PemCertificateChain[0]) // intermediate cert
if err != nil {
return certChain{}, errtype.NewRefreshError(
"failed to parse intermediate cert",
inst.String(),
err,
)
}
c, err := parseCert(resp.PemCertificate) // client cert
if err != nil {
return certChain{}, errtype.NewRefreshError(
"failed to parse client cert",
inst.String(),
err,
)
}
return certChain{
root: rc,
intermediate: ic,
client: c,
}, nil
}
// createTLSConfig returns a *tls.Config for connecting securely to the AlloyDB
// instance.
func createTLSConfig(inst instanceURI, cc certChain, info connectInfo, k *rsa.PrivateKey) *tls.Config {
certs := x509.NewCertPool()
certs.AddCert(cc.root)
return &tls.Config{
InsecureSkipVerify: true,
VerifyPeerCertificate: func(rawCerts [][]byte, _ [][]*x509.Certificate) error {
var parsed []*x509.Certificate
for _, r := range rawCerts {
c, err := x509.ParseCertificate(r)
if err != nil {
return errtype.NewDialError("failed to parse X.509 certificate", inst.String(), err)
}
parsed = append(parsed, c)
}
server := parsed[0]
inter := x509.NewCertPool()
for i := 1; i < len(parsed); i++ {
inter.AddCert(parsed[i])
}
opts := x509.VerifyOptions{Roots: certs, Intermediates: inter}
if _, err := server.Verify(opts); err != nil {
return errtype.NewDialError("failed to verify certificate", inst.String(), err)
}
serverName := fmt.Sprintf("%v.server.alloydb", info.uid)
if server.Subject.CommonName != serverName {
return errtype.NewDialError(
fmt.Sprintf("certificate had CN %q, expected %q",
server.Subject.CommonName, serverName),
inst.String(),
nil,
)
}
return nil
},
Certificates: []tls.Certificate{tls.Certificate{
Certificate: [][]byte{cc.client.Raw, cc.intermediate.Raw},
PrivateKey: k,
Leaf: cc.client,
}},
RootCAs: certs,
MinVersion: tls.VersionTLS13,
}
}
// newRefresher creates a Refresher.
func newRefresher(
client *alloydbapi.Client,
dialerID string,
) refresher {
return refresher{
client: client,
dialerID: dialerID,
}
}
// refresher manages the AlloyDB Admin API access to instance metadata and to
// ephemeral certificates.
type refresher struct {
// client provides access to the AlloyDB Admin API
client *alloydbapi.Client
// dialerID is the unique ID of the associated dialer.
dialerID string
}
type refreshResult struct {
instanceIPAddr string
conf *tls.Config
expiry time.Time
}
type certChain struct {
root *x509.Certificate
intermediate *x509.Certificate
client *x509.Certificate
}
func (r refresher) performRefresh(ctx context.Context, cn instanceURI, k *rsa.PrivateKey) (res refreshResult, err error) {
var refreshEnd trace.EndSpanFunc
ctx, refreshEnd = trace.StartSpan(ctx, "cloud.google.com/go/alloydbconn/internal.RefreshConnection",
trace.AddInstanceName(cn.String()),
)
defer func() {
go trace.RecordRefreshResult(context.Background(), cn.String(), r.dialerID, err)
refreshEnd(err)
}()
type mdRes struct {
info connectInfo
err error
}
mdCh := make(chan mdRes, 1)
go func() {
defer close(mdCh)
c, err := fetchMetadata(ctx, r.client, cn)
mdCh <- mdRes{info: c, err: err}
}()
type certRes struct {
cc certChain
err error
}
certCh := make(chan certRes, 1)
go func() {
defer close(certCh)
cc, err := fetchEphemeralCert(ctx, r.client, cn, k)
certCh <- certRes{cc: cc, err: err}
}()
var info connectInfo
select {
case r := <-mdCh:
if r.err != nil {
return refreshResult{}, fmt.Errorf("failed to get instance IP address: %w", r.err)
}
info = r.info
case <-ctx.Done():
return refreshResult{}, fmt.Errorf("refresh failed: %w", ctx.Err())
}
var cc certChain
select {
case r := <-certCh:
if r.err != nil {
return refreshResult{}, fmt.Errorf("fetch ephemeral cert failed: %w", r.err)
}
cc = r.cc
case <-ctx.Done():
return refreshResult{}, fmt.Errorf("refresh failed: %w", ctx.Err())
}
c := createTLSConfig(cn, cc, info, k)
var expiry time.Time
// This should never not be the case, but we check to avoid a potential nil-pointer
if len(c.Certificates) > 0 {
expiry = c.Certificates[0].Leaf.NotAfter
}
return refreshResult{instanceIPAddr: info.ipAddr, conf: c, expiry: expiry}, nil
}