This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSecurityVerificationServiceController.java
More file actions
345 lines (328 loc) · 14.9 KB
/
SecurityVerificationServiceController.java
File metadata and controls
345 lines (328 loc) · 14.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*-
* ===============LICENSE_START=======================================================
* Acumos
* ===================================================================================
* Copyright (C) 2019 AT&T Intellectual Property & Tech Mahindra. All rights reserved.
* Modifications Copyright (C) 2019 Nordix Foundation.
* ===================================================================================* This Acumos software file is distributed by AT&T and Tech Mahindra
* under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* This file is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ===============LICENSE_END=========================================================
*/
package org.acumos.securityverification.controller;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.lang.invoke.MethodHandles;
import java.nio.charset.Charset;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.acumos.cds.client.CommonDataServiceRestClientImpl;
import org.acumos.cds.client.ICommonDataServiceRestClient;
import org.acumos.cds.domain.MLPSiteConfig;
import org.acumos.cds.domain.MLPSolution;
import org.acumos.cds.domain.MLPSolutionRevision;
import org.acumos.securityverification.exception.AcumosServiceException;
import org.acumos.securityverification.logging.LogConfig;
import org.acumos.securityverification.service.UploadArtifactSVOutput;
import org.acumos.securityverification.transport.ErrorTransport;
import org.acumos.securityverification.transport.SVResponse;
import org.acumos.securityverification.transport.ScanResult;
import org.acumos.securityverification.transport.SuccessTransport;
import org.acumos.securityverification.utils.SVServiceConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientResponseException;
import org.springframework.web.client.RestTemplate;
@RestController
public class SecurityVerificationServiceController extends AbstractController {
private static final Logger logger =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
@Autowired private Environment env;
public void setEnvironment(Environment env1) {
env = env1;
}
@ApiOperation(value = "Security Verification Service Scan.", response = SuccessTransport.class)
@RequestMapping(
value =
"/scan/"
+ SVServiceConstants.SOLUTIONID
+ "/{solutionId}/"
+ SVServiceConstants.REVISIONID
+ "/{revisionId}/"
+ SVServiceConstants.USERID
+ "/{userId}",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public SVResponse securityVerification(
@PathVariable("solutionId") String solutionId,
@PathVariable("revisionId") String revisionId,
@PathVariable("userId") String userId) {
try {
LogConfig.setEnteringMDCs("security-verification-service", "securityVerification");
logger.warn("securityVerification: solutionId {} revisionId {}", solutionId, revisionId);
ICommonDataServiceRestClient client = getCcdsClient();
MLPSolutionRevision mlpSolutionRevision = client.getSolutionRevision(solutionId, revisionId);
mlpSolutionRevision.setVerifiedLicense("IP");
client.updateSolutionRevision(mlpSolutionRevision);
startScanJob(solutionId, revisionId, userId);
LogConfig.clearMDCDetails();
return new SuccessTransport(HttpServletResponse.SC_OK, null);
} catch (Exception ex) {
logger.warn("securityVerification failed: {}", ex.toString());
return new ErrorTransport(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "securityVerification failed", ex);
}
}
private ResponseEntity<SVResponse> startScanJob(
String solutionId, String revisionId, String userId) {
logger.warn("startScanJob: ");
// HttpHeaders headers =
// createAuthHeader(
// env.getProperty("jenkins.client.user"),
// env.getProperty("jenkins.client.password"));
RestTemplate restTemplate = new RestTemplate();
// HttpEntity<String> entity = new HttpEntity<String>(body.toString(), headers);
HttpEntity<String> entity = new HttpEntity<String>("");
StringBuilder url = new StringBuilder();
url.append(env.getProperty("jenkins.client.url"));
url.append("job/");
url.append(env.getProperty("jenkins.client.scanJob"));
url.append("/buildWithParameters?");
url.append("solutionId=");
url.append(solutionId);
url.append("&revisionId=");
url.append(revisionId);
url.append("&userId=");
url.append(userId);
logger.warn("startScanJob: jenkinsJobUrl {}", url.toString());
ResponseEntity<SVResponse> svResponse =
restTemplate.exchange(url.toString(), HttpMethod.POST, entity, SVResponse.class);
logger.warn("startScanJob: API result {}", svResponse.getBody());
return svResponse;
}
private HttpHeaders createAuthHeader(String username, String password) {
return new HttpHeaders() {
{
String auth = username + ":" + password;
byte[] encodedAuth = Base64.getEncoder().encode(auth.getBytes(Charset.forName("US-ASCII")));
String authHeader = "Basic " + new String(encodedAuth);
set("Authorization", authHeader);
}
};
}
@ApiOperation(
value = "Security Verification Service ScanResult.",
response = SuccessTransport.class)
@RequestMapping(
value =
"/scanresult/"
+ SVServiceConstants.SOLUTIONID
+ "/{solutionId}/"
+ SVServiceConstants.REVISIONID
+ "/{revisionId}",
method = RequestMethod.POST,
produces = MediaType.APPLICATION_JSON_VALUE)
public SVResponse scanResult(
@PathVariable("solutionId") String solutionId,
@PathVariable("revisionId") String revisionId,
@RequestBody String result) {
try {
LogConfig.setEnteringMDCs("security-verification-service", "scanResult");
logger.debug(
"scanResult: solutionId {} revisionId {}, valid request body {}",
solutionId,
revisionId,
result);
try {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
ScanResult scanResultObject = objectMapper.readValue(result, ScanResult.class);
String verifiedLicense = scanResultObject.getVerifiedLicense();
updateVerifiedLicenseStatus(solutionId, verifiedLicense);
String reason = scanResultObject.getReason();
logger.debug(
"scanResult: solutionId {} revisionId {}, valid request, verifiedLicense {}, reason {}",
solutionId,
revisionId,
verifiedLicense,
reason);
ICommonDataServiceRestClient client = getCcdsClient();
UUID uidNumber = UUID.randomUUID();
String uid = uidNumber.toString();
StringBuilder scanResultJsonFilePath = new StringBuilder();
scanResultJsonFilePath.append("/maven/scan/");
scanResultJsonFilePath.append(uid);
scanResultJsonFilePath.append(SVServiceConstants.FORWARD_SLASH);
new File(scanResultJsonFilePath.toString()).mkdirs();
scanResultJsonFilePath.append(SVServiceConstants.SCAN_RESULT_JSON);
File scanResultJsonFile = new File(scanResultJsonFilePath.toString());
BufferedWriter writer = new BufferedWriter(new FileWriter(scanResultJsonFile));
writer.write(result);
writer.close();
logger.debug(
"scanResult: solutionId {} revisionId {}, scanResultJsonFile {}",
solutionId,
revisionId,
scanResultJsonFilePath);
try {
uploadToArtifact(solutionId, revisionId, scanResultJsonFile);
LogConfig.clearMDCDetails();
return new SuccessTransport(HttpServletResponse.SC_OK, null);
} catch (Exception ex) {
logger.warn(", failed to upload artifact: {}", ex.toString());
LogConfig.clearMDCDetails();
return new ErrorTransport(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"saving scanresult failed, failed to upload artifact",
ex);
}
} catch (Exception ex) {
logger.warn("unable to process request body: {}", ex.toString());
LogConfig.clearMDCDetails();
return new ErrorTransport(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
"saving scanresult failed, unable to process request body",
ex);
}
} catch (Exception ex) {
logger.warn("saving scanresult failed: {}", ex.toString());
LogConfig.clearMDCDetails();
return new ErrorTransport(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "saving scanresult failed", ex);
}
}
private void uploadToArtifact(String solutionId, String revisionId, File file)
throws AcumosServiceException, FileNotFoundException {
logger.debug("Inside uploadToArtifact");
if (file != null) {
long fileSizeByKB = file.length();
if (fileSizeByKB > 0) {
logger.debug("In side if conditoin fileSizeByKB {}", fileSizeByKB);
ICommonDataServiceRestClient client =
new CommonDataServiceRestClientImpl(
env.getProperty("cdms.client.url"),
env.getProperty("cdms.client.username"),
env.getProperty("cdms.client.password"),
null);
// client.setRequestId(MDC.get(ONAPLogConstants.MDCs.REQUEST_ID));
MLPSolution mlpSolution = client.getSolution(solutionId);
String userId = mlpSolution.getUserId();
MLPSolutionRevision mlpSolutionRevision =
client.getSolutionRevision(solutionId, revisionId);
UploadArtifactSVOutput uploadArtifactSVOutput = new UploadArtifactSVOutput(env);
uploadArtifactSVOutput.addCreateArtifact(
solutionId, revisionId, mlpSolutionRevision.getVersion(), userId, file);
}
}
}
private void updateVerifiedLicenseStatus(String solutionId, String verifiedLicense) {
logger.debug(
"Inside updateVerifiedLicenseStatus, solutionId: {} Status: {}",
solutionId,
verifiedLicense);
ICommonDataServiceRestClient client =
new CommonDataServiceRestClientImpl(
env.getProperty("cdms.client.url"),
env.getProperty("cdms.client.username"),
env.getProperty("cdms.client.password"),
null);
if (verifiedLicense.equalsIgnoreCase("true")) {
verifiedLicense = "SU";
}
if (verifiedLicense.equalsIgnoreCase("false")) {
verifiedLicense = "FA";
}
List<MLPSolutionRevision> mlpSolutionRevisions = client.getSolutionRevisions(solutionId);
for (MLPSolutionRevision mlpSolutionRevision : mlpSolutionRevisions) {
mlpSolutionRevision.setVerifiedLicense(verifiedLicense);
client.updateSolutionRevision(mlpSolutionRevision);
}
}
@ApiOperation(value = "Add default SiteConfig Verification.")
@RequestMapping(
value = SVServiceConstants.UPDATE_SITE_CONFIG,
method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public SVResponse siteConfigVerification() throws Exception {
logger.debug("Inside siteConfigVerification adding default SiteConfig Verification Json");
try {
LogConfig.setEnteringMDCs("security-verification-service", "siteConfigVerification");
ICommonDataServiceRestClient client = getCcdsClient();
String siteConfigJson = null;
if (client != null) siteConfigJson = createSiteConfig(client);
LogConfig.clearMDCDetails();
return new SuccessTransport(HttpServletResponse.SC_OK, siteConfigJson);
} catch (Exception ex) {
logger.warn("createSiteConfig failed: {}", ex.toString());
return new ErrorTransport(
HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "createSiteConfig failed", ex);
}
}
private String createSiteConfig(ICommonDataServiceRestClient client) throws Exception {
logger.debug("Inside createSiteConfig");
MLPSiteConfig mlpSiteConfig = null;
try {
mlpSiteConfig = client.getSiteConfig(SVServiceConstants.SITE_VERIFICATION_KEY);
} catch (RestClientResponseException ex) {
logger.error("getSiteConfig failed, server reports: {}", ex.getResponseBodyAsString());
throw ex;
}
if (StringUtils.isEmpty(mlpSiteConfig)) {
logger.debug("createSiteConfig: no siteConfig verification key in database");
String siteConfigJsonFromConfiguration = env.getProperty("siteConfig");
logger.debug("siteConfig.verification env: {} ", siteConfigJsonFromConfiguration);
MLPSiteConfig config = new MLPSiteConfig();
config.setConfigKey(SVServiceConstants.SITE_VERIFICATION_KEY);
config.setConfigValue(siteConfigJsonFromConfiguration);
try {
logger.debug("createSiteConfig: setting value {}", config.getConfigValue());
mlpSiteConfig = client.createSiteConfig(config);
logger.debug("createSiteConfig: result {}", mlpSiteConfig.getConfigValue());
return mlpSiteConfig.getConfigValue();
} catch (RestClientResponseException ex) {
logger.error("createSiteConfig failed, server reports: {}", ex.getResponseBodyAsString());
throw ex;
}
} else {
return mlpSiteConfig.getConfigValue();
}
}
private ICommonDataServiceRestClient getCcdsClient() {
logger.debug("Inside getCcdsClient");
ICommonDataServiceRestClient client =
new CommonDataServiceRestClientImpl(
env.getProperty(SVServiceConstants.CDMS_CLIENT_URL),
env.getProperty(SVServiceConstants.CDMS_CLIENT_USER),
env.getProperty(SVServiceConstants.CDMS_CLIENT_PWD),
null);
return client;
}
}