diff --git a/constants/constants.go b/constants/constants.go index e207067..36dc93f 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -39,7 +39,6 @@ const ( ) // These represent me endpoints URL - const ( BASE_ME_URL = "/api/me" USER_DETAILS_URL = BASE_ME_URL + "/access" @@ -47,3 +46,8 @@ const ( INVITE_TO_ORGANIZATION_URL = ORGANIZATION_URL + "/invite" REMOVE_MEMBER_FROM_ORGANIZATION_URL = ORGANIZATION_URL + "/remove_member" ) + +// These represent REST API related constants +const ( + ContentTypeJSON = "application/json" +) diff --git a/examples/basicExample/example.go b/examples/basicExample/example.go index 7b645cc..287d3b7 100644 --- a/examples/basicExample/example.go +++ b/examples/basicExample/example.go @@ -11,8 +11,8 @@ import ( func main() { - // Configuring the IntelOwlClient! - clientOptions := gointelowl.IntelOwlClientOptions{ + // Configuring the Client! + clientOptions := gointelowl.ClientOptions{ Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE", Token: "PUT-YOUR-TOKEN-HERE", Certificate: "", @@ -26,7 +26,7 @@ func main() { } // Making the client! - client := gointelowl.NewIntelOwlClient( + client := gointelowl.NewClient( &clientOptions, nil, loggerParams, diff --git a/examples/client/client.go b/examples/client/client.go index 2e8e5c6..1d05710 100644 --- a/examples/client/client.go +++ b/examples/client/client.go @@ -9,17 +9,17 @@ import ( func main() { /* - Making a new client through NewIntelOwlClient: + Making a new client through NewClient: This takes the following parameters: - 1. IntelOwlClientOptions + 1. ClientOptions 2. A *http.Client (if you do not provide one. One will be made by default) 3. LoggerParams - These are parameters that allow you to easily configure your IntelOwlClient to your liking. + These are parameters that allow you to easily configure your Client to your liking. For a better understanding you can read it in the documentation: https://github.com/intelowlproject/go-intelowl/tree/main/examples/optionalParams */ - // Configuring the IntelOwlClient! - clientOptions := gointelowl.IntelOwlClientOptions{ + // Configuring the Client! + clientOptions := gointelowl.ClientOptions{ Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE", Token: "PUT-YOUR-TOKEN-HERE", Certificate: "", @@ -34,7 +34,7 @@ func main() { } // Making the client! - client := gointelowl.NewIntelOwlClient( + client := gointelowl.NewClient( &clientOptions, nil, loggerParams, diff --git a/examples/endpoints/endpoints.go b/examples/endpoints/endpoints.go index fcdddb9..5573e6f 100644 --- a/examples/endpoints/endpoints.go +++ b/examples/endpoints/endpoints.go @@ -11,8 +11,8 @@ import ( func main() { - // Configuring the IntelOwlClient! - clientOptions := gointelowl.IntelOwlClientOptions{ + // Configuring the Client! + clientOptions := gointelowl.ClientOptions{ Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE", Token: "PUT-YOUR-TOKEN-HERE", Certificate: "", @@ -25,7 +25,7 @@ func main() { } // Making the client! - client := gointelowl.NewIntelOwlClient( + client := gointelowl.NewClient( &clientOptions, nil, loggerParams, @@ -42,7 +42,7 @@ func main() { // Getting the tag list! tagList, err := client.TagService.List(ctx) - // checking for any pesky errors if there's any error it'll return an IntelOwlError + // checking for any pesky errors if there's any error it'll return an Error if err != nil { fmt.Println(err) } else { diff --git a/examples/optionalParams/optionalParams.go b/examples/optionalParams/optionalParams.go index 474b740..5c703b3 100644 --- a/examples/optionalParams/optionalParams.go +++ b/examples/optionalParams/optionalParams.go @@ -14,8 +14,8 @@ For this example I'll be using the tag params! */ func main() { - // Configuring the IntelOwlClient! - clientOptions := gointelowl.IntelOwlClientOptions{ + // Configuring the Client! + clientOptions := gointelowl.ClientOptions{ Url: "PUT-YOUR-INTELOWL-INSTANCE-URL-HERE", Token: "PUT-YOUR-TOKEN-HERE", Certificate: "", @@ -28,7 +28,7 @@ func main() { } // Making the client! - client := gointelowl.NewIntelOwlClient( + client := gointelowl.NewClient( &clientOptions, nil, loggerParams, diff --git a/gointelowl/analysis.go b/gointelowl/analysis.go index af266bd..ed4a892 100644 --- a/gointelowl/analysis.go +++ b/gointelowl/analysis.go @@ -67,7 +67,7 @@ type MultipleAnalysisResponse struct { // Endpoint: POST /api/analyze_observable // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_observable -func (client *IntelOwlClient) CreateObservableAnalysis(ctx context.Context, params *ObservableAnalysisParams) (*AnalysisResponse, error) { +func (client *Client) CreateObservableAnalysis(ctx context.Context, params *ObservableAnalysisParams) (*AnalysisResponse, error) { requestUrl := client.options.Url + constants.ANALYZE_OBSERVABLE_URL method := "POST" contentType := "application/json" @@ -96,7 +96,7 @@ func (client *IntelOwlClient) CreateObservableAnalysis(ctx context.Context, para // Endpoint: POST /api/analyze_multiple_observables // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_multiple_observables -func (client *IntelOwlClient) CreateMultipleObservableAnalysis(ctx context.Context, params *MultipleObservableAnalysisParams) (*MultipleAnalysisResponse, error) { +func (client *Client) CreateMultipleObservableAnalysis(ctx context.Context, params *MultipleObservableAnalysisParams) (*MultipleAnalysisResponse, error) { requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_OBSERVABLES_URL method := "POST" contentType := "application/json" @@ -124,7 +124,7 @@ func (client *IntelOwlClient) CreateMultipleObservableAnalysis(ctx context.Conte // Endpoint: POST /api/analyze_file // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_file -func (client *IntelOwlClient) CreateFileAnalysis(ctx context.Context, fileAnalysisParams *FileAnalysisParams) (*AnalysisResponse, error) { +func (client *Client) CreateFileAnalysis(ctx context.Context, fileAnalysisParams *FileAnalysisParams) (*AnalysisResponse, error) { requestUrl := client.options.Url + constants.ANALYZE_FILE_URL // * Making the multiform data body := &bytes.Buffer{} @@ -202,7 +202,7 @@ func (client *IntelOwlClient) CreateFileAnalysis(ctx context.Context, fileAnalys // Endpoint: POST /api/analyze_mutliple_files // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyze_multiple_files -func (client *IntelOwlClient) CreateMultipleFileAnalysis(ctx context.Context, fileAnalysisParams *MultipleFileAnalysisParams) (*MultipleAnalysisResponse, error) { +func (client *Client) CreateMultipleFileAnalysis(ctx context.Context, fileAnalysisParams *MultipleFileAnalysisParams) (*MultipleAnalysisResponse, error) { requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_FILES_URL // * Making the multiform data body := &bytes.Buffer{} diff --git a/gointelowl/analyzer.go b/gointelowl/analyzer.go index 041fd25..6233354 100644 --- a/gointelowl/analyzer.go +++ b/gointelowl/analyzer.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "net/http" "sort" "github.com/intelowlproject/go-intelowl/constants" @@ -29,7 +30,7 @@ type AnalyzerConfig struct { // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyzer type AnalyzerService struct { - client *IntelOwlClient + client *Client } // GetConfigs lists down every analyzer configuration in your IntelOwl instance. @@ -39,8 +40,8 @@ type AnalyzerService struct { // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/get_analyzer_configs func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]AnalyzerConfig, error) { requestUrl := analyzerService.client.options.Url + constants.ANALYZER_CONFIG_URL - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -62,7 +63,7 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal } // * sorting them alphabetically sort.Strings(analyzerNames) - analyzerConfigurationList := []AnalyzerConfig{} + var analyzerConfigurationList []AnalyzerConfig for _, analyzerName := range analyzerNames { analyzerConfig := analyzerConfigurationResponse[analyzerName] analyzerConfigurationList = append(analyzerConfigurationList, analyzerConfig) @@ -78,8 +79,8 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal func (analyzerService *AnalyzerService) HealthCheck(ctx context.Context, analyzerName string) (bool, error) { route := analyzerService.client.options.Url + constants.ANALYZER_HEALTHCHECK_URL requestUrl := fmt.Sprintf(route, analyzerName) - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err diff --git a/gointelowl/client.go b/gointelowl/client.go index ca03d2e..8ecb7b1 100644 --- a/gointelowl/client.go +++ b/gointelowl/client.go @@ -9,15 +9,14 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "net/http" "os" "strings" "time" ) -// IntelOwlError represents an error that has occurred when communicating with IntelOwl. -type IntelOwlError struct { +// Error represents an error that has occurred when communicating with IntelOwl. +type Error struct { StatusCode int Message string Response *http.Response @@ -25,14 +24,14 @@ type IntelOwlError struct { // Error lets you implement the error interface. // This is used for making custom go errors. -func (intelOwlError *IntelOwlError) Error() string { +func (intelOwlError *Error) Error() string { errorMessage := fmt.Sprintf("Status Code: %d \n Error: %s", intelOwlError.StatusCode, intelOwlError.Message) return errorMessage } -// newIntelOwlError lets you easily create new IntelOwlErrors. -func newIntelOwlError(statusCode int, message string, response *http.Response) *IntelOwlError { - return &IntelOwlError{ +// newError lets you easily create new Errors. +func newError(statusCode int, message string, response *http.Response) *Error { + return &Error{ StatusCode: statusCode, Message: message, Response: response, @@ -44,8 +43,8 @@ type successResponse struct { Data []byte } -// IntelOwlClientOptions represents the fields needed to configure and use the IntelOwlClient -type IntelOwlClientOptions struct { +// ClientOptions represents the fields needed to configure and use the Client +type ClientOptions struct { Url string `json:"url"` Token string `json:"token"` // Certificate represents your SSL cert: path to the cert file! @@ -54,16 +53,16 @@ type IntelOwlClientOptions struct { Timeout uint64 `json:"timeout"` } -// IntelOwlClient handles all the communication with your IntelOwl instance. -type IntelOwlClient struct { - options *IntelOwlClientOptions +// Client handles all the communication with your IntelOwl instance. +type Client struct { + options *ClientOptions client *http.Client TagService *TagService JobService *JobService AnalyzerService *AnalyzerService ConnectorService *ConnectorService UserService *UserService - Logger *IntelOwlLogger + Logger *Logger } // TLP represents an enum for the TLP attribute used in IntelOwl's REST API. @@ -113,7 +112,7 @@ func ParseTLP(s string) TLP { } // Implementing the MarshalJSON interface to make our custom Marshal for the enum -func (tlp TLP) MarshalJSON() ([]byte, error) { +func (tlp *TLP) MarshalJSON() ([]byte, error) { return json.Marshal(tlp.String()) } @@ -129,8 +128,8 @@ func (tlp *TLP) UnmarshalJSON(data []byte) (err error) { return nil } -// NewIntelOwlClient lets you easily create a new IntelOwlClient by providing IntelOwlClientOptions, http.Clients, and LoggerParams. -func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client, loggerParams *LoggerParams) IntelOwlClient { +// NewClient lets you easily create a new Client by providing ClientOptions, http.Clients, and LoggerParams. +func NewClient(options *ClientOptions, httpClient *http.Client, loggerParams *LoggerParams) Client { var timeout time.Duration @@ -148,7 +147,7 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client, } // configuring the client - client := IntelOwlClient{ + client := Client{ options: options, client: httpClient, } @@ -171,33 +170,33 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client, } // configuring the logger! - client.Logger = &IntelOwlLogger{} + client.Logger = &Logger{} client.Logger.Init(loggerParams) return client } -// NewIntelOwlClientThroughJsonFile lets you create a new IntelOwlClient through a JSON file that contains your IntelOwlClientOptions -func NewIntelOwlClientThroughJsonFile(filePath string, httpClient *http.Client, loggerParams *LoggerParams) (*IntelOwlClient, error) { +// NewClientFromJsonFile lets you create a new Client through a JSON file that contains your ClientOptions +func NewClientFromJsonFile(filePath string, httpClient *http.Client, loggerParams *LoggerParams) (*Client, error) { optionsBytes, err := os.ReadFile(filePath) if err != nil { errorMessage := fmt.Sprintf("Could not read %s", filePath) - intelOwlError := newIntelOwlError(400, errorMessage, nil) + intelOwlError := newError(400, errorMessage, nil) return nil, intelOwlError } - intelOwlClientOptions := &IntelOwlClientOptions{} + intelOwlClientOptions := &ClientOptions{} if unmarshalError := json.Unmarshal(optionsBytes, &intelOwlClientOptions); unmarshalError != nil { return nil, unmarshalError } - intelOwlClient := NewIntelOwlClient(intelOwlClientOptions, httpClient, loggerParams) + intelOwlClient := NewClient(intelOwlClientOptions, httpClient, loggerParams) return &intelOwlClient, nil } // buildRequest is used for building requests. -func (client *IntelOwlClient) buildRequest(ctx context.Context, method string, contentType string, body io.Reader, url string) (*http.Request, error) { +func (client *Client) buildRequest(ctx context.Context, method string, contentType string, body io.Reader, url string) (*http.Request, error) { request, err := http.NewRequestWithContext(ctx, method, url, body) if err != nil { return nil, err @@ -211,7 +210,7 @@ func (client *IntelOwlClient) buildRequest(ctx context.Context, method string, c } // newRequest is used for making requests. -func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) { +func (client *Client) newRequest(ctx context.Context, request *http.Request) (*successResponse, error) { response, err := client.client.Do(request) // Checking for context errors such as reaching the deadline and/or Timeout @@ -226,17 +225,17 @@ func (client *IntelOwlClient) newRequest(ctx context.Context, request *http.Requ defer response.Body.Close() - msgBytes, err := ioutil.ReadAll(response.Body) + msgBytes, err := io.ReadAll(response.Body) statusCode := response.StatusCode if err != nil { errorMessage := fmt.Sprintf("Could not convert JSON response. Status code: %d", statusCode) - intelOwlError := newIntelOwlError(statusCode, errorMessage, response) + intelOwlError := newError(statusCode, errorMessage, response) return nil, intelOwlError } if statusCode < http.StatusOK || statusCode >= http.StatusBadRequest { errorMessage := string(msgBytes) - intelOwlError := newIntelOwlError(statusCode, errorMessage, response) + intelOwlError := newError(statusCode, errorMessage, response) return nil, intelOwlError } diff --git a/gointelowl/connector.go b/gointelowl/connector.go index 49ba3fd..43489d2 100644 --- a/gointelowl/connector.go +++ b/gointelowl/connector.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "net/http" "sort" "github.com/intelowlproject/go-intelowl/constants" @@ -21,7 +22,7 @@ type ConnectorConfig struct { // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/connector type ConnectorService struct { - client *IntelOwlClient + client *Client } // GetConfigs lists down every connector configuration in your IntelOwl instance. @@ -31,8 +32,8 @@ type ConnectorService struct { // 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" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -70,8 +71,8 @@ func (connectorService *ConnectorService) GetConfigs(ctx context.Context) (*[]Co 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" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err diff --git a/gointelowl/job.go b/gointelowl/job.go index 8f5c7df..46e636f 100644 --- a/gointelowl/job.go +++ b/gointelowl/job.go @@ -74,7 +74,7 @@ type JobListResponse struct { // // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs type JobService struct { - client *IntelOwlClient + client *Client } // List fetches all the jobs in your IntelOwl instance. @@ -84,8 +84,8 @@ type JobService struct { // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_list func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error) { requestUrl := jobService.client.options.Url + constants.BASE_JOB_URL - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -95,9 +95,9 @@ func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error return nil, err } jobList := JobListResponse{} - marashalError := json.Unmarshal(successResp.Data, &jobList) - if marashalError != nil { - return nil, marashalError + marshalError := json.Unmarshal(successResp.Data, &jobList) + if marshalError != nil { + return nil, marshalError } return &jobList, nil @@ -111,8 +111,8 @@ func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, error) { route := jobService.client.options.Url + constants.SPECIFIC_JOB_URL requestUrl := fmt.Sprintf(route, jobId) - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -137,8 +137,8 @@ func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, erro func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64) ([]byte, error) { route := jobService.client.options.Url + constants.DOWNLOAD_SAMPLE_JOB_URL requestUrl := fmt.Sprintf(route, jobId) - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -158,8 +158,8 @@ func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64) func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, error) { route := jobService.client.options.Url + constants.SPECIFIC_JOB_URL requestUrl := fmt.Sprintf(route, jobId) - contentType := "application/json" - method := "DELETE" + contentType := constants.ContentTypeJSON + method := http.MethodDelete request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err @@ -182,8 +182,8 @@ func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, e func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, error) { route := jobService.client.options.Url + constants.KILL_JOB_URL requestUrl := fmt.Sprintf(route, jobId) - contentType := "application/json" - method := "PATCH" + contentType := constants.ContentTypeJSON + method := http.MethodPatch request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err @@ -206,8 +206,8 @@ func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, err func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) { route := jobService.client.options.Url + constants.KILL_ANALYZER_JOB_URL requestUrl := fmt.Sprintf(route, jobId, analyzerName) - contentType := "application/json" - method := "PATCH" + contentType := constants.ContentTypeJSON + method := http.MethodPatch request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err @@ -230,8 +230,8 @@ func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, an func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) { route := jobService.client.options.Url + constants.RETRY_ANALYZER_JOB_URL requestUrl := fmt.Sprintf(route, jobId, analyzerName) - contentType := "application/json" - method := "PATCH" + contentType := constants.ContentTypeJSON + method := http.MethodPatch request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err @@ -254,8 +254,8 @@ func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, a func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) { route := jobService.client.options.Url + constants.KILL_CONNECTOR_JOB_URL requestUrl := fmt.Sprintf(route, jobId, connectorName) - contentType := "application/json" - method := "PATCH" + contentType := constants.ContentTypeJSON + method := http.MethodPatch request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err @@ -278,8 +278,8 @@ func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, c func (jobService *JobService) RetryConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) { route := jobService.client.options.Url + constants.RETRY_CONNECTOR_JOB_URL requestUrl := fmt.Sprintf(route, jobId, connectorName) - contentType := "application/json" - method := "PATCH" + contentType := constants.ContentTypeJSON + method := http.MethodPatch request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err diff --git a/gointelowl/logger.go b/gointelowl/logger.go index 4d69518..b1dfcc4 100644 --- a/gointelowl/logger.go +++ b/gointelowl/logger.go @@ -14,16 +14,16 @@ type LoggerParams struct { Level logrus.Level } -// IntelOwlLogger represents a logger to be used by the developer. -// IntelOwlLogger implements the Logrus logger. +// Logger represents a logger to be used by the developer. +// Logger implements the Logrus logger. // // Logrus docs: https://github.com/sirupsen/logrus -type IntelOwlLogger struct { +type Logger struct { Logger *logrus.Logger } -// Init initializes the IntelOwlLogger via LoggerParams -func (intelOwlLogger *IntelOwlLogger) Init(loggerParams *LoggerParams) { +// Init initializes the Logger via LoggerParams +func (intelOwlLogger *Logger) Init(loggerParams *LoggerParams) { logger := logrus.New() // Where to log the data! diff --git a/gointelowl/me.go b/gointelowl/me.go index 7c78768..887b645 100644 --- a/gointelowl/me.go +++ b/gointelowl/me.go @@ -29,7 +29,7 @@ type User struct { } type UserService struct { - client *IntelOwlClient + client *Client } type Owner struct { @@ -77,8 +77,8 @@ type InvitationParams struct { // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_access_retrieve func (userService *UserService) Access(ctx context.Context) (*User, error) { requestUrl := userService.client.options.Url + constants.USER_DETAILS_URL - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -101,8 +101,8 @@ func (userService *UserService) Access(ctx context.Context) (*User, error) { // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_list func (userService *UserService) Organization(ctx context.Context) (*Organization, error) { requestUrl := userService.client.options.Url + constants.ORGANIZATION_URL - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -131,8 +131,8 @@ func (userService *UserService) CreateOrganization(ctx context.Context, organiza if err != nil { return nil, err } - contentType := "application/json" - method := "POST" + contentType := constants.ContentTypeJSON + method := http.MethodPost body := bytes.NewBuffer(orgJson) request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) if err != nil { @@ -163,8 +163,8 @@ func (userService *UserService) InviteToOrganization(ctx context.Context, member if err != nil { return nil, err } - contentType := "application/json" - method := "POST" + contentType := constants.ContentTypeJSON + method := http.MethodPost body := bytes.NewBuffer(memberJson) request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) if err != nil { @@ -195,8 +195,8 @@ func (userService *UserService) RemoveMemberFromOrganization(ctx context.Context if err != nil { return false, err } - contentType := "application/json" - method := "POST" + contentType := constants.ContentTypeJSON + method := http.MethodPost body := bytes.NewBuffer(memberJson) request, err := userService.client.buildRequest(ctx, method, contentType, body, requestUrl) if err != nil { diff --git a/gointelowl/tag.go b/gointelowl/tag.go index 2594c26..f84f580 100644 --- a/gointelowl/tag.go +++ b/gointelowl/tag.go @@ -28,7 +28,7 @@ type Tag struct { // // IntelOwl REST API tag docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/tags type TagService struct { - client *IntelOwlClient + client *Client } // checkTagID is used to check if a tag ID is valid (id should be greater than zero). @@ -46,8 +46,8 @@ func checkTagID(id uint64) error { // IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/tags/operation/tags_list func (tagService *TagService) List(ctx context.Context) (*[]Tag, error) { requestUrl := tagService.client.options.Url + constants.BASE_TAG_URL - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -57,9 +57,9 @@ func (tagService *TagService) List(ctx context.Context) (*[]Tag, error) { return nil, err } var tagList []Tag - marashalError := json.Unmarshal(successResp.Data, &tagList) - if marashalError != nil { - return nil, marashalError + marshalError := json.Unmarshal(successResp.Data, &tagList) + if marshalError != nil { + return nil, marshalError } return &tagList, nil @@ -76,8 +76,8 @@ func (tagService *TagService) Get(ctx context.Context, tagId uint64) (*Tag, erro } route := tagService.client.options.Url + constants.SPECIFIC_TAG_URL requestUrl := fmt.Sprintf(route, tagId) - contentType := "application/json" - method := "GET" + contentType := constants.ContentTypeJSON + method := http.MethodGet request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return nil, err @@ -105,8 +105,8 @@ func (tagService *TagService) Create(ctx context.Context, tagParams *TagParams) if err != nil { return nil, err } - contentType := "application/json" - method := "POST" + contentType := constants.ContentTypeJSON + method := http.MethodPost body := bytes.NewBuffer(tagJson) request, err := tagService.client.buildRequest(ctx, method, contentType, body, requestUrl) if err != nil { @@ -137,8 +137,8 @@ func (tagService *TagService) Update(ctx context.Context, tagId uint64, tagParam if err != nil { return nil, err } - contentType := "application/json" - method := "PUT" + contentType := constants.ContentTypeJSON + method := http.MethodPut body := bytes.NewBuffer(tagJson) request, err := tagService.client.buildRequest(ctx, method, contentType, body, requestUrl) if err != nil { @@ -168,8 +168,8 @@ func (tagService *TagService) Delete(ctx context.Context, tagId uint64) (bool, e } route := tagService.client.options.Url + constants.SPECIFIC_TAG_URL requestUrl := fmt.Sprintf(route, tagId) - contentType := "application/json" - method := "DELETE" + contentType := constants.ContentTypeJSON + method := http.MethodDelete request, err := tagService.client.buildRequest(ctx, method, contentType, nil, requestUrl) if err != nil { return false, err diff --git a/tests/analyzerService_test.go b/tests/analyzerService_test.go index 2ce918d..41c39ce 100644 --- a/tests/analyzerService_test.go +++ b/tests/analyzerService_test.go @@ -66,7 +66,7 @@ func TestAnalyzerServiceGetConfigs(t *testing.T) { Input: nil, Data: serverErrorString, StatusCode: http.StatusInternalServerError, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusInternalServerError, Message: serverErrorString, }, @@ -75,7 +75,7 @@ func TestAnalyzerServiceGetConfigs(t *testing.T) { Input: nil, Data: badGatewayErrorString, StatusCode: http.StatusBadGateway, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadGateway, Message: badGatewayErrorString, }, @@ -110,7 +110,7 @@ func TestAnalyzerServiceHealthCheck(t *testing.T) { Input: "notAnAnalyzer", Data: `{"errors": {"detail": "Analyzer doesn't exist"}}`, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: `{"errors": {"detail": "Analyzer doesn't exist"}}`, }, diff --git a/tests/connectorService_test.go b/tests/connectorService_test.go index e6cef8b..dbef446 100644 --- a/tests/connectorService_test.go +++ b/tests/connectorService_test.go @@ -68,7 +68,7 @@ func TestConnectorServiceHealthCheck(t *testing.T) { Input: "notAConnector", Data: `{"errors": {"detail": "Connector doesn't exist"}}`, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: `{"errors": {"detail": "Connector doesn't exist"}}`, }, diff --git a/tests/intelOwl_test.go b/tests/intelOwl_test.go index c9e0d16..b33851d 100644 --- a/tests/intelOwl_test.go +++ b/tests/intelOwl_test.go @@ -21,7 +21,7 @@ type TestData struct { } // Setting up the router, client, and test server -func setup() (testClient gointelowl.IntelOwlClient, apiHandler *http.ServeMux, closeServer func()) { +func setup() (testClient gointelowl.Client, apiHandler *http.ServeMux, closeServer func()) { apiHandler = http.NewServeMux() @@ -47,7 +47,7 @@ func testMethod(t *testing.T, request *http.Request, wantedMethod string) { func testError(t *testing.T, testData TestData, err error) { t.Helper() if testData.StatusCode < http.StatusOK || testData.StatusCode >= http.StatusBadRequest { - diff := cmp.Diff(testData.Want, err, cmpopts.IgnoreFields(gointelowl.IntelOwlError{}, "Response")) + diff := cmp.Diff(testData.Want, err, cmpopts.IgnoreFields(gointelowl.Error{}, "Response")) if diff != "" { t.Fatalf(diff) } @@ -82,9 +82,9 @@ func serverHandler(t *testing.T, testData TestData, expectedMethod string) http. return http.HandlerFunc(handler) } -func NewTestIntelOwlClient(url string) gointelowl.IntelOwlClient { - return gointelowl.NewIntelOwlClient( - &gointelowl.IntelOwlClientOptions{ +func NewTestIntelOwlClient(url string) gointelowl.Client { + return gointelowl.NewClient( + &gointelowl.ClientOptions{ Url: url, Token: "test-token", Certificate: "", diff --git a/tests/jobService_test.go b/tests/jobService_test.go index 9dfcda3..ff09d09 100644 --- a/tests/jobService_test.go +++ b/tests/jobService_test.go @@ -64,7 +64,7 @@ func TestJobServiceGet(t *testing.T) { Input: 9000, Data: `{"detail":"Not found."}`, StatusCode: http.StatusNotFound, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusNotFound, Message: `{"detail":"Not found."}`, }, @@ -108,7 +108,7 @@ func TestJobServiceDownloadSample(t *testing.T) { Input: 2, Data: doesNotHaveASampleResponseJsonString, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: doesNotHaveASampleResponseJsonString, }, @@ -151,7 +151,7 @@ func TestJobServiceDelete(t *testing.T) { Input: 300, Data: notFoundJson, StatusCode: http.StatusNotFound, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusNotFound, Message: notFoundJson, }, @@ -191,7 +191,7 @@ func TestJobServiceKill(t *testing.T) { Input: 300, Data: `{"detail":"Not found."}`, StatusCode: http.StatusNotFound, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusNotFound, Message: `{"detail":"Not found."}`, }, @@ -200,7 +200,7 @@ func TestJobServiceKill(t *testing.T) { Input: 71, Data: `{"errors":{"detail":"Job is not running"}}`, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: `{"errors":{"detail":"Job is not running"}}`, }, @@ -251,7 +251,7 @@ func TestJobServiceKillAnalyzer(t *testing.T) { }, Data: `{"errors":{"analyzer report":"Not found."}}`, StatusCode: http.StatusNotFound, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusNotFound, Message: `{"errors":{"analyzer report":"Not found."}}`, }, @@ -263,7 +263,7 @@ func TestJobServiceKillAnalyzer(t *testing.T) { }, Data: `{"errors":{"detail":"Plugin call is not running or pending"}}`, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: `{"errors":{"detail":"Plugin call is not running or pending"}}`, }, diff --git a/tests/tagService_test.go b/tests/tagService_test.go index 671ac1f..c7c0d82 100644 --- a/tests/tagService_test.go +++ b/tests/tagService_test.go @@ -73,7 +73,7 @@ func TestTagServiceGet(t *testing.T) { Input: 9000, StatusCode: http.StatusNotFound, Data: `{"detail": "Not found."}`, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusNotFound, Message: `{"detail": "Not found."}`, }, @@ -127,7 +127,7 @@ func TestTagServiceCreate(t *testing.T) { }, Data: `{"label":["tag with this label already exists."]}`, StatusCode: http.StatusBadRequest, - Want: &gointelowl.IntelOwlError{ + Want: &gointelowl.Error{ StatusCode: http.StatusBadRequest, Message: `{"label":["tag with this label already exists."]}`, },