Skip to content

Commit 188483d

Browse files
committed
Merge branch 'develop' into audit-trigger
2 parents 6007160 + fccc8a0 commit 188483d

File tree

5 files changed

+88
-37
lines changed

5 files changed

+88
-37
lines changed

api/restHandler/ImageScanRestHandler.go

Lines changed: 45 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning"
2424
securityBean "github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning/bean"
2525
security2 "github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning/repository"
26+
"github.com/devtron-labs/devtron/util/sliceUtil"
2627
"net/http"
2728
"strconv"
2829

@@ -104,6 +105,45 @@ func (impl ImageScanRestHandlerImpl) ScanExecutionList(w http.ResponseWriter, r
104105
return
105106
}
106107
token := r.Header.Get("token")
108+
isSuperAdmin := false
109+
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobal, casbin.ActionGet, "*"); ok {
110+
isSuperAdmin = true
111+
}
112+
var ids []int
113+
if isSuperAdmin {
114+
ids = sliceUtil.NewSliceFromFuncExec(filteredDeployInfoList, func(item *security2.ImageScanDeployInfo) int {
115+
return item.Id
116+
})
117+
} else {
118+
ids, err = impl.getAuthorisedImageScanDeployInfoIds(token, filteredDeployInfoList)
119+
if err != nil {
120+
impl.logger.Errorw("error in getting authorised image scan deploy info ids", "err", err)
121+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
122+
return
123+
}
124+
}
125+
126+
if len(ids) == 0 {
127+
responseList := make([]*securityBean.ImageScanHistoryResponse, 0)
128+
common.WriteJsonResp(w, nil, &securityBean.ImageScanHistoryListingResponse{ImageScanHistoryResponse: responseList}, http.StatusOK)
129+
return
130+
}
131+
132+
results, err := impl.imageScanService.FetchScanExecutionListing(request, ids)
133+
if err != nil {
134+
impl.logger.Errorw("service err, ScanExecutionList", "err", err, "payload", request)
135+
if util.IsErrNoRows(err) {
136+
responseList := make([]*securityBean.ImageScanHistoryResponse, 0)
137+
common.WriteJsonResp(w, nil, &securityBean.ImageScanHistoryListingResponse{ImageScanHistoryResponse: responseList}, http.StatusOK)
138+
} else {
139+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
140+
}
141+
return
142+
}
143+
common.WriteJsonResp(w, err, results, http.StatusOK)
144+
}
145+
146+
func (impl ImageScanRestHandlerImpl) getAuthorisedImageScanDeployInfoIds(token string, filteredDeployInfoList []*security2.ImageScanDeployInfo) ([]int, error) {
107147
var ids []int
108148
var appRBACObjects []string
109149
var envRBACObjects []string
@@ -119,8 +159,8 @@ func (impl ImageScanRestHandlerImpl) ScanExecutionList(w http.ResponseWriter, r
119159

120160
appObjects, envObjects, appIdtoApp, envIdToEnv, err := impl.enforcerUtil.GetAppAndEnvRBACNamesByAppAndEnvIds(IdToAppEnvPairs)
121161
if err != nil {
122-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
123-
return
162+
impl.logger.Errorw("error in getting app and env rbac objects", "err", err)
163+
return nil, err
124164
}
125165

126166
for _, item := range filteredDeployInfoList {
@@ -136,8 +176,8 @@ func (impl ImageScanRestHandlerImpl) ScanExecutionList(w http.ResponseWriter, r
136176
} else if item.ScanObjectMetaId > 0 && (item.ObjectType == ObjectTypePod) {
137177
environments, err := impl.environmentService.GetByClusterId(item.ClusterId)
138178
if err != nil {
139-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
140-
return
179+
impl.logger.Errorw("error in getting environments for cluster", "clusterId", item.ClusterId, "err", err)
180+
return nil, err
141181
}
142182
for _, environment := range environments {
143183
podObject := environment.EnvironmentIdentifier
@@ -163,25 +203,7 @@ func (impl ImageScanRestHandlerImpl) ScanExecutionList(w http.ResponseWriter, r
163203
}
164204
}
165205
}
166-
167-
if ids == nil || len(ids) == 0 {
168-
responseList := make([]*securityBean.ImageScanHistoryResponse, 0)
169-
common.WriteJsonResp(w, nil, &securityBean.ImageScanHistoryListingResponse{ImageScanHistoryResponse: responseList}, http.StatusOK)
170-
return
171-
}
172-
173-
results, err := impl.imageScanService.FetchScanExecutionListing(request, ids)
174-
if err != nil {
175-
impl.logger.Errorw("service err, ScanExecutionList", "err", err, "payload", request)
176-
if util.IsErrNoRows(err) {
177-
responseList := make([]*securityBean.ImageScanHistoryResponse, 0)
178-
common.WriteJsonResp(w, nil, &securityBean.ImageScanHistoryListingResponse{ImageScanHistoryResponse: responseList}, http.StatusOK)
179-
} else {
180-
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
181-
}
182-
return
183-
}
184-
common.WriteJsonResp(w, err, results, http.StatusOK)
206+
return ids, nil
185207
}
186208

187209
func (impl ImageScanRestHandlerImpl) FetchExecutionDetail(w http.ResponseWriter, r *http.Request) {

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -774,31 +774,41 @@ func (impl *CdWorkflowRepositoryImpl) FindDeployedCdWorkflowRunnersByPipelineId(
774774
}
775775

776776
func (impl *CdWorkflowRepositoryImpl) FindLatestCdWorkflowRunnerArtifactMetadataForAppAndEnvIds(appVsEnvIdMap map[int][]int, runnerType apiBean.WorkflowType) ([]*cdWorkflow.CdWorkflowRunnerArtifactMetadata, error) {
777-
var allRunners []*cdWorkflow.CdWorkflowRunnerArtifactMetadata
777+
var runners []*cdWorkflow.CdWorkflowRunnerArtifactMetadata
778+
779+
// Prepare the (app_id, env_id) tuple list for the query
780+
tupleList := make([]interface{}, 0, len(appVsEnvIdMap))
781+
for appId, envIds := range appVsEnvIdMap {
782+
for _, envId := range envIds {
783+
tupleList = append(tupleList, []interface{}{appId, envId})
784+
}
785+
}
786+
if len(tupleList) == 0 {
787+
return nil, nil
788+
}
789+
778790
query := `
779791
WITH RankedData AS (
780792
SELECT
781793
p.app_id AS "app_id",
782794
p.environment_id AS "env_id",
783-
p.deleted AS "deleted",
795+
p.deleted AS "deleted",
784796
wf.ci_artifact_id AS "ci_artifact_id",
785797
ci_artifact.parent_ci_artifact AS "parent_ci_artifact",
786798
ci_artifact.scanned AS "scanned",
787799
ROW_NUMBER() OVER (PARTITION BY p.app_id, p.environment_id ORDER BY cd_workflow_runner.id DESC) AS rn
788800
FROM cd_workflow_runner INNER JOIN cd_workflow wf ON wf.id = cd_workflow_runner.cd_workflow_id
789801
INNER JOIN pipeline p ON p.id = wf.pipeline_id
790802
INNER JOIN ci_artifact ON ci_artifact.id = wf.ci_artifact_id
791-
WHERE cd_workflow_runner.workflow_type = ? AND p.app_id = ? AND p.environment_id IN (?))
803+
WHERE cd_workflow_runner.workflow_type = ?
804+
AND (p.app_id, p.environment_id) IN ( ? )
805+
)
792806
SELECT "app_id","env_id","ci_artifact_id","parent_ci_artifact","scanned" FROM RankedData WHERE rn = 1 and deleted= false;
793807
`
794-
for appId, envIds := range appVsEnvIdMap {
795-
var runners []*cdWorkflow.CdWorkflowRunnerArtifactMetadata
796-
_, err := impl.dbConnection.Query(&runners, query, runnerType, appId, pg.In(envIds))
797-
if err != nil {
798-
impl.logger.Errorw("error in getting cdWfrs by appId and envIds and runner type", "appVsEnvIdMap", appVsEnvIdMap, "err", err)
799-
return nil, err
800-
}
801-
allRunners = append(allRunners, runners...)
808+
_, err := impl.dbConnection.Query(&runners, query, runnerType, pg.In(tupleList))
809+
if err != nil {
810+
impl.logger.Errorw("error in getting cdWfrs by appId and envIds and runner type", "appVsEnvIdMap", appVsEnvIdMap, "err", err)
811+
return nil, err
802812
}
803-
return allRunners, nil
813+
return runners, nil
804814
}

pkg/pipeline/PipelineStageService.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/devtron-labs/devtron/internal/util"
2626
"github.com/devtron-labs/devtron/pkg/pipeline/adapter"
2727
"github.com/devtron-labs/devtron/pkg/pipeline/bean"
28+
"github.com/devtron-labs/devtron/pkg/pipeline/helper"
2829
"github.com/devtron-labs/devtron/pkg/pipeline/repository"
2930
"github.com/devtron-labs/devtron/pkg/plugin"
3031
repository2 "github.com/devtron-labs/devtron/pkg/plugin/repository"
@@ -679,14 +680,15 @@ func (impl *PipelineStageServiceImpl) CreateStageSteps(steps []*bean.PipelineSta
679680
impl.logger.Errorw("error in creating script and mapping for inline step", "err", err, "inlineStepDetail", inlineStepDetail)
680681
return err
681682
}
683+
682684
inlineStep := &repository.PipelineStageStep{
683685
PipelineStageId: stageId,
684686
Name: step.Name,
685687
Description: step.Description,
686688
Index: step.Index,
687689
StepType: step.StepType,
688690
ScriptId: scriptEntryId,
689-
OutputDirectoryPath: step.OutputDirectoryPath,
691+
OutputDirectoryPath: helper.FilterReservedPathFromOutputDirPath(step.OutputDirectoryPath), // TODO: silently filtering reserved paths, not throwing error as of now since this flow is not in tx
690692
DependentOnStep: dependentOnStep,
691693
Deleted: false,
692694
AuditLog: sql.AuditLog{
@@ -1205,7 +1207,7 @@ func (impl *PipelineStageServiceImpl) UpdateStageStepsWithTx(steps []*bean.Pipel
12051207
Description: step.Description,
12061208
Index: step.Index,
12071209
StepType: step.StepType,
1208-
OutputDirectoryPath: step.OutputDirectoryPath,
1210+
OutputDirectoryPath: helper.FilterReservedPathFromOutputDirPath(step.OutputDirectoryPath),
12091211
DependentOnStep: dependentOnStep,
12101212
Deleted: false,
12111213
AuditLog: sql.AuditLog{

pkg/pipeline/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ const Starting = "Starting"
4040
const TERMINATE_MESSAGE = "workflow shutdown with strategy: Terminate"
4141
const FORCE_ABORT_MESSAGE_AFTER_STARTING_STAGE = "workflow shutdown with strategy: Force Abort"
4242
const POD_TIMEOUT_MESSAGE = "Pod was active on the node longer than the specified deadline"
43+
const CiRunnerWorkingDir = "/devtroncd"

pkg/pipeline/helper/helper.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package helper
2+
3+
import (
4+
"github.com/devtron-labs/devtron/pkg/pipeline/constants"
5+
"strings"
6+
)
7+
8+
func FilterReservedPathFromOutputDirPath(outputDirectoryPath []string) []string {
9+
var newOutputDirPath []string
10+
for _, path := range outputDirectoryPath {
11+
if !strings.HasPrefix(path, constants.CiRunnerWorkingDir) {
12+
newOutputDirPath = append(newOutputDirPath, path)
13+
}
14+
}
15+
return newOutputDirPath
16+
}

0 commit comments

Comments
 (0)