[7.x] use a custom wrapper around chance with longer defaults… (#49866)

* use a custom wrapper around chance with longer defaults to avoid conflicts

* fix a reference to chance

* fix another reference to chance service

# Conflicts:
#	x-pack/test/saml_api_integration/apis/security/saml_login.ts
This commit is contained in:
Spencer 2019-10-31 08:52:10 -07:00 committed by GitHub
parent 9f4673d4ec
commit c88cad27ca
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 147 additions and 93 deletions

View file

@ -20,7 +20,7 @@
export default function ({ getService }) {
const esArchiver = getService('esArchiver');
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');
describe('params', () => {
before(() => esArchiver.load('index_patterns/basic_index'));
@ -63,8 +63,8 @@ export default function ({ getService }) {
supertest
.get('/api/index_patterns/_fields_for_wildcard')
.query({
pattern: chance.word(),
[chance.word()]: chance.word(),
pattern: randomness.word(),
[randomness.word()]: randomness.word(),
})
.expect(400));
});

View file

@ -1,29 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Chance from 'chance';
export function ChanceProvider({ getService }) {
const log = getService('log');
const seed = Date.now();
log.debug('randomness seed: %j', seed);
return new Chance(seed);
}

View file

@ -22,14 +22,11 @@ import { services as commonServices } from '../../common/services';
// @ts-ignore not TS yet
import { KibanaSupertestProvider, ElasticsearchSupertestProvider } from './supertest';
// @ts-ignore not TS yet
import { ChanceProvider } from './chance';
export const services = {
es: commonServices.es,
esArchiver: commonServices.esArchiver,
retry: commonServices.retry,
supertest: KibanaSupertestProvider,
esSupertest: ElasticsearchSupertestProvider,
chance: ChanceProvider,
randomness: commonServices.randomness,
};

View file

@ -21,10 +21,12 @@ import { LegacyEsProvider } from './legacy_es';
import { EsArchiverProvider } from './es_archiver';
import { KibanaServerProvider } from './kibana_server';
import { RetryProvider } from './retry';
import { RandomnessProvider } from './randomness';
export const services = {
es: LegacyEsProvider,
esArchiver: EsArchiverProvider,
kibanaServer: KibanaServerProvider,
retry: RetryProvider,
randomness: RandomnessProvider,
};

View file

@ -0,0 +1,89 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import Chance from 'chance';
import { FtrProviderContext } from '../ftr_provider_context';
interface CharOptions {
pool?: string;
alpha?: boolean;
numeric?: boolean;
symbols?: boolean;
casing?: 'lower' | 'upper';
}
interface StringOptions extends CharOptions {
length?: number;
}
interface NumberOptions {
min?: number;
max?: number;
}
export function RandomnessProvider({ getService }: FtrProviderContext) {
const log = getService('log');
const seed = Date.now();
log.debug('randomness seed: %j', seed);
const chance = new Chance(seed);
return new (class RandomnessService {
/**
* Generate a random natural number
*
* range: 0 to 9007199254740991
*
*/
naturalNumber(options?: NumberOptions) {
return chance.natural(options);
}
/**
* Generate a random integer
*/
integer(options?: NumberOptions) {
return chance.integer(options);
}
/**
* Generate a random number, defaults to at least 4 and no more than 8 syllables
*/
word(options: { syllables?: number } = {}) {
const { syllables = this.naturalNumber({ min: 4, max: 8 }) } = options;
return chance.word({
syllables,
});
}
/**
* Generate a random string, defaults to at least 8 and no more than 15 alpha-numeric characters
*/
string(options: StringOptions = {}) {
return chance.string({
length: this.naturalNumber({ min: 8, max: 15 }),
...(options.pool === 'undefined' ? { alpha: true, numeric: true, symbols: false } : {}),
...options,
});
}
})();
}

View file

@ -11,7 +11,7 @@ export default function ({ getService }) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const es = getService('es');
const chance = getService('chance');
const randomness = getService('randomness');
describe('assign_tags_to_beats', () => {
const archive = 'beats/list';
@ -179,9 +179,8 @@ export default function ({ getService }) {
expect(beat.tags).to.eql(['production']);
});
// FLAKY: https://github.com/elastic/kibana/issues/34038
it.skip('should return errors for non-existent beats', async () => {
const nonExistentBeatId = chance.word();
it('should return errors for non-existent beats', async () => {
const nonExistentBeatId = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')
@ -197,7 +196,7 @@ export default function ({ getService }) {
});
it('should return errors for non-existent tags', async () => {
const nonExistentTag = chance.word();
const nonExistentTag = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')
@ -221,8 +220,8 @@ export default function ({ getService }) {
});
it('should return errors for non-existent beats and tags', async () => {
const nonExistentBeatId = chance.word();
const nonExistentTag = chance.word();
const nonExistentBeatId = randomness.word();
const nonExistentTag = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/assignments')

View file

@ -11,7 +11,7 @@ import { ES_INDEX_NAME } from './constants';
export default function ({ getService }) {
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');
const es = getService('es');
describe('enroll_beat', () => {
@ -20,20 +20,20 @@ export default function ({ getService }) {
let beat;
beforeEach(async () => {
validEnrollmentToken = chance.word();
validEnrollmentToken = randomness.word();
beatId = chance.word();
beatId = randomness.word();
const version =
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 });
randomness.integer({ min: 1, max: 10 });
beat = {
type: 'filebeat',
host_name: 'foo.bar.com',
name: chance.word(),
name: randomness.word(),
version,
};
@ -94,7 +94,7 @@ export default function ({ getService }) {
const { body: apiResponse } = await supertest
.post(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')
.set('kbn-beats-enrollment-token', chance.word())
.set('kbn-beats-enrollment-token', randomness.word())
.send(beat)
.expect(400);

View file

@ -11,7 +11,7 @@ export default function ({ getService }) {
const supertest = getService('supertest');
const esArchiver = getService('esArchiver');
const es = getService('es');
const chance = getService('chance');
const randomness = getService('randomness');
describe('remove_tags_from_beats', () => {
const archive = 'beats/list';
@ -135,7 +135,7 @@ export default function ({ getService }) {
});
it('should return errors for non-existent beats', async () => {
const nonExistentBeatId = chance.word();
const nonExistentBeatId = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')
@ -151,7 +151,7 @@ export default function ({ getService }) {
});
it('should return errors for non-existent tags', async () => {
const nonExistentTag = chance.word();
const nonExistentTag = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')
@ -175,8 +175,8 @@ export default function ({ getService }) {
});
it('should return errors for non-existent beats and tags', async () => {
const nonExistentBeatId = chance.word();
const nonExistentTag = chance.word();
const nonExistentBeatId = randomness.word();
const nonExistentTag = randomness.word();
const { body: apiResponse } = await supertest
.post('/api/beats/agents_tags/removals')

View file

@ -9,7 +9,6 @@ import { ES_INDEX_NAME } from './constants';
export default function ({ getService }) {
const supertest = getService('supertest');
// const chance = getService('chance');
const es = getService('es');
const esArchiver = getService('esArchiver');

View file

@ -10,7 +10,7 @@ import moment from 'moment';
export default function ({ getService }) {
const supertest = getService('supertest');
const chance = getService('chance');
const randomness = getService('randomness');
const es = getService('es');
const esArchiver = getService('esArchiver');
@ -27,18 +27,18 @@ export default function ({ getService }) {
'SSsX2Byyo1B1bGxV8C3G4QldhE5iH87EY_1r21-bwbI';
const version =
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 }) +
randomness.integer({ min: 1, max: 10 }) +
'.' +
chance.integer({ min: 1, max: 10 });
randomness.integer({ min: 1, max: 10 });
beat = {
type: `${chance.word()}beat`,
host_name: `www.${chance.word()}.net`,
name: chance.word(),
type: `${randomness.word()}beat`,
host_name: `www.${randomness.word()}.net`,
name: randomness.word(),
version,
ephemeral_id: chance.word(),
ephemeral_id: randomness.word(),
};
await es.index({
@ -90,7 +90,7 @@ export default function ({ getService }) {
const { body } = await supertest
.put(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')
.set('kbn-beats-access-token', chance.word())
.set('kbn-beats-access-token', randomness.word())
.send(beat)
.expect(401);
@ -109,7 +109,7 @@ export default function ({ getService }) {
});
it('should return an error for a non-existent beat', async () => {
const beatId = chance.word();
const beatId = randomness.word();
const { body } = await supertest
.put(`/api/beats/agent/${beatId}`)
.set('kbn-xsrf', 'xxx')

View file

@ -25,14 +25,11 @@ import { SiemGraphQLClientProvider, SiemGraphQLClientFactoryProvider } from './s
import { InfraOpsSourceConfigurationProvider } from './infraops_source_configuration';
export const services = {
chance: kibanaApiIntegrationServices.chance,
...kibanaCommonServices,
esSupertest: kibanaApiIntegrationServices.esSupertest,
supertest: kibanaApiIntegrationServices.supertest,
esArchiver: kibanaCommonServices.esArchiver,
kibanaServer: kibanaCommonServices.kibanaServer,
retry: kibanaCommonServices.retry,
es: LegacyEsProvider,
esSupertestWithoutAuth: EsSupertestWithoutAuthProvider,
infraOpsGraphQLClient: InfraOpsGraphQLClientProvider,

View file

@ -13,7 +13,7 @@ export function RandomProvider({ getService }) {
log.debug('randomness seed: %j', seed);
const chance = new Chance(seed);
return new class Random {
return new class Randomness {
int(min = 3, max = 15) {
return chance.integer({ min, max });
}

View file

@ -10,7 +10,7 @@ import { FtrProviderContext } from '../../ftr_provider_context';
export default function({ getService }: FtrProviderContext) {
const es = getService('es');
const chance = getService('chance');
const randomness = getService('randomness');
const supertest = getService('supertest');
const SAVED_OBJECT_WITH_SECRET_TYPE = 'saved-object-with-secret';
@ -37,9 +37,9 @@ export default function({ getService }: FtrProviderContext) {
let savedObject: SavedObject;
beforeEach(async () => {
savedObjectOriginalAttributes = {
publicProperty: chance.string(),
publicPropertyExcludedFromAAD: chance.string(),
privateProperty: chance.string(),
publicProperty: randomness.string(),
publicPropertyExcludedFromAAD: randomness.string(),
privateProperty: randomness.string(),
};
const { body } = await supertest
@ -74,17 +74,17 @@ export default function({ getService }: FtrProviderContext) {
{
type: SAVED_OBJECT_WITH_SECRET_TYPE,
attributes: {
publicProperty: chance.string(),
publicPropertyExcludedFromAAD: chance.string(),
privateProperty: chance.string(),
publicProperty: randomness.string(),
publicPropertyExcludedFromAAD: randomness.string(),
privateProperty: randomness.string(),
},
},
{
type: SAVED_OBJECT_WITH_SECRET_TYPE,
attributes: {
publicProperty: chance.string(),
publicPropertyExcludedFromAAD: chance.string(),
privateProperty: chance.string(),
publicProperty: randomness.string(),
publicPropertyExcludedFromAAD: randomness.string(),
privateProperty: randomness.string(),
},
},
];
@ -162,9 +162,9 @@ export default function({ getService }: FtrProviderContext) {
it('#update encrypts attributes and strips them from response', async () => {
const updatedAttributes = {
publicProperty: chance.string(),
publicPropertyExcludedFromAAD: chance.string(),
privateProperty: chance.string(),
publicProperty: randomness.string(),
publicPropertyExcludedFromAAD: randomness.string(),
privateProperty: randomness.string(),
};
const { body: response } = await supertest
@ -197,7 +197,7 @@ export default function({ getService }: FtrProviderContext) {
});
it('#getDecryptedAsInternalUser is able to decrypt if non-AAD attribute has changed', async () => {
const updatedAttributes = { publicPropertyExcludedFromAAD: chance.string() };
const updatedAttributes = { publicPropertyExcludedFromAAD: randomness.string() };
const { body: response } = await supertest
.put(`${getURLAPIBaseURL()}${SAVED_OBJECT_WITH_SECRET_TYPE}/${savedObject.id}`)
@ -220,7 +220,7 @@ export default function({ getService }: FtrProviderContext) {
});
it('#getDecryptedAsInternalUser fails to decrypt if AAD attribute has changed', async () => {
const updatedAttributes = { publicProperty: chance.string() };
const updatedAttributes = { publicProperty: randomness.string() };
const { body: response } = await supertest
.put(`${getURLAPIBaseURL()}${SAVED_OBJECT_WITH_SECRET_TYPE}/${savedObject.id}`)

View file

@ -14,7 +14,7 @@ import { getLogoutRequest, getSAMLRequestId, getSAMLResponse } from '../../fixtu
import { FtrProviderContext } from '../../ftr_provider_context';
export default function({ getService }: FtrProviderContext) {
const chance = getService('chance');
const randomness = getService('randomness');
const supertest = getService('supertestWithoutAuth');
const config = getService('config');
@ -23,7 +23,7 @@ export default function({ getService }: FtrProviderContext) {
function createSAMLResponse(options = {}) {
return getSAMLResponse({
destination: `http://localhost:${kibanaServerConfig.port}/api/security/v1/saml`,
sessionIndex: chance.natural(),
sessionIndex: String(randomness.naturalNumber()),
...options,
});
}
@ -365,7 +365,7 @@ export default function({ getService }: FtrProviderContext) {
const handshakeCookie = request.cookie(handshakeResponse.headers['set-cookie'][0])!;
const samlRequestId = await getSAMLRequestId(handshakeResponse.headers.location);
idpSessionIndex = chance.natural();
idpSessionIndex = String(randomness.naturalNumber());
const samlAuthenticationResponse = await supertest
.post('/api/security/saml/callback')
.set('kbn-xsrf', 'xxx')

View file

@ -20,7 +20,7 @@ export default async function({ readConfigFile }: FtrConfigProviderContext) {
testFiles: [require.resolve('./apis')],
servers: xPackAPITestsConfig.get('servers'),
services: {
chance: kibanaAPITestsConfig.get('services.chance'),
randomness: kibanaAPITestsConfig.get('services.randomness'),
es: kibanaAPITestsConfig.get('services.es'),
supertestWithoutAuth: xPackAPITestsConfig.get('services.supertestWithoutAuth'),
},

View file

@ -7,7 +7,7 @@
import { services as apiIntegrationServices } from '../api_integration/services';
export const services = {
chance: apiIntegrationServices.chance,
randomness: apiIntegrationServices.randomness,
es: apiIntegrationServices.es,
supertestWithoutAuth: apiIntegrationServices.supertestWithoutAuth,
};