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
#![warn(missing_docs, non_ascii_idents, trivial_numeric_casts,
    unused_crate_dependencies, noop_method_call, single_use_lifetimes, trivial_casts,
    unused_lifetimes, nonstandard_style, variant_size_differences)]
#![deny(keyword_idents)]
// #![warn(clippy::missing_docs_in_private_items)]
#![allow(clippy::needless_return)]
// #![allow(clippy::needless_return, clippy::while_let_on_iterator, clippy::collapsible_else_if)]

//! A library to help access the Assemblyline API from rust

mod types;
mod connection;
mod modules;

use std::sync::Arc;

use modules::file::File;
use modules::help::Help;
use modules::alert::Alert;
use modules::bundle::Bundle;
use modules::error::Error;
use modules::search::Search;
use modules::ingest::Ingest;
use modules::submit::Submit;
pub use types::{Authentication, JsonMap};
pub use connection::Connection;


/// A client to communicate with the Assemblyline API
pub struct Client {
    // Connection handler
    // _connection: Arc<Connection>,

    /// alert specific API endpoints
    pub alert: Alert,
    /// bundle specific API endpoints
    pub bundle: Bundle,
    /// error specific API endpoints
    pub error: Error,
    /// file specific API endpoints
    pub file: File,
    // self.hash_search = HashSearch(self._connection)
    /// Help API endpoints
    pub help: Help,
    // self.heuristics = Heuristics(self._connection)
    /// Ingest API endpoints
    pub ingest: Ingest,
    // self.live = Live(self._connection)
    // self.ontology = Ontology(self._connection)
    // self.replay = Replay(self._connection)
    // self.result = Result(self._connection)
    // self.safelist = Safelist(self._connection)
    /// search API endpoints
    pub search: Search,
    // self.service = Service(self._connection)
    // self.signature = Signature(self._connection)
    // self.socketio = SocketIO(self._connection)
    // self.submission = Submission(self._connection)
    /// File submission API endpoints
    pub submit: Submit,
    // self.system = System(self._connection)
    // self.user = User(self._connection)
    // self.workflow = Workflow(self._connection)
}

impl Client {
    /// Connect to an assemblyline system
    pub async fn connect(server: String, auth: Authentication) -> Result<Self, types::Error> {
        let connection = Arc::new(Connection::connect(server, auth, None, true, Default::default(), None, None).await?);
        Ok(Self {
            alert: Alert::new(connection.clone()),
            bundle: Bundle::new(connection.clone()),
            error: Error::new(connection.clone()),
            file: File::new(connection.clone()),
            help: Help::new(connection.clone()),
            ingest: Ingest::new(connection.clone()),
            search: Search::new(connection.clone()),
            submit: Submit::new(connection)
            // _connection,
        })
    }

    /// Connect to an assemblyline system
    pub async fn from_connection(connection: Arc<Connection>) -> Result<Self, types::Error> {
        // let connection = Arc::new(Connection::connect(server, auth, None, true, Default::default(), Some(cert), None).await?);
        Ok(Self {
            alert: Alert::new(connection.clone()),
            bundle: Bundle::new(connection.clone()),
            error: Error::new(connection.clone()),
            file: File::new(connection.clone()),
            help: Help::new(connection.clone()),
            ingest: Ingest::new(connection.clone()),
            search: Search::new(connection.clone()),
            submit: Submit::new(connection)
            // _connection,
        })
    }
}


#[cfg(test)]
mod tests {

    use assemblyline_models::datastore::submission::{SubmissionParams, SubmissionState, ServiceSelection};
    use rand::{thread_rng, Rng};

    use crate::{Authentication, Client};

    fn init() {
        let _ = env_logger::builder().is_test(true).try_init();
    }

    pub (crate) async fn prepare_client() -> Client {
        init();
        let url = std::env::var("ASSEMBLYLINE_URL").unwrap();
        let username = std::env::var("ASSEMBLYLINE_USER").unwrap();
        let key = std::env::var("ASSEMBLYLINE_KEY").unwrap();
        Client::connect(url, Authentication::ApiKey { username, key }).await.unwrap()
    }

    fn random_body() -> Vec<u8> {
        let mut out = vec![];
        let mut prng = thread_rng();
        let length = 128 + prng.gen_range(0..256);
        while out.len() < length {
            out.push(prng.gen());
        }
        out
    }

    #[tokio::test]
    async fn submit_content() {
        let client = prepare_client().await;

        let result = client.submit.single()
            .metadata_item("testbatch".to_owned(), "0".to_owned())
            .params(SubmissionParams{ ttl: 1, ..Default::default()})
            .fname("test-file".to_owned())
            .content(random_body()).await.unwrap();

        assert_eq!(result.state, SubmissionState::Submitted);
    }


    #[tokio::test]
    async fn search_single_page() {
        let client = prepare_client().await;
        let batch: u64 = thread_rng().gen();
        let batch: String = batch.to_string();

        let _result = client.ingest.single()
            .metadata_item("testbatch".to_owned(), batch.clone())
            .notification_queue(batch.clone())
            .params(SubmissionParams{ priority: 300, ttl: 1, services: ServiceSelection{ selected: Some(vec!["Characterize".to_owned()]), ..Default::default()}, ..Default::default()})
            .fname("test-file".to_owned())
            .content(random_body()).await.unwrap();

        for _ in 0..100 {
            match client.ingest.get_message(&batch).await.unwrap() {
                Some(message) => {
                    assert_eq!(message.submission.metadata.get("testbatch").unwrap(), &batch);
                    break;
                },
                None => { tokio::time::sleep(tokio::time::Duration::from_secs(1)).await; },
            }
        }

        for _ in 0..10 {
            let result = client.search.submission(format!("metadata.testbatch: {batch}"))
                .search().await.unwrap();

            if result.items.len() == 1 {
                return
            }

            tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
        }
        panic!()
    }



}