Skip to content

snapshot: fetch credentials from EC2IMDS #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/snapshot/snapshotter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/rs/zerolog"
runsOnConfig "github.com/runs-on/snapshot/internal/config"
"github.com/runs-on/snapshot/internal/utils"
)

const (
Expand Down Expand Up @@ -96,7 +96,7 @@ type VolumeInfo struct {
// NewAWSSnapshotter creates a new AWSSnapshotter instance.
// It initializes the AWS SDK configuration and fetches EC2 instance metadata.
func NewAWSSnapshotter(ctx context.Context, logger *zerolog.Logger, cfg *runsOnConfig.Config) (*AWSSnapshotter, error) {
awsConfig, err := config.LoadDefaultConfig(ctx)
awsConfig, err := utils.GetAWSClientFromEC2IMDS(ctx)
if err != nil {
return nil, fmt.Errorf("failed to load AWS SDK config: %w", err)
}
Expand Down Expand Up @@ -141,7 +141,7 @@ func NewAWSSnapshotter(ctx context.Context, logger *zerolog.Logger, cfg *runsOnC
return &AWSSnapshotter{
logger: logger,
config: cfg,
ec2Client: ec2.NewFromConfig(awsConfig),
ec2Client: ec2.NewFromConfig(*awsConfig),
}, nil
}

Expand Down
24 changes: 24 additions & 0 deletions internal/utils/helpers.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package utils

import (
"context"
"encoding/json"
"fmt"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)

func PrettyPrint(v interface{}) string {
Expand All @@ -12,3 +19,20 @@ func PrettyPrint(v interface{}) string {
}
return string(b)
}

// GetAWSClientFromEC2IMDS retrieves AWS config from EC2 IMDS,
// ignoring any local AWS config (e.g. ~/.aws) and ENV variables.
//
// This ensures that we always assume RunsOn instance profile IAM role, regardless of what happens in other GHA actions/steps.
func GetAWSClientFromEC2IMDS(context context.Context) (*aws.Config, error) {
provider := ec2rolecreds.New(func(o *ec2rolecreds.Options) {
o.Client = imds.New(imds.Options{})
})

cfg, err := config.LoadDefaultConfig(context, config.WithRegion(os.Getenv("RUNS_ON_AWS_REGION")), config.WithCredentialsProvider(aws.NewCredentialsCache(provider)))
if err != nil {
return nil, fmt.Errorf("failed to load AWS config: %w", err)
}

return &cfg, nil
}
Binary file modified main-linux-amd64
Binary file not shown.
Binary file modified main-linux-arm64
Binary file not shown.