autoupdate: Return response with client's app ID

Currently in autoupdate we sent a default hard coded app ID to the
client. However, this is not the bahvior of omaha, as omaha always, reply with
the same app ID requested by the user. This CL, modifes this bahavior in
autoupdate to return the app ID associated with the client's request.

BUG=chromium:821514
TEST=cros flash

Change-Id: I7cd36c8e7ed6e36bed257a5373e62483136b585e
Reviewed-on: https://chromium-review.googlesource.com/961585
Commit-Ready: Amin Hassani <ahassani@chromium.org>
Tested-by: Amin Hassani <ahassani@chromium.org>
Reviewed-by: Xixuan Wu <xixuan@chromium.org>
Reviewed-by: Don Garrett <dgarrett@chromium.org>
(cherry picked from commit d7a913a3455a4e857a11889516f9c7bf6204bf23)
Reviewed-on: https://chromium-review.googlesource.com/1043166
Reviewed-by: Keith Haddow <haddowk@chromium.org>
Commit-Queue: Keith Haddow <haddowk@chromium.org>
Tested-by: Keith Haddow <haddowk@chromium.org>
diff --git a/autoupdate.py b/autoupdate.py
index ca5dd1e..34fc4bb 100644
--- a/autoupdate.py
+++ b/autoupdate.py
@@ -874,6 +874,7 @@
 
     # Parse the XML we got into the components we care about.
     protocol, app, event, update_check = autoupdate_lib.ParseUpdateRequest(data)
+    appid = app.getAttribute('appid')
 
     # Process attributes of the update check.
     request_attrs = self._ProcessUpdateComponents(app, event)
@@ -892,7 +893,7 @@
             self.max_updates -= 1
 
       _Log('A non-update event notification received. Returning an ack.')
-      return autoupdate_lib.GetEventResponse(protocol)
+      return autoupdate_lib.GetEventResponse(protocol, appid)
 
     if request_attrs.forced_update_label:
       if label:
@@ -906,7 +907,7 @@
     # update requests from the same client due to a timeout.
     if self.max_updates == 0:
       _Log('Request received but max number of updates already served.')
-      return autoupdate_lib.GetNoUpdateResponse(protocol)
+      return autoupdate_lib.GetNoUpdateResponse(protocol, appid)
 
     _Log('Update Check Received. Client is using protocol version: %s',
          protocol)
@@ -943,7 +944,7 @@
     except AutoupdateError as e:
       # Raised if we fail to generate an update payload.
       _Log('Failed to process an update: %r', e)
-      return autoupdate_lib.GetNoUpdateResponse(protocol)
+      return autoupdate_lib.GetNoUpdateResponse(protocol, appid)
 
     # Sign the metadata hash, if requested.
     signed_metadata_hash = None
@@ -960,7 +961,8 @@
     update_response = autoupdate_lib.GetUpdateResponse(
         metadata_obj.sha1, metadata_obj.sha256, metadata_obj.size, url,
         metadata_obj.is_delta_format, metadata_obj.metadata_size,
-        signed_metadata_hash, public_key_data, protocol, self.critical_update)
+        signed_metadata_hash, public_key_data, protocol, appid,
+        self.critical_update)
 
     _Log('Responding to client to use url %s to get image', url)
     return update_response
diff --git a/autoupdate_lib.py b/autoupdate_lib.py
index 23091e9..58d033b 100644
--- a/autoupdate_lib.py
+++ b/autoupdate_lib.py
@@ -13,7 +13,6 @@
 import time
 from xml.dom import minidom
 
-
 # Update events and result codes.
 EVENT_TYPE_UNKNOWN = 0
 EVENT_TYPE_DOWNLOAD_COMPLETE = 1
@@ -28,10 +27,6 @@
 EVENT_RESULT_UPDATE_DEFERRED = 9
 
 
-# A default app_id value.
-_APP_ID = '87efface-864d-49a5-9bb3-4b050a7c227a'
-
-
 # Responses for the various Omaha protocols indexed by the protocol version.
 #
 # Update available responses:
@@ -39,7 +34,7 @@
 _UPDATE_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <updatecheck
         ChromeOSVersion="999999.0.0"
@@ -56,7 +51,7 @@
 _UPDATE_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <response protocol="3.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <updatecheck status="ok">
         <urls>
@@ -85,7 +80,7 @@
 _NO_UPDATE_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <updatecheck status="noupdate"/>
     </app>
@@ -93,7 +88,7 @@
 _NO_UPDATE_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <response protocol="3.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <updatecheck status="noupdate"/>
     </app>
@@ -105,7 +100,7 @@
 _EVENT_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <event status="ok"/>
     </app>
@@ -113,7 +108,7 @@
 _EVENT_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?>
   <response protocol="3.0">
     <daystart elapsed_seconds="%(time_elapsed)s"/>
-    <app appid="{%(appid)s}" status="ok">
+    <app appid="%(appid)s" status="ok">
       <ping status="ok"/>
       <event status="ok"/>
     </app>
@@ -130,10 +125,10 @@
   return now[3] * 3600 + now[4] * 60 + now[5]
 
 
-def GetCommonResponseValues():
+def GetCommonResponseValues(appid):
   """Returns a dictionary of default values for the response."""
   response_values = {}
-  response_values['appid'] = _APP_ID
+  response_values['appid'] = appid
   response_values['time_elapsed'] = GetSecondsSinceMidnight()
   return response_values
 
@@ -154,7 +149,7 @@
 
 
 def GetUpdateResponse(sha1, sha256, size, url, is_delta_format, metadata_size,
-                      signed_metadata_hash, public_key, protocol,
+                      signed_metadata_hash, public_key, protocol, appid,
                       critical_update=False):
   """Returns a protocol-specific response to the client for a new update.
 
@@ -168,12 +163,13 @@
     signed_metadata_hash: the signed metadata hash or None if not signed.
     public_key: the public key to transmit to the client or None if no key.
     protocol: client's protocol version from the request Xml.
+    appid: the appid associated with the response.
     critical_update: whether this is a critical update.
 
   Returns:
     Xml string to be passed back to client.
   """
-  response_values = GetCommonResponseValues()
+  response_values = GetCommonResponseValues(appid)
   response_values['sha1'] = sha1
   response_values['sha256'] = sha256
   # sha256 is base64 encoded, encode it to hex.
@@ -203,29 +199,31 @@
   return GetSubstitutedResponse(_UPDATE_RESPONSE, protocol, response_values)
 
 
-def GetNoUpdateResponse(protocol):
+def GetNoUpdateResponse(protocol, appid):
   """Returns a protocol-specific response to the client for no update.
 
   Args:
     protocol: client's protocol version from the request Xml.
+    appid: the appid associated with the response.
 
   Returns:
     Xml string to be passed back to client.
   """
-  response_values = GetCommonResponseValues()
+  response_values = GetCommonResponseValues(appid)
   return GetSubstitutedResponse(_NO_UPDATE_RESPONSE, protocol, response_values)
 
 
-def GetEventResponse(protocol):
+def GetEventResponse(protocol, appid):
   """Returns a protocol-specific response to a client event notification.
 
   Args:
     protocol: client's protocol version from the request Xml.
+    appid: the appid associated with the response.
 
   Returns:
     Xml string to be passed back to client.
   """
-  response_values = GetCommonResponseValues()
+  response_values = GetCommonResponseValues(appid)
   return GetSubstitutedResponse(_EVENT_RESPONSE, protocol, response_values)