Skip to content

Commit

Permalink
User email as rate limiter key
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Di Tommaso <[email protected]>
  • Loading branch information
pditommaso committed Dec 12, 2024
1 parent d72b69c commit eddc9ee
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ class ContainerController {
final ip = addressResolver.resolve(httpRequest)
// check the rate limit before continuing
if( rateLimiterService )
rateLimiterService.acquirePull(new AcquireRequest(identity.userId as String, ip))
rateLimiterService.acquirePull(new AcquireRequest(identity.userEmail, ip))
// create request data
final data = makeRequestData(req, identity, ip)
final token = containerService.computeToken(data)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class RegistryProxyController {

if( route.manifest && route.digest ){
String ip = addressResolver.resolve(httpRequest)
rateLimiterService?.acquirePull( new AcquireRequest(route.identity.userId as String, ip) )
rateLimiterService?.acquirePull( new AcquireRequest(route.identity.userEmail, ip) )
}

// check if it's a container under build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class AcquireRequest {
/**
* Principal key to use in the search. Can be null
*/
String userId
String user

/**
* Secondary key to use if principal is not present
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,20 @@ class SpillwayRateLimiter implements RateLimiterService {

@Override
void acquireBuild(AcquireRequest request) throws SlowDownException {
Spillway<String> resource = request.userId ? authsBuilds : anonymousBuilds
String key = request.userId ?: request.ip
Spillway<String> resource = request.user ? authsBuilds : anonymousBuilds
String key = request.user ?: request.ip
if (!resource.tryCall(key)) {
final prefix = request.userId ? 'user' : 'IP'
final prefix = request.user ? 'user' : 'IP'
throw new SlowDownException("Request exceeded build rate limit for $prefix $key")
}
}

@Override
void acquirePull(AcquireRequest request) throws SlowDownException {
Spillway<String> resource = request.userId ? authsPulls : anonymousPulls
String key = request.userId ?: request.ip
Spillway<String> resource = request.user ? authsPulls : anonymousPulls
String key = request.user ?: request.ip
if (!resource.tryCall(key)) {
final prefix = request.userId ? 'user' : 'IP'
final prefix = request.user ? 'user' : 'IP'
throw new SlowDownException("Request exceeded pull rate limit for $prefix $key")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class ContainerBuildServiceImpl implements ContainerBuildService, JobHandler<Bui
// check the build rate limit
try {
if( rateLimiterService )
rateLimiterService.acquireBuild(new AcquireRequest(request.identity.userId as String, request.ip))
rateLimiterService.acquireBuild(new AcquireRequest(request.identity.userEmail, request.ip))
}
catch (Exception e) {
buildStore.removeBuild(request.targetImage)
Expand Down
4 changes: 4 additions & 0 deletions src/main/groovy/io/seqera/wave/tower/PlatformId.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class PlatformId {
return user?.id
}

String getUserEmail() {
return user?.email
}

static PlatformId of(User user, SubmitContainerTokenRequest request) {
new PlatformId(
user,
Expand Down
3 changes: 2 additions & 1 deletion src/test/groovy/io/seqera/wave/tower/PlatformIdTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class PlatformIdTest extends Specification {

def 'should create form a container request' () {
when:
def id = PlatformId.of(new User(id:1), new SubmitContainerTokenRequest(
def id = PlatformId.of(new User(id:1, email: '[email protected]'), new SubmitContainerTokenRequest(
towerWorkspaceId: 100,
towerEndpoint: 'http://foo.com',
towerAccessToken: 'token-123',
Expand All @@ -53,6 +53,7 @@ class PlatformIdTest extends Specification {
id.workspaceId == 100
id.towerEndpoint == 'http://foo.com'
id.accessToken == 'token-123'
id.userEmail == '[email protected]'
}

def 'should create form a inspect request' () {
Expand Down

0 comments on commit eddc9ee

Please sign in to comment.