Skip to content

Refactor out cached ApkHashExtractor #3767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.firebase.appdistribution.impl;

import static com.google.firebase.appdistribution.impl.ReleaseIdentificationUtils.getPackageInfoWithMetadata;

import android.content.Context;
import android.content.pm.PackageInfo;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.firebase.appdistribution.FirebaseAppDistributionException;
import com.google.firebase.appdistribution.FirebaseAppDistributionException.Status;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/** Extracts a hash of the installed APK. */
class ApkHashExtractor {

private static final String TAG = "ApkHashExtractor";
private static final int BYTES_IN_LONG = 8;

private final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();
private Context applicationContext;

ApkHashExtractor(Context applicationContext) {
this.applicationContext = applicationContext;
}

/**
* Extract the SHA-256 hash of the installed APK.
*
* <p>The result is stored in an in-memory cache to avoid computing it repeatedly.
*/
String extractApkHash() throws FirebaseAppDistributionException {
PackageInfo metadataPackageInfo = getPackageInfoWithMetadata(applicationContext);
String installedReleaseApkHash = extractApkHash(metadataPackageInfo);
if (installedReleaseApkHash == null || installedReleaseApkHash.isEmpty()) {
throw new FirebaseAppDistributionException(
"Could not calculate hash of installed APK", Status.UNKNOWN);
}
return installedReleaseApkHash;
}

private String extractApkHash(PackageInfo packageInfo) {
File sourceFile = new File(packageInfo.applicationInfo.sourceDir);

String key =
String.format(
Locale.ENGLISH, "%s.%d", sourceFile.getAbsolutePath(), sourceFile.lastModified());
if (!cachedApkHashes.containsKey(key)) {
cachedApkHashes.put(key, calculateApkHash(sourceFile));
}
return cachedApkHashes.get(key);
}

@Nullable
String calculateApkHash(@NonNull File file) {
LogWrapper.getInstance()
.v(
TAG,
String.format(
"Calculating release id for %s (%d bytes)", file.getPath(), file.length()));

long start = System.currentTimeMillis();
long entries = 0;
String zipFingerprint = null;
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
ArrayList<Byte> checksums = new ArrayList<>();

// Since calculating the codeHash returned from the release backend is computationally
// expensive, we has the existing checksum data from the ZipFile and compare it to
// (1) the apk hash returned by the backend, or (2) look up a mapping from the apk zip hash to
// the full codehash, and compare that to the codehash to the backend
ZipFile zis = new ZipFile(file);
try {
Enumeration<? extends ZipEntry> zipEntries = zis.entries();
while (zipEntries.hasMoreElements()) {
ZipEntry zip = zipEntries.nextElement();
entries += 1;
byte[] crcBytes = longToByteArray(zip.getCrc());
for (byte b : crcBytes) {
checksums.add(b);
}
}
} finally {
zis.close();
}
byte[] checksumByteArray = digest.digest(arrayListToByteArray(checksums));
StringBuilder sb = new StringBuilder();
for (byte b : checksumByteArray) {
sb.append(String.format("%02x", b));
}
zipFingerprint = sb.toString();

} catch (IOException | NoSuchAlgorithmException e) {
LogWrapper.getInstance().v(TAG, "id calculation failed for " + file.getPath());
return null;
} finally {
long elapsed = System.currentTimeMillis() - start;
LogWrapper.getInstance()
.v(
TAG,
String.format(
"Computed hash of %s (%d entries, %d ms elapsed): %s",
file.getPath(), entries, elapsed, zipFingerprint));
}

return zipFingerprint;
}

private static byte[] longToByteArray(long x) {
ByteBuffer buffer = ByteBuffer.allocate(BYTES_IN_LONG);
buffer.putLong(x);
return buffer.array();
}

