forked from GoogleCloudPlatform/cloud-sql-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dial.go
98 lines (87 loc) · 2.85 KB
/
dial.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
// Copyright 2015 Google Inc. All Rights Reserved.
//
// 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
//
// http://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 proxy
import (
"fmt"
"net"
"net/http"
"sync"
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/certs"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
)
const port = 3307
var dialClient struct {
// This client is initialized in Init/InitClient/InitDefault and read in Dial.
c *Client
sync.Mutex
}
// Dial returns a net.Conn connected to the Cloud SQL Instance specified. The
// format of 'instance' is "project-name:region:instance-name".
//
// If one of the Init functions hasn't been called yet, InitDefault is called.
//
// This is a network-level function; consider looking in the dialers
// subdirectory for more convenience functions related to actually logging into
// your database.
func Dial(instance string) (net.Conn, error) {
dialClient.Lock()
c := dialClient.c
dialClient.Unlock()
if c == nil {
if err := InitDefault(context.Background()); err != nil {
return nil, fmt.Errorf("default proxy initialization failed; consider calling proxy.Init explicitly: %v", err)
}
// InitDefault initialized the client.
dialClient.Lock()
c = dialClient.c
dialClient.Unlock()
}
return c.Dial(instance)
}
// Dialer is a convenience type to model the standard 'Dial' function.
type Dialer func(net, addr string) (net.Conn, error)
// Init must be called before Dial is called. This is a more flexible version
// of InitDefault, but allows you to set more fields.
//
// The http.Client is used to authenticate API requests.
// The connset parameter is optional.
// If the dialer is nil, net.Conn is used.
func Init(auth *http.Client, connset *ConnSet, dialer Dialer) {
dialClient.Lock()
dialClient.c = &Client{
Port: port,
Certs: certs.NewCertSource("https://www.googleapis.com/sql/v1beta4/", auth, true),
Conns: connset,
Dialer: dialer,
}
dialClient.Unlock()
}
// InitClient is similar to Init, but allows you to specify the Client
// directly.
func InitClient(c Client) {
dialClient.Lock()
dialClient.c = &c
dialClient.Unlock()
}
// InitDefault attempts to initialize the Dial function using application
// default credentials.
func InitDefault(ctx context.Context) error {
cl, err := google.DefaultClient(ctx, "https://www.googleapis.com/auth/sqlservice.admin")
if err != nil {
return err
}
Init(cl, nil, nil)
return nil
}