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
//  CHECK.rs
//    by Lut99
//
//  Created:
//    02 Feb 2024, 11:08:20
//  Last edited:
//    08 Feb 2024, 17:18:29
//  Auto updated?
//    Yes
//
//  Description:
//!   Implements the `brane check`-subcommand, which attempts to validate
//!   a workflow against remote policy.
//

use std::io::Read;
use std::sync::Arc;
use std::{fs, io};

use brane_ast::{CompileResult, Workflow};
use brane_dsl::{Language, ParserOptions};
use console::style;
use error_trace::trace;
use log::{debug, info};
use specifications::data::DataIndex;
use specifications::driving::{CheckReply, CheckRequest, DriverServiceClient};
use specifications::package::PackageIndex;
use specifications::profiling::{self};

pub use crate::errors::CheckError as Error;
use crate::instance::InstanceInfo;


/***** HELPER FUNCTIONS *****/
/// Compiles the given source text for the given remote instance.
///
/// # Arguments
/// - `instance`: The [`InstanceInfo`] describing the instance for which we will compile.
/// - `input`: Some description of where the input comes from (used for debugging).
/// - `source`: The raw source text.
/// - `language`: The [`Language`] as which to parse the `source` text.
/// - `user`: An override to set the end user of the workflow result instead of hte instance one.
///
/// # Returns
/// A compiled [`Workflow`].
///
/// Note that this already printed any warnings or errors.
///
/// # Errors
/// This function errors if we failed to get remote packages/datasets, or if the input was not valid BraneScript/Bakery.
async fn compile(instance: &InstanceInfo, input: &str, source: String, language: Language, user: Option<String>) -> Result<Workflow, Error> {
    // Read the package index from the remote first
    let url: String = format!("{}/graphql", instance.api);
    debug!("Retrieving package index from '{url}'");
    let pindex: PackageIndex = match brane_tsk::api::get_package_index(&url).await {
        Ok(pindex) => pindex,
        Err(err) => {
            return Err(Error::PackageIndexRetrieve { url, err });
        },
    };

    // Next up, the data index
    let url: String = format!("{}/data/info", instance.api);
    debug!("Retrieving data index from '{url}'");
    let dindex: DataIndex = match brane_tsk::api::get_data_index(&url).await {
        Ok(dindex) => dindex,
        Err(err) => {
            return Err(Error::DataIndexRetrieve { url, err });
        },
    };

    // Hit the Brane compiler
    match brane_ast::compile_program(source.as_bytes(), &pindex, &dindex, &ParserOptions::new(language)) {
        CompileResult::Workflow(mut wf, warns) => {
            // Emit the warnings before continuing
            for warn in warns {
                warn.prettyprint(input, &source);
            }

            // Inject a user
            wf.user = Arc::new(Some(user.unwrap_or_else(|| instance.user.clone())));

            // OK
            Ok(wf)
        },
        CompileResult::Err(errs) => {
            // Print 'em
            for err in errs {
                err.prettyprint(input, &source);
            }
            Err(Error::AstCompile { input: input.into() })
        },
        CompileResult::Eof(err) => {
            err.prettyprint(input, source);
            Err(Error::AstCompile { input: input.into() })
        },

        // The rest does not occur for this variation of the function
        CompileResult::Program(_, _) | CompileResult::Unresolved(_, _) => unreachable!(),
    }
}





