planetary-api 0.2.0

The Planetary TES API service
Documentation
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! The implementation of the TES task endpoints (v1).

use std::path::Component;

use anyhow::Context;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::bail;
use axum::Extension;
use axum::extract::State;
use glob::Pattern;
use planetary_db::format_log_message;
use planetary_server::Error;
use planetary_server::Json;
use planetary_server::Path;
use planetary_server::Query;
use planetary_server::ServerResponse;
use reqwest::header;
use secrecy::ExposeSecret;
use tes::v1::types::requests::DEFAULT_PAGE_SIZE;
use tes::v1::types::requests::GetTaskParams;
use tes::v1::types::requests::ListTasksParams;
use tes::v1::types::requests::MAX_PAGE_SIZE;
use tes::v1::types::requests::Task as RequestTask;
use tes::v1::types::requests::View;
use tes::v1::types::responses::CreatedTask;
use tes::v1::types::responses::ListTasks;
use tes::v1::types::responses::TaskResponse;
use tes::v1::types::task::Executor;
use tes::v1::types::task::Input;
use tes::v1::types::task::IoType;
use tes::v1::types::task::Output;
use tes::v1::types::task::Resources;
use tes::v1::types::task::State as TesState;
use tokio_retry2::Retry;
use tokio_retry2::RetryError;
use url::Url;

use crate::Username;
use crate::notify_retry;
use crate::retry_durations;

/// Ensures that the given URL is supported by Planetary.
fn ensure_supported_url(url: &str, kind: &str, allow_file_urls: bool) -> Result<()> {
    let url: Url = url
        .parse()
        .with_context(|| format!("invalid {kind} URL `{url}`"))?;

    match url.scheme() {
        "http" | "https" | "az" | "s3" | "gs" => {}
        "file" if allow_file_urls => {}
        scheme => bail!("{kind} URL `{url}` uses unsupported scheme `{scheme}`"),
    }

    // File URLs are required to be convertible to a UTF-8 file path with no
    // parent-directory segments
    if url.scheme() == "file" {
        let path = url.to_file_path().map_err(|_| {
            anyhow!("invalid {kind} file URL `{url}`: host must be empty or `localhost`")
        })?;

        if path.components().any(|c| matches!(c, Component::ParentDir)) {
            bail!("{kind} file URL `{url}` cannot contain `..` path segments");
        }

        if path.into_os_string().into_string().is_err() {
            bail!("{kind} URL `{url}` cannot be represented in UTF-8");
        }
    }

    Ok(())
}

