-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #282 from eclipse-passage/564818
564818 API revision | conditions | key keepers review
- Loading branch information
Showing
12 changed files
with
232 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
....passage.lic.equinox/src/org/eclipse/passage/lic/internal/equinox/io/BundleKeyKeeper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 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.lic.internal.equinox.io; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import org.eclipse.passage.lic.internal.api.LicensedProduct; | ||
import org.eclipse.passage.lic.internal.api.LicensingException; | ||
import org.eclipse.passage.lic.internal.api.io.KeyKeeper; | ||
import org.eclipse.passage.lic.internal.base.io.FileNameFromLicensedProduct; | ||
import org.eclipse.passage.lic.internal.base.io.PassageFileExtension; | ||
import org.eclipse.passage.lic.internal.equinox.i18n.EquinoxMessages; | ||
import org.osgi.framework.Bundle; | ||
|
||
/** | ||
* Look for the product's public key into OSGI-INF folder of the configured | ||
* bundle. | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public final class BundleKeyKeeper implements KeyKeeper { | ||
|
||
private final Supplier<LicensedProduct> product; | ||
private final Bundle bundle; | ||
|
||
public BundleKeyKeeper(Supplier<LicensedProduct> product, Bundle bundle) { | ||
Objects.requireNonNull(product, "BundleKeyKeeper::product"); //$NON-NLS-1$ | ||
Objects.requireNonNull(bundle, "BundleKeyKeeper::bundle"); //$NON-NLS-1$ | ||
this.product = product; | ||
this.bundle = bundle; | ||
} | ||
|
||
@Override | ||
public LicensedProduct id() { | ||
return product.get(); | ||
} | ||
|
||
@Override | ||
public InputStream productPublicKey() throws LicensingException { | ||
URL resource = resource(Paths.get("OSGI-INF").resolve(keyFile())); //$NON-NLS-1$ | ||
try { | ||
return resource.openStream(); | ||
} catch (IOException e) { | ||
throw new LicensingException(String.format(// | ||
EquinoxMessages.BundleKeyKeeper_failed_to_open_stream, // | ||
product.get(), // | ||
resource.toString())); | ||
} | ||
} | ||
|
||
/** | ||
* Either returns functional URL for the given {@code path} under the configured | ||
* {@code bundle}, or fails with properly explained | ||
* {@linkplain LicensingException} | ||
*/ | ||
private URL resource(Path path) throws LicensingException { | ||
Optional<URL> url = Optional.ofNullable(bundle.getResource(path.toString())); | ||
if (!url.isPresent()) { | ||
throw new LicensingException(String.format(// | ||
EquinoxMessages.BundleKeyKeeper_failed_to_find_file, // | ||
product.get(), // | ||
path.toString(), // | ||
bundle.getSymbolicName())); | ||
} | ||
return url.get(); | ||
} | ||
|
||
private String keyFile() { | ||
return new FileNameFromLicensedProduct(product.get(), new PassageFileExtension.PublicKey()).get(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tests/org.eclipse.passage.lic.equinox.tests/OSGI-INF/fake-product_1.0.0.pub
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
This test .pub file | ||
emulates public key file | ||
for a product [fake-product] | ||
of version [1.0.0] | ||
for test BundleKeyKeeperTest. | ||
|
||
Totally fake. | ||
|
||
Illustrates, after all, | ||
that the format is not checked | ||
on an input stream opening. |
80 changes: 80 additions & 0 deletions
80
...ic.equinox.tests/src/org/eclipse/passage/lic/internal/equinox/io/BundleKeyKeeperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 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.lic.internal.equinox.io; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.eclipse.passage.lic.internal.api.LicensedProduct; | ||
import org.eclipse.passage.lic.internal.api.LicensingException; | ||
import org.eclipse.passage.lic.internal.base.BaseLicensedProduct; | ||
import org.junit.Test; | ||
import org.osgi.framework.Bundle; | ||
import org.osgi.framework.FrameworkUtil; | ||
|
||
/** | ||
* Integration test: demands OSGi running | ||
*/ | ||
@SuppressWarnings("restriction") | ||
public final class BundleKeyKeeperTest { | ||
|
||
@Test | ||
public void exisgingKeyFileFound() throws IOException { | ||
try { | ||
InputStream stream = new BundleKeyKeeper(this::productWithKey, bundle()).productPublicKey(); | ||
assertNotNull(stream); | ||
stream.close(); | ||
} catch (LicensingException e) { | ||
fail("Public key file stream retrieval is not supposed to fail on valid data"); //$NON-NLS-1$ | ||
} | ||
} | ||
|
||
@SuppressWarnings("resource") // stream is not supposed to be opened | ||
@Test | ||
public void foreignProductKeyIsIgnired() throws IOException { | ||
try { | ||
|
||
new BundleKeyKeeper(this::productWithoutKey, bundle()).productPublicKey(); | ||
} catch (LicensingException e) { | ||
assertTrue(e.getMessage().contains("find")); //$NON-NLS-1$ | ||
return; | ||
} | ||
fail("Public key for the foreign product is not supposed to fit"); //$NON-NLS-1$ | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void productIsMandatory() { | ||
new BundleKeyKeeper(null, bundle()); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void bundleIsMandatory() { | ||
new BundleKeyKeeper(this::productWithKey, null); | ||
} | ||
|
||
private Bundle bundle() { | ||
return FrameworkUtil.getBundle(getClass()); | ||
} | ||
|
||
private LicensedProduct productWithKey() { | ||
return new BaseLicensedProduct("fake-product", "1.0.0"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
|
||
private LicensedProduct productWithoutKey() { | ||
return new BaseLicensedProduct("another-fake-product", "1.0.0"); //$NON-NLS-1$ //$NON-NLS-2$ | ||
} | ||
} |