From 2d6832f4b7ca8eed4498b266495c58a55b9790d3 Mon Sep 17 00:00:00 2001
From: Sander van den Oever <S.Y.vandenOever@tudelft.nl>
Date: Tue, 8 Oct 2019 17:28:30 +0200
Subject: [PATCH] Use the original IP instead of Proxy IP for Sentry reports

Resolves #250.
---
 .../factory/QueueSentryClientFactory.java     | 46 +++++++++++++++++++
 src/main/resources/sentry.properties.template |  3 +-
 2 files changed, 48 insertions(+), 1 deletion(-)
 create mode 100644 src/main/java/nl/tudelft/ewi/queue/factory/QueueSentryClientFactory.java

diff --git a/src/main/java/nl/tudelft/ewi/queue/factory/QueueSentryClientFactory.java b/src/main/java/nl/tudelft/ewi/queue/factory/QueueSentryClientFactory.java
new file mode 100644
index 000000000..2941bc61f
--- /dev/null
+++ b/src/main/java/nl/tudelft/ewi/queue/factory/QueueSentryClientFactory.java
@@ -0,0 +1,46 @@
+/*
+ * Queue - A Queueing system that can be used to handle labs in higher education Copyright (C) 2016-2019 Delft
+ * University of Technology
+ *
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero
+ * General Public License as published by the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
+ * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
+ * License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License along with this program. If not,
+ * see <https://www.gnu.org/licenses/>.
+ */
+package nl.tudelft.ewi.queue.factory;
+
+import io.sentry.DefaultSentryClientFactory;
+import io.sentry.SentryClient;
+import io.sentry.dsn.Dsn;
+import io.sentry.event.helper.ContextBuilderHelper;
+import io.sentry.event.helper.ForwardedAddressResolver;
+import io.sentry.event.helper.HttpEventBuilderHelper;
+
+public class QueueSentryClientFactory extends DefaultSentryClientFactory {
+    /**
+     * Enable the ForwardedAddressResolver so we can see actual IPs in Sentry for TAM, which is hosted behind a proxy.
+     *
+     * See: https://docs.sentry.io/clients/java/config/#custom-functionality
+     *
+     * @param dsn URI can be found in Sentry settings (given appropriate access).
+     * @return a new Sentry Client.
+     */
+    @Override
+    public SentryClient createSentryClient(final Dsn dsn) {
+        final SentryClient sentryClient = new SentryClient(createConnection(dsn), getContextManager(dsn));
+
+        // Create and use the ForwardedAddressResolver, which will use the
+        // X-FORWARDED-FOR header for the remote address if it exists.
+        final ForwardedAddressResolver addressResolver = new ForwardedAddressResolver();
+        sentryClient.addBuilderHelper(new HttpEventBuilderHelper(addressResolver));
+
+        sentryClient.addBuilderHelper(new ContextBuilderHelper(sentryClient));
+        return configureSentryClient(sentryClient, dsn);
+    }
+}
diff --git a/src/main/resources/sentry.properties.template b/src/main/resources/sentry.properties.template
index 3d6759228..c795dd3f0 100644
--- a/src/main/resources/sentry.properties.template
+++ b/src/main/resources/sentry.properties.template
@@ -22,4 +22,5 @@
 #dsn=your_key_here
 environment=dev
 servername=localhost
-stacktrace.app.packages=nl.tudelft.ewi.queue
\ No newline at end of file
+stacktrace.app.packages=nl.tudelft.ewi.queue
+factory=nl.tudelft.ewi.queue.factory.QueueSentryClientFactory
\ No newline at end of file
-- 
GitLab