-
Notifications
You must be signed in to change notification settings - Fork 2
/
NakshaHubConfig.java
434 lines (382 loc) · 14.3 KB
/
NakshaHubConfig.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
* Copyright (C) 2017-2024 HERE Europe B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* License-Filename: LICENSE
*/
package com.here.naksha.lib.hub;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.here.naksha.lib.core.NakshaVersion;
import com.here.naksha.lib.core.models.geojson.implementation.XyzFeature;
import com.here.naksha.lib.core.util.json.JsonSerializable;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.Map;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Naksha-Hub service configuration.
*/
public final class NakshaHubConfig extends XyzFeature implements JsonSerializable {
private static final Logger logger = LoggerFactory.getLogger(NakshaHubConfig.class);
/**
* The default application name, used for example as identifier when accessing the PostgresQL database and to read the configuration file
* using the <a href="https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html">XGD</a> standard, therefore from
* directory ({@code ~/.config/<APP_NAME>/...}).
*/
public static final @NotNull String APP_NAME = "naksha";
private static final String NAKSHA_ENV = "NAKSHA_ENV";
/**
* The default Http request body limit in MB.
*/
public static final Integer DEF_REQ_BODY_LIMIT = 25;
/**
* The maximum Http request body limit in MB.
*/
public static final Integer MAX_REQ_BODY_LIMIT = Math.max(25, DEF_REQ_BODY_LIMIT);
/**
* Returns a default application name used at many placed.
*
* @return The default application name.
*/
public static @NotNull String defaultAppName() {
return APP_NAME + "/v" + NakshaVersion.latest;
}
/**
* Returns a className of default NakshaHub instance
*
* @return The default NakshaHub className
*/
public static @NotNull String defaultHubClassName() {
return NakshaHub.class.getName();
}
/**
* Returns a default relative path to Private Key useful for JWT signing
*
* @return The default private key path
*/
public static @NotNull String defaultJwtPvtKeyPath() {
return "auth/jwt.key";
}
/**
* Returns default relative paths to Public Keys useful for JWT signature verification
*
* @return The default public key paths
*/
public static @NotNull String defaultJwtPubKeyPaths() {
return "auth/jwt.pub";
}
@JsonCreator
NakshaHubConfig(
@JsonProperty("id") @NotNull String id,
@JsonProperty("hubClassName") @Nullable String hubClassName,
@JsonProperty("userAgent") @Nullable String userAgent,
@JsonProperty("appId") @Nullable String appId,
@JsonProperty("author") @Nullable String author,
@JsonProperty("httpPort") @Nullable Integer httpPort,
@JsonProperty("hostname") @Nullable String hostname,
@JsonProperty("endpoint") @Nullable String endpoint,
@JsonProperty("env") @Nullable String env,
@JsonProperty("webRoot") @Nullable String webRoot,
@JsonProperty(NAKSHA_AUTH) @Nullable AuthorizationMode authMode,
@JsonProperty(JWT_PVT_KEY_PATH) @Nullable String jwtPvtKeyPath,
@JsonProperty(JWT_PUB_KEY_PATHS) @Nullable String jwtPubKeyPaths,
@JsonProperty("debug") @Nullable Boolean debug,
@JsonProperty("maintenanceIntervalInMins") @Nullable Integer maintenanceIntervalInMins,
@JsonProperty("maintenanceInitialDelayInMins") @Nullable Integer maintenanceInitialDelayInMins,
@JsonProperty("maintenancePoolCoreSize") @Nullable Integer maintenancePoolCoreSize,
@JsonProperty("maintenancePoolMaxSize") @Nullable Integer maintenancePoolMaxSize,
@JsonProperty("storageParams") @Nullable Map<String, Object> storageParams,
@JsonProperty("extensionConfigParams") @Nullable ExtensionConfigParams extensionConfigParams,
@JsonProperty("requestBodyLimit") @Nullable Integer requestBodyLimit,
@JsonProperty("maxParallelRequestsPerCPU") @Nullable Integer maxParallelRequestsPerCPU,
@JsonProperty("maxPctParallelRequestsPerActor") @Nullable Integer maxPctParallelRequestsPerActor) {
super(id);
if (httpPort != null && (httpPort < 0 || httpPort > 65535)) {
logger.atWarn()
.setMessage("Invalid port in Naksha configuration: {}, falling back to default \"8080\"")
.addArgument(httpPort)
.log();
httpPort = 8080;
} else if (httpPort == null || httpPort == 0) {
httpPort = 8080;
}
if (hostname == null || hostname.length() == 0) {
try {
hostname = InetAddress.getLocalHost().getHostAddress();
} catch (UnknownHostException e) {
logger.atWarn()
.setMessage("Unable to resolve the hostname using Java's API, using default \"localhost\".")
.setCause(e)
.log();
hostname = "localhost";
}
}
URL __endpoint = null;
if (endpoint != null && endpoint.length() > 0) {
try {
__endpoint = new URL(endpoint);
} catch (MalformedURLException e) {
logger.atError()
.setMessage("Invalid configuration of endpoint: {}")
.addArgument(endpoint)
.setCause(e)
.log();
}
}
if (__endpoint == null) {
try {
//noinspection HttpUrlsUsage
__endpoint = new URL("http://" + hostname + ":" + httpPort);
} catch (MalformedURLException e) {
logger.atError()
.setMessage("Invalid hostname: {}")
.addArgument(hostname)
.setCause(e)
.log();
hostname = "localhost";
try {
__endpoint = new URL("http://localhost:" + httpPort);
} catch (MalformedURLException ignore) {
}
}
assert __endpoint != null;
}
env = getEnv(env);
this.hubClassName = (hubClassName != null && !hubClassName.isEmpty()) ? hubClassName : defaultHubClassName();
this.appId = appId != null && appId.length() > 0 ? appId : "naksha";
this.author = author;
this.httpPort = httpPort;
this.hostname = hostname;
this.endpoint = __endpoint;
this.env = env;
this.webRoot = webRoot;
this.authMode = (authMode == null) ? AuthorizationMode.JWT : authMode;
this.jwtPvtKeyPath =
(jwtPvtKeyPath != null && !jwtPvtKeyPath.isEmpty()) ? jwtPvtKeyPath : defaultJwtPvtKeyPath();
this.jwtPubKeyPaths =
(jwtPubKeyPaths != null && !jwtPubKeyPaths.isEmpty()) ? jwtPubKeyPaths : defaultJwtPubKeyPaths();
this.userAgent = userAgent != null && !userAgent.isEmpty() ? userAgent : defaultAppName();
this.debug = Boolean.TRUE.equals(debug);
this.maintenanceIntervalInMins =
maintenanceIntervalInMins != null ? maintenanceIntervalInMins : defaultMaintenanceIntervalInMins();
this.maintenanceInitialDelayInMins = maintenanceInitialDelayInMins != null
? maintenanceInitialDelayInMins
: defaultMaintenanceInitialDelayInMins();
this.maintenancePoolCoreSize =
maintenancePoolCoreSize != null ? maintenancePoolCoreSize : defaultMaintenancePoolCoreSize();
this.maintenancePoolMaxSize =
maintenancePoolMaxSize != null ? maintenancePoolMaxSize : defaultMaintenancePoolMaxSize();
this.storageParams = storageParams;
this.extensionConfigParams = extensionConfigParams;
if (requestBodyLimit == null) {
this.requestBodyLimit = DEF_REQ_BODY_LIMIT;
} else if (requestBodyLimit > MAX_REQ_BODY_LIMIT) {
logger.warn(
"Configured request body limit {} MB not supported. Falling back to default limit of {} MB",
requestBodyLimit,
DEF_REQ_BODY_LIMIT);
this.requestBodyLimit = DEF_REQ_BODY_LIMIT;
} else {
this.requestBodyLimit = requestBodyLimit;
}
this.maxParallelRequestsPerCPU =
maxParallelRequestsPerCPU != null ? maxParallelRequestsPerCPU : defaultMaxParallelRequestsPerCPU();
this.maxPctParallelRequestsPerActor = maxPctParallelRequestsPerActor != null
? maxPctParallelRequestsPerActor
: defaultMaxPctParallelRequestsPerActor();
}
private String getEnv(String env) {
// This is only to be backward compatible to support EC2 based deployment
String envVal = System.getenv(NAKSHA_ENV);
if (envVal != null && !envVal.isEmpty() && !"null".equalsIgnoreCase(envVal)) return envVal;
if (env != null && !env.isEmpty() && !"null".equalsIgnoreCase(env)) return env;
return "local";
}
public static final String HTTP_PORT = "httpPort";
/**
* The port at which to listen for HTTP requests.
*/
@JsonProperty(HTTP_PORT)
public final int httpPort;
public static final String HOSTNAME = "hostname";
/**
* The hostname to use to refer to this instance, if {@code null}, then auto-detected.
*/
@JsonProperty(HOSTNAME)
public final @NotNull String hostname;
public static final String APP_ID = "appId";
/**
* The application-id to be used when modifying the admin-database.
*/
@JsonProperty(APP_ID)
public final @NotNull String appId;
public static final String AUTHOR = "author";
/**
* The author to be used when modifying the admin-database.
*/
@JsonProperty(AUTHOR)
public final @Nullable String author;
public static final String ENDPOINT = "endpoint";
/**
* The public endpoint, for example "https://naksha.foo.com/". If {@code null}, then the hostname and HTTP port used.
*/
@JsonProperty(ENDPOINT)
public @NotNull URL endpoint;
public static final String ENV = "env";
/**
* The environment, for example "local", "dev", "e2e" or "prd".
*/
@JsonProperty(ENV)
public final @NotNull String env;
public static final String WEB_ROOT = "webRoot";
/**
* If set, then serving static files from this directory.
*/
@JsonProperty(WEB_ROOT)
public final @Nullable String webRoot;
public static final String JWT_PVT_KEY_PATH = "jwtPvtKeyPath";
/**
* The relative path to Private key file to support JWT signing (e.g. {@code "auth/jwt.key"}).
*/
@JsonProperty(JWT_PVT_KEY_PATH)
public final @NotNull String jwtPvtKeyPath;
public static final String JWT_PUB_KEY_PATHS = "jwtPubKeyPaths";
/**
* The comma separated relative paths to Public key files to support JWT signature verification (e.g. {@code "auth/jwt.pub,auth/jwt_2.pub"}).
*/
@JsonProperty(JWT_PUB_KEY_PATHS)
public final @NotNull String jwtPubKeyPaths;
public static final String USER_AGENT = "userAgent";
/**
* The user-agent to be used for external communication.
*/
@JsonProperty(USER_AGENT)
public final @NotNull String userAgent;
public static final String DEBUG = "debug";
/**
* If debugging mode is enabled.
*/
@JsonProperty(DEBUG)
@JsonInclude(Include.NON_DEFAULT)
public boolean debug;
public static final String HUB_CLASS_NAME = "hubClassName";
/**
* The fully qualified class name to be used to initiate NakshaHub instance
*/
@JsonProperty(HUB_CLASS_NAME)
public final @NotNull String hubClassName;
/**
* The initial delay (in minutes) after the service start up, when the Storage Maintenance job should perform first execution
*/
public final int maintenanceInitialDelayInMins;
/**
* Returns a default initial delay in mins, for starting Storage Maintenance job.
*
* @return The default interval
*/
public static int defaultMaintenanceInitialDelayInMins() {
return 1 * 60; // 1 hour
}
/**
* The interval in minutes, with which the Storage Maintenance job is to be scheduled
*/
public final int maintenanceIntervalInMins;
/**
* Returns a default interval in mins, for scheduling Storage Maintenance job.
*
* @return The default interval
*/
public static int defaultMaintenanceIntervalInMins() {
return 12 * 60; // 12 hours
}
/**
* The initial size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolCoreSize;
/**
* Returns a default initial size of thread pool for running Storage Maintenance jobs in parallel
*
* @return the default core size of maintenance thread pool
*/
public static int defaultMaintenancePoolCoreSize() {
return 5;
}
/**
* The maximum size of thread pool for running Storage Maintenance jobs in parallel
*/
public final int maintenancePoolMaxSize;
/**
* Returns a default maximum size of thread pool for running Storage Maintenance jobs in parallel
*
* @return the default max size of maintenance thread pool
*/
public static int defaultMaintenancePoolMaxSize() {
return 5;
}
/**
* Returns a default threshold per processor for concurrency
*
* @return the default threshold per processor
*/
public static int defaultMaxParallelRequestsPerCPU() {
return 30;
}
/**
* Returns a default percentage threshold per principal for concurrency
*
* @return the default percentage threshold per principal
*/
public static int defaultMaxPctParallelRequestsPerActor() {
return 25;
}
/**
* Optional storage-specific parameters
*/
public final Map<String, Object> storageParams;
/**
* Optional extension-manager parameters
*/
public final ExtensionConfigParams extensionConfigParams;
/**
* Optional Http request body limit in MB. Default is {@link #DEF_REQ_BODY_LIMIT}.
*/
public final Integer requestBodyLimit;
/**
* Optional Total Concurrency Limit
*/
public final Integer maxParallelRequestsPerCPU;
/**
* Optional Total Author Concurrency Threshold
*/
public final Integer maxPctParallelRequestsPerActor;
public static final String NAKSHA_AUTH = "authMode";
/**
* The authorization mode.
*/
@JsonProperty(NAKSHA_AUTH)
public final @NotNull AuthorizationMode authMode;
public enum AuthorizationMode {
DUMMY,
JWT
}
}