Skip to content

Commit

Permalink
fix: fix get audit job
Browse files Browse the repository at this point in the history
  • Loading branch information
artaasadi committed Dec 17, 2024
1 parent 302d6d1 commit bf92c36
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 85 deletions.
85 changes: 36 additions & 49 deletions services/compliance/http_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ func (h *HttpHandler) Register(e *echo.Echo) {
v3.GET("/quick/sequence/:run_id", httpserver2.AuthorizeHandler(h.GetQuickSequenceSummary, authApi.ViewerRole))

v3.GET("/compliance/report/:run_id", httpserver2.AuthorizeHandler(h.GetComplianceJobReport, authApi.ViewerRole))
v3.GET("/job-report/:run_id/summary", httpserver2.AuthorizeHandler(h.GetComplianceJobReport, authApi.ViewerRole))
}

func bindValidate(ctx echo.Context, i any) error {
Expand Down Expand Up @@ -7665,45 +7666,38 @@ func (h HttpHandler) GetQuickScanSummary(c echo.Context) error {

jobId := c.Param("run_id")
view := c.QueryParam("view")
withIncidentsStr := c.QueryParam("with_incidents")
controls := httpserver2.QueryArrayParam(c, "controls")

if view == "" {
view = "control"
}

withIncidents := false
if withIncidentsStr == "true" {
withIncidents = true
complianceJob, err := h.schedulerClient.GetComplianceJobStatus(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get compliance job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get compliance job")
}
complianceJob, err := h.schedulerClient.GetComplianceJobStatus(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get compliance job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get compliance job")
}
if complianceJob.JobStatus == string(schedulerapi.ComplianceJobFailed) {
return echo.NewHTTPError(http.StatusBadRequest, "job has been failed")
} else if complianceJob.JobStatus == string(schedulerapi.ComplianceJobTimeout) {
return echo.NewHTTPError(http.StatusBadRequest, "job has been timed out")
} else if complianceJob.JobStatus == string(schedulerapi.ComplianceJobRunnersInProgress) ||
complianceJob.JobStatus == string(schedulerapi.ComplianceJobCreated) ||
complianceJob.JobStatus == string(schedulerapi.ComplianceJobSummarizerInProgress) {
return echo.NewHTTPError(http.StatusBadRequest, "job is in progress")
}
if complianceJob.WithIncidents {
if complianceJob.SummaryJobId == nil {
return echo.NewHTTPError(http.StatusBadRequest, "compliance job not summarized yet")
}
jobId = strconv.Itoa(int(*complianceJob.SummaryJobId))
} else {
auditJob, err := h.schedulerClient.GetComplianceQuickRun(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get audit job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job")
}
if auditJob.Status == schedulerapi.ComplianceJobFailed {
return echo.NewHTTPError(http.StatusBadRequest, "job has been failed")
} else if auditJob.Status == schedulerapi.ComplianceJobTimeout {
return echo.NewHTTPError(http.StatusBadRequest, "job has been timed out")
} else if auditJob.Status == schedulerapi.ComplianceJobRunnersInProgress || auditJob.Status == schedulerapi.ComplianceJobCreated || auditJob.Status == schedulerapi.ComplianceJobSummarizerInProgress {
return echo.NewHTTPError(http.StatusBadRequest, "job is in progress")
}
}

var result api.AuditSummary

switch view {
case "resource", "resources":
summary, err := es.GetJobReportResourceViewByJobID(c.Request().Context(), h.logger, h.client, jobId, withIncidents)
summary, err := es.GetJobReportResourceViewByJobID(c.Request().Context(), h.logger, h.client, jobId, complianceJob.WithIncidents)
if err != nil {
h.logger.Error("failed to get audit job summary", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job summary")
Expand All @@ -7717,7 +7711,7 @@ func (h HttpHandler) GetQuickScanSummary(c echo.Context) error {
JobSummary: summary.JobSummary,
}
case "control", "controls":
summary, err := es.GetJobReportControlViewByJobID(c.Request().Context(), h.logger, h.client, jobId, withIncidents, controls)
summary, err := es.GetJobReportControlViewByJobID(c.Request().Context(), h.logger, h.client, jobId, complianceJob.WithIncidents, controls)
if err != nil {
h.logger.Error("failed to get audit job summary", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job summary")
Expand All @@ -7731,12 +7725,12 @@ func (h HttpHandler) GetQuickScanSummary(c echo.Context) error {
JobSummary: summary.JobSummary,
}
case "both":
controlSummary, err := es.GetJobReportControlViewByJobID(c.Request().Context(), h.logger, h.client, jobId, withIncidents, controls)
controlSummary, err := es.GetJobReportControlViewByJobID(c.Request().Context(), h.logger, h.client, jobId, complianceJob.WithIncidents, controls)
if err != nil {
h.logger.Error("failed to get audit job summary", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job summary")
}
resourceSummary, err := es.GetJobReportResourceViewByJobID(c.Request().Context(), h.logger, h.client, jobId, withIncidents)
resourceSummary, err := es.GetJobReportResourceViewByJobID(c.Request().Context(), h.logger, h.client, jobId, complianceJob.WithIncidents)
if err != nil {
h.logger.Error("failed to get audit job summary", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job summary")
Expand Down Expand Up @@ -7868,37 +7862,30 @@ func (h HttpHandler) GetComplianceJobReport(c echo.Context) error {

jobId := c.Param("run_id")
controls := httpserver2.QueryArrayParam(c, "controls")
withIncidentsStr := c.QueryParam("with_incidents")

withIncidents := false
if withIncidentsStr == "true" {
withIncidents = true
complianceJob, err := h.schedulerClient.GetComplianceJobStatus(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get compliance job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get compliance job")
}
complianceJob, err := h.schedulerClient.GetComplianceJobStatus(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get compliance job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get compliance job")
}
if complianceJob.JobStatus == string(schedulerapi.ComplianceJobFailed) {
return echo.NewHTTPError(http.StatusBadRequest, "job has been failed")
} else if complianceJob.JobStatus == string(schedulerapi.ComplianceJobTimeout) {
return echo.NewHTTPError(http.StatusBadRequest, "job has been timed out")
} else if complianceJob.JobStatus == string(schedulerapi.ComplianceJobRunnersInProgress) ||
complianceJob.JobStatus == string(schedulerapi.ComplianceJobCreated) ||
complianceJob.JobStatus == string(schedulerapi.ComplianceJobSummarizerInProgress) {
return echo.NewHTTPError(http.StatusBadRequest, "job is in progress")
}
if complianceJob.WithIncidents {
if complianceJob.SummaryJobId == nil {
return echo.NewHTTPError(http.StatusBadRequest, "compliance job not summarized yet")
}
jobId = strconv.Itoa(int(*complianceJob.SummaryJobId))
} else {
auditJob, err := h.schedulerClient.GetComplianceQuickRun(clientCtx, jobId)
if err != nil {
h.logger.Error("failed to get audit job", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get audit job")
}
if auditJob.Status == schedulerapi.ComplianceJobFailed {
return echo.NewHTTPError(http.StatusBadRequest, "job has been failed")
} else if auditJob.Status == schedulerapi.ComplianceJobTimeout {
return echo.NewHTTPError(http.StatusBadRequest, "job has been timed out")
} else if auditJob.Status == schedulerapi.ComplianceJobRunnersInProgress || auditJob.Status == schedulerapi.ComplianceJobCreated || auditJob.Status == schedulerapi.ComplianceJobSummarizerInProgress {
return echo.NewHTTPError(http.StatusBadRequest, "job is in progress")
}
}

summary, err := es.GetJobReportControlSummaryByJobID(c.Request().Context(), h.logger, h.client,
jobId, withIncidents, controls)
jobId, complianceJob.WithIncidents, controls)
if err != nil {
h.logger.Error("failed to get job report control summary by job id", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get job report control summary by job id")
Expand Down
1 change: 1 addition & 0 deletions services/describe/api/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ type GetDescribeJobStatusResponse struct {

type GetComplianceJobStatusResponse struct {
JobId uint `json:"job_id"`
WithIncidents bool `json:"with_incidents"`
SummaryJobId *uint `json:"summary_job_id"`
IntegrationInfo IntegrationInfo `json:"integration_info"`
JobStatus string `json:"job_status"`
Expand Down
1 change: 0 additions & 1 deletion services/describe/client/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ type SchedulerServiceClient interface {
ListComplianceJobsHistory(ctx *httpclient.Context, interval, triggerType, createdBy string, cursor, perPage int) (*api.ListComplianceJobsHistoryResponse, error)
GetSummaryJobs(ctx *httpclient.Context, jobIDs []string) ([]string, error)
GetIntegrationLastDiscoveryJob(ctx *httpclient.Context, request api.GetIntegrationLastDiscoveryJobRequest) (*model.DescribeIntegrationJob, error)
GetComplianceQuickRun(ctx *httpclient.Context, jobID string) (*api.ComplianceJob, error)
GetComplianceQuickSequence(ctx *httpclient.Context, jobID string) (*api.QuickScanSequence, error)
GetComplianceJobStatus(ctx *httpclient.Context, jobId string) (*api.GetComplianceJobStatusResponse, error)
}
Expand Down
39 changes: 4 additions & 35 deletions services/describe/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (h HttpServer) Register(e *echo.Echo) {

v3.GET("/integration/discovery/last-job", httpserver.AuthorizeHandler(h.GetIntegrationLastDiscoveryJob, apiAuth.ViewerRole))

v3.GET("/compliance/quick/:run_id", httpserver.AuthorizeHandler(h.GetComplianceQuickRun, apiAuth.ViewerRole))
v3.POST("/compliance/quick/sequence", httpserver.AuthorizeHandler(h.CreateComplianceQuickSequence, apiAuth.EditorRole))
v3.GET("/compliance/quick/sequence/:run_id", httpserver.AuthorizeHandler(h.GetComplianceQuickSequence, apiAuth.ViewerRole))
}
Expand Down Expand Up @@ -1643,12 +1642,12 @@ func (h HttpServer) GetComplianceJobStatus(ctx echo.Context) error {
summaryJobId = &summaryJobs[0].ID
}

var connectionInfo api.IntegrationInfo
var integrationInfo api.IntegrationInfo
integration, err := h.Scheduler.integrationClient.GetIntegration(clientCtx, j.IntegrationID)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
connectionInfo = api.IntegrationInfo{
integrationInfo = api.IntegrationInfo{
IntegrationID: integration.IntegrationID,
IntegrationType: string(integration.IntegrationType),
ProviderID: integration.ProviderID,
Expand All @@ -1657,8 +1656,9 @@ func (h HttpServer) GetComplianceJobStatus(ctx echo.Context) error {

jobsResult := api.GetComplianceJobStatusResponse{
JobId: j.ID,
WithIncidents: j.WithIncidents,
SummaryJobId: summaryJobId,
IntegrationInfo: connectionInfo,
IntegrationInfo: integrationInfo,
BenchmarkId: j.BenchmarkID,
JobStatus: string(j.Status),
CreatedAt: j.CreatedAt,
Expand Down Expand Up @@ -3663,37 +3663,6 @@ func parseTimeInterval(intervalStr string) (*time.Time, *time.Time, error) {
return &startTime, &endTime, nil
}

// GetComplianceQuickRun godoc
//
// @Summary Get audit job by job id
// @Description Get audit job by job id
// @Security BearerToken
// @Tags audit
// @Accept json
// @Produce json
// @Success 200
// @Router /schedule/api/v3/compliance/quick/{run_id} [get]
func (h HttpServer) GetComplianceQuickRun(c echo.Context) error {
jobIdStr := c.Param("run_id")

var jobId int64
var err error
if jobIdStr != "" {
jobId, err = strconv.ParseInt(jobIdStr, 10, 64)
if err != nil {
return err
}
}

auditJob, err := h.DB.GetComplianceJobByID(uint(jobId))
if err != nil {
h.Scheduler.logger.Error("failed to get compliance quick run", zap.Error(err))
return echo.NewHTTPError(http.StatusInternalServerError, "failed to get compliance quick run")
}

return c.JSON(http.StatusOK, auditJob.ToApi())
}

// CreateComplianceQuickSequence godoc
//
// @Summary Create Compliance Quick Sequence
Expand Down

0 comments on commit bf92c36

Please sign in to comment.