awshelpers

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	AuthAssumeRoleEnvVar = "INFRASPEC_IAM_ROLE" // OS environment variable name through which Assume Role ARN may be passed for authentication
	// InfraspecCloudAccessKeyID is the access key ID used when authenticating with an InfraSpec Cloud token
	InfraspecCloudAccessKeyID        = "infraspec-api"
	InfraspecCloudDefaultEndpointURL = "https://infraspec.sh"
)
View Source
const (
	// HealthCheckTimeout is the maximum time to wait for the health check
	HealthCheckTimeout = 5 * time.Second
	// HealthCheckPath is the path to the health check endpoint
	HealthCheckPath = "/_health"
)

Variables

This section is empty.

Functions

func BuildServiceEndpoint added in v0.1.0

func BuildServiceEndpoint(baseEndpoint, subdomain string) string

BuildServiceEndpoint constructs a service-specific endpoint URL by adding a subdomain to the base endpoint. For example:

func CheckVirtualCloudHealth added in v0.1.0

func CheckVirtualCloudHealth() error

CheckVirtualCloudHealth verifies that the InfraSpec Virtual Cloud API is accessible. It skips the check if endpoints are overridden for localhost testing. Returns nil if the health check passes or if it should be skipped, otherwise returns an error.

func GetAllAwsRegions

func GetAllAwsRegions() ([]string, error)

GetAllAwsRegions gets the list of AWS regions available in this account.

func GetAvailabilityZones

func GetAvailabilityZones(region string) ([]string, error)

GetAvailabilityZones gets the Availability Zones for a given AWS region. Note that for certain regions (e.g. us-east-1), different AWS accounts have access to different availability zones.

func GetRandomRegion

func GetRandomRegion(approvedRegions, forbiddenRegions []string) (string, error)

GetRandomRegion gets a randomly chosen AWS region. If approvedRegions is not empty, this will be a region from the approvedRegions list; otherwise, this method will fetch the latest list of regions from the AWS APIs and pick one of those. If forbiddenRegions is not empty, this method will make sure the returned region is not in the forbiddenRegions list.

func GetRandomRegionForService

func GetRandomRegionForService(serviceName string) (string, error)

GetRandomRegionForService retrieves a list of AWS regions in which a service is available Then returns one region randomly from the list

func GetRandomStableRegion

func GetRandomStableRegion(approvedRegions, forbiddenRegions []string) (string, error)

GetRandomStableRegion gets a randomly chosen AWS region that is considered stable. Like GetRandomRegion, you can further restrict the stable region list using approvedRegions and forbiddenRegions. We consider stable regions to be those that have been around for at least 1 year. Note that regions in the approvedRegions list that are not considered stable are ignored.

func GetRegionsForService

func GetRegionsForService(serviceName string) ([]string, error)

GetRegionsForService gets all AWS regions in which a service is available and returns errors. See https://docs.aws.amazon.com/systems-manager/latest/userguide/parameter-store-public-parameters-global-infrastructure.html

func GetVirtualCloudEndpoint added in v0.1.0

func GetVirtualCloudEndpoint(service string) (string, bool)

GetVirtualCloudEndpoint returns the endpoint URL to use for the given AWS service when InfraSpec Virtual Cloud mode is enabled. The function looks for a service-specific environment variable (e.g. AWS_ENDPOINT_URL_RDS) and falls back to AWS_ENDPOINT_URL, finally defaulting to the InfraSpec Cloud endpoint with service-specific subdomain.

