Skip to content

Commit

Permalink
Support "enterprise" license types (#49223)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tvernum committed Nov 20, 2019
1 parent f92f915 commit 31512fb
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ protected void execute(Terminal terminal, OptionSet options) throws Exception {
ExitCodes.USAGE,
"Must specify either --license or --licenseFile");
}
if (licenseSpec == null) {
throw new UserException(ExitCodes.DATA_ERROR, "Could not parse license spec");
}

// sign
License license = new LicenseSigner(privateKeyPath, publicKeyPath).sign(licenseSpec);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,6 @@
*/
package org.elasticsearch.license;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import org.apache.lucene.util.CollectionUtil;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchParseException;
Expand All @@ -31,11 +22,83 @@
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.protocol.xpack.license.LicenseStatus;

import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Data structure for license. Use {@link Builder} to build a license.
* Provides serialization/deserialization & validation methods for license object
*/
public class License implements ToXContentObject {

public enum LicenseType {
BASIC,
STANDARD,
GOLD,
PLATINUM,
ENTERPRISE,
TRIAL;

public String getTypeName() {
return name().toLowerCase(Locale.ROOT);
}

public static LicenseType parse(String type) throws IllegalArgumentException {
try {
return LicenseType.valueOf(type.toUpperCase(Locale.ROOT));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException("unrecognised license type [ " + type + "], supported license types are ["
+ Stream.of(values()).map(LicenseType::getTypeName).collect(Collectors.joining(",")) + "]");
}
}

/**
* Backward compatible license type parsing for older license models
*/
public static LicenseType resolve(String name) {
switch (name.toLowerCase(Locale.ROOT)) {
case "missing":
return null;
case "trial":
case "none": // bwc for 1.x subscription_type field
case "dev": // bwc for 1.x subscription_type field
case "development": // bwc for 1.x subscription_type field
return TRIAL;
case "basic":
return BASIC;
case "standard":
return STANDARD;
case "silver":
case "gold":
return GOLD;
case "platinum":
case "cloud_internal":
case "internal": // bwc for 1.x subscription_type field
return PLATINUM;
case "enterprise":
return ENTERPRISE;
default:
throw new IllegalArgumentException("unknown license type [" + name + "]");
}
}

static boolean isBasic(String typeName) {
return BASIC.getTypeName().equals(typeName);
}

static boolean isTrial(String typeName) {
return TRIAL.getTypeName().equals(typeName);
}
}

public static final int VERSION_START = 1;
public static final int VERSION_NO_FEATURE_TYPE = 2;
public static final int VERSION_START_DATE = 3;
Expand Down Expand Up @@ -102,28 +165,25 @@ public static int compare(OperationMode opMode1, OperationMode opMode2) {
return Integer.compare(opMode1.id, opMode2.id);
}

public static OperationMode resolve(String type) {
switch (type.toLowerCase(Locale.ROOT)) {
case "missing":
return MISSING;
case "trial":
case "none": // bwc for 1.x subscription_type field
case "dev": // bwc for 1.x subscription_type field
case "development": // bwc for 1.x subscription_type field
return TRIAL;
case "basic":
public static OperationMode resolve(String typeName) {
LicenseType type = LicenseType.resolve(typeName);
if (type == null) {
return MISSING;
}
switch (type) {
case BASIC:
return BASIC;
case "standard":
case STANDARD:
return STANDARD;
case "silver":
case "gold":
case GOLD:
return GOLD;
case "platinum":
case "cloud_internal":
case "internal": // bwc for 1.x subscription_type field
case PLATINUM:
case ENTERPRISE: // TODO Add an explicit enterprise operating mode
return PLATINUM;
case TRIAL:
return TRIAL;
default:
throw new IllegalArgumentException("unknown type [" + type + "]");
throw new IllegalArgumentException("unsupported license type [" + type.getTypeName() + "]");
}
}

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

public Builder type(LicenseType type) {
return type(type.getTypeName());
}

public Builder type(String type) {
this.type = type;
return this;
Expand Down Expand Up @@ -778,6 +842,7 @@ public Builder validate() {
}
return this;
}

}

}
Loading

0 comments on commit 31512fb

Please sign in to comment.