Skip to content

Commit 42f18ba

Browse files
WeltraumschafZero3141
authored andcommitted
#51 Remove deprecated config api for username and userid
Since DefectDojo supports thedetermination ofuser info directly from the auth token, this is not needed anymore. Signed-off-by: Sven Strittmatter <sven.strittmatter@iteratec.com>
1 parent 882fcde commit 42f18ba

File tree

7 files changed

+23
-121
lines changed

7 files changed

+23
-121
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>io.securecodebox</groupId>
66
<artifactId>defectdojo-client</artifactId>
7-
<version>1.0.2-SNAPSHOT</version>
7+
<version>2.0.0-SNAPSHOT</version>
88
<packaging>jar</packaging>
99
<name>DefectDojo Client Java</name>
1010
<description>

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

Lines changed: 4 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public final class Config {
2020
* Default for {@link #maxPageCountForGets}
2121
*/
2222
static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100;
23+
2324
/**
2425
* URL of the host which serves the DefectDojo API.
2526
* <p>
@@ -29,24 +30,12 @@ public final class Config {
2930
*/
3031
@NonNull
3132
private final String url;
33+
3234
/**
3335
* API key to authorize against the DefectDojo API.
3436
*/
3537
@NonNull
3638
private final String apiKey;
37-
/**
38-
* This name is used to set the creator of entities created in DefectDojo (findings etc.).
39-
* <p>
40-
* Since DefectDojo requires the id of the user this client lib must do a lookup to determine the according id.
41-
* This does not work, if the user does nit have appropriate privileges. In this case you can set the {@link #userId}
42-
* directly with the appropriate id of the user you want as creator.
43-
* </p>
44-
*
45-
* @deprecated Must not be used anymore because we determine the userid via user_profile API endpoint.
46-
*/
47-
@NonNull
48-
@Deprecated
49-
private final String username;
5039

5140
/**
5241
* How many pages of objects are fetched from the DefectDojo API
@@ -59,36 +48,18 @@ public final class Config {
5948
*/
6049
private final int maxPageCountForGets;
6150

62-
/**
63-
* Overwrite the creator by userid
64-
* <p>
65-
* <strong>IMPORTANT</strong>: If this is set (not {@code null}) the {@link #username} is ignored!
66-
* </p>
67-
* <p>
68-
* This option is necessary, if the user belonging to the {@link #apiKey} has no privilege to determine it's userid.
69-
* </p>
70-
*
71-
* @deprecated Must not be used anymore because we determine the userid via user_profile API endpoint.
72-
*/
73-
@Deprecated
74-
private final Long userId;
75-
7651
/**
7752
* Dedicated constructor
7853
*
7954
* @param url not {@code null}
8055
* @param apiKey not {@code null}
81-
* @param username not {@code null}
8256
* @param maxPageCountForGets not less than 1
83-
* @param userId may be {@code null} (see {@link #userId})
8457
*/
85-
public Config(final @NonNull String url, final @NonNull String apiKey, final @NonNull String username, final int maxPageCountForGets, final Long userId) {
58+
public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) {
8659
super();
8760
this.url = url;
8861
this.apiKey = apiKey;
89-
this.username = username;
9062
this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets");
91-
this.userId = userId;
9263
}
9364

9465
private static int validateIsGreaterZero(final int number, final String name) {
@@ -99,38 +70,14 @@ private static int validateIsGreaterZero(final int number, final String name) {
9970
return number;
10071
}
10172

102-
/**
103-
* Default constructor which sets {@link #userId} to {@code null}
104-
*
105-
* @param url not {@code null}
106-
* @param apiKey not {@code null}
107-
* @param username not {@code null}
108-
* @param maxPageCountForGets not less than 1
109-
*/
110-
public Config(final String url, final String apiKey, final String username, final int maxPageCountForGets) {
111-
this(url, apiKey, username, maxPageCountForGets, null);
112-
}
113-
11473
/**
11574
* Creates config from environment variables
11675
*
11776
* @return never {@code null}
11877
*/
11978
public static Config fromEnv() {
12079
final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL);
121-
final var username = findRequiredEnvVar(EnvVars.DEFECTDOJO_USERNAME);
12280
final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY);
123-
final Long userId;
124-
125-
try {
126-
userId = Optional.ofNullable(findEnvVar(EnvVars.DEFECTDOJO_USER_ID))
127-
.map(Long::parseLong).orElse(null);
128-
} catch (final NumberFormatException e) {
129-
throw new ConfigException(
130-
String.format("Given user id for environment variable '%s' is not a valid id! Given was '%s'.", EnvVars.DEFECTDOJO_USER_ID.literal, findEnvVar(EnvVars.DEFECTDOJO_USER_ID)),
131-
e);
132-
}
133-
13481
final int maxPageCountForGets;
13582

13683
if (hasEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)) {
@@ -144,7 +91,7 @@ public static Config fromEnv() {
14491
maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS;
14592
}
14693

147-
return new Config(url, apiKey, username, maxPageCountForGets, userId);
94+
return new Config(url, apiKey, maxPageCountForGets);
14895
}
14996

15097
private static boolean hasEnvVar(final @NonNull EnvVars name) {
@@ -169,9 +116,7 @@ private static String findRequiredEnvVar(final @NonNull EnvVars name) {
169116
*/
170117
public enum EnvVars {
171118
DEFECTDOJO_URL("DEFECTDOJO_URL"),
172-
DEFECTDOJO_USERNAME("DEFECTDOJO_USERNAME"),
173119
DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"),
174-
DEFECTDOJO_USER_ID("DEFECTDOJO_USER_ID"),
175120
DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS");
176121
/**
177122
* Literal name of configuration environment name

src/test/java/io/securecodebox/persistence/defectdojo/config/ConfigTest.java

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ConfigTest {
3030
@Test
3131
void constructor_urlMustNotBeNull() {
3232
final var thrown = assertThrows(NullPointerException.class, () -> {
33-
new Config(null, "apiKey", "username", 1, null);
33+
new Config(null, "apiKey", 1);
3434
});
3535

3636
assertThat(thrown.getMessage(), startsWith("url "));
@@ -39,112 +39,73 @@ void constructor_urlMustNotBeNull() {
3939
@Test
4040
void constructor_apiKeyMustNotBeNull() {
4141
final var thrown = assertThrows(NullPointerException.class, () -> {
42-
new Config("url", null, "username", 1, null);
42+
new Config("url", null, 1);
4343
});
4444

4545
assertThat(thrown.getMessage(), startsWith("apiKey "));
4646
}
4747

48-
@Test
49-
void constructor_usernameMustNotBeNull() {
50-
final var thrown = assertThrows(NullPointerException.class, () -> {
51-
new Config("url", "apiKey", null, 1, null);
52-
});
53-
54-
assertThat(thrown.getMessage(), startsWith("username "));
55-
}
56-
5748
@ParameterizedTest
5849
@ValueSource(ints = {0, -1, -2, -23, -42, Integer.MIN_VALUE})
5950
void constructor_maxPageCountForGetsMustNotBeLessThanOne(final int number) {
6051
final var thrown = assertThrows(IllegalArgumentException.class, () -> {
61-
new Config("url", "apiKey", "username", number, null);
52+
new Config("url", "apiKey", number);
6253
});
6354

6455
assertThat(thrown.getMessage(), startsWith("maxPageCountForGets "));
6556
}
6657

6758
@Test
6859
void fromEnv() {
69-
environmentVariables.set("DEFECTDOJO_URL", "url")
70-
.set("DEFECTDOJO_USERNAME", "username")
60+
environmentVariables
61+
.set("DEFECTDOJO_URL", "url")
7162
.set("DEFECTDOJO_APIKEY", "apikey")
72-
.set("DEFECTDOJO_USER_ID", "42")
7363
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "23");
7464

7565
final var sut = Config.fromEnv();
7666

7767
assertAll(
7868
() -> assertThat(sut.getUrl(), is("url")),
79-
() -> assertThat(sut.getUsername(), is("username")),
8069
() -> assertThat(sut.getApiKey(), is("apikey")),
81-
() -> assertThat(sut.getUserId(), is(42L)),
8270
() -> assertThat(sut.getMaxPageCountForGets(), is(23))
8371
);
8472
}
8573

8674
@Test
8775
void fromEnv_throwsExceptionIfNoUrlSet() {
88-
environmentVariables.set("DEFECTDOJO_USERNAME", "username")
89-
.set("DEFECTDOJO_APIKEY", "apikey")
90-
.set("DEFECTDOJO_USER_ID", "42");
76+
environmentVariables
77+
.set("DEFECTDOJO_APIKEY", "apikey");
9178

9279
final var thrown = assertThrows(ConfigException.class, Config::fromEnv);
9380

9481
assertThat(thrown.getMessage(), is("Missing environment variable 'DEFECTDOJO_URL'!"));
9582
}
9683

97-
@Test
98-
void fromEnv_throwsExceptionIfNoUserNameSet() {
99-
environmentVariables.set("DEFECTDOJO_URL", "url")
100-
.set("DEFECTDOJO_APIKEY", "apikey")
101-
.set("DEFECTDOJO_USER_ID", "42");
102-
103-
final var thrown = assertThrows(ConfigException.class, Config::fromEnv);
104-
105-
assertThat(thrown.getMessage(), is("Missing environment variable 'DEFECTDOJO_USERNAME'!"));
106-
}
107-
10884
@Test
10985
void fromEnv_throwsExceptionIfNoApiKeySet() {
110-
environmentVariables.set("DEFECTDOJO_URL", "url")
111-
.set("DEFECTDOJO_USERNAME", "username")
112-
.set("DEFECTDOJO_USER_ID", "42");
86+
environmentVariables
87+
.set("DEFECTDOJO_URL", "url");
11388

11489
final var thrown = assertThrows(ConfigException.class, Config::fromEnv);
11590

11691
assertThat(thrown.getMessage(), is("Missing environment variable 'DEFECTDOJO_APIKEY'!"));
11792
}
11893

119-
@Test
120-
void fromEnv_throwsExceptionIfUserIdIsNotParsableToLong() {
121-
environmentVariables.set("DEFECTDOJO_URL", "url")
122-
.set("DEFECTDOJO_USERNAME", "username")
123-
.set("DEFECTDOJO_APIKEY", "apikey")
124-
.set("DEFECTDOJO_USER_ID", "foo");
125-
126-
final var thrown = assertThrows(ConfigException.class, Config::fromEnv);
127-
128-
assertThat(thrown.getMessage(), is("Given user id for environment variable 'DEFECTDOJO_USER_ID' is not a valid id! Given was 'foo'."));
129-
}
130-
13194
@Test
13295
void fromEnv_usesDefaultIfNoMaxPageCountForGetSet() {
133-
environmentVariables.set("DEFECTDOJO_URL", "url")
134-
.set("DEFECTDOJO_USERNAME", "username")
135-
.set("DEFECTDOJO_APIKEY", "apikey")
136-
.set("DEFECTDOJO_USER_ID", "42");
96+
environmentVariables
97+
.set("DEFECTDOJO_URL", "url")
98+
.set("DEFECTDOJO_APIKEY", "apikey");
13799

138100
final var sut = Config.fromEnv();
139101
assertThat(sut.getMaxPageCountForGets(), is(Config.DEFAULT_MAX_PAGE_COUNT_FOR_GETS));
140102
}
141103

142104
@Test
143105
void fromEnv_throwsExceptionIfMaxPageCountForGetIsNotParseableToInteger() {
144-
environmentVariables.set("DEFECTDOJO_URL", "url")
145-
.set("DEFECTDOJO_USERNAME", "username")
106+
environmentVariables
107+
.set("DEFECTDOJO_URL", "url")
146108
.set("DEFECTDOJO_APIKEY", "apikey")
147-
.set("DEFECTDOJO_USER_ID", "42")
148109
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "foo");
149110

150111
final var thrown = assertThrows(ConfigException.class, Config::fromEnv);

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ class DefaultImportScanServiceTest {
2020
private final Config config = new Config(
2121
"http://localhost",
2222
"apiKey",
23-
"username",
24-
23,
25-
42L
23+
23
2624
);
2725
private final DefaultImportScanService sut = new DefaultImportScanService(config);
2826

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

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

132132
@BeforeEach
133133
void setup() {
134-
config = new Config("https://defectdojo.example.com", "abc", "test-user", 42);
134+
config = new Config("https://defectdojo.example.com", "abc", 42);
135135
underTest = new FindingService(config);
136136
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
137137
}

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ void createDefault_passesConfig() {
3636
final var config = new Config(
3737
"url",
3838
"apiKey",
39-
"username",
40-
23,
41-
42L
39+
23
4240
);
4341

4442
final var sut = (DefaultImportScanService) ImportScanService.createDefault(config);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class UserProfileServiceTest {
4848

4949
@BeforeEach
5050
void setup() {
51-
config = new Config("https://defectdojo.example.com", "abc", "test-user", 42);
51+
config = new Config("https://defectdojo.example.com", "abc", 42);
5252
underTest = new UserProfileService(config);
5353
mockServer = MockRestServiceServer.createServer(underTest.getRestTemplate());
5454
}

0 commit comments

Comments
 (0)