Skip to content

Commit a9774fd

Browse files
committed
Merge branch 'persist-cookies' into revert-v2-changes
2 parents baba144 + b552059 commit a9774fd

File tree

7 files changed

+76
-13
lines changed

7 files changed

+76
-13
lines changed

splunk/src/main/java/com/splunk/HttpService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,12 @@ public void removeAllCookies() {
316316
}
317317

318318
/**
319-
* Returns true if the cookeStore has any cookies, false otherwise
319+
* Returns true if the cookieStore has any Splunk Authorization cookies, false otherwise
320320
*
321321
* @return True if there are cookies, false otherwise
322322
*/
323-
public Boolean hasCookies() {
324-
return !cookieStore.isEmpty();
323+
public Boolean hasSplunkAuthCookies() {
324+
return cookieStore.hasSplunkAuthCookie();
325325
}
326326

327327
/**

splunk/src/main/java/com/splunk/Receiver.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,13 @@ public Socket attach(String indexName, Args args) throws IOException {
9898
headers.add("Accept-Encoding: identity");
9999
headers.add("X-Splunk-Input-Mode: Streaming");
100100

101-
if (service.hasCookies()) {
101+
if (service.hasSplunkAuthCookies()) {
102102
headers.add(String.format("Cookie: %s", service.stringifyCookies()));
103103
} else {
104+
// to persist the cookies other than Splunk such as from Load Balancer
105+
if(!service.cookieStore.isEmpty()){
106+
headers.add(String.format("Cookie: %s", service.stringifyCookies()));
107+
}
104108
headers.add(String.format("Authorization: %s", service.getToken()));
105109
}
106110
headers.add("");

splunk/src/main/java/com/splunk/Service.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ public UserCollection getUsers(Args args) {
11121112
* @return The current {@code Service} instance.
11131113
*/
11141114
public Service login() {
1115-
if (!this.cookieStore.isEmpty() && (this.username == null || this.password == null)) {
1115+
if (this.cookieStore.hasSplunkAuthCookie() && (this.username == null || this.password == null)) {
11161116
return this;
11171117
}
11181118
else if (this.username == null || this.password == null) {
@@ -1312,7 +1312,7 @@ public Job search(String query, Map<String, Object> args) {
13121312
*/
13131313
@Override public ResponseMessage send(String path, RequestMessage request) {
13141314
// cookieStore is a protected member of HttpService
1315-
if (token != null && cookieStore.isEmpty()) {
1315+
if (token != null && !cookieStore.hasSplunkAuthCookie() ) {
13161316
request.getHeader().put("Authorization", token);
13171317
}
13181318
return super.send(fullpath(path), request);

splunk/src/main/java/com/splunk/SimpleCookieStore.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
*/
2929
class SimpleCookieStore {
3030

31+
public static final String SPLUNK_AUTH_COOKIE = "splunkd_";
32+
3133
private Map<String, String> cookieJar = new HashMap<String, String>();
3234
/**
3335
* Adds cookies from a "Set-Cookie" header to the cookie store.
@@ -69,6 +71,18 @@ public Boolean isEmpty() {
6971
return cookieJar.isEmpty();
7072
}
7173

74+
public boolean hasSplunkAuthCookie(){
75+
if(cookieJar.isEmpty()){
76+
return false;
77+
}
78+
for(String cookie : cookieJar.keySet()){
79+
if(cookie.startsWith(SPLUNK_AUTH_COOKIE)){
80+
return true;
81+
}
82+
}
83+
return false;
84+
}
85+
7286
/**
7387
* Removes all cookies from SimpleCookieStore
7488
*/

splunk/src/test/java/com/splunk/CookieTest.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
package com.splunk;
1818

19+
import java.net.HttpCookie;
1920
import java.util.HashMap;
2021
import java.util.Map;
2122

22-
import org.junit.Assert;
23-
import org.junit.Assume;
24-
import org.junit.Before;
25-
import org.junit.Test;
23+
import org.junit.*;
2624

2725
public class CookieTest extends SDKTestCase {
2826

@@ -124,6 +122,32 @@ public void testLoginWithMultipleCookies() {
124122
s.getSettings().refresh();
125123
}
126124

125+
@Test
126+
public void testLoginWithOtherCookies() {
127+
String otherCookies = "load=balancer;";
128+
service.logout();
129+
service.cookieStore.removeAll();
130+
service.cookieStore.add(otherCookies);
131+
service.login();
132+
service.getApplications();
133+
service.cookieStore.removeAll();
134+
}
135+
136+
@Test
137+
public void testUsingAuthTokenAndOtherCookie(){
138+
String validToken = service.getToken();
139+
Assert.assertTrue(validToken.startsWith("Splunk "));
140+
String otherCookies = "load=balancer;";
141+
Map<String, Object> args = new HashMap<>();
142+
args.put("cookie", otherCookies);
143+
args.put("host",service.getHost());
144+
args.put("port", service.getPort());
145+
Service s = new Service(args);
146+
s.setToken(validToken);
147+
s.getApplications();
148+
Assert.assertEquals(otherCookies.trim(),s.cookieStore.getCookies().trim());
149+
}
150+
127151
@Test
128152
public void testLoginWithMultipleInvalidCookies() {
129153
String validCookie = service.stringifyCookies();

splunk/src/test/java/com/splunk/IndexTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public void testAttachWithCookieHeader() throws IOException {
7777
// Cookies not implemented before version 6.2
7878
return;
7979
}
80-
// Check that their are cookies at all
81-
Assert.assertTrue(service.hasCookies());
80+
// Check that their are Splunk Auth cookies at all
81+
Assert.assertTrue(service.hasSplunkAuthCookies());
8282

8383
// Make a service that only has that cookie
8484
String validCookie = service.stringifyCookies();

splunk/src/test/java/com/splunk/ReceiverTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.io.IOException;
2525
import java.io.OutputStream;
2626
import java.net.Socket;
27+
import java.util.HashMap;
28+
import java.util.Map;
2729

2830
public class ReceiverTest extends SDKTestCase {
2931

@@ -36,9 +38,28 @@ public void testReceiverWithoutCookie() {
3638
@Test
3739
public void testReceiverWithCookie() {
3840
Assume.assumeTrue(service.versionIsAtLeast("6.2"));
39-
Assert.assertTrue(service.hasCookies());
41+
Assert.assertTrue(service.hasSplunkAuthCookies());
4042
testReceiver(service);
4143
}
44+
45+
@Test
46+
public void testReceiverWithoutSplunkCookie() {
47+
String validToken = service.getToken();
48+
Assert.assertTrue(validToken.startsWith("Splunk "));
49+
String otherCookies = "load=balancer;";
50+
Map<String, Object> args = new HashMap<>();
51+
args.put("cookie", otherCookies);
52+
args.put("host",service.getHost());
53+
args.put("port", service.getPort());
54+
Service s = new Service(args);
55+
s.setToken(validToken);
56+
s.version = s.getInfo().getVersion();
57+
System.out.println(s.version);
58+
Assume.assumeTrue(s.versionIsAtLeast("6.2"));
59+
Assert.assertTrue(!s.cookieStore.isEmpty());
60+
testReceiver(s);
61+
}
62+
4263
// Make a few simple requests and make sure the results look ok.
4364
public void testReceiver(Service passedService) {
4465
Receiver receiver = passedService.getReceiver();

0 commit comments

Comments
 (0)