-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathConfig.java
149 lines (130 loc) · 4.35 KB
/
Config.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// SPDX-FileCopyrightText: the secureCodeBox authors
//
// SPDX-License-Identifier: Apache-2.0
package io.securecodebox.persistence.defectdojo.config;
import io.securecodebox.persistence.defectdojo.exception.ConfigException;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
/**
* Configures the DefectDojo client
*/
@Getter
@ToString
@EqualsAndHashCode
public final class Config {
/**
* Default for {@link #maxPageCountForGets}
*/
static final int DEFAULT_MAX_PAGE_COUNT_FOR_GETS = 100;
/**
* Null pattern object.
*/
public static final Config NULL = new Config("", "", DEFAULT_MAX_PAGE_COUNT_FOR_GETS);
/**
* URL of the host which serves the DefectDojo API.
* <p>
* It is only allowed to configure the base URL (e.g. {@literal "https://defectdojo.securecodebox.io/"} without
* any path. The path to the concrete API endpoints are maintained by this client library itself.
* </p>
*/
@NonNull
private final String url;
/**
* API key to authorize against the DefectDojo API.
*/
@NonNull
private final String apiKey;
/**
* How many pages of objects are fetched from the DefectDojo API
* <p>
* This setting is to avoid out of memory scenarios.
* </p>
* <p>
* Defaults to {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS}.
* </p>
*/
private final int maxPageCountForGets;
/**
* Convenience constructor which sets {@link #DEFAULT_MAX_PAGE_COUNT_FOR_GETS}
*
* @param url not {@code null}
* @param apiKey not {@code null}
*/
public Config(final @NonNull String url, final @NonNull String apiKey) {
this(url, apiKey, DEFAULT_MAX_PAGE_COUNT_FOR_GETS);
}
/**
* Dedicated constructor
*
* @param url not {@code null}
* @param apiKey not {@code null}
* @param maxPageCountForGets not less than 1
*/
public Config(final @NonNull String url, final @NonNull String apiKey, final int maxPageCountForGets) {
super();
this.url = url;
this.apiKey = apiKey;
this.maxPageCountForGets = validateIsGreaterZero(maxPageCountForGets, "maxPageCountForGets");
}
private static int validateIsGreaterZero(final int number, final String name) {
if (number < 1) {
throw new IllegalArgumentException(String.format("%s must be greater than 0!", name));
}
return number;
}
/**
* Creates config from environment variables
*
* @return never {@code null}
*/
public static Config fromEnv() {
final var url = findRequiredEnvVar(EnvVars.DEFECTDOJO_URL);
final var apiKey = findRequiredEnvVar(EnvVars.DEFECTDOJO_APIKEY);
final int maxPageCountForGets;
if (hasEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)) {
try {
maxPageCountForGets = Integer.parseInt(findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS));
} catch (final NumberFormatException e) {
throw new ConfigException(String.format("Given value for environment variable '%s' is not a valid number! Given was '%s'.", EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS.literal, findEnvVar(EnvVars.DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS)),
e);
}
} else {
maxPageCountForGets = DEFAULT_MAX_PAGE_COUNT_FOR_GETS;
}
return new Config(url, apiKey, maxPageCountForGets);
}
private static boolean hasEnvVar(final @NonNull EnvVars name) {
return findEnvVar(name) != null;
}
private static String findEnvVar(final @NonNull EnvVars name) {
return System.getenv(name.literal);
}
private static String findRequiredEnvVar(final @NonNull EnvVars name) {
final var envVar = System.getenv(name.literal);
if (envVar == null) {
throw new ConfigException(String.format("Missing environment variable '%s'!", name.literal));
}
return envVar;
}
/**
* Enumerates the available environment variables to configure the client
*/
public enum EnvVars {
DEFECTDOJO_URL("DEFECTDOJO_URL"),
DEFECTDOJO_APIKEY("DEFECTDOJO_APIKEY"),
DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS("DEFECTDOJO_MAX_PAGE_COUNT_FOR_GETS");
/**
* Literal name of configuration environment name
* <p>
* We use an own value to hold the actual value, so that refactoring the enum name does
* not break the published API of env var names.
* </p>
*/
private final String literal;
EnvVars(final String literal) {
this.literal = literal;
}
}
}