Search in sources :

Example 1 with SigningConfiguration

use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.

the class BuildApksCommand method getStampSigningConfiguration.

private static SigningConfiguration getStampSigningConfiguration(ParsedFlags flags, PrintStream out, SystemEnvironmentProvider systemEnvironmentProvider) {
    // Signing-related flags.
    Optional<Path> signingKeystorePath = KEYSTORE_FLAG.getValue(flags);
    Optional<Password> signingKeystorePassword = KEYSTORE_PASSWORD_FLAG.getValue(flags);
    Optional<String> signingKeyAlias = KEY_ALIAS_FLAG.getValue(flags);
    Optional<Password> signingKeyPassword = KEY_PASSWORD_FLAG.getValue(flags);
    // Stamp-related flags.
    Optional<Path> stampKeystorePath = STAMP_KEYSTORE_FLAG.getValue(flags);
    Optional<Password> stampKeystorePassword = STAMP_KEYSTORE_PASSWORD_FLAG.getValue(flags);
    Optional<String> stampKeyAlias = STAMP_KEY_ALIAS_FLAG.getValue(flags);
    Optional<Password> stampKeyPassword = STAMP_KEY_PASSWORD_FLAG.getValue(flags);
    Path keystorePath = null;
    Optional<Password> keystorePassword = Optional.empty();
    if (stampKeystorePath.isPresent()) {
        keystorePath = stampKeystorePath.get();
        keystorePassword = stampKeystorePassword;
    } else if (signingKeystorePath.isPresent()) {
        keystorePath = signingKeystorePath.get();
        keystorePassword = signingKeystorePassword;
    }
    if (keystorePath == null) {
        // Try to use debug keystore if present.
        Optional<SigningConfiguration> debugConfig = DebugKeystoreUtils.getDebugSigningConfiguration(systemEnvironmentProvider);
        if (debugConfig.isPresent()) {
            out.printf("INFO: The stamp will be signed with the debug keystore found at '%s'.%n", DebugKeystoreUtils.DEBUG_KEYSTORE_CACHE.getUnchecked(systemEnvironmentProvider).get());
            return debugConfig.get();
        } else {
            throw InvalidCommandException.builder().withInternalMessage("No key was found to sign the stamp.").build();
        }
    }
    String keyAlias = null;
    Optional<Password> keyPassword = Optional.empty();
    if (stampKeyAlias.isPresent()) {
        keyAlias = stampKeyAlias.get();
        keyPassword = stampKeyPassword;
    } else if (signingKeyAlias.isPresent()) {
        keyAlias = signingKeyAlias.get();
        keyPassword = signingKeyPassword;
    }
    if (keyAlias == null) {
        throw InvalidCommandException.builder().withInternalMessage("Flag --stamp-key-alias or --ks-key-alias are required when --stamp-ks or --ks are" + " set.").build();
    }
    return SigningConfiguration.extractFromKeystore(keystorePath, keyAlias, keystorePassword, keyPassword);
}
Also used : Path(java.nio.file.Path) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) Password(com.android.tools.build.bundletool.model.Password)

Example 2 with SigningConfiguration

use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.

the class BuildSdkApksCommand method populateSigningConfigurationFromFlags.

private static void populateSigningConfigurationFromFlags(Builder buildSdkApksCommand, ParsedFlags flags, PrintStream out, SystemEnvironmentProvider provider) {
    // Signing-related arguments.
    Optional<Path> keystorePath = KEYSTORE_FLAG.getValue(flags);
    Optional<String> keyAlias = KEY_ALIAS_FLAG.getValue(flags);
    Optional<Password> keystorePassword = KEYSTORE_PASSWORD_FLAG.getValue(flags);
    Optional<Password> keyPassword = KEY_PASSWORD_FLAG.getValue(flags);
    if (keystorePath.isPresent() && keyAlias.isPresent()) {
        SignerConfig signerConfig = SignerConfig.extractFromKeystore(keystorePath.get(), keyAlias.get(), keystorePassword, keyPassword);
        SigningConfiguration.Builder builder = SigningConfiguration.builder().setSignerConfig(signerConfig);
        buildSdkApksCommand.setSigningConfiguration(builder.build());
    } else if (keystorePath.isPresent() && !keyAlias.isPresent()) {
        throw InvalidCommandException.builder().withInternalMessage("Flag --ks-key-alias is required when --ks is set.").build();
    } else if (!keystorePath.isPresent() && keyAlias.isPresent()) {
        throw InvalidCommandException.builder().withInternalMessage("Flag --ks is required when --ks-key-alias is set.").build();
    } else {
        // Try to use debug keystore if present.
        Optional<SigningConfiguration> debugConfig = DebugKeystoreUtils.getDebugSigningConfiguration(provider);
        if (debugConfig.isPresent()) {
            out.printf("INFO: The APKs will be signed with the debug keystore found at '%s'.%n", DebugKeystoreUtils.DEBUG_KEYSTORE_CACHE.getUnchecked(provider).get());
            buildSdkApksCommand.setSigningConfiguration(debugConfig.get());
        } else {
            out.println("WARNING: The APKs won't be signed and thus not installable unless you also pass a " + "keystore via the flag --ks. See the command help for more information.");
        }
    }
}
Also used : Path(java.nio.file.Path) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) SignerConfig(com.android.tools.build.bundletool.model.SignerConfig) Password(com.android.tools.build.bundletool.model.Password)

