Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add setting to restrict license types #49418

Merged
merged 6 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix resolution of v1 style licenses
  • Loading branch information
tvernum committed Nov 22, 2019
commit b08cde5784692de0420a1748e5f598df2e1903fe
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,23 @@ public static LicenseType parse(String type) throws IllegalArgumentException {
/**
* Backward compatible license type parsing for older license models
*/
public static LicenseType resolve(String name) {
public static LicenseType resolve(License license) {
if (license.version == VERSION_START) {
// in 1.x: the acceptable values for 'subscription_type': none | dev | silver | gold | platinum
return resolve(license.subscriptionType);
} else {
// in 2.x: the acceptable values for 'type': trial | basic | silver | dev | gold | platinum
// in 5.x: the acceptable values for 'type': trial | basic | standard | dev | gold | platinum
// in 6.x: the acceptable values for 'type': trial | basic | standard | dev | gold | platinum
// in 7.x: the acceptable values for 'type': trial | basic | standard | dev | gold | platinum | enterprise
return resolve(license.type);
}
}

/**
* Backward compatible license type parsing for older license models
*/
static LicenseType resolve(String name) {
switch (name.toLowerCase(Locale.ROOT)) {
case "missing":
return null;
Expand Down Expand Up @@ -168,8 +184,12 @@ public static int compare(OperationMode opMode1, OperationMode opMode2) {
return Integer.compare(opMode1.id, opMode2.id);
}

public static OperationMode resolve(String typeName) {
LicenseType type = LicenseType.resolve(typeName);
/**
* Determine the operating mode for a license type
* @see LicenseType#resolve(License)
* @see #parse(String)
*/
public static OperationMode resolve(LicenseType type) {
if (type == null) {
return MISSING;
}
Expand All @@ -190,6 +210,21 @@ public static OperationMode resolve(String typeName) {
}
}

/**
* Parses an {@code OperatingMode} from a String.
* The string must name an operating mode, and not a licensing level (that is, it cannot parse old style license levels
* such as "dev" or "silver").
* @see #description()
*/
public static OperationMode parse(String mode) {
try {
return OperationMode.valueOf(mode.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("unrecognised license operating mode [ " + mode + "], supported modes are ["
+ Stream.of(values()).map(OperationMode::description).collect(Collectors.joining(",")) + "]");
}
}

public String description() {
return name().toLowerCase(Locale.ROOT);
}
Expand All @@ -215,13 +250,7 @@ private License(int version, String uid, String issuer, String issuedTo, long is
}
this.maxNodes = maxNodes;
this.startDate = startDate;
if (version == VERSION_START) {
// in 1.x: the acceptable values for 'subscription_type': none | dev | silver | gold | platinum
this.operationMode = OperationMode.resolve(subscriptionType);
} else {
// in 2.x: the acceptable values for 'type': trial | basic | silver | dev | gold | platinum
this.operationMode = OperationMode.resolve(type);
}
this.operationMode = OperationMode.resolve(LicenseType.resolve(this));
validate();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public void registerLicense(final PutLicenseRequest request, final ActionListene
final License newLicense = request.license();
final License.LicenseType licenseType;
try {
licenseType = License.LicenseType.resolve(newLicense.type());
licenseType = License.LicenseType.resolve(newLicense);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not consume any part of the license before we validate its signature first

} catch (Exception e) {
listener.onFailure(e);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ private synchronized void onChange(Path file) {
// this UTF-8 conversion is much pickier than java String
final String operationMode = new BytesRef(content).utf8ToString();
try {
newOperationMode = OperationMode.resolve(operationMode);
newOperationMode = OperationMode.parse(operationMode);
} catch (IllegalArgumentException e) {
logger.error(
(Supplier<?>) () -> new ParameterizedMessage(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public RemoteClusterLicenseChecker(final Client client, final Predicate<License.
}

public static boolean isLicensePlatinumOrTrial(final XPackInfoResponse.LicenseInfo licenseInfo) {
final License.OperationMode mode = License.OperationMode.resolve(licenseInfo.getMode());
final License.OperationMode mode = License.OperationMode.parse(licenseInfo.getMode());
return mode == License.OperationMode.PLATINUM || mode == License.OperationMode.TRIAL;
}

Expand Down Expand Up @@ -168,7 +168,7 @@ public void onResponse(final XPackInfoResponse xPackInfoResponse) {
return;
}
if ((licenseInfo.getStatus() == LicenseStatus.ACTIVE) == false
|| predicate.test(License.OperationMode.resolve(licenseInfo.getMode())) == false) {
|| predicate.test(License.OperationMode.parse(licenseInfo.getMode())) == false) {
listener.onResponse(LicenseCheck.failure(new RemoteClusterLicenseInfo(clusterAlias.get(), licenseInfo)));
return;
}
Expand Down Expand Up @@ -282,7 +282,7 @@ public static String buildErrorMessage(
final String message = String.format(
Locale.ROOT,
"the license mode [%s] on cluster [%s] does not enable [%s]",
License.OperationMode.resolve(remoteClusterLicenseInfo.licenseInfo().getMode()),
License.OperationMode.parse(remoteClusterLicenseInfo.licenseInfo().getMode()),
remoteClusterLicenseInfo.clusterAlias(),
feature);
error.append(message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static TrainedModelConfig.Builder fromXContent(XContentParser parser, boo
throw new IllegalArgumentException("[" + ESTIMATED_OPERATIONS.getPreferredName() + "] must be greater than or equal to 0");
}
this.estimatedOperations = estimatedOperations;
this.licenseLevel = License.OperationMode.resolve(ExceptionsHelper.requireNonNull(licenseLevel, LICENSE_LEVEL));
this.licenseLevel = License.OperationMode.parse(ExceptionsHelper.requireNonNull(licenseLevel, LICENSE_LEVEL));
}

public TrainedModelConfig(StreamInput in) throws IOException {
Expand All @@ -153,7 +153,7 @@ public TrainedModelConfig(StreamInput in) throws IOException {
input = new TrainedModelInput(in);
estimatedHeapMemory = in.readVLong();
estimatedOperations = in.readVLong();
licenseLevel = License.OperationMode.resolve(in.readString());
licenseLevel = License.OperationMode.parse(in.readString());
}

public String getModelId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ public void testResolveUnknown() {

for (String type : types) {
try {
OperationMode.resolve(type);
final License.LicenseType licenseType = License.LicenseType.resolve(type);
OperationMode.resolve(licenseType);

fail(String.format(Locale.ROOT, "[%s] should not be recognized as an operation mode", type));
}
Expand All @@ -69,7 +70,8 @@ public void testResolveUnknown() {

private static void assertResolve(OperationMode expected, String... types) {
for (String type : types) {
assertThat(OperationMode.resolve(type), equalTo(expected));
License.LicenseType licenseType = License.LicenseType.resolve(type);
assertThat(OperationMode.resolve(licenseType), equalTo(expected));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void init() throws Exception {
}

public void testLicenseOperationModeUpdate() throws Exception {
String type = randomFrom("trial", "basic", "standard", "gold", "platinum");
License.LicenseType type = randomFrom(License.LicenseType.values());
License license = License.builder()
.uid("id")
.expiryDate(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ public static License generateSignedLicense(long issueDate, TimeValue expiryDura
}

public static License generateSignedLicense(String type, long issueDate, TimeValue expiryDuration) throws Exception {
// If there's an explicit "type", don't generate v1 style license because they don't have the same types.
int version = randomIntBetween(type == null ? License.VERSION_START : License.VERSION_NO_FEATURE_TYPE, License.VERSION_CURRENT);
return generateSignedLicense(type, version, issueDate, expiryDuration);
return generateSignedLicense(type, randomIntBetween(License.VERSION_START, License.VERSION_CURRENT), issueDate, expiryDuration);
}

public static License generateSignedLicenseOldSignature() {
Expand Down