From 8d431c225ab5d6894547d31415933a435db18957 Mon Sep 17 00:00:00 2001 From: Andrei Aaron Date: Mon, 6 Jan 2025 12:02:35 +0000 Subject: [PATCH 1/2] chore(dynamodb): refactor multiple apikey metadb calls into a single one Signed-off-by: Andrei Aaron --- pkg/meta/dynamodb/dynamodb.go | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/pkg/meta/dynamodb/dynamodb.go b/pkg/meta/dynamodb/dynamodb.go index 8488c78eb..fcbb1b765 100644 --- a/pkg/meta/dynamodb/dynamodb.go +++ b/pkg/meta/dynamodb/dynamodb.go @@ -1730,23 +1730,29 @@ func (dwr DynamoDB) GetUserAPIKeys(ctx context.Context) ([]mTypes.APIKeyDetails, return nil, fmt.Errorf("failed to get userData for identity %s %w", userid, err) } + changed := false + for hashedKey, apiKeyDetails := range userData.APIKeys { // if expiresAt is not nil value if !apiKeyDetails.ExpirationDate.Equal(time.Time{}) && time.Now().After(apiKeyDetails.ExpirationDate) { apiKeyDetails.IsExpired = true + + changed = true } userData.APIKeys[hashedKey] = apiKeyDetails - err = dwr.SetUserData(ctx, userData) - if err != nil { - return nil, err - } - apiKeys = append(apiKeys, apiKeyDetails) } - return apiKeys, nil + if !changed { + // return early, no need to make a call to update key expiry in the DB + return apiKeys, nil + } + + err = dwr.SetUserData(ctx, userData) + + return apiKeys, err } func (dwr DynamoDB) AddUserAPIKey(ctx context.Context, hashedKey string, apiKeyDetails *mTypes.APIKeyDetails) error { From f756fda34c57e072565d8d8c2978a138451bc502 Mon Sep 17 00:00:00 2001 From: Andrei Aaron Date: Thu, 9 Jan 2025 18:39:16 +0000 Subject: [PATCH 2/2] fix(metadb): wrong error message in PatchDB() implementation Signed-off-by: Andrei Aaron --- errors/errors.go | 1 + pkg/meta/boltdb/boltdb.go | 2 +- pkg/meta/dynamodb/dynamodb.go | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index f46569ea2..60dbffea5 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -129,6 +129,7 @@ var ( ErrMissingAuthHeader = errors.New("required authorization header is missing") ErrUserAPIKeyNotFound = errors.New("user info for given API key hash not found") ErrUserSessionNotFound = errors.New("user session for given ID not found") + ErrInvalidMetaDBVersion = errors.New("unrecognized version meta") ErrBucketDoesNotExist = errors.New("bucket does not exist") ErrOpenIDProviderDoesNotExist = errors.New("openid provider does not exist in given config") ErrHashKeyNotCreated = errors.New("cookiestore generated random hash key is nil, aborting") diff --git a/pkg/meta/boltdb/boltdb.go b/pkg/meta/boltdb/boltdb.go index 0166bf517..d7ee08f32 100644 --- a/pkg/meta/boltdb/boltdb.go +++ b/pkg/meta/boltdb/boltdb.go @@ -1569,7 +1569,7 @@ func (bdw *BoltDB) PatchDB() error { } if version.GetVersionIndex(DBVersion) == -1 { - return fmt.Errorf("DB has broken format, no version found %w", err) + return fmt.Errorf("%w: %s could not identify patches", zerr.ErrInvalidMetaDBVersion, DBVersion) } for patchIndex, patch := range bdw.Patches { diff --git a/pkg/meta/dynamodb/dynamodb.go b/pkg/meta/dynamodb/dynamodb.go index fcbb1b765..5357d0122 100644 --- a/pkg/meta/dynamodb/dynamodb.go +++ b/pkg/meta/dynamodb/dynamodb.go @@ -2055,7 +2055,7 @@ func (dwr *DynamoDB) PatchDB() error { } if version.GetVersionIndex(DBVersion) == -1 { - return fmt.Errorf("DB has broken format, no version found %w", err) + return fmt.Errorf("%w: %s could not identify patches", zerr.ErrInvalidMetaDBVersion, DBVersion) } for patchIndex, patch := range dwr.Patches {