/
connector.go
88 lines (80 loc) · 3.05 KB
/
connector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package gointelowl
import (
"context"
"encoding/json"
"fmt"
"sort"
"github.com/intelowlproject/go-intelowl/constants"
)
// ConnectorConfig represents how a connector is configured in IntelOwl.
//
// IntelOwl docs: https://intelowl.readthedocs.io/en/latest/Usage.html#connectors-customization
type ConnectorConfig struct {
BaseConfigurationType
MaximumTlp TLP `json:"maximum_tlp"`
}
// ConnectorService handles communication with connector related methods of the IntelOwl API.
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/connector
type ConnectorService struct {
client *IntelOwlClient
}
// GetConfigs lists down every connector configuration in your IntelOwl instance.
//
// Endpoint: GET /api/get_connector_configs
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/get_connector_configs
func (connectorService *ConnectorService) GetConfigs(ctx context.Context) (*[]ConnectorConfig, error) {
requestUrl := connectorService.client.options.Url + constants.CONNECTOR_CONFIG_URL
contentType := "application/json"
method := "GET"
request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return nil, err
}
successResp, err := connectorService.client.newRequest(ctx, request)
if err != nil {
return nil, err
}
connectorConfigurationResponse := map[string]ConnectorConfig{}
if unmarshalError := json.Unmarshal(successResp.Data, &connectorConfigurationResponse); unmarshalError != nil {
return nil, unmarshalError
}
connectorNames := make([]string, 0)
// *getting all the analyzer key names!
for connectorName := range connectorConfigurationResponse {
connectorNames = append(connectorNames, connectorName)
}
// * sorting them alphabetically
sort.Strings(connectorNames)
connectorConfigurationList := []ConnectorConfig{}
for _, connectorName := range connectorNames {
connectorConfig := connectorConfigurationResponse[connectorName]
connectorConfigurationList = append(connectorConfigurationList, connectorConfig)
}
return &connectorConfigurationList, nil
}
// HealthCheck checks if the specified connector is up and running
//
// Endpoint: GET /api/connector/{NameOfConnector}/healthcheck
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/connector/operation/connector_healthcheck_retrieve
func (connectorService *ConnectorService) HealthCheck(ctx context.Context, connectorName string) (bool, error) {
route := connectorService.client.options.Url + constants.CONNECTOR_HEALTHCHECK_URL
requestUrl := fmt.Sprintf(route, connectorName)
contentType := "application/json"
method := "GET"
request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
if err != nil {
return false, err
}
status := StatusResponse{}
successResp, err := connectorService.client.newRequest(ctx, request)
if err != nil {
return false, err
}
if unmarshalError := json.Unmarshal(successResp.Data, &status); unmarshalError != nil {
return false, unmarshalError
}
return status.Status, nil
}