Skip to content

Commit 178201a

Browse files
committed
version 0.6: support key generation when key file is absent
1 parent 6e4937f commit 178201a

File tree

7 files changed

+71
-22
lines changed

7 files changed

+71
-22
lines changed

crxtool-core/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<parent>
77
<groupId>com.github.mike10004</groupId>
88
<artifactId>crxtool</artifactId>
9-
<version>0.5</version>
9+
<version>0.6</version>
1010
</parent>
1111
<artifactId>crxtool-core</artifactId>
1212
<name>crxtool-core</name>

crxtool-maven-plugin-example/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>com.github.mike10004</groupId>
77
<artifactId>crxtool</artifactId>
8-
<version>0.5</version>
8+
<version>0.6</version>
99
</parent>
1010
<artifactId>crxtool-maven-plugin-example</artifactId>
1111
<packaging>pom</packaging>

crxtool-maven-plugin/pom.xml

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
<parent>
44
<artifactId>crxtool</artifactId>
55
<groupId>com.github.mike10004</groupId>
6-
<version>0.5</version>
6+
<version>0.6</version>
77
</parent>
88
<modelVersion>4.0.0</modelVersion>
99
<artifactId>crxtool-maven-plugin</artifactId>
1010
<packaging>maven-plugin</packaging>
11-
<name>crxtool-maven-plugin Maven Mojo</name>
11+
<name>crxtool-maven-plugin</name>
1212
<url>http://maven.apache.org</url>
1313
<build>
1414
<plugins>
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.github.mike10004</groupId>
4747
<artifactId>crxtool-testing</artifactId>
48-
<version>0.5</version>
48+
<version>0.6</version>
4949
<scope>test</scope>
5050
</dependency>
5151
</dependencies>

crxtool-maven-plugin/src/main/java/com/github/mike10004/crxtool/maven/PackExtensionMojo.java

