link

package
v0.12.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2026 License: Apache-2.0 Imports: 30 Imported by: 0 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 link connects an Instance to Linked Instances (ADR 0027). This part is Pairing: two Instances, each approving the other, come to pin each other's Instance CA, after which each accepts the other over a link, a mutual TLS connection to the --link-bind listener on which both sides present certificates from their own Instance CA.

The listener accepts any client certificate that chains to the CA the client presents, and decides at each request: the hello and Pairing requests are open to any Instance, everything else only to one whose CA is pinned. A client pins the other side's CA itself, so a Link gives no authority to a machine that merely presents an approved address.

Index

Constants

View Source
const (
	SourceMDNS   = "mdns"
	SourceStatic = "static"
)

Sources of a Discovered Instance.

View Source
const (
	DefaultPort = 4511
	Endpoint    = "link"
)

DefaultPort is the --link-bind listeners' port unless `--port link=N` says otherwise; Endpoint names them in emu.Endpoints and --port.

View Source
const (
	StatePaired = "paired"
	// StateBroken: an Instance CA no longer matches the one pinned at
	// Pairing, this side's or the other's.
	StateBroken = "broken"
)

States of a link.

View Source
const (
	ReasonUnpaired     = "unpaired"
	ReasonCAMismatch   = "ca-mismatch"
	ReasonIncompatible = "incompatible-version"
	ReasonRejected     = "rejected"
	ReasonTimeout      = "not-approved"
	ReasonSelf         = "self"
	ReasonBusy         = "too-many-requests"
	ReasonSuperseded   = "superseded"
)

Reasons a link request is refused, in the refusal's "reason".

View Source
const DefaultApprovalTimeout = 5 * time.Minute

DefaultApprovalTimeout is how long a Pairing request waits for the other side to approve it.

View Source
const MaxPendingRequests = 16

MaxPendingRequests caps the Pairing requests waiting for approval, since any Instance may make one.

View Source
const ServiceType = "_gcpemu._tcp"

ServiceType is the DNS-SD service type Instances advertise their --link-bind listeners under.

View Source
const StoreFile = "links.json"

StoreFile holds a Persistent Instance's Links, Shares and Delegations in its instance directory. They are configuration, not state, so Reset and State snapshots leave them alone (ADR 0027).

Variables

View Source
var Current = Versions{Min: 2, Max: 2}

