Basic read/write implementation with output in JSON

This commit is contained in:
Christopher Talib 2020-01-14 14:49:14 +01:00
parent b4e07b254a
commit 7d741f4cf3
5 changed files with 173 additions and 2 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
styx-poc
*.json

10
go.mod
View file

@ -1,3 +1,13 @@
module gitlab.dcso.lolcat/LABS/styx-poc
go 1.12
require (
github.com/CaliDog/certstream-go v0.0.0-20180219203951-6016c5462366
github.com/google/uuid v1.1.1
github.com/gorilla/websocket v1.4.1
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7
github.com/pkg/errors v0.9.0
github.com/sirupsen/logrus v1.4.2
)

24
go.sum Normal file
View file

@ -0,0 +1,24 @@
github.com/CaliDog/certstream-go v0.0.0-20180219203951-6016c5462366 h1:qjPX+NGqyjCTkoQqEctkfhU4C/B4LhFG0ugVLhM7Maw=
github.com/CaliDog/certstream-go v0.0.0-20180219203951-6016c5462366/go.mod h1:JBo69gi8JyPpZoLZgmZeXiq4o7Ib2qf2RiIxiWC0oYQ=
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/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e h1:ZZCvgaRDZg1gC9/1xrsgaJzQUCQgniKtw0xjWywWAOE=
github.com/jmoiron/jsonq v0.0.0-20150511023944-e874b168d07e/go.mod h1:+rHyWac2R9oAZwFe1wGY2HBzFJJy++RHBg1cU23NkD8=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 h1:lDH9UUVJtmYCjyT0CI4q8xvlXPxeZ0gYCVvWbmPlp88=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/pkg/errors v0.9.0 h1:J8lpUdobwIeCI7OiSxHqEwJUKvJwicL5+3v1oe2Yb4k=
github.com/pkg/errors v0.9.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

28
main.go
View file

@ -1,7 +1,31 @@
package main
import "fmt"
import (
"github.com/CaliDog/certstream-go"
"github.com/op/go-logging"
)
var (
log = logging.MustGetLogger("example")
)
func main() {
fmt.Println("Hello world!")
// The false flag specifies that we want heartbeat messages.
stream, errStream := certstream.CertStreamEventStream(false)
for {
select {
case jq := <-stream:
messageType, err := jq.String("message_type")
if err != nil {
log.Fatal("Error decoding jq string")
}
log.Info("Message type -> ", messageType)
log.Info("recv: ", jq)
case err := <-errStream:
log.Error(err)
}
}
}

112
models/main.go Normal file
View file

@ -0,0 +1,112 @@
package models
import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/google/uuid"
"github.com/jmoiron/jsonq"
"github.com/sirupsen/logrus"
)
const (
nodesFilename = "nodes.json"
edgesFilename = "edges.json"
)
// Node defines the data we gather through the parsing.
type Node struct {
ID uuid.UUID
Flag string `json:"flag"`
}
// Edge defines a relation between two nodes.
type Edge struct {
ID uuid.UUID
NodeOneID uuid.UUID `json:"NodeOneID"`
// NodeTwoID uuid.UUID `json:NodeTwoID` to implement
Timestamp time.Time `json:"Timestamp"`
Data jsonq.JsonQuery `json:"Data"`
}
// SaveData is the main function used to save data. You need to pass a specific
// flag to it and the data recieved. It just saves data, it doesn't filter or
// look for it in the stream.
func SaveData(flag string, data jsonq.JsonQuery) {
err := fileExists(nodesFilename)
if err != nil {
logrus.Error(err)
}
err = fileExists(edgesFilename)
if err != nil {
logrus.Error(err)
}
nodeFile, err := ioutil.ReadFile(nodesFilename)
if err != nil {
logrus.Error(err)
}
edgeFile, err := ioutil.ReadFile(edgesFilename)
if err != nil {
logrus.Error(err)
}
nodeDatas := []Node{}
edgeDatas := []Edge{}
json.Unmarshal(nodeFile, &nodeDatas)
json.Unmarshal(edgeFile, &edgeDatas)
node := &Node{
ID: uuid.New(),
Flag: flag,
}
edge := &Edge{
ID: uuid.New(),
NodeOneID: node.ID,
Timestamp: time.Now(),
Data: data,
}
nodeDatas = append(nodeDatas, *node)
edgeDatas = append(edgeDatas, *edge)
nodeBytes, err := json.Marshal(nodeDatas)
if err != nil {
logrus.Error(err)
}
edgeBytes, err := json.Marshal(edgeDatas)
if err != nil {
logrus.Error(err)
}
err = ioutil.WriteFile(nodesFilename, nodeBytes, 0644)
if err != nil {
logrus.Error(err)
}
err = ioutil.WriteFile(edgesFilename, edgeBytes, 0644)
if err != nil {
logrus.Error(err)
}
}
// Helpers
func fileExists(filename string) error {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
_, err := os.Create(filename)
if err != nil {
return err
}
}
return nil
}