proxy

package
v0.11.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 22, 2026 License: Apache-2.0 Imports: 24 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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.

View Source
const (
	ListenerDirect    = "direct"    // :5432
	ListenerConnector = "connector" // :3307
)

Listener names used in LoginRequest.

View Source
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.

View Source
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/), plus the recovery target time. On a fresh data directory the agent 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.

View Source
const EnvQueryTimeout = "GCPEMU_SQL_QUERY_TIMEOUT"

EnvQueryTimeout is how long the agent lets the statements run.

View Source
const MaxMessages = 10

MaxMessages is how many notices a response carries, as in Cloud SQL.

View Source
const Name = "sql-proxy"

Name is the agent name registered with internal/agent.

View Source
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

View Source
var SSLRequest = []byte{0, 0, 0, 8, 0x04, 0xd2, 0x16, 0x2f}

SSLRequest is the 8-byte SSLRequest packet (used by readiness probes).

Functions

func Main

func Main(ctx context.Context, _ []string) error

Main is the agent entry point.

func PGCommand

func PGCommand() []string

PGCommand is the command line the agent runs for PostgreSQL.

func QueryMain added in v0.4.0

func QueryMain(ctx context.Context, _ []string) error

QueryMain is the sql-query agent's entry point.

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
	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.

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 QueryColumn struct {
	Name string `json:"name"`
	Type string `json:"type"`
	// contains filtered or unexported fields
}

type QueryMessage added in v0.4.0

type QueryMessage struct {
	Message  string `json:"message"`
	Severity string `json:"severity"`
}

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

type QueryStatus struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}

QueryStatus is a google.rpc.Status.

type QueryValue added in v0.4.0

type QueryValue struct {
	Value     string `json:"value,omitempty"`
	NullValue bool   `json:"nullValue,omitempty"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL