-
Notifications
You must be signed in to change notification settings - Fork 29
/
refresh.go
371 lines (340 loc) · 10.3 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// 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 cloudsql
import (
"context"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"encoding/pem"
"fmt"
"strings"
"time"
"cloud.google.com/go/cloudsqlconn/debug"
"cloud.google.com/go/cloudsqlconn/errtype"
"cloud.google.com/go/cloudsqlconn/instance"
"cloud.google.com/go/cloudsqlconn/internal/trace"
"golang.org/x/oauth2"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
const (
// PublicIP is the value for public IP Cloud SQL instances.
PublicIP = "PUBLIC"
// PrivateIP is the value for private IP Cloud SQL instances.
PrivateIP = "PRIVATE"
// PSC is the value for private service connect Cloud SQL instances.
PSC = "PSC"
// AutoIP selects public IP if available and otherwise selects private
// IP.
AutoIP = "AutoIP"
)
// metadata contains information about a Cloud SQL instance needed to create
// connections.
type metadata struct {
ipAddrs map[string]string
serverCACert []*x509.Certificate
serverCAMode string
dnsName string
version string
}
// fetchMetadata uses the Cloud SQL Admin APIs get method to retrieve the
// information about a Cloud SQL instance that is used to create secure
// connections.
func fetchMetadata(
ctx context.Context, client *sqladmin.Service, inst instance.ConnName,
) (m metadata, err error) {
var end trace.EndSpanFunc
ctx, end = trace.StartSpan(ctx, "cloud.google.com/go/cloudsqlconn/internal.FetchMetadata")
defer func() { end(err) }()
db, err := retry50x(ctx, func(ctx2 context.Context) (*sqladmin.ConnectSettings, error) {
return client.Connect.Get(
inst.Project(), inst.Name(),
).Context(ctx2).Do()
}, exponentialBackoff)
if err != nil {
return metadata{}, errtype.NewRefreshError("failed to get instance metadata", inst.String(), err)
}
// validate the instance is supported for authenticated connections
if db.Region != inst.Region() {
msg := fmt.Sprintf(
"provided region was mismatched - got %s, want %s",
inst.Region(), db.Region,
)
return metadata{}, errtype.NewConfigError(msg, inst.String())
}
if db.BackendType != "SECOND_GEN" {
return metadata{}, errtype.NewConfigError(
"unsupported instance - only Second Generation instances are supported",
inst.String(),
)
}
// parse any ip addresses that might be used to connect
ipAddrs := make(map[string]string)
for _, ip := range db.IpAddresses {
switch ip.Type {
case "PRIMARY":
ipAddrs[PublicIP] = ip.IpAddress
case "PRIVATE":
ipAddrs[PrivateIP] = ip.IpAddress
}
}
// resolve DnsName into IP address for PSC
// Note that we have to check for PSC enablement first because CAS instances also set the DnsName.
if db.PscEnabled && db.DnsName != "" {
ipAddrs[PSC] = db.DnsName
}
if len(ipAddrs) == 0 {
return metadata{}, errtype.NewConfigError(
"cannot connect to instance - it has no supported IP addresses",
inst.String(),
)
}
// parse the server-side CA certificate
caCerts := []*x509.Certificate{}
for b, rest := pem.Decode([]byte(db.ServerCaCert.Cert)); b != nil; b, rest = pem.Decode(rest) {
if b == nil {
return metadata{}, errtype.NewRefreshError("failed to decode valid PEM cert", inst.String(), nil)
}
caCert, err := x509.ParseCertificate(b.Bytes)
if err != nil {
return metadata{}, errtype.NewRefreshError(
fmt.Sprintf("failed to parse as X.509 certificate: %v", err),
inst.String(),
nil,
)
}
caCerts = append(caCerts, caCert)
}
m = metadata{
ipAddrs: ipAddrs,
serverCACert: caCerts,
version: db.DatabaseVersion,
dnsName: db.DnsName,
serverCAMode: db.ServerCaMode,
}
return m, nil
}
var expired = time.Time{}.Add(1)
// canRefresh determines if the provided token was refreshed or if it still has
// the sentinel expiration, which means the token was provided without a
// refresh token (as with the Cloud SQL Proxy's --token flag) and therefore
// cannot be refreshed.
func canRefresh(t *oauth2.Token) bool {
return t.Expiry.Unix() != expired.Unix()
}
// refreshToken will retrieve a new token, only if a refresh token is present.
func refreshToken(ts oauth2.TokenSource, tok *oauth2.Token) (*oauth2.Token, error) {
expiredToken := &oauth2.Token{
AccessToken: tok.AccessToken,
TokenType: tok.TokenType,
RefreshToken: tok.RefreshToken,
Expiry: expired,
}
return oauth2.ReuseTokenSource(expiredToken, ts).Token()
}
// fetchEphemeralCert uses the Cloud SQL Admin API's createEphemeral method to
// create a signed TLS certificate that authorized to connect via the Cloud SQL
// instance's serverside proxy. The cert if valid for approximately one hour.
func fetchEphemeralCert(
ctx context.Context,
client *sqladmin.Service,
inst instance.ConnName,
key *rsa.PrivateKey,
ts oauth2.TokenSource,
) (c tls.Certificate, err error) {
var end trace.EndSpanFunc
ctx, end = trace.StartSpan(ctx, "cloud.google.com/go/cloudsqlconn/internal.FetchEphemeralCert")
defer func() { end(err) }()
clientPubKey, err := x509.MarshalPKIXPublicKey(&key.PublicKey)
if err != nil {
return tls.Certificate{}, err
}
req := sqladmin.GenerateEphemeralCertRequest{
PublicKey: string(pem.EncodeToMemory(&pem.Block{Bytes: clientPubKey, Type: "RSA PUBLIC KEY"})),
}
var tok *oauth2.Token
if ts != nil {
var tokErr error
tok, tokErr = ts.Token()
if tokErr != nil {
return tls.Certificate{}, errtype.NewRefreshError(
"failed to retrieve Oauth2 token",
inst.String(),
tokErr,
)
}
// Always refresh the token to ensure its expiration is far enough in
// the future.
tok, tokErr = refreshToken(ts, tok)
if tokErr != nil {
return tls.Certificate{}, errtype.NewRefreshError(
"failed to refresh Oauth2 token",
inst.String(),
tokErr,
)
}
req.AccessToken = tok.AccessToken
}
resp, err := retry50x(ctx, func(ctx2 context.Context) (*sqladmin.GenerateEphemeralCertResponse, error) {
return client.Connect.GenerateEphemeralCert(
inst.Project(), inst.Name(), &req,
).Context(ctx2).Do()
}, exponentialBackoff)
if err != nil {
return tls.Certificate{}, errtype.NewRefreshError(
"create ephemeral cert failed",
inst.String(),
err,
)
}
// parse the client cert
b, _ := pem.Decode([]byte(resp.EphemeralCert.Cert))
if b == nil {
return tls.Certificate{}, errtype.NewRefreshError(
"failed to decode valid PEM cert",
inst.String(),
nil,
)
}
clientCert, err := x509.ParseCertificate(b.Bytes)
if err != nil {
return tls.Certificate{}, errtype.NewRefreshError(
fmt.Sprintf("failed to parse as X.509 certificate: %v", err),
inst.String(),
nil,
)
}
if ts != nil {
// Adjust the certificate's expiration to be the earliest of
// the token's expiration or the certificate's expiration.
if canRefresh(tok) && tok.Expiry.Before(clientCert.NotAfter) {
clientCert.NotAfter = tok.Expiry
}
}
c = tls.Certificate{
Certificate: [][]byte{clientCert.Raw},
PrivateKey: key,
Leaf: clientCert,
}
return c, nil
}
// newAdminAPIClient creates a Refresher.
func newAdminAPIClient(
l debug.ContextLogger,
svc *sqladmin.Service,
key *rsa.PrivateKey,
ts oauth2.TokenSource,
dialerID string,
) adminAPIClient {
return adminAPIClient{
dialerID: dialerID,
logger: l,
key: key,
client: svc,
ts: ts,
}
}
// adminAPIClient manages the SQL Admin API access to instance metadata and to
// ephemeral certificates.
type adminAPIClient struct {
// dialerID is the unique ID of the associated dialer.
dialerID string
logger debug.ContextLogger
// key is used to generate the client certificate
key *rsa.PrivateKey
client *sqladmin.Service
// ts is the TokenSource used for IAM DB AuthN.
ts oauth2.TokenSource
}
// ConnectionInfo immediately performs a full refresh operation using the Cloud
// SQL Admin API.
func (c adminAPIClient) ConnectionInfo(
ctx context.Context, cn instance.ConnName, iamAuthNDial bool,
) (ci ConnectionInfo, err error) {
var refreshEnd trace.EndSpanFunc
ctx, refreshEnd = trace.StartSpan(ctx, "cloud.google.com/go/cloudsqlconn/internal.RefreshConnection",
trace.AddInstanceName(cn.String()),
)
defer func() {
go trace.RecordRefreshResult(context.Background(), cn.String(), c.dialerID, err)
refreshEnd(err)
}()
// start async fetching the instance's metadata
type mdRes struct {
md metadata
err error
}
mdC := make(chan mdRes, 1)
go func() {
defer close(mdC)
md, err := fetchMetadata(ctx, c.client, cn)
mdC <- mdRes{md, err}
}()
// start async fetching the certs
type ecRes struct {
ec tls.Certificate
err error
}
ecC := make(chan ecRes, 1)
go func() {
defer close(ecC)
var iamTS oauth2.TokenSource
if iamAuthNDial {
iamTS = c.ts
}
ec, err := fetchEphemeralCert(ctx, c.client, cn, c.key, iamTS)
ecC <- ecRes{ec, err}
}()
// wait for the results of each operation
var md metadata
select {
case r := <-mdC:
if r.err != nil {
return ConnectionInfo{}, fmt.Errorf("failed to get instance: %w", r.err)
}
md = r.md
case <-ctx.Done():
return ci, fmt.Errorf("refresh failed: %w", ctx.Err())
}
if iamAuthNDial {
if vErr := supportsAutoIAMAuthN(md.version); vErr != nil {
return ConnectionInfo{}, vErr
}
}
var ec tls.Certificate
select {
case r := <-ecC:
if r.err != nil {
return ConnectionInfo{}, fmt.Errorf("fetch ephemeral cert failed: %w", r.err)
}
ec = r.ec
case <-ctx.Done():
return ConnectionInfo{}, fmt.Errorf("refresh failed: %w", ctx.Err())
}
return NewConnectionInfo(
cn, md.dnsName, md.serverCAMode, md.version, md.ipAddrs, md.serverCACert, ec,
), nil
}
// supportsAutoIAMAuthN checks that the engine support automatic IAM authn. If
// auto IAM authn was not request, this is a no-op.
func supportsAutoIAMAuthN(version string) error {
switch {
case strings.HasPrefix(version, "POSTGRES"):
return nil
case strings.HasPrefix(version, "MYSQL"):
return nil
default:
return fmt.Errorf("%s does not support Auto IAM DB Authentication", version)
}
}