Skip to content
Open
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
61 changes: 60 additions & 1 deletion crates/engine/src/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub async fn handle_create_table(
mut body: Value,
ctx: &OperationContext,
) -> Result<Value, DynamoDbError> {
// The service answers a TableClass outside its set ahead of the
// BillingMode fault, which is checked here before the read.
let table_class = crate::nested_enums::table_class_fault(&body);
crate::validate_enum_fields(
&body,
&[
Expand All @@ -27,7 +30,8 @@ pub async fn handle_create_table(
clause: crate::EnumClause::Named("tableThroughputMode"),
},
],
)?;
)
.map_err(|e| table_class.clone().unwrap_or(e))?;

crate::require_stream_enabled_member(&body)?;
let enum_faults = crate::nested_enums::EnumFaults::create_table(&mut body)?;
Expand All @@ -50,10 +54,18 @@ pub async fn handle_create_table(
))
}
})?;
read_sse_enabled(input.sse_specification.as_mut())?;
// The service reads the body and checks the table name length before it
// answers the table class fault.
if let Some(fault) = table_class {
extenddb_core::validation::validate_table_name(&input.table_name, &ctx.limits)?;
return Err(fault);
}
enum_faults.check()?;
input.resolve_table_throughput_mode();

validate_create_table(&input, &ctx.limits)?;
check_sse_specification(input.sse_specification.as_ref())?;

// An empty SearchSchema means the same as an absent one, so it is collapsed
// here, once, rather than in each backend. Storing the empty list would make
Expand Down Expand Up @@ -113,6 +125,53 @@ pub async fn handle_create_table(
serialize_output(&output)
}

/// The service's refusals of an `SSESpecification` whose members do not go
/// together, once [`read_sse_enabled`] has read its `Enabled`. They come
/// after the member faults. An `Enabled` that is not a boolean is not
/// checked.
fn check_sse_specification(spec: Option<&Value>) -> Result<(), DynamoDbError> {
let Some(spec) = spec else {
return Ok(());
};
let sse_type = spec.get("SSEType").and_then(Value::as_str);
let key = spec.get("KMSMasterKeyId").is_some_and(|k| !k.is_null());
let reason = match spec.get("Enabled").and_then(Value::as_bool) {
Some(false) if sse_type.is_some() => "SSEType can not be specified if Enabled is false",
Some(false) if key => "KMSMasterKeyId can not be specified if Enabled is false",
Some(true) if sse_type == Some("AES256") => "SSEType AES256 is not supported",
Some(true) if key && sse_type != Some("KMS") => {
"SSEType KMS is required if KMSMasterKeyId is specified"
}
_ => return Ok(()),
};
Err(DynamoDbError::ValidationException(format!(
"One or more parameter values were invalid: {reason}"
)))
}

/// Writes an `Enabled` given as a string back as the boolean the service
/// reads it as, so that the checks and the backends read the same value: a
/// string of [`crate::query_key_values::boolean_text`], in any ASCII case.
/// The service refuses any other string while it reads the body. Measured on
/// UpdateTable on 2026-12-02, each body twice: `"FALSE"`, `"False"`, `"no"`,
/// `"f"`, `"0"` and `"N"` read as false, `"TRUE"`, `"true"`, `"t"` and `"1"`
/// as true, and `""`, `"on"` and `"maybe"` answer `Unexpected token received
/// from parser`.
fn read_sse_enabled(spec: Option<&mut Value>) -> Result<(), DynamoDbError> {
let Some(enabled) = spec.and_then(|spec| spec.get_mut("Enabled")) else {
return Ok(());
};
if let Some(text) = enabled.as_str() {
let read = crate::query_key_values::boolean_text(text).ok_or_else(|| {
DynamoDbError::SerializationException(
"Unexpected token received from parser".to_owned(),
)
})?;
*enabled = Value::Bool(read);
}
Ok(())
}

/// Map a storage-layer error to its `DynamoDB` wire error.
///
/// Exposed so the server's authorization path classifies a key-info lookup
Expand Down
57 changes: 51 additions & 6 deletions crates/engine/src/nested_enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
// SPDX-License-Identifier: Apache-2.0

