Skip to content

Remove deprecated api #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>io.securecodebox</groupId>
<artifactId>defectdojo-client</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>2.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>DefectDojo Client Java</name>
<description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public final class Config {
* Default for {@link #maxPageCountForGets}
*/
static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100;

/**
* URL of the host which serves the DefectDojo API.
* <p>
Expand All @@ -29,24 +30,12 @@ public final class Config {
*/
@NonNull
private final String url;

/**
* API key to authorize against the DefectDojo API.
*/
@NonNull
private final String apiKey;
/**
* This name is used to set the creator of entities created in DefectDojo (findings etc.).
* <p>
* Since DefectDojo requires the id of the user this client lib must do a lookup to determine the according id.
* This does not work, if the user does nit have appropriate privileges. In this case you can set the {@link #userId}
* directly with the appropriate id of the user you want as creator.
* </p>
*
* @deprecated Must not be used anymore because we determine the userid via user_profile API endpoint.
*/
@NonNull
@Deprecated
private final String username;

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

/**
* Overwrite the creator by userid
* <p>
* <strong>IMPORTANT</strong>: If this is set (not {@code null}) the {@link #username} is ignored!
* </p>
* <p>
* This option is necessary, if the user belonging to the {@link #apiKey} has no privilege to determine it's userid.
* </p>
*
* @deprecated Must not be used anymore because we determine the userid via user_profile API endpoint.
*/
@Deprecated
private final Long userId;

/**
* Dedicated constructor
*
* @param url not {@code null}
* @param apiKey not {@code null}
* @param username not {@code null}
* @param maxPageCountForGets not less than 1
* @param userId may be {@code null} (see {@link #userId})
*/
public Config(final @NonNull String url, final @NonNull String apiKey, final @NonNull String username, final int maxPageCountForGets, final Long userId) {
public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) {
super();
this.url = url;
this.apiKey = apiKey;
this.username = username;
this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets");
this.userId = userId;
}

private static int validateIsGreaterZero(final int number, final String name) {
Expand All @@ -99,38 +70,14 @@ private static int validateIsGreaterZero(final int number, final String name) {
return number;
}

/**
* Default constructor which sets {@link #userId} to {@code null}
*
* @param url not {@code null}
* @param apiKey not {@code null}
* @param username not {@code null}
* @param maxPageCountForGets not less than 1
*/
public Config(final String url, final String apiKey, final String username, final int maxPageCountForGets) {
this(url, apiKey, username, maxPageCountForGets, null);
}

/**
* Creates config from environment variables
*
* @return never {@code null}
*/
public static Config fromEnv() {
final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL);
final var username = findRequiredEnvVar(EnvVars.DEFECTDOJO_USERNAME);
final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY);
final Long userId;

try {
userId = Optional.ofNullable(findEnvVar(EnvVars.DEFECTDOJO_USER_ID))
.map(Long::parseLong).orElse(null);
} catch (final NumberFormatException e) {
throw new ConfigException(
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)),
e);
}

final int maxPageCountForGets;

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

return new Config(url, apiKey, username, maxPageCountForGets, userId);
return new Config(url, apiKey, maxPageCountForGets);
}

private static boolean hasEnvVar(final @NonNull EnvVars name) {
Expand All @@ -169,9 +116,7 @@ private static String findRequiredEnvVar(final @NonNull EnvVars name) {
*/
public enum EnvVars {
DEFECTDOJO_URL("DEFECTDOJO_URL"),
DEFECTDOJO_USERNAME("DEFECTDOJO_USERNAME"),
DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"),
DEFECTDOJO_USER_ID("DEFECTDOJO_USER_ID"),
DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS");
/**
* Literal name of configuration environment name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConfigTest {
@Test
void constructor_urlMustNotBeNull() {
final var thrown = assertThrows(NullPointerException.class, () -> {
new Config(null, "apiKey", "username", 1, null);
new Config(null, "apiKey", 1);
});

assertThat(thrown.getMessage(), startsWith("url "));
Expand All @@ -39,112 +39,73 @@ void constructor_urlMustNotBeNull() {
@Test
void constructor_apiKeyMustNotBeNull() {
final var thrown = assertThrows(NullPointerException.class, () -> {
new Config("url", null, "username", 1, null);
new Config("url", null, 1);
});

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

@Test
void constructor_usernameMustNotBeNull() {
final var thrown = assertThrows(NullPointerException.class, () -> {
new Config("url", "apiKey", null, 1, null);
});

assertThat(thrown.getMessage(), startsWith("username "));
}

@ParameterizedTest
@ValueSource(ints = {0, -1, -2, -23, -42, Integer.MIN_VALUE})
void constructor_maxPageCountForGetsMustNotBeLessThanOne(final int number) {
final var thrown = assertThrows(IllegalArgumentException.class, () -> {
new Config("url", "apiKey", "username", number, null);
new Config("url", "apiKey", number);
});

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

@Test
void fromEnv() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_USERNAME", "username")
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "42")
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "23");

final var sut = Config.fromEnv();

assertAll(
() -> assertThat(sut.getUrl(), is("url")),
() -> assertThat(sut.getUsername(), is("username")),
() -> assertThat(sut.getApiKey(), is("apikey")),
() -> assertThat(sut.getUserId(), is(42L)),
() -> assertThat(sut.getMaxPageCountForGets(), is(23))
);
}

@Test
void fromEnv_throwsExceptionIfNoUrlSet() {
environmentVariables.set("DEFECTDOJO_USERNAME", "username")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "42");
environmentVariables
.set("DEFECTDOJO_APIKEY", "apikey");

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

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

@Test
void fromEnv_throwsExceptionIfNoUserNameSet() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "42");

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

assertThat(thrown.getMessage(), is("Missing environment variable 'DEFECTDOJO_USERNAME'!"));
}

@Test
void fromEnv_throwsExceptionIfNoApiKeySet() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_USERNAME", "username")
.set("DEFECTDOJO_USER_ID", "42");
environmentVariables
.set("DEFECTDOJO_URL", "url");

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

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

@Test
void fromEnv_throwsExceptionIfUserIdIsNotParsableToLong() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_USERNAME", "username")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "foo");

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

assertThat(thrown.getMessage(), is("Given user id for environment variable 'DEFECTDOJO_USER_ID' is not a valid id! Given was 'foo'."));
}

@Test
void fromEnv_usesDefaultIfNoMaxPageCountForGetSet() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_USERNAME", "username")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "42");
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey");

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

@Test
void fromEnv_throwsExceptionIfMaxPageCountForGetIsNotParseableToInteger() {
environmentVariables.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_USERNAME", "username")
environmentVariables
.set("DEFECTDOJO_URL", "url")
.set("DEFECTDOJO_APIKEY", "apikey")
.set("DEFECTDOJO_USER_ID", "42")
.set("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS", "foo");

final var thrown = assertThrows(ConfigException.class, Config::fromEnv);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ class DefaultImportScanServiceTest {
private final Config config = new Config(
"http://localhost",
"apiKey",
"username",
23,
42L
23
);
private final DefaultImportScanService sut = new DefaultImportScanService(config);

Expand Down
Loading