Skip to content

Commit

Permalink
rebase fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thomheymann committed Aug 20, 2020
1 parent d40321d commit 20eefbc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 172 deletions.
9 changes: 3 additions & 6 deletions src/core/server/audit_trail/audit_trail_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,10 @@ describe('AuditTrailService', () => {
const { asScoped } = auditTrail.start();
const kibanaRequest = httpServerMock.createKibanaRequest();
const auditor = asScoped(kibanaRequest);
const message = {
type: 'foo',
message: 'bar',
};
auditor.add(() => message);
const eventDecorator = jest.fn();
auditor.add(eventDecorator);

expect(addEventMock).toHaveBeenLastCalledWith(message);
expect(addEventMock).toHaveBeenLastCalledWith(eventDecorator);
});

describe('return the same auditor instance for the same KibanaRequest', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ describe('#setup', () => {
expect(MockLegacyClusterClient).toHaveBeenCalledWith(
expect.objectContaining(customConfig),
expect.objectContaining({ context: ['elasticsearch', 'some-custom-type'] }),
expect.any(Function),
expect.any(Function)
);
});
Expand Down
143 changes: 24 additions & 119 deletions src/core/server/elasticsearch/legacy/cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {

import { errors } from 'elasticsearch';
import { get } from 'lodash';
import { auditTrailServiceMock } from '../../audit_trail/audit_trail_service.mock';
import { Logger } from '../../logging';
import { loggingSystemMock } from '../../logging/logging_system.mock';
import { httpServerMock } from '../../http/http_server.mocks';
Expand All @@ -43,11 +42,7 @@ test('#constructor creates client with parsed config', () => {
const mockEsConfig = { apiVersion: 'es-version' } as any;
const mockLogger = logger.get();

const clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
const clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);
expect(clusterClient).toBeDefined();

expect(mockParseElasticsearchClientConfig).toHaveBeenCalledTimes(1);
Expand All @@ -73,11 +68,7 @@ describe('#callAsInternalUser', () => {
};
MockClient.mockImplementation(() => mockEsClientInstance);

clusterClient = new LegacyClusterClient(
{ apiVersion: 'es-version' } as any,
logger.get(),
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient({ apiVersion: 'es-version' } as any, logger.get());
});

test('fails if cluster client is closed', async () => {
Expand Down Expand Up @@ -246,11 +237,7 @@ describe('#asScoped', () => {
requestHeadersWhitelist: ['one', 'two'],
} as any;

clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);
jest.clearAllMocks();
});

Expand Down Expand Up @@ -285,11 +272,7 @@ describe('#asScoped', () => {

test('properly configures `ignoreCertAndKey` for various configurations', () => {
// Config without SSL.
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);

mockParseElasticsearchClientConfig.mockClear();
clusterClient.asScoped(httpServerMock.createRawRequest({ headers: { one: '1' } }));
Expand All @@ -302,11 +285,7 @@ describe('#asScoped', () => {

// Config ssl.alwaysPresentCertificate === false
mockEsConfig = { ...mockEsConfig, ssl: { alwaysPresentCertificate: false } } as any;
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);

mockParseElasticsearchClientConfig.mockClear();
clusterClient.asScoped(httpServerMock.createRawRequest({ headers: { one: '1' } }));
Expand All @@ -319,11 +298,7 @@ describe('#asScoped', () => {

// Config ssl.alwaysPresentCertificate === true
mockEsConfig = { ...mockEsConfig, ssl: { alwaysPresentCertificate: true } } as any;
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);

mockParseElasticsearchClientConfig.mockClear();
clusterClient.asScoped(httpServerMock.createRawRequest({ headers: { one: '1' } }));
Expand All @@ -344,8 +319,7 @@ describe('#asScoped', () => {
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ one: '1', two: '2' },
expect.any(Object)
{ one: '1', two: '2' }
);
});

Expand All @@ -358,8 +332,7 @@ describe('#asScoped', () => {
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ 'x-opaque-id': 'alpha' },
expect.any(Object)
{ 'x-opaque-id': 'alpha' }
);
});

Expand All @@ -381,142 +354,75 @@ describe('#asScoped', () => {
});

test('does not fail when scope to not defined request', async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);
clusterClient.asScoped();
expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{},
undefined
{}
);
});

test('does not fail when scope to a request without headers', async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);
clusterClient.asScoped({} as any);
expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{},
undefined
{}
);
});

test('calls getAuthHeaders and filters results for a real request', async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory,
() => ({
one: '1',
three: '3',
})
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, () => ({
one: '1',
three: '3',
}));
clusterClient.asScoped(httpServerMock.createRawRequest({ headers: { two: '2' } }));
expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ one: '1', two: '2' },
expect.any(Object)
{ one: '1', two: '2' }
);
});

test('getAuthHeaders results rewrite extends a request headers', async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory,
() => ({ one: 'foo' })
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, () => ({ one: 'foo' }));
clusterClient.asScoped(httpServerMock.createRawRequest({ headers: { one: '1', two: '2' } }));
expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ one: 'foo', two: '2' },
expect.any(Object)
{ one: 'foo', two: '2' }
);
});

