From 27659b591b08a837ecbf94fdee17cb03aae63436 Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Wed, 21 Apr 2021 17:47:29 +0300 Subject: [PATCH 1/7] Bug 573004 persist generated key pairs refactor prior invasion: extract password baking unit from service Signed-off-by: eparovyshnaya --- .../core/ProductOperatorServiceImpl.java | 55 +++++++------------ .../products/core/ProductVersionPassword.java | 42 ++++++++++++++ 2 files changed, 63 insertions(+), 34 deletions(-) create mode 100644 bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionPassword.java diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java index d5e14b061..e10a654bf 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018, 2020 ArSysOp + * Copyright (c) 2018, 2021 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -26,7 +26,6 @@ import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; -import org.eclipse.passage.lic.products.ProductDescriptor; import org.eclipse.passage.lic.products.ProductVersionDescriptor; import org.eclipse.passage.lic.products.model.api.Product; import org.eclipse.passage.lic.products.model.api.ProductVersion; @@ -43,52 +42,41 @@ @Component public class ProductOperatorServiceImpl implements OperatorProductService { - private String pluginId; + private String plugin; - private EnvironmentInfo environmentInfo; - private EventAdmin eventAdmin; + private EnvironmentInfo environment; + private EventAdmin events; @Activate public void activate(BundleContext context) { - pluginId = context.getBundle().getSymbolicName(); + plugin = context.getBundle().getSymbolicName(); } @Reference public void bindEnvironmentInfo(EnvironmentInfo info) { - this.environmentInfo = info; + this.environment = info; } public void unbindEnvironmentInfo(EnvironmentInfo info) { - if (environmentInfo == info) { - this.environmentInfo = null; + if (environment == info) { + this.environment = null; } } @Reference public void bindEventAdmin(EventAdmin admin) { - this.eventAdmin = admin; + this.events = admin; } public void unbindEventAdmin(EventAdmin admin) { - if (this.eventAdmin == admin) { - this.eventAdmin = null; + if (this.events == admin) { + this.events = null; } } @Override public String createPassword(ProductVersionDescriptor descriptor) { - String id = null; - String version = null; - if (descriptor != null) { - ProductDescriptor product = descriptor.getProduct(); - if (product != null) { - id = product.getIdentifier(); - } - version = descriptor.getVersion(); - } - StringBuilder sb = new StringBuilder(); - sb.append(id).append("###").append(version); //$NON-NLS-1$ - return sb.toString(); + return new ProductVersionPassword(descriptor).get(); } @Override @@ -98,7 +86,7 @@ public IStatus createProductKeys(ProductVersionDescriptor descriptor) { productVersion = (ProductVersion) descriptor; } if (productVersion == null) { - return new Status(IStatus.ERROR, pluginId, + return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_invalid_product_version); } String installationToken = productVersion.getInstallationToken(); @@ -107,7 +95,7 @@ public IStatus createProductKeys(ProductVersionDescriptor descriptor) { if (publicFile.exists()) { String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_public_key_already_defined; String message = String.format(pattern, publicFile.getAbsolutePath()); - return new Status(IStatus.ERROR, pluginId, message); + return new Status(IStatus.ERROR, plugin, message); } } String secureToken = productVersion.getSecureToken(); @@ -116,21 +104,21 @@ public IStatus createProductKeys(ProductVersionDescriptor descriptor) { if (privateFile.exists()) { String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined; String message = String.format(pattern, privateFile.getAbsolutePath()); - return new Status(IStatus.ERROR, pluginId, message); + return new Status(IStatus.ERROR, plugin, message); } } Product product = productVersion.getProduct(); String errors = LicensingEcore.extractValidationError(product); if (errors != null) { - return new Status(IStatus.ERROR, pluginId, errors); + return new Status(IStatus.ERROR, plugin, errors); } LicensedProduct licensed = new BaseLicensedProduct(product.getIdentifier(), productVersion.getVersion()); Optional codec = codec(licensed); if (codec.isEmpty()) { String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_unable_to_create_keys; String message = String.format(pattern, licensed.version(), product.getName()); - return new Status(IStatus.ERROR, pluginId, message); + return new Status(IStatus.ERROR, plugin, message); } Path path = new UserHomeProductResidence(licensed).get(); try { @@ -142,14 +130,13 @@ public IStatus createProductKeys(ProductVersionDescriptor descriptor) { codec.get().createKeyPair(open, secret, licensed.identifier(), createPassword(productVersion)); productVersion.setInstallationToken(open.toString()); productVersion.setSecureToken(secret.toString()); - eventAdmin.postEvent(OperatorProductEvents.publicCreated(open.toString())); - eventAdmin.postEvent(OperatorProductEvents.privateCreated(secret.toString())); + events.postEvent(OperatorProductEvents.publicCreated(open.toString())); + events.postEvent(OperatorProductEvents.privateCreated(secret.toString())); String format = ProductsCoreMessages.ProductOperatorServiceImpl_ok_keys_exported; String message = String.format(format, open, secret); - return new Status(IStatus.OK, pluginId, message); + return new Status(IStatus.OK, plugin, message); } catch (Exception e) { - return new Status(IStatus.ERROR, pluginId, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, - e); + return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, e); } } diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionPassword.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionPassword.java new file mode 100644 index 000000000..feb06d65c --- /dev/null +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionPassword.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.internal.products.core; + +import java.util.Objects; +import java.util.function.Supplier; + +import org.eclipse.passage.lic.products.ProductVersionDescriptor; + +final class ProductVersionPassword implements Supplier { + + private final ProductVersionDescriptor source; + + ProductVersionPassword(ProductVersionDescriptor source) { + Objects.requireNonNull(source, "ProductVersionPassword::source"); //$NON-NLS-1$ + this.source = source; + } + + @Override + public String get() { + return id() + "###" + version(); //$NON-NLS-1$ + } + + private String id() { + return source.getProduct().getIdentifier(); + } + + private String version() { + return source.getVersion(); + } + +} From 76444eeec5d5b7f99a2014747a12d02da084e128 Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 09:34:19 +0300 Subject: [PATCH 2/7] Bug 573004 persist generated key pairs refactor prior invasion: remove obsolete parts, defragment keys forming code Signed-off-by: eparovyshnaya --- .../core/ProductOperatorServiceImpl.java | 144 +++++++++++------- .../products/core/ProductVersionKeys.java | 42 +++++ .../ui/handlers/ProductExportHandler.java | 12 +- 3 files changed, 141 insertions(+), 57 deletions(-) create mode 100644 bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java index e10a654bf..48782604d 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java @@ -15,19 +15,20 @@ import java.io.File; import java.nio.file.Path; import java.util.Optional; +import java.util.function.Supplier; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.osgi.service.environment.EnvironmentInfo; import org.eclipse.passage.lic.emf.ecore.LicensingEcore; import org.eclipse.passage.lic.internal.api.LicensedProduct; +import org.eclipse.passage.lic.internal.api.LicensingException; import org.eclipse.passage.lic.internal.api.io.StreamCodec; import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; import org.eclipse.passage.lic.products.ProductVersionDescriptor; -import org.eclipse.passage.lic.products.model.api.Product; import org.eclipse.passage.lic.products.model.api.ProductVersion; import org.eclipse.passage.loc.internal.api.OperatorProductEvents; import org.eclipse.passage.loc.internal.api.OperatorProductService; @@ -80,68 +81,107 @@ public String createPassword(ProductVersionDescriptor descriptor) { } @Override - public IStatus createProductKeys(ProductVersionDescriptor descriptor) { - ProductVersion productVersion = null; - if (descriptor instanceof ProductVersion) { - productVersion = (ProductVersion) descriptor; + public IStatus createProductKeys(ProductVersionDescriptor target) { + Optional existing = keysArePresent(target); + if (existing.isPresent()) { + return error(existing.get()); } - if (productVersion == null) { - return new Status(IStatus.ERROR, plugin, - ProductsCoreMessages.ProductOperatorServiceImpl_e_invalid_product_version); + Optional errors = validate(target); + if (errors.isPresent()) { + return error(errors.get()); } - String installationToken = productVersion.getInstallationToken(); - if (installationToken != null) { - File publicFile = new File(installationToken); - if (publicFile.exists()) { - String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_public_key_already_defined; - String message = String.format(pattern, publicFile.getAbsolutePath()); - return new Status(IStatus.ERROR, plugin, message); - } - } - String secureToken = productVersion.getSecureToken(); - if (secureToken != null) { - File privateFile = new File(secureToken); - if (privateFile.exists()) { - String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined; - String message = String.format(pattern, privateFile.getAbsolutePath()); - return new Status(IStatus.ERROR, plugin, message); - } - } - - Product product = productVersion.getProduct(); - String errors = LicensingEcore.extractValidationError(product); - if (errors != null) { - return new Status(IStatus.ERROR, plugin, errors); - } - LicensedProduct licensed = new BaseLicensedProduct(product.getIdentifier(), productVersion.getVersion()); - Optional codec = codec(licensed); + LicensedProduct product = product(target); + Optional codec = codec(product); if (codec.isEmpty()) { - String pattern = ProductsCoreMessages.ProductOperatorServiceImpl_e_unable_to_create_keys; - String message = String.format(pattern, licensed.version(), product.getName()); - return new Status(IStatus.ERROR, plugin, message); + return noCodec(target, product); } - Path path = new UserHomeProductResidence(licensed).get(); + Path destination = new UserHomeProductResidence(product).get(); try { - new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PrivateKey()); - Path open = path.resolve(// - new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PublicKey()).get()); - Path secret = path.resolve(// - new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PrivateKey()).get()); - codec.get().createKeyPair(open, secret, licensed.identifier(), createPassword(productVersion)); - productVersion.setInstallationToken(open.toString()); - productVersion.setSecureToken(secret.toString()); - events.postEvent(OperatorProductEvents.publicCreated(open.toString())); - events.postEvent(OperatorProductEvents.privateCreated(secret.toString())); - String format = ProductsCoreMessages.ProductOperatorServiceImpl_ok_keys_exported; - String message = String.format(format, open, secret); - return new Status(IStatus.OK, plugin, message); + return createKeyPair(target, product, codec.get(), destination); } catch (Exception e) { - return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, e); + return failed(e); } } + private IStatus createKeyPair(ProductVersionDescriptor target, LicensedProduct product, StreamCodec codec, + Path path) throws LicensingException { + Path open = open(product, path); + Path secret = secret(product, path); + codec.createKeyPair(open, secret, product.identifier(), createPassword(target)); + notify(open, secret); + return created(open, secret); + } + + private Status error(String errors) { + return new Status(IStatus.ERROR, plugin, errors); + } + + private Status noCodec(ProductVersionDescriptor target, LicensedProduct product) { + return error(String.format(ProductsCoreMessages.ProductOperatorServiceImpl_e_unable_to_create_keys, + product.version(), target.getProduct().getName())); + } + + private Status failed(Exception e) { + return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, e); + } + + private Status created(Path open, Path secret) { + return new Status(IStatus.OK, plugin, + String.format(ProductsCoreMessages.ProductOperatorServiceImpl_ok_keys_exported, open, secret)); + } + + private BaseLicensedProduct product(ProductVersionDescriptor target) { + return new BaseLicensedProduct(// + target.getProduct().getIdentifier(), // + target.getVersion()); + } + + private void notify(Path open, Path secret) { + events.postEvent(OperatorProductEvents.publicCreated(open.toString())); + events.postEvent(OperatorProductEvents.privateCreated(secret.toString())); + } + + private Path secret(LicensedProduct licensed, Path path) { + return path.resolve(// + new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PrivateKey()).get()); + } + + private Path open(LicensedProduct licensed, Path path) { + return path.resolve(// + new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PublicKey()).get()); + } + private Optional codec(LicensedProduct product) { return new OperatorGearAware().withGear(gear -> gear.codec(product)); } + private Optional validate(ProductVersionDescriptor target) { + if (!(target instanceof ProductVersion)) { + return Optional.empty(); + } + return Optional.ofNullable(LicensingEcore.extractValidationError(((ProductVersion) target).getProduct())); + } + + private Optional keysArePresent(ProductVersionDescriptor target) { + Optional pub = keyIsPresent(target::getInstallationToken, + ProductsCoreMessages.ProductOperatorServiceImpl_e_public_key_already_defined); + if (pub.isPresent()) { + return pub; + } + return keyIsPresent(target::getSecureToken, + ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined); + } + + private Optional keyIsPresent(Supplier path, String error) { + String existing = path.get(); + if (existing == null) { + return Optional.empty(); + } + File file = new File(existing); + if (file.exists()) { + return Optional.of(String.format(error, file.getAbsolutePath())); + } + return Optional.empty(); + } + } diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java new file mode 100644 index 000000000..be1165fab --- /dev/null +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2021 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.loc.internal.products.core; + +import java.util.Objects; +import java.util.function.Supplier; + +import org.eclipse.passage.lic.products.ProductVersionDescriptor; + +final class ProductVersionKeys implements Supplier { + + private final ProductVersionDescriptor source; + + ProductVersionKeys(ProductVersionDescriptor source) { + Objects.requireNonNull(source, "ProductVersionPassword::source"); //$NON-NLS-1$ + this.source = source; + } + + @Override + public String get() { + return id() + "###" + version(); //$NON-NLS-1$ + } + + private String id() { + return source.getProduct().getIdentifier(); + } + + private String version() { + return source.getVersion(); + } + +} diff --git a/bundles/org.eclipse.passage.loc.products.ui/src/org/eclipse/passage/loc/products/ui/handlers/ProductExportHandler.java b/bundles/org.eclipse.passage.loc.products.ui/src/org/eclipse/passage/loc/products/ui/handlers/ProductExportHandler.java index 7c68f278e..a631cfbe4 100644 --- a/bundles/org.eclipse.passage.loc.products.ui/src/org/eclipse/passage/loc/products/ui/handlers/ProductExportHandler.java +++ b/bundles/org.eclipse.passage.loc.products.ui/src/org/eclipse/passage/loc/products/ui/handlers/ProductExportHandler.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2018, 2020 ArSysOp + * Copyright (c) 2018, 2021 ArSysOp * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License 2.0 which is available at @@ -30,21 +30,23 @@ public class ProductExportHandler { @Execute - public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) ProductVersionDescriptor productVersion, + public void execute(@Named(IServiceConstants.ACTIVE_SELECTION) ProductVersionDescriptor owner, IEclipseContext context) { OperatorProductService service = context.get(OperatorProductService.class); - IStatus status = service.createProductKeys(productVersion); + IStatus status = service.createProductKeys(owner); Shell shell = context.get(Shell.class); if (status.isOK()) { String message = status.getMessage(); MessageDialog.openInformation(shell, ProductsUiMessages.ProductExportHandler_title_ok, message); } else { - ErrorDialog.openError(shell, ProductsUiMessages.ProductExportHandler_title_error, ProductsUiMessages.ProductExportHandler_message_error, status); + ErrorDialog.openError(shell, ProductsUiMessages.ProductExportHandler_title_error, + ProductsUiMessages.ProductExportHandler_message_error, status); } } @CanExecute - public boolean canExecute(@Named(IServiceConstants.ACTIVE_SELECTION) @Optional ProductVersionDescriptor productVersion) { + public boolean canExecute( + @Named(IServiceConstants.ACTIVE_SELECTION) @Optional ProductVersionDescriptor productVersion) { return productVersion != null; } From bc9e0107021ca785c4b42b3ad2a7cad0676a8bea Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 09:42:41 +0300 Subject: [PATCH 3/7] Bug 573004 persist generated key pairs refactor prior invasion Signed-off-by: eparovyshnaya --- .../core/ProductOperatorServiceImpl.java | 114 +--------------- .../products/core/ProductVersionKeys.java | 128 ++++++++++++++++-- 2 files changed, 118 insertions(+), 124 deletions(-) diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java index 48782604d..4fe2e0107 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductOperatorServiceImpl.java @@ -12,28 +12,13 @@ *******************************************************************************/ package org.eclipse.passage.loc.internal.products.core; -import java.io.File; import java.nio.file.Path; -import java.util.Optional; -import java.util.function.Supplier; import org.eclipse.core.runtime.IStatus; -import org.eclipse.core.runtime.Status; import org.eclipse.osgi.service.environment.EnvironmentInfo; -import org.eclipse.passage.lic.emf.ecore.LicensingEcore; -import org.eclipse.passage.lic.internal.api.LicensedProduct; -import org.eclipse.passage.lic.internal.api.LicensingException; -import org.eclipse.passage.lic.internal.api.io.StreamCodec; -import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; -import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; -import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; -import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; import org.eclipse.passage.lic.products.ProductVersionDescriptor; -import org.eclipse.passage.lic.products.model.api.ProductVersion; import org.eclipse.passage.loc.internal.api.OperatorProductEvents; import org.eclipse.passage.loc.internal.api.OperatorProductService; -import org.eclipse.passage.loc.internal.equinox.OperatorGearAware; -import org.eclipse.passage.loc.internal.products.core.i18n.ProductsCoreMessages; import org.osgi.framework.BundleContext; import org.osgi.service.component.annotations.Activate; import org.osgi.service.component.annotations.Component; @@ -44,7 +29,6 @@ public class ProductOperatorServiceImpl implements OperatorProductService { private String plugin; - private EnvironmentInfo environment; private EventAdmin events; @@ -82,106 +66,12 @@ public String createPassword(ProductVersionDescriptor descriptor) { @Override public IStatus createProductKeys(ProductVersionDescriptor target) { - Optional existing = keysArePresent(target); - if (existing.isPresent()) { - return error(existing.get()); - } - Optional errors = validate(target); - if (errors.isPresent()) { - return error(errors.get()); - } - LicensedProduct product = product(target); - Optional codec = codec(product); - if (codec.isEmpty()) { - return noCodec(target, product); - } - Path destination = new UserHomeProductResidence(product).get(); - try { - return createKeyPair(target, product, codec.get(), destination); - } catch (Exception e) { - return failed(e); - } - } - - private IStatus createKeyPair(ProductVersionDescriptor target, LicensedProduct product, StreamCodec codec, - Path path) throws LicensingException { - Path open = open(product, path); - Path secret = secret(product, path); - codec.createKeyPair(open, secret, product.identifier(), createPassword(target)); - notify(open, secret); - return created(open, secret); - } - - private Status error(String errors) { - return new Status(IStatus.ERROR, plugin, errors); - } - - private Status noCodec(ProductVersionDescriptor target, LicensedProduct product) { - return error(String.format(ProductsCoreMessages.ProductOperatorServiceImpl_e_unable_to_create_keys, - product.version(), target.getProduct().getName())); - } - - private Status failed(Exception e) { - return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, e); - } - - private Status created(Path open, Path secret) { - return new Status(IStatus.OK, plugin, - String.format(ProductsCoreMessages.ProductOperatorServiceImpl_ok_keys_exported, open, secret)); - } - - private BaseLicensedProduct product(ProductVersionDescriptor target) { - return new BaseLicensedProduct(// - target.getProduct().getIdentifier(), // - target.getVersion()); + return new ProductVersionKeys(plugin, this::broadcast).createKeys(target); } - private void notify(Path open, Path secret) { + private void broadcast(Path open, Path secret) { events.postEvent(OperatorProductEvents.publicCreated(open.toString())); events.postEvent(OperatorProductEvents.privateCreated(secret.toString())); } - private Path secret(LicensedProduct licensed, Path path) { - return path.resolve(// - new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PrivateKey()).get()); - } - - private Path open(LicensedProduct licensed, Path path) { - return path.resolve(// - new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PublicKey()).get()); - } - - private Optional codec(LicensedProduct product) { - return new OperatorGearAware().withGear(gear -> gear.codec(product)); - } - - private Optional validate(ProductVersionDescriptor target) { - if (!(target instanceof ProductVersion)) { - return Optional.empty(); - } - return Optional.ofNullable(LicensingEcore.extractValidationError(((ProductVersion) target).getProduct())); - } - - private Optional keysArePresent(ProductVersionDescriptor target) { - Optional pub = keyIsPresent(target::getInstallationToken, - ProductsCoreMessages.ProductOperatorServiceImpl_e_public_key_already_defined); - if (pub.isPresent()) { - return pub; - } - return keyIsPresent(target::getSecureToken, - ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined); - } - - private Optional keyIsPresent(Supplier path, String error) { - String existing = path.get(); - if (existing == null) { - return Optional.empty(); - } - File file = new File(existing); - if (file.exists()) { - return Optional.of(String.format(error, file.getAbsolutePath())); - } - return Optional.empty(); - } - } diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java index be1165fab..b4fb12dcf 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java @@ -12,31 +12,135 @@ *******************************************************************************/ package org.eclipse.passage.loc.internal.products.core; +import java.io.File; +import java.nio.file.Path; import java.util.Objects; +import java.util.Optional; +import java.util.function.BiConsumer; import java.util.function.Supplier; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.passage.lic.emf.ecore.LicensingEcore; +import org.eclipse.passage.lic.internal.api.LicensedProduct; +import org.eclipse.passage.lic.internal.api.LicensingException; +import org.eclipse.passage.lic.internal.api.io.StreamCodec; +import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; +import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; +import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; +import org.eclipse.passage.lic.internal.base.io.UserHomeProductResidence; import org.eclipse.passage.lic.products.ProductVersionDescriptor; +import org.eclipse.passage.lic.products.model.api.ProductVersion; +import org.eclipse.passage.loc.internal.equinox.OperatorGearAware; +import org.eclipse.passage.loc.internal.products.core.i18n.ProductsCoreMessages; -final class ProductVersionKeys implements Supplier { +final class ProductVersionKeys { - private final ProductVersionDescriptor source; + private final String plugin; + private final BiConsumer notify; - ProductVersionKeys(ProductVersionDescriptor source) { - Objects.requireNonNull(source, "ProductVersionPassword::source"); //$NON-NLS-1$ - this.source = source; + ProductVersionKeys(String plugin, BiConsumer notify) { + this.notify = notify; + Objects.requireNonNull(plugin, "ProductVersionPassword::plugin"); //$NON-NLS-1$ + this.plugin = plugin; } - @Override - public String get() { - return id() + "###" + version(); //$NON-NLS-1$ + public IStatus createKeys(ProductVersionDescriptor target) { + Optional existing = keysArePresent(target); + if (existing.isPresent()) { + return error(existing.get()); + } + Optional invalid = validate(target); + if (invalid.isPresent()) { + return error(invalid.get()); + } + LicensedProduct product = product(target); + Optional codec = codec(product); + if (codec.isEmpty()) { + return noCodec(target, product); + } + try { + return createKeyPair(target, product, codec.get()); + } catch (Exception e) { + return failed(e); + } } - private String id() { - return source.getProduct().getIdentifier(); + private IStatus createKeyPair(ProductVersionDescriptor target, LicensedProduct product, StreamCodec codec) + throws LicensingException { + Path destination = new UserHomeProductResidence(product).get(); + Path open = open(product, destination); + Path secret = secret(product, destination); + codec.createKeyPair(open, secret, product.identifier(), new ProductVersionPassword(target).get()); + notify.accept(open, secret); + return created(open, secret); } - private String version() { - return source.getVersion(); + private Status error(String errors) { + return new Status(IStatus.ERROR, plugin, errors); } + private Status noCodec(ProductVersionDescriptor target, LicensedProduct product) { + return error(String.format(ProductsCoreMessages.ProductOperatorServiceImpl_e_unable_to_create_keys, + product.version(), target.getProduct().getName())); + } + + private Status failed(Exception e) { + return new Status(IStatus.ERROR, plugin, ProductsCoreMessages.ProductOperatorServiceImpl_e_export_error, e); + } + + private Status created(Path open, Path secret) { + return new Status(IStatus.OK, plugin, + String.format(ProductsCoreMessages.ProductOperatorServiceImpl_ok_keys_exported, open, secret)); + } + + private BaseLicensedProduct product(ProductVersionDescriptor target) { + return new BaseLicensedProduct(// + target.getProduct().getIdentifier(), // + target.getVersion()); + } + + private Path secret(LicensedProduct licensed, Path path) { + return path.resolve(// + new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PrivateKey()).get()); + } + + private Path open(LicensedProduct licensed, Path path) { + return path.resolve(// + new FileNameFromLicensedProduct(licensed, new PassageFileExtension.PublicKey()).get()); + } + + @SuppressWarnings("restriction") + private Optional codec(LicensedProduct product) { + return new OperatorGearAware().withGear(gear -> gear.codec(product)); + } + + private Optional validate(ProductVersionDescriptor target) { + if (!(target instanceof ProductVersion)) { + return Optional.empty(); + } + return Optional.ofNullable(LicensingEcore.extractValidationError(((ProductVersion) target).getProduct())); + } + + private Optional keysArePresent(ProductVersionDescriptor target) { + Optional pub = keyIsPresent(target::getInstallationToken, + ProductsCoreMessages.ProductOperatorServiceImpl_e_public_key_already_defined); + if (pub.isPresent()) { + return pub; + } + return keyIsPresent(target::getSecureToken, + ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined); + } + + private Optional keyIsPresent(Supplier path, String error) { + String existing = path.get(); + if (existing == null) { // model(emf)-driven null + return Optional.empty(); + } + File file = new File(existing); + if (file.exists()) { + return Optional.of(String.format(error, file.getAbsolutePath())); + } + return Optional.empty(); + } } From 4e20354cd5f5c09f515b1359f36fbf82a879573c Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 09:46:26 +0300 Subject: [PATCH 4/7] Bug 573004 persist generated key pairs mark points for operator workspace invocation Signed-off-by: eparovyshnaya --- .../passage/loc/internal/products/core/ProductVersionKeys.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java index b4fb12dcf..76df954c1 100644 --- a/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java +++ b/bundles/org.eclipse.passage.loc.products.core/src/org/eclipse/passage/loc/internal/products/core/ProductVersionKeys.java @@ -73,6 +73,7 @@ private IStatus createKeyPair(ProductVersionDescriptor target, LicensedProduct p Path secret = secret(product, destination); codec.createKeyPair(open, secret, product.identifier(), new ProductVersionPassword(target).get()); notify.accept(open, secret); + // TODO: store .keys_xmi under workspace return created(open, secret); } @@ -132,6 +133,7 @@ private Optional keysArePresent(ProductVersionDescriptor target) { ProductsCoreMessages.ProductOperatorServiceImpl_e_private_key_already_defined); } + // TODO: appeal to workspace here private Optional keyIsPresent(Supplier path, String error) { String existing = path.get(); if (existing == null) { // model(emf)-driven null From 31a2655e3e6763bf7c30157d473d746339f0073d Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 10:18:29 +0300 Subject: [PATCH 5/7] Bug 573004 persist generated key pairs bump service version for amended plug-in Signed-off-by: eparovyshnaya --- .../org.eclipse.passage.loc.products.core/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.eclipse.passage.loc.products.core/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.products.core/META-INF/MANIFEST.MF index 3e60875e3..4489101ec 100644 --- a/bundles/org.eclipse.passage.loc.products.core/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.products.core/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.loc.products.core Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.loc.products.core -Bundle-Version: 0.6.200.qualifier +Bundle-Version: 0.6.300.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright From 75396a120d635353dce3a6c963b1e1b6ee423817 Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 10:36:30 +0300 Subject: [PATCH 6/7] Bug 573004 persist generated key pairs bump service version for affected plug-in Signed-off-by: eparovyshnaya --- .../org.eclipse.passage.loc.products.ui/META-INF/MANIFEST.MF | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundles/org.eclipse.passage.loc.products.ui/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.loc.products.ui/META-INF/MANIFEST.MF index 1b7e811b6..64cfbd791 100644 --- a/bundles/org.eclipse.passage.loc.products.ui/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.loc.products.ui/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Automatic-Module-Name: org.eclipse.passage.loc.products.ui Bundle-ManifestVersion: 2 Bundle-SymbolicName: org.eclipse.passage.loc.products.ui;singleton:=true -Bundle-Version: 0.7.0.qualifier +Bundle-Version: 0.7.100.qualifier Bundle-Name: %Bundle-Name Bundle-Vendor: %Bundle-Vendor Bundle-Copyright: %Bundle-Copyright From 0b08198b9a621f83756cc0752b08b8f69951d422 Mon Sep 17 00:00:00 2001 From: Elena Parovyshnaia Date: Thu, 22 Apr 2021 10:50:37 +0300 Subject: [PATCH 7/7] Bug 573004 persist generated key pairs bump service version for affected feature Signed-off-by: eparovyshnaya --- features/org.eclipse.passage.loc.products.feature/feature.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/org.eclipse.passage.loc.products.feature/feature.xml b/features/org.eclipse.passage.loc.products.feature/feature.xml index 7a0813c08..5788fac7b 100644 --- a/features/org.eclipse.passage.loc.products.feature/feature.xml +++ b/features/org.eclipse.passage.loc.products.feature/feature.xml @@ -14,7 +14,7 @@