mirror of
https://github.com/SecurityBrewery/catalyst.git
synced 2025-12-06 15:22:47 +01:00
Add test for jobs (#34)
This commit is contained in:
@@ -26,11 +26,11 @@ func (b *Bus) PublishRequest(user, f string, ids []driver.DocumentID) error {
|
|||||||
|
|
||||||
func (b *Bus) SubscribeRequest(f func(msg *RequestMsg)) error {
|
func (b *Bus) SubscribeRequest(f func(msg *RequestMsg)) error {
|
||||||
return b.safeSubscribe(b.config.requestKey, ChannelRequest, func(c *emitter.Client, m emitter.Message) {
|
return b.safeSubscribe(b.config.requestKey, ChannelRequest, func(c *emitter.Client, m emitter.Message) {
|
||||||
var msg RequestMsg
|
msg := &RequestMsg{}
|
||||||
if err := json.Unmarshal(m.Payload(), &msg); err != nil {
|
if err := json.Unmarshal(m.Payload(), msg); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go f(&msg)
|
go f(msg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,11 +23,11 @@ func (b *Bus) PublishResult(automation string, data map[string]interface{}, targ
|
|||||||
|
|
||||||
func (b *Bus) SubscribeResult(f func(msg *ResultMsg)) error {
|
func (b *Bus) SubscribeResult(f func(msg *ResultMsg)) error {
|
||||||
return b.safeSubscribe(b.config.resultBusKey, channelResult, func(c *emitter.Client, m emitter.Message) {
|
return b.safeSubscribe(b.config.resultBusKey, channelResult, func(c *emitter.Client, m emitter.Message) {
|
||||||
var msg ResultMsg
|
msg := &ResultMsg{}
|
||||||
if err := json.Unmarshal(m.Payload(), &msg); err != nil {
|
if err := json.Unmarshal(m.Payload(), msg); err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go f(&msg)
|
go f(msg)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ func (db *Database) EnrichArtifact(ctx context.Context, id int64, name string, e
|
|||||||
|
|
||||||
query := `LET d = DOCUMENT(@@collection, @ID)
|
query := `LET d = DOCUMENT(@@collection, @ID)
|
||||||
` + ticketFilterQuery + `
|
` + ticketFilterQuery + `
|
||||||
FOR a IN d.artifacts
|
FOR a IN NOT_NULL(d.artifacts, [])
|
||||||
FILTER a.name == @name
|
FILTER a.name == @name
|
||||||
LET enrichments = NOT_NULL(a.enrichments, {})
|
LET enrichments = NOT_NULL(a.enrichments, {})
|
||||||
LET newenrichments = MERGE(enrichments, ZIP( [@enrichmentname], [@enrichment]) )
|
LET newenrichments = MERGE(enrichments, ZIP( [@enrichmentname], [@enrichment]) )
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ paths:
|
|||||||
schema: { type: array, items: { $ref: "#/definitions/JobResponse" } }
|
schema: { type: array, items: { $ref: "#/definitions/JobResponse" } }
|
||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
- id: "99cd67131b48"
|
- id: "b81c2366-ea37-43d2-b61b-03afdc21d985"
|
||||||
automation: "hash.sha1"
|
automation: "hash.sha1"
|
||||||
payload: "test"
|
payload: "test"
|
||||||
status: "created"
|
status: "created"
|
||||||
@@ -23,9 +23,13 @@ paths:
|
|||||||
summary: "Start a new job"
|
summary: "Start a new job"
|
||||||
operationId: "runJob"
|
operationId: "runJob"
|
||||||
parameters:
|
parameters:
|
||||||
- { name: "job", in: "body", description: "New job", required: true, schema: { $ref: "#/definitions/JobForm" }, x-example: { automation: "hash.sha1", message: { payload: "test" } } }
|
- { name: "job", in: "body", description: "New job", required: true, schema: { $ref: "#/definitions/JobForm" }, x-example: { automation: "hash.sha1", payload: "test" } }
|
||||||
responses:
|
responses:
|
||||||
"204": { description: "successful operation" }
|
"200":
|
||||||
|
description: "successful operation"
|
||||||
|
schema: { $ref: "#/definitions/JobResponse" }
|
||||||
|
examples:
|
||||||
|
test: { id: "87390749-2125-4a87-91c5-da7e3f9bebf1", automation: "hash.sha1", payload: "test", status: "created" }
|
||||||
security: [ { roles: [ "job:write" ] } ]
|
security: [ { roles: [ "job:write" ] } ]
|
||||||
|
|
||||||
/jobs/{id}:
|
/jobs/{id}:
|
||||||
@@ -34,27 +38,27 @@ paths:
|
|||||||
summary: "Get a single job"
|
summary: "Get a single job"
|
||||||
operationId: "getJob"
|
operationId: "getJob"
|
||||||
parameters:
|
parameters:
|
||||||
- { name: "id", in: "path", description: "Job ID", required: true, type: string, x-example: "99cd67131b48" }
|
- { name: "id", in: "path", description: "Job ID", required: true, type: string, x-example: "b81c2366-ea37-43d2-b61b-03afdc21d985" }
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: "successful operation"
|
description: "successful operation"
|
||||||
schema: { $ref: "#/definitions/JobResponse" }
|
schema: { $ref: "#/definitions/JobResponse" }
|
||||||
examples:
|
examples:
|
||||||
test: { id: "99cd67131b48", automation: "hash.sha1", payload: "test", status: "created" }
|
test: { id: "b81c2366-ea37-43d2-b61b-03afdc21d985", automation: "hash.sha1", payload: "test", status: "created" }
|
||||||
security: [ { roles: [ "job:read" ] } ]
|
security: [ { roles: [ "job:read" ] } ]
|
||||||
put:
|
put:
|
||||||
tags: [ "jobs" ]
|
tags: [ "jobs" ]
|
||||||
summary: "Update an existing job"
|
summary: "Update an existing job"
|
||||||
operationId: "updateJob"
|
operationId: "updateJob"
|
||||||
parameters:
|
parameters:
|
||||||
- { name: "id", in: "path", description: "Job ID", required: true, type: string, x-example: "99cd67131b48" }
|
- { name: "id", in: "path", description: "Job ID", required: true, type: string, x-example: "b81c2366-ea37-43d2-b61b-03afdc21d985" }
|
||||||
- { name: "job", in: "body", description: "Job object that needs to be added", required: true, schema: { $ref: "#/definitions/JobUpdate" }, x-example: { status: "failed", running: false } }
|
- { name: "job", in: "body", description: "Job object that needs to be added", required: true, schema: { $ref: "#/definitions/JobUpdate" }, x-example: { status: "failed", running: false } }
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: "successful operation"
|
description: "successful operation"
|
||||||
schema: { $ref: "#/definitions/JobResponse" }
|
schema: { $ref: "#/definitions/JobResponse" }
|
||||||
examples:
|
examples:
|
||||||
test: { id: "99cd67131b48", automation: "hash.sha1", payload: "test", status: "failed" }
|
test: { id: "b81c2366-ea37-43d2-b61b-03afdc21d985", automation: "hash.sha1", payload: "test", status: "failed" }
|
||||||
|
|
||||||
security: [ { roles: [ "job:write" ] } ]
|
security: [ { roles: [ "job:write" ] } ]
|
||||||
|
|
||||||
|
|||||||
@@ -38,7 +38,6 @@ services:
|
|||||||
|
|
||||||
keycloak:
|
keycloak:
|
||||||
image: quay.io/keycloak/keycloak:14.0.0
|
image: quay.io/keycloak/keycloak:14.0.0
|
||||||
platform: linux/amd64
|
|
||||||
environment:
|
environment:
|
||||||
DB_VENDOR: POSTGRES
|
DB_VENDOR: POSTGRES
|
||||||
DB_ADDR: postgres
|
DB_ADDR: postgres
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ type Service interface {
|
|||||||
CurrentUserData(context.Context) (*model.UserDataResponse, error)
|
CurrentUserData(context.Context) (*model.UserDataResponse, error)
|
||||||
UpdateCurrentUserData(context.Context, *model.UserData) (*model.UserDataResponse, error)
|
UpdateCurrentUserData(context.Context, *model.UserData) (*model.UserDataResponse, error)
|
||||||
ListJobs(context.Context) ([]*model.JobResponse, error)
|
ListJobs(context.Context) ([]*model.JobResponse, error)
|
||||||
RunJob(context.Context, *model.JobForm) error
|
RunJob(context.Context, *model.JobForm) (*model.JobResponse, error)
|
||||||
GetJob(context.Context, string) (*model.JobResponse, error)
|
GetJob(context.Context, string) (*model.JobResponse, error)
|
||||||
UpdateJob(context.Context, string, *model.JobUpdate) (*model.JobResponse, error)
|
UpdateJob(context.Context, string, *model.JobUpdate) (*model.JobResponse, error)
|
||||||
GetLogs(context.Context, string) ([]*model.LogEntry, error)
|
GetLogs(context.Context, string) ([]*model.LogEntry, error)
|
||||||
@@ -346,7 +346,8 @@ func (s *server) runJobHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response(w, nil, s.service.RunJob(r.Context(), jobP))
|
result, err := s.service.RunJob(r.Context(), jobP)
|
||||||
|
response(w, result, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *server) getJobHandler(w http.ResponseWriter, r *http.Request) {
|
func (s *server) getJobHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
@@ -95,34 +95,34 @@ var Tests = []struct {
|
|||||||
Args: Args{Method: "Get", URL: "/jobs"},
|
Args: Args{Method: "Get", URL: "/jobs"},
|
||||||
Want: Want{
|
Want: Want{
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Body: []interface{}{map[string]interface{}{"automation": "hash.sha1", "id": "99cd67131b48", "payload": "test", "status": "created"}},
|
Body: []interface{}{map[string]interface{}{"automation": "hash.sha1", "id": "b81c2366-ea37-43d2-b61b-03afdc21d985", "payload": "test", "status": "created"}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "RunJob",
|
Name: "RunJob",
|
||||||
Args: Args{Method: "Post", URL: "/jobs", Data: map[string]interface{}{"automation": "hash.sha1", "message": map[string]interface{}{"payload": "test"}}},
|
Args: Args{Method: "Post", URL: "/jobs", Data: map[string]interface{}{"automation": "hash.sha1", "payload": "test"}},
|
||||||
Want: Want{
|
Want: Want{
|
||||||
Status: 204,
|
Status: 200,
|
||||||
Body: nil,
|
Body: map[string]interface{}{"automation": "hash.sha1", "id": "87390749-2125-4a87-91c5-da7e3f9bebf1", "payload": "test", "status": "created"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "GetJob",
|
Name: "GetJob",
|
||||||
Args: Args{Method: "Get", URL: "/jobs/99cd67131b48"},
|
Args: Args{Method: "Get", URL: "/jobs/b81c2366-ea37-43d2-b61b-03afdc21d985"},
|
||||||
Want: Want{
|
Want: Want{
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Body: map[string]interface{}{"automation": "hash.sha1", "id": "99cd67131b48", "payload": "test", "status": "created"},
|
Body: map[string]interface{}{"automation": "hash.sha1", "id": "b81c2366-ea37-43d2-b61b-03afdc21d985", "payload": "test", "status": "created"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
{
|
{
|
||||||
Name: "UpdateJob",
|
Name: "UpdateJob",
|
||||||
Args: Args{Method: "Put", URL: "/jobs/99cd67131b48", Data: map[string]interface{}{"running": false, "status": "failed"}},
|
Args: Args{Method: "Put", URL: "/jobs/b81c2366-ea37-43d2-b61b-03afdc21d985", Data: map[string]interface{}{"running": false, "status": "failed"}},
|
||||||
Want: Want{
|
Want: Want{
|
||||||
Status: 200,
|
Status: 200,
|
||||||
Body: map[string]interface{}{"automation": "hash.sha1", "id": "99cd67131b48", "payload": "test", "status": "failed"},
|
Body: map[string]interface{}{"automation": "hash.sha1", "id": "b81c2366-ea37-43d2-b61b-03afdc21d985", "payload": "test", "status": "failed"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -556,7 +556,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : [ {
|
"example" : [ {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "created"
|
"status" : "created"
|
||||||
} ]
|
} ]
|
||||||
@@ -585,8 +585,22 @@
|
|||||||
"required" : true
|
"required" : true
|
||||||
},
|
},
|
||||||
"responses" : {
|
"responses" : {
|
||||||
"204" : {
|
"200" : {
|
||||||
"content" : { },
|
"content" : {
|
||||||
|
"application/json" : {
|
||||||
|
"schema" : {
|
||||||
|
"$ref" : "#/components/schemas/JobResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test" : {
|
||||||
|
"example" : {
|
||||||
|
"automation" : "hash.sha1",
|
||||||
|
"id" : "87390749-2125-4a87-91c5-da7e3f9bebf1",
|
||||||
|
"payload" : "test",
|
||||||
|
"status" : "created"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"description" : "successful operation"
|
"description" : "successful operation"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -603,7 +617,7 @@
|
|||||||
"operationId" : "getJob",
|
"operationId" : "getJob",
|
||||||
"parameters" : [ {
|
"parameters" : [ {
|
||||||
"description" : "Job ID",
|
"description" : "Job ID",
|
||||||
"example" : "99cd67131b48",
|
"example" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"in" : "path",
|
"in" : "path",
|
||||||
"name" : "id",
|
"name" : "id",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
@@ -622,7 +636,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : {
|
"example" : {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "created"
|
"status" : "created"
|
||||||
}
|
}
|
||||||
@@ -641,7 +655,7 @@
|
|||||||
"operationId" : "updateJob",
|
"operationId" : "updateJob",
|
||||||
"parameters" : [ {
|
"parameters" : [ {
|
||||||
"description" : "Job ID",
|
"description" : "Job ID",
|
||||||
"example" : "99cd67131b48",
|
"example" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"in" : "path",
|
"in" : "path",
|
||||||
"name" : "id",
|
"name" : "id",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
@@ -671,7 +685,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : {
|
"example" : {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "failed"
|
"status" : "failed"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1791,7 +1791,7 @@ paths:
|
|||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
- automation: hash.sha1
|
- automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: created
|
status: created
|
||||||
schema:
|
schema:
|
||||||
@@ -1815,11 +1815,18 @@ paths:
|
|||||||
$ref: '#/definitions/JobForm'
|
$ref: '#/definitions/JobForm'
|
||||||
x-example:
|
x-example:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
message:
|
payload: test
|
||||||
payload: test
|
|
||||||
responses:
|
responses:
|
||||||
"204":
|
"200":
|
||||||
description: successful operation
|
description: successful operation
|
||||||
|
examples:
|
||||||
|
test:
|
||||||
|
automation: hash.sha1
|
||||||
|
id: 87390749-2125-4a87-91c5-da7e3f9bebf1
|
||||||
|
payload: test
|
||||||
|
status: created
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/JobResponse'
|
||||||
security:
|
security:
|
||||||
- roles:
|
- roles:
|
||||||
- job:write
|
- job:write
|
||||||
@@ -1835,14 +1842,14 @@ paths:
|
|||||||
name: id
|
name: id
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
x-example: 99cd67131b48
|
x-example: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: successful operation
|
description: successful operation
|
||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: created
|
status: created
|
||||||
schema:
|
schema:
|
||||||
@@ -1861,7 +1868,7 @@ paths:
|
|||||||
name: id
|
name: id
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
x-example: 99cd67131b48
|
x-example: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
- description: Job object that needs to be added
|
- description: Job object that needs to be added
|
||||||
in: body
|
in: body
|
||||||
name: job
|
name: job
|
||||||
@@ -1877,7 +1884,7 @@ paths:
|
|||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: failed
|
status: failed
|
||||||
schema:
|
schema:
|
||||||
|
|||||||
@@ -324,7 +324,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : [ {
|
"example" : [ {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "created"
|
"status" : "created"
|
||||||
} ]
|
} ]
|
||||||
@@ -353,8 +353,22 @@
|
|||||||
"required" : true
|
"required" : true
|
||||||
},
|
},
|
||||||
"responses" : {
|
"responses" : {
|
||||||
"204" : {
|
"200" : {
|
||||||
"content" : { },
|
"content" : {
|
||||||
|
"application/json" : {
|
||||||
|
"schema" : {
|
||||||
|
"$ref" : "#/components/schemas/JobResponse"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"test" : {
|
||||||
|
"example" : {
|
||||||
|
"automation" : "hash.sha1",
|
||||||
|
"id" : "87390749-2125-4a87-91c5-da7e3f9bebf1",
|
||||||
|
"payload" : "test",
|
||||||
|
"status" : "created"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
"description" : "successful operation"
|
"description" : "successful operation"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -371,7 +385,7 @@
|
|||||||
"operationId" : "getJob",
|
"operationId" : "getJob",
|
||||||
"parameters" : [ {
|
"parameters" : [ {
|
||||||
"description" : "Job ID",
|
"description" : "Job ID",
|
||||||
"example" : "99cd67131b48",
|
"example" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"in" : "path",
|
"in" : "path",
|
||||||
"name" : "id",
|
"name" : "id",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
@@ -390,7 +404,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : {
|
"example" : {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "created"
|
"status" : "created"
|
||||||
}
|
}
|
||||||
@@ -409,7 +423,7 @@
|
|||||||
"operationId" : "updateJob",
|
"operationId" : "updateJob",
|
||||||
"parameters" : [ {
|
"parameters" : [ {
|
||||||
"description" : "Job ID",
|
"description" : "Job ID",
|
||||||
"example" : "99cd67131b48",
|
"example" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"in" : "path",
|
"in" : "path",
|
||||||
"name" : "id",
|
"name" : "id",
|
||||||
"required" : true,
|
"required" : true,
|
||||||
@@ -439,7 +453,7 @@
|
|||||||
"test" : {
|
"test" : {
|
||||||
"example" : {
|
"example" : {
|
||||||
"automation" : "hash.sha1",
|
"automation" : "hash.sha1",
|
||||||
"id" : "99cd67131b48",
|
"id" : "b81c2366-ea37-43d2-b61b-03afdc21d985",
|
||||||
"payload" : "test",
|
"payload" : "test",
|
||||||
"status" : "failed"
|
"status" : "failed"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1520,7 +1520,7 @@ paths:
|
|||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
- automation: hash.sha1
|
- automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: created
|
status: created
|
||||||
schema:
|
schema:
|
||||||
@@ -1544,11 +1544,18 @@ paths:
|
|||||||
$ref: '#/definitions/JobForm'
|
$ref: '#/definitions/JobForm'
|
||||||
x-example:
|
x-example:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
message:
|
payload: test
|
||||||
payload: test
|
|
||||||
responses:
|
responses:
|
||||||
"204":
|
"200":
|
||||||
description: successful operation
|
description: successful operation
|
||||||
|
examples:
|
||||||
|
test:
|
||||||
|
automation: hash.sha1
|
||||||
|
id: 87390749-2125-4a87-91c5-da7e3f9bebf1
|
||||||
|
payload: test
|
||||||
|
status: created
|
||||||
|
schema:
|
||||||
|
$ref: '#/definitions/JobResponse'
|
||||||
security:
|
security:
|
||||||
- roles:
|
- roles:
|
||||||
- job:write
|
- job:write
|
||||||
@@ -1564,14 +1571,14 @@ paths:
|
|||||||
name: id
|
name: id
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
x-example: 99cd67131b48
|
x-example: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
responses:
|
responses:
|
||||||
"200":
|
"200":
|
||||||
description: successful operation
|
description: successful operation
|
||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: created
|
status: created
|
||||||
schema:
|
schema:
|
||||||
@@ -1590,7 +1597,7 @@ paths:
|
|||||||
name: id
|
name: id
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
x-example: 99cd67131b48
|
x-example: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
- description: Job object that needs to be added
|
- description: Job object that needs to be added
|
||||||
in: body
|
in: body
|
||||||
name: job
|
name: job
|
||||||
@@ -1606,7 +1613,7 @@ paths:
|
|||||||
examples:
|
examples:
|
||||||
test:
|
test:
|
||||||
automation: hash.sha1
|
automation: hash.sha1
|
||||||
id: 99cd67131b48
|
id: b81c2366-ea37-43d2-b61b-03afdc21d985
|
||||||
payload: test
|
payload: test
|
||||||
status: failed
|
status: failed
|
||||||
schema:
|
schema:
|
||||||
|
|||||||
@@ -26,12 +26,20 @@ func (s *Service) ListJobs(ctx context.Context) ([]*model.JobResponse, error) {
|
|||||||
return s.database.JobList(ctx)
|
return s.database.JobList(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) RunJob(ctx context.Context, form *model.JobForm) (err error) {
|
func (s *Service) RunJob(ctx context.Context, form *model.JobForm) (doc *model.JobResponse, err error) {
|
||||||
msgContext := &model.Context{}
|
msgContext := &model.Context{}
|
||||||
newJobID := uuid.NewString()
|
newJobID := uuid.NewString()
|
||||||
|
|
||||||
defer s.publishRequest(ctx, err, "RunJob", jobID(newJobID))
|
defer s.publishRequest(ctx, err, "RunJob", jobID(newJobID))
|
||||||
return s.bus.PublishJob(newJobID, form.Automation, form.Payload, msgContext, form.Origin)
|
err = s.bus.PublishJob(newJobID, form.Automation, form.Payload, msgContext, form.Origin)
|
||||||
|
|
||||||
|
return &model.JobResponse{
|
||||||
|
Automation: form.Automation,
|
||||||
|
ID: newJobID,
|
||||||
|
Origin: form.Origin,
|
||||||
|
Payload: form.Payload,
|
||||||
|
Status: "published",
|
||||||
|
}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Service) GetJob(ctx context.Context, id string) (*model.JobResponse, error) {
|
func (s *Service) GetJob(ctx context.Context, id string) (*model.JobResponse, error) {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
@@ -26,6 +27,10 @@ import (
|
|||||||
func TestBackupAndRestore(t *testing.T) {
|
func TestBackupAndRestore(t *testing.T) {
|
||||||
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
|
||||||
|
if runtime.GOARCH == "arm64" {
|
||||||
|
t.Skip("test does not run on arm")
|
||||||
|
}
|
||||||
|
|
||||||
type want struct {
|
type want struct {
|
||||||
status int
|
status int
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,47 +3,80 @@ package test
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-chi/chi"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/tidwall/gjson"
|
||||||
|
|
||||||
"github.com/SecurityBrewery/catalyst/generated/model"
|
"github.com/SecurityBrewery/catalyst/generated/model"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestJob(t *testing.T) {
|
func TestJob(t *testing.T) {
|
||||||
_, _, _, _, _, _, _, server, cleanup, err := Server(t)
|
log.SetFlags(log.LstdFlags | log.Lshortfile)
|
||||||
|
|
||||||
|
_, _, server, err := Catalyst(t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer cleanup()
|
|
||||||
|
|
||||||
// server.ConfigureRoutes()
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
|
|
||||||
// setup request
|
|
||||||
var req *http.Request
|
|
||||||
b, err := json.Marshal(model.JobForm{
|
b, err := json.Marshal(model.JobForm{
|
||||||
Automation: "hash.sha1",
|
Automation: "hash.sha1",
|
||||||
Origin: nil,
|
Payload: map[string]interface{}{"default": "test"},
|
||||||
Payload: nil,
|
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
result := request(t, server.Server, http.MethodPost, "/api/jobs", bytes.NewBuffer(b))
|
||||||
|
id := gjson.GetBytes(result, "id").String()
|
||||||
|
|
||||||
req = httptest.NewRequest(http.MethodPost, "/jobs", bytes.NewBuffer(b))
|
start := time.Now()
|
||||||
|
for {
|
||||||
|
time.Sleep(5 * time.Second)
|
||||||
|
|
||||||
|
if time.Since(start) > time.Minute {
|
||||||
|
t.Fatal("job did not complete within a minute")
|
||||||
|
}
|
||||||
|
|
||||||
|
job := request(t, server.Server, http.MethodGet, "/api/jobs/"+id, nil)
|
||||||
|
|
||||||
|
status := gjson.GetBytes(job, "status").String()
|
||||||
|
if status != "completed" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
output := gjson.GetBytes(job, "output.hash").String()
|
||||||
|
assert.Equal(t, "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3", output)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func request(t *testing.T, server chi.Router, method, url string, data io.Reader) []byte {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
|
||||||
|
// setup request
|
||||||
|
req := httptest.NewRequest(method, url, data)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
req.Header.Set("PRIVATE-TOKEN", "test")
|
||||||
|
|
||||||
// run request
|
// run request
|
||||||
server.ServeHTTP(w, req)
|
server.ServeHTTP(w, req)
|
||||||
|
|
||||||
result := w.Result()
|
result := w.Result()
|
||||||
|
|
||||||
// assert results
|
b, err := io.ReadAll(result.Body)
|
||||||
if result.StatusCode != http.StatusNoContent {
|
if err != nil {
|
||||||
t.Fatalf("Status got = %v, want %v", result.Status, http.StatusNoContent)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
// if tt.want.status != http.StatusNoContent {
|
|
||||||
// jsonEqual(t, result.Body, tt.want.body)
|
if result.StatusCode != http.StatusOK {
|
||||||
// }
|
t.Fatalf("Status got = %v: %v, want %v", result.Status, string(b), http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
return b
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,13 +67,13 @@ func TestServer(t *testing.T) {
|
|||||||
t.Fatalf("Status got = %v (%s), want %v", result.Status, msg, tt.Want.Status)
|
t.Fatalf("Status got = %v (%s), want %v", result.Status, msg, tt.Want.Status)
|
||||||
}
|
}
|
||||||
if tt.Want.Status != http.StatusNoContent {
|
if tt.Want.Status != http.StatusNoContent {
|
||||||
jsonEqual(t, result.Body, tt.Want.Body)
|
jsonEqual(t, tt.Name, result.Body, tt.Want.Body)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func jsonEqual(t *testing.T, got io.Reader, want interface{}) {
|
func jsonEqual(t *testing.T, name string, got io.Reader, want interface{}) {
|
||||||
var gotObject, wantObject interface{}
|
var gotObject, wantObject interface{}
|
||||||
|
|
||||||
// load bytes
|
// load bytes
|
||||||
@@ -86,7 +86,15 @@ func jsonEqual(t *testing.T, got io.Reader, want interface{}) {
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fields := []string{"secret"}
|
var fields []string
|
||||||
|
|
||||||
|
if name == "CreateUser" {
|
||||||
|
fields = append(fields, "secret")
|
||||||
|
}
|
||||||
|
if name == "RunJob" {
|
||||||
|
fields = append(fields, "id", "status")
|
||||||
|
}
|
||||||
|
|
||||||
for _, field := range fields {
|
for _, field := range fields {
|
||||||
gField := gjson.GetBytes(wantBytes, field)
|
gField := gjson.GetBytes(wantBytes, field)
|
||||||
if gField.Exists() && gjson.GetBytes(gotBytes, field).Exists() {
|
if gField.Exists() && gjson.GetBytes(gotBytes, field).Exists() {
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ func DB(t *testing.T) (context.Context, *catalyst.Config, *bus.Bus, *index.Index
|
|||||||
return nil, nil, nil, nil, nil, nil, nil, err
|
return nil, nil, nil, nil, nil, nil, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = db.JobCreate(ctx, "99cd67131b48", &model.JobForm{
|
_, err = db.JobCreate(ctx, "b81c2366-ea37-43d2-b61b-03afdc21d985", &model.JobForm{
|
||||||
Automation: "hash.sha1",
|
Automation: "hash.sha1",
|
||||||
Payload: "test",
|
Payload: "test",
|
||||||
Origin: nil,
|
Origin: nil,
|
||||||
@@ -201,6 +201,7 @@ func Catalyst(t *testing.T) (context.Context, *catalyst.Config, *catalyst.Server
|
|||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
config.DB.Name = cleanName(t)
|
config.DB.Name = cleanName(t)
|
||||||
|
config.IndexPath = cleanName(t) + ".bleve"
|
||||||
|
|
||||||
c, err := catalyst.New(&hooks.Hooks{
|
c, err := catalyst.New(&hooks.Hooks{
|
||||||
DatabaseAfterConnectFuncs: []func(ctx context.Context, client driver.Client, name string){Clear},
|
DatabaseAfterConnectFuncs: []func(ctx context.Context, client driver.Client, name string){Clear},
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ func TestUser(t *testing.T) {
|
|||||||
t.Fatalf("Status got = %v, want %v", result.Status, tt.want.status)
|
t.Fatalf("Status got = %v, want %v", result.Status, tt.want.status)
|
||||||
}
|
}
|
||||||
if tt.want.status != http.StatusNoContent {
|
if tt.want.status != http.StatusNoContent {
|
||||||
jsonEqual(t, result.Body, tt.want.body)
|
jsonEqual(t, tt.name, result.Body, tt.want.body)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3303,7 +3303,7 @@ export const JobsApiFp = function(configuration?: Configuration) {
|
|||||||
* @param {*} [options] Override http request option.
|
* @param {*} [options] Override http request option.
|
||||||
* @throws {RequiredError}
|
* @throws {RequiredError}
|
||||||
*/
|
*/
|
||||||
async runJob(job: JobForm, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<void>> {
|
async runJob(job: JobForm, options?: AxiosRequestConfig): Promise<(axios?: AxiosInstance, basePath?: string) => AxiosPromise<JobResponse>> {
|
||||||
const localVarAxiosArgs = await localVarAxiosParamCreator.runJob(job, options);
|
const localVarAxiosArgs = await localVarAxiosParamCreator.runJob(job, options);
|
||||||
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
|
return createRequestFunction(localVarAxiosArgs, globalAxios, BASE_PATH, configuration);
|
||||||
},
|
},
|
||||||
@@ -3355,7 +3355,7 @@ export const JobsApiFactory = function (configuration?: Configuration, basePath?
|
|||||||
* @param {*} [options] Override http request option.
|
* @param {*} [options] Override http request option.
|
||||||
* @throws {RequiredError}
|
* @throws {RequiredError}
|
||||||
*/
|
*/
|
||||||
runJob(job: JobForm, options?: any): AxiosPromise<void> {
|
runJob(job: JobForm, options?: any): AxiosPromise<JobResponse> {
|
||||||
return localVarFp.runJob(job, options).then((request) => request(axios, basePath));
|
return localVarFp.runJob(job, options).then((request) => request(axios, basePath));
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user