Skip to content

Commit

Permalink
[BUG] Add support for CIDR mask filters on IP fields (#300)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Torres <[email protected]>

Fixes #306
  • Loading branch information
micrictor authored and kavilla committed May 21, 2021
1 parent fa5ce9a commit 62a3115
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
getOperatorFromFilter,
getOperatorOptions,
isFilterValid,
validateParams,
} from './filter_editor_utils';

import { existsOperator, isBetweenOperator, isOneOfOperator, isOperator } from './filter_operators';
Expand Down Expand Up @@ -149,6 +150,69 @@ describe('Filter editor utils', () => {
});
});

describe('validateParams', () => {
it('should return false if date is not string', () => {
const isValidParams = validateParams(1234, 'date');
expect(isValidParams).toBe(false);
});

it('should return false if date is not valid string', () => {
const isValidParams = validateParams('this is not a date', 'date');
expect(isValidParams).toBe(false);
});

it('should return true if date is valid relative time', () => {
const isValidParams = validateParams('now-15m', 'date');
expect(isValidParams).toBe(true);
});

it('should return true if date is valid absolute time', () => {
const isValidParams = validateParams('1997-01-03', 'date');
expect(isValidParams).toBe(true);
});

it('should return false if ip address is not valid CIDR or IP', () => {
const isValidParams = validateParams('not.a.valid.ip', 'ip');
expect(isValidParams).toBe(false);
});

it('should return false if ip address is has out-of-range octets', () => {
const isValidParams = validateParams('1.1.1.256', 'ip');
expect(isValidParams).toBe(false);
});

it('should return false if ip address CIDR has invalid mask', () => {
const isValidParams = validateParams('1.1.1.1/33', 'ip');
expect(isValidParams).toBe(false);
});

it('should return false if ip address CIDR has invalid network', () => {
const isValidParams = validateParams('256.256.256.256/24', 'ip');
expect(isValidParams).toBe(false);
});

it('should return false if ip address CIDR is invalid CIDR', () => {
const isValidParams = validateParams('192.168.0.0//24', 'ip');
expect(isValidParams).toBe(false);
});

it('should return true if ip address is valid string', () => {
const isValidParams = validateParams('1.1.1.1', 'ip');
expect(isValidParams).toBe(true);
});

it('should return true if ip address is valid CIDR string', () => {
const isValidParams = validateParams('192.168.0.0/24', 'ip');
expect(isValidParams).toBe(true);
});

it('should return true if type is not ip or date', () => {
const testTypes = ['text', 'version', 'keyword'];
const isValidParams = testTypes.every((type) => validateParams('test filter', type));
expect(isValidParams).toBe(true);
});
});

describe('isFilterValid', () => {
it('should return false if index pattern is not provided', () => {
const isValid = isFilterValid(undefined, stubFields[0], isOperator, 'foo');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import dateMath from '@elastic/datemath';
import { FILTER_OPERATORS, Operator } from './filter_operators';
import {
CidrMask,
isFilterable,
IIndexPattern,
IFieldType,
Expand Down Expand Up @@ -68,7 +69,9 @@ export function validateParams(params: any, type: string) {
return Boolean(typeof params === 'string' && moment && moment.isValid());
case 'ip':
try {
return Boolean(new Ipv4Address(params));
return params.includes('/')
? Boolean(new CidrMask(params))
: Boolean(new Ipv4Address(params));
} catch (e) {
return false;
}
Expand Down

0 comments on commit 62a3115

Please sign in to comment.