Skip to content

Commit ffe1948

Browse files
authored
Merge pull request #11 from secureCodeBox/feature/make-max-pages-configurable
Add config option for max number of pages fetched
2 parents b4945c8 + 43182de commit ffe1948

File tree

5 files changed

+26
-39
lines changed

5 files changed

+26
-39
lines changed

.github/workflows/gradle.yml

Lines changed: 0 additions & 24 deletions
This file was deleted.

.github/workflows/test.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will test a Java project with Gradle
22
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
33

4-
name: Publish to Maven Central
4+
name: Java Tests
55
on: push
66

77
jobs:
@@ -15,5 +15,7 @@ jobs:
1515
java-version: 1.11
1616
- name: Grant execute permission for gradlew
1717
run: chmod +x gradlew
18+
- name: Build with Gradle
19+
run: ./gradlew build
1820
- name: Build with Gradle
1921
run: ./gradlew test

src/main/java/io/securecodebox/persistence/defectdojo/config/DefectDojoConfig.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,21 @@ public class DefectDojoConfig {
3131
@Getter
3232
private final String username;
3333

34+
/**
35+
* Determines how many apiPages of Objects are fetched before giving up and failing to avoid outOfMemory scenarios.
36+
*/
37+
@Getter
38+
private final int maxPageCountForGets;
39+
3440
public static DefectDojoConfig fromEnv(){
3541
String url = System.getenv("DEFECTDOJO_URL");
3642
String username = System.getenv("DEFECTDOJO_USERNAME");
3743
String apiKey = System.getenv("DEFECTDOJO_APIKEY");
38-
return new DefectDojoConfig(url, apiKey, username);
44+
45+
int maxPageCountForGets = 100;
46+
if (System.getenv("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS") != null) {
47+
maxPageCountForGets = Integer.parseInt(System.getenv("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS"));
48+
}
49+
return new DefectDojoConfig(url, apiKey, username, maxPageCountForGets);
3950
}
4051
}

src/main/java/io/securecodebox/persistence/defectdojo/service/GenericDefectDojoService.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@
3838
import java.util.*;
3939

4040
abstract public class GenericDefectDojoService<T extends DefectDojoModel> {
41-
protected String defectDojoUrl;
42-
protected String defectDojoApiKey;
41+
protected DefectDojoConfig defectDojoConfig;
4342

4443
protected ObjectMapper objectMapper;
4544
protected ObjectMapper searchStringMapper;
4645

4746
public GenericDefectDojoService(DefectDojoConfig config) {
48-
this.defectDojoUrl = config.getUrl();
49-
this.defectDojoApiKey = config.getApiKey();
47+
this.defectDojoConfig = config;
5048

5149
this.objectMapper = new ObjectMapper();
5250
this.objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
@@ -65,7 +63,7 @@ public GenericDefectDojoService(DefectDojoConfig config) {
6563
*/
6664
private HttpHeaders getDefectDojoAuthorizationHeaders() {
6765
HttpHeaders headers = new HttpHeaders();
68-
headers.set("Authorization", "Token " + defectDojoApiKey);
66+
headers.set("Authorization", "Token " + this.defectDojoConfig.getApiKey());
6967
return headers;
7068
}
7169

@@ -80,7 +78,7 @@ public T get(long id) {
8078
HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders());
8179

8280
ResponseEntity<T> response = restTemplate.exchange(
83-
defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/" + id,
81+
this.defectDojoConfig.getUrl() + "/api/v2/" + this.getUrlPath() + "/" + id,
8482
HttpMethod.GET,
8583
payload,
8684
getModelClass()
@@ -103,7 +101,7 @@ protected DefectDojoResponse<T> internalSearch(Map<String, Object> queryParams,
103101
multiValueMap.set(entry.getKey(), String.valueOf(entry.getValue()));
104102
}
105103

106-
var url = new URI(defectDojoUrl + "/api/v2/" + this.getUrlPath() + "/");
104+
var url = new URI(this.defectDojoConfig.getUrl() + "/api/v2/" + this.getUrlPath() + "/");
107105
var uriBuilder = UriComponentsBuilder.fromUri(url).queryParams(multiValueMap);
108106

109107
ResponseEntity<String> responseString = restTemplate.exchange(
@@ -126,8 +124,8 @@ public List<T> search(Map<String, Object> queryParams) throws URISyntaxException
126124
objects.addAll(response.getResults());
127125

128126
hasNext = response.getNext() != null;
129-
if (page > 100) {
130-
throw new DefectDojoLoopException("Found too many response object. Quitting after " + page + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each.");
127+
if (page > this.defectDojoConfig.getMaxPageCountForGets()) {
128+
throw new DefectDojoLoopException("Found too many response object. Quitting after " + (page - 1) + " paginated API pages of " + DEFECT_DOJO_OBJET_LIMIT + " each.");
131129
}
132130
} while (hasNext);
133131

@@ -161,22 +159,22 @@ public T create(T object) {
161159
RestTemplate restTemplate = new RestTemplate();
162160
HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders());
163161

164-
ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass());
162+
ResponseEntity<T> response = restTemplate.exchange(this.defectDojoConfig.getUrl() + "/api/v2/" + getUrlPath() + "/", HttpMethod.POST, payload, getModelClass());
165163
return response.getBody();
166164
}
167165

168166
public void delete(long id) {
169167
RestTemplate restTemplate = new RestTemplate();
170168
HttpEntity<String> payload = new HttpEntity<>(getDefectDojoAuthorizationHeaders());
171169

172-
restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class);
170+
restTemplate.exchange(this.defectDojoConfig.getUrl() + "/api/v2/" + getUrlPath() + "/" + id + "/", HttpMethod.DELETE, payload, String.class);
173171
}
174172

175173
public T update(T object, long objectId) {
176174
RestTemplate restTemplate = new RestTemplate();
177175
HttpEntity<T> payload = new HttpEntity<T>(object, getDefectDojoAuthorizationHeaders());
178176

179-
ResponseEntity<T> response = restTemplate.exchange(defectDojoUrl + "/api/v2/" + getUrlPath() + "/" + objectId + "/", HttpMethod.PUT, payload, getModelClass());
177+
ResponseEntity<T> response = restTemplate.exchange(this.defectDojoConfig.getUrl() + "/api/v2/" + getUrlPath() + "/" + objectId + "/", HttpMethod.PUT, payload, getModelClass());
180178
return response.getBody();
181179
}
182180
}

src/test/java/io/securecodebox/persistence/defectdojo/service/FindingServiceTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ class FindingServiceTest {
113113

114114
@BeforeEach
115115
void setup() {
116-
config = new DefectDojoConfig("https://defectdojo.example.com", "abc", "test-user");
116+
config = new DefectDojoConfig("https://defectdojo.example.com", "abc", "test-user", 42);
117117
underTest = new FindingService(config);
118118
}
119119

0 commit comments

Comments
 (0)