Skip to content

Commit

Permalink
Redis plugin added
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksejsv committed Nov 22, 2023
1 parent 420ed90 commit c0eb155
Show file tree
Hide file tree
Showing 12 changed files with 670 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dockerfile.example
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/http.so
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/mongodb.so plugins/src/mongodb/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/pastelyzer.so plugins/src/pastelyzer/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/postgresql.so plugins/src/postgresql/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/redis.so plugins/src/redis/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/mysql.so plugins/src/mysql/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/file-csv.so plugins/src/file/csv/*.go
RUN go build -buildmode=plugin -ldflags="-w" -o /go/plugins/sources/abuseipdb.so plugins/src/abuseipdb/*.go
Expand Down
6 changes: 4 additions & 2 deletions Makefile.example
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,14 @@ plugins-local:
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/mongodb.so plugins/src/mongodb/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/pastelyzer.so plugins/src/pastelyzer/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/postgresql.so plugins/src/postgresql/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/redis.so plugins/src/redis/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/mysql.so plugins/src/mysql/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/file-csv.so plugins/src/file/csv/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/sources/abuseipdb.so plugins/src/abuseipdb/*.go
CGO_CFLAGS="-g -O2 -Wno-return-local-addr" go build -buildmode=plugin -ldflags="-w" -o plugins/sources/sqlite.so plugins/src/sqlite/*.go

go build -buildmode=plugin -ldflags="-w" -o plugins/processors/taxonomy.so plugins/src/taxonomy/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/processors/modify.so plugins/src/modify/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/processors/taxonomy.so plugins/src/taxonomy/*.go
go build -buildmode=plugin -ldflags="-w" -o plugins/processors/modify.so plugins/src/modify/*.go

go build -buildmode=plugin -ldflags="-w" -o /dev/null plugins/src/template/*.go

Expand All @@ -192,6 +193,7 @@ test:
go test plugins/src/mongodb/*.go
go test plugins/src/pastelyzer/*.go
go test plugins/src/postgresql/*.go
go test plugins/src/redis/*.go
go test plugins/src/mysql/*.go
go test plugins/src/file/csv/*.go
go test plugins/src/abuseipdb/*.go
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.5.3
2.5.4
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ require (
github.com/montanaflynn/stats v0.7.0 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/olivere/elastic/v7 v7.0.22
github.com/redis/go-redis/v9 v9.3.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/rs/zerolog v1.29.0
github.com/umpc/go-sortedmap v0.0.0-20180422175548-64ab94c482f4
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6r
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/blastrain/vitess-sqlparser v0.0.0-20201030050434-a139afbb1aba h1:hBK2BWzm0OzYZrZy9yzvZZw59C5Do4/miZ8FhEwd5P8=
github.com/blastrain/vitess-sqlparser v0.0.0-20201030050434-a139afbb1aba/go.mod h1:FGQp+RNQwVmLzDq6HBrYCww9qJQyNwH9Qji/quTQII4=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/clbanning/x2j v0.0.0-20191024224557-825249438eec/go.mod h1:jMjuTZXRI4dUb/I5gc9Hdhagfvm9+RyrPryS/auMzxE=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cockroachdb/apd v1.1.0 h1:3LFP3629v+1aKXU5Q37mxmRxX/pIu1nijXydLShEq5I=
Expand All @@ -54,6 +58,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denisenkom/go-mssqldb v0.0.0-20191124224453-732737034ffd/go.mod h1:xbL0rPBG9cCiLr28tMa8zpbdarY27NDyej4t/EjAShU=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/dustin/go-humanize v0.0.0-20171111073723-bb3d318650d4/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/eapache/go-resiliency v1.1.0/go.mod h1:kFI+JgMyC7bLPUVY133qvEBtVayf5mFgVsvEsIPBvNs=
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21/go.mod h1:+020luEh2TKB4/GOp8oxxtq0Daoen/Cii55CzbTV6DU=
Expand Down Expand Up @@ -381,6 +387,8 @@ github.com/prometheus/procfs v0.0.0-20190117184657-bf6a532e95b1/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/redis/go-redis/v9 v9.3.0 h1:RiVDjmig62jIWp7Kk4XVLs0hzV6pI3PyTnnL0cnn0u0=
github.com/redis/go-redis/v9 v9.3.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
5 changes: 5 additions & 0 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ func splitQuery(expr *sqlparser.Select) ([]*sqlparser.Select, error) {
// Handle:
// (field='...' OR field=...')
// (field IN ('...','...'))
// (field='...' AND param='...')
} else if paren, ok := where.(*sqlparser.ParenExpr); ok {
if _, ok := paren.Expr.(*sqlparser.AndExpr); ok {
return selects, nil
}

lefts, err = splitOrIn(fields, paren.Expr, nil)
if err != nil {
return nil, err
Expand Down
91 changes: 91 additions & 0 deletions plugins/src/redis/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# Redis plugin

Plugin to query Redis (https://redis.io) as a data source.


Compile with:
```sh
go build -buildmode=plugin -ldflags="-w" -o redis.so ./*.go
```

**Warning**

Redis does NOT accept complex queries, like SQL databases do.

The easiest workaround is to exclude Redis DB from the `global` namespace
and query it independently, to execute needed queries one by one.


# Access details

Source YAML definition's `access` fields:
- **addr**: HOST:PORT database's access point, for example - `localhost:6379`
- **user**: username to connect to the database
- **password**: user's password
- **db**: database number to use
- **field**: Redis key will be used as this field name


# Usage

Simple example of a new Redis data source. Insert test data:
```sh
redis-cli -u redis://localhost:6379/8
ACL SETUSER graphoscope on >password allkeys +hset +hget +hgetall +select +ping
ACL SAVE # Or 'CONFIG REWRITE'
AUTH graphoscope password

HSET '[email protected]' username 'a' fqdn 'example.com' count 13 seen '18-02-2023T15:34:00.000000Z'
HSET '[email protected]' username 'b' fqdn 'example.com' count 13 seen '19-02-2023T15:34:00.000000Z'
HSET '[email protected]' username 'c' fqdn 'example.com' count 13 seen '20-02-2023T15:34:00.000000Z'
HSET '[email protected]' username 'd' fqdn 'example.com' count 13 seen '21-02-2023T15:34:00.000000Z'
HSET '[email protected]' username 'e' fqdn 'example.com' count 13 seen '22-02-2023T15:34:00.000000Z'
```

Access data will be used by the source's YAML definition. Example:
```yaml
name: retest
label: RETest
icon: database

plugin: redis
inGlobal: false
includeDatetime: false
supportsSQL: false

access:
addr: 127.0.0.1:6379
user: graphoscope
password: password
db: 8
field: email

queryFields:
- email

replaceFields:
datetime: seen
domain: fqdn


relations:
-
from:
id: email
group: email
search: email
attributes: [ "seen", "fqdn" ]

to:
id: username
group: username
search: username

edge:
attributes: [ "count" ]
```
Test with a query:
```sh
curl -XGET 'https://localhost:443/api?uuid=auth-key&sql=FROM+retest+WHERE+email=%[email protected]%27'
```
33 changes: 33 additions & 0 deletions plugins/src/redis/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* SQL to the field/value list convertor
*/

package main

import (
"github.com/blastrain/vitess-sqlparser/sqlparser"
)

/*
* Convert SQL query to the list of [field,value]
*/
func (p *plugin) convert(sel *sqlparser.Select) (string, error) {

// Handle WHERE.
// Top level node pass in an empty interface
// to tell the children this is root.
// Is there any better way?
var rootParent sqlparser.Expr

// List of requested fields & values
fields, err := handleSelectWhere(&sel.Where.Expr, true, &rootParent)
if err != nil {
return "", err
}

if len(fields) == 1 && fields[0][0] == p.source.Access["field"] {
return fields[0][1], nil
}

return "", nil
}
28 changes: 28 additions & 0 deletions plugins/src/redis/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"github.com/cert-lv/graphoscope/pdk"
"github.com/redis/go-redis/v9"
)

/*
* Export symbols
*/
var (
Name = "redis"
Version = "1.0.0"
Plugin plugin
)

/*
* Structure to be imported by the core as a plugin
*/
type plugin struct {

// Inherit default configuration fields
source *pdk.Source

// Custom fields
client *redis.Client
limit int
}
Loading

0 comments on commit c0eb155

Please sign in to comment.