Skip to content

Commit a939057

Browse files
authored
Merge branch 'develop' into appgroup-fix
2 parents 541dae3 + c447ad2 commit a939057

File tree

27 files changed

+717
-106
lines changed

27 files changed

+717
-106
lines changed

Wire.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ import (
4646
"github.com/devtron-labs/devtron/api/infraConfig"
4747
"github.com/devtron-labs/devtron/api/k8s"
4848
"github.com/devtron-labs/devtron/api/module"
49+
"github.com/devtron-labs/devtron/api/resourceScan"
4950
"github.com/devtron-labs/devtron/api/restHandler"
5051
"github.com/devtron-labs/devtron/api/restHandler/app/appInfo"
5152
appList2 "github.com/devtron-labs/devtron/api/restHandler/app/appList"
@@ -211,6 +212,7 @@ func InitializeApp() (*App, error) {
211212
imageTagging.WireSet,
212213
devtronResource.DevtronResourceWireSet,
213214
policyGovernance.PolicyGovernanceWireSet,
215+
resourceScan.ScanningResultWireSet,
214216

215217
// -------wireset end ----------
216218
// -------
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package resourceScan
18+
19+
import (
20+
"fmt"
21+
"github.com/devtron-labs/devtron/api/restHandler/common"
22+
"github.com/devtron-labs/devtron/pkg/auth/authorisation/casbin"
23+
"github.com/devtron-labs/devtron/pkg/auth/user"
24+
"github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning"
25+
"github.com/devtron-labs/devtron/pkg/policyGovernance/security/imageScanning/bean"
26+
"github.com/devtron-labs/devtron/util/rbac"
27+
"go.uber.org/zap"
28+
"gopkg.in/go-playground/validator.v9"
29+
"net/http"
30+
)
31+
32+
type ScanningResultRestHandler interface {
33+
ScanResults(w http.ResponseWriter, r *http.Request)
34+
}
35+
36+
type ScanningResultRestHandlerImpl struct {
37+
logger *zap.SugaredLogger
38+
userService user.UserService
39+
scanService imageScanning.ImageScanService
40+
enforcer casbin.Enforcer
41+
enforcerUtil rbac.EnforcerUtil
42+
validator *validator.Validate
43+
}
44+
45+
func NewScanningResultRestHandlerImpl(
46+
logger *zap.SugaredLogger,
47+
userService user.UserService,
48+
scanService imageScanning.ImageScanService,
49+
enforcer casbin.Enforcer,
50+
enforcerUtil rbac.EnforcerUtil,
51+
validator *validator.Validate,
52+
) *ScanningResultRestHandlerImpl {
53+
return &ScanningResultRestHandlerImpl{
54+
logger: logger,
55+
userService: userService,
56+
scanService: scanService,
57+
enforcer: enforcer,
58+
enforcerUtil: enforcerUtil,
59+
validator: validator,
60+
}
61+
}
62+
63+
func getResourceScanQueryParams(w http.ResponseWriter, r *http.Request) (*bean.ResourceScanQueryParams, error) {
64+
queryParams := &bean.ResourceScanQueryParams{}
65+
var appId, envId, installedAppId, artifactId, installedAppVersionHistoryId int
66+
var err error
67+
appId, err = common.ExtractIntQueryParam(w, r, "appId", 0)
68+
if err != nil {
69+
return queryParams, err
70+
}
71+
queryParams.AppId = appId
72+
73+
installedAppId, err = common.ExtractIntQueryParam(w, r, "installedAppId", 0)
74+
if err != nil {
75+
return queryParams, err
76+
}
77+
queryParams.InstalledAppId = installedAppId
78+
79+
installedAppVersionHistoryId, err = common.ExtractIntQueryParam(w, r, "installedAppVersionHistoryId", 0)
80+
if err != nil {
81+
return queryParams, err
82+
}
83+
queryParams.InstalledAppVersionHistoryId = installedAppVersionHistoryId
84+
85+
envId, err = common.ExtractIntQueryParam(w, r, "envId", 0)
86+
if err != nil {
87+
return queryParams, err
88+
}
89+
queryParams.EnvId = envId
90+
91+
artifactId, err = common.ExtractIntQueryParam(w, r, "artifactId", 0)
92+
if err != nil {
93+
return queryParams, err
94+
}
95+
queryParams.ArtifactId = artifactId
96+
return queryParams, nil
97+
}
98+
99+
func (impl ScanningResultRestHandlerImpl) ScanResults(w http.ResponseWriter, r *http.Request) {
100+
userId, err := impl.userService.GetLoggedInUser(r)
101+
if userId == 0 || err != nil {
102+
common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized)
103+
return
104+
}
105+
resourceScanQueryParams, err := getResourceScanQueryParams(w, r)
106+
if err != nil {
107+
return
108+
}
109+
// RBAC
110+
token := r.Header.Get("token")
111+
object := impl.enforcerUtil.GetAppRBACNameByAppId(resourceScanQueryParams.AppId)
112+
if ok := impl.enforcer.Enforce(token, casbin.ResourceApplications, casbin.ActionGet, object); !ok {
113+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
114+
return
115+
}
116+
if resourceScanQueryParams.EnvId > 0 {
117+
object = impl.enforcerUtil.GetEnvRBACNameByAppId(resourceScanQueryParams.AppId, resourceScanQueryParams.EnvId)
118+
if ok := impl.enforcer.Enforce(token, casbin.ResourceEnvironment, casbin.ActionGet, object); !ok {
119+
common.WriteJsonResp(w, fmt.Errorf("unauthorized user"), "Unauthorized User", http.StatusForbidden)
120+
return
121+
}
122+
}
123+
// RBAC
124+
resp, err := impl.scanService.GetScanResults(resourceScanQueryParams)
125+
if err != nil {
126+
impl.logger.Errorw("service err, GetScanResults", "resourceScanQueryParams", resourceScanQueryParams, "err", err)
127+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
128+
return
129+
}
130+
131+
common.WriteJsonResp(w, nil, resp, http.StatusOK)
132+
133+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package resourceScan
18+
19+
import (
20+
"github.com/gorilla/mux"
21+
)
22+
23+
type ScanningResultRouter interface {
24+
InitScanningResultRouter(configRouter *mux.Router)
25+
}
26+
27+
type ScanningResultRouterImpl struct {
28+
ScanningResultRestHandler ScanningResultRestHandler
29+
}
30+
31+
func NewScanningResultRouterImpl(ScanningResultRestHandler ScanningResultRestHandler) *ScanningResultRouterImpl {
32+
return &ScanningResultRouterImpl{ScanningResultRestHandler: ScanningResultRestHandler}
33+
}
34+
35+
func (router *ScanningResultRouterImpl) InitScanningResultRouter(configRouter *mux.Router) {
36+
configRouter.Path("").HandlerFunc(router.ScanningResultRestHandler.ScanResults).Methods("GET")
37+
}

api/resourceScan/wire_scan.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2024. Devtron Inc.
3+
*/
4+
5+
package resourceScan
6+
7+
import (
8+
"github.com/google/wire"
9+
)
10+
11+
var ScanningResultWireSet = wire.NewSet(
12+
NewScanningResultRouterImpl,
13+
wire.Bind(new(ScanningResultRouter), new(*ScanningResultRouterImpl)),
14+
NewScanningResultRestHandlerImpl,
15+
wire.Bind(new(ScanningResultRestHandler), new(*ScanningResultRestHandlerImpl)),
16+
)

api/router/router.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/devtron-labs/devtron/api/k8s/application"
3838
"github.com/devtron-labs/devtron/api/k8s/capacity"
3939
"github.com/devtron-labs/devtron/api/module"
40+
"github.com/devtron-labs/devtron/api/resourceScan"
4041
"github.com/devtron-labs/devtron/api/restHandler/common"
4142
"github.com/devtron-labs/devtron/api/router/app"
4243
"github.com/devtron-labs/devtron/api/router/app/configDiff"
@@ -120,6 +121,7 @@ type MuxRouter struct {
120121
argoApplicationRouter argoApplication.ArgoApplicationRouter
121122
fluxApplicationRouter fluxApplication2.FluxApplicationRouter
122123
devtronResourceRouter devtronResource.DevtronResourceRouter
124+
scanningResultRouter resourceScan.ScanningResultRouter
123125
}
124126

125127
func NewMuxRouter(logger *zap.SugaredLogger,
@@ -153,6 +155,7 @@ func NewMuxRouter(logger *zap.SugaredLogger,
153155
argoApplicationRouter argoApplication.ArgoApplicationRouter,
154156
devtronResourceRouter devtronResource.DevtronResourceRouter,
155157
fluxApplicationRouter fluxApplication2.FluxApplicationRouter,
158+
scanningResultRouter resourceScan.ScanningResultRouter,
156159
) *MuxRouter {
157160
r := &MuxRouter{
158161
Router: mux.NewRouter(),
@@ -218,6 +221,7 @@ func NewMuxRouter(logger *zap.SugaredLogger,
218221
argoApplicationRouter: argoApplicationRouter,
219222
devtronResourceRouter: devtronResourceRouter,
220223
fluxApplicationRouter: fluxApplicationRouter,
224+
scanningResultRouter: scanningResultRouter,
221225
}
222226
return r
223227
}
@@ -321,6 +325,9 @@ func (r MuxRouter) Init() {
321325
imageScanRouter := r.Router.PathPrefix("/orchestrator/security/scan").Subrouter()
322326
r.imageScanRouter.InitImageScanRouter(imageScanRouter)
323327

328+
scanResultRouter := r.Router.PathPrefix("/orchestrator/scan-result").Subrouter()
329+
r.scanningResultRouter.InitScanningResultRouter(scanResultRouter)
330+
324331
policyRouter := r.Router.PathPrefix("/orchestrator/security/policy").Subrouter()
325332
r.policyRouter.InitPolicyRouter(policyRouter)
326333

@@ -429,4 +436,5 @@ func (r MuxRouter) Init() {
429436

430437
fluxApplicationRouter := r.Router.PathPrefix("/orchestrator/flux-application").Subrouter()
431438
r.fluxApplicationRouter.InitFluxApplicationRouter(fluxApplicationRouter)
439+
432440
}

cmd/external-app/wire_gen.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ require gopkg.in/evanphx/json-patch.v4 v4.12.0 // indirect
289289
replace (
290290
github.com/argoproj/argo-workflows/v3 v3.5.10 => github.com/devtron-labs/argo-workflows/v3 v3.5.10
291291
github.com/devtron-labs/authenticator => github.com/devtron-labs/devtron-services/authenticator v0.0.0-20241219033445-6c0c7082c583
292-
github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241219033445-6c0c7082c583
292+
github.com/devtron-labs/common-lib => github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241225093445-23a98e8bb120
293293
github.com/go-check/check => github.com/go-check/check v0.0.0-20180628173108-788fd7840127
294294
github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.5.5
295295
k8s.io/api => k8s.io/api v0.29.7

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,8 @@ github.com/devtron-labs/argo-workflows/v3 v3.5.10 h1:6rxQOesOzDz6SgQCMDQNHaehsKF
794794
github.com/devtron-labs/argo-workflows/v3 v3.5.10/go.mod h1:/vqxcovDPT4zqr4DjR5v7CF8ggpY1l3TSa2CIG3jmjA=
795795
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20241219033445-6c0c7082c583 h1:YN6O8N4+msUbbmWB+QOpq6ha7hCjF/kkds9bsOZ0EWg=
796796
github.com/devtron-labs/devtron-services/authenticator v0.0.0-20241219033445-6c0c7082c583/go.mod h1:vFJ3M7akwAWCtugDXRW+5Q9P+cp0RiH+K/D5FBbb/JA=
797-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241219033445-6c0c7082c583 h1:TmL6N8vzg0/Lq65Y7PVqkRpvRKGAwCS/74dIPMiTJtU=
798-
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241219033445-6c0c7082c583/go.mod h1:NJSMdv+zTUK3p7rML12RZSeAUKHeLaoY3sR/oK0xhwo=
797+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241225093445-23a98e8bb120 h1:HiYR58c/pglJSD79oKUQ2wu/K/aV4G4cQwvgJs/nB/4=
798+
github.com/devtron-labs/devtron-services/common-lib v0.0.0-20241225093445-23a98e8bb120/go.mod h1:NJSMdv+zTUK3p7rML12RZSeAUKHeLaoY3sR/oK0xhwo=
799799
github.com/devtron-labs/go-bitbucket v0.9.60-beta h1:VEx1jvDgdtDPS6A1uUFoaEi0l1/oLhbr+90xOwr6sDU=
800800
github.com/devtron-labs/go-bitbucket v0.9.60-beta/go.mod h1:GnuiCesvh8xyHeMCb+twm8lBR/kQzJYSKL28ZfObp1Y=
801801
github.com/devtron-labs/protos v0.0.3-0.20240802105333-92ee9bb85d80 h1:xwbTeijNTf4/j1v+tSfwVqwLVnReas/NqEKeQHvSTys=

internal/sql/repository/pipelineConfig/CdWorfkflowRepository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ func (impl *CdWorkflowRepositoryImpl) FindLatestCdWorkflowRunnerByEnvironmentIdA
300300
var wfr CdWorkflowRunner
301301
err := impl.dbConnection.
302302
Model(&wfr).
303-
Column("cd_workflow_runner.*", "CdWorkflow", "CdWorkflow.Pipeline").
303+
Column("cd_workflow_runner.*", "CdWorkflow", "CdWorkflow.Pipeline", "CdWorkflow.CiArtifact").
304304
Where("p.environment_id = ?", environmentId).
305305
Where("p.app_id = ?", appId).
306306
Where("cd_workflow_runner.workflow_type = ?", runnerType).

pkg/policyGovernance/security/imageScanning/CvePolicyService.go

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"bytes"
2121
"encoding/json"
2222
"fmt"
23+
bean2 "github.com/devtron-labs/common-lib/imageScan/bean"
2324
repository1 "github.com/devtron-labs/devtron/internal/sql/repository/app"
2425
"github.com/devtron-labs/devtron/internal/sql/repository/helper"
2526
"github.com/devtron-labs/devtron/pkg/cluster/environment"
@@ -133,22 +134,7 @@ type VerifyImageResponse struct {
133134
FixedVersion string
134135
}
135136

136-
type ScanEvent struct {
137-
Image string `json:"image"`
138-
ImageDigest string `json:"imageDigest"`
139-
AppId int `json:"appId"`
140-
EnvId int `json:"envId"`
141-
PipelineId int `json:"pipelineId"`
142-
CiArtifactId int `json:"ciArtifactId"`
143-
UserId int `json:"userId"`
144-
AccessKey string `json:"accessKey"`
145-
SecretKey string `json:"secretKey"`
146-
Token string `json:"token"`
147-
AwsRegion string `json:"awsRegion"`
148-
DockerRegistryId string `json:"dockerRegistryId"`
149-
}
150-
151-
func (impl *PolicyServiceImpl) SendEventToClairUtility(event *ScanEvent) error {
137+
func (impl *PolicyServiceImpl) SendEventToClairUtility(event *bean2.ImageScanEvent) error {
152138
reqBody, err := json.Marshal(event)
153139
if err != nil {
154140
return err
@@ -228,7 +214,7 @@ func (impl *PolicyServiceImpl) VerifyImage(verifyImageRequest *VerifyImageReques
228214
return nil, err
229215
}
230216
if scanHistory != nil && scanHistory.Id == 0 && objectType != repository3.ScanObjectType_APP {
231-
scanEvent := &ScanEvent{Image: image, ImageDigest: "", PipelineId: 0, UserId: 1}
217+
scanEvent := &bean2.ImageScanEvent{Image: image, ImageDigest: "", PipelineId: 0, UserId: 1}
232218
dockerReg, err := impl.ciTemplateRepository.FindByAppId(app.Id)
233219
if err != nil {
234220
impl.logger.Errorw("error in fetching docker reg ", "err", err)

0 commit comments

Comments
 (0)