Skip to content

Commit

Permalink
Using indicators "status" instead of just "enabled"
Browse files Browse the repository at this point in the history
  • Loading branch information
botherder committed Oct 27, 2020
1 parent b961c64 commit c4fcd22
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
41 changes: 35 additions & 6 deletions api_indicators.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func prepareIndicators(iocs []Indicator) map[string][]string {

func apiIndicatorsFetch(w http.ResponseWriter, r *http.Request) {
// We get the indicators from the DB.
iocs, err := db.GetIndicators(IndicatorsLimit6Months, true)
iocs, err := db.GetIndicators(IndicatorsLimit6Months, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
Expand All @@ -116,7 +116,7 @@ func apiIndicatorsFetch(w http.ResponseWriter, r *http.Request) {
}

func apiIndicatorsFetchRecent(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimit24Hours, true)
iocs, err := db.GetIndicators(IndicatorsLimit24Hours, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
Expand All @@ -127,7 +127,7 @@ func apiIndicatorsFetchRecent(w http.ResponseWriter, r *http.Request) {
}

func apiIndicatorsFetchAll(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, true)
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusEnabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
Expand All @@ -137,8 +137,18 @@ func apiIndicatorsFetchAll(w http.ResponseWriter, r *http.Request) {
responseWithJSON(w, indicators)
}

func apiIndicatorsFetchPending(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusPending)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
}

responseWithJSON(w, iocs)
}

func apiIndicatorsFetchDisabled(w http.ResponseWriter, r *http.Request) {
iocs, err := db.GetIndicators(IndicatorsLimitAll, false)
iocs, err := db.GetIndicators(IndicatorsLimitAll, IndicatorsStatusDisabled)
if err != nil {
errorWithJSON(w, ErrorMsgIndicatorsFetchFailed, http.StatusInternalServerError, err)
return
Expand Down Expand Up @@ -188,14 +198,24 @@ func apiIndicatorsAdd(w http.ResponseWriter, r *http.Request) {
continue
}

// By default, we add indicators as enabled.
status := IndicatorsStatusEnabled
if !req.Enabled {
// If the submitter specifies enabled=False,
// then we add the indicators as "pending".
// NOTE: We don't add indicators directly "disabled", as that does
// not make much sense.
status = IndicatorsStatusPending
}

ioc := Indicator{
Type: indicatorType,
Original: indicator,
Hashed: hashed,
Tags: req.Tags,
Datetime: time.Now().UTC(),
Owner: user.Name,
Enabled: req.Enabled,
Status: status,
}

err = db.AddIndicator(ioc)
Expand Down Expand Up @@ -235,7 +255,16 @@ func apiIndicatorsToggle(w http.ResponseWriter, r *http.Request) {
continue
}

ioc.Enabled = !ioc.Enabled
// If the status of the indicator is not "enabled", it's either
// "pending" or "disabled". In either case, we want to turn it
// to "enabled".
if ioc.Status != IndicatorsStatusEnabled {
ioc.Status = IndicatorsStatusEnabled
} else {
// If it's currently "enabled", we turn it to "disabled".
ioc.Status = IndicatorsStatusDisabled
}

err = db.UpdateIndicator(ioc)
if err != nil {
log.Warning("Failed to update indicator: ", err.Error())
Expand Down
16 changes: 10 additions & 6 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type Indicator struct {
Tags []string `json:"tags"`
Datetime time.Time `json:"datetime"`
Owner string `json:"owner"`
Enabled bool `json:"enabled"`
Status string `json:"status"`
}

type Alert struct {
Expand Down Expand Up @@ -98,6 +98,10 @@ const IndicatorsLimitAll = 0
const IndicatorsLimit6Months = 1
const IndicatorsLimit24Hours = 2

const IndicatorsStatusPending = "pending"
const IndicatorsStatusEnabled = "enabled"
const IndicatorsStatusDisabled = "disabled"

func NewDatabase(url string) (*Database, error) {
client, err := mongo.NewClient(options.Client().ApplyURI(url))
if err != nil {
Expand Down Expand Up @@ -204,7 +208,7 @@ func (d *Database) GetUserByUUID(uuid string) (User, error) {
return userFound, nil
}

func (d *Database) GetIndicators(limit int, enabled bool) ([]Indicator, error) {
func (d *Database) GetIndicators(limit int, status string) ([]Indicator, error) {
var iocs []Indicator
coll := d.DB.Collection("indicators")

Expand All @@ -214,20 +218,20 @@ func (d *Database) GetIndicators(limit int, enabled bool) ([]Indicator, error) {

switch limit {
case IndicatorsLimitAll:
filter = bson.M{"enabled": enabled}
filter = bson.M{"status": status}
case IndicatorsLimit6Months:
filter = bson.M{
"datetime": bson.M{
"$gte": now.AddDate(0, -6, 0),
},
"enabled": enabled,
"status": status,
}
case IndicatorsLimit24Hours:
filter = bson.M{
"datetime": bson.M{
"$gte": now.Add(-24 * time.Hour),
},
"enabled": enabled,
"status": status,
}
}

Expand Down Expand Up @@ -308,7 +312,7 @@ func (d *Database) UpdateIndicator(ioc Indicator) error {
bson.M{"$set": bson.M{
"datetime": time.Now().UTC(),
"tags": ioc.Tags,
"enabled": ioc.Enabled,
"status": ioc.Status,
}})

return err
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897
golang.org/x/net v0.0.0-20201021035429-f5854403a974 // indirect
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 // indirect
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 // indirect
golang.org/x/sys v0.0.0-20201027130517-9d1ec526b7bf // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/go-playground/assert.v1 v1.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200722175500-76b94024e4b6 h1:X9xIZ1YU8bLZA3l6gqDUHSFiD0GFI9S548h6C8nDtOY=
golang.org/x/sys v0.0.0-20200722175500-76b94024e4b6/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5 h1:iCaAy5bMeEvwANu3YnJfWwI0kWAGkEa2RXPdweI/ysk=
golang.org/x/sys v0.0.0-20201024232916-9f70ab9862d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201027130517-9d1ec526b7bf h1:HmHgHRpqpvB74D7bjXkue6kkHJfOrKyYJtW6Sv4jpI4=
golang.org/x/sys v0.0.0-20201027130517-9d1ec526b7bf/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ func startServer() {
// Admin routes.
router.HandleFunc(fmt.Sprintf("/api/indicators/details/{ioc:%s}/", sha256Regex),
authMiddleware(apiIndicatorsDetails, roleAdmin)).Methods("GET")
router.HandleFunc(fmt.Sprintf("/api/indicators/pending/"),
authMiddleware(apiIndicatorsFetchPending, roleAdmin)).Methods("GET")
router.HandleFunc(fmt.Sprintf("/api/indicators/disabled/"),
authMiddleware(apiIndicatorsFetchDisabled, roleAdmin)).Methods("GET")
router.HandleFunc("/api/indicators/toggle/",
Expand Down

0 comments on commit c4fcd22

Please sign in to comment.