Current is the range this build speaks. Version 2 added the load balancing endpoints (#715), without which a front Instance could not name a Delegated Project's --lb-bind addresses, so version 1 is refused.

Functions

func Dialable

func Dialable(addr string) string

Dialable adds DefaultPort to an address without a port.

func Fingerprint

func Fingerprint(c *x509.Certificate) string

Fingerprint is how links name an Instance CA: the SHA-256 of its certificate.

func Negotiate

func Negotiate(ours, theirs Versions, who string) (int, error)

Negotiate returns the highest version that ours and theirs, those of the Instance named who, both speak, or an error naming both.

Types

type Config

type Config struct {
	// CA is this Instance's CA; the Manager follows its rotation.
	CA *ca.CA
	// Instance and ID are this Instance's name and Instance ID.
	Instance, ID string
	Store        *Store
	Log          *slog.Logger
	// Versions is the link protocol range spoken; zero means Current.
	Versions Versions
	// ApprovalTimeout zero means DefaultApprovalTimeout.
	ApprovalTimeout time.Duration
	// HasProject reports whether this Instance has the Project id, which
	// it then cannot delegate.
	HasProject func(id string) bool
	// Serve serves the requests paired Instances forward; nil refuses them.
	Serve ServeFunc
	// Authenticate returns the Principal of an access token this Instance
	// issued, which paired Instances ask about (see
	// Manager.Authenticate). It must not ask this Instance's own
	// links, so that Instances that delegate to each other do not ask
	// each other in circles.
	Authenticate func(ctx context.Context, token string) (principal string, ok bool)
	// VerifyIDToken returns the service account of an ID token this
	// Instance signed, which paired Instances ask about (see
	// Manager.LinkedIDToken).
	VerifyIDToken func(token string) (email string, ok bool)
	// Buckets returns this Instance's Cloud Storage buckets and their
	// Projects, which paired Instances watch (see Manager.BucketOwner).
	Buckets func() map[string]string
	// SigningKey returns this Instance's ID token signing key, which
	// paired Instances list (see Manager.LinkedSigningKeys); ok false
	// when it has none.
	SigningKey func() (SigningKey, bool)
	// LBFrontends returns this Instance's Load balancing frontends, as
	// told to a LAN caller that arrived at the address lanIP, which paired
	// Instances list for the Projects shared with them (see
	// Manager.LinkedLBFrontends).
	LBFrontends func(lanIP string) []LBFrontend
	// LBBind reports whether this Instance serves --lb-bind; without it,
	// paired Instances learn that none of its Load balancing frontends,
	// the LoadBalancer Services it does not list among them included, have
	// an address. nil reports false.
	LBBind func() bool
	// StaticAddrs lists link addresses that Discover asks besides browsing
	// mDNS, for networks multicast doesn't cross.
	StaticAddrs []string
	// MDNS configures Advertise and Discover's mDNS; zero is port 5353.
	MDNS mdns.Options
}

Config configures a Manager.

type Delegation

type Delegation struct {
	Project string `json:"project"`
	Link    string `json:"link"`
}

Delegation is a Project the front Instance delegates to the link Link, which shares it.

type Discovered

type Discovered struct {
	// Name is "<Instance name>@<host>", host being the machine it
	// advertises from, or the static address's.
	Name       string `json:"name"`
	Instance   string `json:"instance"`
	InstanceID string `json:"instanceId"`
	// Address is its --link-bind listener, host:port.
	Address string `json:"address"`
	// Fingerprint is the Instance CA fingerprint it advertises, or
	// presented at a static address. Pairing checks it again.
	Fingerprint string `json:"fingerprint"`
	Version     int    `json:"version"`
	MinVersion  int    `json:"minVersion"`
	Source      string `json:"source"`
	// Link names this Instance's Link to it, when they are paired.
	Link string `json:"link,omitempty"`
	// Problem says why it cannot be paired as it is: an incompatible link
	// protocol version, a CA other than its Link's, or, at a static
	// address, that it cannot be reached.
	Problem string `json:"problem,omitempty"`
}

Discovered is a Discovered Instance: one found on the LAN because it advertises itself, or at a static link address, which `gcpemu link pair` takes by Name. Discovery never pairs it.

type Identity

type Identity struct {
	Name string `json:"name"`
	ID   string `json:"id"`
	// Fingerprint is that of the Instance CA it presented.
	Fingerprint string `json:"fingerprint"`
	// Version and MinVersion are the link protocol range it speaks.
	Version    int `json:"version"`
	MinVersion int `json:"minVersion"`
}

Identity is what an Instance tells another about itself.

type LBFrontend

type LBFrontend struct {
	// Name is its Service endpoint name, by which `gcpemu env` names its
	// variables (ADR 0013).
	Name    string `json:"name"`
	Project string `json:"project"`
	// What names it in messages.
	What string `json:"what"`
	// Address is the --lb-bind address:port to tell the caller, "" when
	// there is none, and Error then says why.
	Address string `json:"address,omitempty"`
	Error   string `json:"error,omitempty"`
}

LBFrontend is a Load balancing frontend, a forwarding rule's or a GKE LoadBalancer Service port's, as a Linked Instance tells a LAN caller it.

type Link struct {
	// Name is the link's name on this side, unique within the Instance.
	Name string `json:"name"`
	// Instance and InstanceID are the other Instance's name and ID.
	Instance   string `json:"instance"`
	InstanceID string `json:"instanceId"`
	// Address is the other Instance's --link-bind address, "" when it has
	// none and only connects to this one.
	Address string `json:"address,omitempty"`
	// Fingerprint and CA are the other Instance's pinned Instance CA.
	Fingerprint string `json:"fingerprint"`
	CA          string `json:"ca"`
	// LocalFingerprint is this Instance's CA at Pairing; once it no longer
	// is, `gcpemu ca rotate` has broken the Link.
	LocalFingerprint string `json:"localFingerprint"`
	// Version is the link protocol version negotiated at Pairing.
	Version  int       `json:"version"`
	PairedAt time.Time `json:"pairedAt"`
	// Broken is what the latest dial over the Link found wrong with the
	// other Instance's CA, at BrokenAt; "" once a dial finds it fine.
	Broken   string    `json:"broken,omitempty"`
	BrokenAt time.Time `json:"brokenAt,omitzero"`
}

Link is what one side of a Pairing keeps of the other: the other Instance and the Instance CA it pinned.

type LinkFrontends

type LinkFrontends struct {
	Link      string       `json:"link"`
	Frontends []LBFrontend `json:"frontends"`
	// LBBind is set when the Linked Instance has --lb-bind.
	LBBind bool `json:"lbBind"`
	// Error says why the link could not be asked, which leaves out its
	// Load balancing frontends.
	Error string `json:"error,omitempty"`
}

LinkFrontends is what a Linked Instance said of its Load balancing frontends.

type LinkState

type LinkState struct {
	Link
	State   string `json:"state"`
	Problem string `json:"problem,omitempty"`
	// Warning is the latest refused connection that claimed to be the
	// other Instance with another CA. This side cannot verify it, so it
	// leaves State alone.
	Warning string `json:"warning,omitempty"`
}

LinkState is a Link and its State, as `gcpemu link list` shows it.

type Manager

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

Manager manages an Instance's links: its --link-bind listeners, the Pairing requests waiting for approval, and the client that dials other Instances' listeners.

func New

func New(cfg Config) *Manager

New returns cfg's Manager; Serve makes it reachable.

func (*Manager) Addresses

func (m *Manager) Addresses() []string

Addresses returns the addresses Serve serves.

func (*Manager) Advertise

func (m *Manager) Advertise(addrs []string) error

Advertise advertises the link listeners at addrs (host:port) over mDNS/DNS-SD, until Close. Only an Instance with --link-bind calls it (ADR 0027).

func (*Manager) Approve

func (m *Manager) Approve(id, name string) (Link, error)

Approve approves the incoming Pairing request id, pinning the requester's Instance CA under the link name name (default the requester's Instance name).

