fix(metadb): fixes for dynamo and bolt (#2884)

* chore(dynamodb): refactor multiple apikey metadb calls into a single one

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>

* fix(metadb): wrong error message in PatchDB() implementation

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>

---------

Signed-off-by: Andrei Aaron <aaaron@luxoft.com>
This commit is contained in:
Andrei Aaron
2025-01-17 11:18:01 +02:00
committed by GitHub
parent cdcafa925f
commit fba695adb9
3 changed files with 15 additions and 8 deletions
+1
View File
@@ -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")
+1 -1
View File
@@ -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 {
+13 -7
View File
@@ -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 {
@@ -2049,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 {