styx/graph/main.go
Christopher Talib f61fe566a5 Basic connection to Dgraph DB
The first work and input to the graph db is set up in this work. It's
for the moment very basic and doesn't cover relations and only works for
certstream data.
2020-03-04 15:16:59 +01:00

127 lines
1.5 KiB
Go

package graph
import (
"context"
"github.com/dgraph-io/dgo/v2"
"github.com/dgraph-io/dgo/v2/protos/api"
"google.golang.org/grpc"
)
func ConnectToDgraph() (*dgo.Dgraph, error) {
conn, err := grpc.Dial("localhost:9080", grpc.WithInsecure())
if err != nil {
return nil, err
}
dgraphClient := dgo.NewDgraphClient(api.NewDgraphClient(conn))
err = setupDgraphSchema(dgraphClient)
if err != nil {
return nil, err
}
return dgraphClient, nil
}
func setupDgraphSchema(c *dgo.Dgraph) error {
err := c.Alter(context.Background(), &api.Operation{
DropAll: true,
})
err = c.Alter(context.Background(), &api.Operation{
Schema: `
id: string @index(term) .
type: string @index(term) .
data: string .
nodeOneID: uid .
nodeTwoID: uid .
source: string @index(term) .
timestamp: string .
created: dateTime .
modified: dateTime .
type Node {
id
type
data
created
modified
}
type Edge {
id
nodeOneID
nodeTwoID
timestamp
source
}
fingerprint: string .
notBefore: string .
notAfter: string .
cn: string .
sourceName: string .
serialNumber: string .
basicConstraints: string .
chain: uid .
type CertNode {
id
fingerprint
notBefore
notAfter
cn
sourceName
serialNumber
basicConstraints
chain
}
type CertRaw {
id
type
created
modified
data
}
type PasteNode {
id
type
created
modified
data
}
meta: uid .
full: string .
type FullPaste {
meta
full
}
type ShodanNode {
id
type
data
created
modified
}
type BalboaNode {
id
type
data
created
modified
}
`})
if err != nil {
return err
}
return nil
}