Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- 1.8
splunk-version:
- "8.0"
- "8.2.0"
- "latest"
runs-on: ${{ matrix.os }}

services:
Expand Down
155 changes: 155 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,161 @@ To build the documentation for the SDK, it is being automatically generated with
cd splunk
mvn javadoc:javadoc

### Usage
#### Login using username and password
```java
import com.splunk.Service;
import com.splunk.ServiceArgs;

/**
* Login using username and password
*/
public class SplunkLogin {

static Service service = null;
public static void main(String args[]) {
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setPort(8089);
loginArgs.setHost("localhost");
loginArgs.setScheme("https");
loginArgs.setUsername("USERNAME"); // Use your username
loginArgs.setPassword("PASSWORD"); // Use your password

// Initialize the SDK client
service = Service.connect(loginArgs);
}
}
```

#### Login using Session Token
```java
import com.splunk.Service;
import com.splunk.ServiceArgs;

/**
* Login using Session token
*/
public class SplunkLogin {

static Service service = null;
/**
* Session Token.
* Actual token length would be longer than this token length.
*/
static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5";

public static void main(String args[]) {
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setPort(8089);
loginArgs.setHost("localhost");
loginArgs.setScheme("https");
loginArgs.setToken(String.format("Splunk %s", token));

// Initialize the SDK client
service = Service.connect(loginArgs);
}
}
```
* Login using username and password will create Session token internally.
* Login using Credentials (username & password) OR directly using Session token are similar.
* In above two approaches, there is one limitation that expiration time of Session token cannot be extended. User has to re-login every time when token expires.
* To overcome this limitation, **Authentication** token is used instead of Session token.
* In **Authentication** token, user has a provision to set token expiration time. Splunk allows user to set relative/absolute time for token expiration.
* In other words, **Authentication** token is configurable whereas Session token cannot be configured.

#### Login using Authentication Token (RECOMMENDED)
```java
import com.splunk.Service;
import com.splunk.ServiceArgs;

/**
* Login using Authentication token
*/
public class SplunkLogin {

static Service service = null;
/**
* Authentication Token.
* Actual token length would be longer than this token length.
*/
static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5";

public static void main(String args[]) {
ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setPort(8089);
loginArgs.setHost("localhost");
loginArgs.setScheme("https");
loginArgs.setToken(String.format("Bearer %s", token));

// Initialize the SDK client
service = Service.connect(loginArgs);
}
}
```

#### Example of running a simple search by first creating the search job
```java
import com.splunk.Job;
import com.splunk.ResultsReader;
import com.splunk.ResultsReaderXml;
import com.splunk.Service;
import com.splunk.ServiceArgs;

/**
* Logged in using Authentication token.
* Assuming that authentication token is already created from Splunk web.
* Create Job using search creation.
* Read results and print _raw fields
*/
public class SearchExample {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirmed that this is working, nice tutorial! 🚀


static Service service = null;

/**
* Authentication Token.
* Actual token length would be longer than this token length.
*/
static String token = "1k_Ostpl6NBe4iVQ5d6I3Ohla_U5";

public static void main(String args[]) {

ServiceArgs loginArgs = new ServiceArgs();
loginArgs.setPort(8089);
loginArgs.setHost("localhost");
loginArgs.setScheme("https");
loginArgs.setToken(String.format("Bearer %s", token));

// Initialize the SDK client
service = Service.connect(loginArgs);

// Run a simple search by first creating the search job
Job job = service.getJobs().create("search index=_internal | head 10");

// Waiting for search results to be ready
while (!job.isReady()) {
try {
Thread.sleep(500); // 500 ms
} catch (Exception e) {
// Handle exception here.
}
}

// Read results
try {
ResultsReader reader = new ResultsReaderXml(job.getEvents());

// Iterate over events and print _raw field
reader.forEach(event -> System.out.println(event.get("_raw")));

} catch (Exception e) {
// Handle exception here.
}
}
}
```

For more information on authentication using tokens, please visit [Splunk Docs](https://docs.splunk.com/Documentation/Splunk/latest/Security/Setupauthenticationwithtokens).

### Unit tests

The Splunk SDK for Java includes several unit tests that are run at
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ version: '3.6'

services:
splunk:
image: "splunk/splunk:8.0"
image: "splunk/splunk:latest"
container_name: splunk
environment:
- SPLUNK_START_ARGS=--accept-license
- SPLUNK_HEC_TOKEN=11111111-1111-1111-1111-1111111111113
- SPLUNK_PASSWORD=changed!
- SPLUNK_APPS_URL=https://github.com/splunk/sdk-app-collection/releases/download/v1.1.0/sdkappcollection.tgz
- JAVA_VERSION=openjdk:8
ports:
- 8000:8000
- 8088:8088
Expand Down