Documentation
¶
Overview ¶
Package cloudmetadata is the shared HTTP client every cloud-provider collector (ec2, gce, azure, digital_ocean, openstack, oci, linode, alibaba, scaleway) uses to talk to its provider's link-local metadata endpoint. Handles short-timeout GETs, per-provider authentication headers (Azure's `Metadata: true`, OCI's `Authorization: Bearer Oracle`), and a typed ErrNotAvailable signal so collectors can short-circuit to a nil result when the host isn't on that provider.
Tests don't stub this package. They point a Client at an httptest.Server URL and exercise the real HTTP round-trip against canned responses.
Index ¶
- Constants
- Variables
- func ClientError(err error) bool
- func SafeName(name string) bool
- type Client
- func (c *Client) Get(ctx context.Context, path string) ([]byte, error)
- func (c *Client) GetJSON(ctx context.Context, path string, out any) error
- func (c *Client) GetWithHeaders(ctx context.Context, path string, headers map[string]string) ([]byte, error)
- func (c *Client) Lookup(ctx context.Context, path string) ([]byte, error)
- func (c *Client) Put(ctx context.Context, path string, headers map[string]string) ([]byte, error)
- func (c *Client) RawGet(ctx context.Context, path string) ([]byte, int, error)
- type Option
- type StatusError
Constants ¶
const DefaultUserAgent = "gohai"
DefaultUserAgent is the User-Agent header applied to every request unless the caller overrides it via WithHeader. Matches Ohai's chef-ohai/<version> pattern, because metadata services sometimes rate- limit or filter by UA, and a stable identifier makes gohai traffic distinguishable in provider logs.
const MaxBody = 1 << 20
MaxBody is the most that one metadata response may hold. A metadata document, such as Azure's compute instance, is some kilobytes. A longer body is from an endpoint that is not the provider's, or a broken one, and fails.
Variables ¶
var ErrNotAvailable = errors.New("cloud metadata endpoint not available")
ErrNotAvailable signals the provider's metadata endpoint couldn't be reached (connection refused, timeout, DNS failure, HTTP non-2xx). Collectors unwrap this via errors.Is and return (nil, nil) so the typed field drops from Facts without surfacing a misleading error.
Functions ¶
func ClientError ¶ added in v1.19.0
ClientError reports whether err is a StatusError with a 4xx status.
func SafeName ¶ added in v1.19.0
SafeName reports whether name, a child name from a metadata directory listing, is safe to join into a request path. A trailing slash, which a listing gives a subdirectory, is ignored. A name with "..", "/", "?", "#", a space or any other character is not safe, because it changes the path or the query of the request.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a thin HTTP wrapper scoped to one provider's metadata base URL. Construct via New; swap the base URL for tests by pointing it at an httptest.Server.
func New ¶
New returns a Client rooted at baseURL. Default timeout is 2s; override with WithTimeout. Each Client shares one transport, with proxy lookup disabled (metadata IPs are link-local, so honoring HTTP_PROXY would route cloud probes through a proxy and almost certainly fail). The User-Agent header defaults to DefaultUserAgent; override with WithHeader("User-Agent", "...") if needed.
func (*Client) Get ¶
Get fetches baseURL + path and returns the response body on 2xx. Wraps transport failures and non-2xx responses in ErrNotAvailable so callers can branch with errors.Is.
func (*Client) GetJSON ¶
GetJSON fetches path and JSON-decodes into out. Transport-level failures propagate as ErrNotAvailable (same as Get); JSON decode errors propagate as-is.
func (*Client) GetWithHeaders ¶
func (c *Client) GetWithHeaders( ctx context.Context, path string, headers map[string]string, ) ([]byte, error)
GetWithHeaders is Get with per-request headers merged onto the Client's default headers. Used by EC2's IMDSv2 flow where the token header must accompany every read but isn't known until Collect runs.
func (*Client) Lookup ¶ added in v1.19.0
Lookup fetches baseURL + path and returns the body on 2xx. Only a request with no answer, or a 404, wraps ErrNotAvailable: the document is not there. Any other non-2xx, such as a 500, is an error of its own, so a metadata service that answers with a failure is not taken for a host with no such document. A cancelled context is its own error.
func (*Client) Put ¶
func (c *Client) Put( ctx context.Context, path string, headers map[string]string, ) ([]byte, error)
Put issues a PUT against baseURL + path with per-request headers merged onto the Client's default headers. It sends no body. Used by EC2's IMDSv2 token flow. EC2 is the only provider that writes rather than reads the metadata endpoint.
func (*Client) RawGet ¶
RawGet issues a GET and returns the response body + status code, without treating non-2xx as ErrNotAvailable. Used by Azure's api-version negotiation which relies on reading the HTTP 400 body Azure emits when no api-version is supplied. Transport failures still wrap ErrNotAvailable.
type Option ¶
type Option func(*Client)
Option configures a Client at construction time.
func WithHeader ¶
WithHeader adds a header sent with every request. Providers use this for auth / content negotiation (Azure's "Metadata: true", OCI's "Authorization: Bearer Oracle"). Multiple calls accumulate.
func WithTimeout ¶
WithTimeout overrides the default 2s per-request timeout.
func WithTransport ¶ added in v1.16.0
func WithTransport( rt http.RoundTripper, ) Option
WithTransport makes rt the transport of the Client's requests. A nil rt keeps the shared transport. A caller's source.Sources.HTTP comes here, so a fake host answers the metadata reads.
type StatusError ¶ added in v1.19.0
type StatusError struct {
// Status is the HTTP status code of the answer.
Status int
// Path is the path that was asked for.
Path string
}
StatusError is the error of a Lookup whose answer was a non-2xx status other than 404. A collector that probes a host which DMI did not confirm can tell a 4xx (another service, not this cloud) from a 5xx (a metadata service that failed) by Status.
func (*StatusError) Error ¶ added in v1.19.0
func (e *StatusError) Error() string
Error returns "unexpected status <status> from <path>".