Example 3 with SigningConfiguration

use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.

the class BuildApksCommandTest method populateLineage_apkFile.

@Test
public void populateLineage_apkFile() throws Exception {
    SigningCertificateLineage.SignerConfig signerConfig = new SigningCertificateLineage.SignerConfig.Builder(privateKey, certificate).build();
    SigningCertificateLineage.SignerConfig oldestSignerConfig = new SigningCertificateLineage.SignerConfig.Builder(oldestSignerPrivateKey, oldestSignerCertificate).build();
    SigningCertificateLineage lineage = new SigningCertificateLineage.Builder(oldestSignerConfig, signerConfig).build();
    com.android.tools.build.bundletool.model.SignerConfig oldestSigner = com.android.tools.build.bundletool.model.SignerConfig.builder().setPrivateKey(oldestSignerPrivateKey).setCertificates(ImmutableList.of(oldestSignerCertificate)).build();
    TestComponent.useTestModule(this, TestModule.builder().withSigningConfig(SigningConfiguration.builder().setSignerConfig(privateKey, certificate).setSigningCertificateLineage(lineage).setOldestSigner(oldestSigner).build()).build());
    File lineageFile = createMinimalistSignedApkFile().toFile();
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    BuildApksCommand commandViaFlags = BuildApksCommand.fromFlags(new FlagParser().parse("--bundle=" + bundlePath, "--output=" + outputFilePath, "--aapt2=" + AAPT2_PATH, "--ks=" + keystorePath, "--ks-key-alias=" + KEY_ALIAS, "--ks-pass=pass:" + KEYSTORE_PASSWORD, "--key-pass=pass:" + KEY_PASSWORD, "--lineage=" + lineageFile, "--oldest-signer=" + oldestSignerPropertiesPath), new PrintStream(output), systemEnvironmentProvider, fakeAdbServer);
    SigningConfiguration signingConfiguration = commandViaFlags.getSigningConfiguration().get();
    assertThat(signingConfiguration.getSigningCertificateLineage().get().getCertificatesInLineage()).containsExactly(oldestSignerCertificate, certificate);
    assertThat(signingConfiguration.getOldestSigner().get().getPrivateKey()).isEqualTo(oldestSignerConfig.getPrivateKey());
    assertThat(signingConfiguration.getOldestSigner().get().getCertificates()).containsExactly(oldestSignerConfig.getCertificate());
}
Also used : PrintStream(java.io.PrintStream) ApksigSigningConfiguration(com.android.tools.build.bundletool.model.ApksigSigningConfiguration) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SigningCertificateLineage(com.android.apksig.SigningCertificateLineage) SignerConfig(com.android.tools.build.bundletool.model.SignerConfig) SignerConfig(com.android.tools.build.bundletool.model.SignerConfig) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) DeviceFactory.createDeviceSpecFile(com.android.tools.build.bundletool.testing.DeviceFactory.createDeviceSpecFile) File(java.io.File) CodeRelatedFile(com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile) Test(org.junit.Test)

Example 4 with SigningConfiguration

use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.

the class BuildApksCommandTest method buildingViaFlagsAndBuilderHasSameResult_stamp_separateKeystore.

