Skip to content

Commit 040b049

Browse files
authored
Merge pull request #81 from cybsecmaster/feature/doc-42
DOC-42 changes
2 parents 277caf3 + c697469 commit 040b049

File tree

1 file changed

+113
-77
lines changed

1 file changed

+113
-77
lines changed

docs/thehive/user-guides/organisation/functions.md

Lines changed: 113 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,69 @@
11
# *Functions*
22

3-
!!! Info
4-
This feature is available with TheHive versions 5.1 and higher.
3+
## Overview
54

6-
Functions enable you to integrate external applications directly into TheHive processing.
5+
A Function in TheHive is a custom JavaScript code block that runs within the platform, accepting inputs from external sources, processing data, and interacting directly with TheHive's APIs to seamlessly integrate external applications into its workflow.
76

8-
A Function is a piece of custom Javascript code that runs inside TheHive. The function can receive inputs from the outside, treat it and call TheHive APIs directly.
7+
For example, you can use a Function to create alerts within TheHive without the need for an additional Python service to convert the data.
98

10-
This can be used for instance to create alerts inside TheHive without a python glue service that transforms the data.
9+
This feature is supported in TheHive starting from version 5.1 and later.
1110

12-
## Create a function
11+
---
1312

14-
Let's imagine that when an event occurs in your system, you want to create an alert in TheHive. Your external system has its own schema for the events, something like:
13+
## Function Usages
1514

16-
!!! Example ""
15+
In real-life use, a Function in TheHive automates tasks like processing data from external systems, triggering alerts, or updating cases. It streamlines workflows by connecting TheHive to other tools, reducing manual work and improving incident management efficiency.
1716

18-
```json
19-
{
20-
"eventId": "d9ec98b1-410f-40eb-8634-cfe189749da6",
21-
"date": "2021-06-05T12:45:36.698Z",
22-
"title": "An intrusion was detected",
23-
"details": "An intrusion was detected on the server 10.10.43.2",
24-
"data": [
25-
{"kind": "ip", "value": "10.10.43.2", "name": "server-ip" },
26-
{"kind": "name", "value": "root", "name": "login" },
27-
{"kind": "ip", "value": "92.43.123.1", "name": "origin" }
28-
]
29-
}
30-
```
17+
---
3118

32-
This format is not the same as TheHive, so you need to transform the data to match TheHive alert format.
19+
## Function Types
3320

34-
As an `org-admin`, you can create new functions for your organisation that can take this input, transform it into TheHive format and create an alert from it.
21+
Below are the different types of functions supported in TheHive. The function type you select determines the scope where the function can be executed:
3522

36-
The code of the function would be something like this:
23+
1. **API**: These functions are triggered by an external service through TheHive's public API, allowing automated workflows initiated from outside the platform.
3724

38-
!!! Example ""
25+
2. **Notification**: Notification functions act as notifiers and are triggered when certain events occur, such as alerts or case updates, automating the notification process based on predefined conditions.
3926

40-
```javascript
41-
function handle(input, context) {
42-
const theHiveAlert = {
43-
"type": "event",
44-
"source": "my-system",
45-
"sourceRef": input.eventId,
46-
"title": input.title,
47-
"description": input.details,
48-
"date": (new Date(input.date)).getTime(),
49-
"observables": input.data.map(data => {
50-
// map event data kind to TheHive Observable type
51-
const dataType = data.kind === "ip" ? "ip": "other";
52-
return {
53-
"dataType": dataType,
54-
"data": data.value,
55-
"tags": [`name:${data.name}`] // use a tag for the data name
56-
}
57-
})
58-
};
59-
// call TheHive APIs, here alert creation
60-
return context.alert.create(theHiveAlert);
61-
}
62-
```
27+
3. **Action: Case**: Functions of the `action:case` type are manually triggered within the context of a specific case, allowing users to perform actions related to case management.
28+
29+
4. **Action: Alert**: The `action:alert` type refers to functions manually executed in the context of an alert, enabling users to process and act on alerts within TheHive.
30+
31+
---
32+
33+
## Function Modes
6334

64-
<figure markdown>
65-
![Create function](./images/functions_create.png){ width="500" }
66-
</figure>
35+
A function in TheHive can operate in one of three modes:
6736

68-
A function can be in one of three modes:
37+
- **Enabled**: The function will run as expected when triggered.
38+
- **Disabled**: The function will not run when triggered.
39+
- **Dry-Run**: The function will execute, but no entities will be created or modified in TheHive. Entity creation attempts will return `null`, making this mode ideal for testing integrations before going live.
6940

70-
- `Enabled`: The function will be executed when called
71-
- `Disabled`: The function will not be executed when called
72-
- `Dry-Run`: The function will be executed but no entity will be created or modified in TheHive. Entity creations will return `null` instead. This can be useful to test your integration before setting it live.
41+
---
7342

74-
The creation page allows you to test your function and see what it would return once executed.
75-
In `dry-run` mode, the function will be executed but no resource creation or modification will be executed.
43+
## Create a Function
7644

77-
<figure markdown>
78-
![Test function input](./images/functions_test_input.png){ width="500" }
79-
</figure>
45+
Follow the steps below to create a function in TheHive:
8046