private static byte[] arrayListToByteArray(ArrayList<Byte> list) {
byte[] result = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
result[i] = list.get(i);
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
import com.google.firebase.appdistribution.UpdateProgress;
import com.google.firebase.appdistribution.UpdateStatus;
import com.google.firebase.appdistribution.UpdateTask;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;

/**
* This class is the "real" implementation of the Firebase App Distribution API which should only be
Expand Down Expand Up @@ -101,34 +99,6 @@ class FirebaseAppDistributionImpl implements FirebaseAppDistribution {
lifecycleNotifier.addOnActivityResumedListener(this::onActivityResumed);
}

FirebaseAppDistributionImpl(
@NonNull FirebaseApp firebaseApp,
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider,
@NonNull SignInStorage signInStorage,
@NonNull FirebaseAppDistributionLifecycleNotifier lifecycleNotifier) {
this(
firebaseApp,
new TesterSignInManager(firebaseApp, firebaseInstallationsApiProvider, signInStorage),
new NewReleaseFetcher(
firebaseApp,
new FirebaseAppDistributionTesterApiClient(),
firebaseInstallationsApiProvider),
new ApkUpdater(firebaseApp, new ApkInstaller()),
new AabUpdater(),
signInStorage,
lifecycleNotifier);
}

FirebaseAppDistributionImpl(
@NonNull FirebaseApp firebaseApp,
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider) {
this(
firebaseApp,
firebaseInstallationsApiProvider,
new SignInStorage(firebaseApp.getApplicationContext()),
FirebaseAppDistributionLifecycleNotifier.getInstance());
}

@Override
@NonNull
public UpdateTask updateIfNewReleaseAvailable() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.google.firebase.components.ComponentContainer;
import com.google.firebase.components.ComponentRegistrar;
import com.google.firebase.components.Dependency;
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.platforminfo.LibraryVersionComponent;
import java.util.Arrays;
Expand Down Expand Up @@ -55,13 +56,26 @@ public class FirebaseAppDistributionRegistrar implements ComponentRegistrar {

private FirebaseAppDistribution buildFirebaseAppDistribution(ComponentContainer container) {
FirebaseApp firebaseApp = container.get(FirebaseApp.class);
FirebaseAppDistribution appDistribution =
new FirebaseAppDistributionImpl(
firebaseApp, container.getProvider(FirebaseInstallationsApi.class));
Context context = firebaseApp.getApplicationContext();
Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider =
container.getProvider(FirebaseInstallationsApi.class);
SignInStorage signInStorage = new SignInStorage(context);
FirebaseAppDistributionTesterApiClient testerApiClient =
new FirebaseAppDistributionTesterApiClient();
FirebaseAppDistributionLifecycleNotifier lifecycleNotifier =
FirebaseAppDistributionLifecycleNotifier.getInstance();
ApkHashExtractor apkHashExtractor = new ApkHashExtractor(firebaseApp.getApplicationContext());
FirebaseAppDistribution appDistribution =
new FirebaseAppDistributionImpl(
firebaseApp,
new TesterSignInManager(firebaseApp, firebaseInstallationsApiProvider, signInStorage),
new NewReleaseFetcher(
firebaseApp, testerApiClient, firebaseInstallationsApiProvider, apkHashExtractor),
new ApkUpdater(firebaseApp, new ApkInstaller()),
new AabUpdater(),
signInStorage,
lifecycleNotifier);

Context context = firebaseApp.getApplicationContext();
if (context instanceof Application) {
Application firebaseApplication = (Application) context;
firebaseApplication.registerActivityLifecycleCallbacks(lifecycleNotifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,42 @@ void d(@NonNull String msg) {
Log.d(LOG_TAG, msg);
}

void d(@NonNull String additionalTag, @NonNull String msg) {
Log.d(LOG_TAG, prependTag(additionalTag, msg));
}

void v(@NonNull String msg) {
Log.v(LOG_TAG, msg);
}

void v(@NonNull String additionalTag, @NonNull String msg) {
Log.v(LOG_TAG, prependTag(additionalTag, msg));
}

void i(@NonNull String msg) {
Log.i(LOG_TAG, msg);
}

void i(@NonNull String additionalTag, @NonNull String msg) {
Log.i(LOG_TAG, prependTag(additionalTag, msg));
}

void w(@NonNull String msg) {
Log.w(LOG_TAG, msg);
}

void w(@NonNull String additionalTag, @NonNull String msg) {
Log.w(LOG_TAG, prependTag(additionalTag, msg));
}

void w(@NonNull String msg, @NonNull Throwable tr) {
Log.w(LOG_TAG, msg, tr);
}

void w(@NonNull String additionalTag, @NonNull String msg, @NonNull Throwable tr) {
Log.w(LOG_TAG, prependTag(additionalTag, msg), tr);
}

void e(@NonNull String msg) {
Log.e(LOG_TAG, msg);
}
Expand All @@ -60,5 +80,13 @@ void e(@NonNull String msg, @NonNull Throwable tr) {
Log.e(LOG_TAG, msg, tr);
}

void e(@NonNull String additionalTag, @NonNull String msg, @NonNull Throwable tr) {
Log.e(LOG_TAG, prependTag(additionalTag, msg), tr);
}

private String prependTag(String tag, String msg) {
return String.format("%s: %s", tag, msg);
}

private LogWrapper() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@

package com.google.firebase.appdistribution.impl;

import static com.google.firebase.appdistribution.impl.ReleaseIdentificationUtils.calculateApkHash;
import static com.google.firebase.appdistribution.impl.ReleaseIdentificationUtils.getPackageInfo;
import static com.google.firebase.appdistribution.impl.ReleaseIdentificationUtils.getPackageInfoWithMetadata;
import static com.google.firebase.appdistribution.impl.TaskUtils.runAsyncInTask;

import android.content.Context;
import android.content.pm.PackageInfo;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
Expand All @@ -34,8 +31,6 @@
import com.google.firebase.inject.Provider;
import com.google.firebase.installations.FirebaseInstallationsApi;
import com.google.firebase.installations.InstallationTokenResult;
import java.io.File;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Executor;
Expand All @@ -51,6 +46,7 @@ class NewReleaseFetcher {
private final FirebaseApp firebaseApp;
private final FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient;
private final Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider;
private final ApkHashExtractor apkHashExtractor;
private final Context context;
// Maintain an in-memory mapping from source file to APK hash to avoid re-calculating the hash
private static final ConcurrentMap<String, String> cachedApkHashes = new ConcurrentHashMap<>();
Expand All @@ -61,22 +57,26 @@ class NewReleaseFetcher {
NewReleaseFetcher(
@NonNull FirebaseApp firebaseApp,
@NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient,
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider) {
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider,
ApkHashExtractor apkHashExtractor) {
this(
firebaseApp,
firebaseAppDistributionTesterApiClient,
firebaseInstallationsApiProvider,
apkHashExtractor,
Executors.newSingleThreadExecutor());
}

NewReleaseFetcher(
@NonNull FirebaseApp firebaseApp,
@NonNull FirebaseAppDistributionTesterApiClient firebaseAppDistributionTesterApiClient,
@NonNull Provider<FirebaseInstallationsApi> firebaseInstallationsApiProvider,
ApkHashExtractor apkHashExtractor,
@NonNull Executor executor) {
this.firebaseApp = firebaseApp;
this.firebaseAppDistributionTesterApiClient = firebaseAppDistributionTesterApiClient;
this.firebaseInstallationsApiProvider = firebaseInstallationsApiProvider;
this.apkHashExtractor = apkHashExtractor;
this.taskExecutor = executor;
this.context = firebaseApp.getApplicationContext();
}
Expand Down Expand Up @@ -206,29 +206,10 @@ private String getInstalledAppVersionName(Context context)
return getPackageInfo(context).versionName;
}

@VisibleForTesting
String extractApkHash(PackageInfo packageInfo) {
File sourceFile = new File(packageInfo.applicationInfo.sourceDir);

String key =
String.format(
Locale.ENGLISH, "%s.%d", sourceFile.getAbsolutePath(), sourceFile.lastModified());
if (!cachedApkHashes.containsKey(key)) {
cachedApkHashes.put(key, calculateApkHash(sourceFile));
}
return cachedApkHashes.get(key);
}

private boolean hasSameHashAsInstalledRelease(AppDistributionReleaseInternal newRelease)
throws FirebaseAppDistributionException {
Context context = firebaseApp.getApplicationContext();
PackageInfo metadataPackageInfo = getPackageInfoWithMetadata(context);
String installedReleaseApkHash = extractApkHash(metadataPackageInfo);

if (installedReleaseApkHash == null || installedReleaseApkHash.isEmpty()) {
throw new FirebaseAppDistributionException(
"Could not calculate hash of installed APK", Status.UNKNOWN);
} else if (newRelease.getApkHash().isEmpty()) {
String installedReleaseApkHash = apkHashExtractor.extractApkHash();
if (newRelease.getApkHash().isEmpty()) {
throw new FirebaseAppDistributionException(
"Missing APK hash from new release", Status.UNKNOWN);
}
Expand Down
Loading