/***** LIBRARY *****/
/// Handles the `brane check`-subcommand, which attempts to validate a workflow against remote policy.
///
/// # Arguments
/// - `file`: The path to the file to load as input. `-` means stdin.
/// - `language`: The [`Language`] of the input file.
/// - `user`: An override for the user in the instance file, if any.
/// - `profile`: If true, show profile timings of the request if available.
///
/// # Errors
/// This function errors if we failed to perform the check.
pub async fn handle(file: String, language: Language, user: Option<String>, profile: bool) -> Result<(), Error> {
    info!("Handling 'brane check {}'", if file == "-" { "<stdin>" } else { file.as_str() });


    /***** PREPARATION *****/
    let prof: profiling::ProfileScope = profiling::ProfileScope::new("Local preparation");

    // Resolve the input file to a source string
    debug!("Loading input from '{file}'...");
    let load = prof.time("Input loading");
    let (input, source): (String, String) = if file == "-" {
        // Read from stdin
        let mut source: String = String::new();
        if let Err(err) = io::stdin().read_to_string(&mut source) {
            return Err(Error::InputStdinRead { err });
        }
        ("<stdin>".into(), source)
    } else {
        // Read from a file
        match fs::read_to_string(&file) {
            Ok(source) => (file, source),
            Err(err) => return Err(Error::InputFileRead { path: file.into(), err }),
        }
    };
    load.stop();

    // Get the current instance
    debug!("Retrieving active instance info...");
    let instance: InstanceInfo = match prof.time_func("Instance resolution", InstanceInfo::from_active_path) {
        Ok(config) => config,
        Err(err) => {
            return Err(Error::ActiveInstanceInfoLoad { err });
        },
    };

    // Attempt to compile the input
    debug!("Compiling source text to Brane WIR...");
    let workflow: Workflow = match prof.time_fut("Workflow compilation", compile(&instance, &input, source, language, user)).await {
        Ok(wf) => wf,
        Err(err) => return Err(Error::WorkflowCompile { input, err: Box::new(err) }),
    };
    let sworkflow: String = match prof.time_func("Workflow serialization", || serde_json::to_string(&workflow)) {
        Ok(swf) => swf,
        Err(err) => return Err(Error::WorkflowSerialize { input, err }),
    };

    // Connect to the driver
    debug!("Connecting to driver '{}'...", instance.drv);
    let rem = prof.time("Driver time");
    let mut client: DriverServiceClient = match DriverServiceClient::connect(instance.drv.to_string()).await {
        Ok(client) => client,
        Err(err) => {
            return Err(Error::DriverConnect { address: instance.drv, err });
        },
    };

    // Send the request
    debug!("Sending check request to driver '{}' and awaiting response...", instance.drv);
    let res: CheckReply = match client.check(CheckRequest { workflow: sworkflow }).await {
        Ok(res) => res.into_inner(),
        Err(err) => return Err(Error::DriverCheck { address: instance.drv, err }),
    };
    rem.stop();

    // FIRST: Print profile information if available
    if profile {
        println!();
        println!("{}", (0..80).map(|_| '-').collect::<String>());
        println!("LOCAL PROFILE RESULTS:");
        println!("{}", prof.display());
        if let Some(prof) = res.profile {
            // Attempt to parse it
            match serde_json::from_str::<profiling::ProfileScope>(&prof) {
                Ok(prof) => {
                    // Print
                    println!();
                    println!("REMOTE PROFILE RESULTS:");
                    println!("{}", prof.display());
                },
                Err(err) => warn!("{}", trace!(("Failed to deserialize profile information in CheckReply"), err)),
            }
        }
        println!("{}", (0..80).map(|_| '-').collect::<String>());
        println!();

        // Drop both of them to avoid writing them again
        std::mem::forget(prof);
    }

    // Consider the verdict
    if res.verdict {
        println!("Workflow {} was {} by all domains", style(&workflow.id).bold().cyan(), style("accepted").bold().green());
    } else {
        println!("Workflow {} was {} by at least one domain", style("").bold().cyan(), style("rejected").bold().red());

        if let Some(who) = res.who {
            println!(" > Checker of domain {} rejected workflow", style(who).bold().cyan());
            if !res.reasons.is_empty() {
                println!("   Reasons for denial:");
                for reason in res.reasons {
                    println!("    - {}", style(reason).bold());
                }
            }
        }
    }
    println!();

    // Either way, the request itself was a success
    Ok(())
}