Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/elastic/kibana into threa…
Browse files Browse the repository at this point in the history
…dpool_rejection_alert
  • Loading branch information
igoristic committed Oct 26, 2020
2 parents 36f80f0 + 141426d commit ffd436d
Show file tree
Hide file tree
Showing 43 changed files with 1,382 additions and 267 deletions.
36 changes: 3 additions & 33 deletions src/core/server/elasticsearch/client/client_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,28 +216,14 @@ describe('parseClientOptions', () => {
);
});

it('adds auth to the nodes if both `username` and `password` are set', () => {
let options = parseClientOptions(
it('does not add auth to the nodes', () => {
const options = parseClientOptions(
createConfig({
username: 'user',
hosts: ['http://node-A:9200'],
}),
false
);
expect(options.nodes).toMatchInlineSnapshot(`
Array [
Object {
"url": "http://node-a:9200/",
},
]
`);

options = parseClientOptions(
createConfig({
password: 'pass',
hosts: ['http://node-A:9200'],
}),
false
true
);
expect(options.nodes).toMatchInlineSnapshot(`
Array [
Expand All @@ -246,22 +232,6 @@ describe('parseClientOptions', () => {
},
]
`);

options = parseClientOptions(
createConfig({
username: 'user',
password: 'pass',
hosts: ['http://node-A:9200'],
}),
false
);
expect(options.nodes).toMatchInlineSnapshot(`
Array [
Object {
"url": "http://user:pass@node-a:9200/",
},
]
`);
});
});
describe('when `scoped` is true', () => {
Expand Down
12 changes: 2 additions & 10 deletions src/core/server/elasticsearch/client/client_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export function parseClientOptions(
};
}

clientOptions.nodes = config.hosts.map((host) => convertHost(host, !scoped, config));
clientOptions.nodes = config.hosts.map((host) => convertHost(host));

if (config.ssl) {
clientOptions.ssl = generateSslConfig(
Expand Down Expand Up @@ -140,18 +140,10 @@ const generateSslConfig = (
return ssl;
};

const convertHost = (
host: string,
needAuth: boolean,
{ username, password }: ElasticsearchClientConfig
): NodeOptions => {
const convertHost = (host: string): NodeOptions => {
const url = new URL(host);
const isHTTPS = url.protocol === 'https:';
url.port = url.port || (isHTTPS ? '443' : '80');
if (needAuth && username && password) {
url.username = username;
url.password = password;
}

return {
url,
Expand Down
61 changes: 43 additions & 18 deletions x-pack/plugins/alerts/server/task_runner/task_runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -621,9 +621,7 @@ describe('Task Runner', () => {
expect(await taskRunner.run()).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
expect(taskRunnerFactoryInitializerParams.logger.error).toHaveBeenCalledWith(
Expand Down Expand Up @@ -727,9 +725,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:00:10.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);

Expand Down Expand Up @@ -781,9 +777,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down Expand Up @@ -814,9 +808,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down Expand Up @@ -846,13 +838,48 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": 1970-01-01T00:05:00.000Z,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});

test(`doesn't change previousStartedAt when it fails to run`, async () => {
const originalAlertSate = {
previousStartedAt: '1970-01-05T00:00:00.000Z',
};

alertType.executor.mockImplementation(
({ services: executorServices }: AlertExecutorOptions) => {
throw new Error('OMG');
}
);

const taskRunner = new TaskRunner(
alertType,
{
...mockedTaskInstance,
state: originalAlertSate,
},
taskRunnerFactoryInitializerParams
);

alertsClient.get.mockResolvedValueOnce(mockedAlertTypeSavedObject);
encryptedSavedObjectsClient.getDecryptedAsInternalUser.mockResolvedValueOnce({
id: '1',
type: 'alert',
attributes: {
apiKey: Buffer.from('123:abc').toString('base64'),
},
references: [],
});

const runnerResult = await taskRunner.run();

expect(runnerResult.state.previousStartedAt).toEqual(
new Date(originalAlertSate.previousStartedAt)
);
});

test('avoids rescheduling a failed Alert Task Runner when it throws due to failing to fetch the alert', async () => {
alertsClient.get.mockImplementation(() => {
throw SavedObjectsErrorHelpers.createGenericNotFoundError('task', '1');
Expand All @@ -878,9 +905,7 @@ describe('Task Runner', () => {
expect(runnerResult).toMatchInlineSnapshot(`
Object {
"runAt": undefined,
"state": Object {
"previousStartedAt": 1970-01-01T00:00:00.000Z,
},
"state": Object {},
}
`);
});
Expand Down
9 changes: 3 additions & 6 deletions x-pack/plugins/alerts/server/task_runner/task_runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export class TaskRunner {
async run(): Promise<AlertTaskRunResult> {
const {
params: { alertId, spaceId },
startedAt: previousStartedAt,
startedAt,
state: originalState,
} = this.taskInstance;

Expand Down Expand Up @@ -360,7 +360,7 @@ export class TaskRunner {
(stateUpdates: AlertTaskState) => {
return {
...stateUpdates,
previousStartedAt,
previousStartedAt: startedAt,
};
},
(err: Error) => {
Expand All @@ -370,10 +370,7 @@ export class TaskRunner {
} else {
this.logger.error(message);
}
return {
...originalState,
previousStartedAt,
};
return originalState;
}
),
runAt: resolveErr<Date | undefined, Error>(runAt, (err) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,5 @@ export const TOKEN_TYPE_INFO = [
];

export const FLYOUT_ARIA_LABEL_ID = 'credentialsFlyoutTitle';

export const DOCS_HREF = 'https://www.elastic.co/guide/en/app-search/current/authentication.html';
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,98 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { setMockValues, setMockActions } from '../../../../__mocks__/kea.mock';

import React from 'react';
import { shallow } from 'enzyme';
import { EuiFlyoutBody } from '@elastic/eui';
import { EuiFlyoutBody, EuiForm } from '@elastic/eui';

import { ApiTokenTypes } from '../constants';
import { defaultApiToken } from '../credentials_logic';

import {
FormKeyName,
FormKeyType,
FormKeyReadWriteAccess,
FormKeyEngineAccess,
FormKeyUpdateWarning,
} from './form_components';
import { CredentialsFlyoutBody } from './body';

describe('CredentialsFlyoutBody', () => {
const values = {
activeApiToken: defaultApiToken,
activeApiTokenExists: false,
};
const actions = {
onApiTokenChange: jest.fn(),
};

beforeEach(() => {
jest.clearAllMocks();
setMockValues(values);
setMockActions(actions);
});

it('renders', () => {
const wrapper = shallow(<CredentialsFlyoutBody />);

expect(wrapper.find(EuiFlyoutBody)).toHaveLength(1);
expect(wrapper.find(EuiForm)).toHaveLength(1);
});

it('shows the expected form components on default private key creation', () => {
const wrapper = shallow(<CredentialsFlyoutBody />);

expect(wrapper.find(FormKeyName)).toHaveLength(1);
expect(wrapper.find(FormKeyType)).toHaveLength(1);
expect(wrapper.find(FormKeyReadWriteAccess)).toHaveLength(1);
expect(wrapper.find(FormKeyEngineAccess)).toHaveLength(1);
expect(wrapper.find(FormKeyUpdateWarning)).toHaveLength(0);
});

it('does not show read-write access options for search keys', () => {
setMockValues({
...values,
activeApiToken: {
...defaultApiToken,
type: ApiTokenTypes.Search,
},
});
const wrapper = shallow(<CredentialsFlyoutBody />);

expect(wrapper.find(FormKeyReadWriteAccess)).toHaveLength(0);
expect(wrapper.find(FormKeyEngineAccess)).toHaveLength(1);
});

it('does not show read-write or engine access options for admin keys', () => {
setMockValues({
...values,
activeApiToken: {
...defaultApiToken,
type: ApiTokenTypes.Admin,
},
});
const wrapper = shallow(<CredentialsFlyoutBody />);

expect(wrapper.find(FormKeyReadWriteAccess)).toHaveLength(0);
expect(wrapper.find(FormKeyEngineAccess)).toHaveLength(0);
});

it('shows a warning if updating an existing key', () => {
setMockValues({ ...values, activeApiTokenExists: true });
const wrapper = shallow(<CredentialsFlyoutBody />);

expect(wrapper.find(FormKeyUpdateWarning)).toHaveLength(1);
});

it('calls onApiTokenChange on form submit', () => {
const wrapper = shallow(<CredentialsFlyoutBody />);

const preventDefault = jest.fn();
wrapper.find(EuiForm).simulate('submit', { preventDefault });

expect(preventDefault).toHaveBeenCalled();
expect(actions.onApiTokenChange).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,41 @@
*/

import React from 'react';
import { EuiFlyoutBody } from '@elastic/eui';
import { useValues, useActions } from 'kea';
import { EuiFlyoutBody, EuiForm } from '@elastic/eui';

import { FlashMessages } from '../../../../shared/flash_messages';
import { CredentialsLogic } from '../credentials_logic';
import { ApiTokenTypes } from '../constants';

import {
FormKeyName,
FormKeyType,
FormKeyReadWriteAccess,
FormKeyEngineAccess,
FormKeyUpdateWarning,
} from './form_components';

export const CredentialsFlyoutBody: React.FC = () => {
const { onApiTokenChange } = useActions(CredentialsLogic);
const { activeApiToken, activeApiTokenExists } = useValues(CredentialsLogic);

return (
<EuiFlyoutBody>
<FlashMessages />
Details go here
<EuiForm
onSubmit={(e) => {
e.preventDefault();
onApiTokenChange();
}}
component="form"
>
<FormKeyName />
<FormKeyType />
{activeApiToken.type === ApiTokenTypes.Private && <FormKeyReadWriteAccess />}
{activeApiToken.type !== ApiTokenTypes.Admin && <FormKeyEngineAccess />}
</EuiForm>
{activeApiTokenExists && <FormKeyUpdateWarning />}
</EuiFlyoutBody>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

export { FormKeyName } from './key_name';
export { FormKeyType } from './key_type';
export { FormKeyReadWriteAccess } from './key_read_write_access';
export { FormKeyEngineAccess } from './key_engine_access';
export { FormKeyUpdateWarning } from './key_update_warning';
Loading

0 comments on commit ffd436d

Please sign in to comment.