Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 1 | # -*- coding: utf-8 -*- |
Mike Frysinger | 8b0fc37 | 2022-09-08 07:24:24 | [diff] [blame] | 2 | # Copyright 2011 The ChromiumOS Authors |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 3 | # Use of this source code is governed by a BSD-style license that can be |
| 4 | # found in the LICENSE file. |
| 5 | |
| 6 | """Devserver module for handling update client requests.""" |
| 7 | |
| 8 | from __future__ import print_function |
| 9 | |
Chris McDonald | 771446e | 2021-08-05 20:23:08 | [diff] [blame] | 10 | import logging |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 11 | import os |
| 12 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 13 | import cherrypy # pylint: disable=import-error |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 14 | from six.moves import urllib |
| 15 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 16 | |
| 17 | # TODO(crbug.com/872441): We try to import nebraska from different places |
| 18 | # because when we install the devserver, we copy the nebraska.py into the main |
| 19 | # directory. Once this bug is resolved, we can always import from nebraska |
| 20 | # directory. |
| 21 | try: |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 22 | from nebraska import nebraska |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 23 | except ImportError: |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 24 | import nebraska |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 25 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 26 | |
| 27 | # Module-local log function. |
| 28 | def _Log(message, *args): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 29 | return logging.info(message, *args) |
Amin Hassani | 3587fb3 | 2021-04-28 17:10:01 | [diff] [blame] | 30 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 31 | |
| 32 | class AutoupdateError(Exception): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 33 | """Exception classes used by this module.""" |
| 34 | |
| 35 | # pylint: disable=unnecessary-pass |
| 36 | pass |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def _ChangeUrlPort(url, new_port): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 40 | """Return the URL passed in with a different port""" |
| 41 | scheme, netloc, path, query, fragment = urllib.parse.urlsplit(url) |
| 42 | host_port = netloc.split(":") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 43 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 44 | if len(host_port) == 1: |
| 45 | host_port.append(new_port) |
| 46 | else: |
| 47 | host_port[1] = new_port |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 48 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 49 | print(host_port) |
| 50 | netloc = "%s:%s" % tuple(host_port) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 51 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 52 | # pylint: disable=too-many-function-args |
| 53 | return urllib.parse.urlunsplit((scheme, netloc, path, query, fragment)) |
| 54 | |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 55 | |
| 56 | def _NonePathJoin(*args): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 57 | """os.path.join that filters None's from the argument list.""" |
| 58 | return os.path.join(*[x for x in args if x is not None]) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 59 | |
| 60 | |
| 61 | class Autoupdate(object): |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 62 | """Class that contains functionality that handles Chrome OS update pings.""" |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 63 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 64 | def __init__(self, xbuddy, static_dir=None): |
| 65 | """Initializes the class. |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 66 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 67 | Args: |
| 68 | xbuddy: The xbuddy path. |
| 69 | static_dir: The path to the devserver static directory. |
| 70 | """ |
| 71 | self.xbuddy = xbuddy |
| 72 | self.static_dir = static_dir |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 73 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 74 | def GetDevserverUrl(self): |
| 75 | """Returns the devserver url base.""" |
| 76 | x_forwarded_host = cherrypy.request.headers.get("X-Forwarded-Host") |
| 77 | if x_forwarded_host: |
| 78 | # Select the left most <ip>:<port> value so that the request is |
| 79 | # forwarded correctly. |
| 80 | x_forwarded_host = [x.strip() for x in x_forwarded_host.split(",")][ |
| 81 | 0 |
| 82 | ] |
| 83 | hostname = "http://" + x_forwarded_host |
| 84 | else: |
| 85 | hostname = cherrypy.request.base |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 86 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 87 | return hostname |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 88 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 89 | def GetStaticUrl(self): |
| 90 | """Returns the static url base that should prefix all payload responses.""" |
| 91 | hostname = self.GetDevserverUrl() |
| 92 | _Log("Handling update ping as %s", hostname) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 93 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 94 | static_urlbase = "%s/static" % hostname |
| 95 | _Log("Using static url base %s", static_urlbase) |
| 96 | return static_urlbase |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 97 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 98 | def GetBuildID(self, label, board): |
| 99 | """Find the build id of the given lable and board. |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 100 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 101 | Args: |
| 102 | label: from update request |
| 103 | board: from update request |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 104 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 105 | Returns: |
| 106 | The build id of given label and board. e.g. reef-release/R88-13591.0.0 |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 107 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 108 | Raises: |
| 109 | AutoupdateError: If the update could not be found. |
| 110 | """ |
| 111 | label = label or "" |
| 112 | label_list = label.split("/") |
| 113 | if label_list[0] == "xbuddy": |
| 114 | # If path explicitly calls xbuddy, pop off the tag. |
| 115 | label_list.pop() |
| 116 | x_label, _ = self.xbuddy.Translate(label_list, board=board) |
| 117 | return x_label |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 118 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 119 | def HandleUpdatePing(self, data, label="", **kwargs): |
| 120 | """Handles an update ping from an update client. |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 121 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 122 | Args: |
| 123 | data: XML blob from client. |
| 124 | label: optional label for the update. |
| 125 | kwargs: The map of query strings passed to the /update API. |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 126 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 127 | Returns: |
| 128 | Update payload message for client. |
| 129 | """ |
| 130 | # Change the URL's string query dictionary provided by cherrypy to a valid |
| 131 | # dictionary that has proper values for its keys. e.g. True instead of |
| 132 | # 'True'. |
| 133 | kwargs = nebraska.QueryDictToDict(kwargs) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 134 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 135 | # Process attributes of the update check. |
| 136 | request = nebraska.Request(data) |
| 137 | if request.request_type == nebraska.Request.RequestType.EVENT: |
| 138 | _Log("A non-update event notification received. Returning an ack.") |
| 139 | n = nebraska.Nebraska() |
| 140 | n.UpdateConfig(**kwargs) |
| 141 | return n.GetResponseToRequest(request) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 142 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 143 | _Log("Update Check Received.") |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 144 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 145 | try: |
| 146 | build_id = self.GetBuildID(label, request.board) |
| 147 | base_url = "/".join((self.GetStaticUrl(), build_id)) |
| 148 | local_payload_dir = _NonePathJoin(self.static_dir, build_id) |
| 149 | except AutoupdateError as e: |
| 150 | # Raised if we fail to generate an update payload. |
| 151 | _Log( |
| 152 | "Failed to process an update request, but we will defer to " |
| 153 | "nebraska to respond with no-update. The error was %s", |
| 154 | e, |
| 155 | ) |
Amin Hassani | 2aa3428 | 2020-11-18 01:18:19 | [diff] [blame] | 156 | |
Jack Rosenthal | 8de609d | 2023-02-09 20:20:35 | [diff] [blame] | 157 | _Log("Responding to client to use url %s to get image", base_url) |
| 158 | n = nebraska.Nebraska() |
| 159 | n.UpdateConfig( |
| 160 | update_payloads_address=base_url, |
| 161 | update_app_index=nebraska.AppIndex(local_payload_dir), |
| 162 | ) |
| 163 | return n.GetResponseToRequest(request) |