From 41de3c027ca82ca53b13907efc2012571c8b2226 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 15:23:55 +0530 Subject: [PATCH 01/18] Wildcard checks in-line with other SDKs Added check for wildcards in 'app' and 'owner' while creating/removing a StoragePassword --- .../java/com/splunk/PasswordCollection.java | 20 +++++++++ .../test/java/com/splunk/PasswordTest.java | 44 +++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 4603160c..9eb67ac7 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -50,6 +50,9 @@ public class PasswordCollection extends EntityCollection { * @return The new credential. */ public Password create(String name, String password) { + if(checkForWildcards()){ + throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards."); + } Args args = new Args("password", password); return create(name, args); } @@ -63,6 +66,9 @@ public Password create(String name, String password) { * @return The new credential. */ public Password create(String name, String password, String realm) { + if(checkForWildcards()){ + throw new IllegalArgumentException("While creating StoragePasswords, namespace cannot have wildcards."); + } Args args = new Args(); args.put("password", password); args.put("realm", realm); @@ -97,11 +103,17 @@ public Password get(Object key) { * @return The removed credential, or null if not found. */ public Password remove(String realm, String name) { + if(checkForWildcards()){ + throw new IllegalArgumentException("app context must be specified when removing a password."); + } return super.remove(String.format("%s:%s:", realm, name)); } @Override public Password remove(String key) { + if(checkForWildcards()){ + throw new IllegalArgumentException("app context must be specified when removing a password."); + } // Make it compatible with the old way (low-efficient) if (!key.contains(":")) { Password password = getByUsername((String) key); @@ -129,4 +141,12 @@ private Password getByUsername(String name) { } return null; } + + private Boolean checkForWildcards(){ + Boolean isWildCard = false; + if(service.getOwner().equals("-") || service.getApp().equals("-")){ + isWildCard = true; + } + return isWildCard; + } } diff --git a/splunk/src/test/java/com/splunk/PasswordTest.java b/splunk/src/test/java/com/splunk/PasswordTest.java index 175c8c4c..2cf655b3 100644 --- a/splunk/src/test/java/com/splunk/PasswordTest.java +++ b/splunk/src/test/java/com/splunk/PasswordTest.java @@ -19,6 +19,8 @@ import org.junit.Assert; import org.junit.Test; +import java.util.HashMap; + public class PasswordTest extends SDKTestCase { @Test public void testGettersOfDefaultPasswords() { @@ -129,4 +131,46 @@ public void testPasswordsCompatibleGetByName() { passwords.remove(username); Assert.assertNull(passwords.get(username)); } + @Test + public void testPasswordsCrudWithWildCards(){ + HashMap args = new HashMap(); + args = Command.defaultValues; + args.put("owner", "-"); + args.put("app", "-"); + args.put("username", "admin"); + args.put("password", "changed!"); + Service service = Service.connect(args); + PasswordCollection passwords = service.getPasswords(); + Assert.assertEquals(passwords.size(),0); + + String name = "no-owner"; + String value = "sdk-test-password"; + String realm = "sdk-test-realm"; + + Password password; + // Create a password + try{ + password = passwords.create(name, value); + }catch(IllegalArgumentException e){ + Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage()); + } + try{ + password = passwords.create(name, value, realm); + }catch(IllegalArgumentException e){ + Assert.assertEquals("While creating StoragePasswords, namespace cannot have wildcards.", e.getMessage()); + } + // Remove a password + try{ + password = passwords.remove(name); + }catch(IllegalArgumentException e){ + Assert.assertEquals("app context must be specified when removing a password.", e.getMessage()); + } + try{ + password = passwords.remove(realm, name); + }catch(IllegalArgumentException e){ + Assert.assertEquals("app context must be specified when removing a password.", e.getMessage()); + } + passwords = service.getPasswords(); + Assert.assertEquals(passwords.size(),0); + } } From 0cf88c004cdbae774ec039b53d98152db5316d75 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 15:24:46 +0530 Subject: [PATCH 02/18] Update PasswordTest.java --- splunk/src/test/java/com/splunk/PasswordTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunk/src/test/java/com/splunk/PasswordTest.java b/splunk/src/test/java/com/splunk/PasswordTest.java index 2cf655b3..3d32d393 100644 --- a/splunk/src/test/java/com/splunk/PasswordTest.java +++ b/splunk/src/test/java/com/splunk/PasswordTest.java @@ -132,7 +132,7 @@ public void testPasswordsCompatibleGetByName() { Assert.assertNull(passwords.get(username)); } @Test - public void testPasswordsCrudWithWildCards(){ + public void testPasswordsWithWildCards(){ HashMap args = new HashMap(); args = Command.defaultValues; args.put("owner", "-"); From 771b9719917f76c57d9563c1a0bea52cc3d1e43b Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 17:17:58 +0530 Subject: [PATCH 03/18] Added 'args' parameter for Saved Search History function --- .../src/main/java/com/splunk/SavedSearch.java | 52 +++++++++++++------ .../test/java/com/splunk/SavedSearchTest.java | 32 ++++++++++++ 2 files changed, 69 insertions(+), 15 deletions(-) diff --git a/splunk/src/main/java/com/splunk/SavedSearch.java b/splunk/src/main/java/com/splunk/SavedSearch.java index 96979316..b01fe17d 100644 --- a/splunk/src/main/java/com/splunk/SavedSearch.java +++ b/splunk/src/main/java/com/splunk/SavedSearch.java @@ -68,13 +68,13 @@ public Job dispatch() throws InterruptedException { * Runs the saved search using dispatch arguments. * * @param args Dispatch arguments:
    - *
  • "dispatch.now": A time string that is used to dispatch the search as + *
  • "dispatch.now": A time string that is used to dispatch the search as * though the specified time were the current time.
  • - *
  • "dispatch.*": Overwrites the value of the search field specified in + *
  • "dispatch.*": Overwrites the value of the search field specified in * "*".
  • - *
  • "trigger_actions": A Boolean that indicates whether to trigger alert + *
  • "trigger_actions": A Boolean that indicates whether to trigger alert * actions.
  • - *
  • "force_dispatch": A Boolean that indicates whether to start a new + *
  • "force_dispatch": A Boolean that indicates whether to start a new * search if another instance of this search is already running.
* @return The search job. * @throws InterruptedException The InterruptedException instance @@ -93,7 +93,7 @@ public Job dispatch(Map args) throws InterruptedException { return job; } - + /** * Runs the saved search using dispatch arguments. * @@ -114,6 +114,28 @@ public Job dispatch(SavedSearchDispatchArgs args) throws InterruptedException { */ public Job[] history() { ResponseMessage response = service.get(actionPath("history")); + AtomFeed feed; + return parseHistoryResponse(response); + } + + /** + * Returns an array of search jobs based on passed search arguments + * + * @param args + * @return An array of search jobs + */ + public Job[] history(Map args) { + ResponseMessage response = service.get(actionPath("history"), args); + return parseHistoryResponse(response); + } + + /** + * Parses response message from history action path + * + * @param response + * @return result An array of Job + */ + private Job[] parseHistoryResponse(final ResponseMessage response) { AtomFeed feed; try { feed = AtomFeed.parseStream(response.getContent()); @@ -170,7 +192,7 @@ public String getActionEmailCc() { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @return The search command (or pipeline). @@ -630,7 +652,7 @@ public String getActionSummaryIndexName() { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @return The search command (or pipeline). @@ -906,7 +928,7 @@ public int getDispatchMaxCount() { public int getDispatchMaxTime() { return getInteger("dispatch.max_time"); } - + /** * Returns how frequently Splunk runs the MapReduce reduce phase * on accumulated map values. @@ -928,7 +950,7 @@ public int getDispatchReduceFrequency() { public boolean getDispatchRtBackfill() { return getDispatchRealTimeBackfill(); } - + /** * Indicates whether to back fill the real-time window for this search. * This attribute only applies to real-time searches. @@ -1235,7 +1257,7 @@ public void setActionEmailCc(String cc) { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @param command The search command (or pipeline). @@ -1547,7 +1569,7 @@ public void setActionPopulateLookupTtl(String ttl) { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @param command The search command (or pipeline). @@ -1614,7 +1636,7 @@ public void setActionRssTtl(String ttl) { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @param command The search command (or pipeline). @@ -1702,7 +1724,7 @@ public void setActionSummaryIndexName(String name) { *

* Generally, this command is a template search pipeline that is realized * with values from the saved search. To reference saved search field - * values, wrap them in "$". For example, use "$name$" to reference the + * values, wrap them in "$". For example, use "$name$" to reference the * saved search name, or use "$search$" to reference the search query. * * @param command The search command (or pipeline). @@ -1954,7 +1976,7 @@ public void setDisabled(boolean disabled) { public void setDispatchBuckets(String buckets) { setDispatchBuckets(Integer.parseInt(buckets)); } - + /** * Sets the maximum number of timeline buckets. * @@ -2161,7 +2183,7 @@ public void setRequestUiDispatchView(String view) { public void setRestartOnSearchpeerAdd(boolean restart) { setRestartOnSearchPeerAdd(restart); } - + /** * Sets whether a real-time search managed by the scheduler is * restarted when a search peer becomes available for this saved search. diff --git a/splunk/src/test/java/com/splunk/SavedSearchTest.java b/splunk/src/test/java/com/splunk/SavedSearchTest.java index fcaf672b..dc45caa1 100644 --- a/splunk/src/test/java/com/splunk/SavedSearchTest.java +++ b/splunk/src/test/java/com/splunk/SavedSearchTest.java @@ -21,6 +21,8 @@ import org.junit.Before; import org.junit.Test; +import java.util.HashMap; + public class SavedSearchTest extends SDKTestCase { SavedSearchCollection savedSearches; String savedSearchName; @@ -429,4 +431,34 @@ public boolean predicate() { Assert.fail(e.toString()); } } + + @Test + public void testHistoryWithArgs(){ + savedSearch.refresh(); + Assert.assertEquals(0, savedSearch.history().length); + try { + Job job; + for(int i = 0 ; i < 31 ; i++){ + job = savedSearch.dispatch(); + while(!job.isReady()){ + sleep(2); + } + } + //history without any argument, it will return max 30 jobs only. + Assert.assertEquals(30, savedSearch.history().length); + + //history with argument 'count' set to '0' i.e it returns the whole history + HashMap args = new HashMap(); + args.put("count", 0); + Assert.assertEquals(31, savedSearch.history(args).length); + + //history with argument 'count' set to '10' i.e. it will return only 10 jobs from history + args.put("count", 10); + args.put("sort_dir", "desc"); + Assert.assertEquals(10, savedSearch.history(args).length); + + } catch (InterruptedException e) { + Assert.fail(e.toString()); + } + } } From 1f2b8a3092e2c954166d85b7f24c584dd5472bf6 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 14 Jun 2022 17:24:11 +0530 Subject: [PATCH 04/18] Update PasswordCollection.java --- splunk/src/main/java/com/splunk/PasswordCollection.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 9eb67ac7..3119013a 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -144,7 +144,7 @@ private Password getByUsername(String name) { private Boolean checkForWildcards(){ Boolean isWildCard = false; - if(service.getOwner().equals("-") || service.getApp().equals("-")){ + if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){ isWildCard = true; } return isWildCard; From a6ed62fd952c5670001eb627eab419064ef5b11a Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 12 Jul 2022 17:07:26 +0530 Subject: [PATCH 05/18] Update PasswordCollection.java changed 'Boolean' to 'boolean' --- splunk/src/main/java/com/splunk/PasswordCollection.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/splunk/src/main/java/com/splunk/PasswordCollection.java b/splunk/src/main/java/com/splunk/PasswordCollection.java index 3119013a..47a50a22 100644 --- a/splunk/src/main/java/com/splunk/PasswordCollection.java +++ b/splunk/src/main/java/com/splunk/PasswordCollection.java @@ -142,8 +142,8 @@ private Password getByUsername(String name) { return null; } - private Boolean checkForWildcards(){ - Boolean isWildCard = false; + private boolean checkForWildcards(){ + boolean isWildCard = false; if(("-").equals(service.getOwner()) || ("-").equals(service.getApp())){ isWildCard = true; } From fabe38c302505fffc76889885f9eb1c2a2b40b84 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 23 Aug 2022 14:03:40 +0530 Subject: [PATCH 06/18] 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 07/18] 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 08/18] 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(); From 7adbaba5be6443e18725ff0a94b3e7232d4847fa Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Mon, 29 Aug 2022 16:04:57 +0530 Subject: [PATCH 09/18] v2 Search API changes reverted --- splunk/src/main/java/com/splunk/Job.java | 21 ++++++++-------- .../main/java/com/splunk/JobCollection.java | 13 ++++++---- splunk/src/main/java/com/splunk/Service.java | 24 +++++++++---------- 3 files changed, 31 insertions(+), 27 deletions(-) diff --git a/splunk/src/main/java/com/splunk/Job.java b/splunk/src/main/java/com/splunk/Job.java index d101eeaf..cb090c65 100644 --- a/splunk/src/main/java/com/splunk/Job.java +++ b/splunk/src/main/java/com/splunk/Job.java @@ -368,18 +368,19 @@ private InputStream getEventsMethod(String methodPath, Map args) { args.put("segmentation", "none"); } + ResponseMessage response = service.get(path + methodPath, args); // Splunk version pre-9.0 doesn't support v2 // v1(GET), v2(POST) - String fullPath; - ResponseMessage response; - if (service.versionIsEarlierThan("9.0")) { - fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; - response = service.get(fullPath, args); - } - else { - fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath; - response = service.post(fullPath, args); - } +// String fullPath; +// ResponseMessage response; +// if (service.versionIsEarlierThan("9.0")) { +// fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; +// response = service.get(fullPath, args); +// } +// else { +// fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath; +// response = service.post(fullPath, args); +// } return response.getContent(); } diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index b47feadc..b130040c 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -27,14 +27,15 @@ public class JobCollection extends EntityCollection { static String oneShotNotAllowed = String.format( "Oneshot not allowed, use service oneshot search method"); static final String REST_PATH = "search/jobs"; - static final String REST_PATH_V2 = "search/v2/jobs"; + //static final String REST_PATH_V2 = "search/v2/jobs"; /** * Class constructor. * * @param service The connected {@code Service} instance. */ JobCollection(Service service) { - super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); + super(service, REST_PATH, Job.class); + //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -46,7 +47,8 @@ public class JobCollection extends EntityCollection { * return and how to sort them (see {@link CollectionArgs}). */ JobCollection(Service service, Args args) { - super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); + super(service, REST_PATH, Job.class); + //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); this.refreshArgs.put("count", "0"); } @@ -87,8 +89,9 @@ public Job create(String query, Map args) { .item(0) .getTextContent(); - String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; - Job job = new Job(service, path + "/" + sid); + Job job = new Job(service, REST_PATH + "/" + sid); + //String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; + //Job job = new Job(service, path + "/" + sid); job.refresh(); return job; diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index cbe63b4a..6645a315 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -223,13 +223,13 @@ public InputStream export(String search, Map args) { if (!args.containsKey("segmentation")) { args.put("segmentation", "none"); } - ResponseMessage response; - - if (versionIsAtLeast("9.0")) - response = post(JobCollection.REST_PATH_V2 + "/export", args); - else { - response = post(JobCollection.REST_PATH + "/export", args); - } + ResponseMessage response = get(JobCollection.REST_PATH + "/export", args); +// ResponseMessage response; +// if (versionIsAtLeast("9.0")) +// response = post(JobCollection.REST_PATH_V2 + "/export", args); +// else { +// response = post(JobCollection.REST_PATH + "/export", args); +// } return new ExportResultsStream(response.getContent()); } @@ -1257,11 +1257,11 @@ public ResponseMessage parse(String query) { */ public ResponseMessage parse(String query, Map args) { args = Args.create(args).add("q", query); - - if (versionIsAtLeast("9.0")) - return post("search/v2/parser", args); - else - return get("search/parser", args); + return get("search/parser", args); +// if (versionIsAtLeast("9.0")) +// return post("search/v2/parser", args); +// else +// return get("search/parser", args); } /** From 2c18e26d2b6d5948f2efc57973024281275e6199 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Mon, 29 Aug 2022 16:06:12 +0530 Subject: [PATCH 10/18] Update JobCollection.java --- splunk/src/main/java/com/splunk/JobCollection.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index b130040c..3e98771c 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -27,7 +27,7 @@ public class JobCollection extends EntityCollection { static String oneShotNotAllowed = String.format( "Oneshot not allowed, use service oneshot search method"); static final String REST_PATH = "search/jobs"; - //static final String REST_PATH_V2 = "search/v2/jobs"; +// static final String REST_PATH_V2 = "search/v2/jobs"; /** * Class constructor. * @@ -35,7 +35,7 @@ public class JobCollection extends EntityCollection { */ JobCollection(Service service) { super(service, REST_PATH, Job.class); - //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); +// super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -48,7 +48,7 @@ public class JobCollection extends EntityCollection { */ JobCollection(Service service, Args args) { super(service, REST_PATH, Job.class); - //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); +// super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); this.refreshArgs.put("count", "0"); } @@ -90,8 +90,8 @@ public Job create(String query, Map args) { .getTextContent(); Job job = new Job(service, REST_PATH + "/" + sid); - //String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; - //Job job = new Job(service, path + "/" + sid); +// String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; +// Job job = new Job(service, path + "/" + sid); job.refresh(); return job; From d6fe2006e17bab76e2e428b7b6fb181a08042ed5 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Wed, 31 Aug 2022 14:22:20 +0530 Subject: [PATCH 11/18] Revert "Update JobCollection.java" This reverts commit 2c18e26d2b6d5948f2efc57973024281275e6199. --- splunk/src/main/java/com/splunk/JobCollection.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index 3e98771c..b130040c 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -27,7 +27,7 @@ public class JobCollection extends EntityCollection { static String oneShotNotAllowed = String.format( "Oneshot not allowed, use service oneshot search method"); static final String REST_PATH = "search/jobs"; -// static final String REST_PATH_V2 = "search/v2/jobs"; + //static final String REST_PATH_V2 = "search/v2/jobs"; /** * Class constructor. * @@ -35,7 +35,7 @@ public class JobCollection extends EntityCollection { */ JobCollection(Service service) { super(service, REST_PATH, Job.class); -// super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); + //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -48,7 +48,7 @@ public class JobCollection extends EntityCollection { */ JobCollection(Service service, Args args) { super(service, REST_PATH, Job.class); -// super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); + //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); this.refreshArgs.put("count", "0"); } @@ -90,8 +90,8 @@ public Job create(String query, Map args) { .getTextContent(); Job job = new Job(service, REST_PATH + "/" + sid); -// String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; -// Job job = new Job(service, path + "/" + sid); + //String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; + //Job job = new Job(service, path + "/" + sid); job.refresh(); return job; From e7a921b1ce31d2028870f33dee1a18149548981c Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Wed, 31 Aug 2022 14:22:24 +0530 Subject: [PATCH 12/18] Revert "v2 Search API changes reverted" This reverts commit 7adbaba5be6443e18725ff0a94b3e7232d4847fa. --- splunk/src/main/java/com/splunk/Job.java | 21 ++++++++-------- .../main/java/com/splunk/JobCollection.java | 13 ++++------ splunk/src/main/java/com/splunk/Service.java | 24 +++++++++---------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/splunk/src/main/java/com/splunk/Job.java b/splunk/src/main/java/com/splunk/Job.java index cb090c65..d101eeaf 100644 --- a/splunk/src/main/java/com/splunk/Job.java +++ b/splunk/src/main/java/com/splunk/Job.java @@ -368,19 +368,18 @@ private InputStream getEventsMethod(String methodPath, Map args) { args.put("segmentation", "none"); } - ResponseMessage response = service.get(path + methodPath, args); // Splunk version pre-9.0 doesn't support v2 // v1(GET), v2(POST) -// String fullPath; -// ResponseMessage response; -// if (service.versionIsEarlierThan("9.0")) { -// fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; -// response = service.get(fullPath, args); -// } -// else { -// fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath; -// response = service.post(fullPath, args); -// } + String fullPath; + ResponseMessage response; + if (service.versionIsEarlierThan("9.0")) { + fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; + response = service.get(fullPath, args); + } + else { + fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath; + response = service.post(fullPath, args); + } return response.getContent(); } diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index b130040c..b47feadc 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -27,15 +27,14 @@ public class JobCollection extends EntityCollection { static String oneShotNotAllowed = String.format( "Oneshot not allowed, use service oneshot search method"); static final String REST_PATH = "search/jobs"; - //static final String REST_PATH_V2 = "search/v2/jobs"; + static final String REST_PATH_V2 = "search/v2/jobs"; /** * Class constructor. * * @param service The connected {@code Service} instance. */ JobCollection(Service service) { - super(service, REST_PATH, Job.class); - //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); + super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -47,8 +46,7 @@ public class JobCollection extends EntityCollection { * return and how to sort them (see {@link CollectionArgs}). */ JobCollection(Service service, Args args) { - super(service, REST_PATH, Job.class); - //super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); + super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); this.refreshArgs.put("count", "0"); } @@ -89,9 +87,8 @@ public Job create(String query, Map args) { .item(0) .getTextContent(); - Job job = new Job(service, REST_PATH + "/" + sid); - //String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; - //Job job = new Job(service, path + "/" + sid); + String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; + Job job = new Job(service, path + "/" + sid); job.refresh(); return job; diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index 6645a315..cbe63b4a 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -223,13 +223,13 @@ public InputStream export(String search, Map args) { if (!args.containsKey("segmentation")) { args.put("segmentation", "none"); } - ResponseMessage response = get(JobCollection.REST_PATH + "/export", args); -// ResponseMessage response; -// if (versionIsAtLeast("9.0")) -// response = post(JobCollection.REST_PATH_V2 + "/export", args); -// else { -// response = post(JobCollection.REST_PATH + "/export", args); -// } + ResponseMessage response; + + if (versionIsAtLeast("9.0")) + response = post(JobCollection.REST_PATH_V2 + "/export", args); + else { + response = post(JobCollection.REST_PATH + "/export", args); + } return new ExportResultsStream(response.getContent()); } @@ -1257,11 +1257,11 @@ public ResponseMessage parse(String query) { */ public ResponseMessage parse(String query, Map args) { args = Args.create(args).add("q", query); - return get("search/parser", args); -// if (versionIsAtLeast("9.0")) -// return post("search/v2/parser", args); -// else -// return get("search/parser", args); + + if (versionIsAtLeast("9.0")) + return post("search/v2/parser", args); + else + return get("search/parser", args); } /** From 04e56683e5f26c7495af69cedc32c7c55484c554 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Thu, 1 Sep 2022 14:54:03 +0530 Subject: [PATCH 13/18] updated version checks for v2 Search APIs --- splunk/src/main/java/com/splunk/Job.java | 2 +- splunk/src/main/java/com/splunk/JobCollection.java | 6 +++--- splunk/src/main/java/com/splunk/Service.java | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/splunk/src/main/java/com/splunk/Job.java b/splunk/src/main/java/com/splunk/Job.java index d101eeaf..6b3c05a0 100644 --- a/splunk/src/main/java/com/splunk/Job.java +++ b/splunk/src/main/java/com/splunk/Job.java @@ -372,7 +372,7 @@ private InputStream getEventsMethod(String methodPath, Map args) { // v1(GET), v2(POST) String fullPath; ResponseMessage response; - if (service.versionIsEarlierThan("9.0")) { + if (service.versionIsEarlierThan("9.0.2")) { fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; response = service.get(fullPath, args); } diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index b47feadc..2ab929b8 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -34,7 +34,7 @@ public class JobCollection extends EntityCollection { * @param service The connected {@code Service} instance. */ JobCollection(Service service) { - super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class); + super(service, service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -46,7 +46,7 @@ public class JobCollection extends EntityCollection { * return and how to sort them (see {@link CollectionArgs}). */ JobCollection(Service service, Args args) { - super(service, service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH, Job.class, args); + super(service, service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH, Job.class, args); this.refreshArgs.put("count", "0"); } @@ -87,7 +87,7 @@ public Job create(String query, Map args) { .item(0) .getTextContent(); - String path = service.versionIsAtLeast("9.0") ? REST_PATH_V2 : REST_PATH; + String path = service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH; Job job = new Job(service, path + "/" + sid); job.refresh(); diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index cbe63b4a..2a1fe5c7 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -225,7 +225,7 @@ public InputStream export(String search, Map args) { } ResponseMessage response; - if (versionIsAtLeast("9.0")) + if (versionIsAtLeast("9.0.2")) response = post(JobCollection.REST_PATH_V2 + "/export", args); else { response = post(JobCollection.REST_PATH + "/export", args); @@ -1258,7 +1258,7 @@ public ResponseMessage parse(String query) { public ResponseMessage parse(String query, Map args) { args = Args.create(args).add("q", query); - if (versionIsAtLeast("9.0")) + if (versionIsAtLeast("9.0.2")) return post("search/v2/parser", args); else return get("search/parser", args); @@ -1311,6 +1311,7 @@ public Job search(String query, Map args) { * @return The HTTP response. */ @Override public ResponseMessage send(String path, RequestMessage request) { + System.out.println("Path: "+path); // cookieStore is a protected member of HttpService if (token != null && cookieStore.isEmpty()) { request.getHeader().put("Authorization", token); From baba144e71a8395c254d43726dfe68617e17c321 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Thu, 1 Sep 2022 15:25:28 +0530 Subject: [PATCH 14/18] Update Service.java --- splunk/src/main/java/com/splunk/Service.java | 1 - 1 file changed, 1 deletion(-) diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index 2a1fe5c7..7601f5da 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -1311,7 +1311,6 @@ public Job search(String query, Map args) { * @return The HTTP response. */ @Override public ResponseMessage send(String path, RequestMessage request) { - System.out.println("Path: "+path); // cookieStore is a protected member of HttpService if (token != null && cookieStore.isEmpty()) { request.getHeader().put("Authorization", token); From 9423ce6d37c1a644fbb52a40e8837404a49bf673 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Mon, 12 Sep 2022 17:35:22 +0530 Subject: [PATCH 15/18] revamped version check logic for enabling V2 search APIs --- splunk/src/main/java/com/splunk/Job.java | 4 ++-- .../main/java/com/splunk/JobCollection.java | 6 +++--- splunk/src/main/java/com/splunk/Service.java | 17 ++++++++++++++-- .../src/main/java/com/splunk/ServiceInfo.java | 2 ++ .../src/test/java/com/splunk/ServiceTest.java | 20 +++++++++++++++++++ 5 files changed, 42 insertions(+), 7 deletions(-) diff --git a/splunk/src/main/java/com/splunk/Job.java b/splunk/src/main/java/com/splunk/Job.java index 6b3c05a0..354d413c 100644 --- a/splunk/src/main/java/com/splunk/Job.java +++ b/splunk/src/main/java/com/splunk/Job.java @@ -372,7 +372,7 @@ private InputStream getEventsMethod(String methodPath, Map args) { // v1(GET), v2(POST) String fullPath; ResponseMessage response; - if (service.versionIsEarlierThan("9.0.2")) { + if (!service.enableV2SearchApi()) { fullPath = path.replace(JobCollection.REST_PATH_V2, JobCollection.REST_PATH) + methodPath; response = service.get(fullPath, args); } @@ -380,7 +380,7 @@ private InputStream getEventsMethod(String methodPath, Map args) { fullPath = path.replace(JobCollection.REST_PATH, JobCollection.REST_PATH_V2) + methodPath; response = service.post(fullPath, args); } - + return response.getContent(); } diff --git a/splunk/src/main/java/com/splunk/JobCollection.java b/splunk/src/main/java/com/splunk/JobCollection.java index 2ab929b8..661e21ff 100644 --- a/splunk/src/main/java/com/splunk/JobCollection.java +++ b/splunk/src/main/java/com/splunk/JobCollection.java @@ -34,7 +34,7 @@ public class JobCollection extends EntityCollection { * @param service The connected {@code Service} instance. */ JobCollection(Service service) { - super(service, service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH, Job.class); + super(service, service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -46,7 +46,7 @@ public class JobCollection extends EntityCollection { * return and how to sort them (see {@link CollectionArgs}). */ JobCollection(Service service, Args args) { - super(service, service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH, Job.class, args); + super(service, service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH, Job.class); this.refreshArgs.put("count", "0"); } @@ -87,7 +87,7 @@ public Job create(String query, Map args) { .item(0) .getTextContent(); - String path = service.versionIsAtLeast("9.0.2") ? REST_PATH_V2 : REST_PATH; + String path = service.enableV2SearchApi() ? REST_PATH_V2 : REST_PATH; Job job = new Job(service, path + "/" + sid); job.refresh(); diff --git a/splunk/src/main/java/com/splunk/Service.java b/splunk/src/main/java/com/splunk/Service.java index 1a39a211..d6d15b8f 100644 --- a/splunk/src/main/java/com/splunk/Service.java +++ b/splunk/src/main/java/com/splunk/Service.java @@ -66,6 +66,9 @@ public class Service extends BaseService { /** The version of this Splunk instance, once logged in. */ public String version = null; + /** The type of this Splunk instance, once logged in. */ + public String instanceType = null; + /** The default host name, which is used when a host name is not provided.*/ public static String DEFAULT_HOST = "localhost"; @@ -225,7 +228,7 @@ public InputStream export(String search, Map args) { } ResponseMessage response; - if (versionIsAtLeast("9.0.2")) + if(enableV2SearchApi()) response = post(JobCollection.REST_PATH_V2 + "/export", args); else { response = post(JobCollection.REST_PATH + "/export", args); @@ -1147,6 +1150,7 @@ public Service login(String username, String password) { .getTextContent(); this.token = "Splunk " + sessionKey; this.version = this.getInfo().getVersion(); + this.instanceType = this.getInfo().getInstanceType(); if (versionCompare("4.3") >= 0) this.passwordEndPoint = "storage/passwords"; @@ -1258,7 +1262,7 @@ public ResponseMessage parse(String query) { public ResponseMessage parse(String query, Map args) { args = Args.create(args).add("q", query); - if (versionIsAtLeast("9.0.2")) + if(enableV2SearchApi()) return post("search/v2/parser", args); else return get("search/parser", args); @@ -1350,6 +1354,15 @@ public void setBearerToken(String value) { this.token = value.contains("Splunk") || value.contains("Bearer") ? value : "Bearer " + value; } + + public boolean enableV2SearchApi(){ + if(this.instanceType.equalsIgnoreCase("cloud")) { + return versionIsAtLeast("9.0.2209"); + }else{ + return versionIsAtLeast("9.0.2"); + } + } + /** * Returns true if this Splunk instance's version is no earlier than * the version specified in {@code version}. diff --git a/splunk/src/main/java/com/splunk/ServiceInfo.java b/splunk/src/main/java/com/splunk/ServiceInfo.java index 72f556bd..16ec8aaa 100644 --- a/splunk/src/main/java/com/splunk/ServiceInfo.java +++ b/splunk/src/main/java/com/splunk/ServiceInfo.java @@ -155,6 +155,8 @@ public String getVersion() { return getString("version"); } + public String getInstanceType() {return getString("instance_type", "");} + /** * Indicates whether this Splunk instance is running under a free license. * diff --git a/splunk/src/test/java/com/splunk/ServiceTest.java b/splunk/src/test/java/com/splunk/ServiceTest.java index 1bd80833..ce9aafdf 100644 --- a/splunk/src/test/java/com/splunk/ServiceTest.java +++ b/splunk/src/test/java/com/splunk/ServiceTest.java @@ -715,4 +715,24 @@ public void testPost() { Assert.assertTrue(firstLineIsXmlDtd(response.getContent())); } + /* + Test whether the V2 and V1 Search APIs are switched properly based on the Type of Splunk Instance and Version. + */ + @Test + public void testEnableV2Api(){ + if(service.instanceType.equalsIgnoreCase("cloud")) { + if(service.versionIsEarlierThan("9.0.2209")){ + Assert.assertFalse(service.enableV2SearchApi()); + }else{ + Assert.assertTrue(service.enableV2SearchApi()); + } + }else{ + if(service.versionIsEarlierThan("9.0.2")){ + Assert.assertFalse(service.enableV2SearchApi()); + }else{ + Assert.assertTrue(service.enableV2SearchApi()); + } + } + } + } From d822d129ff79e81d01e2cdafc7d139783ab6a27d Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 13 Sep 2022 11:38:49 +0530 Subject: [PATCH 16/18] Release updates --- CHANGELOG.md | 8 +++++ README.md | 4 +-- deploy | 2 +- deploy.md | 30 +++++++++---------- examples/pom.xml | 2 +- pom.xml | 2 +- splunk/pom.xml | 2 +- .../src/main/java/com/splunk/HttpService.java | 2 +- 8 files changed, 30 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa425ff9..d120e968 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Splunk Enterprise SDK for Java Changelog +## Version 1.9.1 + +### New Features and APIs +* SDK Support for third-party (Load Balancer) "sticky sessions"(cookie persistence) (Github PR [#192](https://github.com/splunk/splunk-sdk-java/pull/192)) + +### Minor Changes +* Special handling related to the semantic versioning of specific Search APIs functional in Splunk Enterprise 9.0.2 and (Splunk Cloud 9.0.2209). These SDK changes will enable seamless transition between the APIs based on the version of the Splunk Enterprise/Cloud (Github PR [#193](https://github.com/splunk/splunk-sdk-java/pull/193)) + ## Version 1.9.0 ### New Features and APIs diff --git a/README.md b/README.md index 3087c219..72b778d5 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ [![Java SDK Test](https://github.com/splunk/splunk-sdk-java/actions/workflows/test.yml/badge.svg?branch=master)](https://github.com/splunk/splunk-sdk-java/actions/workflows/test.yml) # The Splunk Software Development Kit for Java -#### Version 1.9.0 +#### Version 1.9.1 The Splunk Software Development Kit (SDK) for Java contains library code and examples designed to enable developers to build applications using Splunk. @@ -75,7 +75,7 @@ To add the Splunk SDK for Java `.JAR` file as a dependency: com.splunk splunk - 1.9.0 + 1.9.1 ``` diff --git a/deploy b/deploy index cb13e579..26bce595 100755 --- a/deploy +++ b/deploy @@ -2,7 +2,7 @@ declare -r scriptDirectory="$(dirname $(readlink -e $0))" declare -r scriptName="$(basename $0)" -declare -r version="1.9.0" +declare -r version="1.9.1" if [[ $# -ne 1 ]]; then echo 1>&2 "Usage: ${scriptName} {local|staging||production}" diff --git a/deploy.md b/deploy.md index ec5046e5..eab0df26 100644 --- a/deploy.md +++ b/deploy.md @@ -9,8 +9,8 @@ deploy \ ##DESCRIPTION -Deploy transmits **target/splunk-1.9.0.jar**, **target/splunk-1.9.0-javadoc.jar**, and -**target/splunk-1.9.0-sources.jar** to the **local**, **staging**, or **production** +Deploy transmits **target/splunk-1.9.1.jar**, **target/splunk-1.9.1-javadoc.jar**, and +**target/splunk-1.9.1-sources.jar** to the **local**, **staging**, or **production** maven repository. Repository names are mapped to locations as follows. | repository-name | location | @@ -21,18 +21,18 @@ maven repository. Repository names are mapped to locations as follows. After deployment you should find this tree structure at the location of your repository - com/splunk/splunk/1.9.0/ - ├── splunk-1.9.0-javadoc.jar - ├── splunk-1.9.0-javadoc.jar.md5 - ├── splunk-1.9.0-javadoc.jar.sha1 - ├── splunk-1.9.0-sources.jar - ├── splunk-1.9.0-sources.jar.md5 - ├── splunk-1.9.0-sources.jar.sha1 - ├── splunk-1.9.0.jar - ├── splunk-1.9.0.jar.md5 - ├── splunk-1.9.0.jar.sha1 - ├── splunk-1.9.0.pom - ├── splunk-1.9.0.pom.md5 - └── splunk-1.9.0.pom.sha1 + com/splunk/splunk/1.9.1/ + ├── splunk-1.9.1-javadoc.jar + ├── splunk-1.9.1-javadoc.jar.md5 + ├── splunk-1.9.1-javadoc.jar.sha1 + ├── splunk-1.9.1-sources.jar + ├── splunk-1.9.1-sources.jar.md5 + ├── splunk-1.9.1-sources.jar.sha1 + ├── splunk-1.9.1.jar + ├── splunk-1.9.1.jar.md5 + ├── splunk-1.9.1.jar.sha1 + ├── splunk-1.9.1.pom + ├── splunk-1.9.1.pom.md5 + └── splunk-1.9.1.pom.sha1 Verify this structure prior to release. diff --git a/examples/pom.xml b/examples/pom.xml index 4b2b2462..1573e401 100644 --- a/examples/pom.xml +++ b/examples/pom.xml @@ -37,7 +37,7 @@ com.splunk splunk - 1.9.0 + 1.9.1 provided diff --git a/pom.xml b/pom.xml index 195c08c8..222042bd 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ 4.0.0 - 1.9.0 + 1.9.1 true UTF-8 8 diff --git a/splunk/pom.xml b/splunk/pom.xml index 80680170..186d8eda 100644 --- a/splunk/pom.xml +++ b/splunk/pom.xml @@ -5,7 +5,7 @@ 4.0.0 splunk - 1.9.0 + 1.9.1 splunk-sdk-java com.splunk diff --git a/splunk/src/main/java/com/splunk/HttpService.java b/splunk/src/main/java/com/splunk/HttpService.java index aa6769b8..ece901ca 100644 --- a/splunk/src/main/java/com/splunk/HttpService.java +++ b/splunk/src/main/java/com/splunk/HttpService.java @@ -89,7 +89,7 @@ public boolean verify(String s, SSLSession sslSession) { private String prefix = null; static Map defaultHeader = new HashMap() {{ - put("User-Agent", "splunk-sdk-java/1.9.0"); + put("User-Agent", "splunk-sdk-java/1.9.1"); put("Accept", "*/*"); }}; From 9f46b7b3b6966d26c1f1d914fc9d1fc0b7c9b448 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 13 Sep 2022 11:57:42 +0530 Subject: [PATCH 17/18] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 222042bd..827b8db6 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ com.google.code.gson gson - 2.8.6 + 2.8.9 net.sf.opencsv From 0797d898600850c0a1bf7f1a3dd8114129e956c9 Mon Sep 17 00:00:00 2001 From: Abhi Shah Date: Tue, 13 Sep 2022 15:10:38 +0530 Subject: [PATCH 18/18] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d120e968..524833fd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,9 +4,11 @@ ### New Features and APIs * SDK Support for third-party (Load Balancer) "sticky sessions"(cookie persistence) (Github PR [#192](https://github.com/splunk/splunk-sdk-java/pull/192)) +* Added Args option for Saved Search history method (GitHub Issue [#126](https://github.com/splunk/splunk-sdk-java/issues/126) & PR [#188](https://github.com/splunk/splunk-sdk-java/pull/188) ) ### Minor Changes * Special handling related to the semantic versioning of specific Search APIs functional in Splunk Enterprise 9.0.2 and (Splunk Cloud 9.0.2209). These SDK changes will enable seamless transition between the APIs based on the version of the Splunk Enterprise/Cloud (Github PR [#193](https://github.com/splunk/splunk-sdk-java/pull/193)) +* Updated checks to fetch Storage Passwords with wildcards in namespace. (GitHub PR [#187](https://github.com/splunk/splunk-sdk-java/pull/187)) ## Version 1.9.0