persistence_config/
lib.rs

1// Copyright 2020 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4use anyhow::{bail, Error};
5use glob::glob;
6use regex::Regex;
7use serde_derive::Deserialize;
8use std::borrow::Borrow;
9use std::collections::HashMap;
10use std::fmt::Display;
11use std::ops::Deref;
12use std::sync::LazyLock;
13
14/// The outer map is service_name; the inner is tag.
15pub type Config = HashMap<ServiceName, HashMap<Tag, TagConfig>>;
16
17/// Schema for config-file entries. Each config file is a JSON array of these.
18#[derive(Deserialize, Default, Debug, PartialEq)]
19#[cfg_attr(test, derive(Clone))]
20#[serde(deny_unknown_fields)]
21struct TaggedPersist {
22    /// The Inspect data defined here will be published under this tag.
23    /// Tags must not be duplicated within a service, even between files.
24    /// Tags must conform to /[a-z][a-z-]*/.
25    pub tag: String,
26    /// Each tag will only be requestable via a named service. Multiple tags can use the
27    /// same service name, which will be published and routed as DataPersistence_{service_name}.
28    /// Service names must conform to /[a-z][a-z-]*/.
29    pub service_name: String,
30    /// These selectors will be fetched and stored for publication on the next boot.
31    pub selectors: Vec<String>,
32    /// This is the max size of the file saved, which is the JSON-serialized version
33    /// of the selectors' data.
34    pub max_bytes: usize,
35    /// Persistence requests will be throttled to this. Requests received early will be delayed.
36    pub min_seconds_between_fetch: i64,
37    /// Should this tag persist across multiple reboots?
38    #[serde(default)]
39    pub persist_across_boot: bool,
40}
41
42/// Configuration for a single tag for a single service.
43///
44/// See [`TaggedPersist`] for the meaning of corresponding fields.
45#[derive(Debug, Eq, PartialEq)]
46pub struct TagConfig {
47    pub selectors: Vec<String>,
48    pub max_bytes: usize,
49    pub min_seconds_between_fetch: i64,
50    pub persist_across_boot: bool,
51}
52
53/// Wrapper class for a valid tag name.
54///
55/// This is a witness class that can only be constructed from a `String` that
56/// matches [`NAME_PATTERN`].
57#[derive(Clone, Debug, Eq, Hash, PartialEq)]
58pub struct Tag(String);
59
60/// Wrapper class for a valid service name.
61///
62/// This is a witness class that can only be constructed from a `String` that
63/// matches [`NAME_PATTERN`].
64#[derive(Clone, Debug, Eq, Hash, PartialEq)]
65pub struct ServiceName(String);
66
67/// A regular expression corresponding to a valid tag or service name.
68const NAME_PATTERN: &str = r"^[a-z][a-z-]*$";
69
70static NAME_VALIDATOR: LazyLock<Regex> = LazyLock::new(|| Regex::new(NAME_PATTERN).unwrap());
71
72impl Tag {
73    pub fn new(tag: impl Into<String>) -> Result<Self, Error> {
74        let tag = tag.into();
75        if !NAME_VALIDATOR.is_match(&tag) {
76            bail!("Invalid tag {} must match [a-z][a-z-]*", tag);
77        }
78        Ok(Self(tag))
79    }
80
81    pub fn as_str(&self) -> &str {
82        self.0.as_ref()
83    }
84}
85
86impl ServiceName {
87    pub fn new(name: String) -> Result<Self, Error> {
88        if !NAME_VALIDATOR.is_match(&name) {
89            bail!("Invalid service name {} must match [a-z][a-z-]*", name);
90        }
91        Ok(Self(name))
92    }
93}
94
95/// Allow `Tag` to be treated like a `&str` for display, etc.
96impl Deref for Tag {
97    type Target = str;
98
99    fn deref(&self) -> &Self::Target {
100        self.as_str()
101    }
102}
103
104/// Allow `ServiceName` to be treated like a `&str` for display, etc.
105impl Deref for ServiceName {
106    type Target = str;
107
108    fn deref(&self) -> &Self::Target {
109        let Self(tag) = self;
110        tag
111    }
112}
113
114/// Allow treating `Tag` as a `&str` for, e.g., HashMap indexing operations.
115impl Borrow<str> for Tag {
116    fn borrow(&self) -> &str {
117        self
118    }
119}
120
121/// Allow treating `ServiceName` as a `&str` for, e.g., HashMap indexing
122/// operations.
123impl Borrow<str> for ServiceName {
124    fn borrow(&self) -> &str {
125        self
126    }
127}
128
129impl Display for Tag {
130    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131        let Self(name) = self;
132        name.fmt(f)
133    }
134}
135
136impl PartialEq<str> for Tag {
137    fn eq(&self, other: &str) -> bool {
138        self.0 == other
139    }
140}
141
142impl Display for ServiceName {
143    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144        let Self(name) = self;
145        name.fmt(f)
146    }
147}
148
149const CONFIG_GLOB: &str = "/config/data/*.persist";
150
151fn try_insert_items(config: &mut Config, config_text: &str) -> Result<(), Error> {
152    let items: Vec<TaggedPersist> = serde_json5::from_str(config_text)?;
153    for item in items {
154        let TaggedPersist {
155            tag,
156            service_name,
157            selectors,
158            max_bytes,
159            min_seconds_between_fetch,
160            persist_across_boot,
161        } = item;
162        let tag = Tag::new(tag)?;
163        let name = ServiceName::new(service_name)?;
164        if let Some(existing) = config.entry(name.clone()).or_default().insert(
165            tag,
166            TagConfig { selectors, max_bytes, min_seconds_between_fetch, persist_across_boot },
167        ) {
168            bail!("Duplicate TagConfig found: {:?}", existing);
169        }
170    }
171    Ok(())
172}
173
174pub fn load_configuration_files() -> Result<Config, Error> {
175    load_configuration_files_from(CONFIG_GLOB)
176}
177
178pub fn load_configuration_files_from(path: &str) -> Result<Config, Error> {
179    let mut config = HashMap::new();
180    for file_path in glob(path)? {
181        try_insert_items(&mut config, &std::fs::read_to_string(file_path?)?)?;
182    }
183    Ok(config)
184}
185
186#[cfg(test)]
187mod test {
188    use super::*;
189
190    impl From<TaggedPersist> for TagConfig {
191        fn from(
192            TaggedPersist {
193                tag: _,
194                service_name: _,
195                selectors,
196                max_bytes,
197                min_seconds_between_fetch,
198                persist_across_boot,
199            }: TaggedPersist,
200        ) -> Self {
201            Self { selectors, max_bytes, min_seconds_between_fetch, persist_across_boot }
202        }
203    }
204
205    #[fuchsia::test]
206    fn verify_insert_logic() {
207        let mut config = HashMap::new();
208        let taga_servab = "[{tag: 'tag-a', service_name: 'serv-a', max_bytes: 10, \
209                           min_seconds_between_fetch: 31, selectors: ['foo', 'bar']}, \
210                           {tag: 'tag-a', service_name: 'serv-b', max_bytes: 20, \
211                           min_seconds_between_fetch: 32, selectors: ['baz'], \
212                           persist_across_boot: true }]";
213        let tagb_servb = "[{tag: 'tag-b', service_name: 'serv-b', max_bytes: 30, \
214                          min_seconds_between_fetch: 33, selectors: ['quux']}]";
215        // Numbers not allowed in names
216        let bad_tag = "[{tag: 'tag-b1', service_name: 'serv-b', max_bytes: 30, \
217                       min_seconds_between_fetch: 33, selectors: ['quux']}]";
218        // Underscores not allowed in names
219        let bad_serv = "[{tag: 'tag-b', service_name: 'serv_b', max_bytes: 30, \
220                        min_seconds_between_fetch: 33, selectors: ['quux']}]";
221        let persist_aa = TaggedPersist {
222            tag: "tag-a".to_string(),
223            service_name: "serv-a".to_string(),
224            max_bytes: 10,
225            min_seconds_between_fetch: 31,
226            selectors: vec!["foo".to_string(), "bar".to_string()],
227            persist_across_boot: false,
228        };
229        let persist_ba = TaggedPersist {
230            tag: "tag-a".to_string(),
231            service_name: "serv-b".to_string(),
232            max_bytes: 20,
233            min_seconds_between_fetch: 32,
234            selectors: vec!["baz".to_string()],
235            persist_across_boot: true,
236        };
237        let persist_bb = TaggedPersist {
238            tag: "tag-b".to_string(),
239            service_name: "serv-b".to_string(),
240            max_bytes: 30,
241            min_seconds_between_fetch: 33,
242            selectors: vec!["quux".to_string()],
243            persist_across_boot: false,
244        };
245
246        try_insert_items(&mut config, taga_servab).unwrap();
247        try_insert_items(&mut config, tagb_servb).unwrap();
248        assert_eq!(config.len(), 2);
249        let service_a = config.get("serv-a").unwrap();
250        assert_eq!(service_a.len(), 1);
251        assert_eq!(service_a.get("tag-a"), Some(&persist_aa.clone().into()));
252        let service_b = config.get("serv-b").unwrap();
253        assert_eq!(service_b.len(), 2);
254        assert_eq!(service_b.get("tag-a"), Some(&persist_ba.clone().into()));
255        assert_eq!(service_b.get("tag-b"), Some(&persist_bb.clone().into()));
256
257        assert!(try_insert_items(&mut config, bad_tag).is_err());
258        assert!(try_insert_items(&mut config, bad_serv).is_err());
259        // Can't duplicate tags in the same service
260        assert!(try_insert_items(&mut config, tagb_servb).is_err());
261    }
262
263    #[test]
264    fn test_tag_equals_str() {
265        assert_eq!(&Tag::new("foo").unwrap(), "foo");
266    }
267}