Skip to content

Commit

Permalink
Renaming "events" to "alerts
Browse files Browse the repository at this point in the history
  • Loading branch information
botherder committed Jul 6, 2020
1 parent 0ef43f6 commit 054d851
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 36 deletions.
30 changes: 15 additions & 15 deletions api_events.go → api_alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/nu7hatch/gouuid"
)

func apiEventsFetch(w http.ResponseWriter, r *http.Request) {
func apiAlertsFetch(w http.ResponseWriter, r *http.Request) {
var offset int64 = 0
keys, ok := r.URL.Query()["offset"]
if ok && len(keys) == 1 {
Expand All @@ -38,39 +38,39 @@ func apiEventsFetch(w http.ResponseWriter, r *http.Request) {
limit, _ = strconv.ParseInt(keys[0], 10, 64)
}

events, err := db.GetAllEvents(offset, limit)
alerts, err := db.GetAllAlerts(offset, limit)
if err != nil {
errorWithJSON(w, "Failed to fetch events from database", http.StatusInternalServerError, err)
errorWithJSON(w, "Failed to fetch alerts from database", http.StatusInternalServerError, err)
return
}

responseWithJSON(w, events)
responseWithJSON(w, alerts)
}

func apiEventsAdd(w http.ResponseWriter, r *http.Request) {
// We decode the request to an Event.
func apiAlertsAdd(w http.ResponseWriter, r *http.Request) {
// We decode the request to an Alert.
decoder := json.NewDecoder(r.Body)
var event Event
err := decoder.Decode(&event)
var alert Alert
err := decoder.Decode(&alert)
if err != nil {
errorWithJSON(w, "Unable to parse event", http.StatusBadRequest, err)
errorWithJSON(w, "Unable to parse alert", http.StatusBadRequest, err)
return
}

event.Datetime = time.Now().UTC()
alert.Datetime = time.Now().UTC()

uuidInstance, _ := uuid.NewV4()
event.UUID = uuidInstance.String()
alert.UUID = uuidInstance.String()

err = db.AddEvent(event)
err = db.AddAlert(alert)
if err != nil {
errorWithJSON(w, "Unable to store event in database", http.StatusInternalServerError, err)
errorWithJSON(w, "Unable to store alert in database", http.StatusInternalServerError, err)
return
}

response := map[string]string{
"msg": "Event added successfully",
"uuid": event.UUID,
"msg": "Alert added successfully",
"uuid": alert.UUID,
}

responseWithJSON(w, response)
Expand Down
24 changes: 12 additions & 12 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Indicator struct {
Owner string `json:"owner"`
}

type Event struct {
type Alert struct {
Type string `json:"type"`
Match string `json:"match"`
Indicator string `json:"indicator"`
Expand Down Expand Up @@ -87,7 +87,7 @@ type AnalysisResults struct {
Visits []string `json:"visits"`
Resources []phishdetect.Resource `json:"resources"`
HTML string `json:"html"`
EventUUID string `json:"uuid"`
AlertUUID string `json:"uuid"`
}

const IndicatorsLimitAll = 0
Expand Down Expand Up @@ -267,8 +267,8 @@ func (d *Database) AddIndicator(ioc Indicator) error {
return err
}

func (d *Database) GetAllEvents(offset, limit int64) ([]Event, error) {
coll := d.DB.Collection("events")
func (d *Database) GetAllAlerts(offset, limit int64) ([]Alert, error) {
coll := d.DB.Collection("alerts")

opts := options.Find()
opts.SetSort(bson.D{{"datetime", -1}})
Expand All @@ -284,22 +284,22 @@ func (d *Database) GetAllEvents(offset, limit int64) ([]Event, error) {
}
defer cur.Close(context.Background())

events := []Event{}
alerts := []Alert{}
for cur.Next(context.Background()) {
var event Event
if err := cur.Decode(&event); err != nil {
var alert Alert
if err := cur.Decode(&alert); err != nil {
continue
}
events = append(events, event)
alerts = append(alerts, alert)
}

return events, nil
return alerts, nil
}

func (d *Database) AddEvent(event Event) error {
coll := d.DB.Collection("events")
func (d *Database) AddAlert(alert Alert) error {
coll := d.DB.Collection("alerts")

_, err := coll.InsertOne(context.Background(), event)
_, err := coll.InsertOne(context.Background(), alert)
return err
}

Expand Down
14 changes: 7 additions & 7 deletions gui_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,13 @@ func guiLinkAnalyze(w http.ResponseWriter, r *http.Request) {
// For the moment, urlFinal will be the original URL.
urlFinal := url

var eventType string
var alertType string
var results *AnalysisResults
var err error

// If there is no specified HTML string, it means we need to open the link.
if htmlEncoded == "" {
eventType = "analysis_link"
alertType = "analysis_link"
results, err = analyzeURL(url)
if err != nil {
errorPage(w, err.Error())
Expand All @@ -110,7 +110,7 @@ func guiLinkAnalyze(w http.ResponseWriter, r *http.Request) {
urlFinal = results.URLFinal
screenshot = results.Screenshot
} else {
eventType = "analysis_html"
alertType = "analysis_html"
results, err = analyzeHTML(url, htmlEncoded)
if err != nil {
errorPage(w, err.Error())
Expand Down Expand Up @@ -146,20 +146,20 @@ func guiLinkAnalyze(w http.ResponseWriter, r *http.Request) {
// We store a record in the database.
uuidInstance, _ := uuid.NewV4()
uuidString := uuidInstance.String()
event := Event{
Type: eventType,
alert := Alert{
Type: alertType,
Match: url,
Indicator: "",
UserContact: "",
Datetime: time.Now().UTC(),
UUID: uuidString,
}
err = db.AddEvent(event)
err = db.AddAlert(alert)
if err != nil {
log.Error(err)
}

results.EventUUID = uuidString
results.AlertUUID = uuidString
err = db.AddAnalysisResults(*results)
if err != nil {
log.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ func startServer() {
router.HandleFunc("/api/indicators/fetch/", authMiddleware(apiIndicatorsFetch, roleUser)).Methods("GET")
router.HandleFunc("/api/indicators/fetch/recent/", authMiddleware(apiIndicatorsFetchRecent, roleUser)).Methods("GET")
router.HandleFunc("/api/indicators/fetch/all/", authMiddleware(apiIndicatorsFetchAll, roleUser)).Methods("GET")
router.HandleFunc("/api/events/add/", authMiddleware(apiEventsAdd, roleUser)).Methods("POST")
router.HandleFunc("/api/alerts/add/", authMiddleware(apiAlertsAdd, roleUser)).Methods("POST")
router.HandleFunc("/api/reports/add/", authMiddleware(apiReportsAdd, roleUser)).Methods("POST")

// Submitter routes.
router.HandleFunc("/api/indicators/add/", authMiddleware(apiIndicatorsAdd, roleSubmitter)).Methods("POST")

// Admin routes.
router.HandleFunc(fmt.Sprintf("/api/indicators/details/{ioc:%s}/", sha256Regex), authMiddleware(apiIndicatorsDetails, roleAdmin)).Methods("GET")
router.HandleFunc("/api/events/fetch/", authMiddleware(apiEventsFetch, roleAdmin)).Methods("GET")
router.HandleFunc("/api/alerts/fetch/", authMiddleware(apiAlertsFetch, roleAdmin)).Methods("GET")
router.HandleFunc("/api/reports/fetch/", authMiddleware(apiReportsFetch, roleAdmin)).Methods("GET")
router.HandleFunc(fmt.Sprintf("/api/reports/details/{uuid:%s}/", uuidRegex), authMiddleware(apiReportsDetails, roleAdmin)).Methods("GET")
router.HandleFunc("/api/users/pending/", authMiddleware(apiUsersPending, roleAdmin)).Methods("GET")
Expand Down

0 comments on commit 054d851

Please sign in to comment.