-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
564818 API revision | conditions | key keepers review #282
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e04975f
Bug 564818 API revision | conditions | key keepers review
eparovyshnaya bb421f4
Bug 564818 API revision | conditions | key keepers review
eparovyshnaya f10f50d
Bug 564818 API revision | conditions | key keepers review
eparovyshnaya 12d838f
Bug 564818 API revision | conditions | key keepers review
eparovyshnaya f850888
Bug 564818 API revision | conditions | key keepers review
eparovyshnaya File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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$ | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have
PassageBundlePath
later to encapsulate a way to find the info like this?We may want to store the licensing information under a subfolder later, like they store bundle properties under
OSGI-INF/l10n
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thought about that, but lots of questions arised. This implementation sticks to the current functionality to keep status quo where it's possible. Let's do it when we actually will need it.