|
23 | 23 | import io.securecodebox.persistence.defectdojo.exceptions.DefectDojoPersistenceException; |
24 | 24 | import io.securecodebox.persistence.defectdojo.models.ScanFile; |
25 | 25 | import lombok.Data; |
| 26 | +import org.apache.http.HttpHost; |
| 27 | +import org.apache.http.auth.AuthScope; |
| 28 | +import org.apache.http.auth.UsernamePasswordCredentials; |
| 29 | +import org.apache.http.client.CredentialsProvider; |
| 30 | +import org.apache.http.impl.client.BasicCredentialsProvider; |
| 31 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 32 | +import org.apache.http.impl.client.HttpClientBuilder; |
| 33 | +import org.apache.http.impl.client.ProxyAuthenticationStrategy; |
26 | 34 | import org.springframework.core.io.ByteArrayResource; |
27 | 35 | import org.springframework.http.HttpEntity; |
28 | 36 | import org.springframework.http.HttpHeaders; |
29 | 37 | import org.springframework.http.HttpMethod; |
30 | 38 | import org.springframework.http.MediaType; |
| 39 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
31 | 40 | import org.springframework.http.converter.FormHttpMessageConverter; |
32 | 41 | import org.springframework.http.converter.ResourceHttpMessageConverter; |
33 | 42 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter; |
|
41 | 50 |
|
42 | 51 | public class ImportScanService { |
43 | 52 |
|
44 | | - protected String defectDojoUrl; |
45 | | - protected String defectDojoApiKey; |
46 | | - |
47 | | - public ImportScanService(DefectDojoConfig config){ |
48 | | - this.defectDojoUrl = config.getUrl(); |
49 | | - this.defectDojoApiKey = config.getApiKey(); |
50 | | - } |
51 | | - |
52 | | - /** |
53 | | - * @return The DefectDojo Authentication Header |
54 | | - */ |
55 | | - private HttpHeaders getDefectDojoAuthorizationHeaders() { |
56 | | - HttpHeaders headers = new HttpHeaders(); |
57 | | - headers.set("Authorization", "Token " + defectDojoApiKey); |
58 | | - return headers; |
59 | | - } |
60 | | - |
61 | | - /** |
62 | | - * Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterwards, you can have somethings else |
63 | | - */ |
64 | | - protected ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, Object> options) { |
65 | | - RestTemplate restTemplate = new RestTemplate(); |
66 | | - HttpHeaders headers = getDefectDojoAuthorizationHeaders(); |
67 | | - headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
68 | | - restTemplate.setMessageConverters(List.of( |
69 | | - new FormHttpMessageConverter(), |
70 | | - new ResourceHttpMessageConverter(), |
71 | | - new MappingJackson2HttpMessageConverter()) |
72 | | - ); |
73 | | - |
74 | | - MultiValueMap<String, Object> mvn = new LinkedMultiValueMap<>(); |
75 | | - |
76 | | - mvn.add("lead", Long.toString(lead)); |
77 | | - mvn.add("scan_date", currentDate); |
78 | | - mvn.add("scan_type", scanType.getTestType()); |
79 | | - mvn.add("close_old_findings", "true"); |
80 | | - mvn.add("skip_duplicates", "false"); |
81 | | - mvn.add("test_type", String.valueOf(testType)); |
82 | | - |
83 | | - for (String theKey : options.keySet()) { |
84 | | - mvn.remove(theKey); |
| 53 | + protected String defectDojoUrl; |
| 54 | + protected String defectDojoApiKey; |
| 55 | + |
| 56 | + public ImportScanService(DefectDojoConfig config) { |
| 57 | + this.defectDojoUrl = config.getUrl(); |
| 58 | + this.defectDojoApiKey = config.getApiKey(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @return The DefectDojo Authentication Header |
| 63 | + */ |
| 64 | + private HttpHeaders getDefectDojoAuthorizationHeaders() { |
| 65 | + HttpHeaders headers = new HttpHeaders(); |
| 66 | + headers.set("Authorization", "Token " + defectDojoApiKey); |
| 67 | + return headers; |
| 68 | + } |
| 69 | + |
| 70 | + protected RestTemplate getRestTemplate() { |
| 71 | + if (System.getProperty("http.proxyUser") != null && System.getProperty("http.proxyPassword") != null) { |
| 72 | + // Configuring Proxy Authentication explicitly as it isn't done by default for spring rest templates :( |
| 73 | + CredentialsProvider credsProvider = new BasicCredentialsProvider(); |
| 74 | + credsProvider.setCredentials( |
| 75 | + new AuthScope(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort"))), |
| 76 | + new UsernamePasswordCredentials(System.getProperty("http.proxyUser"), System.getProperty("http.proxyPassword")) |
| 77 | + ); |
| 78 | + HttpClientBuilder clientBuilder = HttpClientBuilder.create(); |
| 79 | + |
| 80 | + clientBuilder.useSystemProperties(); |
| 81 | + clientBuilder.setProxy(new HttpHost(System.getProperty("http.proxyHost"), Integer.parseInt(System.getProperty("http.proxyPort")))); |
| 82 | + clientBuilder.setDefaultCredentialsProvider(credsProvider); |
| 83 | + clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); |
| 84 | + |
| 85 | + CloseableHttpClient client = clientBuilder.build(); |
| 86 | + |
| 87 | + HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); |
| 88 | + factory.setHttpClient(client); |
| 89 | + return new RestTemplate(factory); |
| 90 | + } else { |
| 91 | + return new RestTemplate(); |
| 92 | + } |
85 | 93 | } |
86 | | - mvn.addAll(options); |
87 | 94 |
|
88 | | - try { |
89 | | - ByteArrayResource contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) { |
90 | | - @Override |
91 | | - public String getFilename() { |
92 | | - return scanFile.getName(); |
| 95 | + /** |
| 96 | + * Before version 1.5.4. testName (in DefectDojo _test_type_) must be defectDojoScanName, afterwards, you can have somethings else |
| 97 | + */ |
| 98 | + protected ImportScanResponse createFindings(ScanFile scanFile, String endpoint, long lead, String currentDate, ScanType scanType, long testType, MultiValueMap<String, Object> options) { |
| 99 | + var restTemplate = this.getRestTemplate(); |
| 100 | + HttpHeaders headers = getDefectDojoAuthorizationHeaders(); |
| 101 | + headers.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 102 | + restTemplate.setMessageConverters(List.of( |
| 103 | + new FormHttpMessageConverter(), |
| 104 | + new ResourceHttpMessageConverter(), |
| 105 | + new MappingJackson2HttpMessageConverter()) |
| 106 | + ); |
| 107 | + |
| 108 | + MultiValueMap<String, Object> mvn = new LinkedMultiValueMap<>(); |
| 109 | + |
| 110 | + mvn.add("lead", Long.toString(lead)); |
| 111 | + mvn.add("scan_date", currentDate); |
| 112 | + mvn.add("scan_type", scanType.getTestType()); |
| 113 | + mvn.add("close_old_findings", "true"); |
| 114 | + mvn.add("skip_duplicates", "false"); |
| 115 | + mvn.add("test_type", String.valueOf(testType)); |
| 116 | + |
| 117 | + for (String theKey : options.keySet()) { |
| 118 | + mvn.remove(theKey); |
93 | 119 | } |
94 | | - }; |
| 120 | + mvn.addAll(options); |
95 | 121 |
|
96 | | - mvn.add("file", contentsAsResource); |
| 122 | + try { |
| 123 | + ByteArrayResource contentsAsResource = new ByteArrayResource(scanFile.getContent().getBytes(StandardCharsets.UTF_8)) { |
| 124 | + @Override |
| 125 | + public String getFilename() { |
| 126 | + return scanFile.getName(); |
| 127 | + } |
| 128 | + }; |
97 | 129 |
|
98 | | - var payload = new HttpEntity<>(mvn, headers); |
| 130 | + mvn.add("file", contentsAsResource); |
99 | 131 |
|
100 | | - return restTemplate.exchange(defectDojoUrl + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody(); |
101 | | - } catch (HttpClientErrorException e) { |
102 | | - throw new DefectDojoPersistenceException("Failed to attach findings to engagement."); |
| 132 | + var payload = new HttpEntity<>(mvn, headers); |
| 133 | + |
| 134 | + return restTemplate.exchange(defectDojoUrl + "/api/v2/" + endpoint + "/", HttpMethod.POST, payload, ImportScanResponse.class).getBody(); |
| 135 | + } catch (HttpClientErrorException e) { |
| 136 | + throw new DefectDojoPersistenceException("Failed to attach findings to engagement."); |
| 137 | + } |
103 | 138 | } |
104 | | - } |
105 | 139 |
|
106 | | - public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) { |
107 | | - var additionalValues = new LinkedMultiValueMap<String, Object>(); |
108 | | - additionalValues.add("engagement", Long.toString(engagementId)); |
| 140 | + public ImportScanResponse importScan(ScanFile scanFile, long engagementId, long lead, String currentDate, ScanType scanType, long testType) { |
| 141 | + var additionalValues = new LinkedMultiValueMap<String, Object>(); |
| 142 | + additionalValues.add("engagement", Long.toString(engagementId)); |
109 | 143 |
|
110 | | - return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, additionalValues); |
111 | | - } |
| 144 | + return this.createFindings(scanFile, "import-scan", lead, currentDate, scanType, testType, additionalValues); |
| 145 | + } |
112 | 146 |
|
113 | | - public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) { |
114 | | - var additionalValues = new LinkedMultiValueMap<String, Object>(); |
115 | | - additionalValues.add("test", Long.toString(testId)); |
| 147 | + public ImportScanResponse reimportScan(ScanFile scanFile, long testId, long lead, String currentDate, ScanType scanType, long testType) { |
| 148 | + var additionalValues = new LinkedMultiValueMap<String, Object>(); |
| 149 | + additionalValues.add("test", Long.toString(testId)); |
116 | 150 |
|
117 | | - return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, additionalValues); |
118 | | - } |
| 151 | + return this.createFindings(scanFile, "reimport-scan", lead, currentDate, scanType, testType, additionalValues); |
| 152 | + } |
119 | 153 |
|
120 | | - @Data |
121 | | - public static class ImportScanResponse { |
122 | | - @JsonProperty |
123 | | - protected Boolean verified; |
| 154 | + @Data |
| 155 | + public static class ImportScanResponse { |
| 156 | + @JsonProperty |
| 157 | + protected Boolean verified; |
124 | 158 |
|
125 | | - @JsonProperty |
126 | | - protected Boolean active; |
| 159 | + @JsonProperty |
| 160 | + protected Boolean active; |
127 | 161 |
|
128 | | - @JsonProperty("test") |
129 | | - protected long testId; |
130 | | - } |
| 162 | + @JsonProperty("test") |
| 163 | + protected long testId; |
| 164 | + } |
131 | 165 | } |
0 commit comments