Skip to content

Commit

Permalink
removed package name from content names, updated deprecated ioutil co…
Browse files Browse the repository at this point in the history
…de (#64)
  • Loading branch information
its-me-debk007 authored Sep 6, 2023
1 parent 1afd225 commit e5f196e
Show file tree
Hide file tree
Showing 18 changed files with 135 additions and 130 deletions.
6 changes: 5 additions & 1 deletion constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,15 @@ const (
)

// These represent me endpoints URL

const (
BASE_ME_URL = "/api/me"
USER_DETAILS_URL = BASE_ME_URL + "/access"
ORGANIZATION_URL = BASE_ME_URL + "/organization"
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"
)
6 changes: 3 additions & 3 deletions examples/basicExample/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -26,7 +26,7 @@ func main() {
}

// Making the client!
client := gointelowl.NewIntelOwlClient(
client := gointelowl.NewClient(
&clientOptions,
nil,
loggerParams,
Expand Down
12 changes: 6 additions & 6 deletions examples/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -34,7 +34,7 @@ func main() {
}

// Making the client!
client := gointelowl.NewIntelOwlClient(
client := gointelowl.NewClient(
&clientOptions,
nil,
loggerParams,
Expand Down
8 changes: 4 additions & 4 deletions examples/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -25,7 +25,7 @@ func main() {
}

// Making the client!
client := gointelowl.NewIntelOwlClient(
client := gointelowl.NewClient(
&clientOptions,
nil,
loggerParams,
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions examples/optionalParams/optionalParams.go
Original file line number Diff line number Diff line change
Expand Up @@ -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: "",
Expand All @@ -28,7 +28,7 @@ func main() {
}

// Making the client!
client := gointelowl.NewIntelOwlClient(
client := gointelowl.NewClient(
&clientOptions,
nil,
loggerParams,
Expand Down
8 changes: 4 additions & 4 deletions gointelowl/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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{}
Expand Down
13 changes: 7 additions & 6 deletions gointelowl/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"net/http"
"sort"

"github.com/intelowlproject/go-intelowl/constants"
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
55 changes: 27 additions & 28 deletions gointelowl/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,29 @@ 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
}

// 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,
Expand All @@ -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!
Expand All @@ -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.
Expand Down Expand Up @@ -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())
}

Expand All @@ -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

Expand All @@ -148,7 +147,7 @@ func NewIntelOwlClient(options *IntelOwlClientOptions, httpClient *http.Client,
}

// configuring the client
client := IntelOwlClient{
client := Client{
options: options,
client: httpClient,
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
}

Expand Down
Loading

0 comments on commit e5f196e

Please sign in to comment.