func (*Manager) Authenticate

func (m *Manager) Authenticate(ctx context.Context, token string) (principal string, unreachable map[string]error)

Authenticate returns the Principal of token, which this Instance did not issue, as a Linked Instance it delegates to issued it, and the links it could not ask, each with why, naming the link: broken, with no --link-bind address, or not answering.

func (*Manager) BucketOwner

func (m *Manager) BucketOwner(bucket string) (link, project string, ok bool)

BucketOwner returns the link whose Linked Instance has the bucket, and its Project there, among those of the Projects it shares with this Instance.

func (*Manager) Check

func (m *Manager) Check(ctx context.Context, name string) Status

Check dials the link name and reports its Status.

func (*Manager) Close

func (m *Manager) Close() error

Close stops serving and refuses every waiting Pairing request.

func (*Manager) Delegate

func (m *Manager) Delegate(ctx context.Context, id, to string) (Delegation, error)

Delegate delegates the Project id to the link to, which must share it with this Instance, and learns the link's buckets before it returns. It refuses a Project this Instance has, or shares, since local state is never shadowed or merged.

func (m *Manager) DelegatedLinks() []string

DelegatedLinks returns the links any Project is delegated to, by name.

func (*Manager) DelegatedTo

func (m *Manager) DelegatedTo(ref string) (string, bool)

DelegatedTo returns the link the Project ref, an ID or number, is delegated to.

