You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@maven.apache.org by GitBox <gi...@apache.org> on 2020/10/04 14:23:55 UTC

[GitHub] [maven-studies] rfscholte commented on a change in pull request #2: Maven sign plugin

rfscholte commented on a change in pull request #2:
URL: https://github.com/apache/maven-studies/pull/2#discussion_r499251283



##########
File path: src/main/java/org/apache/maven/plugins/sign/SignMojo.java
##########
@@ -0,0 +1,192 @@
+package org.apache.maven.plugins.sign;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.sign.pgp.PGPSigner;
+import org.apache.maven.plugins.sign.pgp.PGPSignerException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectHelper;
+import org.apache.maven.project.artifact.ProjectArtifact;
+import org.eclipse.aether.transform.FileTransformer;
+
+import javax.inject.Inject;
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Sign project artifacts.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Slf4j
+@Mojo( name = "sign", defaultPhase = LifecyclePhase.VERIFY )
+public class SignMojo extends AbstractMojo
+{
+
+    @Parameter( defaultValue = "${project}", readonly = true, required = true )
+    private MavenProject project;
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    protected MavenSession session;
+
+    @Inject
+    private MavenProjectHelper projectHelper;
+
+    private PGPSigner pgpSigner;
+
+    @Override
+    public void execute() throws MojoExecutionException
+    {
+        try
+        {
+            pgpSigner = new PGPSigner(
+                    new PGPSecretKeyInfoFromSettings( session.getSettings().getServer( "pgpKey" ) ) );

Review comment:
       I would expect this to be a parameter

##########
File path: src/main/java/org/apache/maven/plugins/sign/SignMojo.java
##########
@@ -0,0 +1,192 @@
+package org.apache.maven.plugins.sign;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import lombok.SneakyThrows;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.maven.artifact.Artifact;
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.plugins.sign.pgp.PGPSigner;
+import org.apache.maven.plugins.sign.pgp.PGPSignerException;
+import org.apache.maven.project.MavenProject;
+import org.apache.maven.project.MavenProjectHelper;
+import org.apache.maven.project.artifact.ProjectArtifact;
+import org.eclipse.aether.transform.FileTransformer;
+
+import javax.inject.Inject;
+import java.io.BufferedInputStream;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Sign project artifacts.
+ *
+ * @author Slawomir Jaranowski
+ */
+@Slf4j
+@Mojo( name = "sign", defaultPhase = LifecyclePhase.VERIFY )
+public class SignMojo extends AbstractMojo
+{
+
+    @Parameter( defaultValue = "${project}", readonly = true, required = true )
+    private MavenProject project;
+
+    @Parameter( defaultValue = "${session}", required = true, readonly = true )
+    protected MavenSession session;
+
+    @Inject
+    private MavenProjectHelper projectHelper;
+
+    private PGPSigner pgpSigner;
+
+    @Override
+    public void execute() throws MojoExecutionException
+    {
+        try
+        {
+            pgpSigner = new PGPSigner(
+                    new PGPSecretKeyInfoFromSettings( session.getSettings().getServer( "pgpKey" ) ) );
+        }
+        catch ( PGPSignerException e )
+        {
+            throw new MojoExecutionException( e.getMessage(), e );
+        }
+
+        // collect artifact to sign
+        Set<Artifact> artifactsToSign = new HashSet<>();
+
+        artifactsToSign.add( new ProjectArtifact( project ) );
+        artifactsToSign.add( project.getArtifact() );
+        artifactsToSign.addAll( project.getAttachedArtifacts() );
+
+        // sign and attach signature to project
+        artifactsToSign.stream()
+                .map( this::signArtefact )
+                .flatMap( List::stream )
+                .forEach( this::attacheSignResult );
+    }
+
+    /**
+     * Sign given artifact. In result we can have many signatures, transformers can produce multiple output for one
+     * artifact.
+     * <p>
+     * This method ask transformers for inputStream for all artifact mutations, and sign each stream.
+     *
+     * @param artifact artifact to sign
+     * @return sign result
+     */
+    @SneakyThrows
+    private List<SignResult> signArtefact( Artifact artifact )
+    {
+        LOGGER.info( "Sign artifact: {}", artifact );
+
+        org.eclipse.aether.artifact.Artifact srcArtifact = new org.eclipse.aether.artifact.DefaultArtifact(
+                artifact.getGroupId(),
+                artifact.getArtifactId(),
+                artifact.getClassifier(),
+                artifact.getArtifactHandler().getExtension(),
+                artifact.getVersion(),
+                null,
+                artifact.getFile() );
+
+        Collection<FileTransformer> transformersForArtifact = session.getRepositorySession().getFileTransformerManager()

Review comment:
       I hope this could be solved differently. If you mark the plugin with `<extension>true</extension>` Maven should pick up custom FileTransformers. So you should be able to make a `@Named public class SigningFileTransformer` with this business logic

##########
File path: src/main/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettings.java
##########
@@ -0,0 +1,68 @@
+package org.apache.maven.plugins.sign;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.plugins.sign.pgp.PGPSecretKeyInfo;
+import org.apache.maven.settings.Server;
+
+import java.io.File;
+import java.math.BigInteger;
+import java.util.Objects;
+
+/**
+ * Information about pgp key from settings server
+ *
+ * @author Slawomir Jaranowski
+ */
+public class PGPSecretKeyInfoFromSettings implements PGPSecretKeyInfo
+{
+
+    private final Server settingsServer;
+
+    PGPSecretKeyInfoFromSettings( Server settingsServer )
+    {
+        this.settingsServer = Objects.requireNonNull( settingsServer );
+    }
+
+    @Override
+    public Long getKeyId()
+    {
+        try
+        {
+            return new BigInteger( settingsServer.getUsername(), 16 ).longValue();
+        }
+        catch ( NumberFormatException e )
+        {
+            return null;
+        }
+    }
+
+    @Override
+    public char[] getPassphrase()
+    {
+        return settingsServer.getPassphrase().toCharArray();
+    }
+
+    @Override
+    public File getFile()
+    {
+        return new File( settingsServer.getPrivateKey() );

Review comment:
       Is this correct?

##########
File path: src/main/java/org/apache/maven/plugins/sign/PGPSecretKeyInfoFromSettings.java
##########
@@ -0,0 +1,68 @@
+package org.apache.maven.plugins.sign;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.plugins.sign.pgp.PGPSecretKeyInfo;
+import org.apache.maven.settings.Server;
+
+import java.io.File;
+import java.math.BigInteger;
+import java.util.Objects;
+
+/**
+ * Information about pgp key from settings server
+ *
+ * @author Slawomir Jaranowski
+ */
+public class PGPSecretKeyInfoFromSettings implements PGPSecretKeyInfo
+{
+
+    private final Server settingsServer;
+
+    PGPSecretKeyInfoFromSettings( Server settingsServer )
+    {
+        this.settingsServer = Objects.requireNonNull( settingsServer );
+    }
+
+    @Override
+    public Long getKeyId()
+    {
+        try
+        {
+            return new BigInteger( settingsServer.getUsername(), 16 ).longValue();

Review comment:
       Is this correct?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org