From 9c24976f97389aa89ab0eb7aac096b7d444a59ce Mon Sep 17 00:00:00 2001 From: Tianle Huang <60111637+tianleh@users.noreply.github.com> Date: Tue, 1 Mar 2022 16:14:24 -0800 Subject: [PATCH] replace ci prefix only at rewriting part (#1681) Signed-off-by: Tianle Huang --- .../cf-url-rewriter/cf-url-rewriter.ts | 9 ++- .../cf-url-rewriter/cf-url-rewriter.test.ts | 66 +++++++++---------- 2 files changed, 39 insertions(+), 36 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..7e704e21d5 100644 --- a/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts +++ b/deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts @@ -3,8 +3,11 @@ 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('/ci/dbc/')) { + callback(null, errorResponse()); + return; + } if (request.uri.includes("/latest/")) { @@ -24,6 +27,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..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 @@ -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,43 +32,38 @@ 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 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: '123' }); + (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(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/" }] - }, - "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;