-
Notifications
You must be signed in to change notification settings - Fork 40
fix: accept a table ARN as TableName in data-plane operations #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
yesyayen
wants to merge
1
commit into
ExtendDB:main
Choose a base branch
from
yesyayen:fix/table-arn-as-name
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| // Copyright 2026 ExtendDB contributors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| //! Resolve a table ARN supplied in place of a bare `TableName`. | ||
|
|
||
| use crate::error::DynamoDbError; | ||
|
|
||
| /// Resolve a `TableName` that may be supplied as a table ARN to its bare name. | ||
| /// | ||
| /// A non-ARN value is returned unchanged. A table ARN | ||
| /// (`arn:<partition>:<vendor>:<region>:<account>:table/<name>`) resolves to | ||
| /// `<name>`; the account and region are ignored, so the name resolves within | ||
| /// the caller's account. This matches Amazon DynamoDB and DynamoDB Local, which | ||
| /// both accept a table ARN wherever a table name is expected. Index, non-table, | ||
| /// or malformed ARNs are rejected as a validation error. | ||
| /// | ||
| /// # Errors | ||
| /// | ||
| /// Returns `ValidationException` when `name` begins with `arn:` but is not a | ||
| /// well-formed `table/<name>` ARN. | ||
| pub fn resolve_table_arn(name: &str) -> Result<&str, DynamoDbError> { | ||
| if !name.starts_with("arn:") { | ||
| return Ok(name); | ||
| } | ||
| // arn:<partition>:<vendor>:<region>:<account>:<resource>. The 6th field | ||
| // keeps everything after the 5th colon, including any slashes. | ||
| let resource = match name.splitn(6, ':').nth(5) { | ||
| Some(resource) if !resource.is_empty() => resource, | ||
| _ => return Err(invalid_arn_format(name)), | ||
| }; | ||
| let table = match resource.split_once('/') { | ||
| Some(("table", table)) => table, | ||
| Some(_) => return Err(constraint_error(name, "Invalid resource type")), | ||
| None => return Err(invalid_arn_format(name)), | ||
| }; | ||
| // A bare table name only: an index ARN (`table/T/index/i`) leaves a slash | ||
| // here and fails the character check below. | ||
| if table.len() < 3 { | ||
| return Err(constraint_error( | ||
| name, | ||
| "Table name of ARN must have length greater than or equal to 3", | ||
| )); | ||
| } | ||
| if table.len() > 255 | ||
| || !table | ||
| .chars() | ||
| .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '.' || c == '-') | ||
| { | ||
| return Err(invalid_arn_format(name)); | ||
| } | ||
| Ok(table) | ||
| } | ||
|
|
||
| fn constraint_error(arn: &str, constraint: &str) -> DynamoDbError { | ||
| DynamoDbError::ValidationException(format!( | ||
| "1 validation error detected: Value '{arn}' at 'tableName' failed to satisfy constraint: \ | ||
| {constraint}" | ||
| )) | ||
| } | ||
|
|
||
| fn invalid_arn_format(arn: &str) -> DynamoDbError { | ||
| constraint_error( | ||
| arn, | ||
| "Valid ARN format is 'arn:<awsPartition>:<vendor>:<region>:<subscriber>:resourceType/resourceName', \ | ||
| where Resource name must have length less than or equal to 255, Resource name must have length \ | ||
| greater than or equal to 3, Resource name must satisfy regular expression pattern: [a-zA-Z0-9_.-]+", | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn err_msg(name: &str) -> String { | ||
| match resolve_table_arn(name) { | ||
| Err(DynamoDbError::ValidationException(m)) => m, | ||
| other => panic!("expected ValidationException, got {other:?}"), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_arn_passthrough() { | ||
| assert_eq!(resolve_table_arn("my-table").unwrap(), "my-table"); | ||
| // A plain name with a colon is not an ARN and is left for the normal | ||
| // table-name validator to reject. | ||
| assert_eq!(resolve_table_arn("foo:bar").unwrap(), "foo:bar"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn table_arn_resolves_to_bare_name() { | ||
| let arn = "arn:aws:dynamodb:us-east-1:123456789012:table/my-table"; | ||
| assert_eq!(resolve_table_arn(arn).unwrap(), "my-table"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn account_and_region_are_ignored() { | ||
| // Any account/region resolves to the same bare name. | ||
| let a = "arn:aws:dynamodb:us-east-1:000000000000:table/T.able_1"; | ||
| let b = "arn:aws:dynamodb:eu-west-1:999999999999:table/T.able_1"; | ||
| assert_eq!(resolve_table_arn(a).unwrap(), "T.able_1"); | ||
| assert_eq!(resolve_table_arn(b).unwrap(), "T.able_1"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn index_arn_rejected() { | ||
| let arn = "arn:aws:dynamodb:us-east-1:123456789012:table/my-table/index/gsi1"; | ||
| assert!(err_msg(arn).contains("Valid ARN format is")); | ||
| assert!(err_msg(arn).contains("[a-zA-Z0-9_.-]+")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_table_resource_type_rejected() { | ||
| let arn = "arn:aws:dynamodb:us-east-1:123456789012:stream/my-table"; | ||
| assert!(err_msg(arn).contains("Invalid resource type")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn empty_table_name_rejected() { | ||
| let arn = "arn:aws:dynamodb:us-east-1:123456789012:table/"; | ||
| assert!( | ||
| err_msg(arn).contains("Table name of ARN must have length greater than or equal to 3") | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn malformed_arn_missing_resource_rejected() { | ||
| assert!(err_msg("arn:aws:dynamodb").contains("Valid ARN format is")); | ||
| assert!( | ||
| err_msg("arn:aws:dynamodb:us-east-1:123456789012:").contains("Valid ARN format is") | ||
| ); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method brings to mind "parse, don't validate". Is it possible that eventually some of the other ARN elements will be useful? If so, is it worth considering now parsing a table arn into its constituent parts? If it fails to parse correctly, it's invalid. Otherwise, callers can access the specific parts they need. I'd be okay with deferring this in order to resolve the immediate problem, but wanted to raise the suggestion.