@Test
public void buildingViaFlagsAndBuilderHasSameResult_stamp_separateKeystore() throws Exception {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    BuildApksCommand commandViaFlags = BuildApksCommand.fromFlags(new FlagParser().parse("--bundle=" + bundlePath, "--output=" + outputFilePath, "--aapt2=" + AAPT2_PATH, "--ks=" + keystorePath, "--ks-key-alias=" + KEY_ALIAS, "--ks-pass=pass:" + KEYSTORE_PASSWORD, "--key-pass=pass:" + KEY_PASSWORD, "--create-stamp=" + true, "--stamp-ks=" + stampKeystorePath, "--stamp-ks-pass=pass:" + STAMP_KEYSTORE_PASSWORD), new PrintStream(output), systemEnvironmentProvider, fakeAdbServer);
    SigningConfiguration signingConfiguration = SigningConfiguration.builder().setSignerConfig(privateKey, certificate).build();
    SigningConfiguration stampSigningConfiguration = SigningConfiguration.builder().setSignerConfig(stampPrivateKey, stampCertificate).build();
    BuildApksCommand.Builder commandViaBuilder = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setSigningConfiguration(signingConfiguration).setSourceStamp(SourceStamp.builder().setSigningConfiguration(stampSigningConfiguration).build()).setAapt2Command(commandViaFlags.getAapt2Command().get()).setExecutorServiceInternal(commandViaFlags.getExecutorService()).setExecutorServiceCreatedByBundleTool(true).setOutputPrintStream(commandViaFlags.getOutputPrintStream().get());
    DebugKeystoreUtils.getDebugSigningConfiguration(systemEnvironmentProvider).ifPresent(commandViaBuilder::setSigningConfiguration);
    assertThat(commandViaBuilder.build().getSourceStamp()).isEqualTo(commandViaFlags.getSourceStamp());
    assertThat(commandViaBuilder.build().getSigningConfiguration()).isEqualTo(commandViaFlags.getSigningConfiguration());
}
Also used : PrintStream(java.io.PrintStream) ApksigSigningConfiguration(com.android.tools.build.bundletool.model.ApksigSigningConfiguration) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) ByteArrayOutputStream(java.io.ByteArrayOutputStream) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) Test(org.junit.Test)

Example 5 with SigningConfiguration

use of com.android.tools.build.bundletool.model.SigningConfiguration in project bundletool by google.

the class BuildApksCommandTest method buildingViaFlagsAndBuilderHasSameResult_stamp_source.

@Test
public void buildingViaFlagsAndBuilderHasSameResult_stamp_source() throws Exception {
    BuildApksCommand commandViaFlags = BuildApksCommand.fromFlags(new FlagParser().parse("--bundle=" + bundlePath, "--output=" + outputFilePath, "--aapt2=" + AAPT2_PATH, // Optional values.
    "--ks=" + keystorePath, "--ks-key-alias=" + KEY_ALIAS, "--ks-pass=pass:" + KEYSTORE_PASSWORD, "--key-pass=pass:" + KEY_PASSWORD, "--create-stamp=" + true, "--stamp-source=" + STAMP_SOURCE), fakeAdbServer);
    SigningConfiguration signingConfiguration = SigningConfiguration.builder().setSignerConfig(privateKey, certificate).build();
    BuildApksCommand commandViaBuilder = BuildApksCommand.builder().setBundlePath(bundlePath).setOutputFile(outputFilePath).setSigningConfiguration(signingConfiguration).setSourceStamp(SourceStamp.builder().setSigningConfiguration(signingConfiguration).setSource(STAMP_SOURCE).build()).setAapt2Command(commandViaFlags.getAapt2Command().get()).setExecutorServiceInternal(commandViaFlags.getExecutorService()).setExecutorServiceCreatedByBundleTool(true).setOutputPrintStream(commandViaFlags.getOutputPrintStream().get()).build();
    assertThat(commandViaBuilder.getSourceStamp()).isEqualTo(commandViaFlags.getSourceStamp());
    assertThat(commandViaBuilder.getSigningConfiguration()).isEqualTo(commandViaFlags.getSigningConfiguration());
}
Also used : ApksigSigningConfiguration(com.android.tools.build.bundletool.model.ApksigSigningConfiguration) SigningConfiguration(com.android.tools.build.bundletool.model.SigningConfiguration) FlagParser(com.android.tools.build.bundletool.flags.FlagParser) Test(org.junit.Test)

Aggregations

SigningConfiguration (com.android.tools.build.bundletool.model.SigningConfiguration)17 ApksigSigningConfiguration (com.android.tools.build.bundletool.model.ApksigSigningConfiguration)12 Test (org.junit.Test)12 FlagParser (com.android.tools.build.bundletool.flags.FlagParser)10 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 PrintStream (java.io.PrintStream)8 Path (java.nio.file.Path)5 SignerConfig (com.android.tools.build.bundletool.model.SignerConfig)4 CodeRelatedFile (com.android.bundle.CodeTransparencyOuterClass.CodeRelatedFile)3 Password (com.android.tools.build.bundletool.model.Password)3 File (java.io.File)3 SigningCertificateLineage (com.android.apksig.SigningCertificateLineage)2 ZipPath (com.android.tools.build.bundletool.model.ZipPath)2 DeviceFactory.createDeviceSpecFile (com.android.tools.build.bundletool.testing.DeviceFactory.createDeviceSpecFile)2 ApkVerifier (com.android.apksig.ApkVerifier)1 ApkFormatException (com.android.apksig.apk.ApkFormatException)1 ApkDescription (com.android.bundle.Commands.ApkDescription)1 ApkSet (com.android.bundle.Commands.ApkSet)1 BuildApksResult (com.android.bundle.Commands.BuildApksResult)1 Variant (com.android.bundle.Commands.Variant)1