document_pii/
main.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png",
    html_favicon_url = "https://raw.githubusercontent.com/getsentry/relay/master/artwork/relay-icon.png"
)]

use std::collections::BTreeSet;
use std::fs::File;
use std::path::PathBuf;

use clap::{command, Parser};
use serde::Serialize;
use syn::{ItemEnum, ItemStruct};
use walkdir::WalkDir;

use crate::item_collector::AstItemCollector;
use crate::pii_finder::FieldsWithAttribute;

pub mod item_collector;
pub mod pii_finder;

/// Structs and Enums are the only items that are relevant for finding PII fields.
#[derive(Clone)]
pub enum EnumOrStruct {
    Struct(ItemStruct),
    Enum(ItemEnum),
}

/// Gets all the .rs files in a given rust crate/workspace.
fn find_rs_files(dir: &PathBuf) -> Vec<std::path::PathBuf> {
    let walker = WalkDir::new(dir).into_iter();
    let mut rs_files = Vec::new();

    for entry in walker.filter_map(walkdir::Result::ok) {
        if !entry.path().to_string_lossy().contains("src") {
            continue;
        }
        if entry.file_type().is_file() && entry.path().extension().map_or(false, |ext| ext == "rs")
        {
            rs_files.push(entry.into_path());
        }
    }
    rs_files
}

/// Prints documentation for metrics.
#[derive(Debug, Parser, Default)]
#[command(verbatim_doc_comment)]
pub struct Cli {
    /// Optional output path. By default, documentation is printed on stdout.
    #[arg(short, long)]
    pub output: Option<PathBuf>,

    /// Path to the rust crate/workspace.
    #[arg(short, long)]
    pub path: Option<PathBuf>,

    /// The struct or enum of which you want to find all PII fields. Checks all items if none is
    /// provided.
    #[arg(short, long)]
    pub item: Option<String>,

    /// Vector of which PII-values should be looked for, options are: "true, maybe, false".
    #[arg(long, default_value = "true")]
    pub pii_values: Vec<String>,
}

impl Cli {
    pub fn run(self) -> anyhow::Result<()> {
        // User must either provide the path to a rust crate/workspace or be in one when calling this script.
        let path = match self.path.clone() {
            Some(path) => {
                if !path.join("Cargo.toml").exists() {
                    anyhow::bail!("Please provide the path to a rust crate/workspace");
                }
                path
            }
            None => std::env::current_dir()?,
        };

        // Before we can iterate over the PII fields properly, we make a mapping between all
        // paths to types and their AST node, and of all modules and the items in their scope.
        let types_and_use_statements = {
            let rust_file_paths = find_rs_files(&path);
            AstItemCollector::collect(&rust_file_paths)?
        };

        let pii_types =
            types_and_use_statements.find_pii_fields(self.item.as_deref(), &self.pii_values)?;

        // Function also takes a string to replace unnamed fields, for now we just remove them.
        let output_vec = Output::from_btreeset(pii_types);

        match self.output {
            Some(ref path) => serde_json::to_writer_pretty(File::create(path)?, &output_vec)?,
            None => serde_json::to_writer_pretty(std::io::stdout(), &output_vec)?,
        };

        Ok(())
    }
}

#[derive(Serialize, Default, Debug)]
struct Output {
    path: String,
    additional_properties: bool,
}

impl Output {
    fn new(pii_type: FieldsWithAttribute) -> Self {
        let mut output = Self {
            additional_properties: pii_type.attributes.contains_key("additional_properties"),
            ..Default::default()
        };

        output
            .path
            .push_str(&pii_type.type_and_fields[0].qualified_type_name);

        let mut iter = pii_type.type_and_fields.iter().peekable();
        while let Some(path) = iter.next() {
            // If field has attribute "additional_properties" it means it's not a real field
            // but represents unstrucutred data. So we remove it and pass the information as a boolean
            // in order to properly document this fact in the docs.
            if !(output.additional_properties && iter.peek().is_none()) {
                output.path.push_str(&format!(".{}", path.field_ident));
            }
        }

        output.path = output.path.replace("{{Unnamed}}.", "");
        output
    }

    /// Represent the PII fields in a format that will be used in the final output.
    fn from_btreeset(pii_types: BTreeSet<FieldsWithAttribute>) -> Vec<Self> {
        let mut output_vec = vec![];
        for pii in pii_types {
            output_vec.push(Output::new(pii));
        }
        output_vec.sort_by(|a, b| a.path.cmp(&b.path));

        output_vec
    }
}