//! The `ProjectionType` of each index and the `StreamViewType` of a
//! `CreateTable` or `UpdateTable` body, which [`crate::validate_enum_fields`]
//! does not reach.
//! `CreateTable` or `UpdateTable` body, and the `SSEType` and
//! `KMSMasterKeyId` of a `CreateTable` body, which
//! [`crate::validate_enum_fields`] does not reach. Also the `TableClass` of
//! both, which the service refuses with its own sentence.
//!
//! The typed read refuses a value outside its enum with a
//! `SerializationException`. The service answers a `ValidationException`
//! naming each such member, in its own order, once the body is read
//! (measured 2026-11-13, 2026-11-14 and 2026-11-17, us-east-1). So
//! [`EnumFaults`] takes the faults out of the body before the read.
//! (measured 2026-11-13, 2026-11-14, 2026-11-17 and 2026-11-18, us-east-1).
//! So [`EnumFaults`] takes the faults out of the body before the read.
//!
//! The typed read takes a structure written as a JSON array and an enum
//! written as a JSON object, which the service refuses while it reads the
Expand All @@ -26,6 +28,27 @@ const PROJECTION_TYPES: &[&str] = &["ALL", "INCLUDE", "KEYS_ONLY"];
/// The values of `StreamViewType`, in the service's order.
const STREAM_VIEW_TYPES: &[&str] = &["OLD_IMAGE", "KEYS_ONLY", "NEW_AND_OLD_IMAGES", "NEW_IMAGE"];

/// The values of `SSEType`, in the service's order.
const SSE_TYPES: &[&str] = &["AES256", "KMS"];

/// The values of `TableClass`, in the service's order.
const TABLE_CLASSES: &[&str] = &["STANDARD", "STANDARD_INFREQUENT_ACCESS"];

/// The most characters a `KMSMasterKeyId` may hold.
const KMS_MASTER_KEY_ID_MAX: usize = 2048;

/// The service's answer to a `TableClass` outside its set.
pub(crate) fn table_class_fault(body: &Value) -> Option<DynamoDbError> {
let sent = body.get("TableClass")?.as_str()?;
(!TABLE_CLASSES.contains(&sent)).then(|| {
DynamoDbError::ValidationException(format!(
"Invalid table-class parameter provided. Please try again with a valid \
table-class value: [{}].",
TABLE_CLASSES.join(", ")
))
})
}

/// The most faults one refusal lists, the first ones in the service's order.
/// Its count then says 10 too (measured 2026-12-02 with twelve). The PartiQL
/// request checks keep the same number, in a module behind the `partiql`
Expand Down Expand Up @@ -70,15 +93,16 @@ const ENUMS: &[&str] = &[
"TableThroughputMode",
];

/// Each member outside its set: the value sent, the member's path and the
/// constraint it fails.
/// Each member outside its set or over its length: the value sent, the
/// member's path and the constraint it fails.
#[derive(Default)]
pub(crate) struct EnumFaults(Vec<(String, String, String)>);

impl EnumFaults {
pub(crate) fn create_table(body: &mut Value) -> Result<Self, DynamoDbError> {
let mut faults = Self::default();
faults.projections(body, "GlobalSecondaryIndexes", false);
faults.sse_specification(body);
faults.stream_view_type(body);
faults.projections(body, "LocalSecondaryIndexes", false);
faults.member_types(body)
Expand Down Expand Up @@ -132,6 +156,27 @@ impl EnumFaults {
}
}

/// The length of the `KMSMasterKeyId`, which the service counts in
/// characters, then the `SSEType`.
fn sse_specification(&mut self, body: &mut Value) {
if let Some(key) = body
.pointer("/SSESpecification/KMSMasterKeyId")
.and_then(Value::as_str)
&& key.chars().count() > KMS_MASTER_KEY_ID_MAX
{
self.0.push((
key.to_owned(),
"sSESpecification.kMSMasterKeyId".to_owned(),
format!("Member must have length less than or equal to {KMS_MASTER_KEY_ID_MAX}"),
));
}
self.one_of(
body.pointer_mut("/SSESpecification/SSEType"),
"sSESpecification.sSEType".to_owned(),
SSE_TYPES,
);
}

fn stream_view_type(&mut self, body: &mut Value) {
self.one_of(
body.pointer_mut("/StreamSpecification/StreamViewType"),
Expand Down
Loading
Loading