-
-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathDefaultObjectGenerator.java
More file actions
428 lines (373 loc) · 18 KB
/
DefaultObjectGenerator.java
File metadata and controls
428 lines (373 loc) · 18 KB
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
/*
* This file is part of Dependency-Track.
*
* 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
* Copyright (c) OWASP Foundation. All Rights Reserved.
*/
package org.dependencytrack.persistence;
import alpine.Config;
import alpine.common.logging.Logger;
import alpine.model.ConfigProperty;
import alpine.model.ManagedUser;
import alpine.model.Permission;
import alpine.server.auth.PasswordService;
import jakarta.servlet.ServletContextEvent;
import jakarta.servlet.ServletContextListener;
import org.dependencytrack.auth.Permissions;
import org.dependencytrack.common.ConfigKey;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.model.License;
import org.dependencytrack.model.RepositoryType;
import org.dependencytrack.parser.spdx.json.SpdxLicenseDetailParser;
import org.dependencytrack.persistence.defaults.DefaultLicenseGroupImporter;
import org.dependencytrack.persistence.jdbi.MetricsDao;
import org.dependencytrack.util.NotificationUtil;
import org.dependencytrack.util.WaitingLockConfiguration;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;
import static net.javacrumbs.shedlock.core.LockAssert.assertLocked;
import static org.dependencytrack.model.ConfigPropertyConstants.INTERNAL_DEFAULT_OBJECTS_VERSION;
import static org.dependencytrack.persistence.jdbi.JdbiFactory.useJdbiHandle;
import static org.dependencytrack.util.LockProvider.executeWithLockWaiting;
/**
* Creates default objects on an empty database.
*
* @author Steve Springett
* @since 3.0.0
*/
public class DefaultObjectGenerator implements ServletContextListener {
private static final Logger LOGGER = Logger.getLogger(DefaultObjectGenerator.class);
private static final Map<String, List<String>> DEFAULT_TEAM_PERMISSIONS = Map.of(
"Administrators", Stream.of(Permissions.values()).map(Permissions::name).toList(),
"Portfolio Managers", List.of(Permissions.Constants.PORTFOLIO),
"Automation", List.of(Permissions.Constants.PORTFOLIO, Permissions.Constants.BOM_CREATE),
"Badge Viewers", List.of(Permissions.Constants.BADGES_READ));
private static final Map<String, List<String>> DEFAULT_ROLE_PERMISSIONS = Map.of(
"Project Admin", List.of(
Permissions.Constants.BADGES_READ,
Permissions.Constants.BOM_READ,
Permissions.Constants.BOM_CREATE,
Permissions.Constants.FINDING_CREATE,
Permissions.Constants.FINDING_READ,
Permissions.Constants.FINDING_UPDATE,
Permissions.Constants.POLICY_VIOLATION_CREATE,
Permissions.Constants.POLICY_VIOLATION_READ,
Permissions.Constants.POLICY_VIOLATION_UPDATE,
Permissions.Constants.PROJECT_READ,
Permissions.Constants.PROJECT_UPDATE,
Permissions.Constants.PROJECT_DELETE),
"Project Auditor", List.of(
Permissions.Constants.BADGES_READ,
Permissions.Constants.BOM_READ,
Permissions.Constants.FINDING_READ,
Permissions.Constants.POLICY_VIOLATION_READ,
Permissions.Constants.PROJECT_READ),
"Project Editor", List.of(
Permissions.Constants.BOM_CREATE,
Permissions.Constants.BOM_READ,
Permissions.Constants.FINDING_READ,
Permissions.Constants.FINDING_UPDATE,
Permissions.Constants.POLICY_VIOLATION_CREATE,
Permissions.Constants.POLICY_VIOLATION_READ,
Permissions.Constants.POLICY_VIOLATION_UPDATE,
Permissions.Constants.PROJECT_READ,
Permissions.Constants.PROJECT_UPDATE),
"Project Viewer", List.of(
Permissions.Constants.BADGES_READ,
Permissions.Constants.BOM_READ,
Permissions.Constants.PROJECT_READ));
private final Map<String, Permission> persistentPermissionByName = new HashMap<>();
/**
* {@inheritDoc}
*/
@Override
public void contextInitialized(final ServletContextEvent event) {
if (!Config.getInstance().getPropertyAsBoolean(ConfigKey.INIT_TASKS_ENABLED)) {
LOGGER.info("Not populating database with default objects because %s is disabled"
.formatted(ConfigKey.INIT_TASKS_ENABLED.getPropertyName()));
return;
}
// Ensure that this task is only executed by a single instance at once.
// Wait for lock acquisition rather than simply skipping execution,
// since application logic may depend on default objects being present.
final var lockConfig = new WaitingLockConfiguration(
/* createdAt */ Instant.now(),
/* name */ getClass().getName(),
/* lockAtMostFor */ Duration.ofMinutes(5),
/* lockAtLeastFor */ Duration.ZERO,
/* pollInterval */ Duration.ofSeconds(1),
/* waitTimeout */ Duration.ofMinutes(5));
try {
executeWithLockWaiting(lockConfig, this::executeLocked);
} catch (Throwable t) {
if (Config.getInstance().getPropertyAsBoolean(ConfigKey.INIT_AND_EXIT)) {
// Make absolutely sure that we exit with non-zero code so
// the container orchestrator knows to restart the container.
LOGGER.error("Failed to populate database with default objects", t);
System.exit(1);
}
throw new RuntimeException("Failed to populate database with default objects", t);
}
if (Config.getInstance().getPropertyAsBoolean(ConfigKey.INIT_AND_EXIT)) {
LOGGER.info("Exiting because %s is enabled".formatted(ConfigKey.INIT_AND_EXIT.getPropertyName()));
System.exit(0);
}
}
private void executeLocked() {
assertLocked();
if (!shouldExecute()) {
LOGGER.info("Default objects already populated for build %s (timestamp: %s); Skipping".formatted(
Config.getInstance().getApplicationBuildUuid(),
Config.getInstance().getApplicationBuildTimestamp()));
return;
}
// TODO: Make population transactional with recordDefaultObjectsVersion().
LOGGER.info("Initializing default object generator");
try (final var qm = new QueryManager()) {
loadDefaultPermissions(qm);
loadDefaultPersonas(qm);
loadDefaultLicenses(qm);
loadDefaultLicenseGroups(qm);
loadDefaultRepositories(qm);
loadDefaultRoles(qm);
loadDefaultConfigProperties(qm);
loadDefaultNotificationPublishers(qm);
recordDefaultObjectsVersion(qm);
}
LOGGER.info("Ensuring the metrics partitions for today and tomorrow exist.");
ensureMetricsPartitions();
}
/**
* {@inheritDoc}
*/
@Override
public void contextDestroyed(final ServletContextEvent event) {
/* Intentionally blank to satisfy interface */
}
private boolean shouldExecute() {
try (final var qm = new QueryManager()) {
final ConfigProperty configProperty = qm.getConfigProperty(
INTERNAL_DEFAULT_OBJECTS_VERSION.getGroupName(),
INTERNAL_DEFAULT_OBJECTS_VERSION.getPropertyName());
return configProperty == null
|| configProperty.getPropertyValue() == null
|| !Config.getInstance().getApplicationBuildUuid().equals(configProperty.getPropertyValue());
}
}
private void recordDefaultObjectsVersion(final QueryManager qm) {
qm.runInTransaction(() -> {
final ConfigProperty configProperty = qm.getConfigProperty(
INTERNAL_DEFAULT_OBJECTS_VERSION.getGroupName(),
INTERNAL_DEFAULT_OBJECTS_VERSION.getPropertyName());
configProperty.setPropertyValue(Config.getInstance().getApplicationBuildUuid());
});
}
public static void loadDefaultLicenses() {
try (final var qm = new QueryManager()) {
loadDefaultLicenses(qm);
}
}
/**
* Loads the default licenses into the database if no license data exists.
*/
private static void loadDefaultLicenses(final QueryManager qm) {
LOGGER.info("Synchronizing SPDX license definitions to datastore");
final SpdxLicenseDetailParser parser = new SpdxLicenseDetailParser();
try {
final List<License> licenses = parser.getLicenseDefinitions();
for (final License license : licenses) {
LOGGER.debug("Synchronizing: " + license.getName());
qm.synchronizeLicense(license, false);
}
} catch (IOException e) {
LOGGER.error("An error occurred during the parsing SPDX license definitions");
LOGGER.error(e.getMessage());
}
}
/**
* Loads the default license groups into the database if no license groups exists.
*/
private void loadDefaultLicenseGroups(final QueryManager qm) {
final DefaultLicenseGroupImporter importer = new DefaultLicenseGroupImporter(qm);
if (!importer.shouldImport()) {
return;
}
LOGGER.info("Adding default license group definitions to datastore");
try {
importer.loadDefaults();
} catch (IOException e) {
LOGGER.error("An error occurred loading default license group definitions");
LOGGER.error(e.getMessage());
}
}
public void loadDefaultPermissions() {
try (final var qm = new QueryManager()) {
loadDefaultPermissions(qm);
}
}
/**
* Loads the default permissions
*/
private void loadDefaultPermissions(final QueryManager qm) {
LOGGER.info("Synchronizing permissions to datastore");
List<String> existing = Objects.requireNonNullElse(qm.getPermissions(), Collections.<Permission>emptyList())
.stream()
.map(Permission::getName)
.toList();
for (final Permissions value : Permissions.values())
if (!existing.contains(value.name())) {
LOGGER.debug("Creating permission: " + value.name());
persistentPermissionByName.put(value.name(), qm.createPermission(value.name(), value.getDescription()));
}
}
@SuppressWarnings("unused")
void loadDefaultPersonas() {
try (final var qm = new QueryManager()) {
loadDefaultPersonas(qm);
}
}
/**
* Loads the default users and teams
*/
private void loadDefaultPersonas(final QueryManager qm) {
if (!qm.getManagedUsers().isEmpty() && qm.getTeams().getTotal() != 0)
return;
LOGGER.info("Adding default users and teams to datastore");
LOGGER.debug("Creating user: admin");
ManagedUser admin = qm.createManagedUser("admin", "Administrator", "admin@localhost",
new String(PasswordService.createHash("admin".toCharArray())), true, true, false);
for (var name : DEFAULT_TEAM_PERMISSIONS.keySet()) {
LOGGER.debug("Creating team: " + name);
var team = qm.createTeam(name);
LOGGER.debug("Assigning default permissions for team: " + name);
team.setPermissions(getPermissionsByName(DEFAULT_TEAM_PERMISSIONS.get(name)));
qm.persist(team);
}
LOGGER.debug("Adding admin user to System Administrators");
qm.addUserToTeam(admin, qm.getTeam("Administrators"));
admin = qm.getObjectById(ManagedUser.class, admin.getId());
admin.setPermissions(qm.getPermissions());
qm.persist(admin);
}
/**
* Perform a lookup of {@link Permission}s for specified name(s).
*
* @param names permission names
* @return list of {@link Permission}s
*/
private List<Permission> getPermissionsByName(List<String> names) {
return names.stream().map(persistentPermissionByName::get).filter(Objects::nonNull).toList();
}
public void loadDefaultRoles() {
try (final var qm = new QueryManager()) {
loadDefaultRoles(qm);
}
}
/**
* Loads the default Roles
*/
private void loadDefaultRoles(final QueryManager qm) {
if (!qm.getRoles().isEmpty())
return;
LOGGER.info("Adding default roles to datastore");
for (var name : DEFAULT_ROLE_PERMISSIONS.keySet()) {
LOGGER.debug("Creating role: " + name);
qm.createRole(name, getPermissionsByName(DEFAULT_ROLE_PERMISSIONS.get(name)));
}
}
public void loadDefaultRepositories() {
try (final var qm = new QueryManager()) {
loadDefaultRepositories(qm);
}
}
/**
* Loads the default repositories
*/
private void loadDefaultRepositories(final QueryManager qm) {
LOGGER.info("Synchronizing default repositories to datastore");
// @formatter:off
qm.createRepository(RepositoryType.CPAN, "cpan-public-registry", "https://fastapi.metacpan.org/v1/", true, false, false, null, null);
qm.createRepository(RepositoryType.GEM, "rubygems.org", "https://rubygems.org/", true, false, false,null, null);
qm.createRepository(RepositoryType.HEX, "hex.pm", "https://hex.pm/", true, false, false, null, null);
qm.createRepository(RepositoryType.MAVEN, "central", "https://repo1.maven.org/maven2/", true, false, false, null, null);
qm.createRepository(RepositoryType.MAVEN, "atlassian-public", "https://packages.atlassian.com/content/repositories/atlassian-public/", true, false, false, null, null);
qm.createRepository(RepositoryType.MAVEN, "jboss-releases", "https://repository.jboss.org/nexus/content/repositories/releases/", true, false, false, null, null);
qm.createRepository(RepositoryType.MAVEN, "clojars", "https://repo.clojars.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.MAVEN, "google-android", "https://maven.google.com/", true, false, false, null, null);
qm.createRepository(RepositoryType.NPM, "npm-public-registry", "https://registry.npmjs.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.PYPI, "pypi.org", "https://pypi.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.NUGET, "nuget-gallery", "https://api.nuget.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.COMPOSER, "packagist", "https://repo.packagist.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.CARGO, "crates.io", "https://crates.io", true, false, false, null, null);
qm.createRepository(RepositoryType.GO_MODULES, "proxy.golang.org", "https://proxy.golang.org", true, false, false, null, null);
qm.createRepository(RepositoryType.GITHUB, "github.com", "https://github.com", true, false, false, null, null);
qm.createRepository(RepositoryType.HACKAGE, "hackage.haskell", "https://hackage.haskell.org/", true, false, false, null, null);
qm.createRepository(RepositoryType.NIXPKGS, "nixos.org", "https://channels.nixos.org/nixpkgs-unstable/packages.json.br", true, false, false, null, null);
// @formatter:on
}
@SuppressWarnings("unused")
void loadDefaultConfigProperties() {
try (final var qm = new QueryManager()) {
loadDefaultConfigProperties(qm);
}
}
/**
* Loads the default ConfigProperty objects
*/
private void loadDefaultConfigProperties(final QueryManager qm) {
LOGGER.info("Synchronizing config properties to datastore");
for (final ConfigPropertyConstants cpc : ConfigPropertyConstants.values()) {
LOGGER.debug("Creating config property: " + cpc.getGroupName() + " / " + cpc.getPropertyName());
if (qm.getConfigProperty(cpc.getGroupName(), cpc.getPropertyName()) == null) {
qm.createConfigProperty(cpc.getGroupName(), cpc.getPropertyName(), cpc.getDefaultPropertyValue(),
cpc.getPropertyType(), cpc.getDescription());
}
}
}
public void loadDefaultNotificationPublishers() {
try (final var qm = new QueryManager()) {
loadDefaultNotificationPublishers(qm);
}
}
/**
* Loads the default notification publishers
*/
private void loadDefaultNotificationPublishers(final QueryManager qm) {
LOGGER.info("Synchronizing notification publishers to datastore");
try {
NotificationUtil.loadDefaultNotificationPublishers(qm);
} catch (IOException e) {
LOGGER.error("An error occurred while synchronizing a default notification publisher", e);
}
}
/**
* Create metrics partitions for today and tomorrow if they don't exist.
*/
void ensureMetricsPartitions() {
useJdbiHandle(handle -> {
var metricsHandle = handle.attach(MetricsDao.class);
metricsHandle.createMetricsPartitionsForDate(LocalDate.now().toString(), LocalDate.now().plusDays(1).toString());
metricsHandle.createMetricsPartitionsForDate(LocalDate.now().plusDays(1).toString(), LocalDate.now().plusDays(2).toString());
});
}
}