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 | |
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 :).
| |
14 import os | |
15 import subprocess | |
16 import sys | |
17 import tempfile | |
18 | |
19 ADB_KEYS_PATH = '/data/misc/adb/adb_keys' | |
20 | |
21 | |
22 def GetCmdOutput(cmd, env=None): | |
23 env = {} if not env else env | |
24 env['HOME'] = os.environ['HOME'] | |
25 stdout, stderr = subprocess.Popen(cmd, stdout=subprocess.PIPE, | |
26 stderr=subprocess.PIPE, | |
27 env=env).communicate() | |
28 output = stdout + stderr | |
29 return output | |
30 | |
31 | |
32 def PushHostPubkey(device, adb, env, public_keys_set): | |
33 GetCmdOutput([adb, 'devices'], env) | |
34 GetCmdOutput([adb, '-s', device, 'root'], env) | |
35 adb_ls_output = GetCmdOutput([adb, '-s', device, 'shell', 'ls', ADB_KEYS_PATH], | |
36 env) | |
37 # If adb_keys file exists on device add its contents to the public keys set. | |
38 if adb_ls_output == ADB_KEYS_PATH: | |
39 dev_keys = GetCmdOutput([adb, '-s', device, 'shell', 'cat', ADB_KEYS_PATH], | |
40 env).splitlines() | |
41 public_keys_set.update(dev_keys) | |
42 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.
| |
43 f.write('\n'.join(public_keys_set)) | |
44 f.close() | |
45 GetCmdOutput([adb, '-s', device, 'push', f.name, ADB_KEYS_PATH], env) | |
46 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.
| |
47 os.path.exists(f.name) | |
48 | |
49 | |
50 def GetUnauthorizedDevices(adb): | |
51 output = GetCmdOutput([adb, 'devices']).splitlines() | |
52 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.
| |
53 if '\tunauthorized' in line] | |
54 return unauthorized_devices | |
55 | |
56 | |
57 def main(argv): | |
58 parser = optparse.OptionParser() | |
59 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.
| |
60 parser.add_option('--adb-keys-dir', | |
61 help='Point to directory that contains adb keys.', | |
62 default=os.path.join(os.environ['HOME'], '.android')) | |
63 options, _ = parser.parse_args() | |
64 dir_contents = os.listdir(options.adb_keys_dir) | |
65 adb = options.adb if options.adb else 'adb' | |
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(adb) | |
78 | |
79 # Kill server launched with ADB_VENDOR_KEYS | |
80 GetCmdOutput([adb, 'kill-server']).splitlines() | |
81 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.
| |
82 if private_key_files else {} | |
83 for device in unauthorized_devices: | |
84 PushHostPubkey(device, adb, env, public_keys_set) | |
85 | |
86 | |
87 if __name__ == '__main__': | |
88 sys.exit(main(sys.argv)) | |
OLD | NEW |