Skip to content

Commit

Permalink
Support IP whitelisting for throttling.
Browse files Browse the repository at this point in the history
Signed-off-by: EdmondFrank <[email protected]>
  • Loading branch information
EdmondFrank committed Feb 14, 2024
1 parent bf36f58 commit b4dbee4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ export OTEL_RESOURCE_ATTRIBUTES=application=compass-web-service
# Lab
export LAB_MODEL_TRIGGER_COUNT=5

# Safelist
export SAFELIST_IPS=5.6.7.0/24,192.168.1.0/24

# Protected Reports
export RESTRICTED_LABEL_LIST=oss-compass
export RESTRICTED_LABEL_VIEWERS=2
Expand Down
1 change: 1 addition & 0 deletions app/controllers/concerns/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module Common
META_REPO = ENV.fetch('WORKFLOW_REPO_NAME') { 'compass-projects-information' }
ADMIN_WEB_TOKEN = ENV.fetch('ADMIN_WEB_TOKEN')
ADMIN_SLACK_WEBHOOK = ENV.fetch('ADMIN_SLACK_WEBBHOOK') { nil }
SAFELIST_IPS = (ENV.fetch('SAFELIST_IPS') { '' })&.split(',')
RESTRICTED_LABEL_LIST = (ENV.fetch('RESTRICTED_LABEL_LIST') { '' })&.split(',')
RESTRICTED_LABEL_VIEWERS = (ENV.fetch('RESTRICTED_LABEL_VIEWERS') { '' })&.split(',')

Expand Down
8 changes: 7 additions & 1 deletion app/controllers/graphql_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def execute

t << real_ip

t.throttle! if !current_user || real_ip
t.throttle! if !current_user || (real_ip && !safelist_ip(real_ip))

result = CompassWebServiceSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render json: result
Expand All @@ -53,6 +53,12 @@ def throttle_redis
@throttle_redis ||= Redis.new(url: ENV.fetch('REDIS_URL') { 'redis://redis:6379/1' })
end

def safelist_ip(target_ip)
Common::SAFELIST_IPS.any? do |safe_ip|
IPAddr.new(safe_ip).include?(IPAddr.new(target_ip))
end
end

# Handle variables in form data, JSON body, or a blank value
def prepare_variables(variables_param)
case variables_param
Expand Down

0 comments on commit b4dbee4

Please sign in to comment.