ec2internal

package
v0.0.0-...-3b14b87 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package ec2internal contains internal helpers for EC2 metadata.

Index

Constants

View Source
const (
	MetadataSourceNone = iota
	MetadataSourceUUID
	MetadataSourceDMI
	MetadataSourceIMDSv1
	MetadataSourceIMDSv2
)

Metadata sources

View Source
const (
	// CloudProviderName contains the inventory name of for EC2
	CloudProviderName = "AWS"
	// DMIBoardVendor contains the DMI board vendor for EC2
	DMIBoardVendor = "Amazon EC2"
)

Variables

View Source
var (
	MetadataURL         = "http://169.254.169.254/latest/meta-data"
	TokenURL            = "http://169.254.169.254/latest/api/token"
	InstanceIdentityURL = "http://169.254.169.254/latest/dynamic/instance-identity/document/"

	CurrentMetadataSource = MetadataSourceNone

	Token *httputils.APIToken
)

Use variables to ease mocking in tests

Functions

func DoHTTPRequest

func DoHTTPRequest(ctx context.Context, url string, allowedIMDSVersions Ec2IMDSVersionConfig, updateMetadataSource bool) (string, error)

DoHTTPRequest performs an HTTP request to the given url with proper ec2 headers

func GetMetadataItem

func GetMetadataItem(ctx context.Context, endpoint string, allowedIMDSVersions Ec2IMDSVersionConfig, updateMetadataSource bool) (string, error)

GetMetadataItem returns the metadata item at the given endpoint

func GetMetadataItemWithMaxLength

func GetMetadataItemWithMaxLength(ctx context.Context, endpoint string, allowedIMDSVersions Ec2IMDSVersionConfig, updateMetadataSource bool) (string, error)

GetMetadataItemWithMaxLength returns the metadata item at the given endpoint with a maximum length

func GetSourceName

func GetSourceName() string

GetSourceName returns the source used to pull information for EC2

func GetToken

func GetToken(ctx context.Context) (string, time.Time, error)

GetToken fetches a new token from the EC2 metadata service

func SetCloudProviderSource

func SetCloudProviderSource(source int)

SetCloudProviderSource set the best source available for EC2 metadata to the inventories payload.

The different sources that can be used to know if we are running on EC2. This data is registered in the inventories metadata payload.

We current have 3 ways to know we're on EC2 (if one fails we fallback to the next): - we succeed in reaching IMDS v1 or v2 metadata API. - the DMI information match EC2 and we can get the instanceID from it. - the product UUID or hypervisor UUID match EC2 (we know we're on EC2 but can't fetch the instance ID).

Since some ways can temporary fail, we always register the "best" that worked at some point. This is mainly aimed at IMDS which is sometimes unavailable at startup.

Types

type EC2Identity

type EC2Identity struct {
	Region     string
	InstanceID string
	AccountID  string
}

EC2Identity holds the instances identity document nolint: revive

func GetInstanceIdentity

func GetInstanceIdentity(ctx context.Context) (*EC2Identity, error)

GetInstanceIdentity returns the instance identity document for the current instance

type Ec2IMDSVersionConfig

type Ec2IMDSVersionConfig int

Ec2IMDSVersionConfig is an enum to determine how to interact with the IMDSv2 option

const (
	ImdsV1 Ec2IMDSVersionConfig = iota
	ImdsAllVersions
	ImdsV2
)

Enum values for Ec2IMDSVersionConfig

func UseIMDSv2

func UseIMDSv2() Ec2IMDSVersionConfig

UseIMDSv2 returns true if the agent should use IMDSv2

func (Ec2IMDSVersionConfig) V2Allowed

func (v Ec2IMDSVersionConfig) V2Allowed() bool

V2Allowed returns true if the agent is allowed to use IMDSv2

func (Ec2IMDSVersionConfig) V2Only

func (v Ec2IMDSVersionConfig) V2Only() bool

V2Only returns true if the agent is forced to use IMDSv2

Jump to

Keyboard shortcuts

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