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

[Reporting] Separate internal and public API endpoints in Reporting #162288

Merged
merged 58 commits into from
Aug 1, 2023

Conversation

tsullivan
Copy link
Member

@tsullivan tsullivan commented Jul 19, 2023

Summary

Closes #160827
Closes #134517
Closes #149942

In this PR, the following API endpoints are moved into an internal namespace:

New endpoint path Previous
/internal/reporting/diagnose/browser /api/reporting/diagnose/browser
/internal/reporting/diagnose/screenshot /api/reporting/diagnose/screenshot
/internal/reporting/generate/immediate/csv_searchsource /api/reporting/v1/generate/immediate/csv_searchsource
/internal/reporting/generate/{exportTypeId} /api/reporting/generate/{exportTypeId}
/internal/reporting/jobs/count /api/reporting/jobs/count
/internal/reporting/jobs/delete/{jobId} /api/reporting/jobs/delete/{jobId}
/internal/reporting/jobs/info/{jobId} /api/reporting/jobs/info/{jobId}
/internal/reporting/jobs/list /api/reporting/jobs/list

Support for the public APIs continues:

Public endpoint path
/api/reporting/generate/{exportTypeId}
/api/reporting/jobs/delete/{jobId}
/api/reporting/jobs/download/{jobId}

Other changes

  1. Set access options on the routes
  2. Removed API Counter functional tests, which were skipped to begin with.
  3. Replaced functional tests with Jest integration tests.
  4. Consolidated code in the generation routes by creating the getJobParams method of the RequestHandler class.
  5. Added a new test for getJobParams
  6. Consolidated code in the job management routes
  7. Added new code for shared helpers in job management routes
  8. Reorganized libs used for route handlers:
    routes/lib/request_handler.ts             =>  routes/common/generate/request_handler.ts                              
    routes/lib/job_management_pre_routing.ts  =>  routes/common/jobs/job_management_pre_routing.ts
    routes/lib/jobs_query.ts                  =>  routes/common/jobs/jobs_query.ts
    routes/lib/get_document_payload.ts        =>  routes/common/jobs/get_document_payload.ts
    routes/lib/get_counter.ts                 =>  routes/common/get_counter.ts
    routes/lib/authorized_user_pre_routing.ts =>  routes/common/authorized_user_pre_routing.ts
    routes/lib/get_user.ts                    =>  routes/common/get_user.ts
    

Release Note

Updated API endpoint paths for Reporting to clarify which routes are public and which are not. Make sure that any custom script or application that uses Reporting endpoints only uses the public endpoints.

@@ -110,9 +108,7 @@ export class ReportingAPIClient implements IReportingAPI {
}

