-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[HTTP/OAS] Ability to exclude routes from introspection #192675
Merged
jloleysens
merged 8 commits into
elastic:main
from
jloleysens:oas/add-option-to-exclude-route-from-oas-gen
Oct 9, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
df35cfe
refactor oas route to use config schema
jloleysens 8097bd5
added capability to exclude routes from introspection
jloleysens e70e06a
added field to types and fixed minor typo
jloleysens 4fa18e3
Merge branch 'main' into oas/add-option-to-exclude-route-from-oas-gen
jloleysens 50430ec
hiddenFromIntrospection -> excludeFromOAS
jloleysens b964e11
use the excludeFromOAS option in the oas lib code instead
jloleysens de17d30
cleanup tests
jloleysens 1bfe9e1
Merge branch 'main' into oas/add-option-to-exclude-route-from-oas-gen
jloleysens File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,13 @@ | |
|
||
import { Observable, Subscription, combineLatest, firstValueFrom, of, mergeMap } from 'rxjs'; | ||
import { map } from 'rxjs'; | ||
import { schema, TypeOf } from '@kbn/config-schema'; | ||
|
||
import { pick, Semaphore } from '@kbn/std'; | ||
import { generateOpenApiDocument } from '@kbn/router-to-openapispec'; | ||
import { | ||
generateOpenApiDocument, | ||
type GenerateOpenApiDocumentOptionsFilters, | ||
} from '@kbn/router-to-openapispec'; | ||
import { Logger } from '@kbn/logging'; | ||
import { Env } from '@kbn/config'; | ||
import type { CoreContext, CoreService } from '@kbn/core-base-server-internal'; | ||
|
@@ -254,49 +258,55 @@ export class HttpService | |
const baseUrl = | ||
basePath.publicBaseUrl ?? `http://localhost:${config.port}${basePath.serverBasePath}`; | ||
|
||
const stringOrStringArraySchema = schema.oneOf([ | ||
schema.string(), | ||
schema.arrayOf(schema.string()), | ||
]); | ||
const querySchema = schema.object({ | ||
access: schema.maybe(schema.oneOf([schema.literal('public'), schema.literal('internal')])), | ||
excludePathsMatching: schema.maybe(stringOrStringArraySchema), | ||
pathStartsWith: schema.maybe(stringOrStringArraySchema), | ||
pluginId: schema.maybe(schema.string()), | ||
version: schema.maybe(schema.string()), | ||
}); | ||
|
||
server.route({ | ||
path: '/api/oas', | ||
method: 'GET', | ||
handler: async (req, h) => { | ||
const version = req.query?.version; | ||
|
||
let pathStartsWith: undefined | string[]; | ||
if (typeof req.query?.pathStartsWith === 'string') { | ||
pathStartsWith = [req.query.pathStartsWith]; | ||
} else { | ||
pathStartsWith = req.query?.pathStartsWith; | ||
} | ||
|
||
let excludePathsMatching: undefined | string[]; | ||
if (typeof req.query?.excludePathsMatching === 'string') { | ||
excludePathsMatching = [req.query.excludePathsMatching]; | ||
} else { | ||
excludePathsMatching = req.query?.excludePathsMatching; | ||
let filters: GenerateOpenApiDocumentOptionsFilters; | ||
let query: TypeOf<typeof querySchema>; | ||
try { | ||
query = querySchema.validate(req.query); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor to using |
||
filters = { | ||
...query, | ||
excludePathsMatching: | ||
typeof query.excludePathsMatching === 'string' | ||
? [query.excludePathsMatching] | ||
: query.excludePathsMatching, | ||
pathStartsWith: | ||
typeof query.pathStartsWith === 'string' | ||
? [query.pathStartsWith] | ||
: query.pathStartsWith, | ||
}; | ||
} catch (e) { | ||
return h.response({ message: e.message }).code(400); | ||
} | ||
|
||
const pluginId = req.query?.pluginId; | ||
|
||
const access = req.query?.access as 'public' | 'internal' | undefined; | ||
if (access && !['public', 'internal'].some((a) => a === access)) { | ||
return h | ||
.response({ | ||
message: 'Invalid access query parameter. Must be one of "public" or "internal".', | ||
}) | ||
.code(400); | ||
} | ||
|
||
return await firstValueFrom( | ||
of(1).pipe( | ||
HttpService.generateOasSemaphore.acquire(), | ||
mergeMap(async () => { | ||
try { | ||
// Potentially quite expensive | ||
const result = generateOpenApiDocument(this.httpServer.getRouters({ pluginId }), { | ||
baseUrl, | ||
title: 'Kibana HTTP APIs', | ||
version: '0.0.0', // TODO get a better version here | ||
filters: { pathStartsWith, excludePathsMatching, access, version }, | ||
}); | ||
const result = generateOpenApiDocument( | ||
this.httpServer.getRouters({ pluginId: query.pluginId }), | ||
{ | ||
baseUrl, | ||
title: 'Kibana HTTP APIs', | ||
version: '0.0.0', // TODO get a better version here | ||
filters, | ||
} | ||
); | ||
return h.response(result); | ||
} catch (e) { | ||
this.log.error(e); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,13 +105,14 @@ export const getVersionedHeaderParam = ( | |
}); | ||
|
||
export const prepareRoutes = < | ||
R extends { path: string; options: { access?: 'public' | 'internal' } } | ||
R extends { path: string; options: { access?: 'public' | 'internal'; excludeFromOAS?: boolean } } | ||
>( | ||
routes: R[], | ||
filters: GenerateOpenApiDocumentOptionsFilters = {} | ||
): R[] => { | ||
if (Object.getOwnPropertyNames(filters).length === 0) return routes; | ||
return routes.filter((route) => { | ||
if (route.options.excludeFromOAS) return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where we actually exclude routes from OAS docs. |
||
if ( | ||
filters.excludePathsMatching && | ||
filters.excludePathsMatching.some((ex) => route.path.startsWith(ex)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably unnecessary but this might remove a potential mutation guard we accedentaly had
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah man, I somehow missed this. That's a good point, will fix.