-
Notifications
You must be signed in to change notification settings - Fork 1
feat(apps/infra): fixes Check AWS Access implementation #261
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,14 @@ import ( | |
| "bytes" | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
| "time" | ||
|
|
||
| fc "github.com/kloudlite/api/apps/infra/internal/entities/field-constants" | ||
| "github.com/kloudlite/api/common/fields" | ||
| "github.com/kloudlite/api/pkg/errors" | ||
| "github.com/kloudlite/operator/pkg/constants" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "strings" | ||
| "time" | ||
|
|
||
| iamT "github.com/kloudlite/api/apps/iam/types" | ||
| "github.com/kloudlite/api/common" | ||
|
|
@@ -22,7 +23,9 @@ import ( | |
| "github.com/kloudlite/api/pkg/repos" | ||
|
|
||
| "github.com/aws/aws-sdk-go/aws" | ||
| "github.com/aws/aws-sdk-go/aws/credentials" | ||
| "github.com/aws/aws-sdk-go/aws/session" | ||
| "github.com/aws/aws-sdk-go/service/cloudformation" | ||
| "github.com/aws/aws-sdk-go/service/sts" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| ) | ||
|
|
@@ -49,13 +52,15 @@ func generateAWSCloudformationTemplateUrl(args entities.AWSSecretCredentials, ev | |
| return result.String(), nil | ||
| } | ||
|
|
||
| func (d *domain) validateAWSAssumeRole(_ context.Context, paramExternalId string, roleARN string) error { | ||
| func (d *domain) validateAWSAssumeRole(_ context.Context, paramExternalId string, roleARN string, instanceProfileName string, cfStackName string) error { | ||
| sess, err := session.NewSession() | ||
| sess.Config.Region = aws.String("ap-south-1") | ||
| if err != nil { | ||
| d.logger.Errorf(err, "while creating new session") | ||
| return errors.NewE(err) | ||
| } | ||
|
|
||
| // 1. validating IAM Assume Role | ||
| svc := sts.New(sess) | ||
|
|
||
| resp, err := svc.AssumeRole(&sts.AssumeRoleInput{ | ||
|
|
@@ -68,8 +73,39 @@ func (d *domain) validateAWSAssumeRole(_ context.Context, paramExternalId string | |
| return errors.NewE(err) | ||
| } | ||
|
|
||
| if resp.AssumedRoleUser.Arn != nil { | ||
| return nil | ||
| if resp.AssumedRoleUser == nil || resp.AssumedRoleUser.Arn == nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (llm): The error message for a missing AWS assume role does not specify whether the role or the ARN is missing. Providing a more detailed error message could help with debugging. |
||
| return errors.Newf("AWS assume role (%s) not found", roleARN) | ||
| } | ||
|
|
||
| nsess, err := session.NewSession(&aws.Config{ | ||
| Region: aws.String("ap-south-1"), | ||
| Credentials: credentials.NewStaticCredentials(*resp.Credentials.AccessKeyId, *resp.Credentials.SecretAccessKey, *resp.Credentials.SessionToken), | ||
| }) | ||
| if err != nil { | ||
| return errors.NewE(err) | ||
| } | ||
|
|
||
| cf := cloudformation.New(nsess) | ||
| dso, err := cf.DescribeStacks(&cloudformation.DescribeStacksInput{ | ||
| StackName: &cfStackName, | ||
| }) | ||
| if err != nil { | ||
| return errors.NewE(err) | ||
| } | ||
|
|
||
| stackFound := false | ||
|
|
||
| for i := range dso.Stacks { | ||
| if dso.Stacks[i] != nil && *dso.Stacks[i].StackName == cfStackName { | ||
| stackFound = true | ||
| if *dso.Stacks[i].StackStatus != cloudformation.StackStatusCreateComplete { | ||
| return errors.Newf("cloudformation stack (%s) is not completed, yet", cfStackName) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if !stackFound { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (llm): The error message 'waiting for cloudformation stack to be created' might be misleading as it suggests an action (waiting) rather than the actual error state (stack not found). Consider rephrasing to clearly indicate that the stack was not found. |
||
| return errors.Newf("waiting for cloudformation stack to be created") | ||
| } | ||
|
|
||
| return nil | ||
|
|
@@ -94,7 +130,7 @@ func (d *domain) ValidateProviderSecretAWSAccess(ctx InfraContext, name string) | |
| return nil, errors.NewE(err) | ||
| } | ||
|
|
||
| if err := d.validateAWSAssumeRole(ctx, psecret.AWS.CfParamExternalID, psecret.AWS.GetAssumeRoleRoleARN()); err != nil { | ||
| if err := d.validateAWSAssumeRole(ctx, psecret.AWS.CfParamExternalID, psecret.AWS.GetAssumeRoleRoleARN(), psecret.AWS.CfParamInstanceProfileName, psecret.AWS.CfParamStackName); err != nil { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (llm): Adding parameters 'instanceProfileName' and 'cfStackName' to 'validateAWSAssumeRole' significantly increases the function's responsibilities. Consider refactoring to maintain single responsibility principle, possibly by splitting the validation of the assume role and the cloud formation stack into separate functions. |
||
| installationURL, err := generateAWSCloudformationTemplateUrl(*psecret.AWS, d.env) | ||
| if err != nil { | ||
| return nil, errors.NewE(err) | ||
|
|
@@ -246,7 +282,6 @@ func (d *domain) UpdateProviderSecret(ctx InfraContext, providerSecretIn entitie | |
| }, | ||
| patchForUpdate, | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return nil, errors.NewE(err) | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggestion (llm): Hardcoding the AWS region to 'ap-south-1' may limit the flexibility of this function. Consider parameterizing the region or retrieving it from a configuration setting to enhance the function's adaptability to different deployment environments.