Skip to content

Commit c52813c

Browse files
committed
Support "enterprise" license types
This adds "enterprise" as an acceptable type for a license loaded through the PUT _license API. Internally an enterprise license is treated as having a "platinum" operating mode. The handling of License types was refactored to have a new explicit "LicenseType" enum in addition to the existing "OperatingMode" enum. Backport of: elastic#49223
1 parent c239a6a commit c52813c

14 files changed

+270
-138
lines changed

x-pack/license-tools/src/main/java/org/elasticsearch/license/licensor/tools/LicenseGeneratorTool.java

+3
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,9 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception {
8888
ExitCodes.USAGE,
8989
"Must specify either --license or --licenseFile");
9090
}
91+
if (licenseSpec == null) {
92+
throw new UserException(ExitCodes.DATA_ERROR, "Could not parse license spec");
93+
}
9194

9295
// sign
9396
License license = new LicenseSigner(privateKeyPath, publicKeyPath).sign(licenseSpec);

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

+92-27
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@
55
*/
66
package org.elasticsearch.license;
77

8-
import java.io.IOException;
9-
import java.io.InputStream;
10-
import java.nio.ByteBuffer;
11-
import java.util.ArrayList;
12-
import java.util.Base64;
13-
import java.util.Comparator;
14-
import java.util.List;
15-
import java.util.Locale;
16-
178
import org.apache.lucene.util.CollectionUtil;
189
import org.elasticsearch.ElasticsearchException;
1910
import org.elasticsearch.ElasticsearchParseException;
@@ -31,11 +22,83 @@
3122
import org.elasticsearch.common.xcontent.XContentType;
3223
import org.elasticsearch.protocol.xpack.license.LicenseStatus;
3324

25+
import java.io.IOException;
26+
import java.io.InputStream;
27+
import java.nio.ByteBuffer;
28+
import java.util.ArrayList;
29+
import java.util.Base64;
30+
import java.util.Comparator;
31+
import java.util.List;
32+
import java.util.Locale;
33+
import java.util.stream.Collectors;
34+
import java.util.stream.Stream;
35+
3436
/**
3537
* Data structure for license. Use {@link Builder} to build a license.
3638
* Provides serialization/deserialization & validation methods for license object
3739
*/
3840
public class License implements ToXContentObject {
41+
42+
public enum LicenseType {
43+
BASIC,
44+
STANDARD,
45+
GOLD,
46+
PLATINUM,
47+
ENTERPRISE,
48+
TRIAL;
49+
50+
public String getTypeName() {
51+
return name().toLowerCase(Locale.ROOT);
52+
}
53+
54+
public static LicenseType parse(String type) throws IllegalArgumentException {
55+
try {
56+
return LicenseType.valueOf(type.toUpperCase(Locale.ROOT));
57+
} catch (IllegalArgumentException e) {
58+
throw new IllegalArgumentException("unrecognised license type [ " + type + "], supported license types are ["
59+
+ Stream.of(values()).map(LicenseType::getTypeName).collect(Collectors.joining(",")) + "]");
60+
}
61+
}
62+
63+
/**
64+
* Backward compatible license type parsing for older license models
65+
*/
66+
public static LicenseType resolve(String name) {
67+
switch (name.toLowerCase(Locale.ROOT)) {
68+
case "missing":
69+
return null;
70+
case "trial":
71+
case "none": // bwc for 1.x subscription_type field
72+
case "dev": // bwc for 1.x subscription_type field
73+
case "development": // bwc for 1.x subscription_type field
74+
return TRIAL;
75+
case "basic":
76+
return BASIC;
77+
case "standard":
78+
return STANDARD;
79+
case "silver":
80+
case "gold":
81+
return GOLD;
82+
case "platinum":
83+
case "cloud_internal":
84+
case "internal": // bwc for 1.x subscription_type field
85+
return PLATINUM;
86+
case "enterprise":
87+
return ENTERPRISE;
88+
default:
89+
throw new IllegalArgumentException("unknown license type [" + name + "]");
90+
}
91+
}
92+
93+
static boolean isBasic(String typeName) {
94+
return BASIC.getTypeName().equals(typeName);
95+
}
96+
97+
static boolean isTrial(String typeName) {
98+
return TRIAL.getTypeName().equals(typeName);
99+
}
100+
}
101+
39102
public static final int VERSION_START = 1;
40103
public static final int VERSION_NO_FEATURE_TYPE = 2;
41104
public static final int VERSION_START_DATE = 3;
@@ -102,28 +165,25 @@ public static int compare(OperationMode opMode1, OperationMode opMode2) {
102165
return Integer.compare(opMode1.id, opMode2.id);
103166
}
104167

105-
public static OperationMode resolve(String type) {
106-
switch (type.toLowerCase(Locale.ROOT)) {
107-
case "missing":
108-
return MISSING;
109-
case "trial":
110-
case "none": // bwc for 1.x subscription_type field
111-
case "dev": // bwc for 1.x subscription_type field
112-
case "development": // bwc for 1.x subscription_type field
113-
return TRIAL;
114-
case "basic":
168+
public static OperationMode resolve(String typeName) {
169+
LicenseType type = LicenseType.resolve(typeName);
170+
if (type == null) {
171+
return MISSING;
172+
}
173+
switch (type) {
174+
case BASIC:
115175
return BASIC;
116-
case "standard":
176+
case STANDARD:
117177
return STANDARD;
118-
case "silver":
119-
case "gold":
178+
case GOLD:
120179
return GOLD;
121-
case "platinum":
122-
case "cloud_internal":
123-
case "internal": // bwc for 1.x subscription_type field
180+
case PLATINUM:
181+
case ENTERPRISE: // TODO Add an explicit enterprise operating mode
124182
return PLATINUM;
183+
case TRIAL:
184+
return TRIAL;
125185
default:
126-
throw new IllegalArgumentException("unknown type [" + type + "]");
186+
throw new IllegalArgumentException("unsupported license type [" + type.getTypeName() + "]");
127187
}
128188
}
129189

@@ -301,7 +361,7 @@ private void validate() {
301361
throw new IllegalStateException("maxNodes has to be set");
302362
} else if (expiryDate == -1) {
303363
throw new IllegalStateException("expiryDate has to be set");
304-
} else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && "basic".equals(type) == false) {
364+
} else if (expiryDate == LicenseService.BASIC_SELF_GENERATED_LICENSE_EXPIRATION_MILLIS && LicenseType.isBasic(type) == false) {
305365
throw new IllegalStateException("only basic licenses are allowed to have no expiration");
306366
}
307367
}
@@ -689,6 +749,10 @@ public Builder issueDate(long issueDate) {
689749
return this;
690750
}
691751

752+
public Builder type(LicenseType type) {
753+
return type(type.getTypeName());
754+
}
755+
692756
public Builder type(String type) {
693757
this.type = type;
694758
return this;
@@ -778,6 +842,7 @@ public Builder validate() {
778842
}
779843
return this;
780844
}
845+
781846
}
782847

783848
}

0 commit comments

Comments
 (0)