Add a repo size limiter method to Unlimited limiter (#943)

eb/code-1016-2
Darko Draskovic 2024-01-03 14:13:51 +00:00 committed by Harness
parent 630ba3ec15
commit dd49c18b2e
5 changed files with 25 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import (
"context"
"fmt"
"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/auth/authz"
@ -62,6 +63,7 @@ type Controller struct {
pullreqStore store.PullReqStore
urlProvider url.Provider
protectionManager *protection.Manager
resourceLimiter limiter.ResourceLimiter
}
func NewController(
@ -73,6 +75,7 @@ func NewController(
pullreqStore store.PullReqStore,
urlProvider url.Provider,
protectionManager *protection.Manager,
limiter limiter.ResourceLimiter,
) *Controller {
return &Controller{
authorizer: authorizer,
@ -83,6 +86,7 @@ func NewController(
pullreqStore: pullreqStore,
urlProvider: urlProvider,
protectionManager: protectionManager,
resourceLimiter: limiter,
}
}

View File

@ -20,6 +20,7 @@ import (
"strings"
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/services/protection"
@ -46,6 +47,12 @@ func (c *Controller) PreReceive(
return hook.Output{}, err
}
if err := c.resourceLimiter.RepoSize(ctx, in.RepoID); err != nil {
return hook.Output{}, fmt.Errorf(
"resource limit exceeded: %w",
limiter.ErrMaxRepoSizeReached)
}
refUpdates := groupRefsByAction(in.RefUpdates)
if slices.Contains(refUpdates.branches.deleted, repo.DefaultBranch) {

View File

@ -21,11 +21,15 @@ import (
)
var ErrMaxNumReposReached = errors.New("maximum number of repositories reached")
var ErrMaxRepoSizeReached = errors.New("maximum size of repository reached")
// ResourceLimiter is an interface for managing resource limitation.
type ResourceLimiter interface {
// RepoCount allows the creation of a specified number of repositories.
RepoCount(ctx context.Context, count int) error
// RepoSize allows repository growth up to a limit for the given repoID.
RepoSize(ctx context.Context, repoID int64) error
}
var _ ResourceLimiter = Unlimited{}
@ -42,3 +46,8 @@ func NewResourceLimiter() ResourceLimiter {
func (Unlimited) RepoCount(ctx context.Context, count int) error {
return nil
}
//nolint:revive
func (Unlimited) RepoSize(ctx context.Context, repoID int64) error {
return nil
}

View File

@ -16,6 +16,7 @@ package githook
import (
"github.com/harness/gitness/app/api/controller/githook"
"github.com/harness/gitness/app/api/controller/limiter"
"github.com/harness/gitness/app/auth/authz"
eventsgit "github.com/harness/gitness/app/events/git"
"github.com/harness/gitness/app/services/protection"
@ -50,6 +51,7 @@ func ProvideController(
urlProvider url.Provider,
protectionManager *protection.Manager,
githookFactory hook.ClientFactory,
limiter limiter.ResourceLimiter,
) *githook.Controller {
ctrl := githook.NewController(
authorizer,
@ -59,7 +61,8 @@ func ProvideController(
git,
pullreqStore,
urlProvider,
protectionManager)
protectionManager,
limiter)
// TODO: improve wiring if possible
if fct, ok := githookFactory.(*ControllerClientFactory); ok {

View File

@ -256,7 +256,7 @@ func initSystem(ctx context.Context, config *types.Config) (*server.System, erro
if err != nil {
return nil, err
}
githookController := githook.ProvideController(authorizer, principalStore, repoStore, reporter2, gitInterface, pullReqStore, provider, protectionManager, clientFactory)
githookController := githook.ProvideController(authorizer, principalStore, repoStore, reporter2, gitInterface, pullReqStore, provider, protectionManager, clientFactory, resourceLimiter)
serviceaccountController := serviceaccount.NewController(principalUID, authorizer, principalStore, spaceStore, repoStore, tokenStore)
principalController := principal.ProvideController(principalStore)
v := check2.ProvideCheckSanitizers()