Skip to content

Commit

Permalink
fix(util-endpoints): check for entire resource-path being empty
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Aug 12, 2024
1 parent 88d384c commit 5db2f68
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
10 changes: 10 additions & 0 deletions packages/util-endpoints/src/lib/aws/parseArn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ describe(parseArn.name, () => {
resourceId: ["accesspoint", "myendpoint"],
},
],
[
"arn:aws:s3:us-west-2:123456789012::myendpoint",
{
partition: "aws",
service: "s3",
region: "us-west-2",
accountId: "123456789012",
resourceId: ["", "myendpoint"],
},
],
[
"arn:aws:s3:us-west-2:123456789012:accesspoint/myendpoint",
{
Expand Down
15 changes: 11 additions & 4 deletions packages/util-endpoints/src/lib/aws/parseArn.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import { EndpointARN } from "@smithy/types";

const ARN_DELIMITER = ":";
const RESOURCE_DELIMITER = "/";

/**
* Evaluates a single string argument value, and returns an object containing
* details about the parsed ARN.
* If the input was not a valid ARN, the function returns null.
*/
export const parseArn = (value: string): EndpointARN | null => {
const segments = value.split(":");
const segments = value.split(ARN_DELIMITER);

if (segments.length < 6) return null;

const [arn, partition, service, region, accountId, ...resourceId] = segments;
const [arn, partition, service, region, accountId, ...resourcePath] = segments;

if (arn !== "arn" || partition === "" || service === "" || resourcePath.join(ARN_DELIMITER) === "") return null;

if (arn !== "arn" || partition === "" || service === "" || resourceId[0] === "") return null;
const resourceId = resourcePath[0].includes(RESOURCE_DELIMITER)
? resourcePath[0].split(RESOURCE_DELIMITER)
: resourcePath;

return {
partition,
service,
region,
accountId,
resourceId: resourceId[0].includes("/") ? resourceId[0].split("/") : resourceId,
resourceId,
};
};

0 comments on commit 5db2f68

Please sign in to comment.