Documentation
¶
Overview ¶
Package proxy is the Cloud SQL in-container agent (FR-SQL-004..007). It is the entrypoint of every instance container: it starts PostgreSQL (the image's docker-entrypoint.sh, listening on 127.0.0.1:5433 and a unix socket) as a child process, waits until it accepts TCP connections, and then serves
- :5432, a PostgreSQL-protocol-aware proxy that terminates TLS per the instance's SSL mode, asks the emulator whether the connection is allowed (authorized networks, SSL mode, connector enforcement), and either passes BUILT_IN users through to PostgreSQL (SCRAM over 127.0.0.1) or performs IAM database authentication and logs the user in over the unix socket (trust);
- :3307, the Cloud SQL connector / Auth Proxy server side: TLS 1.3 with the instance's server certificate and a required client certificate signed by the instance's client CA, followed by the same logic.
Decisions are made by the emulator through a small HTTP endpoint (see LoginRequest) so that changes to authorized networks, users and IAM policies apply immediately.
Index ¶
- Constants
- Variables
- func Main(ctx context.Context, _ []string) error
- func PGCommand() []string
- func QueryMain(ctx context.Context, _ []string) error
- func WriteLANHeader(w io.Writer, key string, src, dst *net.TCPAddr) error
- type LoginRequest
- type LoginResponse
- type QueryColumn
- type QueryMessage
- type QueryRequest
- type QueryResponse
- type QueryResult
- type QueryRow
- type QueryStatus
- type QueryValue
Constants ¶
const ( EnvControl = "GCPEMU_SQL_CONTROL" // base URL of the emulator's agent endpoint EnvKey = "GCPEMU_SQL_KEY" // per-instance shared secret KeyHeader = "X-Gcpemu-Agent-Key" Dir = "/gcpemu" ServerCert = "server.crt" // server certificate chain (PEM) ServerKey = "server.key" ClientCA = "client-ca.crt" HBAFile = "pg_hba.conf" SocketDir = "/var/run/postgresql" InternalPort = 5433 PublicPort = 5432 ConnectPort = 3307 // ArchiveDir is the archive volume's mount point: completed WAL // segments in wal/ and base backups in base/ (point-in-time recovery). ArchiveDir = "/var/lib/gcpemu-archive" )
Environment variables and files the emulator provides to the agent.
const ( ListenerDirect = "direct" // :5432 ListenerConnector = "connector" // :3307 )
Listener names used in LoginRequest.
const ( ActionDeny = "deny" // send Message as a FATAL error ActionPassthrough = "passthrough" // relay to PostgreSQL over TCP (password auth) ActionPassword = "password" // ask for a cleartext password, then call again ActionTrust = "trust" // authenticated: log in over the unix socket )
Actions returned by the emulator.
const ( RestoreBundle = "restore.tar.gz" RestoreTarget = "restore-target" )
Point-in-time recovery (FR-SQL-008). The emulator gives a clone's container, before it starts, a restore bundle in Dir: a gzipped tar of one base backup (base/<id>/base.tar.gz, from pg_basebackup -Ft -z) and the source's archived WAL (wal/, gzipped as <segment>.gz or, from before the archive command compressed, uncompressed), plus the recovery target time. On a fresh data directory the agent decompresses the WAL, lays the base backup out as PGDATA and sets PostgreSQL up to replay the WAL up to the target and promote; the emulator then applies the clone's own settings.
const EnvQueryTimeout = "GCPEMU_SQL_QUERY_TIMEOUT"
EnvQueryTimeout is how long the agent lets the statements run.
const MaxMessages = 10
MaxMessages is how many notices a response carries, as in Cloud SQL.
const Name = "sql-proxy"
Name is the agent name registered with internal/agent.
const QueryName = "sql-query"
QueryName is the agent that runs instances.executeSql (the Cloud SQL Data API) inside an instance's container: the emulator execs it with a QueryRequest on stdin and reads a QueryResponse from stdout. It logs in over the unix socket (trust), as the emulator's admin commands do, and sends the statements as one simple-protocol query, so that PostgreSQL runs them in one implicit transaction and reports each statement's columns, types and NULLs exactly.
Variables ¶
var SSLRequest = []byte{0, 0, 0, 8, 0x04, 0xd2, 0x16, 0x2f}
SSLRequest is the 8-byte SSLRequest packet (used by readiness probes).
Functions ¶
func PGCommand ¶
func PGCommand() []string
PGCommand is the command line the agent runs for PostgreSQL.
Types ¶
type LoginRequest ¶
type LoginRequest struct {
Listener string `json:"listener"`
Local string `json:"local"` // local IP the client connected to
Remote string `json:"remote"` // client source IP
// LANCaller is the LAN caller's address from the --sql-bind relay's
// signed PROXY header, "" without one (ADR 0030).
LANCaller string `json:"lanCaller,omitempty"`
TLS bool `json:"tls"`
// ClientCert is the verified client certificate (DER), if any.
ClientCert []byte `json:"clientCert,omitempty"`
User string `json:"user"`
Database string `json:"database"`
// Password is set on the second call, after a cleartext password request.
Password *string `json:"password,omitempty"`
}
LoginRequest is sent by the agent for every client startup message.
func (*LoginRequest) Client ¶ added in v0.12.0
func (r *LoginRequest) Client() string
Client is the client's address: the LAN caller's, else the TCP peer's.
type LoginResponse ¶
type LoginResponse struct {
Action string `json:"action"`
Code string `json:"code,omitempty"` // SQLSTATE for deny
Message string `json:"message,omitempty"`
}
LoginResponse is the emulator's decision.
type QueryColumn ¶ added in v0.4.0
type QueryMessage ¶ added in v0.4.0
type QueryRequest ¶ added in v0.4.0
type QueryRequest struct {
User string `json:"user"`
Database string `json:"database"`
SQL string `json:"sql"`
// RowLimit caps the rows returned per statement (0: no cap).
RowLimit int64 `json:"rowLimit,omitempty"`
// MaxBytes caps the size of all values returned; beyond it the result
// is truncated (AllowPartial) or the request fails.
MaxBytes int64 `json:"maxBytes"`
AllowPartial bool `json:"allowPartial,omitempty"`
}
QueryRequest is what the emulator asks the agent to run.
type QueryResponse ¶ added in v0.4.0
type QueryResponse struct {
Messages []QueryMessage `json:"messages,omitempty"`
Results []QueryResult `json:"results,omitempty"`
Status *QueryStatus `json:"status,omitempty"`
}
QueryResponse has the shape of sqladmin's SqlInstancesExecuteSqlResponse without its metadata, which the emulator adds.
func RunQuery ¶ added in v0.4.0
func RunQuery(ctx context.Context, host string, port int, req *QueryRequest) *QueryResponse
RunQuery runs req against the server listening on host:port.
type QueryResult ¶ added in v0.4.0
type QueryResult struct {
Columns []QueryColumn `json:"columns,omitempty"`
Rows []QueryRow `json:"rows,omitempty"`
Message string `json:"message,omitempty"`
PartialResult bool `json:"partialResult,omitempty"`
}
type QueryRow ¶ added in v0.4.0
type QueryRow struct {
Values []QueryValue `json:"values"`
}
type QueryStatus ¶ added in v0.4.0
QueryStatus is a google.rpc.Status.