Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ratelimiter] Add IP allowlist #19

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions common/ratelimit/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,31 @@ type rateLimiter struct {
globalRateParams common.GlobalRateParams

bucketStore BucketStore
allowlist []string

logger common.Logger
}

func NewRateLimiter(rateParams common.GlobalRateParams, bucketStore BucketStore, logger common.Logger) common.RateLimiter {
func NewRateLimiter(rateParams common.GlobalRateParams, bucketStore BucketStore, allowlist []string, logger common.Logger) common.RateLimiter {
return &rateLimiter{
globalRateParams: rateParams,
bucketStore: bucketStore,
allowlist: allowlist,
logger: logger,
}
}

// Checks whether a request from the given requesterID is allowed
func (d *rateLimiter) AllowRequest(ctx context.Context, requesterID common.RequesterID, blobSize uint, rate common.RateParam) (bool, error) {
for _, id := range d.allowlist {
if id == requesterID {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will fail because the disperser constructs the requesterID as ip:quorumID.

Copy link
Contributor

@mooselumph mooselumph Nov 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could change the AllowRequest to accept both requesterID and a tag variable, and construct the key internally to the library.

// Allow 10x the rate for allowlisted IDs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just drop the limit for these IPs, i.e. allowing them to push the traffic to the global limit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I initially implemented that way, but decided to go this way to ensure no single IP can hog the global throughput, which will make all requests to fail globally. wdyt? maybe we can make it 100x?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about 1MiB/s / len(d.allowlist)?

rate = rate * 10
}
}

// Retrieve bucket params for the requester ID
// This will be from dynamo for Disperser and from local storage for DA node

bucketParams, err := d.bucketStore.GetItem(ctx, requesterID)
if err != nil {

Expand Down
10 changes: 10 additions & 0 deletions common/ratelimit/limiter_cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ const (
BucketMultipliersFlagName = "bucket-multipliers"
CountFailedFlagName = "count-failed"
BucketStoreSizeFlagName = "bucket-store-size"
AllowlistFlagName = "allowlist"
)

type Config struct {
common.GlobalRateParams
BucketStoreSize int
UniformRateParam common.RateParam
Allowlist []string
}

func RatelimiterCLIFlags(envPrefix string, flagPrefix string) []cli.Flag {
Expand Down Expand Up @@ -52,6 +54,13 @@ func RatelimiterCLIFlags(envPrefix string, flagPrefix string) []cli.Flag {
EnvVar: common.PrefixEnvVar(envPrefix, "BUCKET_STORE_SIZE"),
Required: false,
},
cli.StringSliceFlag{
Name: common.PrefixFlag(flagPrefix, AllowlistFlagName),
Usage: "Allowlist of IPs to bypass rate limiting",
EnvVar: common.PrefixEnvVar(envPrefix, "ALLOWLIST"),
Required: false,
Value: &cli.StringSlice{},
},
}
}

Expand Down Expand Up @@ -97,6 +106,7 @@ func ReadCLIConfig(ctx *cli.Context, flagPrefix string) (Config, error) {
cfg.Multipliers = multipliers
cfg.GlobalRateParams.CountFailed = ctx.Bool(common.PrefixFlag(flagPrefix, CountFailedFlagName))
cfg.BucketStoreSize = ctx.Int(common.PrefixFlag(flagPrefix, BucketStoreSizeFlagName))
cfg.Allowlist = ctx.StringSlice(common.PrefixFlag(flagPrefix, AllowlistFlagName))

err := validateConfig(cfg)
if err != nil {
Expand Down
23 changes: 21 additions & 2 deletions common/ratelimit/ratelimit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,13 @@ func makeTestRatelimiter() (common.RateLimiter, error) {
return nil, err
}

ratelimiter := ratelimit.NewRateLimiter(globalParams, bucketStore, &mock.Logger{})
ratelimiter := ratelimit.NewRateLimiter(globalParams, bucketStore, []string{"testRetriever2"}, &mock.Logger{})

return ratelimiter, nil

}

func TestRatelimit(t *testing.T) {

ratelimiter, err := makeTestRatelimiter()
assert.NoError(t, err)

Expand All @@ -50,3 +49,23 @@ func TestRatelimit(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, false, allow)
}

func TestRatelimitAllowlist(t *testing.T) {
ratelimiter, err := makeTestRatelimiter()
assert.NoError(t, err)

ctx := context.Background()

retreiverID := "testRetriever2"

// 10x more requests allowed for allowlisted IDs
for i := 0; i < 100; i++ {
allow, err := ratelimiter.AllowRequest(ctx, retreiverID, 10, 100)
assert.NoError(t, err)
assert.Equal(t, true, allow)
}

allow, err := ratelimiter.AllowRequest(ctx, retreiverID, 10, 100)
assert.NoError(t, err)
assert.Equal(t, false, allow)
}
2 changes: 1 addition & 1 deletion disperser/cmd/disperserserver/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func RunDisperserServer(ctx *cli.Context) error {
return err
}
}
ratelimiter = ratelimit.NewRateLimiter(globalParams, bucketStore, logger)
ratelimiter = ratelimit.NewRateLimiter(globalParams, bucketStore, config.RatelimiterConfig.Allowlist, logger)
}

// TODO: create a separate metrics for batcher
Expand Down
2 changes: 1 addition & 1 deletion node/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NodeMain(ctx *cli.Context) error {
return err
}

ratelimiter := ratelimit.NewRateLimiter(globalParams, bucketStore, logger)
ratelimiter := ratelimit.NewRateLimiter(globalParams, bucketStore, []string{}, logger)

// Creates the GRPC server.
server := grpc.NewServer(config, node, logger, ratelimiter)
Expand Down