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 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
|
// 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 main
// This file contains code for supporting local sockets for the Cloud SQL Auth proxy.
import (
"bytes"
"errors"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/GoogleCloudPlatform/cloudsql-proxy/logging"
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/fuse"
"github.com/GoogleCloudPlatform/cloudsql-proxy/proxy/proxy"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
// WatchInstances handles the lifecycle of local sockets used for proxying
// local connections. Values received from the updates channel are
// interpretted as a comma-separated list of instances. The set of sockets in
// 'dir' is the union of 'instances' and the most recent list from 'updates'.
func WatchInstances(dir string, cfgs []instanceConfig, updates <-chan string, cl *http.Client) (<-chan proxy.Conn, error) {
ch := make(chan proxy.Conn, 1)
// Instances specified statically (e.g. as flags to the binary) will always
// be available. They are ignored if also returned by the GCE metadata since
// the socket will already be open.
staticInstances := make(map[string]net.Listener, len(cfgs))
for _, v := range cfgs {
l, err := listenInstance(ch, v)
if err != nil {
return nil, err
}
staticInstances[v.Instance] = l
}
if updates != nil {
go watchInstancesLoop(dir, ch, updates, staticInstances, cl)
}
return ch, nil
}
func watchInstancesLoop(dir string, dst chan<- proxy.Conn, updates <-chan string, static map[string]net.Listener, cl *http.Client) {
dynamicInstances := make(map[string]net.Listener)
for instances := range updates {
// All instances were legal when we started, so we pass false below to ensure we don't skip them
// later if they became unhealthy for some reason; this would be a serious enough problem.
list, err := parseInstanceConfigs(dir, strings.Split(instances, ","), cl, false)
if err != nil {
logging.Errorf("%v", err)
// If we do not have a valid list of instances, skip this update
continue
}
stillOpen := make(map[string]net.Listener)
for _, cfg := range list {
instance := cfg.Instance
// If the instance is specified in the static list don't do anything:
// it's already open and should stay open forever.
if _, ok := static[instance]; ok {
continue
}
if l, ok := dynamicInstances[instance]; ok {
delete(dynamicInstances, instance)
stillOpen[instance] = l
continue
}
l, err := listenInstance(dst, cfg)
if err != nil {
logging.Errorf("Couldn't open socket for %q: %v", instance, err)
continue
}
stillOpen[instance] = l
}
// Any instance in dynamicInstances was not in the most recent metadata
// update. Clean up those instances' sockets by closing them; note that
// this does not affect any existing connections instance.
for instance, listener := range dynamicInstances {
logging.Infof("Closing socket for instance %v", instance)
listener.Close()
}
dynamicInstances = stillOpen
}
for _, v := range static {
if err := v.Close(); err != nil {
logging.Errorf("Error closing %q: %v", v.Addr(), err)
}
}
for _, v := range dynamicInstances {
if err := v.Close(); err != nil {
logging.Errorf("Error closing %q: %v", v.Addr(), err)
}
}
}
func remove(path string) {
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
logging.Infof("Remove(%q) error: %v", path, err)
}
}
// listenInstance starts listening on a new unix socket in dir to connect to the
// specified instance. New connections to this socket are sent to dst.
func listenInstance(dst chan<- proxy.Conn, cfg instanceConfig) (net.Listener, error) {
unix := cfg.Network == "unix"
if unix {
remove(cfg.Address)
}
l, err := net.Listen(cfg.Network, cfg.Address)
if err != nil {
return nil, err
}
if unix {
if err := os.Chmod(cfg.Address, 0777|os.ModeSocket); err != nil {
logging.Errorf("couldn't update permissions for socket file %q: %v; other users may not be unable to connect", cfg.Address, err)
}
}
go func() {
for {
start := time.Now()
c, err := l.Accept()
if err != nil {
logging.Errorf("Error in accept for %q on %v: %v", cfg, cfg.Address, err)
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
d := 10*time.Millisecond - time.Since(start)
if d > 0 {
time.Sleep(d)
}
continue
}
l.Close()
return
}
logging.Verbosef("New connection for %q", cfg.Instance)
switch clientConn := c.(type) {
case *net.TCPConn:
clientConn.SetKeepAlive(true)
clientConn.SetKeepAlivePeriod(1 * time.Minute)
}
dst <- proxy.Conn{cfg.Instance, c}
}
}()
logging.Infof("Listening on %s for %s", cfg.Address, cfg.Instance)
return l, nil
}
type instanceConfig struct {
Instance string
Network, Address string
}
// loopbackForNet maps a network (e.g. tcp6) to the loopback address for that
// network. It is updated during the initialization of validNets to include a
// valid loopback address for "tcp".
var loopbackForNet = map[string]string{
"tcp4": "127.0.0.1",
"tcp6": "::1",
}
// validNets tracks the networks that are valid for this platform and machine.
var validNets = func() map[string]bool {
m := map[string]bool{
"unix": runtime.GOOS != "windows",
}
anyTCP := false
for _, n := range []string{"tcp4", "tcp6"} {
host, ok := loopbackForNet[n]
if !ok {
// This is effectively a compile-time error.
panic(fmt.Sprintf("no loopback address found for %v", n))
}
// Open any port to see if the net is valid.
x, err := net.Listen(n, net.JoinHostPort(host, "0"))
if err != nil {
// Error is too verbose to be useful.
continue
}
x.Close()
m[n] = true
if !anyTCP {
anyTCP = true
// Set the loopback value for generic tcp if it hasn't already been
// set. (If both tcp4/tcp6 are supported the first one in the list
// (tcp4's 127.0.0.1) is used.
loopbackForNet["tcp"] = host
}
}
if anyTCP {
m["tcp"] = true
}
return m
}()
func parseInstanceConfig(dir, instance string, cl *http.Client) (instanceConfig, error) {
var ret instanceConfig
proj, region, name, args, err := proxy.ParseInstanceConnectionName(instance)
if err != nil {
return instanceConfig{}, err
}
ret.Instance = args[0]
regionName := fmt.Sprintf("%s~%s", region, name)
if len(args) == 1 {
// Default to listening via unix socket in specified directory
ret.Network = "unix"
ret.Address = filepath.Join(dir, instance)
} else {
// Parse the instance options if present.
opts := strings.SplitN(args[1], ":", 2)
if len(opts) != 2 {
return instanceConfig{}, fmt.Errorf("invalid instance options: must be in the form `unix:/path/to/socket`, `tcp:port`, `tcp:host:port`; invalid option was %q", strings.Join(opts, ":"))
}
ret.Network = opts[0]
var err error
if ret.Network == "unix" {
if strings.HasPrefix(opts[1], "/") {
ret.Address = opts[1] // Root path.
} else {
ret.Address = filepath.Join(dir, opts[1])
}
} else {
ret.Address, err = parseTCPOpts(opts[0], opts[1])
}
if err != nil {
return instanceConfig{}, err
}
}
// Use the SQL Admin API to verify compatibility with the instance.
sql, err := sqladmin.New(cl)
if err != nil {
return instanceConfig{}, err
}
if *host != "" {
sql.BasePath = *host
}
inst, err := sql.Connect.Get(proj, regionName).Do()
if err != nil {
return instanceConfig{}, err
}
if inst.BackendType == "FIRST_GEN" {
logging.Errorf("WARNING: proxy client does not support first generation Cloud SQL instances.")
return instanceConfig{}, fmt.Errorf("%q is a first generation instance", instance)
}
// Postgres instances use a special suffix on the unix socket.
// See https://www.postgresql.org/docs/11/runtime-config-connection.html
if ret.Network == "unix" && strings.HasPrefix(strings.ToLower(inst.DatabaseVersion), "postgres") {
// Verify the directory exists.
if err := os.MkdirAll(ret.Address, 0755); err != nil {
return instanceConfig{}, err
}
ret.Address = filepath.Join(ret.Address, ".s.PGSQL.5432")
}
if !validNets[ret.Network] {
return ret, fmt.Errorf("invalid %q: unsupported network: %v", instance, ret.Network)
}
return ret, nil
}
// parseTCPOpts parses the instance options when specifying tcp port options.
func parseTCPOpts(ntwk, addrOpt string) (string, error) {
if strings.Contains(addrOpt, ":") {
return addrOpt, nil // User provided a host and port; use that.
}
// No "host" part of the address. Be safe and assume that they want a loopback address.
addr, ok := loopbackForNet[ntwk]
if !ok {
return "", fmt.Errorf("invalid %q:%q: unrecognized network %v", ntwk, addrOpt, ntwk)
}
return net.JoinHostPort(addr, addrOpt), nil
}
// parseInstanceConfigs calls parseInstanceConfig for each instance in the
// provided slice, collecting errors along the way. There may be valid
// instanceConfigs returned even if there's an error.
func parseInstanceConfigs(dir string, instances []string, cl *http.Client, skipFailedInstanceConfigs bool) ([]instanceConfig, error) {
errs := new(bytes.Buffer)
var cfg []instanceConfig
for _, v := range instances {
if v == "" {
continue
}
if c, err := parseInstanceConfig(dir, v, cl); err != nil {
if skipFailedInstanceConfigs {
logging.Infof("There was a problem when parsing an instance configuration but ignoring due to the configuration. Error: %v", err)
} else {
fmt.Fprintf(errs, "\n\t%v", err)
}
} else {
cfg = append(cfg, c)
}
}
var err error
if errs.Len() > 0 {
err = fmt.Errorf("errors parsing config:%s", errs)
}
return cfg, err
}
// CreateInstanceConfigs verifies that the parameters passed to it are valid
// for the proxy for the platform and system and then returns a slice of valid
// instanceConfig. It is possible for the instanceConfig to be empty if no valid
// configurations were specified, however `err` will be set.
func CreateInstanceConfigs(dir string, useFuse bool, instances []string, instancesSrc string, cl *http.Client, skipFailedInstanceConfigs bool) ([]instanceConfig, error) {
if useFuse && !fuse.Supported() {
return nil, errors.New("FUSE not supported on this system")
}
cfgs, err := parseInstanceConfigs(dir, instances, cl, skipFailedInstanceConfigs)
if err != nil {
return nil, err
}
if dir == "" {
// Reasons to set '-dir':
// - Using -fuse
// - Using the metadata to get a list of instances
// - Having an instance that uses a 'unix' network
if useFuse {
return nil, errors.New("must set -dir because -fuse was set")
} else if instancesSrc != "" {
return nil, errors.New("must set -dir because -instances_metadata was set")
} else {
for _, v := range cfgs {
if v.Network == "unix" {
return nil, fmt.Errorf("must set -dir: using a unix socket for %v", v.Instance)
}
}
}
// Otherwise it's safe to not set -dir
}
if useFuse {
if len(instances) != 0 || instancesSrc != "" {
return nil, errors.New("-fuse is not compatible with -projects, -instances, or -instances_metadata")
}
return nil, nil
}
// FUSE disabled.
if len(instances) == 0 && instancesSrc == "" {
// Failure to specifying instance can be caused by following reasons.
// 1. not enough information is provided by flags
// 2. failed to invoke gcloud
var flags string
if fuse.Supported() {
flags = "-projects, -fuse, -instances or -instances_metadata"
} else {
flags = "-projects, -instances or -instances_metadata"
}
errStr := fmt.Sprintf("no instance selected because none of %s is specified", flags)
return nil, errors.New(errStr)
}
return cfgs, nil
}
|