/// Performs validation on the request.
fn validate_task(task: &RequestTask, allow_file_urls: bool) -> Result<()> {
    use std::path::Path;

    fn validate_input(input: &Input, allow_file_urls: bool) -> Result<()> {
        let path = Path::new(&input.path);
        if !path.is_absolute() {
            bail!("input path `{path}` is not absolute", path = input.path);
        }

        // The input path cannot contain `..` segments
        if path.components().any(|c| matches!(c, Component::ParentDir)) {
            bail!(
                "input path `{path}` cannot contain `..` path segments",
                path = input.path
            );
        }

        match (&input.url, &input.content) {
            (None, None) => bail!("input URL is required"),
            (None, Some(content)) if content.is_empty() => bail!("input URL is required"),
            (Some(url), None) => {
                // Check for supported URL schemes
                ensure_supported_url(url, "input", allow_file_urls)?;
            }
            (_, Some(_)) => {
                // If content is specified, URL is ignored; it must be a file
                // type
                if input.ty != IoType::File {
                    bail!("input specifies content but is not a file")
                }
            }
        }

        Ok(())
    }

    fn validate_output(output: &Output, allow_file_urls: bool) -> Result<()> {
        // The output path must be absolute
        let path = Path::new(&output.path);
        if !path.is_absolute() {
            bail!("output path `{path}` is not absolute", path = output.path);
        }

        // The output path cannot contain `..` segments
        if path.components().any(|c| matches!(c, Component::ParentDir)) {
            bail!(
                "output path `{path}` cannot contain `..` path segments",
                path = output.path
            );
        }

        // The output path cannot be root
        if output.path == "/" {
            bail!("output path cannot be `/`");
        }

        // Validate the path prefix, if present
        if let Some(prefix) = &output.path_prefix {
            // Ensure the output path pattern is valid
            Pattern::new(&output.path).with_context(|| {
                format!("invalid output path pattern `{path}`", path = output.path)
            })?;

            // The path prefix must itself be absolute
            if !Path::new(prefix).is_absolute() {
                bail!("output path prefix `{prefix}` is not absolute");
            }

            // The output path prefix cannot be root
            if prefix == "/" {
                bail!("output path prefix cannot be `/`");
            }

            // The output path must start with the prefix too
            if !output.path.starts_with(prefix) {
                bail!(
                    "output path `{path}` does not start with the prefix `{prefix}`",
                    path = output.path
                );
            }
        }

        // Check for supported URL schemes
        ensure_supported_url(&output.url, "output", allow_file_urls)?;

        Ok(())
    }

    fn validate_executor(executor: &Executor) -> Result<()> {
        if executor.image.is_empty() {
            bail!("executor image is required");
        }

        match executor.command.first() {
            Some(first) if !first.is_empty() => {}
            _ => bail!("executor command must have at least one entry"),
        }

        if let Some(stdin) = &executor.stdin
            && !Path::new(stdin).is_absolute()
        {
            bail!("executor stdin path must be a absolute");
        }

        if let Some(stdout) = &executor.stdout
            && !Path::new(stdout).is_absolute()
        {
            bail!("executor stdout path must be a absolute");
        }

        if let Some(stderr) = &executor.stderr
            && !Path::new(stderr).is_absolute()
        {
            bail!("executor stderr path must be a absolute");
        }

        Ok(())
    }

    fn validate_resources(resources: &Resources) -> Result<()> {
        if let Some(cpus) = resources.cpu_cores
            && cpus <= 0
        {
            bail!("task resource cpu cores must be greater than zero");
        }

        if let Some(ram) = resources.ram_gb {
            let ram = ram.ceil();

            if ram <= 0. {
                bail!("task resource RAM must be greater than zero");
            }

            if ram > u64::MAX as f64 {
                bail!(
                    "task resource RAM cannot exceed `{max}` gigabytes",
                    max = u64::MAX
                );
            }
        }

        if let Some(disk) = resources.disk_gb {
            let disk = disk.ceil();

            if disk < 0.0 {
                bail!("task resource disk cannot be less than zero");
            }

            if disk > u64::MAX as f64 {
                bail!(
                    "task resource disk cannot exceed `{max}` gigabytes",
                    max = u64::MAX
                );
            }
        }

        if resources.backend_parameters_strict.unwrap_or(false)
            && resources
                .backend_parameters
                .as_ref()
                .map(|p| !p.is_empty())
                .unwrap_or(false)
        {
            bail!("backend parameters are not supported by this implementation");
        }

        Ok(())
    }

    fn validate_volume(volume: &str) -> Result<()> {
        if !Path::new(volume).is_absolute() {
            bail!("volume path `{volume}` is not absolute");
        }

        Ok(())
    }

    for input in task.inputs.as_deref().unwrap_or_default() {
        validate_input(input, allow_file_urls)?;
    }

    for output in task.outputs.as_deref().unwrap_or_default() {
        validate_output(output, allow_file_urls)?;
    }

    if task.executors.is_empty() {
        bail!("task must contain at least one executor");
    }

    for executor in &task.executors {
        validate_executor(executor)?;
    }

    if let Some(resources) = &task.resources {
        validate_resources(resources)?;
    }

    for volume in task.volumes.as_deref().unwrap_or_default() {
        validate_volume(volume)?;
    }

    Ok(())
}

