version=flag.Bool("version", false, "Print the version of the proxy and exit")
verbose=flag.Bool("verbose", true, "If false, verbose output such as information about when connections are created/closed without error are suppressed")
logDebugStdout=flag.Bool("log_debug_stdout", false, "If true, log messages that are not errors will output to stdout instead of stderr")
refreshCfgThrottle=flag.Duration("refresh_config_throttle", proxy.DefaultRefreshCfgThrottle, "If set, this flag specifies the amount of forced sleep between successive API calls in order to protect client API quota. Minimum allowed value is "+minimumRefreshCfgThrottle.String())
checkRegion=flag.Bool("check_region", false, `If specified, the 'region' portion of the connection string is required for
Unix socket-based connections.`)
// Settings for how to choose which instance to connect to.
dir=flag.String("dir", "", "Directory to use for placing Unix sockets representing database instances")
projects=flag.String("projects", "", `Open sockets for each Cloud SQL Instance in the projects specified
(comma-separated list)`)
instances=flag.String("instances", "", `Comma-separated list of fully qualified instances (project:region:name)
to connect to. If the name has the suffix '=tcp:port', a TCP server is opened
on the specified port to proxy to that instance. Otherwise, one socket file per
instance is opened in 'dir'. You may use INSTANCES environment variable
for the same effect. Using both will use value from flag, Not compatible with -fuse`)
instanceSrc=flag.String("instances_metadata", "", `If provided, it is treated as a path to a metadata value which
is polled for a comma-separated list of instances to connect to. For example,
to use the instance metadata value named 'cloud-sql-instances' you would
provide 'instance/attributes/cloud-sql-instances'. Not compatible with -fuse`)
useFuse=flag.Bool("fuse", false, `Mount a directory at 'dir' using FUSE for accessing instances. Note that the
directory at 'dir' must be empty before this program is started.`)
fuseTmp=flag.String("fuse_tmp", defaultTmp, `Used as a temporary directory if -fuse is set. Note that files in this directory
can be removed automatically by this program.`)
// Settings for limits
maxConnections=flag.Uint64("max_connections", 0, `If provided, the maximum number of connections to establish before refusing new connections. Defaults to 0 (no limit)`)
fdRlimit=flag.Uint64("fd_rlimit", limits.ExpectedFDs, `Sets the rlimit on the number of open file descriptors for the proxy to the provided value. If set to zero, disables attempts to set the rlimit. Defaults to a value which can support 4K connections to one instance`)
termTimeout=flag.Duration("term_timeout", 0, "When set, the proxy will wait for existing connections to close before terminating. Any connections that haven't closed after the timeout will be dropped")
// Settings for authentication.
token=flag.String("token", "", "When set, the proxy uses this Bearer token for authorization.")
tokenFile=flag.String("credential_file", "", `If provided, this json file will be used to retrieve Service Account credentials.
You may set the GOOGLE_APPLICATION_CREDENTIALS environment variable for the same effect.`)
ipAddressTypes=flag.String("ip_address_types", "PUBLIC,PRIVATE", "Default to be 'PUBLIC,PRIVATE'. Options: a list of strings separated by ',', e.g. 'PUBLIC,PRIVATE' ")
skipInvalidInstanceConfigs=flag.Bool("skip_failed_instance_config", false, `Setting this flag will allow you to prevent the proxy from terminating when
some instance configurations could not be parsed and/or are unavailable.`)
// Setting to choose what API to connect to
host=flag.String("host", "", "When set, the proxy uses this host as the base API path. Example: https://sqladmin.googleapis.com")
)
const (
minimumRefreshCfgThrottle=time.Second
port=3307
)
funcinit() {
flag.Usage=func() {
fmt.Fprintf(os.Stderr, `
The Cloud SQL Proxy allows simple, secure connectivity to Google Cloud SQL. It
is a long-running process that opens local sockets (either TCP or Unix sockets)
according to the parameters passed to it. A local application connects to a
Cloud SQL instance by using the corresponding socket.
Authorization:
* On Google Compute Engine, the default service account is used.
The Cloud SQL API must be enabled for the VM.
* When the gcloud command-line tool is installed on the local machine, the
"active account" is used for authentication. Run 'gcloud auth list' to see
which accounts are installed on your local machine and
'gcloud config list account' to view the active account.
* To configure the proxy using a service account, pass the -credential_file
parameter or set the GOOGLE_APPLICATION_CREDENTIALS environment variable.
This will override gcloud or GCE (Google Compute Engine) credentials,
if they exist.
General:
-quiet
Disable log messages (e.g. when new connections are established).
WARNING: this option disables ALL logging output (including connection
errors), which will likely make debugging difficult. The -quiet flag takes
precedence over the -verbose flag.
-log_debug_stdout
When explicitly set to true, verbose and info log messages will be directed
to stdout as opposed to the default stderr.
-verbose
When explicitly set to false, disable log messages that are not errors nor
first-time startup messages (e.g. when new connections are established).
Connection:
-instances
To connect to a specific list of instances, set the instances parameter
to a comma-separated list of instance connection strings. For example:
-instances=my-project:my-region:my-instance
For connectivity over TCP, you must specify a tcp port as part of the
instance string. For example, the following example opens a loopback TCP
socket on port 3306, which will be proxied to connect to the instance
// versionString indiciates the version of the proxy currently in use.
varversionString="1.19.3-dev"
// metadataString indiciates additional build or distribution metadata.
varmetadataString=""
// semanticVersion returns the version of the proxy in a semver format.
funcsemanticVersion() string {
v:=versionString
ifmetadataString!="" {
v+="+"+metadataString
}
returnv
}
// userAgentFromVersionString returns an appropriate user agent string for identifying this proxy process.
funcuserAgentFromVersionString() string {
return"cloud_sql_proxy/"+semanticVersion()
}
constaccountErrorSuffix=`Please create a new VM with Cloud SQL access (scope) enabled under "Identity and API access". Alternatively, create a new "service account key" and specify it using the -credential_file parameter`
funccheckFlags(onGCEbool) error {
if!onGCE {
if*instanceSrc!="" {
returnerrors.New("-instances_metadata unsupported outside of Google Compute Engine")
returnerrors.New(`the default Compute Engine service account is not configured with sufficient permissions to access the Cloud SQL API from this VM. `+accountErrorSuffix)