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..8a81b84e7c163baecc6bd7aa36c2e03649d2731f |
--- /dev/null |
+++ b/scripts/slave/android/authorize_adb_devices.py |
@@ -0,0 +1,88 @@ |
+#!/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 optparse |
jbudorick
2015/03/05 20:21:30
I know we use this in several places, but it is de
jbudorick
2015/03/05 20:21:30
I know we use this in a variety of places, but thi
jbudorick
2015/03/05 20:22:41
I swear this only showed up once while I was writi
navabi
2015/03/05 23:05:59
Done and done :).
|
+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) |
+ f = tempfile.NamedTemporaryFile(delete=False) |
jbudorick
2015/03/05 20:21:30
why delete = false? can't this section just be
wi
navabi
2015/03/05 23:05:59
Done.
|
+ f.write('\n'.join(public_keys_set)) |
+ f.close() |
+ GetCmdOutput([adb, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) |
+ os.unlink(f.name) |
jbudorick
2015/03/05 20:21:30
What's up with these two lines?
navabi
2015/03/05 23:05:59
Not needed if we use with. Changed to use with.
|
+ os.path.exists(f.name) |
+ |
+ |
+def GetUnauthorizedDevices(adb): |
+ output = GetCmdOutput([adb, 'devices']).splitlines() |
+ unauthorized_devices = [line.split('\t')[0] for line in output |
jbudorick
2015/03/05 20:21:30
nit: could just return the list instead of assigni
navabi
2015/03/05 23:05:59
Done.
|
+ if '\tunauthorized' in line] |
+ return unauthorized_devices |
+ |
+ |
+def main(argv): |
+ parser = optparse.OptionParser() |
+ parser.add_option('--adb', help='Path to adb binary.', default='adb') |
jbudorick
2015/03/05 20:21:30
nit: I think something like '--adb-path' would be
navabi
2015/03/05 23:05:59
Done.
|
+ parser.add_option('--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 = options.adb if options.adb else 'adb' |
+ 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, 'kill-server'], env={}).splitlines() |
+ unauthorized_devices = GetUnauthorizedDevices(adb) |
+ |
+ # Kill server launched with ADB_VENDOR_KEYS |
+ GetCmdOutput([adb, 'kill-server']).splitlines() |
+ env = {'ADB_VENDOR_KEYS': ':'.join(private_key_files)} \ |
jbudorick
2015/03/05 20:21:30
I'll take it on faith that this works, then; I hav
navabi
2015/03/05 23:05:59
Acknowledged.
|
+ if private_key_files else {} |
+ for device in unauthorized_devices: |
+ PushHostPubkey(device, adb, env, public_keys_set) |
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |