Skip to content

Commit

Permalink
Fix eclipse-tycho#203 : assemble-maven-repository should include PGP …
Browse files Browse the repository at this point in the history
…signatures in

the P2 site

Signed-off-by: Christoph Läubrich <laeubi@laeubi-soft.de>
  • Loading branch information
laeubi committed Aug 19, 2021
1 parent 2dfb052 commit 6ad5991
Show file tree
Hide file tree
Showing 4 changed files with 383 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*******************************************************************************
* Copyright (c) 2021 Christoph Läubrich and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Christoph Läubrich - initial API and implementation
*******************************************************************************/
package org.eclipse.tycho.p2.tools.publisher;

import java.util.Map;

import org.eclipse.equinox.p2.metadata.IInstallableUnit;
import org.eclipse.equinox.p2.metadata.MetadataFactory.InstallableUnitDescription;
import org.eclipse.equinox.p2.metadata.Version;
import org.eclipse.equinox.p2.publisher.AbstractAdvice;
import org.eclipse.equinox.p2.publisher.actions.IPropertyAdvice;
import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor;
import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor;

@SuppressWarnings("restriction")
public class PGPSignatureAdvice extends AbstractAdvice implements IPropertyAdvice {
private final String id;
private final Version version;
private final String signature;

public PGPSignatureAdvice(String id, Version version, String signature) {
this.id = id;
this.version = version;
this.signature = signature;
}

@Override
protected String getId() {
return id;
}

@Override
protected Version getVersion() {
return version;
}

@Override
public Map<String, String> getInstallableUnitProperties(InstallableUnitDescription iu) {
return null;
}

@Override
public boolean isApplicable(String configSpec, boolean includeDefault, String candidateId,
Version candidateVersion) {
return id.equals(candidateId) && version.equals(candidateVersion);
}

@Override
public Map<String, String> getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor) {
// workaround Bug 539672
if (descriptor instanceof ArtifactDescriptor) {
ArtifactDescriptor artifactDescriptor = (ArtifactDescriptor) descriptor;
artifactDescriptor.setProperty("pgp.signatures", signature);
}
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ public class TychoFeaturesAndBundlesPublisherApplication extends AbstractPublish
private static final String MAVEN_PREFIX = "maven-";
private BundleDescription[] bundles;
private File[] advices;
private String[] signatures;
private URI categoryDefinition;
private String[] rules;
private String publicKeys;

@Override
public Object run(PublisherInfo publisherInfo) throws Exception {
Expand All @@ -75,6 +77,9 @@ public Object run(PublisherInfo publisherInfo) throws Exception {
SimpleArtifactRepository repo = (SimpleArtifactRepository) repoFactory.load(artifactLocation,
IRepositoryManager.REPOSITORY_HINT_MODIFIABLE, null);
repo.setRules(newRules);
if (publicKeys != null) {
repo.setProperty("pgp.publicKeys", publicKeys);
}
repo.save();
}
}
Expand All @@ -85,41 +90,46 @@ public Object run(PublisherInfo publisherInfo) throws Exception {
protected void processParameter(String arg, String parameter, PublisherInfo publisherInfo)
throws URISyntaxException {
super.processParameter(arg, parameter, publisherInfo);

if (arg.equalsIgnoreCase("-bundles")) {
bundles = Arrays.stream(AbstractPublisherAction.getArrayFromString(parameter, ",")).map(File::new)
.map(t -> {
try {
return BundlesAction.createBundleDescription(t);
} catch (IOException | BundleException e) {
//ignoring files that are "not bundles" they will be skipped on the later steps
return null;
}
}).toArray(BundleDescription[]::new);
}
if (arg.equalsIgnoreCase("-bundlesFile")) {
bundles = Arrays.stream(getArrayFromFile(parameter)).map(File::new).map(t -> {
try {
return BundlesAction.createBundleDescription(t);
} catch (IOException | BundleException e) {
//ignoring files that are "not bundles" they will be skipped on the later steps
System.out.println("Ignore " + t.getName() + " as it is not a bundle!");
return null;
}
}).toArray(BundleDescription[]::new);
}
if (arg.equalsIgnoreCase("-advices")) {
advices = Arrays.stream(AbstractPublisherAction.getArrayFromString(parameter, ","))
.map(str -> str.isBlank() ? null : new File(str)).toArray(File[]::new);
}
if (arg.equalsIgnoreCase("-advicesFile")) {
advices = Arrays.stream(getArrayFromFile(parameter)).map(str -> str.isBlank() ? null : new File(str))
.toArray(File[]::new);
}
if (arg.equalsIgnoreCase("-signatures")) {
signatures = Arrays.stream(getArrayFromFile(parameter)).map(str -> str.isBlank() ? null : new File(str))
.map(file -> {
try {
return FileUtils.readFileToString(file, StandardCharsets.US_ASCII);
} catch (IOException e) {
// skip unreadable files...
return null;
}
}).toArray(String[]::new);
}
if (arg.equalsIgnoreCase("-categoryDefinition")) {
categoryDefinition = URIUtil.fromString(parameter);
}
if (arg.equalsIgnoreCase("-rules")) {
rules = AbstractPublisherAction.getArrayFromString(parameter, ",");
}
if (arg.equalsIgnoreCase("-publicKeys")) {
try {
publicKeys = FileUtils.readFileToString(new File(parameter), StandardCharsets.US_ASCII);
} catch (IOException e) {
throw new URISyntaxException(parameter, "can't read public key file: " + e);
}
}
}

private String[] getArrayFromFile(String parameter) {
Expand All @@ -139,11 +149,13 @@ protected IPublisherAction[] createActions() {
for (int i = 0; i < advices.length; i++) {
File adviceFile = advices[i];
BundleDescription bundleDescription;
if (i >= bundles.length - 1 || (bundleDescription = bundles[i]) == null) {
if (i >= bundles.length || (bundleDescription = bundles[i]) == null) {
System.out.println("Skip advice " + adviceFile + " iff");
continue;
}
String symbolicName = bundleDescription.getSymbolicName();
if (symbolicName == null) {
System.out.println("Skip advice " + adviceFile + " not a bundle");
//not a bundle... no advice...
continue;
}
Expand All @@ -157,6 +169,7 @@ public Map<String, String> getArtifactProperties(IInstallableUnit iu,

// workaround Bug 539672
Map<String, String> properties = super.getInstallableUnitProperties(null);
System.out.println(iu + " = " + properties);
if (properties != null) {
if (descriptor instanceof ArtifactDescriptor) {
ArtifactDescriptor artifactDescriptor = (ArtifactDescriptor) descriptor;
Expand Down Expand Up @@ -194,6 +207,28 @@ public Map<String, String> getInstallableUnitProperties(InstallableUnitDescripti
result.add(new AdviceFilePublisherAction(advicesList));
}
}
if (signatures != null) {
List<PGPSignatureAdvice> signaturesList = new ArrayList<>();
for (int i = 0; i < signatures.length; i++) {
String signature = signatures[i];
if (signature == null) {
//no signature...
continue;
}
BundleDescription bundleDescription;
if (i >= bundles.length || (bundleDescription = bundles[i]) == null) {
continue;
}
String symbolicName = bundleDescription.getSymbolicName();
if (symbolicName == null) {
//not a bundle... no signature...
continue;
}
signaturesList.add(new PGPSignatureAdvice(symbolicName,
PublisherHelper.fromOSGiVersion(bundleDescription.getVersion()), signature));
}
result.add(new SignaturePublisherAction(signaturesList));
}
if (bundles != null) {
result.add(new BundlesAction(bundles));
}
Expand All @@ -203,6 +238,24 @@ public Map<String, String> getInstallableUnitProperties(InstallableUnitDescripti
return result.toArray(IPublisherAction[]::new);
}

private static final class SignaturePublisherAction extends AbstractPublisherAction {

private List<PGPSignatureAdvice> signaturesList;

public SignaturePublisherAction(List<PGPSignatureAdvice> signaturesList) {
this.signaturesList = signaturesList;
}

@Override
public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) {
for (PGPSignatureAdvice signature : signaturesList) {
publisherInfo.addAdvice(signature);
}
return Status.OK_STATUS;
}

}

private static final class AdviceFilePublisherAction extends AbstractPublisherAction {

private List<AdviceFileAdvice> advicesList;
Expand Down
5 changes: 5 additions & 0 deletions tycho-p2/tycho-p2-repository-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
<artifactId>sisu-equinox-launching</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpg-jdk16</artifactId>
<version>1.46</version>
</dependency>
</dependencies>

</project>
Loading

0 comments on commit 6ad5991

Please sign in to comment.