Adding unpacking in JSON format, and types for certstream format

This commit is contained in:
Christopher Talib 2020-01-14 16:31:57 +01:00
parent 6064eed3e9
commit b7dce16c9e
4 changed files with 117 additions and 33 deletions

View file

@ -3,6 +3,8 @@ package main
import (
"github.com/CaliDog/certstream-go"
"github.com/op/go-logging"
"gitlab.dcso.lolcat/LABS/styx/models"
"gitlab.dcso.lolcat/LABS/styx/utils"
)
var (
@ -24,6 +26,10 @@ func main() {
log.Info("Message type -> ", messageType)
log.Info("recv: ", jq)
if data, err := utils.ExtractCertFromStream(jq); err == nil {
models.SaveData("test", *data)
}
case err := <-errStream:
log.Error(err)
}

View file

@ -4,10 +4,8 @@ import (
"encoding/json"
"io/ioutil"
"os"
"time"
"github.com/google/uuid"
"github.com/jmoiron/jsonq"
"github.com/sirupsen/logrus"
)
@ -16,25 +14,10 @@ const (
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) {
func SaveData(flag string, data CertStreamStruct) {
err := fileExists(nodesFilename)
if err != nil {
logrus.Error(err)
@ -61,40 +44,43 @@ func SaveData(flag string, data jsonq.JsonQuery) {
json.Unmarshal(nodeFile, &nodeDatas)
json.Unmarshal(edgeFile, &edgeDatas)
// res := CertStreamStruct{}
// json.Unmarshal(data, &res)
node := &Node{
ID: uuid.New(),
Flag: flag,
Data: data,
}
edge := &Edge{
ID: uuid.New(),
NodeOneID: node.ID,
Timestamp: time.Now(),
Data: data,
}
// edge := &Edge{
// ID: uuid.New(),
// NodeOneID: node.ID,
// Timestamp: time.Now(),
// }
nodeDatas = append(nodeDatas, *node)
edgeDatas = append(edgeDatas, *edge)
// 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)
}
// 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)
}
// err = ioutil.WriteFile(edgesFilename, edgeBytes, 0644)
// if err != nil {
// logrus.Error(err)
// }
}
// Helpers

72
models/types.go Normal file
View file

@ -0,0 +1,72 @@
package models
import (
"time"
"github.com/google/uuid"
)
// Node defines the data we gather through the parsing.
type Node struct {
ID uuid.UUID
Flag string `json:"flag"`
Data CertStreamStruct `json:"data"`
}
// 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"`
}
type LeafCertExtensions struct {
KeyUsage string `json:"keyUsage"`
ExtendedKeyUsage string `json:"extendedKeyUsage"`
BasicConstrains string `json:"basicConstrains"`
SubjectKeyIdentifier string `json:"subjectKeyIdentifier"`
AuthorityInfoAccess string `json:"authorityInfoAccess"`
SubjectAltName string `json:"subjectAltName"`
CertificatePolicies string `json:"certificatePolicies"`
}
type LeafCertSubject struct {
Aggregated string `json:"aggregated"`
C string `json:"C"`
ST string `json:"ST"`
L string `json:"L"`
O string `json:"O"`
OU string `json:"OU"`
CN string `json:"CN"`
}
type LeafCertStruct struct {
Subject *LeafCertSubject `json:"aggregated"`
Extensions *LeafCertExtensions `json:"extensions"`
NotBefore string `json:"not_before"`
NotAfter string `json:"not_after"`
SerialNumber string `json:"serial_number"`
Fingerprint string `json:"fingerprint"`
AsDer string `json:"as_der"`
AllDomains []string `json:"all_domains"`
}
type Source struct {
URL string `json:"url"`
Name string `json:"name"`
}
type CertStreamData struct {
UpdateType string `json:"update_type"`
LeafCert *LeafCertStruct `json:"leaf_cert"`
Chain []*LeafCertStruct `json:"chain"`
CertIndex int `json:"cert_index"`
Seen time.Time `json:"seen"`
Source *Source `json:"source"`
}
type CertStreamStruct struct {
MessageType string `json:"message_data"`
Data CertStreamData `json:"data"`
}

20
utils/main.go Normal file
View file

@ -0,0 +1,20 @@
package utils
import (
"github.com/jmoiron/jsonq"
"gitlab.dcso.lolcat/LABS/styx/models"
)
func ExtractCertFromStream(input jsonq.JsonQuery) (*models.CertStreamStruct, error) {
messageType, err := input.String("message_type")
if err != nil {
return nil, err
}
res := models.CertStreamStruct{
MessageType: messageType,
}
return &res, nil
}