Skip to content
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

OCM-8208 | test: Add new function: GrantValidAccessKeys for backend usage #50

Merged
merged 1 commit into from
May 31, 2024
Merged
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
38 changes: 38 additions & 0 deletions pkg/aws/aws_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (

elb "github.com/aws/aws-sdk-go-v2/service/elasticloadbalancing"
"github.com/aws/aws-sdk-go-v2/service/route53"

CON "github.com/openshift-online/ocm-common/pkg/aws/consts"
)

type AWSClient struct {
Expand All @@ -32,6 +34,12 @@ type AWSClient struct {
AccountID string
KmsClient *kms.Client
CloudWatchLogsClient *cloudwatchlogs.Client
AWSConfig *aws.Config
}

type AccessKeyMod struct {
AccessKeyId string `ini:"aws_access_key_id,omitempty"`
SecretAccessKey string `ini:"aws_secret_access_key,omitempty"`
}

func CreateAWSClient(profileName string, region string) (*AWSClient, error) {
Expand Down Expand Up @@ -79,6 +87,7 @@ func CreateAWSClient(profileName string, region string) (*AWSClient, error) {
IamClient: iam.NewFromConfig(cfg),
ClientContext: context.TODO(),
KmsClient: kms.NewFromConfig(cfg),
AWSConfig: &cfg,
}
awsClient.AccountID = awsClient.GetAWSAccountID()
return awsClient, nil
Expand Down Expand Up @@ -106,3 +115,32 @@ func (client *AWSClient) CloudFormation() *cloudformation.Client {
func (client *AWSClient) ELB() *elb.Client {
return client.ElbClient
}

func GrantValidAccessKeys(userName string) (*AccessKeyMod, error) {
var cre aws.Credentials
var keysMod *AccessKeyMod
var err error
retryTimes := 3
for retryTimes > 0 {
if cre.AccessKeyID != "" {
break
}
client, err := CreateAWSClient(userName, CON.DefaultAWSRegion)
if err != nil {
return nil, err
}

cre, err = client.AWSConfig.Credentials.Retrieve(client.ClientContext)
if err != nil {
return nil, err
}
log.LogInfo(">>> Access key grant successfully")

keysMod = &AccessKeyMod{
robpblake marked this conversation as resolved.
Show resolved Hide resolved
AccessKeyId: cre.AccessKeyID,
SecretAccessKey: cre.SecretAccessKey,
}
retryTimes--
}
return keysMod, err
}
Loading