mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2026-01-07 14:53:17 +01:00
Release catalyst
This commit is contained in:
182
database/busdb/busdb.go
Normal file
182
database/busdb/busdb.go
Normal file
@@ -0,0 +1,182 @@
|
||||
package busdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/arangodb/go-driver"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/bus"
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
)
|
||||
|
||||
type Hook interface {
|
||||
PublishAction(action string, context, msg map[string]interface{}) error
|
||||
PublishUpdate(col, id string) error
|
||||
}
|
||||
|
||||
// BusDatabase
|
||||
// 1. Save entry to log
|
||||
// 2. Send update ticket to bus
|
||||
// 3. Add document to index
|
||||
type BusDatabase struct {
|
||||
internal driver.Database
|
||||
logCollection driver.Collection
|
||||
bus *bus.Bus
|
||||
// index *index.Index
|
||||
}
|
||||
|
||||
func NewDatabase(ctx context.Context, internal driver.Database, b *bus.Bus) (*BusDatabase, error) {
|
||||
logCollection, err := internal.Collection(ctx, LogCollectionName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &BusDatabase{
|
||||
internal: internal,
|
||||
logCollection: logCollection,
|
||||
bus: b,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type OperationType int
|
||||
|
||||
const (
|
||||
Create OperationType = iota
|
||||
Read = iota
|
||||
Update = iota
|
||||
)
|
||||
|
||||
type Operation struct {
|
||||
OperationType OperationType
|
||||
Ids []driver.DocumentID
|
||||
Msg string
|
||||
}
|
||||
|
||||
var CreateOperation = &Operation{OperationType: Create}
|
||||
var ReadOperation = &Operation{OperationType: Read}
|
||||
|
||||
func (db BusDatabase) Query(ctx context.Context, query string, vars map[string]interface{}, operation *Operation) (driver.Cursor, *models.LogEntry, error) {
|
||||
cur, err := db.internal.Query(ctx, query, vars)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var logs *models.LogEntry
|
||||
|
||||
switch {
|
||||
case operation.OperationType == Update:
|
||||
if err := db.LogAndNotify(ctx, operation.Ids, operation.Msg); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return cur, logs, err
|
||||
}
|
||||
|
||||
func (db BusDatabase) LogAndNotify(ctx context.Context, ids []driver.DocumentID, msg string) error {
|
||||
var logEntries []*models.LogEntry
|
||||
for _, i := range ids {
|
||||
logEntries = append(logEntries, &models.LogEntry{Reference: i.String(), Message: msg})
|
||||
}
|
||||
|
||||
if err := db.LogBatchCreate(ctx, logEntries); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.bus.PublishUpdate(ids)
|
||||
}
|
||||
|
||||
func (db BusDatabase) Remove(ctx context.Context) error {
|
||||
return db.internal.Remove(ctx)
|
||||
}
|
||||
|
||||
func (db BusDatabase) Collection(ctx context.Context, name string) (driver.Collection, error) {
|
||||
return db.internal.Collection(ctx, name)
|
||||
}
|
||||
|
||||
type Collection struct {
|
||||
internal driver.Collection
|
||||
db *BusDatabase
|
||||
}
|
||||
|
||||
func NewCollection(internal driver.Collection, db *BusDatabase) *Collection {
|
||||
return &Collection{internal: internal, db: db}
|
||||
}
|
||||
|
||||
func (c Collection) CreateDocument(ctx, newctx context.Context, key string, document interface{}) (driver.DocumentMeta, error) {
|
||||
meta, err := c.internal.CreateDocument(newctx, &Keyed{Key: key, Doc: document})
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
err = c.db.LogAndNotify(ctx, []driver.DocumentID{meta.ID}, "Document created")
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func (c Collection) CreateEdge(ctx, newctx context.Context, edge *driver.EdgeDocument) (driver.DocumentMeta, error) {
|
||||
meta, err := c.internal.CreateDocument(newctx, edge)
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
err = c.db.LogAndNotify(ctx, []driver.DocumentID{meta.ID}, "Document created")
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
return meta, nil
|
||||
}
|
||||
|
||||
func (c Collection) CreateEdges(ctx context.Context, edges []*driver.EdgeDocument) (driver.DocumentMetaSlice, error) {
|
||||
metas, errs, err := c.internal.CreateDocuments(ctx, edges)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if errs.FirstNonNil() != nil {
|
||||
return nil, errs.FirstNonNil()
|
||||
}
|
||||
|
||||
var ids []driver.DocumentID
|
||||
for _, meta := range metas {
|
||||
ids = append(ids, meta.ID)
|
||||
}
|
||||
|
||||
err = c.db.LogAndNotify(ctx, ids, "Document created")
|
||||
if err != nil {
|
||||
return metas, err
|
||||
}
|
||||
|
||||
return metas, nil
|
||||
}
|
||||
|
||||
func (c Collection) DocumentExists(ctx context.Context, id string) (bool, error) {
|
||||
return c.internal.DocumentExists(ctx, id)
|
||||
}
|
||||
|
||||
func (c Collection) ReadDocument(ctx context.Context, key string, result interface{}) (driver.DocumentMeta, error) {
|
||||
return c.internal.ReadDocument(ctx, key, result)
|
||||
}
|
||||
|
||||
func (c Collection) UpdateDocument(ctx context.Context, key string, update interface{}) (driver.DocumentMeta, error) {
|
||||
meta, err := c.internal.UpdateDocument(ctx, key, update)
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
return meta, c.db.bus.PublishUpdate([]driver.DocumentID{meta.ID})
|
||||
}
|
||||
|
||||
func (c Collection) ReplaceDocument(ctx context.Context, key string, document interface{}) (driver.DocumentMeta, error) {
|
||||
meta, err := c.internal.ReplaceDocument(ctx, key, document)
|
||||
if err != nil {
|
||||
return meta, err
|
||||
}
|
||||
|
||||
return meta, c.db.bus.PublishUpdate([]driver.DocumentID{meta.ID})
|
||||
}
|
||||
|
||||
func (c Collection) RemoveDocument(ctx context.Context, formatInt string) (driver.DocumentMeta, error) {
|
||||
return c.internal.RemoveDocument(ctx, formatInt)
|
||||
}
|
||||
34
database/busdb/context.go
Normal file
34
database/busdb/context.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package busdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
"github.com/SecurityBrewery/catalyst/role"
|
||||
)
|
||||
|
||||
const (
|
||||
userContextKey = "user"
|
||||
groupContextKey = "groups"
|
||||
)
|
||||
|
||||
func SetContext(ctx *gin.Context, user *models.UserResponse) {
|
||||
user.Roles = role.Strings(role.Explodes(user.Roles))
|
||||
ctx.Set(userContextKey, user)
|
||||
}
|
||||
|
||||
func SetGroupContext(ctx *gin.Context, groups []string) {
|
||||
ctx.Set(groupContextKey, groups)
|
||||
}
|
||||
|
||||
func UserContext(ctx context.Context, user *models.UserResponse) context.Context {
|
||||
user.Roles = role.Strings(role.Explodes(user.Roles))
|
||||
return context.WithValue(ctx, userContextKey, user)
|
||||
}
|
||||
|
||||
func UserFromContext(ctx context.Context) (*models.UserResponse, bool) {
|
||||
u, ok := ctx.Value(userContextKey).(*models.UserResponse)
|
||||
return u, ok
|
||||
}
|
||||
25
database/busdb/keyed.go
Normal file
25
database/busdb/keyed.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package busdb
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
type Keyed struct {
|
||||
Key string
|
||||
Doc interface{}
|
||||
}
|
||||
|
||||
func (p Keyed) MarshalJSON() ([]byte, error) {
|
||||
b, err := json.Marshal(p.Doc)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var m map[string]interface{}
|
||||
err = json.Unmarshal(b, &m)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m["_key"] = p.Key
|
||||
|
||||
return json.Marshal(m)
|
||||
}
|
||||
92
database/busdb/log.go
Normal file
92
database/busdb/log.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package busdb
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/arangodb/go-driver"
|
||||
|
||||
"github.com/SecurityBrewery/catalyst/generated/models"
|
||||
)
|
||||
|
||||
const LogCollectionName = "logs"
|
||||
|
||||
func (db *BusDatabase) LogCreate(ctx context.Context, id, message string) (*models.LogEntry, error) {
|
||||
user, ok := UserFromContext(ctx)
|
||||
if !ok {
|
||||
return nil, errors.New("no user in context")
|
||||
}
|
||||
|
||||
logentry := &models.LogEntry{
|
||||
Reference: id,
|
||||
Created: time.Now(),
|
||||
Creator: user.ID,
|
||||
Message: message,
|
||||
}
|
||||
|
||||
doc := models.LogEntry{}
|
||||
_, err := db.logCollection.CreateDocument(driver.WithReturnNew(ctx, &doc), logentry)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &doc, db.bus.PublishUpdate([]driver.DocumentID{driver.DocumentID(logentry.Reference)})
|
||||
}
|
||||
|
||||
func (db *BusDatabase) LogBatchCreate(ctx context.Context, logEntryForms []*models.LogEntry) error {
|
||||
user, ok := UserFromContext(ctx)
|
||||
if !ok {
|
||||
return errors.New("no user in context")
|
||||
}
|
||||
|
||||
var ids []driver.DocumentID
|
||||
var logentries []*models.LogEntry
|
||||
for _, logEntryForm := range logEntryForms {
|
||||
logentry := &models.LogEntry{
|
||||
Reference: logEntryForm.Reference,
|
||||
Created: time.Now(),
|
||||
Creator: user.ID,
|
||||
Message: logEntryForm.Message,
|
||||
}
|
||||
|
||||
logentries = append(logentries, logentry)
|
||||
ids = append(ids, driver.DocumentID(logentry.Reference))
|
||||
}
|
||||
|
||||
_, errs, err := db.logCollection.CreateDocuments(ctx, logentries)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = errs.FirstNonNil()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return db.bus.PublishUpdate(ids)
|
||||
}
|
||||
|
||||
func (db *BusDatabase) LogList(ctx context.Context, reference string) ([]*models.LogEntry, error) {
|
||||
query := "FOR d IN @@collection FILTER d.reference == @reference SORT d.created DESC RETURN d"
|
||||
cursor, err := db.internal.Query(ctx, query, map[string]interface{}{
|
||||
"@collection": LogCollectionName,
|
||||
"reference": reference,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer cursor.Close()
|
||||
var docs []*models.LogEntry
|
||||
for {
|
||||
var doc models.LogEntry
|
||||
_, err := cursor.ReadDocument(ctx, &doc)
|
||||
if driver.IsNoMoreDocuments(err) {
|
||||
break
|
||||
} else if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
docs = append(docs, &doc)
|
||||
}
|
||||
|
||||
return docs, err
|
||||
}
|
||||
Reference in New Issue
Block a user