package cloudsql

import "cloud.google.com/go/cloudsqlconn/internal/cloudsql"

Index

Constants

const (

	// RefreshTimeout is the maximum amount of time to wait for a refresh
	// cycle to complete. This value should be greater than the
	// refreshInterval.
	RefreshTimeout = 60 * time.Second

	// FailoverPeriod is the frequency with which the dialer will check
	// if the DNS record has changed for connections configured using
	// a DNS name.
	FailoverPeriod = 30 * time.Second
)
const (
	// PublicIP is the value for public IP Cloud SQL instances.
	PublicIP = "PUBLIC"
	// PrivateIP is the value for private IP Cloud SQL instances.
	PrivateIP = "PRIVATE"
	// PSC is the value for private service connect Cloud SQL instances.
	PSC = "PSC"
	// AutoIP selects public IP if available and otherwise selects private
	// IP.
	AutoIP = "AutoIP"
)

Variables

var DNSResolver = &DNSInstanceConnectionNameResolver{
	dnsResolver: net.DefaultResolver,
}

DNSResolver uses the default net.Resolver to find TXT records containing an instance name for a DNS record.

var DefaultResolver = &ConnNameResolver{}

DefaultResolver simply parses instance names.

Types

type ConnNameResolver

type ConnNameResolver struct {
}

ConnNameResolver simply parses instance names. Implements InstanceConnectionNameResolver

func (*ConnNameResolver) Resolve

func (r *ConnNameResolver) Resolve(_ context.Context, icn string) (instanceName instance.ConnName, err error)

Resolve returns the instance name, possibly using DNS. This will return an instance.ConnName or an error if it was unable to resolve an instance name.

type ConnectionInfo

type ConnectionInfo struct {
	ConnectionName    instance.ConnName
	ClientCertificate tls.Certificate
	ServerCACert      []*x509.Certificate
	ServerCAMode      string
	DBVersion         string
	// The DNSName is from the ConnectSettings API.
	// It is used to validate the server identity of the CAS instances.
	DNSName    string
	Expiration time.Time
	// contains filtered or unexported fields
}

ConnectionInfo contains all necessary information to connect securely to the server-side Proxy running on a Cloud SQL instance.

func NewConnectionInfo

func NewConnectionInfo(
	cn instance.ConnName,
	dnsName string,
	serverCAMode string,
	version string,
	ipAddrs map[string]string,
	serverCACert []*x509.Certificate,
	clientCert tls.Certificate,
) ConnectionInfo

NewConnectionInfo initializes a ConnectionInfo struct.

func (ConnectionInfo) Addr

func (c ConnectionInfo) Addr(ipType string) (string, error)

Addr returns the IP address or DNS name for the given IP type.

func (ConnectionInfo) TLSConfig

func (c ConnectionInfo) TLSConfig() *tls.Config

TLSConfig constructs a TLS configuration for the given connection info.

type DNSInstanceConnectionNameResolver

type DNSInstanceConnectionNameResolver struct {
	// contains filtered or unexported fields
}

DNSInstanceConnectionNameResolver can resolve domain names into instance names using TXT records in DNS. Implements InstanceConnectionNameResolver

func (*DNSInstanceConnectionNameResolver) Resolve

func (r *DNSInstanceConnectionNameResolver) Resolve(ctx context.Context, icn string) (instanceName instance.ConnName, err error)

Resolve returns the instance name, possibly using DNS. This will return an instance.ConnName or an error if it was unable to resolve an instance name.

type LazyRefreshCache

type LazyRefreshCache struct {
	// contains filtered or unexported fields
}

LazyRefreshCache is caches connection info and refreshes the cache only when a caller requests connection info and the current certificate is expired.

func NewLazyRefreshCache

func NewLazyRefreshCache(
	cn instance.ConnName,
	l debug.ContextLogger,
	client *sqladmin.Service,
	key *rsa.PrivateKey,
	_ time.Duration,
	tp auth.TokenProvider,
	dialerID string,
	useIAMAuthNDial bool,
) *LazyRefreshCache

NewLazyRefreshCache initializes a new LazyRefreshCache.

func (*LazyRefreshCache) Close

func (c *LazyRefreshCache) Close() error

Close is a no-op and provided purely for a consistent interface with other caching types.

func (*LazyRefreshCache) ConnectionInfo

func (c *LazyRefreshCache) ConnectionInfo(
	ctx context.Context,
) (ConnectionInfo, error)

ConnectionInfo returns connection info for the associated instance. New connection info is retrieved under two conditions: - the current connection info's certificate has expired, or - a caller has separately called ForceRefresh

func (*LazyRefreshCache) ForceRefresh

func (c *LazyRefreshCache) ForceRefresh()

ForceRefresh invalidates the caches and configures the next call to ConnectionInfo to retrieve a fresh connection info.

func (*LazyRefreshCache) UpdateRefresh

func (c *LazyRefreshCache) UpdateRefresh(useIAMAuthNDial *bool)

UpdateRefresh updates the refresh operation to either enable or disable IAM authentication for the cached connection info.

type RefreshAheadCache

type RefreshAheadCache struct {
	// contains filtered or unexported fields
}

RefreshAheadCache manages the information used to connect to the Cloud SQL instance by periodically calling the Cloud SQL Admin API. It automatically refreshes the required information approximately 4 minutes before the previous certificate expires (every ~56 minutes).

func NewRefreshAheadCache

func NewRefreshAheadCache(
	cn instance.ConnName,
	l debug.ContextLogger,
	client *sqladmin.Service,
	key *rsa.PrivateKey,
	refreshTimeout time.Duration,
	tp auth.TokenProvider,
	dialerID string,
	useIAMAuthNDial bool,
) *RefreshAheadCache

NewRefreshAheadCache initializes a new Instance given an instance connection name

func (*RefreshAheadCache) Close

func (i *RefreshAheadCache) Close() error

Close closes the instance; it stops the refresh cycle and prevents it from making additional calls to the Cloud SQL Admin API.

func (*RefreshAheadCache) ConnectionInfo

func (i *RefreshAheadCache) ConnectionInfo(ctx context.Context) (ConnectionInfo, error)

ConnectionInfo returns an IP address specified by ipType (i.e., public or private) and a TLS config that can be used to connect to a Cloud SQL instance.

func (*RefreshAheadCache) ForceRefresh

func (i *RefreshAheadCache) ForceRefresh()

ForceRefresh triggers an immediate refresh operation to be scheduled and used for future connection attempts. Until the refresh completes, the existing connection info will be available for use if valid.

func (*RefreshAheadCache) UpdateRefresh

func (i *RefreshAheadCache) UpdateRefresh(useIAMAuthNDial *bool)

UpdateRefresh cancels all existing refresh attempts and schedules new attempts with the provided config only if it differs from the current configuration.

Source Files

instance.go lazy.go refresh.go resolver.go retry.go

Version
v1.15.0 (latest)
Published
Feb 12, 2025
Platform
linux/amd64
Imports
21 packages
Last checked
5 months ago

Tools for package owners.