improve internal feature handling

Original commit: elastic/x-pack-elasticsearch@4b088cb64c
This commit is contained in:
Areek Zillur 2014-10-28 11:56:00 -04:00
parent 9947dab389
commit 3445af6d93
2 changed files with 55 additions and 47 deletions

View File

@ -95,6 +95,9 @@ public class ESLicenseSigner {
.withHolder(licenseSpec.issuedTo()) .withHolder(licenseSpec.issuedTo())
.withIssuer(licenseSpec.issuer()); .withIssuer(licenseSpec.issuer());
// NOTE: to add additional feature(s) to the internal license
// encode the new feature(s) in featureToXContent rather
// than doing licenseBuilder.addFeature(..)
XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON); XContentBuilder contentBuilder = XContentFactory.contentBuilder(XContentType.JSON);
featureToXContent(licenseSpec, contentBuilder); featureToXContent(licenseSpec, contentBuilder);
licenseBuilder.addFeature(contentBuilder.string()); licenseBuilder.addFeature(contentBuilder.string());

View File

@ -87,7 +87,7 @@ public class ESLicenseManager {
} }
} catch (ExpiredLicenseException e) { } catch (ExpiredLicenseException e) {
throw new InvalidLicenseException("Expired License"); throw new InvalidLicenseException("Expired License");
} catch (InvalidLicenseException | IOException e) { } catch (InvalidLicenseException e) {
throw new InvalidLicenseException("Invalid License"); throw new InvalidLicenseException("Invalid License");
} }
} }
@ -97,22 +97,16 @@ public class ESLicenseManager {
License license = licenseManager.decryptAndVerifyLicense(signedLicense); License license = licenseManager.decryptAndVerifyLicense(signedLicense);
ESLicense.Builder builder = ESLicense.builder(); ESLicense.Builder builder = ESLicense.builder();
for (License.Feature feature : license.getFeatures()) { if (license.getFeatures().size() == 1) {
String featureName = feature.getName();
LicenseFeatures licenseFeatures;
try { try {
licenseFeatures = licenseFeaturesFromSource(featureName); String featureName = license.getFeatures().get(0).getName();
if (licenseFeatures.maxNodes != -1 LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName);
&& licenseFeatures.feature != null
&& licenseFeatures.type != null
&& licenseFeatures.subscriptionType != null) {
builder.maxNodes(licenseFeatures.maxNodes) builder.maxNodes(licenseFeatures.maxNodes)
.feature(licenseFeatures.feature) .feature(licenseFeatures.feature)
.type(licenseFeatures.type) .type(licenseFeatures.type)
.subscriptionType(licenseFeatures.subscriptionType); .subscriptionType(licenseFeatures.subscriptionType);
break; } catch (IOException ignored) {
} }
} catch (IOException ignored) {}
} }
return builder return builder
@ -125,7 +119,7 @@ public class ESLicenseManager {
.build(); .build();
} }
private static void verifyLicenseFields(License license, ESLicense eslicense) throws IOException { private static void verifyLicenseFields(License license, ESLicense eslicense) {
boolean licenseValid = license.getProductKey().equals(eslicense.uid()) boolean licenseValid = license.getProductKey().equals(eslicense.uid())
&& license.getHolder().equals(eslicense.issuedTo()) && license.getHolder().equals(eslicense.issuedTo())
&& license.getIssueDate() == eslicense.issueDate() && license.getIssueDate() == eslicense.issueDate()
@ -135,16 +129,15 @@ public class ESLicenseManager {
boolean typeValid = false; boolean typeValid = false;
boolean subscriptionTypeValid = false; boolean subscriptionTypeValid = false;
for (License.Feature feature : license.getFeatures()) { if (license.getFeatures().size() == 1) {
String featureName = feature.getName(); try {
String featureName = license.getFeatures().get(0).getName();
LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName); LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName);
maxNodesValid = eslicense.maxNodes() == licenseFeatures.maxNodes; maxNodesValid = eslicense.maxNodes() == licenseFeatures.maxNodes;
typeValid = eslicense.type().equals(licenseFeatures.type); typeValid = eslicense.type().equals(licenseFeatures.type);
subscriptionTypeValid = eslicense.subscriptionType().equals(licenseFeatures.subscriptionType); subscriptionTypeValid = eslicense.subscriptionType().equals(licenseFeatures.subscriptionType);
featureValid = eslicense.feature().equals(licenseFeatures.feature); featureValid = eslicense.feature().equals(licenseFeatures.feature);
} catch (IOException ignored) {
if (maxNodesValid && typeValid && subscriptionTypeValid && featureValid) {
break;
} }
} }
if (!licenseValid || !featureValid || !maxNodesValid || !typeValid || !subscriptionTypeValid) { if (!licenseValid || !featureValid || !maxNodesValid || !typeValid || !subscriptionTypeValid) {
@ -173,19 +166,21 @@ public class ESLicenseManager {
private static LicenseFeatures licenseFeaturesFromSource(String source) throws IOException { private static LicenseFeatures licenseFeaturesFromSource(String source) throws IOException {
XContentParser parser = XContentFactory.xContent(source).createParser(source); XContentParser parser = XContentFactory.xContent(source).createParser(source);
XContentParser.Token token;
String feature = null; String feature = null;
String type = null; String type = null;
String subscriptionType = null; String subscriptionType = null;
int maxNodes = -1; int maxNodes = -1;
String currentName = null; XContentParser.Token token = parser.nextToken();
if (token == XContentParser.Token.START_OBJECT) {
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) { if (token == XContentParser.Token.FIELD_NAME) {
currentName = parser.currentName(); String currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) { token = parser.nextToken();
switch (currentName) { if (token.isValue()) {
if (token == XContentParser.Token.VALUE_STRING) {
switch (currentFieldName) {
case FeatureFields.FEATURE: case FeatureFields.FEATURE:
feature = parser.text(); feature = parser.text();
break; break;
@ -197,11 +192,21 @@ public class ESLicenseManager {
break; break;
} }
} else if (token == XContentParser.Token.VALUE_NUMBER) { } else if (token == XContentParser.Token.VALUE_NUMBER) {
if (FeatureFields.MAX_NODES.equals(currentName)) { if (FeatureFields.MAX_NODES.equals(currentFieldName)) {
maxNodes = parser.intValue(); maxNodes = parser.intValue();
} }
} }
} else if (token == XContentParser.Token.START_ARRAY) {
// It was probably created by newer version - ignoring
parser.skipChildren();
} else if (token == XContentParser.Token.START_OBJECT) {
// It was probably created by newer version - ignoring
parser.skipChildren();
} }
}
}
}
// Should we throw a ElasticsearchParseException here?
return new LicenseFeatures(feature, type, subscriptionType, maxNodes); return new LicenseFeatures(feature, type, subscriptionType, maxNodes);
} }