omaha_client/protocol/request.rs
1// Copyright 2019 The Fuchsia Authors
2//
3// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6// This file may not be copied, modified, or distributed except according to
7// those terms.
8
9use crate::protocol::Cohort;
10use serde::{Serialize, Serializer};
11use serde_repr::Serialize_repr;
12use std::collections::HashMap;
13
14#[cfg(test)]
15mod tests;
16
17/// This is the key for the http request header that identifies the 'updater' that is sending a
18/// request.
19pub const HEADER_UPDATER_NAME: &str = "X-Goog-Update-Updater";
20
21/// This is the key for the http request header that identifies whether this is an interactive
22/// or a background update (see InstallSource).
23pub const HEADER_INTERACTIVITY: &str = "X-Goog-Update-Interactivity";
24
25/// This is the key for the http request header that identifies the app id(s) that are included in
26/// this request.
27pub const HEADER_APP_ID: &str = "X-Goog-Update-AppId";
28
29/// An Omaha protocol request.
30///
31/// This holds the data for constructing a request to the Omaha service.
32///
33/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#request
34#[derive(Debug, Default, Serialize)]
35pub struct Request {
36 /// The current Omaha protocol version (which this is meant to be used with, is 3.0. This
37 /// should always be set to "3.0".
38 ///
39 /// This is the 'protocol' attribute of the request object.
40 #[serde(rename = "protocol")]
41 pub protocol_version: String,
42
43 /// This is the string identifying the updater software itself (this client). e.g. "fuchsia"
44 pub updater: String,
45
46 /// The version of the updater itself (e.g. "Fuchsia/Rust-0.0.0.1"). This is the version of the
47 /// updater implemented using this Crate.
48 ///
49 /// This is the 'updaterversion' attribute of the request object.
50 #[serde(rename = "updaterversion")]
51 pub updater_version: String,
52
53 /// The install source trigger for this request.
54 #[serde(rename = "installsource")]
55 pub install_source: InstallSource,
56
57 /// The system update is always done by "the machine" aka system-level or administrator
58 /// privileges.
59 ///
60 /// This is the 'ismachine' attribute of the request object.
61 #[serde(rename = "ismachine")]
62 pub is_machine: bool,
63
64 /// The randomly generated GUID for a single Omaha request.
65 ///
66 /// This is the 'requestid' attribute of the request object.
67 #[serde(rename = "requestid")]
68 #[serde(skip_serializing_if = "Option::is_none")]
69 pub request_id: Option<GUID>,
70
71 /// The randomly generated GUID for all Omaha requests in an update session.
72 ///
73 /// This is the 'sessionid' attribute of the request object.
74 #[serde(rename = "sessionid")]
75 #[serde(skip_serializing_if = "Option::is_none")]
76 pub session_id: Option<GUID>,
77
78 /// Information about the device operating system.
79 ///
80 /// This is the 'os' child object of the request object.
81 pub os: OS,
82
83 /// The applications to update.
84 ///
85 /// These are the 'app' children objects of the request object
86 #[serde(rename = "app")]
87 pub apps: Vec<App>,
88}
89
90/// This is a serialization wrapper for a Request, as a Request object serializes into a value,
91/// for an object, not an object that is '{"request": {....} }'. This wrapper provides the request
92/// wrapping that Omaha expects to see.
93#[derive(Debug, Default, Serialize)]
94pub struct RequestWrapper {
95 pub request: Request,
96}
97
98/// Enum of the possible reasons that this update request was initiated.
99#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
100#[serde(rename_all = "lowercase")]
101pub enum InstallSource {
102 /// This update check was triggered "on demand", by a user.
103 OnDemand,
104
105 /// This update check was triggered as part of a background task, unattended by a user.
106 #[default]
107 ScheduledTask,
108}
109
110/// Information about the platform / operating system.
111///
112/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#os
113#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
114pub struct OS {
115 /// The device platform (e.g. 'Fuchsia')
116 pub platform: String,
117
118 /// The version of the platform
119 pub version: String,
120
121 /// The patch level of the platform (e.g. "12345_arm64")
122 #[serde(rename = "sp")]
123 pub service_pack: String,
124
125 /// The platform architecture (e.g. "x86-64")
126 pub arch: String,
127}
128
129/// Information about an individual app that an update check is being performed for.
130///
131/// While unlikely, it's possible for a single request to have an update check, a ping, and for it
132/// to be reporting an event.
133///
134/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#app-request
135#[derive(Debug, Default, Clone, Serialize)]
136pub struct App {
137 /// This is the GUID or product ID that uniquely identifies the product to Omaha.
138 ///
139 /// This is the 'appid' attribute of the app object.
140 #[serde(rename = "appid")]
141 pub id: String,
142
143 /// The version of the product that's currently installed. This is in 'A.B.C.D' format.
144 ///
145 /// This is the version attribute of the app object.
146 pub version: String,
147
148 /// The fingerprint for the application.
149 ///
150 /// This is the fp attribute of the app object.
151 #[serde(rename = "fp")]
152 #[serde(skip_serializing_if = "Option::is_none")]
153 pub fingerprint: Option<String>,
154
155 /// This is the cohort id, as previously assigned by the Omaha service. This is a machine-
156 /// readable string, not meant for user display.
157 ///
158 /// This holds the following fields of the app object:
159 /// cohort
160 /// cohorthint
161 /// cohortname
162 #[serde(flatten)]
163 pub cohort: Option<Cohort>,
164
165 /// If present, this request is an update check.
166 #[serde(rename = "updatecheck")]
167 #[serde(skip_serializing_if = "Option::is_none")]
168 pub update_check: Option<UpdateCheck>,
169
170 /// These are events to report to Omaha.
171 #[serde(rename = "event")]
172 #[serde(skip_serializing_if = "Vec::is_empty")]
173 pub events: Vec<Event>,
174
175 /// An optional status ping.
176 #[serde(skip_serializing_if = "Option::is_none")]
177 pub ping: Option<Ping>,
178
179 /// Extra fields to include (App-specific fields used to extend the protocol).
180 ///
181 /// # NOTE: Can break the omaha protocol if improperly used.
182 ///
183 /// This is listed last in the struct, and should remain so, due to how Serde behaves when
184 /// flattening fields into the parent. If this map contains a field whose name matches that of
185 /// another field in the struct (such as `id`), it will overwrite that field. If that field is
186 /// optionally serialized (such as `update_check`), it will still overwrite that field
187 /// (regardless of the presence or not of the field it's overwriting).
188 #[serde(flatten)]
189 pub extra_fields: HashMap<String, String>,
190}
191
192/// This is an update check for the parent App object.
193///
194/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#updatecheck-request
195#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize)]
196pub struct UpdateCheck {
197 /// If the update is disabled, the client will not honor an 'update' response. The default
198 /// value of false indicates that the client will attempt an update if instructed that one is
199 /// available.
200 #[serde(skip_serializing_if = "std::ops::Not::not")]
201 #[serde(rename = "updatedisabled")]
202 pub disabled: bool,
203
204 /// If true, Omaha will offer an update even if the client is already running the same version.
205 #[serde(skip_serializing_if = "std::ops::Not::not")]
206 #[serde(rename = "sameversionupdate")]
207 pub offer_update_if_same_version: bool,
208}
209
210impl UpdateCheck {
211 /// Public constructor for an update check request on an app that will not honor an 'update'
212 /// response and will not perform an update if one is available.
213 pub fn disabled() -> Self {
214 UpdateCheck {
215 disabled: true,
216 offer_update_if_same_version: false,
217 }
218 }
219}
220
221/// This is a status ping to the Omaha service.
222///
223/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#ping-request
224///
225/// These pings only support the Client-Regulated Counting method (Date-based). For more info, see
226/// https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#client-regulated-Counting-days-based
227#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize)]
228pub struct Ping {
229 /// This is the January 1, 2007 epoch-based value for the date that was previously sent to the
230 /// client by the service, as the elapsed_days value of the daystart object, if the application
231 /// is active.
232 ///
233 /// This is the 'ad' attribute of the ping object.
234 #[serde(rename = "ad")]
235 #[serde(skip_serializing_if = "Option::is_none")]
236 pub date_last_active: Option<u32>,
237
238 /// This is the January 1, 2007 epoch-based value for the date that was previously sent to the
239 /// client by the service, as the elapsed_days value of the daystart object, if the application
240 /// is active or not.
241 ///
242 /// This is the 'rd' attribute of the ping object.
243 #[serde(rename = "rd")]
244 #[serde(skip_serializing_if = "Option::is_none")]
245 pub date_last_roll_call: Option<u32>,
246}
247
248/// An event that is being reported to the Omaha service.
249///
250/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#event-request
251#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize)]
252pub struct Event {
253 /// This is the event type for the event (see the enum for more information).
254 ///
255 /// This is the eventtype attribute of the event object.
256 #[serde(rename = "eventtype")]
257 pub event_type: EventType,
258
259 /// This is the result code for the event. All event types share a namespace for result codes.
260 ///
261 /// This is the eventresult attribute of the event object.
262 #[serde(rename = "eventresult")]
263 pub event_result: EventResult,
264
265 /// This is an opaque error value that may be provided. It's meaning is application specific.
266 ///
267 /// This is the errorcode attribute of the event object.
268 #[serde(skip_serializing_if = "Option::is_none")]
269 pub errorcode: Option<EventErrorCode>,
270
271 /// The version of the app that was present on the machine at the time of the update-check of
272 /// this update flow, regardless of the success or failure of the update operation.
273 #[serde(skip_serializing_if = "Option::is_none")]
274 #[serde(rename = "previousversion")]
275 pub previous_version: Option<String>,
276
277 /// The version of the app that the update flow to which this event belongs attempted to
278 /// reach, regardless of success or failure of the update operation.
279 #[serde(skip_serializing_if = "Option::is_none")]
280 #[serde(rename = "nextversion")]
281 pub next_version: Option<String>,
282
283 /// For events representing a download, the time elapsed between the start of the download and
284 /// the end of the download, in milliseconds. For events representing an entire update flow,
285 /// the sum of all such download times over the course of the update flow.
286 /// Sent in <event>s that have an eventtype of "1", "2", "3", and "14" only.
287 #[serde(skip_serializing_if = "Option::is_none")]
288 pub download_time_ms: Option<u64>,
289}
290
291impl Event {
292 /// Creates a new successful event for the given event type.
293 pub fn success(event_type: EventType) -> Self {
294 Self {
295 event_type,
296 event_result: EventResult::Success,
297 ..Self::default()
298 }
299 }
300
301 /// Creates a new error event for the given event error code.
302 pub fn error(errorcode: EventErrorCode) -> Self {
303 Self {
304 event_type: EventType::UpdateComplete,
305 event_result: EventResult::Error,
306 errorcode: Some(errorcode),
307 ..Self::default()
308 }
309 }
310}
311
312/// The type of event that is being reported. These are specified by the Omaha protocol.
313///
314/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#event-request
315#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize_repr)]
316#[repr(u8)]
317pub enum EventType {
318 #[default]
319 Unknown = 0,
320
321 /// The initial download of the application is complete.
322 DownloadComplete = 1,
323
324 /// The initial installation of the application is complete.
325 InstallComplete = 2,
326
327 /// The application update is complete.
328 UpdateComplete = 3,
329
330 /// The download of the update for the application has started.
331 UpdateDownloadStarted = 13,
332
333 /// The download of the update for the application is complete.
334 UpdateDownloadFinished = 14,
335
336 /// The application is now using the updated software. This is sent after a successful boot
337 /// into the update software.
338 RebootedAfterUpdate = 54,
339}
340
341/// The result of event that is being reported. These are specified by the Omaha protocol.
342///
343/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#event-request
344#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize_repr)]
345#[repr(u8)]
346pub enum EventResult {
347 #[default]
348 Error = 0,
349 Success = 1,
350 SuccessAndRestartRequired = 2,
351 SuccessAndAppRestartRequired = 3,
352 Cancelled = 4,
353 ErrorInSystemInstaller = 8,
354
355 /// The client acknowledges that it received the 'update' response, but it will not be acting
356 /// on the update at this time (deferred by Policy).
357 UpdateDeferred = 9,
358}
359
360/// The error code of the event. These are application specific.
361#[derive(Debug, Default, Clone, Eq, PartialEq, Serialize_repr)]
362#[repr(i32)]
363pub enum EventErrorCode {
364 /// Error when parsing Omaha response.
365 #[default]
366 ParseResponse = 0,
367 /// Error when constructing install plan.
368 ConstructInstallPlan = 1,
369 /// Error when installing the update.
370 Installation = 2,
371 /// The update is denied by policy.
372 DeniedByPolicy = 3,
373}
374
375/// The GUID used in Omaha protocol for sessionid and requestid.
376///
377/// See https://github.com/google/omaha/blob/HEAD/doc/ServerProtocolV3.md#guids
378#[derive(Debug, Default, Clone, Eq, PartialEq)]
379pub struct GUID {
380 uuid: uuid::Uuid,
381}
382
383impl GUID {
384 /// Creates a new random GUID.
385 #[cfg(not(test))]
386 pub fn new() -> Self {
387 Self {
388 uuid: uuid::Uuid::new_v4(),
389 }
390 }
391
392 // For unit tests, creates GUID using a thread local counter, so that for every test case,
393 // the first GUID will be {00000000-0000-0000-0000-000000000000},
394 // and the second will be {00000000-0000-0000-0000-000000000001}, and so on.
395 #[cfg(test)]
396 pub fn new() -> Self {
397 thread_local! {
398 static COUNTER: std::cell::RefCell<u128> =
399 const { std::cell::RefCell::new(0) };
400 }
401 COUNTER.with(|counter| {
402 let mut counter = counter.borrow_mut();
403 let guid = Self::from_u128(*counter);
404 *counter += 1;
405 guid
406 })
407 }
408
409 #[cfg(test)]
410 pub fn from_u128(n: u128) -> Self {
411 Self {
412 uuid: uuid::Uuid::from_u128(n),
413 }
414 }
415}
416
417// Wrap the uuid in {}.
418impl Serialize for GUID {
419 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
420 where
421 S: Serializer,
422 {
423 self.uuid.as_braced().serialize(serializer)
424 }
425}