1717 * under the License.
1818 */
1919
20- import { clusterClientMock , clusterClientInstanceMock } from './core_service.test.mocks' ;
20+ import {
21+ MockLegacyScopedClusterClient ,
22+ MockElasticsearchClient ,
23+ legacyClusterClientInstanceMock ,
24+ } from './core_service.test.mocks' ;
2125
2226import Boom from 'boom' ;
2327import { Request } from 'hapi' ;
2428import { errors as esErrors } from 'elasticsearch' ;
2529import { LegacyElasticsearchErrorHelpers } from '../../elasticsearch/legacy' ;
2630
31+ import { elasticsearchClientMock } from '../../elasticsearch/client/mocks' ;
32+ import { ResponseError } from '@elastic/elasticsearch/lib/errors' ;
2733import * as kbnTestServer from '../../../../test_utils/kbn_server' ;
34+ import { InternalElasticsearchServiceStart } from '../../elasticsearch' ;
2835
2936interface User {
3037 id : string ;
@@ -44,6 +51,17 @@ const cookieOptions = {
4451} ;
4552
4653describe ( 'http service' , ( ) => {
54+ let esClient : ReturnType < typeof elasticsearchClientMock . createInternalClient > ;
55+
56+ beforeEach ( async ( ) => {
57+ esClient = elasticsearchClientMock . createInternalClient ( ) ;
58+ MockElasticsearchClient . mockImplementation ( ( ) => esClient ) ;
59+ } , 30000 ) ;
60+
61+ afterEach ( async ( ) => {
62+ MockElasticsearchClient . mockClear ( ) ;
63+ } ) ;
64+
4765 describe ( 'auth' , ( ) => {
4866 let root : ReturnType < typeof kbnTestServer . createRoot > ;
4967 beforeEach ( async ( ) => {
@@ -200,7 +218,7 @@ describe('http service', () => {
200218 } , 30000 ) ;
201219
202220 afterEach ( async ( ) => {
203- clusterClientMock . mockClear ( ) ;
221+ MockLegacyScopedClusterClient . mockClear ( ) ;
204222 await root . shutdown ( ) ;
205223 } ) ;
206224
@@ -363,7 +381,7 @@ describe('http service', () => {
363381 } , 30000 ) ;
364382
365383 afterEach ( async ( ) => {
366- clusterClientMock . mockClear ( ) ;
384+ MockLegacyScopedClusterClient . mockClear ( ) ;
367385 await root . shutdown ( ) ;
368386 } ) ;
369387
@@ -386,7 +404,7 @@ describe('http service', () => {
386404 await kbnTestServer . request . get ( root , '/new-platform/' ) . expect ( 200 ) ;
387405
388406 // client contains authHeaders for BWC with legacy platform.
389- const [ client ] = clusterClientMock . mock . calls ;
407+ const [ client ] = MockLegacyScopedClusterClient . mock . calls ;
390408 const [ , , clientHeaders ] = client ;
391409 expect ( clientHeaders ) . toEqual ( authHeaders ) ;
392410 } ) ;
@@ -410,7 +428,7 @@ describe('http service', () => {
410428 . set ( 'Authorization' , authorizationHeader )
411429 . expect ( 200 ) ;
412430
413- const [ client ] = clusterClientMock . mock . calls ;
431+ const [ client ] = MockLegacyScopedClusterClient . mock . calls ;
414432 const [ , , clientHeaders ] = client ;
415433 expect ( clientHeaders ) . toEqual ( { authorization : authorizationHeader } ) ;
416434 } ) ;
@@ -426,7 +444,7 @@ describe('http service', () => {
426444 } )
427445 ) ;
428446
429- clusterClientInstanceMock . callAsCurrentUser . mockRejectedValue ( authenticationError ) ;
447+ legacyClusterClientInstanceMock . callAsCurrentUser . mockRejectedValue ( authenticationError ) ;
430448
431449 const router = createRouter ( '/new-platform' ) ;
432450 router . get ( { path : '/' , validate : false } , async ( context , req , res ) => {
@@ -441,4 +459,91 @@ describe('http service', () => {
441459 expect ( response . header [ 'www-authenticate' ] ) . toEqual ( 'authenticate header' ) ;
442460 } ) ;
443461 } ) ;
462+
463+ describe ( 'elasticsearch client' , ( ) => {
464+ let root : ReturnType < typeof kbnTestServer . createRoot > ;
465+
466+ beforeEach ( async ( ) => {
467+ root = kbnTestServer . createRoot ( { plugins : { initialize : false } } ) ;
468+ } , 30000 ) ;
469+
470+ afterEach ( async ( ) => {
471+ MockElasticsearchClient . mockClear ( ) ;
472+ await root . shutdown ( ) ;
473+ } ) ;
474+
475+ it ( 'forwards unauthorized errors from elasticsearch' , async ( ) => {
476+ const { http } = await root . setup ( ) ;
477+ const { createRouter } = http ;
478+ // eslint-disable-next-line prefer-const
479+ let elasticsearch : InternalElasticsearchServiceStart ;
480+
481+ esClient . ping . mockImplementation ( ( ) =>
482+ elasticsearchClientMock . createClientError (
483+ new ResponseError ( {
484+ statusCode : 401 ,
485+ body : {
486+ error : {
487+ type : 'Unauthorized' ,
488+ } ,
489+ } ,
490+ warnings : [ ] ,
491+ headers : {
492+ 'WWW-Authenticate' : 'content' ,
493+ } ,
494+ meta : { } as any ,
495+ } )
496+ )
497+ ) ;
498+
499+ const router = createRouter ( '/new-platform' ) ;
500+ router . get ( { path : '/' , validate : false } , async ( context , req , res ) => {
501+ await elasticsearch . client . asScoped ( req ) . asInternalUser . ping ( ) ;
502+ return res . ok ( ) ;
503+ } ) ;
504+
505+ const coreStart = await root . start ( ) ;
506+ elasticsearch = coreStart . elasticsearch ;
507+
508+ const { header } = await kbnTestServer . request . get ( root , '/new-platform/' ) . expect ( 401 ) ;
509+
510+ expect ( header [ 'www-authenticate' ] ) . toEqual ( 'content' ) ;
511+ } ) ;
512+
513+ it ( 'uses a default value for `www-authenticate` header when ES 401 does not specify it' , async ( ) => {
514+ const { http } = await root . setup ( ) ;
515+ const { createRouter } = http ;
516+ // eslint-disable-next-line prefer-const
517+ let elasticsearch : InternalElasticsearchServiceStart ;
518+
519+ esClient . ping . mockImplementation ( ( ) =>
520+ elasticsearchClientMock . createClientError (
521+ new ResponseError ( {
522+ statusCode : 401 ,
523+ body : {
524+ error : {
525+ type : 'Unauthorized' ,
526+ } ,
527+ } ,
528+ warnings : [ ] ,
529+ headers : { } ,
530+ meta : { } as any ,
531+ } )
532+ )
533+ ) ;
534+
535+ const router = createRouter ( '/new-platform' ) ;
536+ router . get ( { path : '/' , validate : false } , async ( context , req , res ) => {
537+ await elasticsearch . client . asScoped ( req ) . asInternalUser . ping ( ) ;
538+ return res . ok ( ) ;
539+ } ) ;
540+
541+ const coreStart = await root . start ( ) ;
542+ elasticsearch = coreStart . elasticsearch ;
543+
544+ const { header } = await kbnTestServer . request . get ( root , '/new-platform/' ) . expect ( 401 ) ;
545+
546+ expect ( header [ 'www-authenticate' ] ) . toEqual ( 'Basic realm="Authorization Required"' ) ;
547+ } ) ;
548+ } ) ;
444549} ) ;
0 commit comments