1 /*
   2  * Copyright (c) 2014, 2019, 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 jdk.incubator.jpackage.internal;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.text.MessageFormat;
  31 import java.util.*;
  32 
  33 import static jdk.incubator.jpackage.internal.StandardBundlerParam.*;
  34 import static jdk.incubator.jpackage.internal.MacAppBundler.*;
  35 import static jdk.incubator.jpackage.internal.OverridableResource.createResource;
  36 
  37 public class MacAppStoreBundler extends MacBaseInstallerBundler {
  38 
  39     private static final ResourceBundle I18N = ResourceBundle.getBundle(
  40             "jdk.incubator.jpackage.internal.resources.MacResources");
  41 
  42     private static final String TEMPLATE_BUNDLE_ICON_HIDPI = "java.icns";
  43 
  44     public static final BundlerParamInfo<String> MAC_APP_STORE_APP_SIGNING_KEY =
  45             new StandardBundlerParam<>(
  46             "mac.signing-key-app",
  47             String.class,
  48             params -> {
  49                 String result = MacBaseInstallerBundler.findKey(
  50                         "3rd Party Mac Developer Application: " +
  51                         SIGNING_KEY_USER.fetchFrom(params),
  52                         SIGNING_KEYCHAIN.fetchFrom(params),
  53                         VERBOSE.fetchFrom(params));
  54                 if (result != null) {
  55                     MacCertificate certificate = new MacCertificate(result);
  56 
  57                     if (!certificate.isValid()) {
  58                         Log.error(MessageFormat.format(
  59                                 I18N.getString("error.certificate.expired"),
  60                                 result));
  61                     }
  62                 }
  63 
  64                 return result;
  65             },
  66             (s, p) -> s);
  67 
  68     public static final BundlerParamInfo<String> MAC_APP_STORE_PKG_SIGNING_KEY =
  69             new StandardBundlerParam<>(
  70             "mac.signing-key-pkg",
  71             String.class,
  72             params -> {
  73                 String result = MacBaseInstallerBundler.findKey(
  74                         "3rd Party Mac Developer Installer: " +
  75                         SIGNING_KEY_USER.fetchFrom(params),
  76                         SIGNING_KEYCHAIN.fetchFrom(params),
  77                         VERBOSE.fetchFrom(params));
  78 
  79                 if (result != null) {
  80                     MacCertificate certificate = new MacCertificate(result);
  81 
  82                     if (!certificate.isValid()) {
  83                         Log.error(MessageFormat.format(
  84                                 I18N.getString("error.certificate.expired"),
  85                                 result));
  86                     }
  87                 }
  88 
  89                 return result;
  90             },
  91             (s, p) -> s);
  92 
  93     public static final BundlerParamInfo<String> INSTALLER_SUFFIX =
  94             new StandardBundlerParam<> (
  95             "mac.app-store.installerName.suffix",
  96             String.class,
  97             params -> "-MacAppStore",
  98             (s, p) -> s);
  99 
 100     public File bundle(Map<String, ? super Object> params,
 101             File outdir) throws PackagerException {
 102         Log.verbose(MessageFormat.format(I18N.getString(
 103                 "message.building-bundle"), APP_NAME.fetchFrom(params)));
 104 
 105         IOUtils.writableOutputDir(outdir.toPath());
 106 
 107         // first, load in some overrides
 108         // icns needs @2 versions, so load in the @2 default
 109         params.put(DEFAULT_ICNS_ICON.getID(), TEMPLATE_BUNDLE_ICON_HIDPI);
 110 
 111         // now we create the app
 112         File appImageDir = APP_IMAGE_TEMP_ROOT.fetchFrom(params);
 113         try {
 114             appImageDir.mkdirs();
 115 
 116             try {
 117                 MacAppImageBuilder.addNewKeychain(params);
 118             } catch (InterruptedException e) {
 119                 Log.error(e.getMessage());
 120             }
 121             // first, make sure we don't use the local signing key
 122             params.put(DEVELOPER_ID_APP_SIGNING_KEY.getID(), null);
 123             File appLocation = prepareAppBundle(params);
 124 
 125             String signingIdentity =
 126                     MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params);
 127             String identifierPrefix =
 128                     BUNDLE_ID_SIGNING_PREFIX.fetchFrom(params);
 129 
 130             MacAppImageBuilder.signAppBundle(params, appLocation.toPath(),
 131                     signingIdentity, identifierPrefix);
 132             MacAppImageBuilder.restoreKeychainList(params);
 133 
 134             ProcessBuilder pb;
 135 
 136             // create the final pkg file
 137             File finalPKG = new File(outdir, INSTALLER_NAME.fetchFrom(params)
 138                     + INSTALLER_SUFFIX.fetchFrom(params)
 139                     + ".pkg");
 140             outdir.mkdirs();
 141 
 142             String installIdentify =
 143                     MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params);
 144 
 145             List<String> buildOptions = new ArrayList<>();
 146             buildOptions.add("productbuild");
 147             buildOptions.add("--component");
 148             buildOptions.add(appLocation.toString());
 149             buildOptions.add("/Applications");
 150             buildOptions.add("--sign");
 151             buildOptions.add(installIdentify);
 152             buildOptions.add("--product");
 153             buildOptions.add(appLocation + "/Contents/Info.plist");
 154             String keychainName = SIGNING_KEYCHAIN.fetchFrom(params);
 155             if (keychainName != null && !keychainName.isEmpty()) {
 156                 buildOptions.add("--keychain");
 157                 buildOptions.add(keychainName);
 158             }
 159             buildOptions.add(finalPKG.getAbsolutePath());
 160 
 161             pb = new ProcessBuilder(buildOptions);
 162 
 163             IOUtils.exec(pb);
 164             return finalPKG;
 165         } catch (PackagerException pe) {
 166             throw pe;
 167         } catch (Exception ex) {
 168             Log.verbose(ex);
 169             throw new PackagerException(ex);
 170         }
 171     }
 172 
 173     ///////////////////////////////////////////////////////////////////////
 174     // Implement Bundler
 175     ///////////////////////////////////////////////////////////////////////
 176 
 177     @Override
 178     public String getName() {
 179         return I18N.getString("store.bundler.name");
 180     }
 181 
 182     @Override
 183     public String getID() {
 184         return "mac.appStore";
 185     }
 186 
 187     @Override
 188     public boolean validate(Map<String, ? super Object> params)
 189             throws ConfigException {
 190         try {
 191             Objects.requireNonNull(params);
 192 
 193             // hdiutil is always available so there's no need to test for
 194             // availability.
 195             // run basic validation to ensure requirements are met
 196 
 197             // we are not interested in return code, only possible exception
 198             validateAppImageAndBundeler(params);
 199 
 200             // reject explicitly set to not sign
 201             if (!Optional.ofNullable(MacAppImageBuilder.
 202                     SIGN_BUNDLE.fetchFrom(params)).orElse(Boolean.TRUE)) {
 203                 throw new ConfigException(
 204                         I18N.getString("error.must-sign-app-store"),
 205                         I18N.getString("error.must-sign-app-store.advice"));
 206             }
 207 
 208             // make sure we have settings for signatures
 209             if (MAC_APP_STORE_APP_SIGNING_KEY.fetchFrom(params) == null) {
 210                 throw new ConfigException(
 211                         I18N.getString("error.no-app-signing-key"),
 212                         I18N.getString("error.no-app-signing-key.advice"));
 213             }
 214             if (MAC_APP_STORE_PKG_SIGNING_KEY.fetchFrom(params) == null) {
 215                 throw new ConfigException(
 216                         I18N.getString("error.no-pkg-signing-key"),
 217                         I18N.getString("error.no-pkg-signing-key.advice"));
 218             }
 219 
 220             // things we could check...
 221             // check the icons, make sure it has hidpi icons
 222             // check the category,
 223             // make sure it fits in the list apple has provided
 224             // validate bundle identifier is reverse dns
 225             // check for \a+\.\a+\..
 226 
 227             return true;
 228         } catch (RuntimeException re) {
 229             if (re.getCause() instanceof ConfigException) {
 230                 throw (ConfigException) re.getCause();
 231             } else {
 232                 throw new ConfigException(re);
 233             }
 234         }
 235     }
 236 
 237     @Override
 238     public File execute(Map<String, ? super Object> params,
 239             File outputParentDir) throws PackagerException {
 240         return bundle(params, outputParentDir);
 241     }
 242 
 243     @Override
 244     public boolean supported(boolean runtimeInstaller) {
 245         // return (!runtimeInstaller &&
 246         //         Platform.getPlatform() == Platform.MAC);
 247         return false; // mac-app-store not yet supported
 248     }
 249 
 250     @Override
 251     public boolean isDefault() {
 252         return false;
 253     }
 254 
 255 }