From 3e5b73c767939225aa0c63c05b6d8af3bdf02338 Mon Sep 17 00:00:00 2001 From: Felix Kaiser Date: Sun, 6 Apr 2025 17:41:21 -0700 Subject: [PATCH] Add support for apigateway --- link2aws.js | 11 ++++++++--- test/test.js | 14 +++++++++++++- testcases/aws.json | 2 ++ 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/link2aws.js b/link2aws.js index 121286a..61813a1 100644 --- a/link2aws.js +++ b/link2aws.js @@ -57,8 +57,13 @@ class ARN { } } - // ...:resource-type/resource-id (resource-id can contain slashes!) - else if (typeof (tokens[5]) != 'undefined' && tokens[5].indexOf('/') > 0) { + // ...:resource-type/resource-id (common case) + // ...:/resource-type/resource-id (apigateway) + // resource-id can contain slashes. + else if (typeof (tokens[5]) != 'undefined' && tokens[5].slice(1).indexOf('/') >= 0) { + if (tokens[5].startsWith('/')) { + tokens[5] = tokens[5].slice(1); + } this.resource_type = tokens[5].slice(0, tokens[5].indexOf('/')); this.resource = tokens[5].slice(tokens[5].indexOf('/') + 1, tokens[5].length); this.hasPath = true; @@ -182,7 +187,7 @@ class ARN { }, }, "apigateway": { // Manage Amazon API Gateway - "": null, + "restapis": () => `https://${this.region}.${this.console}/apigateway/main/apis/${this.resource}/resources?api=${this.resource}®ion=${this.region}`, }, "appconfig": { // AWS AppConfig "application": null, diff --git a/test/test.js b/test/test.js index bdb62ce..2f925b0 100644 --- a/test/test.js +++ b/test/test.js @@ -40,7 +40,19 @@ describe('main.ARN', function () { assert.equal(arn.account, 'account-id'); assert.equal(arn.resource, 'resource-id'); assert.equal(arn.resource_type, 'resource-type'); - }); + }); + + it('should tokenize ARNs with /resource-type/resource-id', function () { + var arn = new main.ARN('arn:partition:service:region:account-id:/resource-type/resource-id'); + assert.equal(arn.arn, 'arn:partition:service:region:account-id:/resource-type/resource-id') + assert.equal(arn.prefix, 'arn'); + assert.equal(arn.partition, 'partition'); + assert.equal(arn.service, 'service'); + assert.equal(arn.region, 'region'); + assert.equal(arn.account, 'account-id'); + assert.equal(arn.resource, 'resource-id'); + assert.equal(arn.resource_type, 'resource-type'); + }); it('should tokenize ARNs with resource-type:resource-id', function () { var arn = new main.ARN('arn:partition:service:region:account-id:resource-type:resource-id'); diff --git a/testcases/aws.json b/testcases/aws.json index 515d36b..d56100c 100644 --- a/testcases/aws.json +++ b/testcases/aws.json @@ -6,6 +6,8 @@ "arn:aws:acm:us-east-1:123456789012:certificate/1f6ee793-4064-4a10-9567-f03875640b35": "https://console.aws.amazon.com/acm/home?region=us-east-1#/?id=1f6ee793-4064-4a10-9567-f03875640b35", + "arn:aws:apigateway:us-west-1::/restapis/abc123defg": "https://us-west-1.console.aws.amazon.com/apigateway/main/apis/abc123defg/resources?api=abc123defg®ion=us-west-1", + "arn:aws:ec2:us-west-1:136693071363:image/ami-0851c4af3ebd71c35": "https://us-west-1.console.aws.amazon.com/ec2/home?region=us-west-1#ImageDetails:imageId=ami-0851c4af3ebd71c35", "arn:aws:ec2:us-east-1:123456789012:instance/1234": "https://us-east-1.console.aws.amazon.com/ec2/home?region=us-east-1#InstanceDetails:instanceId=1234", "arn:aws:ec2:us-east-1:123456789012:natgateway/nat-123456": "https://us-east-1.console.aws.amazon.com/vpcconsole/home?region=us-east-1#NatGatewayDetails:natGatewayId=nat-123456",