Check limiter on restoring repo and spaces (#1174)

pull/3495/head
Atefeh Mohseni-Ejiyeh 2024-04-03 00:36:57 +00:00 committed by Harness
parent 8893d55eb1
commit 719db4483c
2 changed files with 30 additions and 1 deletions

View File

@ -16,9 +16,11 @@ package repo
import (
"context"
"database/sql"
"fmt"
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/controller/limiter"
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/errors"
@ -74,9 +76,22 @@ func (c *Controller) RestoreNoAuth(
newIdentifier *string,
newParentID *int64,
) (*types.Repository, error) {
repo, err := c.repoStore.Restore(ctx, repo, newIdentifier, newParentID)
var err error
err = c.tx.WithTx(ctx, func(ctx context.Context) error {
if err := c.resourceLimiter.RepoCount(ctx, *newParentID, 1); err != nil {
return fmt.Errorf("resource limit exceeded: %w", limiter.ErrMaxNumReposReached)
}
repo, err = c.repoStore.Restore(ctx, repo, newIdentifier, newParentID)
if err != nil {
return fmt.Errorf("failed to restore the repo: %w", err)
}
return nil
}, sql.TxOptions{Isolation: sql.LevelSerializable})
if err != nil {
return nil, fmt.Errorf("failed to restore the repo: %w", err)
}
return repo, nil
}

View File

@ -22,6 +22,7 @@ import (
"time"
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/controller/limiter"
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/paths"
@ -108,6 +109,19 @@ func (c *Controller) restoreSpaceInnerInTx(
newParentID *int64,
spacePath string,
) (*types.Space, error) {
repoCount, err := c.repoStore.Count(
ctx,
space.ID,
&types.RepoFilter{DeletedBeforeOrAt: &deletedAt, Recursive: true},
)
if err != nil {
return nil, fmt.Errorf("failed to count repos in space %d recursively: %w", space.ID, err)
}
if err := c.resourceLimiter.RepoCount(ctx, *newParentID, int(repoCount)); err != nil {
return nil, fmt.Errorf("resource limit exceeded: %w", limiter.ErrMaxNumReposReached)
}
filter := &types.SpaceFilter{
Page: 1,
Size: math.MaxInt,