+35-7
Original file line numberDiff line numberDiff line change
@@ -55,29 +55,43 @@ public class PackExtensionMojo extends AbstractMojo {
5555
private File outputFile;
5656

5757
/**
58-
* Flag that specifies whether the CRX header should be included. If this is
58+
* Flag that specifies whether the CRX header is to be included. If this is
5959
* true, the output file is in ZIP format.
6060
*/
61-
@Parameter
61+
@Parameter(property = PROP_PREFIX + "excludeHeader")
6262
private boolean excludeHeader;
6363

64+
/**
65+
* Flag that specifies whether a signing key is to be generated if the
66+
* specified private key file is absent. Use in conjunction with
67+
* {@link #privateKey}.
68+
*/
69+
@Parameter(property = PROP_PREFIX + "generateKeyIfAbsent")
70+
private boolean generateKeyIfAbsent;
71+
6472
@Override
6573
public void execute() throws MojoExecutionException {
6674
File outputFile = getOutputFile();
6775
try {
6876
KeyPair keyPair;
69-
if (privateKey != null) {
77+
boolean excludeHeader = isExcludeHeader();
78+
File privateKey_ = getPrivateKey();
79+
if (privateKey_ != null && excludeHeader) {
80+
throw new PrivateKeyParameterConflictException("private key file is specified but excludeHeader is true; if the header is excluded, no private key is required");
81+
}
82+
if (privateKey_ == null || (isGenerateKeyIfAbsent() && !privateKey_.isFile())) {
83+
getLog().debug("generating private key (specified key file is " + privateKey_ + ")");
84+
keyPair = KeyPairs.generateRsKeyPair(createRandom());
85+
} else {
7086
byte[] keyBytes;
71-
try (Reader reader = new InputStreamReader(new FileInputStream(privateKey), StandardCharsets.US_ASCII)) {
87+
try (Reader reader = new InputStreamReader(new FileInputStream(privateKey_), StandardCharsets.US_ASCII)) {
7288
keyBytes = new PemParser().extractBytes(reader);
7389
}
7490
keyPair = KeyPairs.loadRsaKeyPairFromPrivateKeyBytes(keyBytes);
75-
} else {
76-
keyPair = KeyPairs.generateRsKeyPair(createRandom());
7791
}
7892
Path extensionDir = sourceDirectory.toPath();
7993
com.google.common.io.Files.createParentDirs(outputFile);
80-
if (isExcludeHeader()) {
94+
if (excludeHeader) {
8195
byte[] zipBytes = Zipping.zipDirectory(extensionDir, null);
8296
java.nio.file.Files.write(outputFile.toPath(), zipBytes);
8397
} else {
@@ -132,4 +146,18 @@ public void setExcludeHeader(boolean excludeHeader) {
132146
this.excludeHeader = excludeHeader;
133147
}
134148

149+
static class PrivateKeyParameterConflictException extends MojoExecutionException {
150+
151+
public PrivateKeyParameterConflictException(String message) {
152+
super(message);
153+
}
154+
}
155+
156+
public boolean isGenerateKeyIfAbsent() {
157+
return generateKeyIfAbsent;
158+
}
159+
160+
public void setGenerateKeyIfAbsent(boolean generateKeyIfAbsent) {
161+
this.generateKeyIfAbsent = generateKeyIfAbsent;
162+
}
135163
}

crxtool-maven-plugin/src/test/java/com/github/mike10004/crxtool/maven/PackExtensionMojoTest.java

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.mike10004.crxtool.maven;
22

3+
import com.github.mike10004.crxtool.maven.PackExtensionMojo.PrivateKeyParameterConflictException;
34
import com.google.common.collect.ImmutableSet;
45
import com.google.common.io.BaseEncoding;
56
import com.google.common.io.ByteStreams;
@@ -17,6 +18,7 @@
1718
import java.io.ByteArrayInputStream;
1819
import java.io.File;
1920
import java.io.FileInputStream;
21+
import java.io.FileNotFoundException;
2022
import java.io.IOException;
2123
import java.io.InputStream;
2224
import java.net.URISyntaxException;
@@ -36,30 +38,47 @@ public class PackExtensionMojoTest {
3638

3739
@Test
3840
public void testExecute_crx_noPrivateKey() throws Exception {
39-
File crxFile = testExecute(false, null);
41+
File crxFile = testExecute(false, null, false);
4042
checkMetadata(crxFile);
4143
checkZipDataInCrxFile(crxFile);
4244
}
4345

4446
@Test
4547
public void testExecute_crx_privateKey() throws Exception {
4648
File pemFile = buildPemFile(KeyPairs.generateRsKeyPair(new SecureRandom()).getPrivate().getEncoded());
47-
File crxFile = testExecute(false, pemFile);
49+
File crxFile = testExecute(false, pemFile, false);
50+
checkMetadata(crxFile);
51+
checkZipDataInCrxFile(crxFile);
52+
}
53+
54+
@Test
55+
public void testExecute_crx_privateKeyFileSpecifiedButAbsent() throws Exception {
56+
try {
57+
File pemFile = new File(temporaryFolder.newFolder(), "mykey.pem");
58+
testExecute(false, pemFile, false);
59+
} catch (MojoExecutionException e) {
60+
assertTrue(e.getCause() instanceof FileNotFoundException);
61+
}
62+
}
63+
64+
@Test
65+
public void testExecute_crx_privateKeyFileSpecifiedButAbsent_ignoreAbsent() throws Exception {
66+
File pemFile = new File(temporaryFolder.newFolder(), "mykey.pem");
67+
File crxFile = testExecute(false, pemFile, true);
4868
checkMetadata(crxFile);
4969
checkZipDataInCrxFile(crxFile);
5070
}
5171

5272
@Test
5373
public void testExecute_zip_noPrivateKey() throws Exception {
54-
File zipFile = testExecute(true, null);
74+
File zipFile = testExecute(true, null, false);
5575
checkZipData(Files.asByteSource(zipFile).read());
5676
}
5777

58-
@Test
78+
@Test(expected = PrivateKeyParameterConflictException.class)
5979
public void testExecute_zip_privateKey() throws Exception {
60-
File pemFile = buildPemFile(KeyPairs.generateRsKeyPair(new SecureRandom()).getPrivate().getEncoded());
61-
File zipFile = testExecute(true, pemFile);
62-
checkZipData(Files.asByteSource(zipFile).read());
80+
File pemFile = temporaryFolder.newFile();
81+
testExecute(true, pemFile, false);
6382
}
6483

6584
private File buildPemFile(byte[] privateKey) throws IOException {
@@ -84,10 +103,11 @@ private PackExtensionMojo buildMojo() throws IOException, URISyntaxException {
84103
return mojo;
85104
}
86105

87-
private File testExecute(boolean excludeHeader, @Nullable File privateKey) throws IOException, MojoExecutionException, URISyntaxException {
106+
private File testExecute(boolean excludeHeader, @Nullable File privateKey, boolean generateKey) throws IOException, MojoExecutionException, URISyntaxException {
88107
PackExtensionMojo mojo = buildMojo();
89108
mojo.setExcludeHeader(excludeHeader);
90109
mojo.setPrivateKey(privateKey);
110+
mojo.setGenerateKeyIfAbsent(generateKey);
91111
mojo.execute();
92112
File outputFile = mojo.getOutputFile();
93113
assertTrue("file length", outputFile.length() > 0);
@@ -118,4 +138,5 @@ private void checkZipData(byte[] zipData) throws IOException {
118138
assertEquals("filepaths", ImmutableSet.of("manifest.json", "background.js"), ImmutableSet.copyOf(unzippage.fileEntries()));
119139
}
120140

141+
121142
}

crxtool-testing/pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>crxtool</artifactId>
77
<groupId>com.github.mike10004</groupId>
8-
<version>0.5</version>
8+
<version>0.6</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.github.mike10004</groupId>
88
<artifactId>crxtool</artifactId>
9-
<version>0.5</version>
9+
<version>0.6</version>
1010
<name>crxtool</name>
1111
<description>Chrome extension tools</description>
1212
<packaging>pom</packaging>

0 commit comments

Comments
 (0)