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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

use crate::teaclave_common_proto as proto;
pub use proto::*;

use teaclave_crypto::TeaclaveFile128Key;
use teaclave_types::{
    Entry, EntryBuilder, FileCrypto, TaskFailure, TaskOutputs, TaskResult, TaskStatus,
};

use std::convert::TryInto;
use std::net::Ipv6Addr;

use anyhow::{bail, ensure, Error, Result};

impl UserCredential {
    pub fn new(id: impl Into<String>, token: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            token: token.into(),
        }
    }
}

impl std::convert::TryFrom<proto::FileCryptoInfo> for FileCrypto {
    type Error = Error;
    fn try_from(proto: proto::FileCryptoInfo) -> Result<Self> {
        FileCrypto::new(&proto.schema, &proto.key, &proto.iv)
    }
}

impl std::convert::TryFrom<proto::FileCryptoInfo> for TeaclaveFile128Key {
    type Error = Error;
    fn try_from(proto: proto::FileCryptoInfo) -> Result<Self> {
        let file_crypto = FileCrypto::new(&proto.schema, &proto.key, &proto.iv)?;
        let crypto = match file_crypto {
            FileCrypto::TeaclaveFile128(info) => info,
            _ => anyhow::bail!("FileCryptoInfo not supported"),
        };
        Ok(crypto)
    }
}

impl std::convert::From<FileCrypto> for proto::FileCryptoInfo {
    fn from(crypto: FileCrypto) -> Self {
        let (key, iv) = crypto.key_iv();
        proto::FileCryptoInfo {
            schema: crypto.schema().to_owned(),
            key,
            iv,
        }
    }
}

impl std::convert::From<TeaclaveFile128Key> for proto::FileCryptoInfo {
    fn from(crypto: TeaclaveFile128Key) -> Self {
        let crypto = FileCrypto::TeaclaveFile128(crypto);
        let (key, iv) = crypto.key_iv();
        proto::FileCryptoInfo {
            schema: crypto.schema().to_owned(),
            key,
            iv,
        }
    }
}

pub fn i32_to_task_status(status: i32) -> Result<TaskStatus> {
    let ret = match proto::TaskStatus::from_i32(status) {
        Some(proto::TaskStatus::Created) => TaskStatus::Created,
        Some(proto::TaskStatus::DataAssigned) => TaskStatus::DataAssigned,
        Some(proto::TaskStatus::Approved) => TaskStatus::Approved,
        Some(proto::TaskStatus::Staged) => TaskStatus::Staged,
        Some(proto::TaskStatus::Running) => TaskStatus::Running,
        Some(proto::TaskStatus::Finished) => TaskStatus::Finished,
        Some(proto::TaskStatus::Failed) => TaskStatus::Failed,
        Some(proto::TaskStatus::Canceled) => TaskStatus::Canceled,
        None => bail!("invalid task status"),
    };
    Ok(ret)
}

pub fn i32_from_task_status(status: TaskStatus) -> i32 {
    match status {
        TaskStatus::Created => proto::TaskStatus::Created as i32,
        TaskStatus::DataAssigned => proto::TaskStatus::DataAssigned as i32,
        TaskStatus::Approved => proto::TaskStatus::Approved as i32,
        TaskStatus::Staged => proto::TaskStatus::Staged as i32,
        TaskStatus::Running => proto::TaskStatus::Running as i32,
        TaskStatus::Finished => proto::TaskStatus::Finished as i32,
        TaskStatus::Failed => proto::TaskStatus::Failed as i32,
        TaskStatus::Canceled => proto::TaskStatus::Canceled as i32,
    }
}

impl std::convert::TryFrom<proto::TaskOutputs> for TaskOutputs {
    type Error = Error;
    fn try_from(proto: proto::TaskOutputs) -> Result<Self> {
        let ret = TaskOutputs {
            return_value: proto.return_value,
            tags_map: proto.tags_map.try_into()?,
            log: proto.log,
        };
        Ok(ret)
    }
}
impl std::convert::From<TaskOutputs> for proto::TaskOutputs {
    fn from(outputs: TaskOutputs) -> Self {
        proto::TaskOutputs {
            return_value: outputs.return_value,
            tags_map: outputs.tags_map.into(),
            log: outputs.log,
        }
    }
}

impl std::convert::TryFrom<proto::TaskFailure> for TaskFailure {
    type Error = Error;
    fn try_from(proto: proto::TaskFailure) -> Result<Self> {
        let ret = TaskFailure {
            reason: proto.reason,
        };
        Ok(ret)
    }
}
impl std::convert::From<TaskFailure> for proto::TaskFailure {
    fn from(outputs: TaskFailure) -> Self {
        proto::TaskFailure {
            reason: outputs.reason,
        }
    }
}

impl std::convert::TryFrom<proto::TaskResult> for TaskResult {
    type Error = Error;
    fn try_from(proto: proto::TaskResult) -> Result<Self> {
        let task_result = match proto.result {
            Some(proto_result) => match proto_result {
                proto::task_result::Result::Ok(task_outputs) => {
                    let outputs_info = task_outputs.try_into()?;
                    TaskResult::Ok(outputs_info)
                }
                proto::task_result::Result::Err(task_failure) => {
                    let failure_info = task_failure.try_into()?;
                    TaskResult::Err(failure_info)
                }
            },
            None => TaskResult::NotReady,
        };
        Ok(task_result)
    }
}

