From df289eaf3fb185b034212f501533e7f01a6108e7 Mon Sep 17 00:00:00 2001 From: jkennedyvz <65985482+jkennedyvz@users.noreply.github.com> Date: Wed, 8 Nov 2023 21:08:07 -0800 Subject: [PATCH] Sync Tn/add op vars (#976) * add global variables table * set up global vars ui and backend * add tests / test data * remove logs * fix tests * resolve TODOS * add button to add new variables * remove pagination * change text * clean up outdated comments * add user_vars and vars_user_map tables * update copyright * remove ID from DTO * delete unneeded permissions * shorten name for brevity * shorten name for brevity lowercase * setup backend for op vars * set up front end routing * add table for op vars * fix column issues * add 'add var' button (no css) * add seed data for op vars * fix tests * fix delete Op test * add op var tests * remove old todos * fix button css * remove duplicate line * fix typo * fix duplicate name bug * ensure operation names are unique in an operation * get similar errors for modifying op var * fix delete bug * update tests to match new functionality * make vars capitlized no spaces * fix tests * send variables to services (#950) --------- Co-authored-by: Tyler Noblett Co-authored-by: Tyler Noblett --- .../enhancementservices/event_new_evidence.go | 54 +++++++++++++++++-- .../src/helpers/request_validation.ts | 2 + .../src/helpers/request_validation.ts | 2 + 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/backend/enhancementservices/event_new_evidence.go b/backend/enhancementservices/event_new_evidence.go index e9142889d..e51a81a66 100644 --- a/backend/enhancementservices/event_new_evidence.go +++ b/backend/enhancementservices/event_new_evidence.go @@ -8,6 +8,7 @@ import ( "fmt" "github.com/ashirt-ops/ashirt-server/backend/database" + "github.com/ashirt-ops/ashirt-server/backend/dtos" "github.com/ashirt-ops/ashirt-server/backend/helpers" "github.com/ashirt-ops/ashirt-server/backend/models" @@ -15,10 +16,12 @@ import ( ) type NewEvidencePayload struct { - Type string `json:"type" db:"type"` - EvidenceUUID string `json:"evidenceUuid" db:"uuid"` - OperationSlug string `json:"operationSlug" db:"operation_slug"` - ContentType string `json:"contentType" db:"content_type"` + Type string `json:"type" db:"type"` + EvidenceUUID string `json:"evidenceUuid" db:"uuid"` + OperationSlug string `json:"operationSlug" db:"operation_slug"` + ContentType string `json:"contentType" db:"content_type"` + GlobalVariables []dtos.GlobalVar `json:"globalVariables"` + OperationVars []dtos.OperationVar `json:"operationVariables"` } type ExpandedNewEvidencePayload struct { @@ -78,7 +81,21 @@ func batchBuildNewEvidencePayloadSpecial(ctx context.Context, db database.Connec func batchBuildNewEvidencePayloadFromIDs(db database.ConnectionProxy, evidenceIDs []int64) ([]ExpandedNewEvidencePayload, error) { var payloads []ExpandedNewEvidencePayload - err := db.Select(&payloads, sq.Select( + var globalVariables []models.GlobalVar + + err := db.Select(&globalVariables, sq.Select("name", "value").From("global_vars")) + if err != nil { + return nil, fmt.Errorf("unable to gather global variables for worker") + } + var globalVariablesDTO []dtos.GlobalVar + for _, v := range globalVariables { + globalVariablesDTO = append(globalVariablesDTO, dtos.GlobalVar{ + Name: v.Name, + Value: v.Value, + }) + } + + err = db.Select(&payloads, sq.Select( "e.id AS id", "e.uuid AS uuid", "e.content_type", @@ -95,5 +112,32 @@ func batchBuildNewEvidencePayloadFromIDs(db database.ConnectionProxy, evidenceID return nil, fmt.Errorf("unable to gather evidence data for worker") } + for i := range payloads { + var operationVariables []models.OperationVar + + err := db.Select(&operationVariables, + sq.Select("ov.name", "ov.value", "ov.slug"). + From("operation_vars ov"). + Join("var_operation_map vom ON ov.id = vom.var_id"). + Join("operations o ON o.id = vom.operation_id"). + Where(sq.Eq{"o.slug": payloads[i].OperationSlug})) + if err != nil { + return nil, fmt.Errorf("unable to gather operation variables for worker") + } + + var operationVariablesDTO []dtos.OperationVar + for _, v := range operationVariables { + operationVariablesDTO = append(operationVariablesDTO, dtos.OperationVar{ + Name: v.Name, + Value: v.Value, + VarSlug: v.Slug, + OperationSlug: payloads[i].OperationSlug, + }) + } + + payloads[i].OperationVars = operationVariablesDTO + payloads[i].GlobalVariables = globalVariablesDTO + } + return payloads, nil } diff --git a/enhancement_worker_templates/web/typescript_express/src/helpers/request_validation.ts b/enhancement_worker_templates/web/typescript_express/src/helpers/request_validation.ts index 2d3f9d118..87afc5390 100644 --- a/enhancement_worker_templates/web/typescript_express/src/helpers/request_validation.ts +++ b/enhancement_worker_templates/web/typescript_express/src/helpers/request_validation.ts @@ -12,6 +12,8 @@ export type EvidenceCreatedMessage = { evidenceUuid: string, operationSlug: string, contentType: typeof SupportedContentTypes[number] + globalVariables: Record[] + operationVariables: Record[] } /** diff --git a/enhancement_workers/demo-tesseract/src/helpers/request_validation.ts b/enhancement_workers/demo-tesseract/src/helpers/request_validation.ts index 539ddde79..f9d9912c4 100644 --- a/enhancement_workers/demo-tesseract/src/helpers/request_validation.ts +++ b/enhancement_workers/demo-tesseract/src/helpers/request_validation.ts @@ -12,6 +12,8 @@ export type EvidenceCreatedMessage = { evidenceUuid: string, operationSlug: string, contentType: typeof SupportedContentTypes[number] + globalVariables: Record[] + operationVariables: Record[] } /**