Skip to content
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

replace ci prefix only at rewriting part #1681

Merged
merged 4 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions deployment/lambdas/cf-url-rewriter/cf-url-rewriter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/")) {

Expand All @@ -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);
}
}
Expand Down
66 changes: 32 additions & 34 deletions deployment/test/lambdas/cf-url-rewriter/cf-url-rewriter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,66 +9,61 @@ 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;

(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(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"
}
);
});

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,
{
Expand All @@ -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,
{
Expand All @@ -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;
Expand All @@ -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;

Expand Down