1 /* 2 * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.oracle.tools.packager.mac; 27 28 import com.oracle.tools.packager.BundlerParamInfo; 29 import com.oracle.tools.packager.StandardBundlerParam; 30 import com.oracle.tools.packager.Log; 31 import com.oracle.tools.packager.ConfigException; 32 import com.oracle.tools.packager.IOUtils; 33 import com.oracle.tools.packager.Platform; 34 import com.oracle.tools.packager.UnsupportedPlatformException; 35 import jdk.packager.internal.legacy.builders.mac.MacAppImageBuilder; 36 37 import java.io.File; 38 import java.io.FileNotFoundException; 39 import java.io.IOException; 40 import java.text.MessageFormat; 41 import java.util.ArrayList; 42 import java.util.Arrays; 43 import java.util.Collection; 44 import java.util.LinkedHashSet; 45 import java.util.List; 46 import java.util.Map; 47 import java.util.Optional; 48 import java.util.ResourceBundle; 49 50 import static com.oracle.tools.packager.StandardBundlerParam.*; 51 import static com.oracle.tools.packager.mac.MacAppBundler.*; 52 import jdk.packager.internal.legacy.mac.MacCertificate; 53 54 public class MacAppStoreBundler extends MacBaseInstallerBundler { 55 56 private static final ResourceBundle I18N = 57 ResourceBundle.getBundle(MacAppStoreBundler.class.getName()); 58 59 private static final String TEMPLATE_BUNDLE_ICON_HIDPI = "GenericAppHiDPI.icns"; 60 private final static String DEFAULT_ENTITLEMENTS = "MacAppStore.entitlements"; 61 private final static String DEFAULT_INHERIT_ENTITLEMENTS = "MacAppStore_Inherit.entitlements"; 62 63 public static final BundlerParamInfo<String> MAC_APP_STORE_APP_SIGNING_KEY = new StandardBundlerParam<>( 64 I18N.getString("param.signing-key-app.name"), 65 I18N.getString("param.signing-key-app.description"), 66 "mac.signing-key-app", 67 String.class, 68 params -> { 69 String result = MacBaseInstallerBundler.findKey("3rd Party Mac Developer Application: " + SIGNING_KEY_USER.fetchFrom(params), 70 SIGNING_KEYCHAIN.fetchFrom(params), 71 VERBOSE.fetchFrom(params)); 72 if (result != null) { 73 MacCertificate certificate = new MacCertificate(result, VERBOSE.fetchFrom(params)); 74 75 if (!certificate.isValid()) { 76 Log.info(MessageFormat.format(I18N.getString("error.certificate.expired"), result)); 77 } 78 } 79 80 return result; 81 }, 82 (s, p) -> s); 83 84 public static final BundlerParamInfo<String> MAC_APP_STORE_PKG_SIGNING_KEY = new StandardBundlerParam<>( 85 I18N.getString("param.signing-key-pkg.name"), 86 I18N.getString("param.signing-key-pkg.description"), 87 "mac.signing-key-pkg", 88 String.class, 89 params -> { 90 String result = MacBaseInstallerBundler.findKey("3rd Party Mac Developer Installer: " + SIGNING_KEY_USER.fetchFrom(params), SIGNING_KEYCHAIN.fetchFrom(params), VERBOSE.fetchFrom(params)); 91 92 if (result != null) { 93 MacCertificate certificate = new MacCertificate(result, VERBOSE.fetchFrom(params)); 94 95 if (!certificate.isValid()) { 96 Log.info(MessageFormat.format(I18N.getString("error.certificate.expired"), result)); 97 } 98 } 99 100 return result; 101 }, 102 (s, p) -> s); 103 104 public static final StandardBundlerParam<File> MAC_APP_STORE_ENTITLEMENTS = new StandardBundlerParam<>( 105 I18N.getString("param.mac-app-store-entitlements.name"), 106 I18N.getString("param.mac-app-store-entitlements.description"), 107 "mac.app-store-entitlements", 108 File.class, 109 params -> null, 110 (s, p) -> new File(s)); 111 112 public static final BundlerParamInfo<String> INSTALLER_SUFFIX = new StandardBundlerParam<> ( 113 I18N.getString("param.installer-suffix.name"), 114 I18N.getString("param.installer-suffix.description"), 115 "mac.app-store.installerName.suffix", 116 String.class, 117 params -> "-MacAppStore", 118 (s, p) -> s); 119 120 public MacAppStoreBundler() { 121 super(); 122 baseResourceLoader = MacResources.class; 123 } 124 125 //@Override 126 public File bundle(Map<String, ? super Object> p, File outdir) { 127 Log.info(MessageFormat.format(I18N.getString("message.building-bundle"), APP_NAME.fetchFrom(p))); 128 if (!outdir.isDirectory() && !outdir.mkdirs()) { 129 throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-create-output-dir"), outdir.getAbsolutePath())); 130 } 131 if (!outdir.canWrite()) { 132 throw new RuntimeException(MessageFormat.format(I18N.getString("error.cannot-write-to-output-dir"), outdir.getAbsolutePath())); 133 } 134 135 // first, load in some overrides 136 // icns needs @2 versions, so load in the @2 default 137 p.put(DEFAULT_ICNS_ICON.getID(), TEMPLATE_BUNDLE_ICON_HIDPI); 138 139 // next we need to change the jdk/jre stripping to strip gstreamer 140 // p.put(MAC_RULES.getID(), createMacAppStoreRuntimeRules(p)); 141 142 // now we create the app 143 File appImageDir = APP_IMAGE_BUILD_ROOT.fetchFrom(p); 144 try { 145 appImageDir.mkdirs(); 146 147 // first, make sure we don't use the local signing key 148 p.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null); 149 File appLocation = prepareAppBundle(p); 150 151 prepareEntitlements(p); 152 153 String signingIdentity = MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(p); 154 String identifierPrefix = BUNDLE_ID_SIGNING_PREFIX.fetchFrom(p); 155 String entitlementsFile = getConfig_Entitlements(p).toString(); 156 String inheritEntitlements = getConfig_Inherit_Entitlements(p).toString(); 157 158 MacAppImageBuilder.signAppBundle(p, appLocation.toPath(), signingIdentity, identifierPrefix, entitlementsFile, inheritEntitlements); 159 ProcessBuilder pb; 160 161 // create the final pkg file 162 File finalPKG = new File(outdir, INSTALLER_NAME.fetchFrom(p) 163 + INSTALLER_SUFFIX.fetchFrom(p) 164 + ".pkg"); 165 outdir.mkdirs(); 166 167 String installIdentify = MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(p); 168 169 List<String> buildOptions = new ArrayList<>(); 170 buildOptions.add("productbuild"); 171 buildOptions.add("--component"); 172 buildOptions.add(appLocation.toString()); 173 buildOptions.add("/Applications"); 174 buildOptions.add("--sign"); 175 buildOptions.add(installIdentify); 176 buildOptions.add("--product"); 177 buildOptions.add(appLocation + "/Contents/Info.plist"); 178 String keychainName = SIGNING_KEYCHAIN.fetchFrom(p); 179 if (keychainName != null && !keychainName.isEmpty()) { 180 buildOptions.add("--keychain"); 181 buildOptions.add(keychainName); 182 } 183 buildOptions.add(finalPKG.getAbsolutePath()); 184 185 pb = new ProcessBuilder(buildOptions); 186 187 IOUtils.exec(pb, VERBOSE.fetchFrom(p)); 188 return finalPKG; 189 } catch (Exception ex) { 190 Log.info("App Store Ready Bundle failed : " + ex.getMessage()); 191 ex.printStackTrace(); 192 Log.debug(ex); 193 return null; 194 } finally { 195 try { 196 if (appImageDir != null && !Log.isDebug()) { 197 IOUtils.deleteRecursive(appImageDir); 198 } else if (appImageDir != null) { 199 Log.info(MessageFormat.format(I18N.getString("mesasge.intermediate-bundle-location"), appImageDir.getAbsolutePath())); 200 } 201 if (!VERBOSE.fetchFrom(p)) { 202 //cleanup 203 cleanupConfigFiles(p); 204 } else { 205 Log.info(MessageFormat.format(I18N.getString("message.config-save-location"), CONFIG_ROOT.fetchFrom(p).getAbsolutePath())); 206 } 207 } catch (IOException ex) { 208 //noinspection ReturnInsideFinallyBlock 209 Log.debug(ex.getMessage()); 210 return null; 211 } 212 } 213 } 214 215 protected void cleanupConfigFiles(Map<String, ? super Object> params) { 216 if (getConfig_Entitlements(params) != null) { 217 getConfig_Entitlements(params).delete(); 218 } 219 if (getConfig_Inherit_Entitlements(params) != null) { 220 getConfig_Inherit_Entitlements(params).delete(); 221 } 222 if (MAC_APP_IMAGE.fetchFrom(params) == null) { 223 APP_BUNDLER.fetchFrom(params).cleanupConfigFiles(params); 224 } 225 } 226 227 private File getConfig_Entitlements(Map<String, ? super Object> params) { 228 return new File(CONFIG_ROOT.fetchFrom(params), APP_NAME.fetchFrom(params) + ".entitlements"); 229 } 230 231 private File getConfig_Inherit_Entitlements(Map<String, ? super Object> params) { 232 return new File(CONFIG_ROOT.fetchFrom(params), APP_NAME.fetchFrom(params) + "_Inherit.entitlements"); 233 } 234 235 private void prepareEntitlements(Map<String, ? super Object> params) throws IOException { 236 File entitlements = MAC_APP_STORE_ENTITLEMENTS.fetchFrom(params); 237 if (entitlements == null || !entitlements.exists()) { 238 fetchResource(getEntitlementsFileName(params), 239 I18N.getString("resource.mac-app-store-entitlements"), 240 DEFAULT_ENTITLEMENTS, 241 getConfig_Entitlements(params), 242 VERBOSE.fetchFrom(params), 243 DROP_IN_RESOURCES_ROOT.fetchFrom(params)); 244 } else { 245 fetchResource(getEntitlementsFileName(params), 246 I18N.getString("resource.mac-app-store-entitlements"), 247 entitlements, 248 getConfig_Entitlements(params), 249 VERBOSE.fetchFrom(params), 250 DROP_IN_RESOURCES_ROOT.fetchFrom(params)); 251 } 252 fetchResource(getInheritEntitlementsFileName(params), 253 I18N.getString("resource.mac-app-store-inherit-entitlements"), 254 DEFAULT_INHERIT_ENTITLEMENTS, 255 getConfig_Inherit_Entitlements(params), 256 VERBOSE.fetchFrom(params), 257 DROP_IN_RESOURCES_ROOT.fetchFrom(params)); 258 } 259 260 private String getEntitlementsFileName(Map<String, ? super Object> params) { 261 return MAC_BUNDLER_PREFIX+ APP_NAME.fetchFrom(params) +".entitlements"; 262 } 263 264 private String getInheritEntitlementsFileName(Map<String, ? super Object> params) { 265 return MAC_BUNDLER_PREFIX+ APP_NAME.fetchFrom(params) +"_Inherit.entitlements"; 266 } 267 268 269 ////////////////////////////////////////////////////////////////////////////////// 270 // Implement Bundler 271 ////////////////////////////////////////////////////////////////////////////////// 272 273 @Override 274 public String getName() { 275 return I18N.getString("bundler.name"); 276 } 277 278 @Override 279 public String getDescription() { 280 return I18N.getString("bundler.description"); 281 } 282 283 @Override 284 public String getID() { 285 return "mac.appStore"; 286 } 287 288 @Override 289 public Collection<BundlerParamInfo<?>> getBundleParameters() { 290 Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); 291 results.addAll(getAppBundleParameters()); 292 results.addAll(getMacAppStoreBundleParameters()); 293 return results; 294 } 295 296 public Collection<BundlerParamInfo<?>> getMacAppStoreBundleParameters() { 297 Collection<BundlerParamInfo<?>> results = new LinkedHashSet<>(); 298 299 results.addAll(getAppBundleParameters()); 300 results.remove(DEVELOPER_ID_APP_SIGNING_KEY); 301 results.addAll(Arrays.asList( 302 INSTALLER_SUFFIX, 303 MAC_APP_STORE_APP_SIGNING_KEY, 304 MAC_APP_STORE_ENTITLEMENTS, 305 MAC_APP_STORE_PKG_SIGNING_KEY, 306 SIGNING_KEYCHAIN 307 )); 308 309 return results; 310 } 311 312 @Override 313 public boolean validate(Map<String, ? super Object> params) throws UnsupportedPlatformException, ConfigException { 314 try { 315 if (Platform.getPlatform() != Platform.MAC) { 316 throw new UnsupportedPlatformException(); 317 } 318 319 if (params == null) { 320 throw new ConfigException( 321 I18N.getString("error.parameters-null"), 322 I18N.getString("error.parameters-null.advice")); 323 } 324 325 // hdiutil is always available so there's no need to test for availability. 326 //run basic validation to ensure requirements are met 327 328 //TODO Mac App Store apps cannot use the system runtime 329 330 //we are not interested in return code, only possible exception 331 validateAppImageAndBundeler(params); 332 333 // reject explicitly set to not sign 334 if (!Optional.ofNullable(SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.TRUE)) { 335 throw new ConfigException( 336 I18N.getString("error.must-sign-app-store"), 337 I18N.getString("error.must-sign-app-store.advice")); 338 } 339 340 // make sure we have settings for signatures 341 if (MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params) == null) { 342 throw new ConfigException( 343 I18N.getString("error.no-app-signing-key"), 344 I18N.getString("error.no-app-signing-key.advice")); 345 } 346 if (MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params) == null) { 347 throw new ConfigException( 348 I18N.getString("error.no-pkg-signing-key"), 349 I18N.getString("error.no-pkg-signing-key.advice")); 350 } 351 352 // things we could check... 353 // check the icons, make sure it has hidpi icons 354 // check the category, make sure it fits in the list apple has provided 355 // validate bundle identifier is reverse dns 356 // check for \a+\.\a+\.. 357 358 return true; 359 } catch (RuntimeException re) { 360 if (re.getCause() instanceof ConfigException) { 361 throw (ConfigException) re.getCause(); 362 } else { 363 throw new ConfigException(re); 364 } 365 } 366 } 367 368 @Override 369 public File execute(Map<String, ? super Object> params, File outputParentDir) { 370 return bundle(params, outputParentDir); 371 } 372 }