eager_package_config/
omaha_client.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2022 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use anyhow::{Context as _, Error};
use channel_config::ChannelConfigs;
use fuchsia_url::UnpinnedAbsolutePackageUrl;
use omaha_client::cup_ecdsa::PublicKeys;
use serde::{Deserialize, Serialize};
use std::io;

const EAGER_PACKAGE_CONFIG_PATH: &str = "/config/data/eager_package_config.json";

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct OmahaServer {
    pub service_url: String,
    pub public_keys: PublicKeys,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct EagerPackageConfig {
    pub url: UnpinnedAbsolutePackageUrl,
    pub flavor: Option<String>,
    pub channel_config: ChannelConfigs,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct EagerPackageConfigs {
    pub server: OmahaServer,
    pub packages: Vec<EagerPackageConfig>,
}

#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
pub struct EagerPackageConfigsJson {
    pub eager_package_configs: Vec<EagerPackageConfigs>,
}

impl EagerPackageConfigs {
    /// Load the eager package config from namespace.
    pub fn from_namespace() -> Result<Self, Error> {
        let file = std::fs::File::open(EAGER_PACKAGE_CONFIG_PATH)
            .context("opening eager package config file")?;
        Self::from_reader(io::BufReader::new(file))
    }

    fn from_reader(reader: impl io::Read) -> Result<Self, Error> {
        let eager_package_configs_json: EagerPackageConfigsJson =
            serde_json::from_reader(reader).context("parsing eager package config")?;
        // Omaha client only supports one Omaha server at a time; just take the
        // first server config in this file.
        if eager_package_configs_json.eager_package_configs.len() > 1 {
            log::error!(
                "Warning: this eager package config JSON file contained more \
                than one Omaha server config, but omaha-client only supports \
                one Omaha server."
            );
        }
        let eager_package_configs =
            eager_package_configs_json.eager_package_configs.into_iter().next().ok_or_else(
                || {
                    anyhow::anyhow!(
                        "Eager package config JSON did not contain any server-and-package configs."
                    )
                },
            )?;
        for package in &eager_package_configs.packages {
            package.channel_config.validate().context("validating eager package channel config")?;
        }

        Ok(eager_package_configs)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;
    use channel_config::ChannelConfig;
    use omaha_client::cup_ecdsa::test_support::{
        make_default_json_public_keys_for_test, make_default_public_key_for_test,
        make_default_public_key_id_for_test,
    };
    use omaha_client::cup_ecdsa::PublicKeyAndId;
    use pretty_assertions::assert_eq;

    #[test]
    fn parse_eager_package_configs_json() {
        let json = serde_json::json!(
        {
            "eager_package_configs": [
                {
                    "server": {
                        "service_url": "https://example.com",
                        "public_keys": make_default_json_public_keys_for_test(),
                    },
                    "packages":
                    [
                        {
                            "url": "fuchsia-pkg://example.com/package",
                            "flavor": "debug",
                            "channel_config":
                                {
                                    "channels":
                                        [
                                            {
                                                "name": "stable",
                                                "repo": "stable",
                                                "appid": "1a2b3c4d"
                                            },
                                            {
                                                "name": "beta",
                                                "repo": "beta",
                                                "appid": "1a2b3c4d"
                                            },
                                            {
                                                "name": "alpha",
                                                "repo": "alpha",
                                                "appid": "1a2b3c4d"
                                            },
                                            {
                                                "name": "test",
                                                "repo": "test",
                                                "appid": "2b3c4d5e"
                                            }
                                        ],
                                    "default_channel": "stable"
                                }
                        },
                        {
                            "url": "fuchsia-pkg://example.com/package2",
                            "channel_config":
                                {
                                    "channels":
                                        [
                                            {
                                                "name": "stable",
                                                "repo": "stable",
                                                "appid": "3c4d5e6f"
                                            }
                                        ]
                                }
                        }
                    ]
                }
            ]
        });

        assert_eq!(
            EagerPackageConfigs::from_reader(json.to_string().as_bytes()).unwrap(),
            EagerPackageConfigs {
                server: OmahaServer {
                    service_url: "https://example.com".into(),
                    public_keys: PublicKeys {
                        latest: PublicKeyAndId {
                            id: make_default_public_key_id_for_test(),
                            key: make_default_public_key_for_test(),
                        },
                        historical: vec![],
                    }
                },
                packages: vec![
                    EagerPackageConfig {
                        url: UnpinnedAbsolutePackageUrl::parse("fuchsia-pkg://example.com/package")
                            .unwrap(),
                        flavor: Some("debug".into()),
                        channel_config: ChannelConfigs {
                            default_channel: Some("stable".into()),
                            known_channels: vec![
                                ChannelConfig {
                                    name: "stable".into(),
                                    repo: "stable".into(),
                                    appid: Some("1a2b3c4d".into()),
                                    check_interval_secs: None,
                                },
                                ChannelConfig {
                                    name: "beta".into(),
                                    repo: "beta".into(),
                                    appid: Some("1a2b3c4d".into()),
                                    check_interval_secs: None,
                                },
                                ChannelConfig {
                                    name: "alpha".into(),
                                    repo: "alpha".into(),
                                    appid: Some("1a2b3c4d".into()),
                                    check_interval_secs: None,
                                },
                                ChannelConfig {
                                    name: "test".into(),
                                    repo: "test".into(),
                                    appid: Some("2b3c4d5e".into()),
                                    check_interval_secs: None,
                                },
                            ]
                        },
                    },
                    EagerPackageConfig {
                        url: UnpinnedAbsolutePackageUrl::parse(
                            "fuchsia-pkg://example.com/package2"
                        )
                        .unwrap(),
                        flavor: None,
                        channel_config: ChannelConfigs {
                            default_channel: None,
                            known_channels: vec![ChannelConfig {
                                name: "stable".into(),
                                repo: "stable".into(),
                                appid: Some("3c4d5e6f".into()),
                                check_interval_secs: None,
                            },]
                        },
                    },
                ]
            }
        );
    }

    #[test]
    fn parse_eager_package_configs_json_reject_invalid() {
        let json = br#"
        {
            "eager_package_configs": [
                {
                    "server": {},
                    "packages":
                    [
                        {
                            "url": "fuchsia-pkg://example.com/package",
                            "channel_config":
                                {
                                    "channels":
                                        [
                                            {
                                                "name": "stable",
                                                "repo": "stable",
                                            }
                                        ],
                                    "default_channel": "invalid"
                                }
                        }
                    ]
                }
            ]
        }"#;
        assert_matches!(EagerPackageConfigs::from_reader(&json[..]), Err(_));
    }
}