Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
seeds:
- name: order_codes
description: Order codes, one of which the audit does not accept.
columns:
- name: code
type: VARCHAR
audits:
- accepted_values:
values: [A, B]
31 changes: 28 additions & 3 deletions clients/typescript/src/schema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,10 @@ export interface components {
/** Format: int64 */
priority: number;
reason: string;
/** Format: uuid */
/**
* Format: uuid
* @description A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
*/
run_id: string;
target: string;
};
Expand Down Expand Up @@ -1883,7 +1886,10 @@ export interface components {
/** Format: int64 */
priority: number;
reason: string;
/** Format: uuid */
/**
* Format: uuid
* @description A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
*/
run_id: string;
/** @description Narrow the job with `sqb build` selectors; only nodes already in the job can remain. */
select?: string[];
Expand Down Expand Up @@ -2684,7 +2690,10 @@ export interface components {
/** Format: int64 */
priority: number;
reason: string;
/** Format: uuid */
/**
* Format: uuid
* @description A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
*/
run_id: string;
/** Format: uuid */
source_run_id: string;
Expand Down Expand Up @@ -6040,6 +6049,14 @@ export interface operations {
"application/json": components["schemas"]["MutationResponse"];
};
};
400: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["ApiErrorBody"];
};
};
/** @description Session rejected */
401: {
headers: {
Expand Down Expand Up @@ -6200,6 +6217,14 @@ export interface operations {
"application/json": components["schemas"]["MutationResponse"];
};
};
400: {
headers: {
[name: string]: unknown;
};
content: {
"application/json": components["schemas"]["ApiErrorBody"];
};
};
/** @description Session rejected */
401: {
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ pub(crate) async fn statistics(

#[utoipa::path(post, path = "/api/control/runs", operation_id = "requestJobRun",
params(("x-sqb-csrf" = String, Header)), request_body = JobRunRequest,
responses((status = 200, body = MutationResponse), (status = 401, description = "Session rejected"),
responses((status = 200, body = MutationResponse),
(status = 400, body = crate::api::models::ApiErrorBody), (status = 401, description = "Session rejected"),
(status = 403, body = crate::api::models::ApiErrorBody)), security(("session" = []), ("cliSession" = [])))]
pub(crate) async fn request_job(
(State(store), Extension(context)): (State<ControlStore>, Extension<AuditContext>),
Expand Down Expand Up @@ -148,7 +149,8 @@ pub(crate) async fn cancel_run(

#[utoipa::path(post, path = "/api/control/runs/rerun", operation_id = "rerunPersonRun",
params(("x-sqb-csrf" = String, Header)), request_body = RerunRequest,
responses((status = 200, body = MutationResponse), (status = 401, description = "Session rejected"),
responses((status = 200, body = MutationResponse),
(status = 400, body = crate::api::models::ApiErrorBody), (status = 401, description = "Session rejected"),
(status = 403, body = crate::api::models::ApiErrorBody)), security(("session" = []), ("cliSession" = [])))]
pub(crate) async fn rerun(
(State(store), Extension(context)): (State<ControlStore>, Extension<AuditContext>),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::runs::_helpers::requests::permissions::{authorize_execution, executio
use crate::runs::_helpers::requests::publication::{
publish_request, record_authored, request_retry,
};
use crate::runs::_helpers::requests::run_id::refuse_derived_run_id;
use crate::runs::constants::ADHOC_AUDIT_TAG;
use crate::runs::models::{
AdhocCheckRequest, AdhocPermission, AdhocRunRequest, CommandPlan, PlannerResource, RunCommand,
Expand Down Expand Up @@ -222,6 +223,7 @@ pub(in crate::runs) async fn request_adhoc_run_in(
(context, principal_id): (&AuditContext, Uuid),
(planner, request): (&dyn CommandPlanner, &AdhocRunRequest),
) -> Result<(ControlTransaction, bool), ControlError> {
refuse_derived_run_id(request.run_id)?;
let (transaction, context) = authorize_listing(
transaction,
(context, principal_id),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::runs::_helpers::requests::permissions::authorize_execution;
use crate::runs::_helpers::requests::publication::{
publish_request, record_authored, request_retry,
};
use crate::runs::_helpers::requests::run_id::refuse_derived_run_id;
use crate::runs::constants::MANUAL_JOB_AUDIT_TAG;
use crate::runs::main::frozen_job_request::frozen_job_request;
use crate::runs::models::{JobRunRequest, RunWindow};
Expand All @@ -26,6 +27,7 @@ pub(crate) async fn request_job_run_in(
(context, credentials): (&AuditContext, Option<Uuid>),
(planner, request): (Option<&dyn CommandPlanner>, &JobRunRequest),
) -> Result<(ControlTransaction, bool), ControlError> {
refuse_derived_run_id(request.run_id)?;
let mut context = context.clone();
let scope = PermissionScope {
action: Action::JobRun,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::runs::_helpers::requests::publication::{
publish_request, record_authored, request_retry,
};
use crate::runs::_helpers::requests::rerun::frozen_rerun;
use crate::runs::_helpers::requests::run_id::refuse_derived_run_id;
use crate::runs::constants::RERUN_AUDIT_TAG;
use crate::runs::models::{RerunRequest, RunRequest};
use crate::store::errors::ControlError;
Expand All @@ -22,6 +23,7 @@ pub(crate) async fn request_rerun_in(
(context, credentials): (&AuditContext, Option<Uuid>),
request: &RerunRequest,
) -> Result<(ControlTransaction, bool), ControlError> {
refuse_derived_run_id(request.run_id)?;
let mut context = context.clone();
let mut prepared = None;
if let Some(credentials) = credentials {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ pub(in crate::runs) mod permissions;
pub(in crate::runs) mod plan;
pub(in crate::runs) mod publication;
pub(in crate::runs) mod rerun;
pub(in crate::runs) mod run_id;
18 changes: 18 additions & 0 deletions crates/sqlbuild-orchestration/src/runs/_helpers/requests/run_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! A run ID a person supplies must not be of the kind the server derives for its own runs.

use uuid::Uuid;

use crate::runs::constants::DERIVED_RUN_ID_VERSION;
use crate::store::errors::ControlError;

/// Refuses a name-based run ID, the kind the server derives for group members and retries.
pub(in crate::runs) fn refuse_derived_run_id(run_id: Uuid) -> Result<(), ControlError> {
if run_id.get_version_num() != DERIVED_RUN_ID_VERSION {
return Ok(());
}
Err(ControlError::Invalid(format!(
"run_id {run_id} is a name-based UUID (version 5), which the server reserves for the IDs it \
derives for group members and retries; send a random (version 4) or time-ordered \
(version 7) UUID"
)))
}
2 changes: 2 additions & 0 deletions crates/sqlbuild-orchestration/src/runs/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ pub(crate) const DEFAULT_RUN_LOG_RETENTION_COUNT: u16 = 200;
pub(crate) const RUN_LOG_RETENTION_BATCH: usize = 10;
/// Artifact digests checked for remaining references per statement.
pub(crate) const RUN_LOG_DIGEST_CHUNK: usize = 500;
/// The UUID version of the run IDs the server derives from other IDs, which people may not supply.
pub(crate) const DERIVED_RUN_ID_VERSION: usize = 5;
3 changes: 3 additions & 0 deletions crates/sqlbuild-orchestration/src/runs/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ impl RunRequest {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(deny_unknown_fields)]
pub struct JobRunRequest {
/// A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
pub run_id: Uuid,
pub location_id: Uuid,
pub target: String,
Expand All @@ -211,6 +212,7 @@ pub struct JobRunRequest {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(deny_unknown_fields)]
pub struct RerunRequest {
/// A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
pub run_id: Uuid,
pub source_run_id: Uuid,
pub from_failure: bool,
Expand Down Expand Up @@ -742,6 +744,7 @@ pub struct RunObservation {
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, utoipa::ToSchema)]
#[serde(deny_unknown_fields)]
pub struct AdhocRunRequest {
/// A new random (UUID v4) or time-ordered (UUID v7) ID; name-based (v5) IDs are refused as the server derives those.
pub run_id: Uuid,
pub location_id: Uuid,
pub target: String,
Expand Down
2 changes: 2 additions & 0 deletions crates/sqlbuild-orchestration/tests/api/test_contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ mod test_publish_encoding;
mod test_reads;
#[path = "test_resource_costs.rs"]
mod test_resource_costs;
#[path = "test_run_ids.rs"]
mod test_run_ids;
#[path = "test_server_errors.rs"]
mod test_server_errors;
#[path = "test_statistics_recovery.rs"]
Expand Down
141 changes: 141 additions & 0 deletions crates/sqlbuild-orchestration/tests/api/test_run_ids.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//! People cannot start runs under the name-based IDs the server derives for its own runs.

use serde_json::{Value, json};
use uuid::{Builder, Uuid};

use crate::api::models::{ApiAuthentication, HumanLogin};
use crate::api::tests::denials_helpers::{job, person_send};
use crate::api::tests::helpers::{start_api_with_authentication, stop_api};
use crate::api::tests::test_types::RunIdRefusalTestCase;
use crate::credentials::tests::role_helpers::role_fixture;
use crate::runs::main::request_job_run::request_job_run;
use crate::runs::models::JobRunRequest;
use crate::runs::tests::helpers::snapshot;
use crate::snapshots::main::publish_snapshot::publish_snapshot;
use crate::store::main::migrate_store::migrate_store;
use crate::store::tests::store_helpers::{close_test_store, open_test_store, run};
use crate::store::tests::store_test_types::Backend;

#[test]
fn given_a_name_based_run_id_when_a_person_requests_a_run_rerun_or_adhoc_run_then_it_is_refused() {
let test_cases = [
RunIdRefusalTestCase {
description: "SQLite run IDs",
backend: Backend::Sqlite,
expected_refusal_status: 400,
expected_random_status: 200,
},
RunIdRefusalTestCase {
description: "PostgreSQL run IDs",
backend: Backend::Postgres,
expected_refusal_status: 400,
expected_random_status: 200,
},
];
for test_case in test_cases {
run(async {
let Some(fixture) = open_test_store(test_case.backend).await else {
return;
};
let store = &fixture.store;
migrate_store(store).await.expect("migrate");
let roles = role_fixture(store).await;
let mut published = snapshot(roles.location_id);
published.generation = 2;
published.documents.insert(
"jobs".into(),
json!({"orders": {"description": "Build orders", "selection": ["orders"]}}),
);
publish_snapshot(store, &published).await.expect("snapshot");
let source = JobRunRequest {
run_id: Uuid::now_v7(),
location_id: roles.location_id,
target: "development".into(),
job_key: "orders".into(),
reason: "Refresh orders".into(),
priority: 0,
window: None,
full_refresh: false,
select: Vec::new(),
exclude: Vec::new(),
};
assert!(
request_job_run(store, (&roles.context, None), &source)
.await
.expect("source run")
);
let api = start_api_with_authentication(
store,
ApiAuthentication {
login: HumanLogin::Password("https://control.example.com".into()),
..ApiAuthentication::default()
},
)
.await;
let client = reqwest::Client::new();
let session = (
roles.administrator.bearer.as_str(),
roles.administrator.csrf.as_str(),
);
let derived = Uuid::new_v5(&source.run_id, b"orders");
let mut job_run = job(roles.location_id, "orders");
job_run["run_id"] = json!(derived);
let requests = [
("runs", job_run),
(
"runs/adhoc",
json!({"run_id": derived, "location_id": roles.location_id,
"target": "development", "command": ["build", "--select", "orders"],
"reason": "Refresh orders", "priority": 0}),
),
(
"runs/rerun",
json!({"run_id": derived, "source_run_id": source.run_id,
"from_failure": false, "reason": "Retry orders", "priority": 0}),
),
];
let mut refusals = Vec::new();
for (path, body) in requests {
let url = format!("{}/api/control/{path}", api.url);
let response = person_send(&client, (&url, session.0, session.1), Some(body)).await;
let status = response.status().as_u16();
let body: Value = response.json().await.expect("JSON refusal");
refusals.push((path, status, body["error"].clone()));
}
let reason = json!(format!(
"run_id {derived} is a name-based UUID (version 5), which the server reserves for \
the IDs it derives for group members and retries; send a random (version 4) or \
time-ordered (version 7) UUID"
));
assert_eq!(
refusals,
vec![
("runs", test_case.expected_refusal_status, reason.clone()),
(
"runs/adhoc",
test_case.expected_refusal_status,
reason.clone()
),
("runs/rerun", test_case.expected_refusal_status, reason),
],
"{}: every person run request refuses a name-based run ID",
test_case.description
);
let mut random = job(roles.location_id, "orders");
random["run_id"] = json!(Builder::from_random_bytes([7; 16]).into_uuid());
let url = format!("{}/api/control/runs", api.url);
let response = person_send(&client, (&url, session.0, session.1), Some(random)).await;
assert_eq!(
(
response.status().as_u16(),
response.json::<Value>().await.expect("JSON")
),
(test_case.expected_random_status, json!({"applied": true})),
"{}: a random run ID is still accepted",
test_case.description
);
stop_api(api).await;
close_test_store(fixture).await;
});
}
}
7 changes: 7 additions & 0 deletions crates/sqlbuild-orchestration/tests/api/test_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ pub(crate) struct DenialTestCase {
pub(crate) expected_status: u16,
}

pub(crate) struct RunIdRefusalTestCase {
pub(crate) description: &'static str,
pub(crate) backend: Backend,
pub(crate) expected_refusal_status: u16,
pub(crate) expected_random_status: u16,
}

pub(crate) struct CustomRoleApiTestCase {
pub(crate) description: &'static str,
pub(crate) backend: Backend,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,14 @@ command selects (`selection`), the nodes it writes (`write_set`), the options th
command that can't run whatever your permissions, and `launchable` is `true` when the launch
would be accepted. A dry run queues nothing and records no audit entry.

To launch, `POST /api/control/runs/adhoc` with the same fields plus a new `run_id` and a
`reason`. The response is `{"applied": true}`; sending the same `run_id` again returns
`{"applied": false}` and starts nothing. The server resolves selectors against the current
publication, and the worker runs exactly the resolved nodes, from the project it verified against
that publication; files that change on the location after that check are not used. The target is
always the one in the request.
To launch, `POST /api/control/runs/adhoc` with the same fields plus a `reason` and a new `run_id`,
a random (version 4) or time-ordered (version 7) UUID. The response is `{"applied": true}`;
sending the same `run_id` again returns `{"applied": false}` and starts nothing. Job runs and
reruns take a `run_id` the same way. The server refuses a name-based (version 5) `run_id` with a
400, because it derives those IDs for the runs it starts itself. The server resolves selectors
against the current publication, and the worker runs exactly the resolved nodes, from the project
it verified against that publication; files that change on the location after that check are not
used. The target is always the one in the request.

| Verb | What the worker runs |
| --- | --- |
Expand Down
Loading
Loading