impl std::convert::From<TaskResult> for proto::TaskResult {
    fn from(result: TaskResult) -> Self {
        let opt_result = match result {
            TaskResult::Ok(outputs) => Some(proto::task_result::Result::Ok(outputs.into())),
            TaskResult::Err(failure) => Some(proto::task_result::Result::Err(failure.into())),
            TaskResult::NotReady => None,
        };

        proto::TaskResult { result: opt_result }
    }
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ExecutorStatus {
    Idle,
    Executing,
}

impl std::convert::TryFrom<proto::ExecutorStatus> for ExecutorStatus {
    type Error = Error;
    fn try_from(status: proto::ExecutorStatus) -> Result<Self> {
        match status {
            proto::ExecutorStatus::Idle => Ok(ExecutorStatus::Idle),
            proto::ExecutorStatus::Executing => Ok(ExecutorStatus::Executing),
        }
    }
}

impl std::convert::TryFrom<i32> for ExecutorStatus {
    type Error = Error;
    fn try_from(status: i32) -> Result<Self> {
        match proto::ExecutorStatus::from_i32(status) {
            Some(proto::ExecutorStatus::Idle) => Ok(ExecutorStatus::Idle),
            Some(proto::ExecutorStatus::Executing) => Ok(ExecutorStatus::Executing),
            _ => bail!("invalid executor status"),
        }
    }
}

impl std::convert::From<ExecutorStatus> for i32 {
    fn from(status: ExecutorStatus) -> Self {
        match status {
            ExecutorStatus::Idle => proto::ExecutorStatus::Idle as i32,
            ExecutorStatus::Executing => proto::ExecutorStatus::Executing as i32,
        }
    }
}

impl std::convert::From<ExecutorStatus> for proto::ExecutorStatus {
    fn from(status: ExecutorStatus) -> Self {
        match status {
            ExecutorStatus::Idle => proto::ExecutorStatus::Idle,
            ExecutorStatus::Executing => proto::ExecutorStatus::Executing,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExecutorCommand {
    NoAction,
    Stop,
    NewTask,
}

impl Default for ExecutorCommand {
    fn default() -> Self {
        ExecutorCommand::NoAction
    }
}

impl std::convert::TryFrom<proto::ExecutorCommand> for ExecutorCommand {
    type Error = Error;
    fn try_from(command: proto::ExecutorCommand) -> Result<Self> {
        match command {
            proto::ExecutorCommand::NoAction => Ok(ExecutorCommand::NoAction),
            proto::ExecutorCommand::Stop => Ok(ExecutorCommand::Stop),
            proto::ExecutorCommand::NewTask => Ok(ExecutorCommand::NewTask),
        }
    }
}

impl std::convert::From<ExecutorCommand> for proto::ExecutorCommand {
    fn from(command: ExecutorCommand) -> Self {
        match command {
            ExecutorCommand::NoAction => proto::ExecutorCommand::NoAction,
            ExecutorCommand::Stop => proto::ExecutorCommand::Stop,
            ExecutorCommand::NewTask => proto::ExecutorCommand::NewTask,
        }
    }
}

impl std::convert::TryFrom<i32> for ExecutorCommand {
    type Error = Error;
    fn try_from(command: i32) -> Result<Self> {
        match proto::ExecutorCommand::from_i32(command) {
            Some(proto::ExecutorCommand::NoAction) => Ok(ExecutorCommand::NoAction),
            Some(proto::ExecutorCommand::Stop) => Ok(ExecutorCommand::Stop),
            Some(proto::ExecutorCommand::NewTask) => Ok(ExecutorCommand::NewTask),
            _ => bail!("invalid executor status"),
        }
    }
}

impl std::convert::From<ExecutorCommand> for i32 {
    fn from(command: ExecutorCommand) -> Self {
        match command {
            ExecutorCommand::NoAction => proto::ExecutorCommand::NoAction as i32,
            ExecutorCommand::Stop => proto::ExecutorCommand::Stop as i32,
            ExecutorCommand::NewTask => proto::ExecutorCommand::NewTask as i32,
        }
    }
}

impl std::convert::TryFrom<proto::Entry> for Entry {
    type Error = Error;

    fn try_from(proto: crate::teaclave_common_proto::Entry) -> Result<Self> {
        const IPV6_LENTGH: usize = 16;

        let len = proto.ip.len();
        ensure!(len == IPV6_LENTGH, "invalid ip length: {}", len);
        let ip = Ipv6Addr::from(<Vec<u8> as TryInto<[u8; 16]>>::try_into(proto.ip).unwrap());

        let builder = EntryBuilder::new();
        let entry = builder
            .microsecond(proto.microsecond)
            .ip(ip)
            .user(proto.user)
            .message(proto.message.clone())
            .result(proto.result)
            .build();

        Ok(entry)
    }
}

impl From<Entry> for proto::Entry {
    fn from(entry: Entry) -> Self {
        Self {
            microsecond: entry.datetime().timestamp_micros(),
            ip: entry.ip().octets().to_vec(),
            user: entry.user(),
            message: entry.message(),
            result: entry.result(),
        }
    }
}