@@ -13,6 +13,7 @@ import (
1313 "github.com/aws/aws-sdk-go-v2/service/iam"
1414 "github.com/aws/aws-sdk-go-v2/service/iam/types"
1515 "github.com/aws/smithy-go/logging"
16+ c "github.com/scality/cosi-driver/pkg/constants"
1617 "github.com/scality/cosi-driver/pkg/util"
1718 "k8s.io/klog/v2"
1819)
@@ -48,6 +49,7 @@ var InitIAMClient = func(params util.StorageClientParameters) (*IAMClient, error
4849 }
4950
5051 if strings .HasPrefix (params .IAMEndpoint , "https://" ) {
52+ klog .V (c .LvlDebug ).InfoS ("Configuring TLS transport for IAM client" , "IAMEndpoint" , params .IAMEndpoint )
5153 httpClient .Transport = util .ConfigureTLSTransport (params .TLSCert )
5254 }
5355
@@ -60,7 +62,7 @@ var InitIAMClient = func(params util.StorageClientParameters) (*IAMClient, error
6062 config .WithLogger (logger ),
6163 )
6264 if err != nil {
63- return nil , fmt . Errorf ( "failed to load AWS config: %w" , err )
65+ return nil , err
6466 }
6567
6668 iamClient := iam .NewFromConfig (awsCfg , func (o * iam.Options ) {
@@ -79,12 +81,7 @@ func (client *IAMClient) CreateUser(ctx context.Context, userName string) error
7981 }
8082
8183 _ , err := client .IAMService .CreateUser (ctx , input )
82- if err != nil {
83- return fmt .Errorf ("failed to create IAM user %s: %w" , userName , err )
84- }
85-
86- klog .InfoS ("IAM user creation succeeded" , "user" , userName )
87- return nil
84+ return err
8885}
8986
9087// AttachS3WildcardInlinePolicy attaches an inline policy to an IAM user for a specific bucket.
@@ -110,12 +107,7 @@ func (client *IAMClient) AttachS3WildcardInlinePolicy(ctx context.Context, userN
110107 }
111108
112109 _ , err := client .IAMService .PutUserPolicy (ctx , input )
113- if err != nil {
114- return fmt .Errorf ("failed to attach inline policy to IAM user %s: %w" , userName , err )
115- }
116-
117- klog .InfoS ("Inline policy attachment succeeded" , "user" , userName , "policyName" , bucketName )
118- return nil
110+ return err
119111}
120112
121113// CreateAccessKey generates access keys for an IAM user.
@@ -125,12 +117,7 @@ func (client *IAMClient) CreateAccessKey(ctx context.Context, userName string) (
125117 }
126118
127119 output , err := client .IAMService .CreateAccessKey (ctx , input )
128- if err != nil {
129- return nil , fmt .Errorf ("failed to create access key for IAM user %s: %w" , userName , err )
130- }
131-
132- klog .InfoS ("Access key creation succeeded" , "user" , userName )
133- return output , nil
120+ return output , err
134121}
135122
136123// CreateBucketAccess is a helper that combines user creation, policy attachment, and access key generation.
@@ -139,16 +126,19 @@ func (client *IAMClient) CreateBucketAccess(ctx context.Context, userName, bucke
139126 if err != nil {
140127 return nil , err
141128 }
129+ klog .V (c .LvlInfo ).InfoS ("Successfully created IAM user" , "userName" , userName )
142130
143131 err = client .AttachS3WildcardInlinePolicy (ctx , userName , bucketName )
144132 if err != nil {
145133 return nil , err
146134 }
135+ klog .V (c .LvlInfo ).InfoS ("Successfully attached inline policy" , "userName" , userName , "policyName" , bucketName )
147136
148137 accessKeyOutput , err := client .CreateAccessKey (ctx , userName )
149138 if err != nil {
150139 return nil , err
151140 }
141+ klog .V (c .LvlInfo ).InfoS ("Successfully created access key" , "userName" , userName )
152142
153143 return accessKeyOutput , nil
154144}
@@ -159,32 +149,31 @@ func (client *IAMClient) RevokeBucketAccess(ctx context.Context, userName, bucke
159149 if err != nil {
160150 return err
161151 }
152+ klog .V (c .LvlInfo ).InfoS ("Verified IAM user exists" , "userName" , userName )
162153
163154 err = client .DeleteInlinePolicy (ctx , userName , bucketName )
164155 if err != nil {
165156 return err
166157 }
158+ klog .V (c .LvlInfo ).InfoS ("Deleted inline policy if it existed" , "userName" , userName , "policyName" , bucketName )
167159
168160 err = client .DeleteAllAccessKeys (ctx , userName )
169161 if err != nil {
170162 return err
171163 }
164+ klog .V (c .LvlInfo ).InfoS ("Deleted all access keys if any existed" , "userName" , userName )
172165
173166 err = client .DeleteUser (ctx , userName )
174167 if err != nil {
175168 return err
176169 }
177-
178- klog .InfoS ("Successfully revoked bucket access" , "user" , userName , "bucket" , bucketName )
170+ klog .V (c .LvlInfo ).InfoS ("Deleted IAM user" , "userName" , userName )
179171 return nil
180172}
181173
182174func (client * IAMClient ) EnsureUserExists (ctx context.Context , userName string ) error {
183175 _ , err := client .IAMService .GetUser (ctx , & iam.GetUserInput {UserName : & userName })
184- if err != nil {
185- return fmt .Errorf ("failed to get IAM user %s: %w" , userName , err )
186- }
187- return nil
176+ return err
188177}
189178
190179func (client * IAMClient ) DeleteInlinePolicy (ctx context.Context , userName , bucketName string ) error {
@@ -195,36 +184,37 @@ func (client *IAMClient) DeleteInlinePolicy(ctx context.Context, userName, bucke
195184 if err != nil {
196185 var noSuchEntityErr * types.NoSuchEntityException
197186 if errors .As (err , & noSuchEntityErr ) {
198- klog .V (3 ).InfoS ("Inline policy does not exist, skipping deletion" , "user" , userName , "policyName" , bucketName )
187+ klog .V (c . LvlDebug ).InfoS ("Inline policy does not exist, skipping deletion" , "user" , userName , "policyName" , bucketName )
199188 return nil
200189 }
201- return fmt . Errorf ( "failed to delete inline policy %s for user %s: %w" , bucketName , userName , err )
190+ return err
202191 }
203- klog .InfoS ("Successfully deleted inline policy" , "user " , userName , "policyName" , bucketName )
192+ klog .V ( c . LvlDebug ). InfoS ("Successfully deleted inline policy" , "userName " , userName , "policyName" , bucketName )
204193 return nil
205194}
206195
207196func (client * IAMClient ) DeleteAllAccessKeys (ctx context.Context , userName string ) error {
208197 listKeysOutput , err := client .IAMService .ListAccessKeys (ctx , & iam.ListAccessKeysInput {UserName : & userName })
209198 if err != nil {
210- return fmt . Errorf ( "failed to list access keys for IAM user %s: %w" , userName , err )
199+ return err
211200 }
212201 var noSuchEntityErr * types.NoSuchEntityException
213202 for _ , key := range listKeysOutput .AccessKeyMetadata {
203+ klog .V (c .LvlTrace ).InfoS ("Deleting access key" , "userName" , userName , "accessKeyId" , * key .AccessKeyId )
214204 _ , err := client .IAMService .DeleteAccessKey (ctx , & iam.DeleteAccessKeyInput {
215205 UserName : & userName ,
216206 AccessKeyId : key .AccessKeyId ,
217207 })
218208 if err != nil {
219209 if errors .As (err , & noSuchEntityErr ) {
220- klog .V (5 ).InfoS ("Access key does not exist, skipping deletion" , "user " , userName , "accessKeyId" , * key .AccessKeyId )
210+ klog .V (c . LvlTrace ).InfoS ("Access key does not exist, skipping deletion" , "userName " , userName , "accessKeyId" , * key .AccessKeyId )
221211 continue
222212 }
223- return fmt . Errorf ( "failed to delete access key %s for IAM user %s: %w" , * key . AccessKeyId , userName , err )
213+ return err
224214 }
225- klog .V (5 ).InfoS ("Successfully deleted access key" , "user " , userName , "accessKeyId" , * key .AccessKeyId )
215+ klog .V (c . LvlTrace ).InfoS ("Successfully deleted access key" , "userName " , userName , "accessKeyId" , * key .AccessKeyId )
226216 }
227- klog .InfoS ("Successfully deleted all access keys" , "user " , userName )
217+ klog .V ( c . LvlDebug ). InfoS ("Successfully deleted all access keys" , "userName " , userName )
228218 return nil
229219}
230220
@@ -236,8 +226,7 @@ func (client *IAMClient) DeleteUser(ctx context.Context, userName string) error
236226 klog .InfoS ("IAM user does not exist, skipping deletion" , "user" , userName )
237227 return nil // User doesn't exist, nothing to delete
238228 }
239- return fmt . Errorf ( "failed to delete IAM user %s: %w" , userName , err )
229+ return err
240230 }
241- klog .InfoS ("Successfully deleted IAM user" , "user" , userName )
242231 return nil
243232}
0 commit comments