Darin Petkov | c3fd90c | 2011-05-11 14:23:00 -0700 | [diff] [blame] | 1 | # Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 5 | """Devserver module for handling update client requests.""" |
| 6 | |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 9 | import base64 |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 10 | import collections |
Matthew Sartori | 497105b | 2015-12-08 14:01:57 -0800 | [diff] [blame] | 11 | import fcntl |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 12 | import json |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 13 | import os |
Chris Sosa | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 14 | import subprocess |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 15 | import sys |
Gilad Arnold | d0c7175 | 2013-12-06 11:48:45 -0800 | [diff] [blame] | 16 | import threading |
Darin Petkov | 2b2ff4b | 2010-07-27 15:02:09 -0700 | [diff] [blame] | 17 | import time |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 18 | import urllib2 |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 19 | import urlparse |
Chris Sosa | 7c93136 | 2010-10-11 19:49:01 -0700 | [diff] [blame] | 20 | |
Gilad Arnold | abb352e | 2012-09-23 01:24:27 -0700 | [diff] [blame] | 21 | import cherrypy |
| 22 | |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 23 | # Allow importing from dev/host/lib when running from source tree. |
| 24 | lib_dir = os.path.join(os.path.dirname(__file__), 'host', 'lib') |
| 25 | if os.path.exists(lib_dir) and os.path.isdir(lib_dir): |
| 26 | sys.path.insert(1, lib_dir) |
| 27 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 28 | import build_util |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 29 | import autoupdate_lib |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 30 | import common_util |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 31 | import devserver_constants as constants |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 32 | import log_util |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 33 | # pylint: disable=F0401 |
| 34 | import update_payload |
Chris Sosa | 05491b1 | 2010-11-08 17:14:16 -0800 | [diff] [blame] | 35 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 36 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 37 | # If used by client in place of an pre-update version string, forces an update |
| 38 | # to the client regardless of the relative versions of the payload and client. |
| 39 | FORCED_UPDATE = 'ForcedUpdate' |
| 40 | |
| 41 | # Files needed to serve an update. |
| 42 | UPDATE_FILES = ( |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 43 | constants.UPDATE_FILE, |
| 44 | constants.STATEFUL_FILE, |
| 45 | constants.METADATA_FILE |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 46 | ) |
| 47 | |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 48 | # Module-local log function. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 49 | def _Log(message, *args): |
| 50 | return log_util.LogWithTag('UPDATE', message, *args) |
Gilad Arnold | c65330c | 2012-09-20 15:17:48 -0700 | [diff] [blame] | 51 | |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 52 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 53 | class AutoupdateError(Exception): |
| 54 | """Exception classes used by this module.""" |
| 55 | pass |
| 56 | |
| 57 | |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 58 | def _ChangeUrlPort(url, new_port): |
| 59 | """Return the URL passed in with a different port""" |
| 60 | scheme, netloc, path, query, fragment = urlparse.urlsplit(url) |
| 61 | host_port = netloc.split(':') |
| 62 | |
| 63 | if len(host_port) == 1: |
| 64 | host_port.append(new_port) |
| 65 | else: |
| 66 | host_port[1] = new_port |
| 67 | |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 68 | print(host_port) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 69 | netloc = '%s:%s' % tuple(host_port) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 70 | |
| 71 | return urlparse.urlunsplit((scheme, netloc, path, query, fragment)) |
| 72 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 73 | def _NonePathJoin(*args): |
| 74 | """os.path.join that filters None's from the argument list.""" |
| 75 | return os.path.join(*filter(None, args)) |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 76 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 77 | |
| 78 | class HostInfo(object): |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 79 | """Records information about an individual host. |
| 80 | |
| 81 | Members: |
| 82 | attrs: Static attributes (legacy) |
| 83 | log: Complete log of recorded client entries |
| 84 | """ |
| 85 | |
| 86 | def __init__(self): |
| 87 | # A dictionary of current attributes pertaining to the host. |
| 88 | self.attrs = {} |
| 89 | |
| 90 | # A list of pairs consisting of a timestamp and a dictionary of recorded |
| 91 | # attributes. |
| 92 | self.log = [] |
| 93 | |
| 94 | def __repr__(self): |
| 95 | return 'attrs=%s, log=%s' % (self.attrs, self.log) |
| 96 | |
| 97 | def AddLogEntry(self, entry): |
| 98 | """Append a new log entry.""" |
| 99 | # Append a timestamp. |
| 100 | assert not 'timestamp' in entry, 'Oops, timestamp field already in use' |
| 101 | entry['timestamp'] = time.strftime('%Y-%m-%d %H:%M:%S') |
| 102 | # Add entry to hosts' message log. |
| 103 | self.log.append(entry) |
| 104 | |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 105 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 106 | class HostInfoTable(object): |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 107 | """Records information about a set of hosts who engage in update activity. |
| 108 | |
| 109 | Members: |
| 110 | table: Table of information on hosts. |
| 111 | """ |
| 112 | |
| 113 | def __init__(self): |
| 114 | # A dictionary of host information. Keys are normally IP addresses. |
| 115 | self.table = {} |
| 116 | |
| 117 | def __repr__(self): |
| 118 | return '%s' % self.table |
| 119 | |
| 120 | def GetInitHostInfo(self, host_id): |
| 121 | """Return a host's info object, or create a new one if none exists.""" |
| 122 | return self.table.setdefault(host_id, HostInfo()) |
| 123 | |
| 124 | def GetHostInfo(self, host_id): |
| 125 | """Return an info object for given host, if such exists.""" |
Chris Sosa | 1885d03 | 2012-11-29 17:07:27 -0800 | [diff] [blame] | 126 | return self.table.get(host_id) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 127 | |
| 128 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 129 | class UpdateMetadata(object): |
| 130 | """Object containing metadata about an update payload.""" |
| 131 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 132 | def __init__(self, sha1, sha256, size, is_delta_format, metadata_size, |
| 133 | metadata_hash): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 134 | self.sha1 = sha1 |
| 135 | self.sha256 = sha256 |
| 136 | self.size = size |
| 137 | self.is_delta_format = is_delta_format |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 138 | self.metadata_size = metadata_size |
| 139 | self.metadata_hash = metadata_hash |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 140 | |
| 141 | |
joychen | 921e1fb | 2013-06-28 11:12:20 -0700 | [diff] [blame] | 142 | class Autoupdate(build_util.BuildObject): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 143 | """Class that contains functionality that handles Chrome OS update pings. |
| 144 | |
| 145 | Members: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 146 | urlbase: base URL, other than devserver, for update images. |
| 147 | forced_image: path to an image to use for all updates. |
| 148 | payload_path: path to pre-generated payload to serve. |
| 149 | src_image: if specified, creates a delta payload from this image. |
| 150 | proxy_port: port of local proxy to tell client to connect to you |
| 151 | through. |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 152 | board: board for the image. Needed for pre-generating of updates. |
| 153 | copy_to_static_root: copies images generated from the cache to ~/static. |
| 154 | private_key: path to private key in PEM format. |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 155 | private_key_for_metadata_hash_signature: path to private key in PEM format. |
| 156 | public_key: path to public key in PEM format. |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 157 | critical_update: whether provisioned payload is critical. |
| 158 | remote_payload: whether provisioned payload is remotely staged. |
| 159 | max_updates: maximum number of updates we'll try to provision. |
| 160 | host_log: record full history of host update events. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 161 | """ |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 162 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 163 | _OLD_PAYLOAD_URL_PREFIX = '/static/archive' |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 164 | _PAYLOAD_URL_PREFIX = '/static/' |
| 165 | _FILEINFO_URL_PREFIX = '/api/fileinfo/' |
| 166 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 167 | SHA1_ATTR = 'sha1' |
| 168 | SHA256_ATTR = 'sha256' |
| 169 | SIZE_ATTR = 'size' |
| 170 | ISDELTA_ATTR = 'is_delta' |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 171 | METADATA_SIZE_ATTR = 'metadata_size' |
| 172 | METADATA_HASH_ATTR = 'metadata_hash' |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 173 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 174 | def __init__(self, xbuddy, urlbase=None, forced_image=None, payload_path=None, |
Gabe Black | 7099486 | 2014-09-05 00:50:58 -0700 | [diff] [blame] | 175 | proxy_port=None, src_image='', board=None, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 176 | copy_to_static_root=True, private_key=None, |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 177 | private_key_for_metadata_hash_signature=None, public_key=None, |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 178 | critical_update=False, remote_payload=False, max_updates=-1, |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 179 | host_log=False, *args, **kwargs): |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 180 | super(Autoupdate, self).__init__(*args, **kwargs) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 181 | self.xbuddy = xbuddy |
| 182 | self.urlbase = urlbase or None |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 183 | self.forced_image = forced_image |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 184 | self.payload_path = payload_path |
Chris Sosa | 62f720b | 2010-10-26 21:39:48 -0700 | [diff] [blame] | 185 | self.src_image = src_image |
Don Garrett | 0ad0937 | 2010-12-06 16:20:30 -0800 | [diff] [blame] | 186 | self.proxy_port = proxy_port |
joychen | 562699a | 2013-08-13 15:22:14 -0700 | [diff] [blame] | 187 | self.board = board or self.GetDefaultBoardID() |
Chris Sosa | 08d55a2 | 2011-01-19 16:08:02 -0800 | [diff] [blame] | 188 | self.copy_to_static_root = copy_to_static_root |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 189 | self.private_key = private_key |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 190 | self.private_key_for_metadata_hash_signature = \ |
| 191 | private_key_for_metadata_hash_signature |
| 192 | self.public_key = public_key |
Satoru Takabayashi | d733cbe | 2011-11-15 09:36:32 -0800 | [diff] [blame] | 193 | self.critical_update = critical_update |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 194 | self.remote_payload = remote_payload |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 195 | self.max_updates = max_updates |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 196 | self.host_log = host_log |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 197 | |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 198 | self.pregenerated_path = None |
Sean O'Connor | 14b6a0a | 2010-03-20 23:23:48 -0700 | [diff] [blame] | 199 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 200 | # Initialize empty host info cache. Used to keep track of various bits of |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 201 | # information about a given host. A host is identified by its IP address. |
| 202 | # The info stored for each host includes a complete log of events for this |
| 203 | # host, as well as a dictionary of current attributes derived from events. |
| 204 | self.host_infos = HostInfoTable() |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 205 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 206 | self._update_count_lock = threading.Lock() |
Gilad Arnold | d0c7175 | 2013-12-06 11:48:45 -0800 | [diff] [blame] | 207 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 208 | @classmethod |
| 209 | def _ReadMetadataFromStream(cls, stream): |
| 210 | """Returns metadata obj from input json stream that implements .read().""" |
| 211 | file_attr_dict = {} |
| 212 | try: |
| 213 | file_attr_dict = json.loads(stream.read()) |
| 214 | except IOError: |
| 215 | return None |
| 216 | |
| 217 | sha1 = file_attr_dict.get(cls.SHA1_ATTR) |
| 218 | sha256 = file_attr_dict.get(cls.SHA256_ATTR) |
| 219 | size = file_attr_dict.get(cls.SIZE_ATTR) |
| 220 | is_delta = file_attr_dict.get(cls.ISDELTA_ATTR) |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 221 | metadata_size = file_attr_dict.get(cls.METADATA_SIZE_ATTR) |
| 222 | metadata_hash = file_attr_dict.get(cls.METADATA_HASH_ATTR) |
| 223 | return UpdateMetadata(sha1, sha256, size, is_delta, metadata_size, |
| 224 | metadata_hash) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 225 | |
| 226 | @staticmethod |
| 227 | def _ReadMetadataFromFile(payload_dir): |
| 228 | """Returns metadata object from the metadata_file in the payload_dir""" |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 229 | metadata_file = os.path.join(payload_dir, constants.METADATA_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 230 | if os.path.exists(metadata_file): |
Matthew Sartori | 497105b | 2015-12-08 14:01:57 -0800 | [diff] [blame] | 231 | metadata_stream = open(metadata_file, 'r') |
| 232 | fcntl.lockf(metadata_stream.fileno(), fcntl.LOCK_SH) |
| 233 | metadata = Autoupdate._ReadMetadataFromStream(metadata_stream) |
| 234 | fcntl.lockf(metadata_stream.fileno(), fcntl.LOCK_UN) |
| 235 | return metadata |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 236 | |
| 237 | @classmethod |
| 238 | def _StoreMetadataToFile(cls, payload_dir, metadata_obj): |
| 239 | """Stores metadata object into the metadata_file of the payload_dir""" |
| 240 | file_dict = {cls.SHA1_ATTR: metadata_obj.sha1, |
| 241 | cls.SHA256_ATTR: metadata_obj.sha256, |
| 242 | cls.SIZE_ATTR: metadata_obj.size, |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 243 | cls.ISDELTA_ATTR: metadata_obj.is_delta_format, |
| 244 | cls.METADATA_SIZE_ATTR: metadata_obj.metadata_size, |
| 245 | cls.METADATA_HASH_ATTR: metadata_obj.metadata_hash} |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 246 | metadata_file = os.path.join(payload_dir, constants.METADATA_FILE) |
Matthew Sartori | 497105b | 2015-12-08 14:01:57 -0800 | [diff] [blame] | 247 | file_handle = open(metadata_file, 'w') |
| 248 | fcntl.lockf(file_handle.fileno(), fcntl.LOCK_EX) |
| 249 | json.dump(file_dict, file_handle) |
| 250 | fcntl.lockf(file_handle.fileno(), fcntl.LOCK_UN) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 251 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 252 | @staticmethod |
| 253 | def _GetVersionFromDir(image_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 254 | """Returns the version of the image based on the name of the directory.""" |
| 255 | latest_version = os.path.basename(image_dir) |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 256 | parts = latest_version.split('-') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 257 | # If we can't get a version number from the directory, default to a high |
| 258 | # number to allow the update to happen |
| 259 | return parts[1] if len(parts) == 3 else "9999.0.0" |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 260 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 261 | @staticmethod |
| 262 | def _CanUpdate(client_version, latest_version): |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 263 | """True if the latest_version is greater than the client_version.""" |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 264 | _Log('client version %s latest version %s', client_version, latest_version) |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 265 | |
| 266 | client_tokens = client_version.replace('_', '').split('.') |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 267 | latest_tokens = latest_version.replace('_', '').split('.') |
Daniel Erat | 8a0bc4a | 2011-09-30 08:52:52 -0700 | [diff] [blame] | 268 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 269 | if len(latest_tokens) == len(client_tokens) == 3: |
| 270 | return latest_tokens > client_tokens |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 271 | else: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 272 | # If the directory name isn't a version number, let it pass. |
| 273 | return True |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 274 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 275 | @staticmethod |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 276 | def IsDeltaFormatFile(filename): |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 277 | try: |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 278 | with open(filename) as payload_file: |
| 279 | payload = update_payload.Payload(payload_file) |
| 280 | payload.Init() |
| 281 | return payload.IsDelta() |
| 282 | except (IOError, update_payload.PayloadError): |
| 283 | # For unit tests we may not have real files, so it's ok to ignore these |
| 284 | # errors. |
Andrew de los Reyes | 5679b97 | 2010-10-25 17:34:49 -0700 | [diff] [blame] | 285 | return False |
| 286 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 287 | def GenerateUpdateFile(self, src_image, image_path, output_dir): |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 288 | """Generates an update gz given a full path to an image. |
| 289 | |
| 290 | Args: |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 291 | src_image: Path to a source image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 292 | image_path: Full path to image. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 293 | output_dir: Path to the generated update file. |
| 294 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 295 | Raises: |
| 296 | subprocess.CalledProcessError if the update generator fails to generate a |
| 297 | stateful payload. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 298 | """ |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 299 | update_path = os.path.join(output_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 300 | _Log('Generating update image %s', update_path) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 301 | |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 302 | update_command = [ |
Chris Sosa | 5b8b5eb | 2012-03-27 11:15:27 -0700 | [diff] [blame] | 303 | 'cros_generate_update_payload', |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 304 | '--image', image_path, |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 305 | '--out_metadata_hash_file', os.path.join(output_dir, |
| 306 | constants.METADATA_HASH_FILE), |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 307 | '--output', update_path, |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 308 | ] |
Chris Sosa | 4136e69 | 2010-10-28 23:42:37 -0700 | [diff] [blame] | 309 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 310 | if src_image: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 311 | update_command.extend(['--src_image', src_image]) |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 312 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 313 | if self.private_key: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 314 | update_command.extend(['--private_key', self.private_key]) |
Chris Sosa | 0f1ec84 | 2011-02-14 16:33:22 -0800 | [diff] [blame] | 315 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 316 | _Log('Running %s', ' '.join(update_command)) |
| 317 | subprocess.check_call(update_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 318 | |
Chris Sosa | 5214858 | 2012-11-15 15:35:58 -0800 | [diff] [blame] | 319 | @staticmethod |
| 320 | def GenerateStatefulFile(image_path, output_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 321 | """Generates a stateful update payload given a full path to an image. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 322 | |
| 323 | Args: |
| 324 | image_path: Full path to image. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 325 | output_dir: Directory for emitting the stateful update payload. |
| 326 | |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 327 | Raises: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 328 | subprocess.CalledProcessError if the update generator fails to generate a |
Chris Sosa | 908fd6f | 2010-11-10 17:31:18 -0800 | [diff] [blame] | 329 | stateful payload. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 330 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 331 | update_command = [ |
| 332 | 'cros_generate_stateful_update_payload', |
| 333 | '--image', image_path, |
| 334 | '--output_dir', output_dir, |
| 335 | ] |
| 336 | _Log('Running %s', ' '.join(update_command)) |
| 337 | subprocess.check_call(update_command) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 338 | |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 339 | def FindCachedUpdateImageSubDir(self, src_image, dest_image): |
| 340 | """Find directory to store a cached update. |
| 341 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 342 | Given one, or two images for an update, this finds which cache directory |
| 343 | should hold the update files, even if they don't exist yet. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 344 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 345 | Returns: |
| 346 | A directory path for storing a cached update, of the following form: |
| 347 | Non-delta updates: |
| 348 | CACHE_DIR/<dest_hash> |
| 349 | Delta updates: |
| 350 | CACHE_DIR/<src_hash>_<dest_hash> |
| 351 | Signed updates (self.private_key): |
| 352 | CACHE_DIR/<src_hash>_<dest_hash>+<private_key_hash> |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 353 | """ |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 354 | update_dir = '' |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 355 | if src_image: |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 356 | update_dir += common_util.GetFileMd5(src_image) + '_' |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 357 | |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 358 | update_dir += common_util.GetFileMd5(dest_image) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 359 | if self.private_key: |
Gilad Arnold | 55a2a37 | 2012-10-02 09:46:32 -0700 | [diff] [blame] | 360 | update_dir += '+' + common_util.GetFileMd5(self.private_key) |
Chris Sosa | 744e147 | 2011-09-07 19:32:50 -0700 | [diff] [blame] | 361 | |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 362 | return os.path.join(constants.CACHE_DIR, update_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 363 | |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 364 | def GenerateUpdateImage(self, image_path, output_dir): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 365 | """Force generates an update payload based on the given image_path. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 366 | |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 367 | Args: |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 368 | image_path: full path to the image. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 369 | output_dir: the directory to write the update payloads to |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 370 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 371 | Raises: |
| 372 | AutoupdateError if it failed to generate either update or stateful |
| 373 | payload. |
Chris Sosa | de91f67 | 2010-11-16 10:05:44 -0800 | [diff] [blame] | 374 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 375 | _Log('Generating update for image %s', image_path) |
Andrew de los Reyes | 9a52871 | 2010-06-30 10:29:43 -0700 | [diff] [blame] | 376 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 377 | # Delete any previous state in this directory. |
| 378 | os.system('rm -rf "%s"' % output_dir) |
| 379 | os.makedirs(output_dir) |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 380 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 381 | try: |
| 382 | self.GenerateUpdateFile(self.src_image, image_path, output_dir) |
| 383 | self.GenerateStatefulFile(image_path, output_dir) |
| 384 | except subprocess.CalledProcessError: |
| 385 | os.system('rm -rf "%s"' % output_dir) |
| 386 | raise AutoupdateError('Failed to generate update in %s' % output_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 387 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 388 | def GenerateUpdateImageWithCache(self, image_path): |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 389 | """Force generates an update payload based on the given image_path. |
rtc@google.com | ded2240 | 2009-10-26 22:36:21 +0000 | [diff] [blame] | 390 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 391 | Args: |
| 392 | image_path: full path to the image. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 393 | |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 394 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 395 | update directory relative to static_image_dir. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 396 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 397 | Raises: |
| 398 | AutoupdateError if it we need to generate a payload and fail to do so. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 399 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 400 | _Log('Generating update for src %s image %s', self.src_image, image_path) |
Chris Sosa | e67b78f | 2010-11-04 17:33:16 -0700 | [diff] [blame] | 401 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 402 | # If it was pregenerated, don't regenerate. |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 403 | if self.pregenerated_path: |
| 404 | return self.pregenerated_path |
Don Garrett | fff4c32 | 2010-11-19 13:37:12 -0800 | [diff] [blame] | 405 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 406 | # Which sub_dir should hold our cached update image. |
| 407 | cache_sub_dir = self.FindCachedUpdateImageSubDir(self.src_image, image_path) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 408 | _Log('Caching in sub_dir "%s"', cache_sub_dir) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 409 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 410 | # The cached payloads exist in a cache dir. |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 411 | cache_dir = os.path.join(self.static_dir, cache_sub_dir) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 412 | |
| 413 | cache_update_payload = os.path.join(cache_dir, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 414 | constants.UPDATE_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 415 | cache_stateful_payload = os.path.join(cache_dir, |
joychen | 25d2597 | 2013-07-30 14:54:16 -0700 | [diff] [blame] | 416 | constants.STATEFUL_FILE) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 417 | # Check to see if this cache directory is valid. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 418 | if not (os.path.exists(cache_update_payload) and |
| 419 | os.path.exists(cache_stateful_payload)): |
| 420 | self.GenerateUpdateImage(image_path, cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 421 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 422 | # Don't regenerate the image for this devserver instance. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 423 | self.pregenerated_path = cache_sub_dir |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 424 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 425 | # Generate the cache file. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 426 | self.GetLocalPayloadAttrs(cache_dir) |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 427 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 428 | return cache_sub_dir |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 429 | |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 430 | def _SymlinkUpdateFiles(self, target_dir, link_dir): |
| 431 | """Symlinks the update-related files from target_dir to link_dir. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 432 | |
| 433 | Every time an update is called, clear existing files/symlinks in the |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 434 | link_dir, and replace them with symlinks to the target_dir. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 435 | |
| 436 | Args: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 437 | target_dir: Location of the target files. |
| 438 | link_dir: Directory where the links should exist after. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 439 | """ |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 440 | _Log('Linking %s to %s', target_dir, link_dir) |
| 441 | if link_dir == target_dir: |
| 442 | _Log('Cannot symlink into the same directory.') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 443 | return |
| 444 | for f in UPDATE_FILES: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 445 | link = os.path.join(link_dir, f) |
| 446 | target = os.path.join(target_dir, f) |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 447 | common_util.SymlinkFile(target, link) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 448 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 449 | def GetUpdateForLabel(self, client_version, label, |
| 450 | image_name=constants.TEST_IMAGE_FILE): |
| 451 | """Given a label, get an update from the directory. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 452 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 453 | Args: |
| 454 | client_version: Current version of the client or FORCED_UPDATE |
| 455 | label: the relative directory inside the static dir |
| 456 | image_name: If the image type was specified by the update rpc, we try to |
| 457 | find an image with this file name first. This is by default |
| 458 | "chromiumos_test_image.bin" but can also take any of the values in |
| 459 | devserver_constants.ALL_IMAGES |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 460 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 461 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 462 | A relative path to the directory with the update payload. |
| 463 | This is the label if an update did not need to be generated, but can |
| 464 | be label/cache/hashed_dir_for_update. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 465 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 466 | Raises: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 467 | AutoupdateError: If client version is higher than available update found |
| 468 | at the directory given by the label. |
Don Garrett | f90edf0 | 2010-11-16 17:36:14 -0800 | [diff] [blame] | 469 | """ |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 470 | _Log('Update label/file: %s/%s', label, image_name) |
| 471 | static_image_dir = _NonePathJoin(self.static_dir, label) |
| 472 | static_update_path = _NonePathJoin(static_image_dir, constants.UPDATE_FILE) |
| 473 | static_image_path = _NonePathJoin(static_image_dir, image_name) |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 474 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 475 | # Update the client only if client version is older than available update. |
| 476 | latest_version = self._GetVersionFromDir(static_image_dir) |
| 477 | if not (client_version == FORCED_UPDATE or |
| 478 | self._CanUpdate(client_version, latest_version)): |
| 479 | raise AutoupdateError( |
| 480 | 'Update check received but no update available for client') |
Don Garrett | ee25e55 | 2010-11-23 12:09:35 -0800 | [diff] [blame] | 481 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 482 | if label and os.path.exists(static_update_path): |
| 483 | # An update payload was found for the given label, return it. |
| 484 | return label |
| 485 | elif os.path.exists(static_image_path) and common_util.IsInsideChroot(): |
| 486 | # Image was found for the given label. Generate update if we can. |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 487 | rel_path = self.GenerateUpdateImageWithCache(static_image_path) |
| 488 | # Add links from the static directory to the update. |
| 489 | cache_path = _NonePathJoin(self.static_dir, rel_path) |
| 490 | self._SymlinkUpdateFiles(cache_path, static_image_dir) |
| 491 | return label |
Don Garrett | 0c880e2 | 2010-11-17 18:13:37 -0800 | [diff] [blame] | 492 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 493 | # The label didn't resolve. |
| 494 | return None |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 495 | |
| 496 | def PreGenerateUpdate(self): |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 497 | """Pre-generates an update and prints out the relative path it. |
| 498 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 499 | Returns relative path of the update. |
Chris Sosa | 65d339b | 2013-01-21 18:59:21 -0800 | [diff] [blame] | 500 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 501 | Raises: |
| 502 | AutoupdateError if it failed to generate the payload. |
| 503 | """ |
| 504 | _Log('Pre-generating the update payload') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 505 | # Does not work with labels so just use static dir. (empty label) |
| 506 | pregenerated_update = self.GetPathToPayload('', FORCED_UPDATE, self.board) |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 507 | print('PREGENERATED_UPDATE=%s' % _NonePathJoin(pregenerated_update, |
| 508 | constants.UPDATE_FILE)) |
Chris Sosa | 417e55d | 2011-01-25 16:40:48 -0800 | [diff] [blame] | 509 | return pregenerated_update |
Chris Sosa | 2c048f1 | 2010-10-27 16:05:27 -0700 | [diff] [blame] | 510 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 511 | def _GetRemotePayloadAttrs(self, url): |
| 512 | """Returns hashes, size and delta flag of a remote update payload. |
| 513 | |
| 514 | Obtain attributes of a payload file available on a remote devserver. This |
| 515 | is based on the assumption that the payload URL uses the /static prefix. We |
| 516 | need to make sure that both clients (requests) and remote devserver |
| 517 | (provisioning) preserve this invariant. |
| 518 | |
| 519 | Args: |
| 520 | url: URL of statically staged remote file (http://host:port/static/...) |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 521 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 522 | Returns: |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 523 | A UpdateMetadata object. |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 524 | """ |
| 525 | if self._PAYLOAD_URL_PREFIX not in url: |
| 526 | raise AutoupdateError( |
| 527 | 'Payload URL does not have the expected prefix (%s)' % |
| 528 | self._PAYLOAD_URL_PREFIX) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 529 | |
joychen | ed64b22 | 2013-06-21 16:39:34 -0700 | [diff] [blame] | 530 | if self._OLD_PAYLOAD_URL_PREFIX in url: |
| 531 | fileinfo_url = url.replace(self._OLD_PAYLOAD_URL_PREFIX, |
| 532 | self._FILEINFO_URL_PREFIX) |
| 533 | else: |
| 534 | fileinfo_url = url.replace(self._PAYLOAD_URL_PREFIX, |
| 535 | self._FILEINFO_URL_PREFIX) |
| 536 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 537 | _Log('Retrieving file info for remote payload via %s', fileinfo_url) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 538 | try: |
| 539 | conn = urllib2.urlopen(fileinfo_url) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 540 | metadata_obj = Autoupdate._ReadMetadataFromStream(conn) |
| 541 | # These fields are required for remote calls. |
| 542 | if not metadata_obj: |
| 543 | raise AutoupdateError('Failed to obtain remote payload info') |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 544 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 545 | return metadata_obj |
| 546 | except IOError as e: |
| 547 | raise AutoupdateError('Failed to obtain remote payload info: %s', e) |
| 548 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 549 | @staticmethod |
| 550 | def _GetMetadataHash(payload_dir): |
David Zeuthen | f27f150 | 2013-11-13 10:38:16 -0800 | [diff] [blame] | 551 | """Gets the metadata hash, if it exists. |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 552 | |
| 553 | Args: |
| 554 | payload_dir: The payload directory. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 555 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 556 | Returns: |
David Zeuthen | f27f150 | 2013-11-13 10:38:16 -0800 | [diff] [blame] | 557 | The metadata hash, base-64 encoded or None if there is no metadata hash. |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 558 | """ |
| 559 | path = os.path.join(payload_dir, constants.METADATA_HASH_FILE) |
David Zeuthen | f27f150 | 2013-11-13 10:38:16 -0800 | [diff] [blame] | 560 | if os.path.exists(path): |
| 561 | return base64.b64encode(open(path, 'rb').read()) |
| 562 | else: |
| 563 | return None |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 564 | |
| 565 | @staticmethod |
| 566 | def _GetMetadataSize(payload_filename): |
| 567 | """Gets the size of the metadata in a payload file. |
| 568 | |
| 569 | Args: |
| 570 | payload_filename: Path to the payload file. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 571 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 572 | Returns: |
| 573 | The size of the payload metadata, as reported in the payload header. |
| 574 | """ |
Alex Deymo | a6ac00d | 2015-10-15 09:14:58 -0700 | [diff] [blame] | 575 | try: |
| 576 | with open(payload_filename) as payload_file: |
| 577 | payload = update_payload.Payload(payload_file) |
| 578 | payload.Init() |
| 579 | return payload.metadata_size |
| 580 | except (IOError, update_payload.PayloadError): |
| 581 | # For unit tests we may not have real files, so it's ok to ignore these |
| 582 | # errors. |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 583 | return 0 |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 584 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 585 | def GetLocalPayloadAttrs(self, payload_dir): |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 586 | """Returns hashes, size and delta flag of a local update payload. |
| 587 | |
| 588 | Args: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 589 | payload_dir: Path to the directory the payload is in. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 590 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 591 | Returns: |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 592 | A UpdateMetadata object. |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 593 | """ |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 594 | filename = os.path.join(payload_dir, constants.UPDATE_FILE) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 595 | if not os.path.exists(filename): |
| 596 | raise AutoupdateError('update.gz not present in payload dir %s' % |
| 597 | payload_dir) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 598 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 599 | metadata_obj = Autoupdate._ReadMetadataFromFile(payload_dir) |
| 600 | if not metadata_obj or not (metadata_obj.sha1 and |
| 601 | metadata_obj.sha256 and |
| 602 | metadata_obj.size): |
| 603 | sha1 = common_util.GetFileSha1(filename) |
| 604 | sha256 = common_util.GetFileSha256(filename) |
| 605 | size = common_util.GetFileSize(filename) |
Gilad Arnold | e74b381 | 2013-04-22 11:27:38 -0700 | [diff] [blame] | 606 | is_delta_format = self.IsDeltaFormatFile(filename) |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 607 | metadata_size = self._GetMetadataSize(filename) |
| 608 | metadata_hash = self._GetMetadataHash(payload_dir) |
| 609 | metadata_obj = UpdateMetadata(sha1, sha256, size, is_delta_format, |
| 610 | metadata_size, metadata_hash) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 611 | Autoupdate._StoreMetadataToFile(payload_dir, metadata_obj) |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 612 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 613 | return metadata_obj |
| 614 | |
| 615 | def _ProcessUpdateComponents(self, app, event): |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 616 | """Processes the components of an update request. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 617 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 618 | Args: |
| 619 | app: An app component of an update request. |
| 620 | event: An event component of an update request. |
| 621 | |
| 622 | Returns: |
| 623 | A named tuple containing attributes of the update requests as the |
| 624 | following fields: 'forced_update_label', 'client_version', 'board', |
| 625 | 'event_result' and 'event_type'. |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 626 | """ |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 627 | # Initialize an empty dictionary for event attributes to log. |
| 628 | log_message = {} |
Jay Srinivasan | ac69d26 | 2012-10-30 19:05:53 -0700 | [diff] [blame] | 629 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 630 | # Determine request IP, strip any IPv6 data for simplicity. |
| 631 | client_ip = cherrypy.request.remote.ip.split(':')[-1] |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 632 | # Obtain (or init) info object for this client. |
| 633 | curr_host_info = self.host_infos.GetInitHostInfo(client_ip) |
| 634 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 635 | client_version = FORCED_UPDATE |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 636 | board = None |
| 637 | if app: |
| 638 | client_version = app.getAttribute('version') |
| 639 | channel = app.getAttribute('track') |
| 640 | board = (app.hasAttribute('board') and app.getAttribute('board') |
Don Garrett | fb15e32 | 2016-06-21 19:12:08 -0700 | [diff] [blame] | 641 | or self.GetDefaultBoardID()) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 642 | # Add attributes to log message |
| 643 | log_message['version'] = client_version |
| 644 | log_message['track'] = channel |
| 645 | log_message['board'] = board |
| 646 | curr_host_info.attrs['last_known_version'] = client_version |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 647 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 648 | event_result = None |
| 649 | event_type = None |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 650 | if event: |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 651 | event_result = int(event[0].getAttribute('eventresult')) |
| 652 | event_type = int(event[0].getAttribute('eventtype')) |
Gilad Arnold | b11a894 | 2012-03-13 15:33:21 -0700 | [diff] [blame] | 653 | client_previous_version = (event[0].getAttribute('previousversion') |
| 654 | if event[0].hasAttribute('previousversion') |
| 655 | else None) |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 656 | # Store attributes to legacy host info structure |
| 657 | curr_host_info.attrs['last_event_status'] = event_result |
| 658 | curr_host_info.attrs['last_event_type'] = event_type |
| 659 | # Add attributes to log message |
| 660 | log_message['event_result'] = event_result |
| 661 | log_message['event_type'] = event_type |
Gilad Arnold | b11a894 | 2012-03-13 15:33:21 -0700 | [diff] [blame] | 662 | if client_previous_version is not None: |
| 663 | log_message['previous_version'] = client_previous_version |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 664 | |
Gilad Arnold | 8318eac | 2012-10-04 12:52:23 -0700 | [diff] [blame] | 665 | # Log host event, if so instructed. |
| 666 | if self.host_log: |
| 667 | curr_host_info.AddLogEntry(log_message) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 668 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 669 | UpdateRequestAttrs = collections.namedtuple( |
| 670 | 'UpdateRequestAttrs', |
| 671 | ('forced_update_label', 'client_version', 'board', 'event_result', |
| 672 | 'event_type')) |
| 673 | |
| 674 | return UpdateRequestAttrs( |
| 675 | curr_host_info.attrs.pop('forced_update_label', None), |
| 676 | client_version, board, event_result, event_type) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 677 | |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 678 | @classmethod |
| 679 | def _CheckOmahaRequest(cls, app): |
| 680 | """Checks |app| component of Omaha Request for correctly formed data. |
| 681 | |
| 682 | Raises: |
| 683 | common_util.DevServerHTTPError: if any check fails. All 400 error codes to |
| 684 | indicate a bad HTTP request. |
| 685 | """ |
| 686 | if not app: |
| 687 | raise common_util.DevServerHTTPError( |
| 688 | 400, 'Missing app component in Omaha Request') |
| 689 | |
| 690 | hardware_class = app.getAttribute('hardware_class') |
| 691 | if not hardware_class: |
| 692 | raise common_util.DevServerHTTPError( |
| 693 | 400, 'hardware_class is required in Omaha Request') |
| 694 | |
| 695 | track = app.getAttribute('track') |
Chris Sosa | fc71544 | 2014-04-09 20:45:23 -0700 | [diff] [blame] | 696 | if not (track and track.endswith('-channel')): |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 697 | raise common_util.DevServerHTTPError( |
Chris Sosa | fc71544 | 2014-04-09 20:45:23 -0700 | [diff] [blame] | 698 | 400, 'Omaha requests need a valid update channel') |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 699 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 700 | def _GetStaticUrl(self): |
| 701 | """Returns the static url base that should prefix all payload responses.""" |
| 702 | x_forwarded_host = cherrypy.request.headers.get('X-Forwarded-Host') |
| 703 | if x_forwarded_host: |
| 704 | hostname = 'http://' + x_forwarded_host |
| 705 | else: |
| 706 | hostname = cherrypy.request.base |
| 707 | |
| 708 | if self.urlbase: |
| 709 | static_urlbase = self.urlbase |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 710 | else: |
| 711 | static_urlbase = '%s/static' % hostname |
| 712 | |
| 713 | # If we have a proxy port, adjust the URL we instruct the client to |
| 714 | # use to go through the proxy. |
| 715 | if self.proxy_port: |
| 716 | static_urlbase = _ChangeUrlPort(static_urlbase, self.proxy_port) |
| 717 | |
| 718 | _Log('Using static url base %s', static_urlbase) |
| 719 | _Log('Handling update ping as %s', hostname) |
| 720 | return static_urlbase |
| 721 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 722 | def GetPathToPayload(self, label, client_version, board): |
| 723 | """Find a payload locally. |
| 724 | |
| 725 | See devserver's update rpc for documentation. |
| 726 | |
| 727 | Args: |
| 728 | label: from update request |
| 729 | client_version: from update request |
| 730 | board: from update request |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 731 | |
| 732 | Returns: |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 733 | The relative path to an update from the static_dir |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 734 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 735 | Raises: |
| 736 | AutoupdateError: If the update could not be found. |
| 737 | """ |
| 738 | path_to_payload = None |
| 739 | #TODO(joychen): deprecate --payload flag |
| 740 | if self.payload_path: |
| 741 | # Copy the image from the path to '/forced_payload' |
| 742 | label = 'forced_payload' |
| 743 | dest_path = os.path.join(self.static_dir, label, constants.UPDATE_FILE) |
| 744 | dest_stateful = os.path.join(self.static_dir, label, |
| 745 | constants.STATEFUL_FILE) |
Alex Deymo | 48e970d | 2015-09-23 14:34:41 -0700 | [diff] [blame] | 746 | dest_meta = os.path.join(self.static_dir, label, constants.METADATA_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 747 | |
| 748 | src_path = os.path.abspath(self.payload_path) |
| 749 | src_stateful = os.path.join(os.path.dirname(src_path), |
| 750 | constants.STATEFUL_FILE) |
| 751 | common_util.MkDirP(os.path.join(self.static_dir, label)) |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 752 | common_util.SymlinkFile(src_path, dest_path) |
Alex Deymo | 48e970d | 2015-09-23 14:34:41 -0700 | [diff] [blame] | 753 | # The old metadata file should be regenerated whenever a new payload is |
| 754 | # used. |
| 755 | try: |
| 756 | os.unlink(dest_meta) |
| 757 | except OSError: |
| 758 | pass |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 759 | if os.path.exists(src_stateful): |
| 760 | # The stateful payload is optional. |
Alex Deymo | 3e2d495 | 2013-09-03 21:49:41 -0700 | [diff] [blame] | 761 | common_util.SymlinkFile(src_stateful, dest_stateful) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 762 | else: |
| 763 | _Log('WARN: %s not found. Expected for dev and test builds', |
| 764 | constants.STATEFUL_FILE) |
| 765 | if os.path.exists(dest_stateful): |
| 766 | os.remove(dest_stateful) |
| 767 | path_to_payload = self.GetUpdateForLabel(client_version, label) |
| 768 | #TODO(joychen): deprecate --image flag |
| 769 | elif self.forced_image: |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 770 | if self.forced_image.startswith('xbuddy:'): |
| 771 | # This is trying to use an xbuddy path in place of a path to an image. |
joychen | dbfe6c9 | 2013-08-16 20:03:49 -0700 | [diff] [blame] | 772 | xbuddy_label = self.forced_image.split(':')[1] |
| 773 | self.forced_image = None |
joychen | 365a574 | 2013-08-21 10:41:18 -0700 | [diff] [blame] | 774 | # Make sure the xbuddy path target is in the directory. |
| 775 | path_to_payload, _image_name = self.xbuddy.Get(xbuddy_label.split('/')) |
| 776 | # Pretend to have called update with this update path to payload. |
Chris Sosa | 54ef81e | 2013-08-27 16:45:12 -0700 | [diff] [blame] | 777 | self.GetPathToPayload(xbuddy_label, client_version, board) |
| 778 | else: |
| 779 | src_path = os.path.abspath(self.forced_image) |
| 780 | if os.path.exists(src_path) and common_util.IsInsideChroot(): |
| 781 | # Image was found for the given label. Generate update if we can. |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 782 | path_to_payload = self.GenerateUpdateImageWithCache(src_path) |
| 783 | # Add links from the static directory to the update. |
| 784 | cache_path = _NonePathJoin(self.static_dir, path_to_payload) |
| 785 | self._SymlinkUpdateFiles(cache_path, self.static_dir) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 786 | else: |
| 787 | label = label or '' |
| 788 | label_list = label.split('/') |
| 789 | # Suppose that the path follows old protocol of indexing straight |
| 790 | # into static_dir with board/version label. |
| 791 | # Attempt to get the update in that directory, generating if necc. |
| 792 | path_to_payload = self.GetUpdateForLabel(client_version, label) |
| 793 | if path_to_payload is None: |
| 794 | # There was no update or image found in the directory. |
| 795 | # Let XBuddy find an image, and then generate an update to it. |
| 796 | if label_list[0] == 'xbuddy': |
| 797 | # If path explicitly calls xbuddy, pop off the tag. |
| 798 | label_list.pop() |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 799 | x_label, image_name = self.xbuddy.Translate(label_list, board=board) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 800 | if image_name not in constants.ALL_IMAGES: |
| 801 | raise AutoupdateError( |
| 802 | "Use an image alias: dev, base, test, or recovery.") |
| 803 | # Path has been resolved, try to get the image. |
| 804 | path_to_payload = self.GetUpdateForLabel(client_version, x_label, |
| 805 | image_name) |
| 806 | if path_to_payload is None: |
| 807 | # Neither image nor update payload found after translation. |
| 808 | # Try to get an update to a test image from GS using the label. |
| 809 | path_to_payload, _image_name = self.xbuddy.Get( |
| 810 | ['remote', label, 'full_payload']) |
| 811 | |
| 812 | # One of the above options should have gotten us a relative path. |
| 813 | if path_to_payload is None: |
| 814 | raise AutoupdateError('Failed to get an update for: %s' % label) |
| 815 | else: |
Chris Sosa | 7549080 | 2013-09-30 17:21:45 -0700 | [diff] [blame] | 816 | return path_to_payload |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 817 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 818 | @staticmethod |
| 819 | def _SignMetadataHash(private_key_path, metadata_hash): |
| 820 | """Signs metadata hash. |
| 821 | |
| 822 | Signs a metadata hash with a private key. This includes padding the |
| 823 | hash with PKCS#1 v1.5 padding as well as an ASN.1 header. |
| 824 | |
| 825 | Args: |
| 826 | private_key_path: The path to a private key to use for signing. |
| 827 | metadata_hash: A raw SHA-256 hash (32 bytes). |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 828 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 829 | Returns: |
| 830 | The raw signature. |
| 831 | """ |
| 832 | args = ['openssl', 'rsautl', '-pkcs', '-sign', '-inkey', private_key_path] |
| 833 | padded_metadata_hash = ('\x30\x31\x30\x0d\x06\x09\x60\x86' |
| 834 | '\x48\x01\x65\x03\x04\x02\x01\x05' |
| 835 | '\x00\x04\x20') + metadata_hash |
| 836 | child = subprocess.Popen(args, |
| 837 | stdin=subprocess.PIPE, |
| 838 | stdout=subprocess.PIPE) |
| 839 | signature, _ = child.communicate(input=padded_metadata_hash) |
| 840 | return signature |
| 841 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 842 | def HandleUpdatePing(self, data, label=''): |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 843 | """Handles an update ping from an update client. |
| 844 | |
| 845 | Args: |
| 846 | data: XML blob from client. |
| 847 | label: optional label for the update. |
Gilad Arnold | d8d595c | 2014-03-21 13:00:41 -0700 | [diff] [blame] | 848 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 849 | Returns: |
| 850 | Update payload message for client. |
| 851 | """ |
| 852 | # Get the static url base that will form that base of our update url e.g. |
| 853 | # http://hostname:8080/static/update.gz. |
| 854 | static_urlbase = self._GetStaticUrl() |
| 855 | |
| 856 | # Parse the XML we got into the components we care about. |
| 857 | protocol, app, event, update_check = autoupdate_lib.ParseUpdateRequest(data) |
| 858 | |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 859 | # Process attributes of the update check. |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 860 | request_attrs = self._ProcessUpdateComponents(app, event) |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 861 | |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 862 | if not update_check: |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 863 | if ((request_attrs.event_type == |
| 864 | autoupdate_lib.EVENT_TYPE_UPDATE_DOWNLOAD_STARTED) and |
| 865 | request_attrs.event_result == autoupdate_lib.EVENT_RESULT_SUCCESS): |
| 866 | with self._update_count_lock: |
| 867 | if self.max_updates == 0: |
| 868 | _Log('Received too many download_started notifications. This ' |
| 869 | 'probably means a bug in the test environment, such as too ' |
| 870 | 'many clients running concurrently. Alternatively, it could ' |
| 871 | 'be a bug in the update client.') |
| 872 | elif self.max_updates > 0: |
| 873 | self.max_updates -= 1 |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 874 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 875 | _Log('A non-update event notification received. Returning an ack.') |
| 876 | return autoupdate_lib.GetEventResponse(protocol) |
| 877 | |
| 878 | if request_attrs.forced_update_label: |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 879 | if label: |
| 880 | _Log('Label: %s set but being overwritten to %s by request', label, |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 881 | request_attrs.forced_update_label) |
| 882 | label = request_attrs.forced_update_label |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 883 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 884 | # Make sure that we did not already exceed the max number of allowed update |
| 885 | # responses. Note that the counter is only decremented when the client |
| 886 | # reports an actual download, to avoid race conditions between concurrent |
| 887 | # update requests from the same client due to a timeout. |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 888 | if self.max_updates == 0: |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 889 | _Log('Request received but max number of updates already served.') |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 890 | return autoupdate_lib.GetNoUpdateResponse(protocol) |
| 891 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 892 | _Log('Update Check Received. Client is using protocol version: %s', |
| 893 | protocol) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 894 | |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 895 | # Finally its time to generate the omaha response to give to client that |
| 896 | # lets them know where to find the payload and its associated metadata. |
| 897 | metadata_obj = None |
| 898 | |
| 899 | try: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 900 | # Are we provisioning a remote or local payload? |
| 901 | if self.remote_payload: |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 902 | |
| 903 | self._CheckOmahaRequest(app) |
| 904 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 905 | # If no explicit label was provided, use the value of --payload. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 906 | if not label: |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 907 | label = self.payload_path |
Chris Sosa | 0356d3b | 2010-09-16 15:46:22 -0700 | [diff] [blame] | 908 | |
Chris Sosa | 52f15bc | 2013-08-13 17:14:15 -0700 | [diff] [blame] | 909 | # TODO(sosa): Remove backwards-compatible hack. |
Chris Sosa | b26b120 | 2013-08-16 16:40:55 -0700 | [diff] [blame] | 910 | if not '.bin' in label: |
Chris Sosa | 52f15bc | 2013-08-13 17:14:15 -0700 | [diff] [blame] | 911 | url = _NonePathJoin(static_urlbase, label, 'update.gz') |
| 912 | else: |
| 913 | url = _NonePathJoin(static_urlbase, label) |
Chris Sosa | 5d342a2 | 2010-09-28 16:54:41 -0700 | [diff] [blame] | 914 | |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 915 | # Get remote payload attributes. |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 916 | metadata_obj = self._GetRemotePayloadAttrs(url) |
Gilad Arnold | 0c9c860 | 2012-10-02 23:58:58 -0700 | [diff] [blame] | 917 | else: |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 918 | path_to_payload = self.GetPathToPayload( |
| 919 | label, request_attrs.client_version, request_attrs.board) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 920 | url = _NonePathJoin(static_urlbase, path_to_payload, |
joychen | 7c2054a | 2013-07-25 11:14:07 -0700 | [diff] [blame] | 921 | constants.UPDATE_FILE) |
joychen | 121fc9b | 2013-08-02 14:30:30 -0700 | [diff] [blame] | 922 | local_payload_dir = _NonePathJoin(self.static_dir, path_to_payload) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 923 | metadata_obj = self.GetLocalPayloadAttrs(local_payload_dir) |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 924 | except AutoupdateError as e: |
| 925 | # Raised if we fail to generate an update payload. |
| 926 | _Log('Failed to process an update: %r', e) |
| 927 | return autoupdate_lib.GetNoUpdateResponse(protocol) |
| 928 | |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 929 | # Sign the metadata hash, if requested. |
| 930 | signed_metadata_hash = None |
| 931 | if self.private_key_for_metadata_hash_signature: |
| 932 | signed_metadata_hash = base64.b64encode(Autoupdate._SignMetadataHash( |
| 933 | self.private_key_for_metadata_hash_signature, |
| 934 | base64.b64decode(metadata_obj.metadata_hash))) |
| 935 | |
| 936 | # Include public key, if requested. |
| 937 | public_key_data = None |
| 938 | if self.public_key: |
| 939 | public_key_data = base64.b64encode(open(self.public_key, 'r').read()) |
| 940 | |
Chris Sosa | 4b95160 | 2014-04-09 20:26:07 -0700 | [diff] [blame] | 941 | update_response = autoupdate_lib.GetUpdateResponse( |
Chris Sosa | 6a3697f | 2013-01-29 16:44:43 -0800 | [diff] [blame] | 942 | metadata_obj.sha1, metadata_obj.sha256, metadata_obj.size, url, |
David Zeuthen | 52ccd01 | 2013-10-31 12:58:26 -0700 | [diff] [blame] | 943 | metadata_obj.is_delta_format, metadata_obj.metadata_size, |
| 944 | signed_metadata_hash, public_key_data, protocol, self.critical_update) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 945 | |
Gilad Arnold | e7819e7 | 2014-03-21 12:50:48 -0700 | [diff] [blame] | 946 | _Log('Responding to client to use url %s to get image', url) |
Gilad Arnold | d0c7175 | 2013-12-06 11:48:45 -0800 | [diff] [blame] | 947 | return update_response |
| 948 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 949 | def HandleHostInfoPing(self, ip): |
| 950 | """Returns host info dictionary for the given IP in JSON format.""" |
| 951 | assert ip, 'No ip provided.' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 952 | if ip in self.host_infos.table: |
| 953 | return json.dumps(self.host_infos.GetHostInfo(ip).attrs) |
| 954 | |
| 955 | def HandleHostLogPing(self, ip): |
| 956 | """Returns a complete log of events for host in JSON format.""" |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 957 | # If all events requested, return a dictionary of logs keyed by IP address. |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 958 | if ip == 'all': |
| 959 | return json.dumps( |
| 960 | dict([(key, self.host_infos.table[key].log) |
| 961 | for key in self.host_infos.table])) |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 962 | |
| 963 | # Otherwise we're looking for a specific IP address, so find its log. |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 964 | if ip in self.host_infos.table: |
| 965 | return json.dumps(self.host_infos.GetHostInfo(ip).log) |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 966 | |
Gilad Arnold | 4ba437d | 2012-10-05 15:28:27 -0700 | [diff] [blame] | 967 | # If no events were logged for this IP, return an empty log. |
| 968 | return json.dumps([]) |
| 969 | |
Dale Curtis | c9aaf3a | 2011-08-09 15:47:40 -0700 | [diff] [blame] | 970 | def HandleSetUpdatePing(self, ip, label): |
| 971 | """Sets forced_update_label for a given host.""" |
| 972 | assert ip, 'No ip provided.' |
| 973 | assert label, 'No label provided.' |
Gilad Arnold | 286a006 | 2012-01-12 13:47:02 -0800 | [diff] [blame] | 974 | self.host_infos.GetInitHostInfo(ip).attrs['forced_update_label'] = label |