Skip to content

Commit

Permalink
COSI-19: refactor-use ctm in init s3 anc iam
Browse files Browse the repository at this point in the history
  • Loading branch information
anurag4DSB committed Jan 2, 2025
1 parent 6701bdc commit 42f9128
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 52 deletions.
4 changes: 1 addition & 3 deletions pkg/clients/iam/iam_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type IAMClient struct {

var LoadAWSConfig = config.LoadDefaultConfig

var InitIAMClient = func(params util.StorageClientParameters) (*IAMClient, error) {
var InitIAMClient = func(ctx context.Context, params util.StorageClientParameters) (*IAMClient, error) {
var logger logging.Logger
if params.Debug {
logger = logging.NewStandardLogger(os.Stdout)
Expand All @@ -55,8 +55,6 @@ var InitIAMClient = func(params util.StorageClientParameters) (*IAMClient, error
httpClient.Transport = util.ConfigureTLSTransport(params.TLSCert)
}

ctx := context.Background()

awsCfg, err := LoadAWSConfig(ctx,
config.WithRegion(params.Region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(params.AccessKeyID, params.SecretAccessKey, "")),
Expand Down
60 changes: 30 additions & 30 deletions pkg/clients/iam/iam_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var _ = Describe("IAMClient", func() {
return &iam.CreateUserOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.CreateUser(ctx, "test-user")
Expand All @@ -64,7 +64,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated CreateUser failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.CreateUser(ctx, "test-user")
Expand All @@ -83,7 +83,7 @@ var _ = Describe("IAMClient", func() {
return &iam.PutUserPolicyOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.AttachS3WildcardInlinePolicy(ctx, "test-user", bucketName)
Expand All @@ -95,7 +95,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated PutUserPolicy failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.AttachS3WildcardInlinePolicy(ctx, "test-user", "test-bucket")
Expand All @@ -114,7 +114,7 @@ var _ = Describe("IAMClient", func() {
}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateAccessKey(ctx, "test-user")
Expand All @@ -128,7 +128,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated CreateAccessKey failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateAccessKey(ctx, "test-user")
Expand Down Expand Up @@ -167,7 +167,7 @@ var _ = Describe("IAMClient", func() {
}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -182,7 +182,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated CreateUser failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -200,7 +200,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated AttachS3WildcardInlinePolicy failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -222,7 +222,7 @@ var _ = Describe("IAMClient", func() {
return nil, fmt.Errorf("simulated CreateAccessKey failure")
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

output, err := client.CreateBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -233,20 +233,20 @@ var _ = Describe("IAMClient", func() {
})

Describe("InitIAMClient", func() {
It("should return an error if AWS config loading fails", func() {
It("should return an error if AWS config loading fails", func(ctx SpecContext) {
originalLoadAWSConfig := iamclient.LoadAWSConfig
defer func() { iamclient.LoadAWSConfig = originalLoadAWSConfig }()

iamclient.LoadAWSConfig = func(ctx context.Context, optFns ...func(*config.LoadOptions) error) (aws.Config, error) {
return aws.Config{}, fmt.Errorf("mock LoadAWSConfig failure")
}

client, err := iamclient.InitIAMClient(params)
client, err := iamclient.InitIAMClient(ctx, params)
Expect(err).To(HaveOccurred())
Expect(client).To(BeNil())
})

It("should set up a logger when Debug is enabled", func() {
It("should set up a logger when Debug is enabled", func(ctx SpecContext) {
params.Debug = true

// Mock LoadAWSConfig
Expand All @@ -266,7 +266,7 @@ var _ = Describe("IAMClient", func() {
return aws.Config{}, nil // Simulate a successful load
}

_, err := iamclient.InitIAMClient(params)
_, err := iamclient.InitIAMClient(ctx, params)
Expect(err).To(BeNil())
Expect(loggerUsed).To(BeTrue(), "Expected logger to be used when Debug is enabled")
})
Expand All @@ -290,7 +290,7 @@ var _ = Describe("IAMClient", func() {
return nil, noSuchEntityError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "non-existent-user", "test-bucket")
Expand All @@ -303,7 +303,7 @@ var _ = Describe("IAMClient", func() {
return nil, noSuchEntityError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -316,7 +316,7 @@ var _ = Describe("IAMClient", func() {
return nil, noSuchEntityError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -328,7 +328,7 @@ var _ = Describe("IAMClient", func() {
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -341,15 +341,15 @@ var _ = Describe("IAMClient", func() {
return nil, &types.NoSuchEntityException{}
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expect(err).To(BeNil())
})

It("should successfully delete all access keys for the user", func(ctx SpecContext) {
client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -361,7 +361,7 @@ var _ = Describe("IAMClient", func() {
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -374,15 +374,15 @@ var _ = Describe("IAMClient", func() {
return nil, noSuchEntityError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expect(err).To(BeNil())
})

It("should successfully delete the user", func(ctx SpecContext) {
client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -394,7 +394,7 @@ var _ = Describe("IAMClient", func() {
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.RevokeBucketAccess(ctx, "test-user", "test-bucket")
Expand All @@ -409,7 +409,7 @@ var _ = Describe("IAMClient", func() {
return &iam.DeleteUserPolicyOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteInlinePolicy(ctx, "test-user", "test-bucket")
Expand All @@ -421,15 +421,15 @@ var _ = Describe("IAMClient", func() {
return nil, &types.NoSuchEntityException{}
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteInlinePolicy(ctx, "test-user", "test-bucket")
Expect(err).To(BeNil())
})

It("should successfully delete all access keys", func(ctx SpecContext) {
client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteAllAccessKeys(ctx, "test-user")
Expand All @@ -442,7 +442,7 @@ var _ = Describe("IAMClient", func() {
return &iam.DeleteUserOutput{}, nil
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteUser(ctx, "test-user")
Expand All @@ -454,7 +454,7 @@ var _ = Describe("IAMClient", func() {
return nil, &types.NoSuchEntityException{}
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteUser(ctx, "test-user")
Expand All @@ -466,7 +466,7 @@ var _ = Describe("IAMClient", func() {
return nil, accessDeniedError
}

client, _ := iamclient.InitIAMClient(params)
client, _ := iamclient.InitIAMClient(ctx, params)
client.IAMService = mockIAM

err := client.DeleteAllAccessKeys(ctx, "test-user")
Expand Down
4 changes: 1 addition & 3 deletions pkg/clients/s3/s3_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type S3Client struct {

var LoadAWSConfig = config.LoadDefaultConfig

var InitS3Client = func(params util.StorageClientParameters) (*S3Client, error) {
var InitS3Client = func(ctx context.Context, params util.StorageClientParameters) (*S3Client, error) {
var logger logging.Logger
if params.Debug {
logger = logging.NewStandardLogger(os.Stdout)
Expand All @@ -45,8 +45,6 @@ var InitS3Client = func(params util.StorageClientParameters) (*S3Client, error)
httpClient.Transport = util.ConfigureTLSTransport(params.TLSCert)
}

ctx := context.Background()

awsCfg, err := LoadAWSConfig(ctx,
config.WithRegion(params.Region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(params.AccessKeyID, params.SecretAccessKey, "")),
Expand Down
24 changes: 12 additions & 12 deletions pkg/clients/s3/s3_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,36 @@ var _ = Describe("S3Client", func() {
})

Describe("InitS3Client", func() {
It("should initialize the S3 client without error", func() {
client, err := s3client.InitS3Client(params)
It("should initialize the S3 client without error", func(ctx SpecContext) {
client, err := s3client.InitS3Client(ctx, params)
Expect(err).To(BeNil())
Expect(client).NotTo(BeNil())
Expect(client).To(BeAssignableToTypeOf(&s3client.S3Client{}))
})

It("should use the default region when none is provided", func() {
client, err := s3client.InitS3Client(params)
It("should use the default region when none is provided", func(ctx SpecContext) {
client, err := s3client.InitS3Client(ctx, params)
Expect(err).To(BeNil())
Expect(client).NotTo(BeNil())
Expect(client.S3Service).NotTo(BeNil())
opts := client.S3Service.(*s3.Client).Options()
Expect(opts.Region).To(Equal("us-east-1"))
})

It("should return an error if AWS config loading fails", func() {
It("should return an error if AWS config loading fails", func(ctx SpecContext) {
originalLoadAWSConfig := s3client.LoadAWSConfig
defer func() { s3client.LoadAWSConfig = originalLoadAWSConfig }()

s3client.LoadAWSConfig = func(ctx context.Context, optFns ...func(*config.LoadOptions) error) (aws.Config, error) {
return aws.Config{}, fmt.Errorf("mock config loading error")
}

client, err := s3client.InitS3Client(params)
client, err := s3client.InitS3Client(ctx, params)
Expect(err).To(HaveOccurred())
Expect(client).To(BeNil())
})

It("should set up a logger when Debug is enabled", func() {
It("should set up a logger when Debug is enabled", func(ctx SpecContext) {
params.Debug = true

// Mock LoadAWSConfig
Expand All @@ -85,7 +85,7 @@ var _ = Describe("S3Client", func() {
return aws.Config{}, nil // Simulate a successful load
}

_, err := s3client.InitS3Client(params)
_, err := s3client.InitS3Client(ctx, params)
Expect(err).To(BeNil())
Expect(loggerUsed).To(BeTrue(), "Expected logger to be used when Debug is enabled")
})
Expand All @@ -94,10 +94,10 @@ var _ = Describe("S3Client", func() {
Describe("CreateBucket", func() {
var mockS3 *mock.MockS3Client

BeforeEach(func() {
BeforeEach(func(ctx SpecContext) {
mockS3 = &mock.MockS3Client{}
params.Region = "us-west-2"
client, _ := s3client.InitS3Client(params)
client, _ := s3client.InitS3Client(ctx, params)
client.S3Service = mockS3
})

Expand All @@ -108,7 +108,7 @@ var _ = Describe("S3Client", func() {
return &s3.CreateBucketOutput{}, nil
}

client, _ := s3client.InitS3Client(params)
client, _ := s3client.InitS3Client(ctx, params)
client.S3Service = mockS3

err := client.CreateBucket(ctx, "new-bucket", params)
Expand All @@ -120,7 +120,7 @@ var _ = Describe("S3Client", func() {
return nil, fmt.Errorf("SomeOtherError: Something went wrong")
}

client, _ := s3client.InitS3Client(params)
client, _ := s3client.InitS3Client(ctx, params)
client.S3Service = mockS3

err := client.CreateBucket(ctx, "new-bucket", params)
Expand Down
Loading

0 comments on commit 42f9128

Please sign in to comment.