Skip to content

Commit 4f1fbf3

Browse files
authored
Update to Go 1.22 and minor refactors (#48)
* fix: log for user existence check * fix: tests OK upto secret deletion * Add re-fetch specifically for a OverrideExistingSecret edge case * Cleanup debug/useless logs * Removed some TODO/commented lines * logs: remove/replace WithName * TODO removal
1 parent 78ab524 commit 4f1fbf3

File tree

6 files changed

+353
-197
lines changed

6 files changed

+353
-197
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.21 as builder
2+
FROM golang:1.22 as builder
33
ARG TARGETOS
44
ARG TARGETARCH
55

controllers/path_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func (r *PathReconciler) SetupWithManager(mgr ctrl.Manager) error {
184184
}
185185

186186
func (r *PathReconciler) finalizePath(pathResource *s3v1alpha1.Path) error {
187-
logger := log.Log.WithName("finalize")
187+
logger := log.Log.WithValues("controller", "path")
188188
if r.PathDeletion {
189189
var failedPaths []string = make([]string, 0)
190190
for _, path := range pathResource.Spec.Paths {

controllers/s3/factory/interface.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
var (
12-
s3Logger = ctrl.Log.WithName("s3Client")
12+
s3Logger = ctrl.Log.WithValues("logger", "s3client")
1313
)
1414

1515
type S3Client interface {
@@ -30,11 +30,11 @@ type S3Client interface {
3030
UserExist(name string) (bool, error)
3131
CheckUserCredentialsValid(name string, accessKey string, secretKey string) (bool, error)
3232
AddServiceAccountForUser(name string, accessKey string, secretKey string) error
33-
CreateUser(name string, password string) error
34-
DeleteUser(name string) error
33+
CreateUser(accessKey string, secretKey string) error
34+
DeleteUser(accessKey string) error
3535
GetUserPolicies(name string) ([]string, error)
36-
AddPoliciesToUser(username string, policies []string) error
37-
RemovePoliciesFromUser(username string, policies []string) error
36+
AddPoliciesToUser(accessKey string, policies []string) error
37+
RemovePoliciesFromUser(accessKey string, policies []string) error
3838
}
3939

4040
type S3Config struct {

controllers/s3/factory/minioS3Client.go

Lines changed: 58 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,18 @@ func (minioS3Client *MinioS3Client) DeletePolicy(name string) error {
254254
// USER methods //
255255
////////////////////
256256

257-
func (minioS3Client *MinioS3Client) CreateUser(name string, password string) error {
258-
s3Logger.Info("Creating user", "user", name)
259-
err := minioS3Client.adminClient.AddUser(context.Background(), name, password)
257+
func (minioS3Client *MinioS3Client) CreateUser(accessKey string, secretKey string) error {
258+
s3Logger.Info("Creating user", "accessKey", accessKey)
259+
err := minioS3Client.adminClient.AddUser(context.Background(), accessKey, secretKey)
260260
if err != nil {
261-
s3Logger.Error(err, "Error while creating user", "user", name)
261+
s3Logger.Error(err, "Error while creating user", "user", accessKey)
262262
return err
263263
}
264264
return nil
265265
}
266266

267267
func (minioS3Client *MinioS3Client) AddServiceAccountForUser(name string, accessKey string, secretKey string) error {
268-
s3Logger.Info("Adding service account for user", "user", name)
268+
s3Logger.Info("Adding service account for user", "user", name, "accessKey", accessKey)
269269

270270
opts := madmin.AddServiceAccountReq{
271271
AccessKey: accessKey,
@@ -285,27 +285,37 @@ func (minioS3Client *MinioS3Client) AddServiceAccountForUser(name string, access
285285

286286
}
287287

288-
func (minioS3Client *MinioS3Client) UserExist(name string) (bool, error) {
289-
s3Logger.Info("checking user existence", "user", name)
290-
_, _err := minioS3Client.adminClient.GetUserInfo(context.Background(), name)
288+
func (minioS3Client *MinioS3Client) UserExist(accessKey string) (bool, error) {
289+
s3Logger.Info("checking user existence", "accessKey", accessKey)
290+
_, _err := minioS3Client.adminClient.GetUserInfo(context.Background(), accessKey)
291291
if _err != nil {
292-
s3Logger.Info("received code", "user", minio.ToErrorResponse(_err))
293-
if minio.ToErrorResponse(_err).StatusCode == 0 {
292+
if madmin.ToErrorResponse(_err).Code == "XMinioAdminNoSuchUser" {
294293
return false, nil
295294
}
295+
s3Logger.Error(_err, "an error occurred when checking user's existence")
296296
return false, _err
297297
}
298+
298299
return true, nil
299300
}
300301

301-
func (minioS3Client *MinioS3Client) DeleteUser(name string) error {
302-
s3Logger.Info("delete user", "user", name)
303-
return minioS3Client.adminClient.RemoveUser(context.Background(), name)
302+
func (minioS3Client *MinioS3Client) DeleteUser(accessKey string) error {
303+
s3Logger.Info("delete user with accessKey", "accessKey", accessKey)
304+
err := minioS3Client.adminClient.RemoveUser(context.Background(), accessKey)
305+
if err != nil {
306+
if madmin.ToErrorResponse(err).Code == "XMinioAdminNoSuchUser" {
307+
s3Logger.Info("the user was already deleted from s3 backend")
308+
return nil
309+
}
310+
s3Logger.Error(err, "an error occurred when attempting to delete the user")
311+
return err
312+
}
313+
return nil
304314
}
305315

306-
func (minioS3Client *MinioS3Client) GetUserPolicies(name string) ([]string, error) {
307-
s3Logger.Info("Get user policies", "user", name)
308-
userInfo, err := minioS3Client.adminClient.GetUserInfo(context.Background(), name)
316+
func (minioS3Client *MinioS3Client) GetUserPolicies(accessKey string) ([]string, error) {
317+
s3Logger.Info("Get user policies", "accessKey", accessKey)
318+
userInfo, err := minioS3Client.adminClient.GetUserInfo(context.Background(), accessKey)
309319
if err != nil {
310320
s3Logger.Error(err, "Error when getting userInfo")
311321

@@ -315,7 +325,7 @@ func (minioS3Client *MinioS3Client) GetUserPolicies(name string) ([]string, erro
315325
}
316326

317327
func (minioS3Client *MinioS3Client) CheckUserCredentialsValid(name string, accessKey string, secretKey string) (bool, error) {
318-
s3Logger.Info("Check credential for user", "user", name)
328+
s3Logger.Info("Check credentials for user", "user", name, "accessKey", accessKey)
319329
minioTestClientOptions := &minio.Options{
320330
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
321331
Region: minioS3Client.s3Config.Region,
@@ -324,43 +334,64 @@ func (minioS3Client *MinioS3Client) CheckUserCredentialsValid(name string, acces
324334
addTransportOptions(&minioS3Client.s3Config, minioTestClientOptions)
325335
minioTestClient, err := minio.New(minioS3Client.s3Config.S3UrlEndpoint, minioTestClientOptions)
326336
if err != nil {
327-
s3Logger.Error(err, "An error occurred while creating a new minio test client")
337+
s3Logger.Error(err, "An error occurred while creating a new Minio test client")
328338
}
329339

330340
_, err = minioTestClient.ListBuckets(context.Background())
331341
if err != nil {
332-
s3Logger.Error(err, "An error occurred while listing bucket")
333-
return false, err
342+
errAsResponse := minio.ToErrorResponse(err)
343+
if errAsResponse.Code == "SignatureDoesNotMatch" {
344+
s3Logger.Info("the user credentials appear to be invalid", "accessKey", accessKey, "s3BackendError", errAsResponse)
345+
return false, nil
346+
} else if errAsResponse.Code == "InvalidAccessKeyId" {
347+
s3Logger.Info("this accessKey does not exist on the s3 backend", "accessKey", accessKey, "s3BackendError", errAsResponse)
348+
return false, nil
349+
} else {
350+
s3Logger.Error(err, "an error occurred while checking if the S3 user's credentials were valid", "accessKey", accessKey, "code", errAsResponse.Code)
351+
return false, err
352+
}
334353
}
335354
return true, nil
336355
}
337356

338-
func (minioS3Client *MinioS3Client) RemovePoliciesFromUser(username string, policies []string) error {
339-
s3Logger.Info(fmt.Sprintf("Remove policy [%s] from user [%s]", policies, username))
357+
func (minioS3Client *MinioS3Client) RemovePoliciesFromUser(accessKey string, policies []string) error {
358+
s3Logger.Info(fmt.Sprintf("Remove policy [%s] from user [%s]", policies, accessKey))
340359

341360
opts := madmin.PolicyAssociationReq{
342361
Policies: policies,
343-
User: username,
362+
User: accessKey,
344363
}
345364

346365
_, err := minioS3Client.adminClient.DetachPolicy(context.Background(), opts)
347366

348367
if err != nil {
368+
errAsResp := madmin.ToErrorResponse(err)
369+
if errAsResp.Code == "XMinioAdminPolicyChangeAlreadyApplied" {
370+
s3Logger.Info("The policy change has no net effect")
371+
return nil
372+
}
373+
s3Logger.Error(err, "an error occurred when attaching a policy to the user", "code", errAsResp.Code)
349374
return err
350375
}
351376

352377
return nil
353378
}
354379

355-
func (minioS3Client *MinioS3Client) AddPoliciesToUser(username string, policies []string) error {
356-
s3Logger.Info("Adding policies to user", "user", username, "policies", policies)
380+
func (minioS3Client *MinioS3Client) AddPoliciesToUser(accessKey string, policies []string) error {
381+
s3Logger.Info("Adding policies to user", "user", accessKey, "policies", policies)
357382
opts := madmin.PolicyAssociationReq{
358-
User: username,
383+
User: accessKey,
359384
Policies: policies,
360385
}
361386
_, err := minioS3Client.adminClient.AttachPolicy(context.Background(), opts)
362387
if err != nil {
388+
errAsResp := madmin.ToErrorResponse(err)
389+
if errAsResp.Code == "XMinioAdminPolicyChangeAlreadyApplied" {
390+
s3Logger.Info("The policy change has no net effect")
391+
return nil
392+
}
393+
s3Logger.Error(err, "an error occurred when attaching a policy to the user", "code", errAsResp.Code)
363394
return err
364395
}
365396
return nil
366-
}
397+
}

0 commit comments

Comments
 (0)