Enable automated security plugins at the service (root spec) level (#5378)

* feat(plugins): enable support for service-level security definition plugins

* feat(plugins): add tests for service-level security plugins

* fix(syntax): match regexes to last commit

* fix lint

Co-authored-by: Filipe Freire <livrofubia@gmail.com>
This commit is contained in:
Jack Tysoe 2022-12-14 15:30:56 +00:00 committed by GitHub
parent 1bb9607c39
commit 46bb161134
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 137 additions and 1 deletions

View File

@ -1,4 +1,5 @@
import { describe, expect, it } from '@jest/globals';
import { OpenAPIV3 } from 'openapi-types';
import { OA3Operation } from '../types';
import { DCRoute, DCService } from '../types/declarative-config';
@ -336,6 +337,138 @@ describe('services', () => {
expect(await generateServices(spec, tags)).toEqual([specResult]);
});
it('generates service with securityDefinition-based openid-connect plugin', async () => {
const spec = getSpec();
const securityScheme = {
type: 'openIdConnect',
openIdConnectUrl: 'https://idp-endpoint.example.com/.well-kown',
'x-kong-security-openid-connect': {
config: {
'auth_methods': ['bearer'],
},
enabled: true,
protocols: ['http', 'https'],
},
} as OpenAPIV3.OpenIdSecurityScheme;
if (!spec.components) {
spec.components = {};
}
spec.components.securitySchemes = {
'common-aad-scheme': securityScheme,
};
spec.security = [
{
'common-aad-scheme': ['Api.Security.All'],
},
];
spec.paths = {
'/dogs': {
summary: 'Dog stuff',
get: {},
post: {
security: [
{
'common-aad-scheme': ['Api.Security.Write'],
},
],
},
},
};
const specResult = getSpecResult();
specResult.plugins = [
{
name: 'openid-connect',
config: {
'issuer': 'https://idp-endpoint.example.com/.well-kown',
'auth_methods': ['bearer'],
'scopes_required': ['Api.Security.All'],
},
tags: tags,
},
];
specResult.routes = [
{
name: 'My_API-dogs-get',
strip_path: false,
methods: ['GET'],
paths: ['/dogs$'],
tags,
},
{
name: 'My_API-dogs-post',
strip_path: false,
methods: ['POST'],
paths: ['/dogs$'],
tags,
plugins: [
{
name: 'openid-connect',
config: {
'issuer': 'https://idp-endpoint.example.com/.well-kown',
'auth_methods': ['bearer'],
'scopes_required': ['Api.Security.Write'],
},
tags: tags,
},
],
},
];
expect(await generateServices(spec, tags)).toEqual([specResult]);
});
it('generates service and route (override) with securityDefinition-based openid-connect plugin', async () => {
const spec = getSpec();
const securityScheme = {
type: 'openIdConnect',
openIdConnectUrl: 'https://idp-endpoint.example.com/.well-kown',
'x-kong-security-openid-connect': {
config: {
'auth_methods': ['bearer'],
},
enabled: true,
protocols: ['http', 'https'],
},
} as OpenAPIV3.OpenIdSecurityScheme;
if (!spec.components) {
spec.components = {};
}
spec.components.securitySchemes = {
'common-aad-scheme': securityScheme,
};
spec.security = [
{
'common-aad-scheme': ['Api.Security.All'],
},
];
const specResult = getSpecResult();
specResult.plugins = [
{
name: 'openid-connect',
config: {
'issuer': 'https://idp-endpoint.example.com/.well-kown',
'auth_methods': ['bearer'],
'scopes_required': ['Api.Security.All'],
},
tags: tags,
},
];
expect(await generateServices(spec, tags)).toEqual([specResult]);
});
it('replaces variables', async () => {
const spec = getSpec();
spec.servers = [

View File

@ -49,6 +49,9 @@ export async function generateService(server: OA3Server, api: OpenApi3Spec, tags
throw new Error(`expected '${xKongServiceDefaults}' to be an object`);
}
// Generate generic and security-related service-level plugin objects
const serviceSecurityPlugins = generateSecurityPlugins(null, api, tags);
const service: DCService = {
...serviceDefaults,
name,
@ -58,7 +61,7 @@ export async function generateService(server: OA3Server, api: OpenApi3Spec, tags
// not a hostname, but the Upstream name
port: Number(parsedUrl.port || '80'),
path: parsedUrl.pathname,
plugins: globalPlugins.plugins,
plugins: [...globalPlugins.plugins, ...serviceSecurityPlugins],
routes: [],
tags,
};