fn print_error(error: &anyhow::Error) {
    eprintln!("Error: {error}");

    let mut cause = error.source();
    while let Some(ref e) = cause {
        eprintln!("  caused by: {e}");
        cause = e.source();
    }
}

fn main() {
    let cli = Cli::parse();

    match cli.run() {
        Ok(()) => (),
        Err(error) => {
            print_error(&error);
            std::process::exit(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use path_slash::PathBufExt;

    use crate::item_collector::TypesAndScopedPaths;

    use super::*;

    const RUST_TEST_CRATE: &str = "../../tests/test_pii_docs";

    fn get_types_and_use_statements() -> TypesAndScopedPaths {
        let rust_crate = PathBuf::from_slash(RUST_TEST_CRATE);
        let rust_file_paths = find_rs_files(&rust_crate);
        AstItemCollector::collect(&rust_file_paths).unwrap()
    }

    // On windows the assert fails because of how file paths are different there.
    #[cfg(not(target_os = "windows"))]
    #[test]
    fn test_find_rs_files() {
        let rust_crate = PathBuf::from_slash(RUST_TEST_CRATE);
        let mut rust_file_paths = find_rs_files(&rust_crate);
        rust_file_paths.sort_unstable();
        insta::assert_debug_snapshot!(rust_file_paths);
    }

    #[test]
    fn test_single_type() {
        let types_and_use_statements = get_types_and_use_statements();

        let pii_types = types_and_use_statements
            .find_pii_fields(Some("test_pii_docs::SubStruct"), &vec!["true".to_string()])
            .unwrap();

        let output = Output::from_btreeset(pii_types);
        insta::assert_debug_snapshot!(output);
    }

    #[test]
    fn test_scoped_paths() {
        let types_and_use_statements = get_types_and_use_statements();

        let TypesAndScopedPaths { scoped_paths, .. } = types_and_use_statements;
        insta::assert_debug_snapshot!(scoped_paths);
    }

    #[test]
    fn test_pii_true() {
        let types_and_use_statements = get_types_and_use_statements();

        let pii_types = types_and_use_statements
            .find_pii_fields(None, &vec!["true".to_string()])
            .unwrap();

        let output = Output::from_btreeset(pii_types);
        insta::assert_debug_snapshot!(output);
    }

    #[test]
    fn test_pii_false() {
        let types_and_use_statements = get_types_and_use_statements();

        let pii_types = types_and_use_statements
            .find_pii_fields(None, &vec!["false".to_string()])
            .unwrap();

        let output = Output::from_btreeset(pii_types);
        insta::assert_debug_snapshot!(output);
    }

    #[test]
    fn test_pii_all() {
        let types_and_use_statements = get_types_and_use_statements();

        let pii_types = types_and_use_statements
            .find_pii_fields(
                None,
                &vec!["true".to_string(), "false".to_string(), "maybe".to_string()],
            )
            .unwrap();

        let output = Output::from_btreeset(pii_types);
        insta::assert_debug_snapshot!(output);
    }

    #[test]
    fn test_pii_retain_additional_properties_truth_table()
    /*
    Fields should be chosen if there is a pii match, and either retain = "true", or there's no
    "additional_properties" attribute.
    Logic: ((pii match) & (retain = "true" | !additional_properties))

    truth table:

    +-----------+-----------------+----------  ------------+----------+
    | pii match | retain = "true" | !additional_properties | selected |
    +-----------+----------------------+-------------------+----------+
    | True      | True            | True                   | True     |
    | True      | True            | False                  | True     |
    | True      | False           | True                   | False    |
    | True      | False           | False                  | True     |
    | False     | True            | True                   | False    |
    | False     | True            | False                  | False    |
    | False     | False           | True                   | False    |
    | False     | False           | False                  | False    |
    +-----------+-----------------+------------------------+----------+

     */
    {
        let types_and_use_statements = get_types_and_use_statements();

        let pii_types = types_and_use_statements
            .find_pii_fields(None, &vec!["truth_table_test".to_string()])
            .unwrap();

        let output = Output::from_btreeset(pii_types);
        insta::assert_debug_snapshot!(output);
    }
}