Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add searchPulses functionality, Add new search examples, and refactor (pulses.go) into (pulses.go, indicators.go, searchpulses.go, threatintelfeeds.go) #9

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions examples/main.go → examples/getPulseDetail/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ package main

import (
"fmt"
"otxapi"
"os"
"otxapi"
)

func main() {
// you can set your api key environment variable however you prefer..Including inline!

os.Setenv("X_OTX_API_KEY", "mysecretkey")
os.Setenv("X_OTX_API_KEY", "myotxapikey")
fmt.Println("Found API Key in environment var X_OTX_API_KEY! Validating key...")
fmt.Println(fmt.Sprintf("%s", os.Getenv("X_OTX_API_KEY")))
client := otxapi.NewClient(nil)
Expand Down
85 changes: 85 additions & 0 deletions examples/searchAllPulses/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2015 The go-otxapi AUTHORS. All rights reserved.
//
// Use of this source code is governed by an Apache
// license that can be found in the LICENSE file.
package main

import (
"flag"
"fmt"
"os"
"otxapi"
)

func main() {
os.Setenv("X_OTX_API_KEY", "myapikey")
client := otxapi.NewClient(nil)

searchString := flag.String("q", "", "Search string")
flag.Parse()

opt := &otxapi.ListOptions{Query: *searchString}

pulseCh := make(chan otxapi.Pulse, 10)
responseCh := make(chan otxapi.Response, 10)
doneCh := make(chan bool)

go func() {
err := SearchAllPulses(client, opt, pulseCh, responseCh)
if err != nil {
fmt.Println(err)
close(pulseCh)
close(responseCh)
}
}()

go func() {
for result := range pulseCh {
fmt.Printf("%v : %v\n", *result.ID, *result.Name)
}
doneCh <- true
}()

<-doneCh
}

func SearchAllPulses(c *otxapi.Client, opt *otxapi.ListOptions, pulseCh chan otxapi.Pulse, responseCh chan otxapi.Response) error {
if opt.PerPage == 0 {
opt.PerPage = 5
}
opt.Page = 1
pulseSearchResponse, resp, err := c.SearchPulses.Search(opt)
if err != nil {
return err
}

for _, result := range pulseSearchResponse.Results {
pulseCh <- result
}
responseCh <- resp

var pages int

if *pulseSearchResponse.Count > 0 {
pages = *pulseSearchResponse.Count / opt.PerPage
} else {
pages = 1
}

for opt.Page = 2; opt.Page <= pages; opt.Page++ {
pulseSearchResponse, resp, err = c.SearchPulses.Search(opt)
if err != nil {
return err
}

for _, result := range pulseSearchResponse.Results {
pulseCh <- result
}
responseCh <- resp
}

close(pulseCh)
close(responseCh)

return err
}
30 changes: 30 additions & 0 deletions examples/searchPulses/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2015 The go-otxapi AUTHORS. All rights reserved.
//
// Use of this source code is governed by an Apache
// license that can be found in the LICENSE file.
package main

import (
"flag"
"fmt"
"os"
"otxapi"
)

func main() {
os.Setenv("X_OTX_API_KEY", "myotxapikey")
client := otxapi.NewClient(nil)

searchString := flag.String("q", "", "Search string")
flag.Parse()

opt := &otxapi.ListOptions{Query: *searchString}
pulseSearchResponse, _, _ := client.SearchPulses.Search(opt)
fmt.Printf("Number of results: %d\n\n", *pulseSearchResponse.Count)
for _, result := range pulseSearchResponse.Results {
fmt.Printf("%s - %s\n", *result.ID, *result.Name)
for _, indicator := range result.Indicators {
fmt.Printf(" %s = %s\n", *indicator.Type, *indicator.Indicator)
}
}
}
18 changes: 18 additions & 0 deletions src/otxapi/indicators.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package otxapi

type OTXIndicatorService struct {
client *Client
}

type Indicator struct {
ID *string `json:"_id"`
Indicator *string `json:"indicator"`
Type *string `json:"type"`
Description *string `json:"description,omitempty"`
Content *string `json:"content,omitempty"`
Title *string `json:"title,omitempty"`
}

func (r Indicator) String() string {
return Stringify(r)
}
66 changes: 51 additions & 15 deletions src/otxapi/otxapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import (

const (
get = "GET"
post = "POST"
libraryVersion = "0.1"
userAgent = "go-otx-api/" + libraryVersion
defaultBaseURL = "https://otx.alienvault.com/"
subscriptionsURLPath = "api/v1/pulses/subscribed"
pulseDetailURLPath = "api/v1/pulses/"
pulseURLPath = "api/v1/pulses/"
userURLPath = "api/v1/user/"
searchPulsesURLPath = "api/v1/search/pulses"
apiVersion = "v1"
)

Expand All @@ -36,9 +38,10 @@ type Client struct {
UserAgent string

// OTX API Services
UserDetail *OTXUserDetailService
PulseDetail *OTXPulseDetailService
ThreatIntel *OTXThreatIntelFeedService
UserDetail *OTXUserDetailService
PulseDetail *OTXPulseDetailService
ThreatIntel *OTXThreatIntelFeedService
SearchPulses *OTXSearchPulsesSearvice
}

// Response is a otx API response. This wraps the standard http.Response
Expand All @@ -60,6 +63,13 @@ type ListOptions struct {

// For paginated result sets, the number of results to include per page.
PerPage int `url:"limit,omitempty"`

// For search/pulses sort order
// Order by one of these fields: modified, created, subscriber_count
Sort string `url:"sort,omitempty"`

// Query string for search/pulses
Query string `url:"q,omitempty"`
}

// addOptions adds the parameters in opt as URL query parameters to s. opt
Expand All @@ -84,10 +94,10 @@ func addOptions(s string, opt interface{}) (string, error) {
return u.String(), nil
}

func (c *OTXPulseDetailService) Get(id_string string) (PulseDetail, Response, error) {
func (c *OTXPulseDetailService) Get(id_string string) (Pulse, Response, error) {
client := &http.Client{}

req, _ := http.NewRequest(get, fmt.Sprintf("%s/%s/%s/", defaultBaseURL, pulseDetailURLPath, id_string), nil)
req, _ := http.NewRequest(get, fmt.Sprintf("%s/%s/%s/", defaultBaseURL, pulseURLPath, id_string), nil)
req.Header.Set("X-OTX-API-KEY", fmt.Sprintf("%s", os.Getenv("X_OTX_API_KEY")))

response, _ := client.Do(req)
Expand All @@ -98,7 +108,7 @@ func (c *OTXPulseDetailService) Get(id_string string) (PulseDetail, Response, er
fmt.Printf("%s", err)
os.Exit(1)
}
pulse_detail := new(PulseDetail)
pulse_detail := new(Pulse)
json.Unmarshal(contents, &(pulse_detail))
json.Unmarshal(contents, &(resp.Content))

Expand All @@ -107,7 +117,7 @@ func (c *OTXPulseDetailService) Get(id_string string) (PulseDetail, Response, er

func (c *OTXThreatIntelFeedService) List(opt *ListOptions) (ThreatIntelFeed, Response, error) {
client := &http.Client{}
requestpath, err := addOptions(defaultBaseURL + subscriptionsURLPath, opt)
requestpath, err := addOptions(defaultBaseURL+subscriptionsURLPath, opt)
if err != nil {
return ThreatIntelFeed{}, Response{}, err
}
Expand Down Expand Up @@ -153,6 +163,32 @@ func (c *OTXUserDetailService) Get() (UserDetail, *Response, error) {
return *userdetail, resp, err
}

func (c *OTXSearchPulsesSearvice) Search(opt *ListOptions) (PulseSearchResponse, Response, error) {
client := &http.Client{}
requestpath, err := addOptions(defaultBaseURL+searchPulsesURLPath, opt)

req, _ := http.NewRequest(get, requestpath, nil)
req.Header.Set("X-OTX-API-KEY", fmt.Sprintf("%s", os.Getenv("X_OTX_API_KEY")))

response, _ := client.Do(req)
resp := Response{Response: response}

contents, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("%s", err)
os.Exit(1)
}
pulseSearchResponse := new(PulseSearchResponse)
err = json.Unmarshal(contents, &(pulseSearchResponse))
json.Unmarshal(contents, &(resp.Content))
if err != nil {
fmt.Println("error not nil on json unmarshall")
fmt.Println(err)
}

return *pulseSearchResponse, resp, err
}

// NewClient returns a new OTX API client. If a nil httpClient is
// provided, http.DefaultClient will be used.
func NewClient(httpClient *http.Client) *Client {
Expand Down Expand Up @@ -182,12 +218,12 @@ func (c *Client) Do(req *http.Request, v interface{}) (*Response, error) {
response := newResponse(resp)

// check response for error
err = CheckResponse(resp)
if err != nil {
// even though there was an error, we still return the response
// in case the caller wants to inspect it further
return response, err
}
err = CheckResponse(resp)
if err != nil {
// even though there was an error, we still return the response
// in case the caller wants to inspect it further
return response, err
}

content, err := ioutil.ReadAll(resp.Body)
if err != nil {
Expand Down Expand Up @@ -255,7 +291,7 @@ func CheckResponse(r *http.Response) error {
}

type Error struct {
Message string
Message string
}

func (e *Error) Error() string {
Expand Down
58 changes: 17 additions & 41 deletions src/otxapi/pulses.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,27 @@
package otxapi



type OTXPulseDetailService struct {
client *Client
}

// Pulse represents an OTX Pulse
type PulseDetail struct {
ID *string `json:"id"`
Author *string `json:"author_name"`
Name *string `json:"name"`
Description *string `json:"description,omitempty"`
CreatedAt *string `json:"created,omitempty"`
ModifiedAt *string `json:"modified"`
References []string `json:"references,omitempty"`
Tags []string `json:"tags,omitempty"`
Indicators []struct {
ID *string `json:"_id"`
Indicator *string `json:"indicator"`
Type *string `json:"type"`
Description *string `json:"description,omitempty"`
} `json:"indicators,omitempty"`
Revision *float32 `json:"revision,omitempty"`
}

func (r PulseDetail) String() string {
return Stringify(r)
}

type OTXThreatIntelFeedService struct {
client *Client
}

type ThreatIntelFeed struct {
Pulses []PulseDetail `json:"results"`
// These fields provide the page values for paginating through a set of
// results. Any or all of these may be set to the zero value for
// responses that are not part of a paginated set, or for which there
// are no additional pages.
//NextPageNum int Coming soon
//PrevPageNum int Coming soon
NextPageString *string `json:"next"`
PrevPageString *string `json:"prev"`
Count int `json:"count"`
// Pulse results from search/pulse
type Pulse struct {
ID *string `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
ModifiedTimestamp *string `json:"modified,omitempty"`
Adversary *string `json:"adversary,omitempty"`
TLP *string `json:"TLP,omitempty"`
TargetedCountries []string `json:"targeted_countries"`
AuthorName *string `json:"author_name,omitempty"`
Revision *int `json:"revision,omitempty"`
Description *string `json:"description,omitempty"`
Public *int `json:"public,omitempty"`
References []string `json:"references,omitempty"`
Tags []string `json:"tags,omitempty"`
Industries []string `json:"industries,omitempty"`
Indicators []Indicator `json:"indicators,omitempty"`
}

func (r ThreatIntelFeed) String() string {
func (r Pulse) String() string {
return Stringify(r)
}
18 changes: 18 additions & 0 deletions src/otxapi/searchpulses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package otxapi

type OTXSearchPulsesSearvice struct {
client *Client
}

// Pulse from search/pulse
type PulseSearchResponse struct {
Next *string `json:"next,omitempty"`
ExactMatch *string `json:"exact_match,omitempty"`
Previous *string `json:"previous,omitempty"`
Count *int `json:"count,omitempty"`
Results []Pulse `json:"results,omitempty"`
}

func (r PulseSearchResponse) String() string {
return Stringify(r)
}
22 changes: 22 additions & 0 deletions src/otxapi/threatintelfeeds.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package otxapi

type OTXThreatIntelFeedService struct {
client *Client
}

type ThreatIntelFeed struct {
Pulses []Pulse `json:"results"`
// These fields provide the page values for paginating through a set of
// results. Any or all of these may be set to the zero value for
// responses that are not part of a paginated set, or for which there
// are no additional pages.
//NextPageNum int Coming soon
//PrevPageNum int Coming soon
NextPageString *string `json:"next"`
PrevPageString *string `json:"prev"`
Count int `json:"count"`
}

func (r ThreatIntelFeed) String() string {
return Stringify(r)
}
Loading