131 lines
1.6 KiB
Go
131 lines
1.6 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{
|
|
DropOp: api.Operation_ALL,
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = c.Alter(context.Background(), &api.Operation{
|
|
Schema: `
|
|
id: string @index(term) .
|
|
type: string @index(term) .
|
|
ndata: string .
|
|
nodeOne: string @index(term) .
|
|
nodeTwo: string @index(term) .
|
|
sourceName: string @index(term) .
|
|
timestamp: string .
|
|
created: string .
|
|
modified: string .
|
|
|
|
type Node {
|
|
id
|
|
type
|
|
ndata
|
|
created
|
|
modified
|
|
}
|
|
|
|
type Edge {
|
|
id
|
|
nodeOne
|
|
nodeTwo
|
|
timestamp
|
|
sourceName
|
|
}
|
|
|
|
fingerprint: string .
|
|
notBefore: string .
|
|
notAfter: string .
|
|
cn: string .
|
|
sourceName: string .
|
|
serialNumber: string .
|
|
basicConstraints: string .
|
|
chain: uid .
|
|
csdata: uid .
|
|
|
|
type CertNode {
|
|
id
|
|
fingerprint
|
|
notBefore
|
|
notAfter
|
|
cn
|
|
sourceName
|
|
serialNumber
|
|
basicConstraints
|
|
chain
|
|
}
|
|
|
|
type CertRaw {
|
|
id
|
|
type
|
|
created
|
|
modified
|
|
csdata
|
|
}
|
|
|
|
type PasteNode {
|
|
id
|
|
type
|
|
created
|
|
modified
|
|
ndata
|
|
}
|
|
|
|
meta: uid .
|
|
full: string .
|
|
|
|
type FullPaste {
|
|
meta
|
|
full
|
|
}
|
|
|
|
type ShodanNode {
|
|
id
|
|
type
|
|
ndata
|
|
created
|
|
modified
|
|
}
|
|
|
|
type BalboaNode {
|
|
id
|
|
type
|
|
ndata
|
|
created
|
|
modified
|
|
}
|
|
`})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|