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..450d627c14dd57eeed064287e54e1b47475fa610 |
--- /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 |
+import os |
+import subprocess |
+import sys |
+import tempfile |
+ |
+CURPATH = os.path.dirname(os.path.realpath(__file__)) |
+ADB = os.path.join(CURPATH, os.pardir, os.pardir, 'tools', 'adb') |
jbudorick
2015/03/05 05:15:02
O_O
What's this script's intended lifetime? We've
Vadim Sh.
2015/03/05 05:20:26
Committing binary into the repository is not a goo
jbudorick
2015/03/05 05:30:03
adb is part of the android SDK, which gets pulled
navabi
2015/03/05 05:30:20
I checked and we don't have an adb binary in /usr/
navabi
2015/03/05 05:35:34
Yes, we can point to the ADB in the chromium check
jbudorick
2015/03/05 05:37:37
Can we not just use the one from the checkout?
jbudorick
2015/03/05 05:38:53
didn't see this before my last response, oops.
|
+ADB_KEYS_PATH = '/data/misc/adb/adb_keys' |
+ |
+ |
+def GetCmdOutput(cmd, 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, 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) |
+ f.write('\n'.join(public_keys_set)) |
+ f.close() |
+ GetCmdOutput(['cat', f.name], env) |
navabi
2015/03/05 05:14:45
this was for debugging. remove.
|
+ GetCmdOutput([ADB, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) |
+ os.unlink(f.name) |
+ os.path.exists(f.name) |
+ |
+ |
+def GetUnauthorizedDevices(): |
+ output = GetCmdOutput([ADB, 'devices']).splitlines() |
+ unauthorized_devices = [line.split('\t')[0] for line in output |
+ if '\tunauthorized' in line] |
+ return unauthorized_devices |
+ |
+ |
+def main(argv): |
+ parser = optparse.OptionParser() |
+ 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) |
+ 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() |
+ |
+ # Kill server and relaunch with ADB_VENDOR_KEYS |
+ env = {'ADB_VENDOR_KEYS': ':'.join(private_key_files)} \ |
+ if private_key_files else {} |
+ GetCmdOutput([ADB, 'kill-server']).splitlines() |
+ for device in unauthorized_devices: |
+ PushHostPubkey(device, env, public_keys_set) |
jbudorick
2015/03/05 05:30:03
Also, does this even work? If they're unauthorized
navabi
2015/03/05 05:35:34
The idea is that all the devices will have the chr
|
+ |
+ |
+if __name__ == '__main__': |
+ sys.exit(main(sys.argv)) |