55
66package io .securecodebox .persistence .defectdojo .config ;
77
8- import lombok .AllArgsConstructor ;
9- import lombok .Getter ;
10- import lombok .NonNull ;
11- import lombok .ToString ;
8+ import lombok .*;
129
1310import java .util .Optional ;
1411
1714 */
1815@ Getter
1916@ ToString
20- @ AllArgsConstructor
17+ @ EqualsAndHashCode
2118public final class Config {
2219 /**
2320 * Default for {@link #maxPageCountForGets}
@@ -30,10 +27,12 @@ public final class Config {
3027 * any path. The path to the concrete API endpoints are maintained by this client library itself.
3128 * </p>
3229 */
30+ @ NonNull
3331 private final String url ;
3432 /**
3533 * API key to authorize against the DefectDojo API.
3634 */
35+ @ NonNull
3736 private final String apiKey ;
3837 /**
3938 * This name is used to set the creator of entities created in DefectDojo (findings etc.).
@@ -45,6 +44,7 @@ public final class Config {
4544 *
4645 * @deprecated Must not be used anymore because we determine the userid via user_profile API endpoint.
4746 */
47+ @ NonNull
4848 @ Deprecated
4949 private final String username ;
5050
@@ -73,6 +73,32 @@ public final class Config {
7373 @ Deprecated
7474 private final Long userId ;
7575
76+ /**
77+ * Dedicated constructor
78+ *
79+ * @param url not {@code null}
80+ * @param apiKey not {@code null}
81+ * @param username not {@code null}
82+ * @param maxPageCountForGets not less than 1
83+ * @param userId may be {@code null} (see {@link #userId})
84+ */
85+ public Config (final @ NonNull String url , final @ NonNull String apiKey , final @ NonNull String username , final int maxPageCountForGets , final Long userId ) {
86+ super ();
87+ this .url = url ;
88+ this .apiKey = apiKey ;
89+ this .username = username ;
90+ this .maxPageCountForGets = validateIsGreaterZero (maxPageCountForGets , "maxPageCountForGets" );
91+ this .userId = userId ;
92+ }
93+
94+ private static int validateIsGreaterZero (final int number , final String name ) {
95+ if (number < 1 ) {
96+ throw new IllegalArgumentException (String .format ("%s must be greater than 0!" , name ));
97+ }
98+
99+ return number ;
100+ }
101+
76102 /**
77103 * Default constructor which sets {@link #userId} to {@code null}
78104 *
@@ -81,8 +107,7 @@ public final class Config {
81107 * @param username not {@code null}
82108 * @param maxPageCountForGets not less than 1
83109 */
84- public Config (final @ NonNull String url , final @ NonNull String apiKey , final @ NonNull String username , final int maxPageCountForGets ) {
85- // FIXME: Implement check that maxPageCountForGets is not less than 1
110+ public Config (final String url , final String apiKey , final String username , final int maxPageCountForGets ) {
86111 this (url , apiKey , username , maxPageCountForGets , null );
87112 }
88113
@@ -92,17 +117,43 @@ public Config(final @NonNull String url, final @NonNull String apiKey, final @No
92117 * @return never {@code null}
93118 */
94119 public static Config fromEnv () {
95- final var url = System .getenv ("DEFECTDOJO_URL" );
96- final var username = System .getenv ("DEFECTDOJO_USERNAME" );
97- final var apiKey = System .getenv ("DEFECTDOJO_APIKEY" );
98- final var userId = Optional .ofNullable (System .getenv ("DEFECTDOJO_USER_ID" )).map (Long ::parseLong ).orElse (null );
120+ final var url = System .getenv (EnvVars .DEFECTDOJO_URL .literal );
121+ final var username = System .getenv (EnvVars .DEFECTDOJO_USERNAME .literal );
122+ final var apiKey = System .getenv (EnvVars .DEFECTDOJO_APIKEY .literal );
123+ final var userId = Optional .ofNullable (System .getenv (EnvVars .DEFECTDOJO_USER_ID .literal ))
124+ .map (Long ::parseLong ).orElse (null );
99125
100- int maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS ;
126+ final int maxPageCountForGets ;
101127
102- if (System .getenv ("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS" ) != null ) {
103- maxPageCountForGets = Integer .parseInt (System .getenv ("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS" ));
128+ if (System .getenv (EnvVars .DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS .literal ) != null ) {
129+ maxPageCountForGets = Integer .parseInt (System .getenv (EnvVars .DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS .literal ));
130+ } else {
131+ maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS ;
104132 }
105133
106134 return new Config (url , apiKey , username , maxPageCountForGets , userId );
107135 }
136+
137+ /**
138+ * Enumerates the available environment variables to configure the client
139+ */
140+ public enum EnvVars {
141+ DEFECTDOJO_URL ("DEFECTDOJO_URL" ),
142+ DEFECTDOJO_USERNAME ("DEFECTDOJO_USERNAME" ),
143+ DEFECTDOJO_APIKEY ("DEFECTDOJO_APIKEY" ),
144+ DEFECTDOJO_USER_ID ("DEFECTDOJO_USER_ID" ),
145+ DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS ("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS" );
146+ /**
147+ * Literal name of configuration environment name
148+ * <p>
149+ * We use an own value to hold the actual value, so that refactoring the enum name does
150+ * not break the published API of env var names.
151+ * </p>
152+ */
153+ private final String literal ;
154+
155+ EnvVars (final String literal ) {
156+ this .literal = literal ;
157+ }
158+ }
108159}
0 commit comments