FIX: Use prometheus_trusted_ip_allowlist_regex (#7)

FIX: Use prometheus_trusted_ip_allowlist_regex (#7)

prometheus_trusted_ip_whitelist_regex is deprecated and replaced with prometheus_trusted_ip_allowlist_regex

diff --git a/lib/middleware/metrics.rb b/lib/middleware/metrics.rb
index e10bc8b..146743e 100644
--- a/lib/middleware/metrics.rb
+++ b/lib/middleware/metrics.rb
@@ -29,9 +29,9 @@ module DiscoursePrometheus
     end
 
     def is_trusted_ip?(env)
-      return false if GlobalSetting.prometheus_trusted_ip_whitelist_regex.empty?
+      return false if GlobalSetting.prometheus_trusted_ip_allowlist_regex.empty?
       begin
-        trusted_ip_regex = Regexp.new GlobalSetting.prometheus_trusted_ip_whitelist_regex
+        trusted_ip_regex = Regexp.new GlobalSetting.prometheus_trusted_ip_allowlist_regex
         request = Rack::Request.new(env)
         ip = IPAddr.new(request.ip)
       rescue => e
diff --git a/plugin.rb b/plugin.rb
index 67e4648..5eb4f25 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -29,11 +29,18 @@ require_relative("lib/demon")
 require_relative("lib/middleware/metrics")
 
 GlobalSetting.add_default :prometheus_collector_port, 9405
-GlobalSetting.add_default :prometheus_trusted_ip_whitelist_regex, ''
+GlobalSetting.add_default :prometheus_trusted_ip_allowlist_regex, ''
 
 Rails.configuration.middleware.unshift DiscoursePrometheus::Middleware::Metrics
 
 after_initialize do
+  if GlobalSetting.respond_to?(:prometheus_trusted_ip_whitelist_regex) && GlobalSetting.prometheus_trusted_ip_allowlist_regex.blank?
+    Discourse.deprecate("prometheus_trusted_ip_whitelist_regex is deprecated, use the prometheus_trusted_ip_allowlist_regex.", drop_from: "2.6")
+    GlobalSetting.define_singleton_method("prometheus_trusted_ip_allowlist_regex") do
+      GlobalSetting.prometheus_trusted_ip_whitelist_regex
+    end
+  end
+
   $prometheus_client = PrometheusExporter::Client.new(
     host: 'localhost',
     port: GlobalSetting.prometheus_collector_port
diff --git a/spec/middleware/metrics_spec.rb b/spec/middleware/metrics_spec.rb
index f30168a..c4365b9 100644
--- a/spec/middleware/metrics_spec.rb
+++ b/spec/middleware/metrics_spec.rb
@@ -9,7 +9,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
     ::DiscoursePrometheus::Middleware::Metrics.new(app)
   end
 
-  it "will 404 for unauthed if prometheus_trusted_ip_whitelist_regex is unset" do
+  it "will 404 for unauthed if prometheus_trusted_ip_allowlist_regex is unset" do
     status, = middleware.call("PATH_INFO" => '/metrics', "REMOTE_ADDR" => '200.0.1.1', "rack.input" => StringIO.new)
     expect(status).to eq(404)
   end
@@ -20,7 +20,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "will 404 for unauthed and invalid regex" do
-    global_setting :prometheus_trusted_ip_whitelist_regex, "unbalanced bracket["
+    global_setting :prometheus_trusted_ip_allowlist_regex, "unbalanced bracket["
     status, = middleware.call("PATH_INFO" => '/metrics', "REMOTE_ADDR" => '200.0.1.1', "rack.input" => StringIO.new)
     expect(status).to eq(404)
   end
@@ -43,7 +43,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "can proxy the dedicated port even with invalid regex" do
-    global_setting :prometheus_trusted_ip_whitelist_regex, "unbalanced bracket["
+    global_setting :prometheus_trusted_ip_allowlist_regex, "unbalanced bracket["
     stub_request(:get, "http://localhost:#{GlobalSetting.prometheus_collector_port}/metrics").
       to_return(status: 200, body: "hello world", headers: {})
 
@@ -56,7 +56,7 @@ describe ::DiscoursePrometheus::Middleware::Metrics do
   end
 
   it "can proxy the dedicated port on trusted IP" do
-    global_setting :prometheus_trusted_ip_whitelist_regex, "(200\.0)"
+    global_setting :prometheus_trusted_ip_allowlist_regex, "(200\.0)"
     stub_request(:get, "http://localhost:#{GlobalSetting.prometheus_collector_port}/metrics").
       to_return(status: 200, body: "hello world", headers: {})
 

GitHub sha: 80e21a1b

This commit appears in #7 which was merged by SamSaffron.