Skip to content

Commit e8c8aed

Browse files
authored
Move license settings (#94566)
Continued refactoring of the license service. This commit follows #94261 which moved the LicenseService -> ClusterStateLicenseService and re-introduced LicenseService as interface. That change did not move the settings to keep the diff small. This change moves those settings to a new class LicenseSettings. LicenseService will be re-introduced a subsequent commit.
1 parent ccc2d94 commit e8c8aed

File tree

36 files changed

+107
-104
lines changed

36 files changed

+107
-104
lines changed

x-pack/plugin/autoscaling/src/internalClusterTest/java/org/elasticsearch/xpack/autoscaling/AbstractFrozenAutoscalingIntegTestCase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import java.util.stream.IntStream;
4040

4141
import static org.elasticsearch.index.IndexSettings.INDEX_SOFT_DELETES_SETTING;
42-
import static org.elasticsearch.license.LicenseService.SELF_GENERATED_LICENSE_TYPE;
42+
import static org.elasticsearch.license.LicenseSettings.SELF_GENERATED_LICENSE_TYPE;
4343
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
4444
import static org.hamcrest.Matchers.equalTo;
4545

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import org.elasticsearch.index.shard.ShardId;
5858
import org.elasticsearch.indices.IndicesService;
5959
import org.elasticsearch.indices.store.IndicesStore;
60-
import org.elasticsearch.license.LicenseService;
60+
import org.elasticsearch.license.LicenseSettings;
6161
import org.elasticsearch.license.LicensesMetadata;
6262
import org.elasticsearch.persistent.PersistentTasksCustomMetadata;
6363
import org.elasticsearch.plugins.Plugin;
@@ -304,7 +304,7 @@ private NodeConfigurationSource createNodeConfigurationSource(final String leade
304304
builder.put(XPackSettings.SECURITY_ENABLED.getKey(), false);
305305
builder.put(XPackSettings.WATCHER_ENABLED.getKey(), false);
306306
builder.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false);
307-
builder.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial");
307+
builder.put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial");
308308
// Let cluster state api return quickly in order to speed up auto follow tests:
309309
builder.put(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT.getKey(), TimeValue.timeValueMillis(100));
310310
if (leaderCluster) {

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrSingleNodeTestCase.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import org.elasticsearch.cluster.service.ClusterService;
1515
import org.elasticsearch.common.settings.Settings;
1616
import org.elasticsearch.core.TimeValue;
17-
import org.elasticsearch.license.LicenseService;
17+
import org.elasticsearch.license.LicenseSettings;
1818
import org.elasticsearch.license.LicensesMetadata;
1919
import org.elasticsearch.plugins.Plugin;
2020
import org.elasticsearch.test.ESSingleNodeTestCase;
@@ -48,7 +48,7 @@ protected Settings nodeSettings() {
4848
builder.put(XPackSettings.SECURITY_ENABLED.getKey(), false);
4949
builder.put(XPackSettings.WATCHER_ENABLED.getKey(), false);
5050
builder.put(XPackSettings.MACHINE_LEARNING_ENABLED.getKey(), false);
51-
builder.put(LicenseService.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial");
51+
builder.put(LicenseSettings.SELF_GENERATED_LICENSE_TYPE.getKey(), "trial");
5252
// Let cluster state api return quickly in order to speed up auto follow tests:
5353
builder.put(CcrSettings.CCR_WAIT_FOR_METADATA_TIMEOUT.getKey(), TimeValue.timeValueMillis(100));
5454
return builder.build();

x-pack/plugin/core/src/javaRestTest/java/org/elasticsearch/xpack/core/LicenseInstallationIT.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.elasticsearch.common.util.concurrent.ThreadContext;
1717
import org.elasticsearch.core.TimeValue;
1818
import org.elasticsearch.license.License;
19-
import org.elasticsearch.license.LicenseService;
19+
import org.elasticsearch.license.LicenseSettings;
2020
import org.elasticsearch.license.TestUtils;
2121
import org.elasticsearch.test.rest.ESRestTestCase;
2222
import org.elasticsearch.xcontent.ToXContent;
@@ -131,8 +131,8 @@ private Request createPutLicenseRequest(License signedLicense) throws IOExceptio
131131
private License generateRandomLicense(String licenseId, long expiryDate) throws Exception {
132132
int version = randomIntBetween(VERSION_NO_FEATURE_TYPE, VERSION_CURRENT);
133133
License.LicenseType type = version < VERSION_ENTERPRISE
134-
? randomValueOtherThan(License.LicenseType.ENTERPRISE, () -> randomFrom(LicenseService.ALLOWABLE_UPLOAD_TYPES))
135-
: randomFrom(LicenseService.ALLOWABLE_UPLOAD_TYPES);
134+
? randomValueOtherThan(License.LicenseType.ENTERPRISE, () -> randomFrom(LicenseSettings.ALLOWABLE_UPLOAD_TYPES))
135+
: randomFrom(LicenseSettings.ALLOWABLE_UPLOAD_TYPES);
136136
final License.Builder builder = License.builder()
137137
.uid(licenseId)
138138
.version(version)

x-pack/plugin/core/src/main/java/org/elasticsearch/license/ClusterStateLicenseService.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public class ClusterStateLicenseService extends AbstractLifecycleComponent imple
7979

8080
/**
8181
* Which license types are permitted to be uploaded to the cluster
82-
* @see LicenseService#ALLOWED_LICENSE_TYPES_SETTING
82+
* @see LicenseSettings#ALLOWED_LICENSE_TYPES_SETTING
8383
*/
8484
private final List<License.LicenseType> allowedLicenseTypes;
8585

@@ -115,7 +115,7 @@ public ClusterStateLicenseService(
115115
this.clock = clock;
116116
this.scheduler = new SchedulerEngine(settings, clock);
117117
this.licenseState = licenseState;
118-
this.allowedLicenseTypes = LicenseService.ALLOWED_LICENSE_TYPES_SETTING.get(settings);
118+
this.allowedLicenseTypes = LicenseSettings.ALLOWED_LICENSE_TYPES_SETTING.get(settings);
119119
this.scheduler.register(this);
120120
populateExpirationCallbacks();
121121

@@ -317,12 +317,12 @@ private LicensesMetadata getLicensesMetadata() {
317317

318318
void startTrialLicense(PostStartTrialRequest request, final ActionListener<PostStartTrialResponse> listener) {
319319
License.LicenseType requestedType = License.LicenseType.parse(request.getType());
320-
if (LicenseService.VALID_TRIAL_TYPES.contains(requestedType) == false) {
320+
if (LicenseSettings.VALID_TRIAL_TYPES.contains(requestedType) == false) {
321321
throw new IllegalArgumentException(
322322
"Cannot start trial of type ["
323323
+ requestedType.getTypeName()
324324
+ "]. Valid trial types are ["
325-
+ LicenseService.VALID_TRIAL_TYPES.stream()
325+
+ LicenseSettings.VALID_TRIAL_TYPES.stream()
326326
.map(License.LicenseType::getTypeName)
327327
.sorted()
328328
.collect(Collectors.joining(","))
@@ -455,7 +455,7 @@ public void clusterChanged(ClusterChangedEvent event) {
455455

456456
protected String getExpiryWarning(long licenseExpiryDate, long currentTime) {
457457
final long diff = licenseExpiryDate - currentTime;
458-
if (LicenseService.LICENSE_EXPIRATION_WARNING_PERIOD.getMillis() > diff) {
458+
if (LicenseSettings.LICENSE_EXPIRATION_WARNING_PERIOD.getMillis() > diff) {
459459
final long days = TimeUnit.MILLISECONDS.toDays(diff);
460460
final String expiryMessage = (days == 0 && diff > 0)
461461
? "expires today"
@@ -483,7 +483,7 @@ protected void updateLicenseState(final License license) {
483483
}
484484
if (license != null) {
485485
final boolean active;
486-
if (LicenseUtils.getExpiryDate(license) == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) {
486+
if (LicenseUtils.getExpiryDate(license) == LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) {
487487
active = true;
488488
} else {
489489
active = time >= license.issueDate() && time < LicenseUtils.getExpiryDate(license);
@@ -544,7 +544,7 @@ SchedulerEngine.Schedule nextLicenseCheck(License license) {
544544
} else if (time < LicenseUtils.getExpiryDate(license)) {
545545
// Re-check the license every day during the warning period up to the license expiration.
546546
// This will cause the warning message to be updated that is emitted on soon-expiring license use.
547-
long nextTime = LicenseUtils.getExpiryDate(license) - LicenseService.LICENSE_EXPIRATION_WARNING_PERIOD.getMillis();
547+
long nextTime = LicenseUtils.getExpiryDate(license) - LicenseSettings.LICENSE_EXPIRATION_WARNING_PERIOD.getMillis();
548548
while (nextTime <= time) {
549549
nextTime += TimeValue.timeValueDays(1).getMillis();
550550
}

x-pack/plugin/core/src/main/java/org/elasticsearch/license/License.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ private License(
264264
// We will validate that only a basic license can have the BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS
265265
// in the validate() method.
266266
if (expiryDate == -1) {
267-
this.expiryDate = LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
267+
this.expiryDate = LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
268268
} else {
269269
this.expiryDate = expiryDate;
270270
}
@@ -381,7 +381,7 @@ private void validate() {
381381
throw new IllegalStateException("feature can not be null");
382382
} else if (expiryDate == -1) {
383383
throw new IllegalStateException("expiryDate has to be set");
384-
} else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && LicenseType.isBasic(type) == false) {
384+
} else if (expiryDate == LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && LicenseType.isBasic(type) == false) {
385385
throw new IllegalStateException("only basic licenses are allowed to have no expiration");
386386
}
387387

@@ -527,7 +527,7 @@ public XContentBuilder toInnerXContent(XContentBuilder builder, Params params) t
527527
builder.field(Fields.FEATURE, feature);
528528
}
529529

530-
if (expiryDate != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) {
530+
if (expiryDate != LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS) {
531531
builder.timeField(Fields.EXPIRY_DATE_IN_MILLIS, Fields.EXPIRY_DATE, expiryDate);
532532
}
533533

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseService.java renamed to x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseSettings.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
import java.util.stream.Collectors;
1717
import java.util.stream.Stream;
1818

19-
public interface LicenseService {
20-
Setting<License.LicenseType> SELF_GENERATED_LICENSE_TYPE = new Setting<>(
19+
/**
20+
* Settings related to the license.
21+
*/
22+
public abstract class LicenseSettings {
23+
public static final Setting<License.LicenseType> SELF_GENERATED_LICENSE_TYPE = new Setting<>(
2124
"xpack.license.self_generated.type",
2225
License.LicenseType.BASIC.getTypeName(),
2326
(s) -> {
@@ -26,17 +29,17 @@ public interface LicenseService {
2629
},
2730
Setting.Property.NodeScope
2831
);
29-
List<License.LicenseType> ALLOWABLE_UPLOAD_TYPES = getAllowableUploadTypes();
30-
Setting<List<License.LicenseType>> ALLOWED_LICENSE_TYPES_SETTING = Setting.listSetting(
32+
public static final List<License.LicenseType> ALLOWABLE_UPLOAD_TYPES = getAllowableUploadTypes();
33+
public static final Setting<List<License.LicenseType>> ALLOWED_LICENSE_TYPES_SETTING = Setting.listSetting(
3134
"xpack.license.upload.types",
3235
ALLOWABLE_UPLOAD_TYPES.stream().map(License.LicenseType::getTypeName).toList(),
3336
License.LicenseType::parse,
34-
LicenseService::validateUploadTypesSetting,
37+
LicenseSettings::validateUploadTypesSetting,
3538
Setting.Property.NodeScope
3639
);
3740
// pkg private for tests
38-
TimeValue NON_BASIC_SELF_GENERATED_LICENSE_DURATION = TimeValue.timeValueHours(30 * 24);
39-
Set<License.LicenseType> VALID_TRIAL_TYPES = Set.of(
41+
static final TimeValue NON_BASIC_SELF_GENERATED_LICENSE_DURATION = TimeValue.timeValueHours(30 * 24);
42+
static final Set<License.LicenseType> VALID_TRIAL_TYPES = Set.of(
4043
License.LicenseType.GOLD,
4144
License.LicenseType.PLATINUM,
4245
License.LicenseType.ENTERPRISE,
@@ -45,19 +48,19 @@ public interface LicenseService {
4548
/**
4649
* Period before the license expires when warning starts being added to the response header
4750
*/
48-
TimeValue LICENSE_EXPIRATION_WARNING_PERIOD = TimeValue.timeValueDays(7);
49-
long BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS = XPackInfoResponse.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
51+
static final TimeValue LICENSE_EXPIRATION_WARNING_PERIOD = TimeValue.timeValueDays(7);
52+
static final long BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS = XPackInfoResponse.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
5053
/**
5154
* Max number of nodes licensed by generated trial license
5255
*/
53-
int SELF_GENERATED_LICENSE_MAX_NODES = 1000;
54-
int SELF_GENERATED_LICENSE_MAX_RESOURCE_UNITS = SELF_GENERATED_LICENSE_MAX_NODES;
56+
static final int SELF_GENERATED_LICENSE_MAX_NODES = 1000;
57+
static final int SELF_GENERATED_LICENSE_MAX_RESOURCE_UNITS = SELF_GENERATED_LICENSE_MAX_NODES;
5558

56-
static List<License.LicenseType> getAllowableUploadTypes() {
59+
private static List<License.LicenseType> getAllowableUploadTypes() {
5760
return Stream.of(License.LicenseType.values()).filter(t -> t != License.LicenseType.BASIC).toList();
5861
}
5962

60-
static void validateUploadTypesSetting(List<License.LicenseType> value) {
63+
private static void validateUploadTypesSetting(List<License.LicenseType> value) {
6164
if (ALLOWABLE_UPLOAD_TYPES.containsAll(value) == false) {
6265
throw new IllegalArgumentException(
6366
"Invalid value ["

x-pack/plugin/core/src/main/java/org/elasticsearch/license/LicenseUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public static boolean isLicenseExpiredException(ElasticsearchSecurityException e
4949

5050
public static boolean licenseNeedsExtended(License license) {
5151
return LicenseType.isBasic(license.type())
52-
&& getExpiryDate(license) != LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
52+
&& getExpiryDate(license) != LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS;
5353
}
5454

5555
/**

x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartBasicClusterTask.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,18 @@ public void onFailure(@Nullable Exception e) {
100100
private boolean shouldGenerateNewBasicLicense(License currentLicense) {
101101
return currentLicense == null
102102
|| License.LicenseType.isBasic(currentLicense.type()) == false
103-
|| LicenseService.SELF_GENERATED_LICENSE_MAX_NODES != currentLicense.maxNodes()
104-
|| LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS != LicenseUtils.getExpiryDate(currentLicense);
103+
|| LicenseSettings.SELF_GENERATED_LICENSE_MAX_NODES != currentLicense.maxNodes()
104+
|| LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS != LicenseUtils.getExpiryDate(currentLicense);
105105
}
106106

107107
private License generateBasicLicense(DiscoveryNodes discoveryNodes) {
108108
final License.Builder specBuilder = License.builder()
109109
.uid(UUID.randomUUID().toString())
110110
.issuedTo(clusterName)
111-
.maxNodes(LicenseService.SELF_GENERATED_LICENSE_MAX_NODES)
111+
.maxNodes(LicenseSettings.SELF_GENERATED_LICENSE_MAX_NODES)
112112
.issueDate(clock.millis())
113113
.type(License.LicenseType.BASIC)
114-
.expiryDate(LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS);
114+
.expiryDate(LicenseSettings.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS);
115115

116116
return SelfGeneratedLicense.create(specBuilder, discoveryNodes);
117117
}

x-pack/plugin/core/src/main/java/org/elasticsearch/license/StartTrialClusterTask.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private LicensesMetadata execute(
7575
return currentLicensesMetadata;
7676
} else if (currentLicensesMetadata == null || currentLicensesMetadata.isEligibleForTrial()) {
7777
long issueDate = clock.millis();
78-
long expiryDate = issueDate + LicenseService.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.getMillis();
78+
long expiryDate = issueDate + LicenseSettings.NON_BASIC_SELF_GENERATED_LICENSE_DURATION.getMillis();
7979

8080
License.Builder specBuilder = License.builder()
8181
.uid(UUID.randomUUID().toString())
@@ -84,9 +84,9 @@ private LicensesMetadata execute(
8484
.type(request.getType())
8585
.expiryDate(expiryDate);
8686
if (License.LicenseType.isEnterprise(request.getType())) {
87-
specBuilder.maxResourceUnits(LicenseService.SELF_GENERATED_LICENSE_MAX_RESOURCE_UNITS);
87+
specBuilder.maxResourceUnits(LicenseSettings.SELF_GENERATED_LICENSE_MAX_RESOURCE_UNITS);
8888
} else {
89-
specBuilder.maxNodes(LicenseService.SELF_GENERATED_LICENSE_MAX_NODES);
89+
specBuilder.maxNodes(LicenseSettings.SELF_GENERATED_LICENSE_MAX_NODES);
9090
}
9191
License selfGeneratedLicense = SelfGeneratedLicense.create(specBuilder, discoveryNodes);
9292
LicensesMetadata newLicensesMetadata = new LicensesMetadata(selfGeneratedLicense, Version.CURRENT);

0 commit comments

Comments
 (0)