diff --git a/pkg/clients/iam/iam_client.go b/pkg/clients/iam/iam_client.go index 736ade1f..644c86ed 100644 --- a/pkg/clients/iam/iam_client.go +++ b/pkg/clients/iam/iam_client.go @@ -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) @@ -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, "")), diff --git a/pkg/clients/iam/iam_client_test.go b/pkg/clients/iam/iam_client_test.go index a12786ab..f7b82c84 100644 --- a/pkg/clients/iam/iam_client_test.go +++ b/pkg/clients/iam/iam_client_test.go @@ -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") @@ -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") @@ -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) @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -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") @@ -233,7 +233,7 @@ 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 }() @@ -241,12 +241,12 @@ var _ = Describe("IAMClient", func() { 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 @@ -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") }) @@ -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") @@ -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") @@ -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") @@ -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") @@ -341,7 +341,7 @@ 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") @@ -349,7 +349,7 @@ var _ = Describe("IAMClient", func() { }) 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") @@ -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") @@ -374,7 +374,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") @@ -382,7 +382,7 @@ var _ = Describe("IAMClient", func() { }) 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") @@ -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") @@ -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") @@ -421,7 +421,7 @@ 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") @@ -429,7 +429,7 @@ var _ = Describe("IAMClient", func() { }) 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") @@ -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") @@ -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") @@ -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") diff --git a/pkg/clients/s3/s3_client.go b/pkg/clients/s3/s3_client.go index 9b84368c..72a41874 100644 --- a/pkg/clients/s3/s3_client.go +++ b/pkg/clients/s3/s3_client.go @@ -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) @@ -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, "")), diff --git a/pkg/clients/s3/s3_client_test.go b/pkg/clients/s3/s3_client_test.go index 01c5fe04..f159ee25 100644 --- a/pkg/clients/s3/s3_client_test.go +++ b/pkg/clients/s3/s3_client_test.go @@ -36,15 +36,15 @@ 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()) @@ -52,7 +52,7 @@ var _ = Describe("S3Client", func() { 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 }() @@ -60,12 +60,12 @@ var _ = Describe("S3Client", func() { 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 @@ -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") }) @@ -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 }) @@ -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) @@ -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) diff --git a/pkg/driver/provisioner_server_impl.go b/pkg/driver/provisioner_server_impl.go index d5cba7cc..16ace71e 100644 --- a/pkg/driver/provisioner_server_impl.go +++ b/pkg/driver/provisioner_server_impl.go @@ -315,14 +315,14 @@ func initializeObjectStorageClient(ctx context.Context, clientset kubernetes.Int var client interface{} switch service { case "S3": - client, err = s3client.InitS3Client(*storageClientParameters) + client, err = s3client.InitS3Client(ctx, *storageClientParameters) if err != nil { klog.ErrorS(err, "Failed to initialize S3 client", "endpoint", storageClientParameters.Endpoint) return nil, nil, status.Error(codes.Internal, "failed to initialize S3 client") } klog.V(c.LvlDebug).InfoS("Successfully initialized S3 client", "endpoint", storageClientParameters.Endpoint) case "IAM": - client, err = iamclient.InitIAMClient(*storageClientParameters) + client, err = iamclient.InitIAMClient(ctx, *storageClientParameters) if err != nil { klog.ErrorS(err, "Failed to initialize IAM client", "endpoint", storageClientParameters.Endpoint) return nil, nil, status.Error(codes.Internal, "failed to initialize IAM client") diff --git a/pkg/driver/provisioner_server_impl_test.go b/pkg/driver/provisioner_server_impl_test.go index 6775c5fa..367f4a09 100644 --- a/pkg/driver/provisioner_server_impl_test.go +++ b/pkg/driver/provisioner_server_impl_test.go @@ -431,7 +431,7 @@ var _ = Describe("initializeObjectStorageClient", Ordered, func() { originalInitS3Client := s3client.InitS3Client defer func() { s3client.InitS3Client = originalInitS3Client }() - s3client.InitS3Client = func(params util.StorageClientParameters) (*s3client.S3Client, error) { + s3client.InitS3Client = func(ctx context.Context, params util.StorageClientParameters) (*s3client.S3Client, error) { return nil, fmt.Errorf("mock S3 client error") } @@ -456,7 +456,7 @@ var _ = Describe("initializeObjectStorageClient", Ordered, func() { originalInitIAMClient := iamclient.InitIAMClient defer func() { iamclient.InitIAMClient = originalInitIAMClient }() - iamclient.InitIAMClient = func(params util.StorageClientParameters) (*iamclient.IAMClient, error) { + iamclient.InitIAMClient = func(ctx context.Context, params util.StorageClientParameters) (*iamclient.IAMClient, error) { return nil, fmt.Errorf("mock IAM error") }