Merge pull request #243 from travis-ci/ar-cd-rate-setting

Add api builds rate limit to API
This commit is contained in:
carlad 2016-04-05 10:29:35 +02:00
commit 855997aba8
5 changed files with 42 additions and 10 deletions

3
.rspec Normal file
View File

@ -0,0 +1,3 @@
--colour
--tty
--format documentation

View File

@ -50,9 +50,9 @@ class Rack::Attack
# Ban time: 5 hours # Ban time: 5 hours
# Ban after: 10 POST requests within five minutes to /auth/github # Ban after: 10 POST requests within five minutes to /auth/github
blacklist('hammering /auth/github') do |request| blacklist('hammering /auth/github') do |request|
Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 2, findtime: 5.minutes, bantime: bantime(5.hours)) do Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 2, findtime: 5.minutes, bantime: bantime(5.hours)) do
request.post? and request.path == '/auth/github' request.post? and request.path == '/auth/github'
end end
end end
#### ####
@ -60,9 +60,9 @@ class Rack::Attack
# Ban time: 1 hour # Ban time: 1 hour
# Ban after: 10 POST requests within 30 seconds # Ban after: 10 POST requests within 30 seconds
blacklist('spamming with POST requests') do |request| blacklist('spamming with POST requests') do |request|
Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 10, findtime: 30.seconds, bantime: bantime(1.hour)) do Rack::Attack::Allow2Ban.filter(request.identifier, maxretry: 10, findtime: 30.seconds, bantime: bantime(1.hour)) do
request.post? and not POST_WHITELISTED.include? request.path request.post? and not POST_WHITELISTED.include? request.path
end end
end end

View File

@ -62,5 +62,9 @@ module Travis::API::V3
__send__(name, *args, &block) __send__(name, *args, &block)
end end
def settings
@settings ||= JSON.load(super)
end
end end
end end

View File

@ -22,14 +22,19 @@ module Travis::API::V3
accepted(remaining_requests: remaining, repository: repository, request: payload) accepted(remaining_requests: remaining, repository: repository, request: payload)
end end
def limit def limit(repository)
Travis.config.requests_create_api_limit || LIMIT if repository.settings.nil?
LIMIT
else
repository.settings["api_builds_rate_limit"] || LIMIT
end
end end
def remaining_requests(repository) def remaining_requests(repository)
return limit if access_control.full_access? api_builds_rate_limit = limit(repository)
return api_builds_rate_limit if access_control.full_access?
count = query(:requests).count(repository, TIME_FRAME) count = query(:requests).count(repository, TIME_FRAME)
count > limit ? 0 : limit - count count > api_builds_rate_limit ? 0 : api_builds_rate_limit - count
end end
end end
end end

View File

@ -239,6 +239,26 @@ describe Travis::API::V3::Services::Requests::Create do
} }
end end
describe "overrides default request limit if included in repository.settings" do
before { repo.update_attribute(:settings, { api_builds_rate_limit: 12 }.to_json) }
before { 10.times { repo.requests.create(event_type: 'api', result: 'accepted') } }
before { post("/v3/repo/#{repo.id}/requests", {}, headers) }
example { expect(last_response.status).to be == 202 }
example { expect(JSON.load(body).to_s).to include(
"@type",
"repository",
"remaining_requests",
"2",
"request",
"representation",
"minimal",
"slug",
"svenfuchs/minimal")
}
end
describe "passing the token in params" do describe "passing the token in params" do
let(:params) {{ request: { token: 'foo-bar' }}} let(:params) {{ request: { token: 'foo-bar' }}}
example { expect(sidekiq_params[:credentials]).to be == { example { expect(sidekiq_params[:credentials]).to be == {