From b6a6c0f8e482817e9ec7176063c8c7d3a97c6d8e Mon Sep 17 00:00:00 2001 From: Tianle Huang Date: Tue, 1 Mar 2022 19:52:29 +0000 Subject: [PATCH 1/4] replace ci prefix only at rewriting part Signed-off-by: Tianle Huang --- deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts | 4 ++-- .../test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts b/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts index d38b03620e..108cf96c1d 100644 --- a/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts +++ b/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts @@ -3,8 +3,6 @@ import { httpsGet } from './https-get'; export async function handler(event: CloudFrontRequestEvent, context: Context, callback: CloudFrontRequestCallback) { const request = event.Records[0].cf.request; - // Incoming URLs from ci.opensearch.org will have a '/ci/123/' prefix, remove the prefix path from requests into S3. - request.uri = request.uri.replace(/^\/ci\/...\//, '\/'); if (request.uri.includes("/latest/")) { @@ -24,6 +22,8 @@ export async function handler(event: CloudFrontRequestEvent, context: Context, c } } else { + // Incoming URLs from ci.opensearch.org will have a '/ci/123/' prefix, remove the prefix path from requests into S3. + request.uri = request.uri.replace(/^\/ci\/...\//, '\/'); callback(null, request); } } diff --git a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts index c56be7213f..ceb76fe229 100644 --- a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts +++ b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts @@ -42,14 +42,14 @@ test('handler with latest url and with ci keyword and valid latest field', async await handler(event, context, callback); - expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/bundle-build-dashboards/1.2.0/index.json'); + expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.2.0/index.json'); expect(callback).toHaveBeenCalledWith( null, { "headers": { "cache-control": [{ "key": "Cache-Control", "value": "max-age=3600" }], - "location": [{ "key": "Location", "value": "/bundle-build-dashboards/1.2.0/123/linux/x64/" }] + "location": [{ "key": "Location", "value": "/ci/dbc/bundle-build-dashboards/1.2.0/123/linux/x64/" }] }, "status": "302", "statusDescription": "Moved temporarily" From d3cd0664338fffb027a735d54fbefb7f23c11435 Mon Sep 17 00:00:00 2001 From: Tianle Huang Date: Tue, 1 Mar 2022 23:38:34 +0000 Subject: [PATCH 2/4] add unit test for /ci/dbc Signed-off-by: Tianle Huang --- .../cf-url-rewriter/cf-url-rewriter.ts | 5 ++ .../cf-url-rewriter/cf-url-rewriter.test.ts | 66 +++++++++---------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts b/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts index 108cf96c1d..7e704e21d5 100644 --- a/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts +++ b/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts @@ -4,6 +4,11 @@ import { httpsGet } from './https-get'; export async function handler(event: CloudFrontRequestEvent, context: Context, callback: CloudFrontRequestCallback) { const request = event.Records[0].cf.request; + if (!request.uri.includes('/ci/dbc/')) { + callback(null, errorResponse()); + return; + } + if (request.uri.includes("/latest/")) { const indexUri = request.uri.replace(/\/latest\/.*/, '/index.json'); diff --git a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts index ceb76fe229..b0164c5aeb 100644 --- a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts +++ b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts @@ -9,7 +9,7 @@ beforeEach(() => { test('handler with latest url and valid latest field', async () => { - const event = createTestEvent('/bundle-build-dashboards/1.2.0/latest/linux/x64/'); + const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/latest/linux/x64/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; @@ -17,14 +17,14 @@ test('handler with latest url and valid latest field', async () => { await handler(event, context, callback); - expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/bundle-build-dashboards/1.2.0/index.json'); + expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.2.0/index.json'); expect(callback).toHaveBeenCalledWith( null, { "headers": { "cache-control": [{ "key": "Cache-Control", "value": "max-age=3600" }], - "location": [{ "key": "Location", "value": "/bundle-build-dashboards/1.2.0/123/linux/x64/" }] + "location": [{ "key": "Location", "value": "/ci/dbc/bundle-build-dashboards/1.2.0/123/linux/x64/" }] }, "status": "302", "statusDescription": "Moved temporarily" @@ -32,9 +32,9 @@ test('handler with latest url and valid latest field', async () => { ); }); -test('handler with latest url and with ci keyword and valid latest field', async () => { +test('handler with latest url and without ci keyword and valid latest field', async () => { - const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/latest/linux/x64/'); + const event = createTestEvent('/bundle-build-dashboards/1.2.0/latest/linux/x64/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; @@ -42,33 +42,28 @@ test('handler with latest url and with ci keyword and valid latest field', async await handler(event, context, callback); - expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.2.0/index.json'); + expect(httpsGet).not.toHaveBeenCalled(); expect(callback).toHaveBeenCalledWith( null, { - "headers": { - "cache-control": [{ "key": "Cache-Control", "value": "max-age=3600" }], - "location": [{ "key": "Location", "value": "/ci/dbc/bundle-build-dashboards/1.2.0/123/linux/x64/" }] - }, - "status": "302", - "statusDescription": "Moved temporarily" + "body": "The page is not found!", + "status": "404", + "statusDescription": "Not found" } ); }); -test('handler with latest url and empty latest field', async () => { +test('handler without latest url and without ci keyword', async () => { - const event = createTestEvent('/bundle-build-dashboards/1.2.0/latest/linux/x64/'); + const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; - (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '' }); + (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '123' }); await handler(event, context, callback); - expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/bundle-build-dashboards/1.2.0/index.json'); - expect(callback).toHaveBeenCalledWith( null, { @@ -77,22 +72,20 @@ test('handler with latest url and empty latest field', async () => { "statusDescription": "Not found" } ); -}); -test('handler with latest url and exception when getting index.json', async () => { + expect(httpsGet).not.toHaveBeenCalled(); +}) + +test('handler with latest url and without ci keyword', async () => { const event = createTestEvent('/bundle-build-dashboards/1.2.0/latest/linux/x64/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; - (httpsGet as unknown as jest.Mock).mockImplementation(() => { - throw new Error('Error getting!'); - }); + (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '' }); await handler(event, context, callback); - expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/bundle-build-dashboards/1.2.0/index.json'); - expect(callback).toHaveBeenCalledWith( null, { @@ -101,30 +94,35 @@ test('handler with latest url and exception when getting index.json', async () = "statusDescription": "Not found" } ); + + expect(httpsGet).not.toHaveBeenCalled(); }); -test('handler without latest url and without ci keyword', async () => { +test('handler with latest url and exception when getting index.json', async () => { - const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/'); + const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/latest/linux/x64/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; - (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '123' }); + (httpsGet as unknown as jest.Mock).mockImplementation(() => { + throw new Error('Error getting!'); + }); await handler(event, context, callback); + expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.2.0/index.json'); + expect(callback).toHaveBeenCalledWith( null, { - "headers": { "host": [{ "key": "Host", "value": "test.cloudfront.net" }] }, - "uri": "/bundle-build-dashboards/1.2.0/456/linux/x64/" + "body": "The page is not found!", + "status": "404", + "statusDescription": "Not found" } ); +}); - expect(httpsGet).not.toHaveBeenCalled(); -}) - -test('handler without latest url and with ci keyword', async () => { +test('handler without latest url', async () => { const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/456/linux/x64/'); const context = {} as Context; @@ -147,7 +145,7 @@ test('handler without latest url and with ci keyword', async () => { test('handler with /fool(latest)bar/ keyword', async () => { - const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/foollatestbar/'); + const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/456/linux/x64/foollatestbar/'); const context = {} as Context; const callback = jest.fn() as CloudFrontRequestCallback; From 2c40945ce3852a45e66aa77b17dd0c99df1e2860 Mon Sep 17 00:00:00 2001 From: Tianle Huang Date: Tue, 1 Mar 2022 23:42:35 +0000 Subject: [PATCH 3/4] remove duplicated Signed-off-by: Tianle Huang --- .../cf-url-rewriter/cf-url-rewriter.test.ts | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts index b0164c5aeb..6fb69ea5ed 100644 --- a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts +++ b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts @@ -32,28 +32,6 @@ test('handler with latest url and valid latest field', async () => { ); }); -test('handler with latest url and without ci keyword and valid latest field', async () => { - - const event = createTestEvent('/bundle-build-dashboards/1.2.0/latest/linux/x64/'); - const context = {} as Context; - const callback = jest.fn() as CloudFrontRequestCallback; - - (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '123' }); - - await handler(event, context, callback); - - expect(httpsGet).not.toHaveBeenCalled(); - - expect(callback).toHaveBeenCalledWith( - null, - { - "body": "The page is not found!", - "status": "404", - "statusDescription": "Not found" - } - ); -}); - test('handler without latest url and without ci keyword', async () => { const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/'); From d4170b6215abca61d74a365384eb0b149c2c9631 Mon Sep 17 00:00:00 2001 From: Tianle Huang Date: Tue, 1 Mar 2022 23:55:54 +0000 Subject: [PATCH 4/4] add one more test case Signed-off-by: Tianle Huang --- .../cf-url-rewriter/cf-url-rewriter.test.ts | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts index 6fb69ea5ed..9f51e41fcb 100644 --- a/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts +++ b/deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts @@ -32,6 +32,28 @@ test('handler with latest url and valid latest field', async () => { ); }); +test('handler with latest url and empty latest field', async () => { + + const event = createTestEvent('/ci/dbc/bundle-build-dashboards/1.2.0/latest/linux/x64/'); + const context = {} as Context; + const callback = jest.fn() as CloudFrontRequestCallback; + + (httpsGet as unknown as jest.Mock).mockReturnValue({ latest: '' }); + + await handler(event, context, callback); + + expect(httpsGet).toBeCalledWith('https://test.cloudfront.net/ci/dbc/bundle-build-dashboards/1.2.0/index.json'); + + expect(callback).toHaveBeenCalledWith( + null, + { + "body": "The page is not found!", + "status": "404", + "statusDescription": "Not found" + } + ); +}); + test('handler without latest url and without ci keyword', async () => { const event = createTestEvent('/bundle-build-dashboards/1.2.0/456/linux/x64/');