/// The `POST /tasks` route.
pub async fn create_task(
    Extension(username): Extension<Username>,
    State(state): State<crate::State>,
    Json(task): Json<RequestTask>,
) -> ServerResponse<Json<CreatedTask>> {
    if let Err(e) = validate_task(&task, state.allow_file_urls) {
        return Err(Error::bad_request(format!("{e:#}")));
    }

    let id = state.database.insert_task(&username.0, &task).await?;

    // Notify an orchestrator service to start the task
    Retry::spawn_notify(
        retry_durations(),
        || async {
            // Retry the operation is there is a problem sending the request to
            // the server Don't retry if the service returned an
            // error response
            state
                .client
                .post(
                    state
                        .orchestrator
                        .url
                        .join(&format!("/v1/tasks/{id}"))
                        .expect("URL should join"),
                )
                .header(
                    header::AUTHORIZATION,
                    format!(
                        "Bearer {token}",
                        token = state.orchestrator.api_key.expose_secret()
                    ),
                )
                .send()
                .await
                .map_err(RetryError::transient)?
                .error_for_status()
                .map_err(RetryError::permanent)
        },
        notify_retry,
    )
    .await?;

    Ok(Json(CreatedTask { id }))
}

/// The `GET /tasks` route.
pub async fn list_tasks(
    Extension(username): Extension<Username>,
    Query(params): Query<ListTasksParams>,
    State(state): State<crate::State>,
) -> ServerResponse<Json<ListTasks<TaskResponse>>> {
    let page_size = params.page_size.unwrap_or(DEFAULT_PAGE_SIZE);
    if page_size == 0 {
        return Err(Error::bad_request("page size must be greater than zero"));
    }

    if page_size >= MAX_PAGE_SIZE {
        return Err(Error::bad_request(format!(
            "page size must be less than {MAX_PAGE_SIZE}"
        )));
    }

    if params.tag_values.as_ref().map(|v| v.len()).unwrap_or(0)
        > params.tag_keys.as_ref().map(|k| k.len()).unwrap_or(0)
    {
        return Err(Error::bad_request(
            "the number of tag values cannot exceed the number of tag keys",
        ));
    }

    let (tasks, next_page_token) = state.database.get_tasks(&username.0, params).await?;

    Ok(Json(ListTasks {
        tasks,
        next_page_token,
    }))
}

/// The `GET /tasks/:id` route.
pub async fn get_task(
    Path(id): Path<String>,
    Extension(username): Extension<Username>,
    Query(params): Query<GetTaskParams>,
    State(state): State<crate::State>,
) -> ServerResponse<Json<TaskResponse>> {
    let task = state.database.get_task(&username.0, &id, params).await?;
    Ok(Json(task))
}

/// The `POST /tasks/:id:cancel` route.
pub async fn cancel_task(
    Path(id): Path<String>,
    Extension(username): Extension<Username>,
    State(state): State<crate::State>,
) -> ServerResponse<Json<serde_json::Value>> {
    // TODO: remove this once `matchit` 0.8.6 can be used
    let id = id.strip_suffix(":cancel").ok_or_else(Error::not_found)?;

    // Check that the task is currently in a running state
    // This also ensures that the task is associated with the request's user
    let task = state
        .database
        .get_task(
            &username.0,
            id,
            GetTaskParams {
                view: View::Minimal,
            },
        )
        .await?;
    if !task
        .as_minimal()
        .expect("should be a minimal task")
        .state
        .unwrap_or_default()
        .is_executing()
    {
        return Err(Error::not_cancelable());
    }

    // Mark the task as canceling
    if !state
        .database
        .update_task_state(
            id,
            TesState::Canceling,
            &[&format_log_message!("canceling task `{id}`")],
            None,
            None,
        )
        .await?
    {
        return Err(Error::not_cancelable());
    }

    // Notify the orchestrator service to cancel the task
    Retry::spawn_notify(
        retry_durations(),
        || async {
            // Retry the operation is there is a problem sending the request to
            // the server Don't retry if the service returned an
            // error response
            state
                .client
                .delete(
                    state
                        .orchestrator
                        .url
                        .join(&format!("/v1/tasks/{id}"))
                        .expect("URL should join"),
                )
                .header(
                    header::AUTHORIZATION,
                    format!(
                        "Bearer {token}",
                        token = state.orchestrator.api_key.expose_secret()
                    ),
                )
                .send()
                .await
                .map_err(RetryError::transient)?
                .error_for_status()
                .map_err(RetryError::permanent)
        },
        notify_retry,
    )
    .await?;

    // The spec uses a 200 response with an empty JSON object
    Ok(Json(serde_json::Value::Object(Default::default())))
}