func (*Manager) Delegating

func (m *Manager) Delegating() bool

Delegating reports whether this Instance delegates any Project.

func (*Manager) Delegations

func (m *Manager) Delegations() []Delegation

Delegations returns the Delegated Projects, by Project.

func (*Manager) Discover

func (m *Manager) Discover(ctx context.Context) ([]Discovered, error)

Discover lists the Discovered Instances: those that answer an mDNS browse, then those at the static link addresses, without this Instance and each once. Those already paired are marked with their Link, and those that cannot be paired with their problem.

func (*Manager) Forward

func (m *Manager) Forward(w http.ResponseWriter, r *http.Request, name, principal string, fail func(http.ResponseWriter, error), answered func(*http.Response))

Forward forwards r over the link name, asserting that its caller is principal, and writes the Linked Instance's answer, which answered, when not nil, sees first. When the link cannot be used, the Linked Instance cannot be reached or it refuses the link, Forward instead calls fail with an error naming the link, having written nothing.

func (*Manager) Identify

func (m *Manager) Identify(ctx context.Context, addr string) (Identity, error)

Identify asks the Instance whose link listener is at addr who it is, without pairing.

func (*Manager) LinkedIDToken

func (m *Manager) LinkedIDToken(ctx context.Context, token string) (principal string, unreachable []string)

LinkedIDToken returns the service account Principal of the ID token token, which this Instance did not sign, as a Linked Instance it delegates to signed it, and the links it could not ask. A Linked Instance speaks only for the service accounts of the Projects delegated to it.

func (*Manager) LinkedLBFrontends

func (m *Manager) LinkedLBFrontends(ctx context.Context) []LinkFrontends

LinkedLBFrontends asks each link this Instance delegates a Project to, at once, for the Load balancing frontends of the Projects delegated to it, by link. A link it could not ask has an Error and no Load balancing frontends.

func (*Manager) LinkedSigningKeys

func (m *Manager) LinkedSigningKeys() []SigningKey

LinkedSigningKeys returns the signing keys of the links this Instance delegates a Project to, as last heard, by link. It never waits on a Linked Instance.

func (m *Manager) Links() []Link

Links returns this Instance's Links by name.

func (*Manager) Pair

func (m *Manager) Pair(ctx context.Context, addr, fingerprint, name string) (Link, error)

