@@ -34,6 +34,12 @@ import (
34
34
"go.uber.org/zap"
35
35
)
36
36
37
+ const (
38
+ ObjectTypeApp = "app"
39
+ ObjectTypeChart = "chart"
40
+ ObjectTypePod = "pod"
41
+ )
42
+
37
43
type ImageScanRestHandler interface {
38
44
ScanExecutionList (w http.ResponseWriter , r * http.Request )
39
45
FetchExecutionDetail (w http.ResponseWriter , r * http.Request )
@@ -90,37 +96,72 @@ func (impl ImageScanRestHandlerImpl) ScanExecutionList(w http.ResponseWriter, r
90
96
}
91
97
return
92
98
}
99
+
93
100
token := r .Header .Get ("token" )
94
101
var ids []int
102
+ var appRBACObjects []string
103
+ var envRBACObjects []string
104
+ var podRBACObjects []string
105
+ podRBACMap := make (map [string ]int )
106
+
107
+ IdToAppEnvPairs := make (map [int ][2 ]int )
95
108
for _ , item := range deployInfoList {
96
- if item .ScanObjectMetaId > 0 && (item .ObjectType == "app" || item .ObjectType == "chart" ) {
97
- object := impl .enforcerUtil .GetAppRBACNameByAppId (item .ScanObjectMetaId )
98
- if ok := impl .enforcer .Enforce (token , casbin .ResourceApplications , casbin .ActionGet , object ); ! ok {
99
- common .WriteJsonResp (w , fmt .Errorf ("unauthorized user" ), "Unauthorized User" , http .StatusForbidden )
100
- return
109
+ if item .ScanObjectMetaId > 0 && (item .ObjectType == ObjectTypeApp || item .ObjectType == ObjectTypeChart ) {
110
+ IdToAppEnvPairs [item .Id ] = [2 ]int {item .ScanObjectMetaId , item .EnvId }
111
+ }
112
+ }
113
+
114
+ appObjects , envObjects , appIdtoApp , envIdToEnv , err := impl .enforcerUtil .GetAppAndEnvRBACNamesByAppAndEnvIds (IdToAppEnvPairs )
115
+ if err != nil {
116
+ common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
117
+ return
118
+ }
119
+
120
+ for _ , item := range deployInfoList {
121
+ if item .ScanObjectMetaId > 0 && (item .ObjectType == ObjectTypeApp || item .ObjectType == ObjectTypeChart ) {
122
+ appObject := appObjects [item .Id ]
123
+ envObject := envObjects [item .Id ]
124
+ if appObject != "" {
125
+ appRBACObjects = append (appRBACObjects , appObject )
101
126
}
102
- object = impl .enforcerUtil .GetEnvRBACNameByAppId (item .ScanObjectMetaId , item .EnvId )
103
- if ok := impl .enforcer .Enforce (token , casbin .ResourceEnvironment , casbin .ActionGet , object ); ok {
104
- ids = append (ids , item .Id )
127
+ if envObject != "" {
128
+ envRBACObjects = append (envRBACObjects , envObject )
105
129
}
106
- } else if item .ScanObjectMetaId > 0 && (item .ObjectType == "pod" ) {
130
+ } else if item .ScanObjectMetaId > 0 && (item .ObjectType == ObjectTypePod ) {
107
131
environments , err := impl .environmentService .GetByClusterId (item .ClusterId )
108
132
if err != nil {
109
133
common .WriteJsonResp (w , err , nil , http .StatusInternalServerError )
110
134
return
111
135
}
112
- pass := false
113
136
for _ , environment := range environments {
114
- if ok := impl .enforcer .Enforce (token , casbin .ResourceGlobalEnvironment , casbin .ActionGet , environment .EnvironmentIdentifier ); ok {
115
- pass = true
116
- continue
117
- }
137
+ podObject := environment .EnvironmentIdentifier
138
+ podRBACObjects = append (podRBACObjects , podObject )
139
+ podRBACMap [podObject ] = item .Id
118
140
}
119
- if pass {
120
- ids = append (ids , item .Id )
141
+ }
142
+ }
143
+
144
+ appResults := impl .enforcer .EnforceInBatch (token , casbin .ResourceApplications , casbin .ActionGet , appRBACObjects )
145
+ envResults := impl .enforcer .EnforceInBatch (token , casbin .ResourceEnvironment , casbin .ActionGet , envRBACObjects )
146
+ podResults := impl .enforcer .EnforceInBatch (token , casbin .ResourceGlobalEnvironment , casbin .ActionGet , podRBACObjects )
147
+
148
+ for _ , item := range deployInfoList {
149
+ if impl .enforcerUtil .IsAuthorizedForAppInAppResults (item .ScanObjectMetaId , appResults , appIdtoApp ) && impl .enforcerUtil .IsAuthorizedForEnvInEnvResults (item .ScanObjectMetaId , item .EnvId , envResults , appIdtoApp , envIdToEnv ) {
150
+ ids = append (ids , item .Id )
151
+ }
152
+ }
153
+ for podObject , authorized := range podResults {
154
+ if authorized {
155
+ if itemId , exists := podRBACMap [podObject ]; exists {
156
+ ids = append (ids , itemId )
121
157
}
122
158
}
123
- // skip for pod
159
+ }
160
+
161
+ if ids == nil || len (ids ) == 0 {
162
+ responseList := make ([]* securityBean.ImageScanHistoryResponse , 0 )
163
+ common .WriteJsonResp (w , nil , & securityBean.ImageScanHistoryListingResponse {ImageScanHistoryResponse : responseList }, http .StatusOK )
164
+ return
124
165
}
125
166
126
167
results , err := impl .imageScanService .FetchScanExecutionListing (request , ids )
0 commit comments