Skip to content

Commit

Permalink
Code is conditional based on the version
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Feb 4, 2020
1 parent be61507 commit 00724de
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 5 deletions.
4 changes: 4 additions & 0 deletions x-pack/plugins/security/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { SecurityLicenseService, SecurityLicense } from '../common/licensing';
import { setupSavedObjects } from './saved_objects';
import { SecurityAuditLogger } from './audit';
import { elasticsearchClientPlugin } from './elasticsearch_client_plugin';
import { VersionService } from './version';

export type SpacesService = Pick<
SpacesPluginSetup['spacesService'],
Expand Down Expand Up @@ -88,6 +89,7 @@ export class Plugin {
private clusterClient?: ICustomClusterClient;
private spacesService?: SpacesService | symbol = Symbol('not accessed');
private securityLicenseService?: SecurityLicenseService;
private readonly version: VersionService;

private legacyAPI?: LegacyAPI;
private readonly getLegacyAPI = () => {
Expand All @@ -107,6 +109,7 @@ export class Plugin {
};

constructor(private readonly initializerContext: PluginInitializerContext) {
this.version = new VersionService(initializerContext.env.packageInfo.version);
this.logger = this.initializerContext.logger.get();
}

Expand Down Expand Up @@ -164,6 +167,7 @@ export class Plugin {
authc,
authz,
csp: core.http.csp,
version: this.version,
});

return deepFreeze<SecurityPluginSetup>({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { mockAuthenticatedUser } from '../../../common/model/authenticated_user.mock';
import { authenticationMock } from '../../authentication/index.mock';
import { authorizationMock } from '../../authorization/index.mock';
import { Version, VersionService } from '../../version';

describe('Common authentication routes', () => {
let router: jest.Mocked<IRouter>;
Expand All @@ -50,6 +51,7 @@ describe('Common authentication routes', () => {
authc,
authz: authorizationMock.create(),
csp: httpServiceMock.createSetupContract().csp,
version: new VersionService(Version.V_7_7_0),
});
});

Expand Down
23 changes: 18 additions & 5 deletions x-pack/plugins/security/server/routes/authentication/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,24 @@ import { schema } from '@kbn/config-schema';
import { canRedirectRequest } from '../../authentication';
import { wrapIntoCustomErrorResponse } from '../../errors';
import { createLicensedRouteHandler } from '../licensed_route_handler';
import { Version } from '../../version';
import { RouteDefinitionParams } from '..';

/**
* Defines routes that are common to various authentication mechanisms.
*/
export function defineCommonRoutes({ router, authc, basePath, logger }: RouteDefinitionParams) {
export function defineCommonRoutes({
router,
authc,
basePath,
logger,
version,
}: RouteDefinitionParams) {
// Generate two identical routes with new and deprecated URL and issue a warning if route with deprecated URL is ever used.
for (const path of ['/api/security/logout', '/api/security/v1/logout']) {
for (const path of [
'/api/security/logout',
...(version.before(Version.V_8_0_0) ? ['/api/security/v1/logout'] : []),
]) {
router.get(
{
path,
Expand All @@ -26,7 +36,7 @@ export function defineCommonRoutes({ router, authc, basePath, logger }: RouteDef
},
async (context, request, response) => {
const serverBasePath = basePath.serverBasePath;
if (path === '/api/security/v1/logout') {
if (version.before(Version.V_8_0_0) && path === '/api/security/v1/logout') {
logger.warn(
`The "${serverBasePath}${path}" URL is deprecated and will stop working in the next major version, please use "${serverBasePath}/api/security/logout" URL instead.`,
{ tags: ['deprecation'] }
Expand Down Expand Up @@ -56,11 +66,14 @@ export function defineCommonRoutes({ router, authc, basePath, logger }: RouteDef
}

// Generate two identical routes with new and deprecated URL and issue a warning if route with deprecated URL is ever used.
for (const path of ['/internal/security/me', '/api/security/v1/me']) {
for (const path of [
'/internal/security/me',
...(version.before(Version.V_8_0_0) ? ['/api/security/v1/me'] : []),
]) {
router.get(
{ path, validate: false },
createLicensedRouteHandler(async (context, request, response) => {
if (path === '/api/security/v1/me') {
if (version.before(Version.V_8_0_0) && path === '/api/security/v1/me') {
logger.warn(
`The "${basePath.serverBasePath}${path}" endpoint is deprecated and will be removed in the next major version.`,
{ tags: ['deprecation'] }
Expand Down
2 changes: 2 additions & 0 deletions x-pack/plugins/security/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { defineApiKeysRoutes } from './api_keys';
import { defineIndicesRoutes } from './indices';
import { defineUsersRoutes } from './users';
import { defineRoleMappingRoutes } from './role_mapping';
import { VersionService } from '../version';

/**
* Describes parameters used to define HTTP routes.
Expand All @@ -28,6 +29,7 @@ export interface RouteDefinitionParams {
config: ConfigType;
authc: Authentication;
authz: Authorization;
version: VersionService;
}

export function defineRoutes(params: RouteDefinitionParams) {
Expand Down
19 changes: 19 additions & 0 deletions x-pack/plugins/security/server/version.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { VersionService, Version } from './version';

describe('#before', function() {
test('0.0.1 is before 1.0.0', () => {
const version = new VersionService('0.0.1');
expect(version.before('1.0.0')).toBe(true);
});

test('7.7.0 is before "Version.V_8_0_0"', () => {
const version = new VersionService('7.7.0');
expect(version.before(Version.V_8_0_0)).toBe(true);
});
});
19 changes: 19 additions & 0 deletions x-pack/plugins/security/server/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import semver from 'semver';

export enum Version {
V_7_7_0 = '7.7.0',
V_8_0_0 = '8.0.0',
}

export class VersionService {
constructor(private readonly currentVersion: string) {}

before(version: string) {
return semver.lt(this.currentVersion, version);
}
}

0 comments on commit 00724de

Please sign in to comment.