Pair asks the Instance at addr, whose Instance CA has the fingerprint the user approved, to pair, and waits for its approval. It then pins that CA under the link name name (default the other Instance's name).

func (*Manager) RefreshBuckets

func (m *Manager) RefreshBuckets(ctx context.Context, name string)

RefreshBuckets asks the link name for its buckets now, as after a request forwarded over it created or deleted one, so that the next request on that bucket is routed by the change.

func (*Manager) Reject

func (m *Manager) Reject(id string) error

Reject refuses the incoming Pairing request id.

func (*Manager) Requests

func (m *Manager) Requests() []Request

Requests returns the incoming Pairing requests, oldest first.

func (*Manager) Serve

func (m *Manager) Serve(l net.Listener) error

Serve serves the link protocol on l over mutual TLS until Close.

func (*Manager) Share

func (m *Manager) Share(id, with string) error

Share offers the Project id to the link with, or to every link when with is "". Sharing alone forwards nothing.

func (*Manager) Shared

func (m *Manager) Shared(name, ref string) bool

Shared reports whether this Instance shares the Project ref, an ID or number, with the link name.

func (*Manager) SharedWith

func (m *Manager) SharedWith(name string) []string

SharedWith returns the Projects this Instance shares with the link name.

func (*Manager) Shares

func (m *Manager) Shares() []Share

Shares returns the Projects this Instance shares, by Project.

func (*Manager) StartWatching

func (m *Manager) StartWatching()

StartWatching has this Instance watch the buckets of every link it delegates a Project to, from now until Close.

func (*Manager) States

func (m *Manager) States() []LinkState

States returns the Links and their State, without dialling anything.

func (*Manager) Undelegate

func (m *Manager) Undelegate(id string) error

Undelegate stops delegating the Project id. It holds no state on this Instance, so it does not exist here until something uses it again.

func (*Manager) Unpair

func (m *Manager) Unpair(name string) error

Unpair removes the link name, so that this side no longer accepts the other Instance, at once.

func (*Manager) Unshare

func (m *Manager) Unshare(id, with string) error

Unshare stops offering the Project id to the link with, or to every link when with is "". Requests a front Instance still forwards for it are refused.

type RefusedError

type RefusedError struct {
	Reason, Msg string
}

RefusedError is a link request the other Instance refused.

func (*RefusedError) Error

func (e *RefusedError) Error() string

type Request

type Request struct {
	// Replaces names the link that approving it replaces: one to the same
	// Instance ID, pinned to another CA, as after `gcpemu ca rotate`.
	Replaces string `json:"replaces,omitempty"`
	ID       string `json:"id"`
	// Name and InstanceID are the requesting Instance's.
	Name        string    `json:"name"`
	InstanceID  string    `json:"instanceId"`
	Fingerprint string    `json:"fingerprint"`
	Address     string    `json:"address,omitempty"`
	Version     int       `json:"version"`
	Received    time.Time `json:"received"`
}

Request is an incoming Pairing request waiting for this side's approval.

type ServeFunc

type ServeFunc func(w http.ResponseWriter, r *http.Request, from, principal string)

ServeFunc serves a request forwarded over the link from, whose caller the other Instance asserts is principal.

type Share

type Share struct {
	Project string `json:"project"`
	With    string `json:"with,omitempty"`
}

Share is a Project an Instance shares with the link With, or with every link it has when With is "" (ADR 0027).

type SigningKey

type SigningKey struct {
	KeyID string `json:"keyId"`
	// JWK is the public key as a JSON Web Key.
	JWK     map[string]string `json:"jwk"`
	CertPEM string            `json:"certPem"`
}

SigningKey is an Instance's ID token signing key, as its certificate endpoints list it.

type Status

type Status struct {
	Name string `json:"name"`
	// State is StatePaired or StateBroken; "" for no such link.
	State string `json:"state"`
	// InboundOnly: the other Instance has no --link-bind address, so this
	// side cannot dial it; it connects in.
	InboundOnly bool `json:"inboundOnly,omitempty"`
	// Reachable reports whether the other Instance answered over the link,
	// speaking Version.
	Reachable bool   `json:"reachable"`
	Version   int    `json:"version,omitempty"`
	Error     string `json:"error,omitempty"`
	// Warning is an unverified sign of trouble, as LinkState's.
	Warning string `json:"warning,omitempty"`
	// Shared is the Projects the other Instance shares with this one, which
	// it can delegate; nil unless Reachable, or when asking failed, which
	// leaves it reachable all the same.
	Shared []string `json:"shared,omitempty"`
}

Status is a link's state as this side sees it.

type Store

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

Store keeps an Instance's Links, Shares and Delegations in a file, or in memory only.

func OpenStore

func OpenStore(path string) (*Store, error)

OpenStore opens the Links in the file at path; a missing file has none. An empty path keeps them in memory, as an Ephemeral Instance does.

func (*Store) Delegations

func (s *Store) Delegations() []Delegation

Delegations returns the Delegations by Project.

func (*Store) List

func (s *Store) List() []Link

List returns the Links by name.

func (*Store) Shares

func (s *Store) Shares() []Share

Shares returns the Shares by Project.

func (*Store) Update

func (s *Store) Update(change func([]Link) ([]Link, error)) error

Update replaces the Links with what change makes of them, and saves them; an error from change changes nothing.

type Versions

type Versions struct{ Min, Max int }

Versions is the range of link protocol versions an Instance speaks.

func (Versions) String

func (v Versions) String() string

String is "1", or "1-2" for a range.

Jump to

Keyboard shortcuts

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