From 3445af6d933e3781f550c1c65b222f7bf39457a4 Mon Sep 17 00:00:00 2001 From: Areek Zillur Date: Tue, 28 Oct 2014 11:56:00 -0400 Subject: [PATCH] improve internal feature handling Original commit: elastic/x-pack-elasticsearch@4b088cb64c6f8bdb372d3358f1029d460f7a9d8f --- .../license/licensor/ESLicenseSigner.java | 3 + .../license/manager/ESLicenseManager.java | 99 ++++++++++--------- 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java index 48ee552d796..a536d1c1e66 100644 --- a/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java +++ b/src/main/java/org/elasticsearch/license/licensor/ESLicenseSigner.java @@ -95,6 +95,9 @@ public class ESLicenseSigner { .withHolder(licenseSpec.issuedTo()) .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); featureToXContent(licenseSpec, contentBuilder); licenseBuilder.addFeature(contentBuilder.string()); diff --git a/src/main/java/org/elasticsearch/license/manager/ESLicenseManager.java b/src/main/java/org/elasticsearch/license/manager/ESLicenseManager.java index 98cbb34b0f5..9ac29edb3a4 100644 --- a/src/main/java/org/elasticsearch/license/manager/ESLicenseManager.java +++ b/src/main/java/org/elasticsearch/license/manager/ESLicenseManager.java @@ -87,7 +87,7 @@ public class ESLicenseManager { } } catch (ExpiredLicenseException e) { throw new InvalidLicenseException("Expired License"); - } catch (InvalidLicenseException | IOException e) { + } catch (InvalidLicenseException e) { throw new InvalidLicenseException("Invalid License"); } } @@ -97,22 +97,16 @@ public class ESLicenseManager { License license = licenseManager.decryptAndVerifyLicense(signedLicense); ESLicense.Builder builder = ESLicense.builder(); - for (License.Feature feature : license.getFeatures()) { - String featureName = feature.getName(); - LicenseFeatures licenseFeatures; + if (license.getFeatures().size() == 1) { try { - licenseFeatures = licenseFeaturesFromSource(featureName); - if (licenseFeatures.maxNodes != -1 - && licenseFeatures.feature != null - && licenseFeatures.type != null - && licenseFeatures.subscriptionType != null) { - builder.maxNodes(licenseFeatures.maxNodes) - .feature(licenseFeatures.feature) - .type(licenseFeatures.type) - .subscriptionType(licenseFeatures.subscriptionType); - break; - } - } catch (IOException ignored) {} + String featureName = license.getFeatures().get(0).getName(); + LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName); + builder.maxNodes(licenseFeatures.maxNodes) + .feature(licenseFeatures.feature) + .type(licenseFeatures.type) + .subscriptionType(licenseFeatures.subscriptionType); + } catch (IOException ignored) { + } } return builder @@ -125,7 +119,7 @@ public class ESLicenseManager { .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()) && license.getHolder().equals(eslicense.issuedTo()) && license.getIssueDate() == eslicense.issueDate() @@ -135,16 +129,15 @@ public class ESLicenseManager { boolean typeValid = false; boolean subscriptionTypeValid = false; - for (License.Feature feature : license.getFeatures()) { - String featureName = feature.getName(); - LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName); - maxNodesValid = eslicense.maxNodes() == licenseFeatures.maxNodes; - typeValid = eslicense.type().equals(licenseFeatures.type); - subscriptionTypeValid = eslicense.subscriptionType().equals(licenseFeatures.subscriptionType); - featureValid = eslicense.feature().equals(licenseFeatures.feature); - - if (maxNodesValid && typeValid && subscriptionTypeValid && featureValid) { - break; + if (license.getFeatures().size() == 1) { + try { + String featureName = license.getFeatures().get(0).getName(); + LicenseFeatures licenseFeatures = licenseFeaturesFromSource(featureName); + maxNodesValid = eslicense.maxNodes() == licenseFeatures.maxNodes; + typeValid = eslicense.type().equals(licenseFeatures.type); + subscriptionTypeValid = eslicense.subscriptionType().equals(licenseFeatures.subscriptionType); + featureValid = eslicense.feature().equals(licenseFeatures.feature); + } catch (IOException ignored) { } } if (!licenseValid || !featureValid || !maxNodesValid || !typeValid || !subscriptionTypeValid) { @@ -173,35 +166,47 @@ public class ESLicenseManager { private static LicenseFeatures licenseFeaturesFromSource(String source) throws IOException { XContentParser parser = XContentFactory.xContent(source).createParser(source); - XContentParser.Token token; String feature = null; String type = null; String subscriptionType = null; int maxNodes = -1; - String currentName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentName = parser.currentName(); - } else if (token == XContentParser.Token.VALUE_STRING) { - switch (currentName) { - case FeatureFields.FEATURE: - feature = parser.text(); - break; - case FeatureFields.TYPE: - type = parser.text(); - break; - case FeatureFields.SUBSCRIPTION_TYPE: - subscriptionType = parser.text(); - break; - } - } else if (token == XContentParser.Token.VALUE_NUMBER) { - if (FeatureFields.MAX_NODES.equals(currentName)) { - maxNodes = parser.intValue(); + XContentParser.Token token = parser.nextToken(); + if (token == XContentParser.Token.START_OBJECT) { + while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { + if (token == XContentParser.Token.FIELD_NAME) { + String currentFieldName = parser.currentName(); + token = parser.nextToken(); + if (token.isValue()) { + if (token == XContentParser.Token.VALUE_STRING) { + switch (currentFieldName) { + case FeatureFields.FEATURE: + feature = parser.text(); + break; + case FeatureFields.TYPE: + type = parser.text(); + break; + case FeatureFields.SUBSCRIPTION_TYPE: + subscriptionType = parser.text(); + break; + } + } else if (token == XContentParser.Token.VALUE_NUMBER) { + if (FeatureFields.MAX_NODES.equals(currentFieldName)) { + 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); }