From 049bac7686508fcb02bfef5f9942032193a98530 Mon Sep 17 00:00:00 2001 From: Daniel Tal <87daniel.tal@gmail.com> Date: Mon, 11 Nov 2024 14:49:06 +0200 Subject: [PATCH 1/5] feat: add DRA endpoint - get image metadata --- .../container-registry-agent/accept.json.sample | 6 ++++++ defaultFilters/container-registry-agent.json | 8 +++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/client-templates/container-registry-agent/accept.json.sample b/client-templates/container-registry-agent/accept.json.sample index bd49ad07c..6fe995f81 100644 --- a/client-templates/container-registry-agent/accept.json.sample +++ b/client-templates/container-registry-agent/accept.json.sample @@ -77,6 +77,12 @@ "method": "POST", "path": "/scan", "origin": "${CR_AGENT_URL}" + }, + { + "//": "returns image-metadata for given repository and image reference", + "method": "GET", + "path": "/image-metadata/*", + "origin": "${CR_AGENT_URL}" } ] } diff --git a/defaultFilters/container-registry-agent.json b/defaultFilters/container-registry-agent.json index 7896f812d..392cff889 100644 --- a/defaultFilters/container-registry-agent.json +++ b/defaultFilters/container-registry-agent.json @@ -77,6 +77,12 @@ "method": "POST", "path": "/scan", "origin": "${CR_AGENT_URL}" + }, + { + "//": "returns image-metadata for given repository and image reference", + "method": "GET", + "path": "/image-metadata/*", + "origin": "${CR_AGENT_URL}" } ] - } \ No newline at end of file + } From 53872c7f97d56ec2b317223f25cd626b85b70b9c Mon Sep 17 00:00:00 2001 From: ciucky Date: Wed, 13 Nov 2024 17:19:11 +0200 Subject: [PATCH 2/5] feat: add filter for azure repos /pull-request endpoint --- .../azure-repos/accept.json.sample | 16 ++ defaultFilters/azure-repos.json | 16 ++ .../runtime-rules-hotloading.test.ts.snap | 228 ++++++++++++++++++ test/unit/filters.test.ts | 27 +++ 4 files changed, 287 insertions(+) diff --git a/client-templates/azure-repos/accept.json.sample b/client-templates/azure-repos/accept.json.sample index 501ffd516..a80885490 100644 --- a/client-templates/azure-repos/accept.json.sample +++ b/client-templates/azure-repos/accept.json.sample @@ -388,6 +388,22 @@ "scheme": "basic", "token": "${BROKER_CLIENT_VALIDATION_BASIC_AUTH}" } + }, + { + "//": "get pull-request info", + "method": "GET", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "origin": "https://${AZURE_REPOS_HOST}/${AZURE_REPOS_ORG}", + "valid": [ + { + "queryParam": "api-version", + "values": ["7.1", ""] + } + ], + "auth": { + "scheme": "basic", + "token": "${BROKER_CLIENT_VALIDATION_BASIC_AUTH}" + } } ] } diff --git a/defaultFilters/azure-repos.json b/defaultFilters/azure-repos.json index faf223982..654c2eb2a 100644 --- a/defaultFilters/azure-repos.json +++ b/defaultFilters/azure-repos.json @@ -388,6 +388,22 @@ "scheme": "basic", "token": "${BROKER_CLIENT_VALIDATION_BASIC_AUTH}" } + }, + { + "//": "get pull-request info", + "method": "GET", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "origin": "https://${AZURE_REPOS_HOST}/${AZURE_REPOS_ORG}", + "valid": [ + { + "queryParam": "api-version", + "values": ["7.1", ""] + } + ], + "auth": { + "scheme": "basic", + "token": "${BROKER_CLIENT_VALIDATION_BASIC_AUTH}" + } } ] } \ No newline at end of file diff --git a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap index 23b520828..bbb51b7d2 100644 --- a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap +++ b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap @@ -179,6 +179,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, Object { "//": "get file content. restrict by file types", "method": "GET", @@ -8026,6 +8045,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, Object { "//": "get core api's location for sanity check", "auth": Object { @@ -16591,6 +16629,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -24413,6 +24470,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -32374,6 +32450,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -40409,6 +40504,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -48402,6 +48516,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -48822,6 +48955,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -64576,6 +64728,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -72386,6 +72557,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -80196,6 +80386,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { @@ -88006,6 +88215,25 @@ Object { "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", "path": "/:owner/_apis/git/repositories/:repo/pullRequests/:pullRef/iterations", }, + Object { + "//": "get pull-request info", + "auth": Object { + "scheme": "basic", + "token": "\${BROKER_CLIENT_VALIDATION_BASIC_AUTH}", + }, + "method": "GET", + "origin": "https://\${AZURE_REPOS_HOST}/\${AZURE_REPOS_ORG}", + "path": "/:owner/_apis/git/repositories/:repo/pullrequests/:pullRef", + "valid": Array [ + Object { + "queryParam": "api-version", + "values": Array [ + "7.1", + "", + ], + }, + ], + }, ], "public": Array [ Object { diff --git a/test/unit/filters.test.ts b/test/unit/filters.test.ts index 514799538..980942152 100644 --- a/test/unit/filters.test.ts +++ b/test/unit/filters.test.ts @@ -235,6 +235,33 @@ describe('filters', () => { const filterResponseUrl = filterResponse ? filterResponse.url : ''; expect(filterResponseUrl).toMatch(url); }); + + it('should allow fetching pr info', () => { + const url = '/test-owner/_apis/git/repositories/test-repo/pullRequests/1'; + + const filterResponse = filter({ + url, + method: 'GET', + }); + + expect(filterResponse).not.toEqual(false); + const filterResponseUrl = filterResponse ? filterResponse.url : ''; + expect(filterResponseUrl).toMatch(url); + }); + + it('should allow fetching pr info with api-version', () => { + const url = + '/test-owner/_apis/git/repositories/test-repo/pullrequests/1?api-version=7.1'; + + const filterResponse = filter({ + url, + method: 'GET', + }); + + expect(filterResponse).not.toEqual(false); + const filterResponseUrl = filterResponse ? filterResponse.url : ''; + expect(filterResponseUrl).toMatch(url); + }); }); describe('for gitlab', () => { From 0f1fa229c17657d0881c0ada69581be8b4b1ee58 Mon Sep 17 00:00:00 2001 From: ciucky Date: Wed, 13 Nov 2024 17:27:46 +0200 Subject: [PATCH 3/5] feat: add filter for gitlab /pull-request endpoint --- client-templates/gitlab/accept.json.sample | 8 ++- defaultFilters/gitlab.json | 6 ++ .../runtime-rules-hotloading.test.ts.snap | 72 +++++++++++++++++++ test/unit/filters.test.ts | 12 ++++ 4 files changed, 97 insertions(+), 1 deletion(-) diff --git a/client-templates/gitlab/accept.json.sample b/client-templates/gitlab/accept.json.sample index 22e7105ee..a06829858 100644 --- a/client-templates/gitlab/accept.json.sample +++ b/client-templates/gitlab/accept.json.sample @@ -1581,6 +1581,12 @@ "method": "GET", "path": "/api/v4/personal_access_tokens/self", "origin": "https://${GITLAB}" - } + }, + { + "//": "get single pull request info", + "method": "GET", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + "origin": "https://${GITLAB}" + } ] } diff --git a/defaultFilters/gitlab.json b/defaultFilters/gitlab.json index a1c41c04e..112859c2a 100644 --- a/defaultFilters/gitlab.json +++ b/defaultFilters/gitlab.json @@ -1581,6 +1581,12 @@ "method": "GET", "path": "/api/v4/personal_access_tokens/self", "origin": "https://${GITLAB}" + }, + { + "//": "get single pull request info", + "method": "GET", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + "origin": "https://${GITLAB}" } ] } \ No newline at end of file diff --git a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap index bbb51b7d2..732385a6b 100644 --- a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap +++ b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap @@ -7606,6 +7606,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "used to get custom pull request template", "method": "GET", @@ -16187,6 +16193,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "list the group's members", "method": "GET", @@ -24058,6 +24070,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, ], "public": Array [ Object { @@ -32016,6 +32034,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "allow info refs (for git clone)", "method": "GET", @@ -40062,6 +40086,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "used to scan IAC files", "method": "GET", @@ -48050,6 +48080,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "allow info refs (for git clone)", "method": "GET", @@ -62647,6 +62683,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "used to scan IAC files", "method": "GET", @@ -64292,6 +64334,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, Object { "//": "used to scan IAC files", "method": "GET", @@ -72145,6 +72193,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, ], "public": Array [ Object { @@ -79974,6 +80028,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, ], "public": Array [ Object { @@ -87803,6 +87863,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, ], "public": Array [ Object { @@ -95632,6 +95698,12 @@ Object { "origin": "https://\${GITLAB}", "path": "/api/v4/personal_access_tokens/self", }, + Object { + "//": "get single pull request info", + "method": "GET", + "origin": "https://\${GITLAB}", + "path": "/api/v4/projects/:project/merge_requests/:pullRef", + }, ], "public": Array [ Object { diff --git a/test/unit/filters.test.ts b/test/unit/filters.test.ts index 980942152..e038b5dd5 100644 --- a/test/unit/filters.test.ts +++ b/test/unit/filters.test.ts @@ -279,6 +279,18 @@ describe('filters', () => { const filterResponseUrl = filterResponse ? filterResponse.url : ''; expect(filterResponseUrl).toMatch(url); }); + + it('should allow fetching PR info', () => { + const url = '/api/v4/projects/test-project/merge_requests/1'; + + const filterResponse = filter({ + url, + method: 'GET', + }); + expect(filterResponse).not.toEqual(false); + const filterResponseUrl = filterResponse ? filterResponse.url : ''; + expect(filterResponseUrl).toMatch(url); + }); }); }); From 36b6a7680b30400888b773e394340a177801d3d4 Mon Sep 17 00:00:00 2001 From: ciucky Date: Wed, 13 Nov 2024 18:32:53 +0200 Subject: [PATCH 4/5] feat: add filter for bitbucket server /pull-request endpoint --- .../accept.json.sample | 10 + .../bitbucket-server/accept.json.sample | 11 + .../bitbucket-server-bearer-auth.json | 10 + defaultFilters/bitbucket-server.json | 11 + .../runtime-rules-hotloading.test.ts.snap | 252 ++++++++++++++++++ test/unit/filters.test.ts | 31 ++- 6 files changed, 323 insertions(+), 2 deletions(-) diff --git a/client-templates/bitbucket-server-bearer-auth/accept.json.sample b/client-templates/bitbucket-server-bearer-auth/accept.json.sample index 18242cb9a..e50a5e25a 100644 --- a/client-templates/bitbucket-server-bearer-auth/accept.json.sample +++ b/client-templates/bitbucket-server-bearer-auth/accept.json.sample @@ -1062,6 +1062,16 @@ "scheme": "bearer", "token": "${BITBUCKET_PAT}" } + }, + { + "//": "get a single pull request info", + "method": "GET", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + "origin": "https://${BITBUCKET}", + "auth": { + "scheme": "bearer", + "token": "${BITBUCKET_PAT}" + } } ] } diff --git a/client-templates/bitbucket-server/accept.json.sample b/client-templates/bitbucket-server/accept.json.sample index 1d3d27c29..6dccfce48 100644 --- a/client-templates/bitbucket-server/accept.json.sample +++ b/client-templates/bitbucket-server/accept.json.sample @@ -1167,6 +1167,17 @@ "username": "${BITBUCKET_USERNAME}", "password": "${BITBUCKET_PASSWORD}" } + }, + { + "//": "get a single pull request info", + "method": "GET", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + "origin": "https://${BITBUCKET_API}", + "auth": { + "scheme": "basic", + "username": "${BITBUCKET_USERNAME}", + "password": "${BITBUCKET_PASSWORD}" + } } ] } diff --git a/defaultFilters/bitbucket-server-bearer-auth.json b/defaultFilters/bitbucket-server-bearer-auth.json index 18242cb9a..e50a5e25a 100644 --- a/defaultFilters/bitbucket-server-bearer-auth.json +++ b/defaultFilters/bitbucket-server-bearer-auth.json @@ -1062,6 +1062,16 @@ "scheme": "bearer", "token": "${BITBUCKET_PAT}" } + }, + { + "//": "get a single pull request info", + "method": "GET", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + "origin": "https://${BITBUCKET}", + "auth": { + "scheme": "bearer", + "token": "${BITBUCKET_PAT}" + } } ] } diff --git a/defaultFilters/bitbucket-server.json b/defaultFilters/bitbucket-server.json index bf84245cc..8e9e1300c 100644 --- a/defaultFilters/bitbucket-server.json +++ b/defaultFilters/bitbucket-server.json @@ -1167,6 +1167,17 @@ "username": "${BITBUCKET_USERNAME}", "password": "${BITBUCKET_PASSWORD}" } + }, + { + "//": "get a single pull request info", + "method": "GET", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + "origin": "https://${BITBUCKET_API}", + "auth": { + "scheme": "basic", + "username": "${BITBUCKET_USERNAME}", + "password": "${BITBUCKET_PASSWORD}" + } } ] } \ No newline at end of file diff --git a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap index 732385a6b..f92c30a01 100644 --- a/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap +++ b/test/unit/__snapshots__/runtime-rules-hotloading.test.ts.snap @@ -1388,6 +1388,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to get custom pull request template", "auth": Object { @@ -2480,6 +2491,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to get custom pull request template", "auth": Object { @@ -9572,6 +9593,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to get repo's commits", "auth": Object { @@ -10786,6 +10818,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to get repo's commits", "auth": Object { @@ -17835,6 +17877,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -18905,6 +18958,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -25712,6 +25775,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -26815,6 +26889,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -33698,6 +33782,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -34823,6 +34918,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -41758,6 +41863,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -42861,6 +42977,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "allow info refs (for git clone)", "auth": Object { @@ -50185,6 +50311,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -51470,6 +51607,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -52584,6 +52732,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -53754,6 +53912,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, Object { "//": "used to scan IAC files", "auth": Object { @@ -65970,6 +66138,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -67040,6 +67219,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -73805,6 +73994,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -74875,6 +75075,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -81640,6 +81850,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -82710,6 +82931,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -89475,6 +89706,17 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "password": "\${BITBUCKET_PASSWORD}", + "scheme": "basic", + "username": "\${BITBUCKET_USERNAME}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET_API}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { @@ -90545,6 +90787,16 @@ Object { "origin": "https://\${BITBUCKET}", "path": "/rest/api/1.0/projects/:project/repos/:repo/permissions/search", }, + Object { + "//": "get a single pull request info", + "auth": Object { + "scheme": "bearer", + "token": "\${BITBUCKET_PAT}", + }, + "method": "GET", + "origin": "https://\${BITBUCKET}", + "path": "/rest/api/1.0/projects/:project/repos/:repo/pull-requests/:pullRequestId", + }, ], "public": Array [ Object { diff --git a/test/unit/filters.test.ts b/test/unit/filters.test.ts index e038b5dd5..a249134a4 100644 --- a/test/unit/filters.test.ts +++ b/test/unit/filters.test.ts @@ -145,6 +145,19 @@ describe('filters', () => { const filterResponseUrl = filterResponse ? filterResponse.url : ''; expect(filterResponseUrl).toMatch(url); }); + + it('should allow fetching single PR info', () => { + const url = + '/rest/api/1.0/projects/test-org/repos/test-repo/pull-requests/1'; + + const filterResponse = filter({ + url, + method: 'GET', + }); + expect(filterResponse).not.toEqual(false); + const filterResponseUrl = filterResponse ? filterResponse.url : ''; + expect(filterResponseUrl).toMatch(url); + }); }); describe('for bitbucket server bearer auth private filters', () => { @@ -204,6 +217,19 @@ describe('filters', () => { const filterResponseUrl = filterResponse ? filterResponse.url : ''; expect(filterResponseUrl).toMatch(url); }); + + it('should allow fetching pr info', () => { + const url = + '/rest/api/1.0/projects/test-org/repos/test-repo/pull-requests/1'; + + const filterResponse = filter({ + url, + method: 'GET', + }); + expect(filterResponse).not.toEqual(false); + const filterResponseUrl = filterResponse ? filterResponse.url : ''; + expect(filterResponseUrl).toMatch(url); + }); }); describe('for azure repos', () => { @@ -236,8 +262,9 @@ describe('filters', () => { expect(filterResponseUrl).toMatch(url); }); - it('should allow fetching pr info', () => { - const url = '/test-owner/_apis/git/repositories/test-repo/pullRequests/1'; + it('should allow fetching single pr info', () => { + const url = + '/test-owner/_apis/git/repositories/test-repo/pullRequests/1'; const filterResponse = filter({ url, From 0ff32aafff3ee81ae5d665e95ee5133dc06d82ac Mon Sep 17 00:00:00 2001 From: Antoine Arlaud Date: Thu, 14 Nov 2024 13:17:34 +0100 Subject: [PATCH 5/5] feat: enable client version control server side --- lib/client/socket.ts | 3 +- .../socketHandlers/notificationHandler.ts | 15 +++ lib/common/utils/version.ts | 2 +- lib/server/socketHandlers/identifyHandler.ts | 33 +++++-- package-lock.json | 1 + package.json | 3 +- ...lient-version-control-newer-client.test.ts | 91 +++++++++++++++++++ ...lient-version-control-older-client.test.ts | 91 +++++++++++++++++++ 8 files changed, 229 insertions(+), 10 deletions(-) create mode 100644 lib/client/socketHandlers/notificationHandler.ts create mode 100644 test/functional/client-version-control-newer-client.test.ts create mode 100644 test/functional/client-version-control-older-client.test.ts diff --git a/lib/client/socket.ts b/lib/client/socket.ts index 6fce936ff..cf2e63052 100644 --- a/lib/client/socket.ts +++ b/lib/client/socket.ts @@ -21,6 +21,7 @@ import { maskToken } from '../common/utils/token'; import { fetchJwt } from './auth/oauth'; import { getServerId } from './dispatcher'; import { determineFilterType } from './utils/filterSelection'; +import { notificationHandler } from './socketHandlers/notificationHandler'; export const createWebSocketConnectionPairs = async ( websocketConnections: WebSocketConnection[], @@ -225,7 +226,7 @@ export const createWebSocket = ( : localClientOps.config.brokerToken, ), ); - + websocket.on('notification', notificationHandler); websocket.on('error', errorHandler); websocket.on('open', () => diff --git a/lib/client/socketHandlers/notificationHandler.ts b/lib/client/socketHandlers/notificationHandler.ts new file mode 100644 index 000000000..179898d41 --- /dev/null +++ b/lib/client/socketHandlers/notificationHandler.ts @@ -0,0 +1,15 @@ +import { log as logger } from '../../logs/logger'; + +export const notificationHandler = ({ level, message }) => { + switch (level) { + case 'error': + logger.error({ message }); + break; + case 'warning': + logger.warn({ message }); + break; + case 'info': + default: + logger.info({ message }); + } +}; diff --git a/lib/common/utils/version.ts b/lib/common/utils/version.ts index 810743c42..234ec34ac 100644 --- a/lib/common/utils/version.ts +++ b/lib/common/utils/version.ts @@ -1,2 +1,2 @@ import packageJson from '../../../package.json'; -export default packageJson['version'] || 'local'; +export default packageJson['version'] || process.env.BROKER_VERSION || 'local'; diff --git a/lib/server/socketHandlers/identifyHandler.ts b/lib/server/socketHandlers/identifyHandler.ts index 782dc8d0e..8f4f72cef 100644 --- a/lib/server/socketHandlers/identifyHandler.ts +++ b/lib/server/socketHandlers/identifyHandler.ts @@ -6,8 +6,13 @@ import { getSocketConnections } from '../socket'; import { metadataWithoutFilters } from '../utils/socket'; import { getDesensitizedToken } from '../utils/token'; import { getForwardWebSocketRequestHandler } from './initHandlers'; +import semver from 'semver'; let response; +const minimalSupportedBrokerVersion = + process.env.MINIMAL_SUPPORTED_BROKER_VERSION ?? '4.100.0'; +const minimalRecommendedBrokerVersion = + process.env.MINIMAL_RECOMMENDED_BROKER_VERSION ?? '4.182.0'; const streamingResponse = legacyStreamResponseHandler; export const initIdentifyHandler = () => { @@ -38,13 +43,27 @@ export const handleIdentifyOnSocket = (clientData, socket, token): boolean => { const { maskedToken, hashedToken } = getDesensitizedToken(token); const clientId = clientData.metadata.clientId; const clientVersion = clientData.metadata.version; - // TODO: If version < cutoff version, then alert first, then deny - // if(clientVersion < minimalVersion) { - // socket.send('error', { message: `Broker Client Version is outdated. Minimal version: ${minimalVersion}. Please upgrade to latest version` }); - // } - // if(clientVersion < minimalSupportedVersion) { - // socket.send('warning', { message: `Broker Client Version is outdated. Minimal version: ${minimalVersion}. Please upgrade to latest version` }); - // } + + if ( + clientVersion != 'local' && + semver.lt(clientVersion, minimalSupportedBrokerVersion) + ) { + socket.send('notification', { + level: 'error', + message: `Broker Client Version is outdated. Minimal version: ${minimalSupportedBrokerVersion}. Please upgrade to latest version`, + }); + socket.end(); + return false; + } + if ( + clientVersion != 'local' && + semver.lt(clientVersion, minimalRecommendedBrokerVersion) + ) { + socket.send('notification', { + level: 'warning', + message: `Broker Client Version is deprecated. Minimal version: ${minimalRecommendedBrokerVersion}. Please upgrade to latest version`, + }); + } logger.info( { diff --git a/package-lock.json b/package-lock.json index c08fd36db..f3f1475f7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -35,6 +35,7 @@ "prom-client": "^11.5.3", "proxy-from-env": "^1.1.0", "qs": "^6.13.0", + "semver": "^7.6.3", "snyk-config": "^4.0.0", "then-fs": "^2.0.0", "tunnel": "0.0.6", diff --git a/package.json b/package.json index 93a8c3ae5..a4fd304ff 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,7 @@ "dev:client": "tsc-watch --project tsconfig.json --onSuccess 'node dist/cli/index.js client' | ./node_modules/.bin/bunyan", "test": "npm run test:unit && npm run test:functional", "test:unit": "jest unit --detectOpenHandles", - "test:functional": "jest functional --detectOpenHandles", + "test:functional": "jest functional --detectOpenHandles --runInBand", "test:bin": "(cd test/bin; ./container-registry-agent/docker-entrypoint-test.sh)", "test:bin:docker": "docker run --rm -it -v $PWD:/home/broker -w /home/broker/test/bin/ snyk/ubuntu ./container-registry-agent/docker-entrypoint-test.sh", "lint": "npm run lint:check && npm run lint:code", @@ -88,6 +88,7 @@ "prom-client": "^11.5.3", "proxy-from-env": "^1.1.0", "qs": "^6.13.0", + "semver": "^7.6.3", "snyk-config": "^4.0.0", "then-fs": "^2.0.0", "tunnel": "0.0.6", diff --git a/test/functional/client-version-control-newer-client.test.ts b/test/functional/client-version-control-newer-client.test.ts new file mode 100644 index 000000000..56ad56d35 --- /dev/null +++ b/test/functional/client-version-control-newer-client.test.ts @@ -0,0 +1,91 @@ +process.env.BROKER_VERSION = '4.100.1'; +import path from 'path'; +import { + BrokerClient, + closeBrokerClient, + createBrokerClient, + waitForBrokerServerConnection, +} from '../setup/broker-client'; +import { + BrokerServer, + closeBrokerServer, + createBrokerServer, + // waitForBrokerClientConnection, + waitForBrokerClientConnections, +} from '../setup/broker-server'; + +import { TestWebServer, createTestWebServer } from '../setup/test-web-server'; + +import { axiosClient } from '../setup/axios-client'; +import { delay } from '../helpers/utils'; + +const fixtures = path.resolve(__dirname, '..', 'fixtures'); +const serverAccept = path.join(fixtures, 'server', 'filters.json'); +const clientAccept = path.join(fixtures, 'client', 'filters.json'); + +describe('newer broker version control', () => { + let tws: TestWebServer; + let bs: BrokerServer; + let bc: BrokerClient; + let brokerToken: string; + let serverMetadata: unknown; + + beforeAll(async () => { + const PORT = 9999; + process.env.BROKER_SERVER_URL = `http://localhost:${PORT}`; + + tws = await createTestWebServer(); + + bs = await createBrokerServer({ port: PORT, filters: serverAccept }); + + bc = await createBrokerClient({ + brokerServerUrl: `http://localhost:${bs.port}`, + brokerToken: 'broker-token-12345', + filters: clientAccept, + type: 'client', + }); + const connData = await waitForBrokerClientConnections(bs, 2); + const primaryIndex = connData.metadataArray[0]['role'] == 'primary' ? 0 : 1; + brokerToken = connData.brokerTokens[primaryIndex]; + serverMetadata = connData.metadataArray[primaryIndex]; + }); + + afterAll(async () => { + await tws.server.close(); + await closeBrokerClient(bc); + await closeBrokerServer(bs); + delete process.env.BROKER_SERVER_URL; + }); + + it('server accepts connection if version is newer than cutoff', async () => { + serverMetadata = await waitForBrokerServerConnection(bc); + + expect(brokerToken).toEqual('broker-token-12345'); + expect(serverMetadata).toMatchObject({ + capabilities: ['receive-post-streams'], + }); + await delay(100); + // expect(isWebsocketConnOpen(bs.server[0])).toBeFalsy() + const response = await axiosClient.get( + `http://localhost:${bc.port}/healthcheck`, + // { some: { example: 'json' } }, + ); + expect(response.status).toEqual(200); + expect(response.data).toStrictEqual([ + { + ok: true, + websocketConnectionOpen: true, + brokerServerUrl: 'http://localhost:9999/?connection_role=primary', + version: '4.100.1', + transport: expect.any(String), + }, + { + ok: true, + websocketConnectionOpen: true, + brokerServerUrl: 'http://localhost:9999/?connection_role=secondary', + version: '4.100.1', + transport: expect.any(String), + }, + ]); + }); +}); diff --git a/test/functional/client-version-control-older-client.test.ts b/test/functional/client-version-control-older-client.test.ts new file mode 100644 index 000000000..d3511dc60 --- /dev/null +++ b/test/functional/client-version-control-older-client.test.ts @@ -0,0 +1,91 @@ +process.env.BROKER_VERSION = '4.90.0'; +import path from 'path'; +import { + BrokerClient, + closeBrokerClient, + createBrokerClient, + waitForBrokerServerConnection, +} from '../setup/broker-client'; +import { + BrokerServer, + closeBrokerServer, + createBrokerServer, + // waitForBrokerClientConnection, + waitForBrokerClientConnections, +} from '../setup/broker-server'; + +import { TestWebServer, createTestWebServer } from '../setup/test-web-server'; + +import { axiosClient } from '../setup/axios-client'; +import { delay } from '../helpers/utils'; + +const fixtures = path.resolve(__dirname, '..', 'fixtures'); +const serverAccept = path.join(fixtures, 'server', 'filters.json'); +const clientAccept = path.join(fixtures, 'client', 'filters.json'); + +describe('older broker version control', () => { + let tws: TestWebServer; + let bs: BrokerServer; + let bc: BrokerClient; + let brokerToken: string; + let serverMetadata: unknown; + + beforeAll(async () => { + const PORT = 9999; + process.env.BROKER_SERVER_URL = `http://localhost:${PORT}`; + + tws = await createTestWebServer(); + + bs = await createBrokerServer({ port: PORT, filters: serverAccept }); + + bc = await createBrokerClient({ + brokerServerUrl: `http://localhost:${bs.port}`, + brokerToken: 'broker-token-12345', + filters: clientAccept, + type: 'client', + }); + const connData = await waitForBrokerClientConnections(bs, 2); + const primaryIndex = connData.metadataArray[0]['role'] == 'primary' ? 0 : 1; + brokerToken = connData.brokerTokens[primaryIndex]; + serverMetadata = connData.metadataArray[primaryIndex]; + }); + + afterAll(async () => { + await tws.server.close(); + await closeBrokerClient(bc); + await closeBrokerServer(bs); + delete process.env.BROKER_SERVER_URL; + }); + + it('server closes connection if version is older than cutoff', async () => { + serverMetadata = await waitForBrokerServerConnection(bc); + + expect(brokerToken).toEqual('broker-token-12345'); + expect(serverMetadata).toMatchObject({ + capabilities: ['receive-post-streams'], + }); + await delay(100); + // expect(isWebsocketConnOpen(bs.server[0])).toBeFalsy() + const response = await axiosClient.get( + `http://localhost:${bc.port}/healthcheck`, + // { some: { example: 'json' } }, + ); + expect(response.status).toEqual(500); + expect(response.data).toStrictEqual([ + { + ok: false, + websocketConnectionOpen: false, + brokerServerUrl: 'http://localhost:9999/?connection_role=primary', + version: '4.90.0', + transport: expect.any(String), + }, + { + ok: false, + websocketConnectionOpen: false, + brokerServerUrl: 'http://localhost:9999/?connection_role=secondary', + version: '4.90.0', + transport: expect.any(String), + }, + ]); + }); +});