From fabe38c302505fffc76889885f9eb1c2a2b40b84 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 23 Aug 2022 14:03:40 +0530 Subject: [PATCH 1/3] Added logic to persist cookies from third party Replaced empty cookiestore check with splunk auth cookie check --- .../src/main/java/com/splunk/HttpService.java | 6 ++-- splunk/src/main/java/com/splunk/Receiver.java | 6 +++- splunk/src/main/java/com/splunk/Service.java | 6 ++-- .../java/com/splunk/SimpleCookieStore.java | 15 +++++++++ .../src/test/java/com/splunk/CookieTest.java | 31 ++++++++++++++++--- .../src/test/java/com/splunk/IndexTest.java | 4 +-- .../test/java/com/splunk/ReceiverTest.java | 2 +- 7 files changed, 57 insertions(+), 13 deletions(-) diff --git a/splunk/src/main/java/com/splunk/HttpService.java b/splunk/src/main/java/com/splunk/HttpService.java index 693f3a36..aa6769b8 100644 --- a/splunk/src/main/java/com/splunk/HttpService.java +++ b/splunk/src/main/java/com/splunk/HttpService.java @@ -316,12 +316,12 @@ public void removeAllCookies() { } /** - * Returns true if the cookeStore has any cookies, false otherwise + * Returns true if the cookieStore has any Splunk Authorization cookies, false otherwise * * @return True if there are cookies, false otherwise */ - public Boolean hasCookies() { - return !cookieStore.isEmpty(); + public Boolean hasSplunkAuthCookies() { + return cookieStore.hasSplunkAuthCookie(); } /** diff --git a/splunk/src/main/java/com/splunk/Receiver.java b/splunk/src/main/java/com/splunk/Receiver.java index b6057cc0..8ae4d826 100644 --- a/splunk/src/main/java/com/splunk/Receiver.java +++ b/splunk/src/main/java/com/splunk/Receiver.java @@ -98,9 +98,13 @@ public Socket attach(String indexName, Args args) throws IOException { headers.add("Accept-Encoding: identity"); headers.add("X-Splunk-Input-Mode: Streaming"); - if (service.hasCookies()) { + if (service.hasSplunkAuthCookies()) { headers.add(String.format("Cookie: %s", service.stringifyCookies())); } else { + // to persist the cookies other than Splunk such as from Load Balancer + if(!service.cookieStore.isEmpty()){ + headers.add(String.format("Cookie: %s", service.stringifyCookies())); + } headers.add(String.format("Authorization: %s", service.getToken())); } headers.add(""); diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index cbe63b4a..2b7aad7b 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -1112,7 +1112,8 @@ public UserCollection getUsers(Args args) { * @return The current {@code Service} instance. */ public Service login() { - if (!this.cookieStore.isEmpty() && (this.username == null || this.password == null)) { + //if (!this.cookieStore.isEmpty() && (this.username == null || this.password == null)) { + if (this.cookieStore.hasSplunkAuthCookie() && (this.username == null || this.password == null)) { return this; } else if (this.username == null || this.password == null) { @@ -1312,7 +1313,8 @@ public Job search(String query, Map args) { */ @Override public ResponseMessage send(String path, RequestMessage request) { // cookieStore is a protected member of HttpService - if (token != null && cookieStore.isEmpty()) { + //if (token != null && cookieStore.isEmpty() ) { + if (token != null && !cookieStore.hasSplunkAuthCookie() ) { request.getHeader().put("Authorization", token); } return super.send(fullpath(path), request); diff --git a/splunk/src/main/java/com/splunk/SimpleCookieStore.java b/splunk/src/main/java/com/splunk/SimpleCookieStore.java index afc1219c..10dacccb 100644 --- a/splunk/src/main/java/com/splunk/SimpleCookieStore.java +++ b/splunk/src/main/java/com/splunk/SimpleCookieStore.java @@ -28,6 +28,8 @@ */ class SimpleCookieStore { + public static final String SPLUNK_AUTH_COOKIE = "splunkd_"; + private Map cookieJar = new HashMap(); /** * Adds cookies from a "Set-Cookie" header to the cookie store. @@ -69,6 +71,19 @@ public Boolean isEmpty() { return cookieJar.isEmpty(); } + public boolean hasSplunkAuthCookie(){ + if(cookieJar.isEmpty()){ + return false; + } + for(String cookie : cookieJar.keySet()){ + if(cookie.startsWith(SPLUNK_AUTH_COOKIE)){ + System.out.println("HELLO"); + return true; + } + } + return false; + } + /** * Removes all cookies from SimpleCookieStore */ diff --git a/splunk/src/test/java/com/splunk/CookieTest.java b/splunk/src/test/java/com/splunk/CookieTest.java index f49b18d2..411fe4af 100644 --- a/splunk/src/test/java/com/splunk/CookieTest.java +++ b/splunk/src/test/java/com/splunk/CookieTest.java @@ -16,13 +16,11 @@ package com.splunk; +import java.net.HttpCookie; import java.util.HashMap; import java.util.Map; -import org.junit.Assert; -import org.junit.Assume; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; public class CookieTest extends SDKTestCase { @@ -124,6 +122,31 @@ public void testLoginWithMultipleCookies() { s.getSettings().refresh(); } + @Test + public void testLoginWithOtherCookies() { + String otherCookies = "load=balancer;"; + service.logout(); + service.cookieStore.removeAll(); + service.cookieStore.add(otherCookies); + service.login(); + service.getApplications(); + } + + @Ignore + @Test + public void testUsingAuthTokenAndOtherCookie(){ + String bearerToken = "eyJraWQiOiJzcGx1bmsuc2VjcmV0IiwiYWxnIjoiSFM1MTIiLCJ2ZXIiOiJ2MiIsInR0eXAiOiJzdGF0aWMifQ.eyJpc3MiOiJhZG1pbiBmcm9tIDZiMjIzZWI5NmY4YiIsInN1YiI6InRlc3QiLCJhdWQiOiJ1c2VyIiwiaWRwIjoiU3BsdW5rIiwianRpIjoiMWE1MWNiZWMyY2Q0ZGQyMWFjODcxOGRmMTA2MjRjZDU1YTlmM2M3Y2E3NjRkNTgwYWU0YTVmOWRiMDAzZjIxOSIsImlhdCI6MTY2MTE2Mzk0OSwiZXhwIjoxNjYzNzU1OTQ5LCJuYnIiOjE2NjM3NTU5NDl9.CfG8pCqNyupge_AM8oX1GXiEDYVkflJuJ4UkeqAWwH3UpKXP1efDhWX57ee_PkhwXCDwQtvmUWd3gCDFZASg_Q"; + String otherCookies = "load=balancer;"; + Map args = new HashMap<>(); + args.put("cookie", otherCookies); + args.put("host","localhost"); + args.put("port", 8089); + Service s = new Service(args); + s.setBearerToken(bearerToken); + s.getApplications(); + Assert.assertEquals(otherCookies.trim(),s.cookieStore.getCookies().trim()); + } + @Test public void testLoginWithMultipleInvalidCookies() { String validCookie = service.stringifyCookies(); diff --git a/splunk/src/test/java/com/splunk/IndexTest.java b/splunk/src/test/java/com/splunk/IndexTest.java index a98dd56a..56d6670a 100644 --- a/splunk/src/test/java/com/splunk/IndexTest.java +++ b/splunk/src/test/java/com/splunk/IndexTest.java @@ -77,8 +77,8 @@ public void testAttachWithCookieHeader() throws IOException { // Cookies not implemented before version 6.2 return; } - // Check that their are cookies at all - Assert.assertTrue(service.hasCookies()); + // Check that their are Splunk Auth cookies at all + Assert.assertTrue(service.hasSplunkAuthCookies()); // Make a service that only has that cookie String validCookie = service.stringifyCookies(); diff --git a/splunk/src/test/java/com/splunk/ReceiverTest.java b/splunk/src/test/java/com/splunk/ReceiverTest.java index 99e4b0e4..cf86272b 100644 --- a/splunk/src/test/java/com/splunk/ReceiverTest.java +++ b/splunk/src/test/java/com/splunk/ReceiverTest.java @@ -36,7 +36,7 @@ public void testReceiverWithoutCookie() { @Test public void testReceiverWithCookie() { Assume.assumeTrue(service.versionIsAtLeast("6.2")); - Assert.assertTrue(service.hasCookies()); + Assert.assertTrue(service.hasSplunkAuthCookies()); testReceiver(service); } // Make a few simple requests and make sure the results look ok. From 9117f038b8ee599196dc04dcfb6b547abeb672b9 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 23 Aug 2022 15:43:39 +0530 Subject: [PATCH 2/3] Update CookieTest.java --- splunk/src/test/java/com/splunk/CookieTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/splunk/src/test/java/com/splunk/CookieTest.java b/splunk/src/test/java/com/splunk/CookieTest.java index 411fe4af..4e1218ff 100644 --- a/splunk/src/test/java/com/splunk/CookieTest.java +++ b/splunk/src/test/java/com/splunk/CookieTest.java @@ -130,6 +130,7 @@ public void testLoginWithOtherCookies() { service.cookieStore.add(otherCookies); service.login(); service.getApplications(); + service.cookieStore.removeAll(); } @Ignore @@ -154,7 +155,7 @@ public void testLoginWithMultipleInvalidCookies() { Map args = getStandardArgs(); Service s = new Service(args); - + s.addCookie("bad=cookie"); s.addCookie(validCookie); s.addCookie("another_bad=cookie"); From b5520594f240f6de9b3943ce8137e3470981423e Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Wed, 24 Aug 2022 14:42:31 +0530 Subject: [PATCH 3/3] Added test case for Receiver without Splunk Cookie added test case removed console print statements modified test case to use session token from service instead of bearer token --- splunk/src/main/java/com/splunk/Service.java | 2 -- .../java/com/splunk/SimpleCookieStore.java | 1 - .../src/test/java/com/splunk/CookieTest.java | 14 ++++++------- .../test/java/com/splunk/ReceiverTest.java | 21 +++++++++++++++++++ 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index 2b7aad7b..40101a85 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -1112,7 +1112,6 @@ public UserCollection getUsers(Args args) { * @return The current {@code Service} instance. */ public Service login() { - //if (!this.cookieStore.isEmpty() && (this.username == null || this.password == null)) { if (this.cookieStore.hasSplunkAuthCookie() && (this.username == null || this.password == null)) { return this; } @@ -1313,7 +1312,6 @@ public Job search(String query, Map args) { */ @Override public ResponseMessage send(String path, RequestMessage request) { // cookieStore is a protected member of HttpService - //if (token != null && cookieStore.isEmpty() ) { if (token != null && !cookieStore.hasSplunkAuthCookie() ) { request.getHeader().put("Authorization", token); } diff --git a/splunk/src/main/java/com/splunk/SimpleCookieStore.java b/splunk/src/main/java/com/splunk/SimpleCookieStore.java index 10dacccb..4fd60664 100644 --- a/splunk/src/main/java/com/splunk/SimpleCookieStore.java +++ b/splunk/src/main/java/com/splunk/SimpleCookieStore.java @@ -77,7 +77,6 @@ public boolean hasSplunkAuthCookie(){ } for(String cookie : cookieJar.keySet()){ if(cookie.startsWith(SPLUNK_AUTH_COOKIE)){ - System.out.println("HELLO"); return true; } } diff --git a/splunk/src/test/java/com/splunk/CookieTest.java b/splunk/src/test/java/com/splunk/CookieTest.java index 4e1218ff..d11cda76 100644 --- a/splunk/src/test/java/com/splunk/CookieTest.java +++ b/splunk/src/test/java/com/splunk/CookieTest.java @@ -133,17 +133,17 @@ public void testLoginWithOtherCookies() { service.cookieStore.removeAll(); } - @Ignore @Test public void testUsingAuthTokenAndOtherCookie(){ - String bearerToken = "eyJraWQiOiJzcGx1bmsuc2VjcmV0IiwiYWxnIjoiSFM1MTIiLCJ2ZXIiOiJ2MiIsInR0eXAiOiJzdGF0aWMifQ.eyJpc3MiOiJhZG1pbiBmcm9tIDZiMjIzZWI5NmY4YiIsInN1YiI6InRlc3QiLCJhdWQiOiJ1c2VyIiwiaWRwIjoiU3BsdW5rIiwianRpIjoiMWE1MWNiZWMyY2Q0ZGQyMWFjODcxOGRmMTA2MjRjZDU1YTlmM2M3Y2E3NjRkNTgwYWU0YTVmOWRiMDAzZjIxOSIsImlhdCI6MTY2MTE2Mzk0OSwiZXhwIjoxNjYzNzU1OTQ5LCJuYnIiOjE2NjM3NTU5NDl9.CfG8pCqNyupge_AM8oX1GXiEDYVkflJuJ4UkeqAWwH3UpKXP1efDhWX57ee_PkhwXCDwQtvmUWd3gCDFZASg_Q"; + String validToken = service.getToken(); + Assert.assertTrue(validToken.startsWith("Splunk ")); String otherCookies = "load=balancer;"; Map args = new HashMap<>(); args.put("cookie", otherCookies); - args.put("host","localhost"); - args.put("port", 8089); - Service s = new Service(args); - s.setBearerToken(bearerToken); + args.put("host",service.getHost()); + args.put("port", service.getPort()); + Service s = new Service(args); + s.setToken(validToken); s.getApplications(); Assert.assertEquals(otherCookies.trim(),s.cookieStore.getCookies().trim()); } @@ -155,7 +155,7 @@ public void testLoginWithMultipleInvalidCookies() { Map args = getStandardArgs(); Service s = new Service(args); - + s.addCookie("bad=cookie"); s.addCookie(validCookie); s.addCookie("another_bad=cookie"); diff --git a/splunk/src/test/java/com/splunk/ReceiverTest.java b/splunk/src/test/java/com/splunk/ReceiverTest.java index cf86272b..e171b9d1 100644 --- a/splunk/src/test/java/com/splunk/ReceiverTest.java +++ b/splunk/src/test/java/com/splunk/ReceiverTest.java @@ -24,6 +24,8 @@ import java.io.IOException; import java.io.OutputStream; import java.net.Socket; +import java.util.HashMap; +import java.util.Map; public class ReceiverTest extends SDKTestCase { @@ -39,6 +41,25 @@ public void testReceiverWithCookie() { Assert.assertTrue(service.hasSplunkAuthCookies()); testReceiver(service); } + + @Test + public void testReceiverWithoutSplunkCookie() { + String validToken = service.getToken(); + Assert.assertTrue(validToken.startsWith("Splunk ")); + String otherCookies = "load=balancer;"; + Map args = new HashMap<>(); + args.put("cookie", otherCookies); + args.put("host",service.getHost()); + args.put("port", service.getPort()); + Service s = new Service(args); + s.setToken(validToken); + s.version = s.getInfo().getVersion(); + System.out.println(s.version); + Assume.assumeTrue(s.versionIsAtLeast("6.2")); + Assert.assertTrue(!s.cookieStore.isEmpty()); + testReceiver(s); + } + // Make a few simple requests and make sure the results look ok. public void testReceiver(Service passedService) { Receiver receiver = passedService.getReceiver();