test("doesn't call getAuthHeaders for a fake request", async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory,
() => ({})
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, () => ({}));
clusterClient.asScoped({ headers: { one: 'foo' } });

expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ one: 'foo' },
undefined
{ one: 'foo' }
);
});

test('filters a fake request headers', async () => {
clusterClient = new LegacyClusterClient(
mockEsConfig,
mockLogger,
auditTrailServiceMock.createAuditorFactory
);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger);
clusterClient.asScoped({ headers: { one: '1', two: '2', three: '3' } });

expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
{ one: '1', two: '2' },
undefined
{ one: '1', two: '2' }
);
});

describe('Auditor', () => {
it('creates Auditor for KibanaRequest', async () => {
const auditor = auditTrailServiceMock.createAuditor();
const auditorFactory = auditTrailServiceMock.createAuditorFactory();
auditorFactory.asScoped.mockReturnValue(auditor);
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, () => auditorFactory);
clusterClient.asScoped(httpServerMock.createKibanaRequest());

expect(MockScopedClusterClient).toHaveBeenCalledTimes(1);
expect(MockScopedClusterClient).toHaveBeenCalledWith(
expect.any(Function),
expect.any(Function),
expect.objectContaining({ 'x-opaque-id': expect.any(String) }),
auditor
);
});

it("doesn't create Auditor for a fake request", async () => {
const getAuthHeaders = jest.fn();
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, getAuthHeaders);
clusterClient.asScoped({ headers: { one: '1', two: '2', three: '3' } });

expect(getAuthHeaders).not.toHaveBeenCalled();
});

it("doesn't create Auditor when no request passed", async () => {
const getAuthHeaders = jest.fn();
clusterClient = new LegacyClusterClient(mockEsConfig, mockLogger, getAuthHeaders);
clusterClient.asScoped();

expect(getAuthHeaders).not.toHaveBeenCalled();
});
});
});

describe('#close', () => {
Expand All @@ -534,8 +440,7 @@ describe('#close', () => {

clusterClient = new LegacyClusterClient(
{ apiVersion: 'es-version', requestHeadersWhitelist: [] } as any,
logger.get(),
auditTrailServiceMock.createAuditorFactory
logger.get()
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/core/server/elasticsearch/legacy/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { Client } from 'elasticsearch';
import { get } from 'lodash';

import { LegacyElasticsearchErrorHelpers } from './errors';
import { GetAuthHeaders, KibanaRequest, isKibanaRequest, isRealRequest } from '../../http';
import { GetAuthHeaders, isKibanaRequest, isRealRequest } from '../../http';
import { filterHeaders, ensureRawRequest } from '../../http/router';
import { Logger } from '../../logging';
import { ScopeableRequest } from '../types';
Expand Down
45 changes: 0 additions & 45 deletions src/core/server/elasticsearch/legacy/scoped_cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
*/

import { LegacyScopedClusterClient } from './scoped_cluster_client';
import { auditTrailServiceMock } from '../../audit_trail/audit_trail_service.mock';

let internalAPICaller: jest.Mock;
let scopedAPICaller: jest.Mock;
Expand Down Expand Up @@ -84,28 +83,6 @@ describe('#callAsInternalUser', () => {

expect(scopedAPICaller).not.toHaveBeenCalled();
});

describe('Auditor', () => {
it('does not fail when no auditor provided', () => {
const clusterClientWithoutAuditor = new LegacyScopedClusterClient(jest.fn(), jest.fn());
expect(() => clusterClientWithoutAuditor.callAsInternalUser('endpoint')).not.toThrow();
});
it('creates an audit record if auditor provided', () => {
const auditor = auditTrailServiceMock.createAuditor();
const clusterClientWithoutAuditor = new LegacyScopedClusterClient(
jest.fn(),
jest.fn(),
{},
auditor
);
clusterClientWithoutAuditor.callAsInternalUser('endpoint');
expect(auditor.add).toHaveBeenCalledTimes(1);
expect(auditor.add).toHaveBeenLastCalledWith({
message: 'endpoint',
type: 'elasticsearch.call.internalUser',
});
});
});
});

describe('#callAsCurrentUser', () => {
Expand Down Expand Up @@ -229,26 +206,4 @@ describe('#callAsCurrentUser', () => {

expect(internalAPICaller).not.toHaveBeenCalled();
});

describe('Auditor', () => {
it('does not fail when no auditor provided', () => {
const clusterClientWithoutAuditor = new LegacyScopedClusterClient(jest.fn(), jest.fn());
expect(() => clusterClientWithoutAuditor.callAsCurrentUser('endpoint')).not.toThrow();
});
it('creates an audit record if auditor provided', () => {
const auditor = auditTrailServiceMock.createAuditor();
const clusterClientWithoutAuditor = new LegacyScopedClusterClient(
jest.fn(),
jest.fn(),
{},
auditor
);
clusterClientWithoutAuditor.callAsCurrentUser('endpoint');
expect(auditor.add).toHaveBeenCalledTimes(1);
expect(auditor.add).toHaveBeenLastCalledWith({
message: 'endpoint',
type: 'elasticsearch.call.currentUser',
});
});
});
});

0 comments on commit 20eefbc

Please sign in to comment.