Skip to content
Open
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
Expand Up @@ -715,23 +715,24 @@ public String runShellWithRetry(
}

/**
* Runs an adb shell command with retry, piping {@code stdinInput} as a single sensitive line to
* standard input and closing stdin immediately afterward so sensitive values do not appear in
* command arguments or stdin failure logs.
* Runs an adb shell command, piping {@code stdinInput} as a single sensitive line to standard
* input and closing stdin immediately afterward so sensitive values do not appear in command
* arguments or stdin failure logs.
*
* <p>The command length must not exceed 4090 UTF-8 bytes (script file fallback is not supported),
* and {@code stdinInput} must not contain newline characters.
*
* @param serial device serial number
* @param command the shell command
* @param stdinInput single-line sensitive input piped to the shell command's standard input
* @param timeout max execution time, or null for default timeout
* @return command std output
* @throws MobileHarnessException if fails to execute the command or timeout
* @throws InterruptedException if the thread executing the command is interrupted
*/
@CanIgnoreReturnValue
public String runShellWithSensitiveStdinWithRetry(
String serial, String command, String stdinInput)
public String runShellWithSensitiveStdin(
String serial, String command, String stdinInput, @Nullable Duration timeout)
throws MobileHarnessException, InterruptedException {
checkArgument(
stdinInput.indexOf('\n') < 0 && stdinInput.indexOf('\r') < 0,
Expand All @@ -748,8 +749,33 @@ public String runShellWithSensitiveStdinWithRetry(
.args("-s", serial, "shell", command)
.sensitiveInputLn(stdinInput)
.closeStdinAfterInput();
if (timeout != null) {
adbCommand = adbCommand.timeout(timeout);
}
return syncCommand(adbCommand).stdoutWithoutTrailingLineTerminator();
}

/**
* Runs an adb shell command with retry, piping {@code stdinInput} as a single sensitive line to
* standard input and closing stdin immediately afterward so sensitive values do not appear in
* command arguments or stdin failure logs.
*
* <p>The command length must not exceed 4090 UTF-8 bytes (script file fallback is not supported),
* and {@code stdinInput} must not contain newline characters.
*
* @param serial device serial number
* @param command the shell command
* @param stdinInput single-line sensitive input piped to the shell command's standard input
* @return command std output
* @throws MobileHarnessException if fails to execute the command or timeout
* @throws InterruptedException if the thread executing the command is interrupted
*/
@CanIgnoreReturnValue
public String runShellWithSensitiveStdinWithRetry(
String serial, String command, String stdinInput)
throws MobileHarnessException, InterruptedException {
return runShellCommandWithRetry(
() -> syncCommand(adbCommand).stdoutWithoutTrailingLineTerminator());
() -> runShellWithSensitiveStdin(serial, command, stdinInput, /* timeout= */ null));
}

private String runShellCommandWithRetry(MobileHarnessCallable<String> command)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public enum AndroidErrorId implements ErrorId {
ANDROID_ACCOUNT_MNGR_UTIL_WRONG_PASSWORD(102_103, ErrorType.CUSTOMER_ISSUE),
ANDROID_ACCOUNT_MNGR_UTIL_REMOVE_ACCOUNT_ERROR(102_104, ErrorType.INFRA_ISSUE),
ANDROID_ACCOUNT_MNGR_UTIL_REMOVE_ACCOUNT_BY_ACCOUNT_UTIL_ERROR(102_105, ErrorType.CUSTOMER_ISSUE),
ANDROID_ACCOUNT_MNGR_UTIL_INVALID_CREDENTIAL(102_106, ErrorType.CUSTOMER_ISSUE),

// AndroidUserUtil: 102_251 ~ 102_400
ANDROID_USER_UTIL_CREATE_WORK_PROFILE_ERROR(102_251, ErrorType.INFRA_ISSUE),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ public class AndroidAccountManagerUtil {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();

/**
* AM instrument arguments template for adding a Google account. Should fill with: email, psw,
* type, if_sync.
* ADB shell template for adding a Google account with the credential read from stdin so raw
* credentials are not exposed in command-line arguments. Should fill with: instrument command,
* email, credential type, account type, auto sync, flow.
*/
private static final String ADB_SHELL_ARGS_TEMPLATE_ADD_GOOGLE_ACCOUNT =
"-e account %s -e credentialType %d -e password %s -e accountType %s -e action add "
private static final String ADB_SHELL_TEMPLATE_ADD_GOOGLE_ACCOUNT =
"IFS= read -r account_password && %s -e account %s -e credentialType %d "
+ "-e password \"$account_password\" -e accountType %s -e action add "
+ "-e sync %s -e flow %s "
+ "-r -w com.google.wireless.qa.mobileharness.tool.android.account/.AccountManagerApp";

Expand Down Expand Up @@ -135,26 +137,33 @@ public void addGoogleAccount(
String serial, AndroidAccountSetting accountSetting, int attempts, @Nullable Integer userId)
throws MobileHarnessException, InterruptedException {
String email = accountSetting.email();
String password = accountSetting.password();
if (password.indexOf('\n') >= 0 || password.indexOf('\r') >= 0) {
throw new MobileHarnessException(
AndroidErrorId.ANDROID_ACCOUNT_MNGR_UTIL_INVALID_CREDENTIAL,
String.format(
"Failed to add Google account %s: credential must not contain newline characters.",
email));
}
String accountUtilOutput = "";
String instrumentCommand =
userId == null ? ADB_SHELL_INSTRUMENT : String.format("am instrument --user %d", userId);
// Escape special chars in psk if needed
String password = ShellUtils.shellEscape(accountSetting.password());
// Escape special chars in email if needed.
String escapedEmail = ShellUtils.shellEscape(email);
for (int i = 1; i <= attempts; i++) {
try {
accountUtilOutput =
adb.runShell(
adb.runShellWithSensitiveStdin(
serial,
instrumentCommand
+ " "
+ String.format(
ADB_SHELL_ARGS_TEMPLATE_ADD_GOOGLE_ACCOUNT,
email,
accountSetting.credentialType().ordinal() + 1,
password,
accountSetting.accountType().getName(),
accountSetting.autoSync(),
"accountutil"),
String.format(
ADB_SHELL_TEMPLATE_ADD_GOOGLE_ACCOUNT,
instrumentCommand,
escapedEmail,
accountSetting.credentialType().ordinal() + 1,
accountSetting.accountType().getName(),
accountSetting.autoSync(),
"accountutil"),
password,
Duration.ofSeconds(20));
if (accountUtilOutput.contains(OUTPUT_INSTRUMENTATION_RESULT_OK)) {
return;
Expand Down Expand Up @@ -183,18 +192,17 @@ public void addGoogleAccount(
for (int i = 1; i <= attempts; i++) {
try {
accountManagerOutput =
adb.runShell(
adb.runShellWithSensitiveStdin(
serial,
instrumentCommand
+ " "
+ String.format(
ADB_SHELL_ARGS_TEMPLATE_ADD_GOOGLE_ACCOUNT,
email,
accountSetting.credentialType().ordinal() + 1,
password,
accountSetting.accountType().getName(),
accountSetting.autoSync(),
"accountmanager"),
String.format(
ADB_SHELL_TEMPLATE_ADD_GOOGLE_ACCOUNT,
instrumentCommand,
escapedEmail,
accountSetting.credentialType().ordinal() + 1,
accountSetting.accountType().getName(),
accountSetting.autoSync(),
"accountmanager"),
password,
Duration.ofSeconds(20));
if (accountManagerOutput.contains(OUTPUT_INSTRUMENTATION_RESULT_OK)) {
logger.atInfo().log(
Expand Down Expand Up @@ -336,12 +344,13 @@ public List<String> getGoogleAccounts(String serial)
*/
private void removeGoogleAccountInternal(String serial, String email)
throws MobileHarnessException, InterruptedException {
String escapedEmail = ShellUtils.shellEscape(email);
String accountUtilOutput = "";
try {
accountUtilOutput =
adb.runShell(
serial,
String.format(ADB_SHELL_TEMPLATE_REMOVE_GOOGLE_ACCOUNT, email, "accountutil"),
String.format(ADB_SHELL_TEMPLATE_REMOVE_GOOGLE_ACCOUNT, escapedEmail, "accountutil"),
Duration.ofSeconds(20));
if (accountUtilOutput.contains(OUTPUT_INSTRUMENTATION_RESULT_OK)) {
logger.atInfo().log(
Expand All @@ -361,7 +370,8 @@ private void removeGoogleAccountInternal(String serial, String email)
accountManagerOutput =
adb.runShellWithRetry(
serial,
String.format(ADB_SHELL_TEMPLATE_REMOVE_GOOGLE_ACCOUNT, email, "accountmanager"));
String.format(
ADB_SHELL_TEMPLATE_REMOVE_GOOGLE_ACCOUNT, escapedEmail, "accountmanager"));
} catch (MobileHarnessException e) {
exception = e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ public abstract class AddAccountArgs {
/** Maximal number of attempts to add Google account on device until it succeeds. */
public abstract Optional<Integer> maxAttempts();

/**
* Whether to force installing the signed version of account helper APKs regardless of device
* build keys.
*/
public abstract Optional<Boolean> forceInstallSignedApks();

public static Builder builder() {
return new AutoValue_AddAccountArgs.Builder();
}
Expand All @@ -47,6 +53,8 @@ public abstract static class Builder {

public abstract Builder setMaxAttempts(int maxAttempts);

public abstract Builder setForceInstallSignedApks(boolean forceInstallSignedApks);

public abstract AddAccountArgs build();
}
}
Loading