Skip to content

Commit

Permalink
Store cluster ID in database
Browse files Browse the repository at this point in the history
This makes the cluster ID (previously system UUID) available to other instances and services.

Instead of `Config.getInstance().getSystemUuid()`, `ClusterInfo.getClusterId()` must be used from now on.

As the cluster ID is only generated once and then never updated, it will only be loaded upon first invocation of `ClusterInfo.getClusterId()`.

Updating of the cluster ID via API or UI is prevented by marking it as read-only.

Relates to DependencyTrack/hyades#925

Signed-off-by: nscuro <[email protected]>
  • Loading branch information
nscuro committed Mar 29, 2024
1 parent b250ac6 commit b3ae98f
Show file tree
Hide file tree
Showing 6 changed files with 224 additions and 75 deletions.
64 changes: 64 additions & 0 deletions src/main/java/org/dependencytrack/common/ClusterInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.common;

import alpine.Config;
import alpine.model.ConfigProperty;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import org.dependencytrack.model.ConfigPropertyConstants;
import org.dependencytrack.persistence.QueryManager;

import javax.jdo.Query;
import java.util.UUID;

import static java.util.Objects.requireNonNull;

public final class ClusterInfo {

private static final Supplier<String> CLUSTER_ID_SUPPLIER = Suppliers.memoize(ClusterInfo::loadClusterId);

public static String getClusterId() {
return CLUSTER_ID_SUPPLIER.get();
}

private static String loadClusterId() {
if (Config.isUnitTestsEnabled()) {
return UUID.randomUUID().toString();
}

try (final var qm = new QueryManager()) {
final Query<ConfigProperty> query = qm.getPersistenceManager().newQuery(ConfigProperty.class);
query.setFilter("groupName == :groupName && propertyName == :propertyName");
query.setParameters(
ConfigPropertyConstants.INTERNAL_CLUSTER_ID.getGroupName(),
ConfigPropertyConstants.INTERNAL_CLUSTER_ID.getPropertyName()
);
query.setResult("propertyValue");

try {
final String clusterId = query.executeResultUnique(String.class);
return requireNonNull(clusterId, "Cluster ID must not be null");
} finally {
query.closeAll();
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public final class ManagedHttpClientFactory {
+ SystemUtil.getOsName() + "; "
+ SystemUtil.getOsVersion()
+ ") ManagedHttpClient/"
+ Config.getInstance().getSystemUuid();
+ ClusterInfo.getClusterId();
}

private ManagedHttpClientFactory() { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* 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.model;

public enum ConfigPropertyAccessMode {

READ_ONLY,

READ_WRITE

}
Loading

0 comments on commit b3ae98f

Please sign in to comment.