Index: scripts/slave/android/authorize_adb_devices.py |
diff --git a/scripts/slave/android/authorize_adb_devices.py b/scripts/slave/android/authorize_adb_devices.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..fee3667ce33bde51d90a3b76117ede4e9be9f5b5 |
--- /dev/null |
+++ b/scripts/slave/android/authorize_adb_devices.py |
@@ -0,0 +1,85 @@ |
+#!/usr/bin/env python |
+# Copyright 2015 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+"""Push host key onto Android devices using public userdebug key. |
+ |
+If the device has the chrome-infrastructure private adb key on it, use |
+ADB_VENDOR_KEYS to push the host-specific private key onto the devices that are |
+not authorized. |
+""" |
+ |
+import argparse |
+import os |
+import subprocess |
+import sys |
+import tempfile |
+ |
+ADB_KEYS_PATH = '/data/misc/adb/adb_keys' |
+ |
+ |
+def GetCmdOutput(cmd, env=None): |
+ env = {} if not env else env |
+ env['HOME'] = os.environ['HOME'] |
+ stdout, stderr = subprocess.Popen(cmd, stdout=subprocess.PIPE, |
+ stderr=subprocess.PIPE, |
+ env=env).communicate() |
+ output = stdout + stderr |
+ return output |
+ |
+ |
+def PushHostPubkey(device, adb, env, public_keys_set): |
+ GetCmdOutput([adb, 'devices'], env) |
+ GetCmdOutput([adb, '-s', device, 'root'], env) |
+ adb_ls_output = GetCmdOutput([adb, '-s', device, 'shell', 'ls', ADB_KEYS_PATH], |
+ env) |
+ # If adb_keys file exists on device add its contents to the public keys set. |
+ if adb_ls_output == ADB_KEYS_PATH: |
+ dev_keys = GetCmdOutput([adb, '-s', device, 'shell', 'cat', ADB_KEYS_PATH], |
+ env).splitlines() |
+ public_keys_set.update(dev_keys) |
+ |
+ with tempfile.NamedTemporaryFile() as f: |
+ f.write('\n'.join(public_keys_set)) |
+ f.flush() |
+ GetCmdOutput([adb, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) |
+ |
+ |
+def GetUnauthorizedDevices(adb): |
+ output = GetCmdOutput([adb, 'devices']).splitlines() |
+ return [line.split('\t')[0] for line in output if '\tunauthorized' in line] |
+ |
+ |
+def main(argv): |
+ parser = argparse.ArgumentParser() |
+ parser.add_argument('--adb-path', help='Path to adb binary.', default='adb') |
+ parser.add_argument('--adb-keys-dir', |
+ help='Point to directory that contains adb keys.', |
+ default=os.path.join(os.environ['HOME'], '.android')) |
+ options = parser.parse_args() |
+ dir_contents = os.listdir(options.adb_keys_dir) |
+ adb_path = options.adb_path |
+ private_key_files = [os.path.join(options.adb_keys_dir, key) |
+ for key in dir_contents if key.endswith('adbkey')] |
+ public_key_files = [os.path.join(options.adb_keys_dir, key) |
+ for key in dir_contents if key.endswith('adbkey.pub')] |
+ public_keys_set = set() |
+ for public_key_file in public_key_files: |
+ with open(public_key_file, 'r') as f: |
+ public_keys_set.add(f.read().strip()) |
+ |
+ # Kill server and find unauthorized devices without ADB_VENDOR_KEYS |
+ GetCmdOutput([adb_path, 'kill-server'], env={}).splitlines() |
+ unauthorized_devices = GetUnauthorizedDevices(adb_path) |
+ |
+ # Kill server launched with ADB_VENDOR_KEYS |
+ GetCmdOutput([adb_path, 'kill-server']).splitlines() |
+ env = ({'ADB_VENDOR_KEYS': ':'.join(private_key_files)} if private_key_files |
+ else {}) |
+ for device in unauthorized_devices: |
+ PushHostPubkey(device, adb_path, env, public_keys_set) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |