OLD | NEW |
---|---|
(Empty) | |
1 #!/usr/bin/env python | |
2 # Copyright 2015 The Chromium Authors. All rights reserved. | |
3 # Use of this source code is governed by a BSD-style license that can be | |
4 # found in the LICENSE file. | |
5 | |
6 """Push host key onto Android devices using public userdebug key. | |
7 | |
8 If the device has the chrome-infrastructure private adb key on it, use | |
9 ADB_VENDOR_KEYS to push the host-specific private key onto the devices that are | |
10 not authorized. | |
11 | |
12 """ | |
13 import optparse | |
14 import os | |
15 import subprocess | |
16 import sys | |
17 import tempfile | |
18 | |
19 CURPATH = os.path.dirname(os.path.realpath(__file__)) | |
20 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.
| |
21 ADB_KEYS_PATH = '/data/misc/adb/adb_keys' | |
22 | |
23 | |
24 def GetCmdOutput(cmd, env={}): | |
25 env['HOME'] = os.environ['HOME'] | |
26 stdout, stderr = subprocess.Popen(cmd, stdout=subprocess.PIPE, | |
27 stderr=subprocess.PIPE, | |
28 env=env).communicate() | |
29 output = stdout + stderr | |
30 return output | |
31 | |
32 | |
33 def PushHostPubkey(device, env, public_keys_set): | |
34 GetCmdOutput([ADB, 'devices'], env) | |
35 GetCmdOutput([ADB, '-s', device, 'root'], env) | |
36 adb_ls_output = GetCmdOutput([ADB, '-s', device, 'shell', 'ls', ADB_KEYS_PATH], | |
37 env) | |
38 # If adb_keys file exists on device add its contents to the public keys set. | |
39 if adb_ls_output == ADB_KEYS_PATH: | |
40 dev_keys = GetCmdOutput([ADB, '-s', device, 'shell', 'cat', ADB_KEYS_PATH], | |
41 env).splitlines() | |
42 public_keys_set.update(dev_keys) | |
43 f = tempfile.NamedTemporaryFile(delete=False) | |
44 f.write('\n'.join(public_keys_set)) | |
45 f.close() | |
46 GetCmdOutput(['cat', f.name], env) | |
navabi
2015/03/05 05:14:45
this was for debugging. remove.
| |
47 GetCmdOutput([ADB, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) | |
48 os.unlink(f.name) | |
49 os.path.exists(f.name) | |
50 | |
51 | |
52 def GetUnauthorizedDevices(): | |
53 output = GetCmdOutput([ADB, 'devices']).splitlines() | |
54 unauthorized_devices = [line.split('\t')[0] for line in output | |
55 if '\tunauthorized' in line] | |
56 return unauthorized_devices | |
57 | |
58 | |
59 def main(argv): | |
60 parser = optparse.OptionParser() | |
61 parser.add_option('--adb-keys-dir', | |
62 help='Point to directory that contains adb keys.', | |
63 default=os.path.join(os.environ['HOME'], '.android')) | |
64 options, _ = parser.parse_args() | |
65 dir_contents = os.listdir(options.adb_keys_dir) | |
66 private_key_files = [os.path.join(options.adb_keys_dir, key) | |
67 for key in dir_contents if key.endswith('adbkey')] | |
68 public_key_files = [os.path.join(options.adb_keys_dir, key) | |
69 for key in dir_contents if key.endswith('adbkey.pub')] | |
70 public_keys_set = set() | |
71 for public_key_file in public_key_files: | |
72 with open(public_key_file, 'r') as f: | |
73 public_keys_set.add(f.read().strip()) | |
74 | |
75 # Kill server and find unauthorized devices without ADB_VENDOR_KEYS | |
76 GetCmdOutput([ADB, 'kill-server'], env={}).splitlines() | |
77 unauthorized_devices = GetUnauthorizedDevices() | |
78 | |
79 # Kill server and relaunch with ADB_VENDOR_KEYS | |
80 env = {'ADB_VENDOR_KEYS': ':'.join(private_key_files)} \ | |
81 if private_key_files else {} | |
82 GetCmdOutput([ADB, 'kill-server']).splitlines() | |
83 for device in unauthorized_devices: | |
84 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
| |
85 | |
86 | |
87 if __name__ == '__main__': | |
88 sys.exit(main(sys.argv)) | |
OLD | NEW |