81-
<figure markdown>
82-
![Test function result](./images/function_test_result.png){ width="500" }
83-
</figure>
47+
1. **Navigate to the Organization Tab**: On the left side of the interface, go to the "Organization" tab.
48+
2. **Access Functions**: In the navigation options, locate "Functions" and click on it.
49+
3. **Create a New Function**: Click on the **+ Create a function** button.
50+
4. **Fill Out the Create Function Form**: You will be presented with the "Create Function" form. Fill out the following details:
51+
- **Name**: Enter a name for the function.
52+
- **Mode**: Select the mode (Enabled, Disabled, or Dry-Run).
53+
- **Type**: Choose the function type (API, Notification, Action: Case, Action: Alert).
54+
- **Description**: Provide a brief description of the function’s purpose.
55+
- **Definition**: Write or paste the function's JavaScript code.
56+
- **Test Function**: You can test the function using input data.
57+
- **How to Call the Function**: Provides an example command for calling the function.
58+
5. **Save the Function**: After completing the form, click **Save** to create the function.
8459

85-
## Call a function
60+
&nbsp;
61+
62+
![Create function](./images/functions_create.png)
63+
64+
---
65+
66+
## Call a Function
8667

8768
Once saved, the function can then be called with an http call from your system:
8869

@@ -105,13 +86,63 @@ Once saved, the function can then be called with an http call from your system:
10586
```
10687

10788
TheHive will take your input (the body of the http call), the definition of your function and execute the function with the input.
108-
It will respond to the http call with the data returned by the function.
109-
110-
## Example: Create an alert from a Splunk alert
111-
112-
When creating a Splunk alert, you can define a [webhook as an action](https://docs.splunk.com/Documentation/Splunk/9.0.0/Alert/Webhooks). So when the alert is triggered the webhook is called with a payload. But the payload is defined by splunk and can't be changed.
113-
114-
It should look a bit like:
89+
It will respond to the HTTP call with the data returned by the function.
90+
91+
---
92+
93+
## Example 1: Function Use Case
94+
95+
Suppose you want to create an alert in TheHive when an event occurs in your system. Your external system may have its own event schema, similar to the example below:
96+
97+
```json
98+
{
99+
"eventId": "d9ec98b1-410f-40eb-8634-cfe189749da6",
100+
"date": "2021-06-05T12:45:36.698Z",
101+
"title": "An intrusion was detected",
102+
"details": "An intrusion was detected on the server 10.10.43.2",
103+
"data": [
104+
{"kind": "ip", "value": "10.10.43.2", "name": "server-ip" },
105+
{"kind": "name", "value": "root", "name": "login" },
106+
{"kind": "ip", "value": "92.43.123.1", "name": "origin" }
107+
]
108+
}
109+
```
110+
111+
Because this format differs from TheHive's alert schema, the data needs to be transformed into the correct format.
112+
113+
As an org-admin, you can create a new function for your organization to convert this input into TheHive's alert format and generate an alert. The function might look like this:
114+
115+
```javascript
116+
function handle(input, context) {
117+
const theHiveAlert = {
118+
"type": "event",
119+
"source": "my-system",
120+
"sourceRef": input.eventId,
121+
"title": input.title,
122+
"description": input.details,
123+
"date": (new Date(input.date)).getTime(),
124+
"observables": input.data.map(data => {
125+
// map event data kind to TheHive Observable type
126+
const dataType = data.kind === "ip" ? "ip" : "other";
127+
return {
128+
"dataType": dataType,
129+
"data": data.value,
130+
"tags": [`name:${data.name}`] // use a tag for the data name
131+
};
132+
})
133+
};
134+
// call TheHive APIs, here alert creation
135+
return context.alert.create(theHiveAlert);
136+
}
137+
```
138+
139+
Creating and testing this function allows you to effortlessly convert your external event data into a format that TheHive can process as an alert.
140+
141+
---
142+
143+
## Example 2: Creating an Alert Based on a Splunk Alert
144+
145+
When setting up a Splunk alert, you can configure a [webhook as an action](https://docs.splunk.com/Documentation/Splunk/9.0.0/Alert/Webhooks). When the alert is triggered, the webhook will be invoked with a predefined payload, which cannot be modified. The payload will resemble something like this:
115146

116147
!!! Example ""
117148

@@ -149,7 +180,7 @@ It should look a bit like:
149180
}
150181
```
151182

152-
To transform this splunk alert into a TheHive alert, a function like this can be used:
183+
To convert this Splunk alert into a TheHive alert, you can use a function like the following:
153184

154185
!!! Example ""
155186

@@ -172,14 +203,16 @@ To transform this splunk alert into a TheHive alert, a function like this can be
172203
}
173204
```
174205

175-
In splunk, you will need to set the webhook url to TheHive function url.
206+
In Splunk, you'll need to configure the webhook URL to point to the TheHive function URL.
176207

177-
## Example: Cold case automation
208+
---
178209

179-
When called, this function will:
210+
## Example 3: Cold Case Automation Process
180211

181-
- find all cases that are `New` or `InProgress` and were not updated in the last month
182-
- to each of those cases, add a tag `cold-case`
212+
When invoked, this function will:
213+
214+
- Identify all cases marked as `New` or `InProgress` that haven't been updated in the past month.
215+
- Add a `cold-case` tag to each of these cases.
183216

184217
!!! Example ""
185218

@@ -214,6 +247,7 @@ When called, this function will:
214247
});
215248
}
216249
```
250+
---
217251

218252
## Context API
219253

@@ -412,3 +446,5 @@ When called, this function will:
412446
- `function.update(functionIdOrName: string, update: InputUpdateFunction): void`
413447
- `function.delete(functionIdOrName: string): void`
414448
- `function.find(query: any[]): OutputFunction`
449+
450+
&nbsp;

0 commit comments

Comments
 (0)