Skip to content

Commit 52f56e6

Browse files
author
Alexey Semenyuk
committed
8353196: [macos] Contents of ".jpackage.xml" file are wrong when building .pkg from unsigned app image
Reviewed-by: almatvee
1 parent acd4da4 commit 52f56e6

File tree

5 files changed

+77
-17
lines changed

5 files changed

+77
-17
lines changed

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacAppImageBuilder.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,11 @@ private void writeEntry(InputStream in, Path dstFile) throws IOException {
266266
Files.copy(in, dstFile);
267267
}
268268

269+
@Override
270+
protected boolean withAppImageFile(Map<String, ? super Object> params) {
271+
return !withPackageFile;
272+
}
273+
269274
@Override
270275
public void prepareApplicationFiles(Map<String, ? super Object> params)
271276
throws IOException {

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,11 @@ protected Path prepareAppBundle(Map<String, ? super Object> params)
173173
FileUtils.copyRecursive(predefinedImage, appDir,
174174
LinkOption.NOFOLLOW_LINKS);
175175

176-
// Create PackageFile if predefined app image is not signed
177-
if (!StandardBundlerParam.isRuntimeInstaller(params) &&
178-
!AppImageFile.load(predefinedImage).isSigned()) {
176+
// Alter app image if predefined app image is not signed
177+
if (!AppImageFile.load(predefinedImage).isSigned()) {
179178
new PackageFile(APP_NAME.fetchFrom(params)).save(
180179
ApplicationLayout.macAppImage().resolveAt(appDir));
180+
Files.deleteIfExists(AppImageFile.getPathInAppImage(appDir));
181181
// We need to re-sign app image after adding ".package" to it.
182182
// We only do this if app image was not signed which means it is
183183
// signed with ad-hoc signature. App bundles with ad-hoc

src/jdk.jpackage/share/classes/jdk/jpackage/internal/AbstractAppImageBuilder.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ public InputStream getResourceAsStream(String name) {
6666
public abstract void prepareApplicationFiles(
6767
Map<String, ? super Object> params) throws IOException;
6868

69+
protected boolean withAppImageFile(Map<String, ? super Object> params) {
70+
return true;
71+
}
72+
6973
protected void writeCfgFile(Map<String, ? super Object> params) throws
7074
IOException {
7175
new CfgFile().initFromParams(params).create(root);
@@ -96,7 +100,9 @@ protected void copyApplication(Map<String, ? super Object> params)
96100
appLayout.appDirectory().toAbsolutePath(), excludes);
97101
}
98102

99-
AppImageFile.save(root, params);
103+
if (withAppImageFile(params)) {
104+
AppImageFile.save(root, params);
105+
}
100106

101107
List<String> items = APP_CONTENT.fetchFrom(params);
102108
for (String item : items) {

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public Path outputBundle() {
374374
String dirName;
375375
if (!TKit.isOSX()) {
376376
dirName = name();
377-
} else if (hasArgument("--app-image") && hasArgument("--mac-sign")) {
377+
} else if (MacHelper.signPredefinedAppImage(this)) {
378378
// Request to sign external app image, not to build a new one
379379
dirName = getArgumentValue("--app-image");
380380
} else {
@@ -936,38 +936,54 @@ JPackageCommand assertAppLayout() {
936936
return this;
937937
}
938938

939+
private boolean expectAppImageFile() {
940+
if (isRuntime()) {
941+
return false;
942+
}
943+
944+
if (TKit.isOSX()) {
945+
if (MacHelper.signPredefinedAppImage(this)) {
946+
// Request to sign external app image, ".jpackage.xml" file should exist.
947+
return true;
948+
}
949+
950+
if (!isImagePackageType() && hasArgument("--app-image")) {
951+
// Build native macOS package from an external app image.
952+
// If the external app image is signed, ".jpackage.xml" file should be kept, otherwise removed.
953+
return AppImageFile.load(Path.of(getArgumentValue("--app-image"))).macSigned();
954+
}
955+
}
956+
957+
return isImagePackageType();
958+
}
959+
939960
private void assertAppImageFile() {
940961
Path appImageDir = Path.of("");
941962
if (isImagePackageType() && hasArgument("--app-image")) {
942963
appImageDir = Path.of(getArgumentValue("--app-image"));
943964
}
944965

945966
final Path lookupPath = AppImageFile.getPathInAppImage(appImageDir);
946-
if (isRuntime() || (!isImagePackageType() && !TKit.isOSX())) {
967+
if (!expectAppImageFile()) {
947968
assertFileInAppImage(lookupPath, null);
948-
} else if (!TKit.isOSX()) {
949-
assertFileInAppImage(lookupPath, lookupPath);
950969
} else {
951970
assertFileInAppImage(lookupPath, lookupPath);
952971

953-
// If file exist validated important values based on arguments
954-
// Exclude validation when we generating packages from predefined
955-
// app images, since we do not know if image is signed or not.
956-
if (isImagePackageType() || !hasArgument("--app-image")) {
972+
if (TKit.isOSX()) {
957973
final Path rootDir = isImagePackageType() ? outputBundle() :
958974
pathToUnpackedPackageFile(appInstallationDirectory());
959975

960976
AppImageFile aif = AppImageFile.load(rootDir);
961977

962-
boolean expectedValue = hasArgument("--mac-sign");
978+
boolean expectedValue = MacHelper.appImageSigned(this);
963979
boolean actualValue = aif.macSigned();
964-
TKit.assertEquals(Boolean.toString(expectedValue), Boolean.toString(actualValue),
965-
"Check for unexptected value in app image file for <signed>");
980+
TKit.assertEquals(expectedValue, actualValue,
981+
"Check for unexpected value of <signed> property in app image file");
966982

967983
expectedValue = hasArgument("--mac-app-store");
968984
actualValue = aif.macAppStore();
969-
TKit.assertEquals(Boolean.toString(expectedValue), Boolean.toString(actualValue),
970-
"Check for unexptected value in app image file for <app-store>");
985+
TKit.assertEquals(expectedValue, actualValue,
986+
"Check for unexpected value of <app-store> property in app image file");
971987
}
972988
}
973989
}
@@ -1037,11 +1053,17 @@ JPackageCommand setUnpackedPackageLocation(Path path) {
10371053
}
10381054

10391055
JPackageCommand winMsiLogFile(Path v) {
1056+
if (!TKit.isWindows()) {
1057+
throw new UnsupportedOperationException();
1058+
}
10401059
this.winMsiLogFile = v;
10411060
return this;
10421061
}
10431062

10441063
public Optional<Path> winMsiLogFile() {
1064+
if (!TKit.isWindows()) {
1065+
throw new UnsupportedOperationException();
1066+
}
10451067
return Optional.ofNullable(winMsiLogFile);
10461068
}
10471069

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/MacHelper.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.nio.file.Path;
3434
import java.util.ArrayList;
3535
import java.util.List;
36+
import java.util.Objects;
3637
import java.util.Optional;
3738
import java.util.Set;
3839
import java.util.regex.Pattern;
@@ -149,6 +150,32 @@ public static PListWrapper readPList(Stream<String> lines) {
149150
.collect(Collectors.joining()))).get();
150151
}
151152

153+
public static boolean signPredefinedAppImage(JPackageCommand cmd) {
154+
Objects.requireNonNull(cmd);
155+
if (!TKit.isOSX()) {
156+
throw new UnsupportedOperationException();
157+
}
158+
return cmd.hasArgument("--mac-sign") && cmd.hasArgument("--app-image");
159+
}
160+
161+
public static boolean appImageSigned(JPackageCommand cmd) {
162+
Objects.requireNonNull(cmd);
163+
if (!TKit.isOSX()) {
164+
throw new UnsupportedOperationException();
165+
}
166+
167+
if (Optional.ofNullable(cmd.getArgumentValue("--app-image")).map(Path::of).map(AppImageFile::load).map(AppImageFile::macSigned).orElse(false)) {
168+
// The external app image is signed, so the app image is signed too.
169+
return true;
170+
}
171+
172+
if (!cmd.hasArgument("--mac-sign")) {
173+
return false;
174+
}
175+
176+
return (cmd.hasArgument("--mac-signing-key-user-name") || cmd.hasArgument("--mac-app-image-sign-identity"));
177+
}
178+
152179
static PackageHandlers createDmgPackageHandlers() {
153180
return new PackageHandlers(MacHelper::installDmg, MacHelper::uninstallDmg, MacHelper::unpackDmg);
154181
}

0 commit comments

Comments
 (0)