If service is empty, returns the base endpoint URL without subdomain construction. Otherwise, constructs a service-specific subdomain endpoint (e.g. https://dynamodb.infraspec.sh).

func NewAuthenticatedSession

func NewAuthenticatedSession(region string) (*aws.Config, error)

NewAuthenticatedSession creates an AWS Config following to standard AWS authentication workflow. If an InfraSpec Cloud token is configured, it uses that token as the secret access key with "infraspec-api" as the access key ID. If `INFRASPEC_IAM_ROLE` environment variable is set, it assumes IAM role specified in it. Otherwise, uses default credentials.

func NewAuthenticatedSessionFromDefaultCredentials

func NewAuthenticatedSessionFromDefaultCredentials(region string) (*aws.Config, error)

NewAuthenticatedSessionFromDefaultCredentials gets an AWS Config, checking that the user has credentials properly configured in their environment.

func NewAuthenticatedSessionFromInfraspecCloudToken added in v0.1.0

func NewAuthenticatedSessionFromInfraspecCloudToken(region, token string) (*aws.Config, error)

func NewAuthenticatedSessionFromRole

func NewAuthenticatedSessionFromRole(region, roleARN string) (*aws.Config, error)

NewAuthenticatedSessionFromRole returns a new AWS Config after assuming the role whose ARN is provided in roleARN. If the credentials are not properly configured in the underlying environment, an error is returned.

func NewAuthenticatedSessionWithDefaultRegion added in v0.1.0

func NewAuthenticatedSessionWithDefaultRegion() (*aws.Config, error)

NewAuthenticatedSessionWithDefaultRegion creates an AWS Config with the default region.

func NewEc2FullClient added in v0.1.1

func NewEc2FullClient(region string) (*ec2.Client, error)

NewEc2FullClient creates a full EC2 client (not limited to EC2API interface).

func NewEc2FullClientWithDefaultRegion added in v0.1.1

func NewEc2FullClientWithDefaultRegion() (*ec2.Client, error)

NewEc2FullClientWithDefaultRegion creates an EC2 client with the default region.

func NewRdsClient

func NewRdsClient(region string) (*rds.Client, error)

NewRdsClient creates an RDS client.

func NewRdsClientWithDefaultRegion

func NewRdsClientWithDefaultRegion() (*rds.Client, error)

NewRdsClientWithDefaultRegion creates an RDS client with the default region.

Types

type CredentialsError

type CredentialsError struct {
	UnderlyingErr error
}

CredentialsError is an error that occurs because AWS credentials can't be found.

func (CredentialsError) Error

func (err CredentialsError) Error() string

type EC2API added in v0.1.0

type EC2API interface {
	DescribeRegions(ctx context.Context, params *ec2.DescribeRegionsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeRegionsOutput, error)
	DescribeAvailabilityZones(ctx context.Context, params *ec2.DescribeAvailabilityZonesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeAvailabilityZonesOutput, error)
}

EC2API is an interface that matches the methods we use from ec2.Client

func NewEc2Client

func NewEc2Client(region string) (EC2API, error)

NewEc2Client creates an EC2 client that implements EC2API interface.

type MockEC2Client added in v0.1.0

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

MockEC2Client implements EC2API using golden files

func NewMockEC2Client added in v0.1.0

func NewMockEC2Client(region string) *MockEC2Client

NewMockEC2Client creates a new mock EC2 client

func (*MockEC2Client) DescribeAvailabilityZones added in v0.1.0

func (m *MockEC2Client) DescribeAvailabilityZones(ctx context.Context, params *ec2.DescribeAvailabilityZonesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeAvailabilityZonesOutput, error)

DescribeAvailabilityZones loads availability zones from golden file

func (*MockEC2Client) DescribeRegions added in v0.1.0

func (m *MockEC2Client) DescribeRegions(ctx context.Context, params *ec2.DescribeRegionsInput, optFns ...func(*ec2.Options)) (*ec2.DescribeRegionsOutput, error)

DescribeRegions loads regions from golden file

type MockSSMClient added in v0.1.0

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

MockSSMClient implements SSMAPI using golden files

func NewMockSSMClient added in v0.1.0

func NewMockSSMClient(region string) *MockSSMClient

NewMockSSMClient creates a new mock SSM client

func (*MockSSMClient) GetParametersByPath added in v0.1.0

func (m *MockSSMClient) GetParametersByPath(ctx context.Context, params *ssm.GetParametersByPathInput, optFns ...func(*ssm.Options)) (*ssm.GetParametersByPathOutput, error)

GetParametersByPath loads SSM parameters from golden file

type SSMAPI added in v0.1.0

type SSMAPI interface {
	GetParametersByPath(ctx context.Context, params *ssm.GetParametersByPathInput, optFns ...func(*ssm.Options)) (*ssm.GetParametersByPathOutput, error)
}

SSMAPI is an interface that matches the methods we use from ssm.Client

func NewSsmClient

func NewSsmClient(region string) (SSMAPI, error)

NewSsmClient creates an SSM client.

Jump to

Keyboard shortcuts

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