Skip to content

Commit 40eb868

Browse files
authored
[backend] Adding possibility to provide instance ID (#3644)
1 parent acd35df commit 40eb868

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,73 @@
11
package io.openbas;
22

3+
import static io.openbas.database.model.SettingKeys.PLATFORM_INSTANCE;
4+
import static io.openbas.database.model.SettingKeys.PLATFORM_INSTANCE_CREATION;
5+
6+
import io.openbas.config.OpenBASConfig;
7+
import io.openbas.database.model.Setting;
8+
import io.openbas.database.repository.SettingRepository;
39
import io.openbas.tools.FlywayMigrationValidator;
10+
import jakarta.annotation.PostConstruct;
11+
import java.sql.Timestamp;
12+
import java.time.Instant;
13+
import java.util.Optional;
14+
import java.util.UUID;
15+
import lombok.RequiredArgsConstructor;
16+
import lombok.extern.slf4j.Slf4j;
17+
import org.apache.logging.log4j.util.Strings;
418
import org.springframework.boot.SpringApplication;
519
import org.springframework.boot.autoconfigure.SpringBootApplication;
620

721
@SpringBootApplication
22+
@Slf4j
23+
@RequiredArgsConstructor
824
public class App {
925

26+
private final SettingRepository settingRepository;
27+
private final OpenBASConfig openBASConfig;
28+
1029
public static void main(String[] args) {
1130
FlywayMigrationValidator.validateFlywayMigrationNames();
1231
SpringApplication.run(App.class, args);
1332
}
33+
34+
@PostConstruct
35+
public void init() {
36+
log.info("Startup init");
37+
// Get the platform instance id
38+
Optional<Setting> instanceId = this.settingRepository.findByKey(PLATFORM_INSTANCE.key());
39+
Setting instanceCreationDate =
40+
this.settingRepository
41+
.findByKey(PLATFORM_INSTANCE_CREATION.key())
42+
.orElse(new Setting(PLATFORM_INSTANCE_CREATION.key(), ""));
43+
44+
String platformId;
45+
46+
// If we don't have a platform instance id or if it's been specified as another value than the
47+
// one in the database
48+
if (instanceId.isEmpty()
49+
|| (!Strings.isBlank(openBASConfig.getInstanceId())
50+
&& !instanceId.get().getValue().equals(openBASConfig.getInstanceId()))) {
51+
log.info("Updating platform instance id");
52+
// We update the platform instance id using a random UUID if the value does not exist in the
53+
// database
54+
platformId = UUID.randomUUID().toString();
55+
Setting instanceIdSetting =
56+
instanceId.orElse(new Setting(PLATFORM_INSTANCE.key(), platformId));
57+
58+
// If it's been specified as a specific id, we validate that it's a proper UUID and use it
59+
if (!Strings.isBlank(openBASConfig.getInstanceId())) {
60+
platformId = openBASConfig.getInstanceId();
61+
instanceIdSetting.setValue(UUID.fromString(openBASConfig.getInstanceId()).toString());
62+
}
63+
64+
// Then we save the id in database and update/set the creation date
65+
settingRepository.save(instanceIdSetting);
66+
instanceCreationDate.setValue(Timestamp.from(Instant.now()).toString());
67+
settingRepository.save(instanceCreationDate);
68+
} else {
69+
platformId = instanceId.get().getValue();
70+
}
71+
log.info("Startup of the platform - Platform Instance ID: {}", platformId);
72+
}
1473
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package io.openbas;
2+
3+
import static io.openbas.database.model.SettingKeys.PLATFORM_INSTANCE;
4+
import static io.openbas.database.model.SettingKeys.PLATFORM_INSTANCE_CREATION;
5+
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
6+
import static org.mockito.ArgumentMatchers.argThat;
7+
import static org.mockito.Mockito.*;
8+
9+
import io.openbas.config.OpenBASConfig;
10+
import io.openbas.database.model.Setting;
11+
import io.openbas.database.repository.SettingRepository;
12+
import java.util.Optional;
13+
import java.util.UUID;
14+
import org.junit.jupiter.api.*;
15+
import org.mockito.Mock;
16+
import org.springframework.beans.factory.annotation.Autowired;
17+
18+
class AppTest extends IntegrationTest {
19+
20+
@Mock private SettingRepository settingRepository;
21+
@Autowired private OpenBASConfig openBASConfig;
22+
23+
@DisplayName("Should throw an exception when having an incorrect instance id")
24+
@Test
25+
void shouldThrowExceptionWithInvalidInstanceId() {
26+
openBASConfig.setInstanceId("pouet");
27+
when(settingRepository.findByKey(PLATFORM_INSTANCE.key())).thenReturn(Optional.empty());
28+
29+
assertThatThrownBy(() -> new App(settingRepository, openBASConfig).init())
30+
.isInstanceOf(IllegalArgumentException.class)
31+
.hasMessage("Invalid UUID string: pouet");
32+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
33+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
34+
}
35+
36+
@DisplayName("Should update the instance id with the one in the properties")
37+
@Test
38+
void shouldUpdateInstanceIdWithProperty() {
39+
String uuid = UUID.randomUUID().toString();
40+
openBASConfig.setInstanceId(uuid);
41+
new App(settingRepository, openBASConfig).init();
42+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
43+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
44+
verify(settingRepository)
45+
.save(
46+
argThat(
47+
setting ->
48+
PLATFORM_INSTANCE.key().equals(setting.getKey())
49+
&& uuid.equals(setting.getValue())));
50+
verify(settingRepository)
51+
.save(argThat(setting -> PLATFORM_INSTANCE_CREATION.key().equals(setting.getKey())));
52+
}
53+
54+
@DisplayName("Should update the instance id when there is none in db")
55+
@Test
56+
void shouldUpdateInstanceIdWhenNoneInDb() {
57+
// Mock le repository pour retourner empty
58+
when(settingRepository.findByKey(PLATFORM_INSTANCE.key())).thenReturn(Optional.empty());
59+
new App(settingRepository, openBASConfig).init();
60+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
61+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
62+
verify(settingRepository)
63+
.save(argThat(setting -> PLATFORM_INSTANCE.key().equals(setting.getKey())));
64+
verify(settingRepository)
65+
.save(argThat(setting -> PLATFORM_INSTANCE_CREATION.key().equals(setting.getKey())));
66+
}
67+
68+
@DisplayName(
69+
"Should update the instance id when there is none in db and the specified one is blank")
70+
@Test
71+
void shouldUpdateInstanceIdWhenNoneInDbAndSpecifiedOneIsBlank() {
72+
openBASConfig.setInstanceId("");
73+
when(settingRepository.findByKey(PLATFORM_INSTANCE.key())).thenReturn(Optional.empty());
74+
new App(settingRepository, openBASConfig).init();
75+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
76+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
77+
verify(settingRepository)
78+
.save(argThat(setting -> PLATFORM_INSTANCE.key().equals(setting.getKey())));
79+
verify(settingRepository)
80+
.save(argThat(setting -> PLATFORM_INSTANCE_CREATION.key().equals(setting.getKey())));
81+
}
82+
83+
@DisplayName("Shouldn't update anything if instance id in db and the specified one is blank")
84+
@Test
85+
void shouldNotUpdateInstanceIdWhenInDbAndSpecifiedOneIsBlank() {
86+
openBASConfig.setInstanceId("");
87+
when(settingRepository.findByKey(PLATFORM_INSTANCE.key()))
88+
.thenReturn(
89+
Optional.of(new Setting(PLATFORM_INSTANCE.key(), UUID.randomUUID().toString())));
90+
new App(settingRepository, openBASConfig).init();
91+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
92+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
93+
verifyNoMoreInteractions(settingRepository);
94+
}
95+
96+
@DisplayName("Shouldn't update anything if instance id in db and there is no specified one")
97+
@Test
98+
void shouldNotUpdateInstanceIdWhenInDbAndNoSpecifiedOne() {
99+
openBASConfig.setInstanceId(null);
100+
when(settingRepository.findByKey(PLATFORM_INSTANCE.key()))
101+
.thenReturn(
102+
Optional.of(new Setting(PLATFORM_INSTANCE.key(), UUID.randomUUID().toString())));
103+
new App(settingRepository, openBASConfig).init();
104+
verify(settingRepository).findByKey(PLATFORM_INSTANCE.key());
105+
verify(settingRepository).findByKey(PLATFORM_INSTANCE_CREATION.key());
106+
verifyNoMoreInteractions(settingRepository);
107+
}
108+
}

openbas-framework/src/main/java/io/openbas/config/OpenBASConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ public class OpenBASConfig {
6161
@JsonProperty("enabled_dev_features")
6262
private String enabledDevFeatures = "";
6363

64+
@JsonProperty("instance_id")
65+
private String instanceId;
66+
6467
@JsonIgnore private String cookieName = "openbas_token";
6568

6669
@JsonIgnore private String cookieDuration = "P1D";

0 commit comments

Comments
 (0)