Skip to content

Commit

Permalink
Improving constants (#59)
Browse files Browse the repository at this point in the history
* revamping the constants and adding them into the unit tests as well integrating them into the me endpoints

* Fixing spelling mistake in README

* Fixing the README

* Fixing an example
  • Loading branch information
fear-the-reaper authored Sep 5, 2022
1 parent 598ed60 commit c946177
Show file tree
Hide file tree
Showing 15 changed files with 140 additions and 100 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ go-intelowl is a client library/SDK that allows developers to easily automate an
- [Usage](#usage)
- [Examples](#examples)
- [Contribute](#contribute)
- [Liscence](#liscence)
- [License](#liscence)
- [Links](#links)
- [FAQ](#faq)
- [Generate API key](#generate-api-key)
Expand Down Expand Up @@ -95,8 +95,8 @@ func main(){
intelowlOptions := gointelowl.IntelOwlClientOptions{
Url: "your-cool-url-goes-here",
Token: "your-super-secret-token-goes-here",
}
Certificate: "your-optional-certificate-goes-here",
}

client := gointelowl.NewIntelOwlClient(
&intelowlOptions,
Expand All @@ -120,14 +120,16 @@ func main(){
For complete usage of go-intelowl, see the full [package docs](https://pkg.go.dev/github.com/intelowlproject/go-intelowl).

# Contribute
See our [contributor page]() for details how to contribute. If you want to follow the updates, discuss, or just chat then please join our [slack](https://honeynetpublic.slack.com/archives/C01KVGMAKL6) channel we'd love to hear your feedback!
If you want to follow the updates, discuss, contribute, or just chat then please join our [slack](https://honeynetpublic.slack.com/archives/C01KVGMAKL6) channel we'd love to hear your feedback!

# Liscence
# License
Licensed under the GNU AFFERO GENERAL PUBLIC LICENSE.

# Links
- [Intelowl](https://github.com/intelowlproject/IntelOwl)
- [Documentation]()
- [Documentation](https://intelowl.readthedocs.io/en/latest/)
- [API documentation](https://intelowl.readthedocs.io/en/latest/Redoc.html)
- [Examples](./examples/)

# FAQ
## Generate API key
Expand Down
49 changes: 49 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package constants

// These represent tag endpoints URL
const (
BASE_TAG_URL = "/api/tags"
SPECIFIC_TAG_URL = BASE_TAG_URL + "/%d"
)

// These represent job endpoints URL
const (
BASE_JOB_URL = "/api/jobs"
SPECIFIC_JOB_URL = BASE_JOB_URL + "/%d"
DOWNLOAD_SAMPLE_JOB_URL = SPECIFIC_JOB_URL + "/download_sample"
KILL_JOB_URL = SPECIFIC_JOB_URL + "/kill"
KILL_ANALYZER_JOB_URL = SPECIFIC_JOB_URL + "/analyzer/%s/kill"
RETRY_ANALYZER_JOB_URL = SPECIFIC_JOB_URL + "/analyzer/%s/retry"
KILL_CONNECTOR_JOB_URL = SPECIFIC_JOB_URL + "/connector/%s/kill"
RETRY_CONNECTOR_JOB_URL = SPECIFIC_JOB_URL + "/connector/%s/retry"
)

// These represent analyzer endpoints URL
const (
ANALYZER_CONFIG_URL = "/api/get_analyzer_configs"
ANALYZER_HEALTHCHECK_URL = "/api/analyzer/%s/healthcheck"
)

// These represent connector endpoints URL
const (
CONNECTOR_CONFIG_URL = "/api/get_connector_configs"
CONNECTOR_HEALTHCHECK_URL = "/api/connector/%s/healthcheck"
)

// These represent analyze endpoints URL
const (
ANALYZE_OBSERVABLE_URL = "/api/analyze_observable"
ANALYZE_MULTIPLE_OBSERVABLES_URL = "/api/analyze_multiple_observables"
ANALYZE_FILE_URL = "/api/analyze_file"
ANALYZE_MULTIPLE_FILES_URL = "/api/analyze_multiple_files"
)

// 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"
)
12 changes: 6 additions & 6 deletions gointelowl/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"

"github.com/intelowlproject/go-intelowl/constants"
)

// BasicAnalysisParams represents the common fields in an Observable and a File analysis
Expand Down Expand Up @@ -67,7 +68,7 @@ type MultipleAnalysisResponse struct {
//
// 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) {
requestUrl := fmt.Sprintf(ANALYZE_OBSERVABLE_URL, client.options.Url)
requestUrl := client.options.Url + constants.ANALYZE_OBSERVABLE_URL
method := "POST"
contentType := "application/json"
jsonData, _ := json.Marshal(params)
Expand Down Expand Up @@ -96,8 +97,7 @@ func (client *IntelOwlClient) CreateObservableAnalysis(ctx context.Context, para
//
// 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) {
requestUrl := fmt.Sprintf(ANALYZE_MULTIPLE_OBSERVABLES_URL, client.options.Url)

requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_OBSERVABLES_URL
method := "POST"
contentType := "application/json"
jsonData, _ := json.Marshal(params)
Expand Down Expand Up @@ -125,7 +125,7 @@ func (client *IntelOwlClient) CreateMultipleObservableAnalysis(ctx context.Conte
//
// 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) {
requestUrl := fmt.Sprintf(ANALYZE_FILE_URL, client.options.Url)
requestUrl := client.options.Url + constants.ANALYZE_FILE_URL
// * Making the multiform data
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
Expand Down Expand Up @@ -203,7 +203,7 @@ func (client *IntelOwlClient) CreateFileAnalysis(ctx context.Context, fileAnalys
//
// 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) {
requestUrl := fmt.Sprintf(ANALYZE_MULTIPLE_FILES_URL, client.options.Url)
requestUrl := client.options.Url + constants.ANALYZE_MULTIPLE_FILES_URL
// * Making the multiform data
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
Expand Down
7 changes: 5 additions & 2 deletions gointelowl/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"sort"

"github.com/intelowlproject/go-intelowl/constants"
)

// AnalyzerConfig represents how an analyzer is configured in IntelOwl.
Expand Down Expand Up @@ -36,7 +38,7 @@ 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 := fmt.Sprintf(ANALYZER_CONFIG_URL, analyzerService.client.options.Url)
requestUrl := analyzerService.client.options.Url + constants.ANALYZER_CONFIG_URL
contentType := "application/json"
method := "GET"
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand Down Expand Up @@ -74,7 +76,8 @@ func (analyzerService *AnalyzerService) GetConfigs(ctx context.Context) (*[]Anal
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/analyzer/operation/analyzer_healthcheck_retrieve
func (analyzerService *AnalyzerService) HealthCheck(ctx context.Context, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf(ANALYZER_HEALTHCHECK_URL, analyzerService.client.options.Url, analyzerName)
route := analyzerService.client.options.Url + constants.ANALYZER_HEALTHCHECK_URL
requestUrl := fmt.Sprintf(route, analyzerName)
contentType := "application/json"
method := "GET"
request, err := analyzerService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand Down
7 changes: 5 additions & 2 deletions gointelowl/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"
"fmt"
"sort"

"github.com/intelowlproject/go-intelowl/constants"
)

// ConnectorConfig represents how a connector is configured in IntelOwl.
Expand All @@ -28,7 +30,7 @@ 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 := fmt.Sprintf(CONNECTOR_CONFIG_URL, connectorService.client.options.Url)
requestUrl := connectorService.client.options.Url + constants.CONNECTOR_CONFIG_URL
contentType := "application/json"
method := "GET"
request, err := connectorService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand Down Expand Up @@ -66,7 +68,8 @@ func (connectorService *ConnectorService) GetConfigs(ctx context.Context) (*[]Co
//
// 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) {
requestUrl := fmt.Sprintf(CONNECTOR_HEALTHCHECK_URL, connectorService.client.options.Url, connectorName)
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)
Expand Down
39 changes: 0 additions & 39 deletions gointelowl/constants.go

This file was deleted.

28 changes: 19 additions & 9 deletions gointelowl/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"net/http"
"time"

"github.com/intelowlproject/go-intelowl/constants"
)

// UserDetails represents user details in an IntelOwl job.
Expand Down Expand Up @@ -81,7 +83,7 @@ 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 := fmt.Sprintf(BASE_JOB_URL, jobService.client.options.Url)
requestUrl := jobService.client.options.Url + constants.BASE_JOB_URL
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -107,7 +109,8 @@ func (jobService *JobService) List(ctx context.Context) (*JobListResponse, error
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_retrieve
func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, error) {
requestUrl := fmt.Sprintf(SPECIFIC_JOB_URL, jobService.client.options.Url, jobId)
route := jobService.client.options.Url + constants.SPECIFIC_JOB_URL
requestUrl := fmt.Sprintf(route, jobId)
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -132,7 +135,8 @@ func (jobService *JobService) Get(ctx context.Context, jobId uint64) (*Job, erro
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_download_sample_retrieve
func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64) ([]byte, error) {
requestUrl := fmt.Sprintf(DOWNLOAD_SAMPLE_JOB_URL, jobService.client.options.Url, jobId)
route := jobService.client.options.Url + constants.DOWNLOAD_SAMPLE_JOB_URL
requestUrl := fmt.Sprintf(route, jobId)
contentType := "application/json"
method := "GET"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -152,7 +156,8 @@ func (jobService *JobService) DownloadSample(ctx context.Context, jobId uint64)
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_destroy
func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, error) {
requestUrl := fmt.Sprintf(SPECIFIC_JOB_URL, jobService.client.options.Url, jobId)
route := jobService.client.options.Url + constants.SPECIFIC_JOB_URL
requestUrl := fmt.Sprintf(route, jobId)
contentType := "application/json"
method := "DELETE"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -175,7 +180,8 @@ func (jobService *JobService) Delete(ctx context.Context, jobId uint64) (bool, e
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_kill_partial_update
func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, error) {
requestUrl := fmt.Sprintf(KILL_JOB_URL, jobService.client.options.Url, jobId)
route := jobService.client.options.Url + constants.KILL_JOB_URL
requestUrl := fmt.Sprintf(route, jobId)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -198,7 +204,8 @@ func (jobService *JobService) Kill(ctx context.Context, jobId uint64) (bool, err
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_analyzer_kill_partial_update
func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf(KILL_ANALYZER_JOB_URL, jobService.client.options.Url, jobId, analyzerName)
route := jobService.client.options.Url + constants.KILL_ANALYZER_JOB_URL
requestUrl := fmt.Sprintf(route, jobId, analyzerName)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -221,7 +228,8 @@ func (jobService *JobService) KillAnalyzer(ctx context.Context, jobId uint64, an
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_analyzer_retry_partial_update
func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, analyzerName string) (bool, error) {
requestUrl := fmt.Sprintf(RETRY_ANALYZER_JOB_URL, jobService.client.options.Url, jobId, analyzerName)
route := jobService.client.options.Url + constants.RETRY_ANALYZER_JOB_URL
requestUrl := fmt.Sprintf(route, jobId, analyzerName)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -244,7 +252,8 @@ func (jobService *JobService) RetryAnalyzer(ctx context.Context, jobId uint64, a
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_connector_kill_partial_update
func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) {
requestUrl := fmt.Sprintf(KILL_CONNECTOR_JOB_URL, jobService.client.options.Url, jobId, connectorName)
route := jobService.client.options.Url + constants.KILL_CONNECTOR_JOB_URL
requestUrl := fmt.Sprintf(route, jobId, connectorName)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -267,7 +276,8 @@ func (jobService *JobService) KillConnector(ctx context.Context, jobId uint64, c
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/jobs/operation/jobs_connector_retry_partial_update
func (jobService *JobService) RetryConnector(ctx context.Context, jobId uint64, connectorName string) (bool, error) {
requestUrl := fmt.Sprintf(RETRY_CONNECTOR_JOB_URL, jobService.client.options.Url, jobId, connectorName)
route := jobService.client.options.Url + constants.RETRY_CONNECTOR_JOB_URL
requestUrl := fmt.Sprintf(route, jobId, connectorName)
contentType := "application/json"
method := "PATCH"
request, err := jobService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand Down
13 changes: 7 additions & 6 deletions gointelowl/me.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"

"github.com/intelowlproject/go-intelowl/constants"
)

type Details struct {
Expand Down Expand Up @@ -75,7 +76,7 @@ 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 := fmt.Sprintf("%s/api/me/access", userService.client.options.Url)
requestUrl := userService.client.options.Url + constants.USER_DETAILS_URL
contentType := "application/json"
method := "GET"
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -99,7 +100,7 @@ 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 := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
requestUrl := userService.client.options.Url + constants.ORGANIZATION_URL
contentType := "application/json"
method := "GET"
request, err := userService.client.buildRequest(ctx, method, contentType, nil, requestUrl)
Expand All @@ -124,7 +125,7 @@ func (userService *UserService) Organization(ctx context.Context) (*Organization
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
func (userService *UserService) CreateOrganization(ctx context.Context, organizationParams *OrganizationParams) (*Organization, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization", userService.client.options.Url)
requestUrl := userService.client.options.Url + constants.ORGANIZATION_URL
// Getting the relevant JSON data
orgJson, err := json.Marshal(organizationParams)
if err != nil {
Expand Down Expand Up @@ -156,7 +157,7 @@ func (userService *UserService) CreateOrganization(ctx context.Context, organiza
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_invite_create
func (userService *UserService) InviteToOrganization(ctx context.Context, memberParams *MemberParams) (*Invite, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization/invite", userService.client.options.Url)
requestUrl := userService.client.options.Url + constants.INVITE_TO_ORGANIZATION_URL
// Getting the relevant JSON data
memberJson, err := json.Marshal(memberParams)
if err != nil {
Expand Down Expand Up @@ -188,7 +189,7 @@ func (userService *UserService) InviteToOrganization(ctx context.Context, member
//
// IntelOwl REST API docs: https://intelowl.readthedocs.io/en/latest/Redoc.html#tag/me/operation/me_organization_create
func (userService *UserService) RemoveMemberFromOrganization(ctx context.Context, memberParams *MemberParams) (bool, error) {
requestUrl := fmt.Sprintf("%s/api/me/organization/remove_member", userService.client.options.Url)
requestUrl := userService.client.options.Url + constants.REMOVE_MEMBER_FROM_ORGANIZATION_URL
// Getting the relevant JSON data
memberJson, err := json.Marshal(memberParams)
if err != nil {
Expand Down
Loading

0 comments on commit c946177

Please sign in to comment.