Skip to content

OCPBUGS-57923: When AWS_PROFILE is invalid ask for credentials #9803

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
26 changes: 18 additions & 8 deletions pkg/asset/installconfig/aws/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,42 @@ import (
"fmt"
"time"

awsv2 "github.com/aws/aws-sdk-go-v2/aws"
cfgv2 "github.com/aws/aws-sdk-go-v2/config"
ec2v2 "github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/sirupsen/logrus"
)

func setUserCredsInConfig(ctx context.Context) (awsv2.Config, error) {
if err := getUserCredentials(); err != nil {
return awsv2.Config{}, err
}
cfg, err := cfgv2.LoadDefaultConfig(ctx, cfgv2.WithRegion("us-east-1"))
if err != nil {
return awsv2.Config{}, fmt.Errorf("failed to create AWS config: %w", err)
}
return cfg, nil
}

// GetRegions get all regions that are accessible.
func GetRegions(ctx context.Context) ([]string, error) {
// Create a basic/default config. The function is currently called during the survey.
// Pass the default region (used for survey purposes) as the region here. Without a region
// the DescribeRegions call will fail immediately.
cfg, err := cfgv2.LoadDefaultConfig(ctx, cfgv2.WithRegion("us-east-1"))
if err != nil {
return nil, fmt.Errorf("failed to create config from platform: %w", err)
cfg, err = setUserCredsInConfig(ctx)
if err != nil {
return nil, err
}
}

if _, err = cfg.Credentials.Retrieve(ctx); err != nil {
logrus.Debugf("failed to retrieve AWS credentials: %v", err)
if err = getUserCredentials(); err != nil {
return nil, err
}
cfg, err = cfgv2.LoadDefaultConfig(ctx, cfgv2.WithRegion("us-east-1"))
cfg, err = setUserCredsInConfig(ctx)
if err != nil {
return nil, fmt.Errorf("failed to create config from platform: %w", err)
return nil, err
}
}
client := ec2v2.NewFromConfig(cfg)
Expand Down