@@ -18,99 +18,37 @@ package security
18
18
19
19
import (
20
20
"fmt"
21
+ securityBean "github.com/devtron-labs/devtron/internal/sql/repository/security/bean"
21
22
"github.com/devtron-labs/devtron/pkg/sql"
22
23
"github.com/go-pg/pg"
23
24
"github.com/go-pg/pg/orm"
24
25
"time"
25
26
)
26
27
27
28
type CvePolicy struct {
28
- tableName struct {} `sql:"cve_policy_control" pg:",discard_unknown_columns"`
29
- Id int `sql:"id,pk"`
30
- Global bool `sql:"global,notnull"`
31
- ClusterId int `sql:"cluster_id"`
32
- EnvironmentId int `sql:"env_id"`
33
- AppId int `sql:"app_id"`
34
- CVEStoreId string `sql:"cve_store_id"`
35
- Action PolicyAction `sql:"action, notnull"`
36
- Severity * Severity `sql:"severity, notnull "`
37
- Deleted bool `sql:"deleted, notnull"`
29
+ tableName struct {} `sql:"cve_policy_control" pg:",discard_unknown_columns"`
30
+ Id int `sql:"id,pk"`
31
+ Global bool `sql:"global,notnull"`
32
+ ClusterId int `sql:"cluster_id"`
33
+ EnvironmentId int `sql:"env_id"`
34
+ AppId int `sql:"app_id"`
35
+ CVEStoreId string `sql:"cve_store_id"`
36
+ Action securityBean. PolicyAction `sql:"action, notnull"`
37
+ Severity * securityBean. Severity `sql:"severity, notnull "`
38
+ Deleted bool `sql:"deleted, notnull"`
38
39
sql.AuditLog
39
40
CveStore * CveStore
40
41
}
41
42
42
- type PolicyAction int
43
-
44
- const (
45
- Inherit PolicyAction = iota
46
- Allow
47
- Block
48
- Blockiffixed
49
- )
50
-
51
- func (d PolicyAction ) String () string {
52
- return [... ]string {"inherit" , "allow" , "block" , "blockiffixed" }[d ]
53
- }
54
-
55
- // ------------------
56
- type Severity int
57
-
58
- const (
59
- Low Severity = iota
60
- Medium
61
- Critical
62
- High
63
- Safe
64
- )
65
- const (
66
- HIGH string = "high"
67
- CRITICAL string = "critical"
68
- SAFE string = "safe"
69
- LOW string = "low"
70
- MEDIUM string = "medium"
71
- MODERATE string = "moderate"
72
- )
73
-
74
- // Handling for future use
75
- func (d Severity ) ValuesOf (severity string ) Severity {
76
- if severity == CRITICAL || severity == HIGH {
77
- return Critical
78
- } else if severity == MODERATE || severity == MEDIUM {
79
- return Medium
80
- } else if severity == LOW || severity == SAFE {
81
- return Low
82
- }
83
- return Low
84
- }
85
-
86
- // Updating it for future use(not in use for standard severity)
87
- func (d Severity ) String () string {
88
- return [... ]string {"low" , "moderate" , "critical" , "high" , "safe" }[d ]
89
- }
90
-
91
- // ----------------
92
- type PolicyLevel int
93
-
94
- const (
95
- Global PolicyLevel = iota
96
- Cluster
97
- Environment
98
- Application
99
- )
100
-
101
- func (d PolicyLevel ) String () string {
102
- return [... ]string {"global" , "cluster" , "environment" , "application" }[d ]
103
- }
104
-
105
- func (policy * CvePolicy ) PolicyLevel () PolicyLevel {
43
+ func (policy * CvePolicy ) PolicyLevel () securityBean.PolicyLevel {
106
44
if policy .ClusterId != 0 {
107
- return Cluster
45
+ return securityBean . Cluster
108
46
} else if policy .AppId != 0 {
109
- return Application
47
+ return securityBean . Application
110
48
} else if policy .EnvironmentId != 0 {
111
- return Environment
49
+ return securityBean . Environment
112
50
} else {
113
- return Global
51
+ return securityBean . Global
114
52
}
115
53
}
116
54
@@ -250,37 +188,37 @@ func (impl *CvePolicyRepositoryImpl) GetBlockedCVEList(cves []*CveStore, cluster
250
188
return blockedCve , nil
251
189
}
252
190
253
- func EnforceCvePolicy (cves []* CveStore , cvePolicy map [string ]* CvePolicy , severityPolicy map [Severity ]* CvePolicy ) (blockedCVE []* CveStore ) {
191
+ func EnforceCvePolicy (cves []* CveStore , cvePolicy map [string ]* CvePolicy , severityPolicy map [securityBean. Severity ]* CvePolicy ) (blockedCVE []* CveStore ) {
254
192
255
193
for _ , cve := range cves {
256
194
if policy , ok := cvePolicy [cve .Name ]; ok {
257
- if policy .Action == Allow {
195
+ if policy .Action == securityBean . Allow {
258
196
continue
259
- } else if (policy .Action == Block ) || (policy .Action == Blockiffixed && cve .FixedVersion != "" ) {
197
+ } else if (policy .Action == securityBean . Block ) || (policy .Action == securityBean . Blockiffixed && cve .FixedVersion != "" ) {
260
198
blockedCVE = append (blockedCVE , cve )
261
199
}
262
200
} else {
263
- if severityPolicy [cve .Severity ] != nil && severityPolicy [cve .Severity ].Action == Allow {
201
+ if severityPolicy [cve .GetSeverity () ] != nil && severityPolicy [cve .GetSeverity () ].Action == securityBean . Allow {
264
202
continue
265
- } else if severityPolicy [cve .Severity ] != nil && (severityPolicy [cve .Severity ].Action == Block || (severityPolicy [cve .Severity ].Action == Blockiffixed && cve .FixedVersion != "" )) {
203
+ } else if severityPolicy [cve .GetSeverity () ] != nil && (severityPolicy [cve .GetSeverity () ].Action == securityBean . Block || (severityPolicy [cve .GetSeverity () ].Action == securityBean . Blockiffixed && cve .FixedVersion != "" )) {
266
204
blockedCVE = append (blockedCVE , cve )
267
205
}
268
206
}
269
207
}
270
208
return blockedCVE
271
209
}
272
210
273
- func (impl * CvePolicyRepositoryImpl ) getApplicablePolicy (clusterId , envId , appId int , isAppstore bool ) (map [string ]* CvePolicy , map [Severity ]* CvePolicy , error ) {
211
+ func (impl * CvePolicyRepositoryImpl ) getApplicablePolicy (clusterId , envId , appId int , isAppstore bool ) (map [string ]* CvePolicy , map [securityBean. Severity ]* CvePolicy , error ) {
274
212
275
- var policyLevel PolicyLevel
213
+ var policyLevel securityBean. PolicyLevel
276
214
if isAppstore && appId > 0 && envId > 0 && clusterId > 0 {
277
- policyLevel = Environment
215
+ policyLevel = securityBean . Environment
278
216
} else if appId > 0 && envId > 0 && clusterId > 0 {
279
- policyLevel = Application
217
+ policyLevel = securityBean . Application
280
218
} else if envId > 0 && clusterId > 0 {
281
- policyLevel = Environment
219
+ policyLevel = securityBean . Environment
282
220
} else if clusterId > 0 {
283
- policyLevel = Cluster
221
+ policyLevel = securityBean . Cluster
284
222
} else {
285
223
//error in case of global or other policy
286
224
return nil , nil , fmt .Errorf ("policy not identified" )
@@ -290,16 +228,16 @@ func (impl *CvePolicyRepositoryImpl) getApplicablePolicy(clusterId, envId, appId
290
228
return cvePolicy , severityPolicy , err
291
229
}
292
230
293
- func (impl * CvePolicyRepositoryImpl ) getPolicies (policyLevel PolicyLevel , clusterId , environmentId , appId int ) (map [string ]* CvePolicy , map [Severity ]* CvePolicy , error ) {
231
+ func (impl * CvePolicyRepositoryImpl ) getPolicies (policyLevel securityBean. PolicyLevel , clusterId , environmentId , appId int ) (map [string ]* CvePolicy , map [securityBean. Severity ]* CvePolicy , error ) {
294
232
var policies []* CvePolicy
295
233
var err error
296
- if policyLevel == Global {
234
+ if policyLevel == securityBean . Global {
297
235
policies , err = impl .GetGlobalPolicies ()
298
- } else if policyLevel == Cluster {
236
+ } else if policyLevel == securityBean . Cluster {
299
237
policies , err = impl .GetClusterPolicies (clusterId )
300
- } else if policyLevel == Environment {
238
+ } else if policyLevel == securityBean . Environment {
301
239
policies , err = impl .GetEnvPolicies (clusterId , environmentId )
302
- } else if policyLevel == Application {
240
+ } else if policyLevel == securityBean . Application {
303
241
policies , err = impl .GetAppEnvPolicies (clusterId , environmentId , appId )
304
242
} else {
305
243
return nil , nil , fmt .Errorf ("unsupported policy level: %s" , policyLevel )
@@ -314,9 +252,9 @@ func (impl *CvePolicyRepositoryImpl) getPolicies(policyLevel PolicyLevel, cluste
314
252
return cvePolicy , severityPolicy , nil
315
253
}
316
254
317
- func (impl * CvePolicyRepositoryImpl ) getApplicablePolicies (policies []* CvePolicy ) (map [string ]* CvePolicy , map [Severity ]* CvePolicy ) {
255
+ func (impl * CvePolicyRepositoryImpl ) getApplicablePolicies (policies []* CvePolicy ) (map [string ]* CvePolicy , map [securityBean. Severity ]* CvePolicy ) {
318
256
cvePolicy := make (map [string ][]* CvePolicy )
319
- severityPolicy := make (map [Severity ][]* CvePolicy )
257
+ severityPolicy := make (map [securityBean. Severity ][]* CvePolicy )
320
258
for _ , policy := range policies {
321
259
if policy .CVEStoreId != "" {
322
260
cvePolicy [policy .CveStore .Name ] = append (cvePolicy [policy .CveStore .Name ], policy )
@@ -347,8 +285,8 @@ func (impl *CvePolicyRepositoryImpl) getHighestPolicy(allPolicies map[string][]*
347
285
return applicablePolicies
348
286
}
349
287
350
- func (impl * CvePolicyRepositoryImpl ) getHighestPolicyS (allPolicies map [Severity ][]* CvePolicy ) map [Severity ]* CvePolicy {
351
- applicablePolicies := make (map [Severity ]* CvePolicy )
288
+ func (impl * CvePolicyRepositoryImpl ) getHighestPolicyS (allPolicies map [securityBean. Severity ][]* CvePolicy ) map [securityBean. Severity ]* CvePolicy {
289
+ applicablePolicies := make (map [securityBean. Severity ]* CvePolicy )
352
290
for key , policies := range allPolicies {
353
291
var applicablePolicy * CvePolicy
354
292
for _ , policy := range policies {
0 commit comments