|
| 1 | +# frozen_string_literal: true |
| 2 | +require_relative "./installable" |
| 3 | +module Graphql |
| 4 | + class Dashboard < Rails::Engine |
| 5 | + module Limiters |
| 6 | + class LimitersController < Dashboard::ApplicationController |
| 7 | + include Installable |
| 8 | + FALLBACK_CSP_NONCE_GENERATOR = ->(_req) { SecureRandom.hex(32) } |
| 9 | + |
| 10 | + def show |
| 11 | + name = params[:name] |
| 12 | + @title = case name |
| 13 | + when "runtime" |
| 14 | + "Runtime Limiter" |
| 15 | + when "active_operations" |
| 16 | + "Active Operation Limiter" |
| 17 | + when "mutations" |
| 18 | + "Mutation Limiter" |
| 19 | + else |
| 20 | + raise ArgumentError, "Unknown limiter name: #{name}" |
| 21 | + end |
| 22 | + |
| 23 | + limiter = limiter_for(name) |
| 24 | + if limiter.nil? |
| 25 | + @install_path = "http://graphql-ruby.org/limiters/#{name}" |
| 26 | + else |
| 27 | + @chart_mode = params[:chart] || "day" |
| 28 | + @current_soft = limiter.soft_limit_enabled? |
| 29 | + @histogram = limiter.dashboard_histogram(@chart_mode) |
| 30 | + |
| 31 | + # These configs may have already been defined by the application; provide overrides here if not. |
| 32 | + request.content_security_policy_nonce_generator ||= FALLBACK_CSP_NONCE_GENERATOR |
| 33 | + nonce_dirs = request.content_security_policy_nonce_directives || [] |
| 34 | + if !nonce_dirs.include?("style-src") |
| 35 | + nonce_dirs += ["style-src"] |
| 36 | + request.content_security_policy_nonce_directives = nonce_dirs |
| 37 | + end |
| 38 | + @csp_nonce = request.content_security_policy_nonce |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def update |
| 43 | + name = params[:name] |
| 44 | + limiter = limiter_for(name) |
| 45 | + if limiter |
| 46 | + limiter.toggle_soft_limit |
| 47 | + flash[:success] = if limiter.soft_limit_enabled? |
| 48 | + "Enabled soft limiting -- over-limit traffic will be logged but not rejected." |
| 49 | + else |
| 50 | + "Disabled soft limiting -- over-limit traffic will be rejected." |
| 51 | + end |
| 52 | + else |
| 53 | + flash[:warning] = "No limiter configured for #{name.inspect}" |
| 54 | + end |
| 55 | + |
| 56 | + redirect_to graphql_dashboard.limiters_limiter_path(name, chart: params[:chart]) |
| 57 | + end |
| 58 | + |
| 59 | + private |
| 60 | + |
| 61 | + def limiter_for(name) |
| 62 | + case name |
| 63 | + when "runtime" |
| 64 | + schema_class.enterprise_runtime_limiter |
| 65 | + when "active_operations" |
| 66 | + schema_class.enterprise_active_operation_limiter |
| 67 | + when "mutations" |
| 68 | + schema_class.enterprise_mutation_limiter |
| 69 | + else |
| 70 | + raise ArgumentError, "Unknown limiter: #{name}" |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + def feature_installed? |
| 75 | + defined?(GraphQL::Enterprise::Limiter) && |
| 76 | + ( |
| 77 | + schema_class.enterprise_active_operation_limiter || |
| 78 | + schema_class.enterprise_runtime_limiter || |
| 79 | + (schema_class.respond_to?(:enterprise_mutation_limiter) && schema_class.enterprise_mutation_limiter) |
| 80 | + ) |
| 81 | + end |
| 82 | + |
| 83 | + |
| 84 | + INSTALLABLE_COMPONENT_HEADER_HTML = "Rate limiters aren't installed on this schema yet." |
| 85 | + INSTALLABLE_COMPONENT_MESSAGE_HTML = <<-HTML.html_safe |
| 86 | + Check out the docs to get started with GraphQL-Enterprise's |
| 87 | + <a href="https://graphql-ruby.org/limiters/runtime.html">runtime limiter</a> or |
| 88 | + <a href="https://graphql-ruby.org/limiters/active_operations.html">active operation limiter</a>. |
| 89 | + HTML |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | +end |
0 commit comments