Skip to content

Commit

Permalink
Use serverBasePath provided by the core instead of the one provided…
Browse files Browse the repository at this point in the history
… via legacy shim.
  • Loading branch information
azasypkin committed Oct 8, 2019
1 parent a361167 commit 2b88f7b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 20 deletions.
1 change: 0 additions & 1 deletion x-pack/legacy/plugins/security/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ export const security = (kibana) => new kibana.Plugin({
server.plugins.kibana.systemApi
),
cspRules: createCSPRuleString(config.get('csp.rules')),
serverBasePath: config.get('server.basePath'),
});

const plugin = this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ describe('SAMLAuthenticationProvider', () => {

expect(authenticationResult.redirected()).toBe(true);
expect(authenticationResult.redirectURL).toBe(
'/base-path/api/security/saml/capture-url-fragment'
'/mock-server-basepath/api/security/saml/capture-url-fragment'
);
expect(authenticationResult.state).toEqual({ redirectURL: '/base-path/s/foo/some-path' });
});
Expand Down Expand Up @@ -746,7 +746,7 @@ describe('SAMLAuthenticationProvider', () => {

expect(authenticationResult.redirected()).toBe(true);
expect(authenticationResult.redirectURL).toBe(
'/base-path/api/security/saml/capture-url-fragment'
'/mock-server-basepath/api/security/saml/capture-url-fragment'
);
expect(authenticationResult.state).toEqual({ redirectURL: '/base-path/s/foo/some-path' });
});
Expand Down Expand Up @@ -819,7 +819,7 @@ describe('SAMLAuthenticationProvider', () => {

expect(authenticationResult.redirected()).toBe(true);
expect(authenticationResult.redirectURL).toBe(
'/base-path/api/security/saml/capture-url-fragment'
'/mock-server-basepath/api/security/saml/capture-url-fragment'
);
expect(authenticationResult.state).toEqual({ redirectURL: '/base-path/s/foo/some-path' });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -571,10 +571,9 @@ export class SAMLAuthenticationProvider extends BaseAuthenticationProvider {
return this.authenticateViaHandshake(request, '');
}

// We should use `serverBasePath` here as soon as it's provided by the core. Using request scoped
// base path doesn't hurt, but isn't consistent with the rest of API routes.
return AuthenticationResult.redirectTo(`${basePath}/api/security/saml/capture-url-fragment`, {
state: { redirectURL },
});
return AuthenticationResult.redirectTo(
`${this.options.basePath.serverBasePath}/api/security/saml/capture-url-fragment`,
{ state: { redirectURL } }
);
}
}
1 change: 0 additions & 1 deletion x-pack/plugins/security/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ export interface LegacyAPI {
xpackInfo: Pick<XPackInfo, 'isAvailable' | 'feature'>;
isSystemAPIRequest: (request: KibanaRequest) => boolean;
cspRules: string;
serverBasePath?: string;
}

/**
Expand Down
6 changes: 2 additions & 4 deletions x-pack/plugins/security/server/routes/authentication.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ describe('SAML authentication routes', () => {
logger: loggingServiceMock.create().get(),
config: { authc: { providers: ['saml'] } } as ConfigType,
authc,
getLegacyAPI: () =>
({ cspRules: 'test-csp-rule', serverBasePath: '/test-base-path' } as LegacyAPI),
getLegacyAPI: () => ({ cspRules: 'test-csp-rule' } as LegacyAPI),
});
});

Expand All @@ -44,8 +43,7 @@ describe('SAML authentication routes', () => {
logger: loggingServiceMock.create().get(),
config: { authc: { providers: ['basic'] } } as ConfigType,
authc: authenticationMock.create(),
getLegacyAPI: () =>
({ cspRules: 'test-csp-rule', serverBasePath: '/test-base-path' } as LegacyAPI),
getLegacyAPI: () => ({ cspRules: 'test-csp-rule' } as LegacyAPI),
});

const samlRoutePathPredicate = ([{ path }]: [{ path: string }, any]) =>
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/security/server/routes/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ export function defineAuthenticationRoutes(params: RouteDefinitionParams) {
/**
* Defines routes required for SAML authentication.
*/
function defineSAMLRoutes({ router, logger, authc, getLegacyAPI }: RouteDefinitionParams) {
function defineSAMLRoutes({
router,
logger,
authc,
getLegacyAPI,
basePath,
}: RouteDefinitionParams) {
function createCustomResourceResponse(body: string, contentType: string) {
return {
body,
Expand All @@ -37,15 +43,14 @@ function defineSAMLRoutes({ router, logger, authc, getLegacyAPI }: RouteDefiniti
options: { authRequired: false },
},
(context, request, response) => {
const serverBasePath = getLegacyAPI().serverBasePath || '';
// We're also preventing `favicon.ico` request since it can cause new SAML handshake.
return response.custom(
createCustomResourceResponse(
`
<!DOCTYPE html>
<title>Kibana SAML Login</title>
<link rel="icon" href="data:,">
<script src="${serverBasePath}/api/security/saml/capture-url-fragment.js"></script>
<script src="${basePath.serverBasePath}/api/security/saml/capture-url-fragment.js"></script>
`,
'text/html'
)
Expand All @@ -60,12 +65,11 @@ function defineSAMLRoutes({ router, logger, authc, getLegacyAPI }: RouteDefiniti
options: { authRequired: false },
},
(context, request, response) => {
const serverBasePath = getLegacyAPI().serverBasePath || '';
return response.custom(
createCustomResourceResponse(
`
window.location.replace(
'${serverBasePath}/api/security/saml/start?redirectURLFragment=' + encodeURIComponent(window.location.hash)
'${basePath.serverBasePath}/api/security/saml/start?redirectURLFragment=' + encodeURIComponent(window.location.hash)
);
`,
'text/javascript'
Expand Down Expand Up @@ -120,7 +124,7 @@ function defineSAMLRoutes({ router, logger, authc, getLegacyAPI }: RouteDefiniti
async (context, request, response) => {
try {
if (path === '/api/security/v1/saml') {
const serverBasePath = getLegacyAPI().serverBasePath || '';
const serverBasePath = basePath.serverBasePath;
logger.warn(
`The "${serverBasePath}${path}" URL is deprecated and will stop working in the next major version, please use "${serverBasePath}/api/security/saml/callback" URL instead.`,
{ tags: ['deprecation'] }
Expand Down

0 comments on commit 2b88f7b

Please sign in to comment.