public async deleteReport(jobId: string) {
return await this.http.delete<void>(`${API_LIST_URL}/delete/${jobId}`, {
asSystemRequest: true,
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

deleting a report is manually done by the user: not a system request

@@ -151,47 +147,49 @@ export class ReportingAPIClient implements IReportingAPI {
}

public async getInfo(jobId: string) {
const report: ReportApiJSON = await this.http.get(`${API_LIST_URL}/info/${jobId}`, {
asSystemRequest: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getting report info is manually done by the user: not a system request


public getServerBasePath = () => this.http.basePath.serverBasePath;

public verifyBrowser() {
return this.http.post<DiagnoseResponse>(`${API_BASE_URL}/diagnose/browser`, {
asSystemRequest: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using the diagnostic tool is manually done by the user: not a system request

props.reportType,
this.props.apiClient.getDecoratedJobParams(this.props.getJobParams(true))
);
return url.resolve(window.location.href, relativePath); // FIXME: '(from: string, to: string): string' is deprecated
Copy link
Member Author

@tsullivan tsullivan Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't figure out why my editor says this line uses a deprecated signature, so the comment isn't valid

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I'm also not seeing any issues in my editor 👍🏻

@tsullivan tsullivan force-pushed the reporting/use-internal-endpoints branch 2 times, most recently from 4e7152f to c9941b7 Compare July 20, 2023 02:11
@tsullivan tsullivan self-assigned this Jul 20, 2023
@tsullivan tsullivan added (Deprecated) Feature:Reporting Use Reporting:Screenshot, Reporting:CSV, or Reporting:Framework instead Team:SharedUX Team label for AppEx-SharedUX (formerly Global Experience) labels Jul 20, 2023
@tsullivan tsullivan force-pushed the reporting/use-internal-endpoints branch from fffb568 to 61c611e Compare July 20, 2023 22:55
});
});

describe('verifyScreenCapture', () => {
it('should send a post request', async () => {
await apiClient.verifyScreenCapture();

expect(httpClient.post).toHaveBeenCalledWith(
expect.stringContaining('/diagnose/screenshot'),
expect.any(Object)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the options object was removed from the API calls for many of these, since they mistakenly were used to send the asSystemRequest field

@tsullivan tsullivan force-pushed the reporting/use-internal-endpoints branch from 4305bc8 to 5122ed6 Compare July 27, 2023 23:43
counters: Counters
) {
public getJobParams(): BaseParams {
let jobParamsRison: null | string = null;
Copy link
Member Author

@tsullivan tsullivan Jul 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added this helper because without it, this section of code would have had to be duplicated in x-pack/plugins/reporting/server/routes/internal/generate/generate_from_jobparams.ts and x-pack/plugins/reporting/server/routes/public/generate_from_jobparams.ts

res: KibanaResponseFactory;
}

export const commonJobsRouteHandlerFactory = (reporting: ReportingCore) => {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added these helpers to avoid duplicated code in x-pack/plugins/reporting/server/routes/internal/management/jobs.ts and x-pack/plugins/reporting/server/routes/public/jobs.ts

Comment on lines 19 to 22
// TODO: find a way to abstract this using ExportTypeRegistry: it needs a new
// public method to return this array
// const registry = reporting.getExportTypesRegistry();
// const kibanaAccessControlTags = registry.getAllAccessControlTags();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we still care about this, I just copied this comment from x-pack/plugins/reporting/server/routes/generate/generate_from_jobparams.ts

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also tempted to remove this comment - I don't think it's really relevant anymore

@@ -7,29 +7,26 @@

import { schema } from '@kbn/config-schema';
import { ROUTE_TAG_CAN_REDIRECT } from '@kbn/security-plugin/server';
import { promisify } from 'util';
Copy link
Member Author

@tsullivan tsullivan Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A file was deleted and made into 2 versions, but Github shows this as a rename, so this diff makes a good outline of the goals of this PR:

  1. Use constants for the route path strings
  2. Use helpers within route handler functions to avoid duplicated code

Copy link
Contributor

@rshen91 rshen91 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love the constants/routes file making everything so much clearer

props.reportType,
this.props.apiClient.getDecoratedJobParams(this.props.getJobParams(true))
);
return url.resolve(window.location.href, relativePath); // FIXME: '(from: string, to: string): string' is deprecated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I'm also not seeing any issues in my editor 👍🏻


const API_BASE_URL_V1 = '/api/reporting/v1';
const API_BASE_GENERATE_V1 = `${API_BASE_URL_V1}/generate`;
const path = INTERNAL_ROUTES.DOWNLOAD_CSV;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love that there's the constants/routes file in this PR to reference these vs it being spread across the plugin

Comment on lines 19 to 22
// TODO: find a way to abstract this using ExportTypeRegistry: it needs a new
// public method to return this array
// const registry = reporting.getExportTypesRegistry();
// const kibanaAccessControlTags = registry.getAllAccessControlTags();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also tempted to remove this comment - I don't think it's really relevant anymore

@tsullivan tsullivan marked this pull request as ready for review July 28, 2023 16:31
@tsullivan tsullivan requested a review from a team as a code owner July 28, 2023 16:31
@elasticmachine
Copy link
Contributor

Pinging @elastic/appex-sharedux (Team:SharedUX)

@tsullivan tsullivan added the release_note:skip Skip the PR/issue when compiling release notes label Jul 28, 2023
@tsullivan tsullivan requested a review from a team July 28, 2023 17:35
@tsullivan tsullivan added release_note:enhancement and removed release_note:skip Skip the PR/issue when compiling release notes labels Jul 28, 2023
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
reporting 90 91 +1

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
reporting 59.1KB 59.1KB +6.0B

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
reporting 42.8KB 43.1KB +308.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @tsullivan

@tsullivan tsullivan merged commit a2e4279 into elastic:main Aug 1, 2023
@kibanamachine kibanamachine added v8.10.0 backport:skip This commit does not require backporting labels Aug 1, 2023
@tsullivan tsullivan deleted the reporting/use-internal-endpoints branch August 1, 2023 15:24
ThomThomson pushed a commit to ThomThomson/kibana that referenced this pull request Aug 1, 2023
…lastic#162288)

## Summary

Closes elastic#160827
Closes elastic#134517
Closes elastic#149942

In this PR, the following API endpoints are moved into an internal
namespace:

| New endpoint path | Previous |
|---|---|
| `/internal/reporting/diagnose/browser` |
`/api/reporting/diagnose/browser` |
| `/internal/reporting/diagnose/screenshot` |
`/api/reporting/diagnose/screenshot` |
| `/internal/reporting/generate/immediate/csv_searchsource` |
`/api/reporting/v1/generate/immediate/csv_searchsource` |
| `/internal/reporting/generate/{exportTypeId}` |
`/api/reporting/generate/{exportTypeId}` |
| `/internal/reporting/jobs/count` | `/api/reporting/jobs/count` |
| `/internal/reporting/jobs/delete/{jobId}` |
`/api/reporting/jobs/delete/{jobId}` |
| `/internal/reporting/jobs/info/{jobId}` |
`/api/reporting/jobs/info/{jobId}` |
| `/internal/reporting/jobs/list` | `/api/reporting/jobs/list` |

Support for the public APIs continues:

| Public endpoint path |
|---|
| `/api/reporting/generate/{exportTypeId}` |
| `/api/reporting/jobs/delete/{jobId}` |
| `/api/reporting/jobs/download/{jobId}` |

## Other changes
1. Set access options on the routes
2. Removed API Counter functional tests, which were skipped to begin
with.
3. Replaced functional tests with Jest integration tests.
4. Consolidated code in the generation routes by creating the
`getJobParams` method of the `RequestHandler` class.
5. Added a new test for `getJobParams` 
6. Consolidated code in the job management routes 
7. Added new code for shared helpers in job management routes 
8. Reorganized libs used for route handlers:
    ```
routes/lib/request_handler.ts =>
routes/common/generate/request_handler.ts
routes/lib/job_management_pre_routing.ts =>
routes/common/jobs/job_management_pre_routing.ts
routes/lib/jobs_query.ts => routes/common/jobs/jobs_query.ts
routes/lib/get_document_payload.ts =>
routes/common/jobs/get_document_payload.ts
routes/lib/get_counter.ts => routes/common/get_counter.ts
routes/lib/authorized_user_pre_routing.ts =>
routes/common/authorized_user_pre_routing.ts
routes/lib/get_user.ts => routes/common/get_user.ts
   ```

## Release Note
Updated API endpoint paths for Reporting to clarify which routes are
public and which are not. Make sure that any custom script or
application that uses Reporting endpoints only uses the public
endpoints.

---------

Co-authored-by: kibanamachine <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment