Skip to content

Commit

Permalink
UI: validate source/destination service in microsegmentation (#1857)
Browse files Browse the repository at this point in the history
Signed-off-by: craman <[email protected]>

Co-authored-by: craman <[email protected]>
  • Loading branch information
chandrasekhar1996 and craman authored Apr 22, 2022
1 parent da56333 commit f32851d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
20 changes: 19 additions & 1 deletion ui/src/__tests__/components/utils/RegexUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import React from 'react';
import RegexUtils from '../../../components/utils/RegexUtils';
import { StaticWorkloadType } from '../../../components/constants/constants';
import {
MICROSEGMENTATION_SERVICE_NAME_REGEX,
StaticWorkloadType,
} from '../../../components/constants/constants';

describe('RegexUtils', () => {
it('should test regex pattern match', () => {
Expand All @@ -43,5 +46,20 @@ describe('RegexUtils', () => {
expect(
RegexUtils.validate('a.8.9.9', StaticWorkloadType[4].pattern)
).toEqual(null);
expect(
RegexUtils.validate(
'domain:group.member',
MICROSEGMENTATION_SERVICE_NAME_REGEX
)
).toEqual(false);
expect(
RegexUtils.validate('*', MICROSEGMENTATION_SERVICE_NAME_REGEX)
).toEqual(true);
expect(
RegexUtils.validate(
'domain.service',
MICROSEGMENTATION_SERVICE_NAME_REGEX
)
).toEqual(true);
});
});
2 changes: 2 additions & 0 deletions ui/src/components/constants/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export const GROUP_NAME_REGEX =
'([a-zA-Z0-9_][a-zA-Z0-9_-]*\\.)*[a-zA-Z0-9_][a-zA-Z0-9_-]*';
export const GROUP_MEMBER_NAME_REGEX =
'([a-zA-Z0-9_][a-zA-Z0-9_-]*\\.)*[a-zA-Z0-9_][a-zA-Z0-9_-]*';
export const MICROSEGMENTATION_SERVICE_NAME_REGEX =
'\\*|([a-zA-Z0-9_][a-zA-Z0-9_-]*\\.)*[a-zA-Z0-9_][a-zA-Z0-9_-]*';
export const GROUP_ROLES_CATEGORY = 'group-roles';
export const GROUP_MEMBER_PLACEHOLDER = 'user.<userid> or <domain>.<service>';
export const DISPLAY_SPACE = '\u23b5';
Expand Down
49 changes: 49 additions & 0 deletions ui/src/components/microsegmentation/AddSegmentation.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ import RequestUtils from '../utils/RequestUtils';
import InputDropdown from '../denali/InputDropdown';
import Icon from '../denali/icons/Icon';
import {
MICROSEGMENTATION_SERVICE_NAME_REGEX,
MODAL_TIME_OUT,
SEGMENTATION_CATEGORIES,
SEGMENTATION_PROTOCOL,
SERVICE_NAME_REGEX,
} from '../constants/constants';
import NameUtils from '../utils/NameUtils';
import RadioButtonGroup from '../denali/RadioButtonGroup';
import CheckBox from '../denali/CheckBox';
import MicrosegmentationValidationModal from '../modal/MicrosegmentationValidationModal';
import RegexUtils from '../utils/RegexUtils';

const SectionDiv = styled.div`
align-items: flex-start;
Expand Down Expand Up @@ -154,6 +157,8 @@ export default class AddSegmentation extends React.Component {
this.editValidationPolicy = this.editValidationPolicy.bind(this);
this.validateMicrosegmentationPolicy =
this.validateMicrosegmentationPolicy.bind(this);
this.validateFields = this.validateFields.bind(this);
this.validateServiceNames = this.validateServiceNames.bind(this);

this.state = {
category: this.props.editMode
Expand Down Expand Up @@ -503,6 +508,24 @@ export default class AddSegmentation extends React.Component {
return result;
}

validateServiceNames(serviceMembers) {
let error = true;
serviceMembers.forEach((serviceMember) => {
let memberName = serviceMember.memberName
? serviceMember.memberName
: serviceMember;
if (
!RegexUtils.validate(
memberName,
MICROSEGMENTATION_SERVICE_NAME_REGEX
)
) {
error = false;
}
});
return error;
}

validateFields() {
if (this.state.isCategory) {
if (
Expand All @@ -527,6 +550,19 @@ export default class AddSegmentation extends React.Component {
return 1;
}

if (
!this.validateServiceNames(
NameUtils.splitNames(this.state.sourceServiceMembers)
) ||
!this.validateServiceNames(this.state.members)
) {
this.setState({
errorMessage: 'Invalid source service',
saving: 'todo',
});
return 1;
}

if (!this.state.sourcePort || this.state.sourcePort === '') {
this.setState({
errorMessage: 'Source port is required.',
Expand Down Expand Up @@ -562,6 +598,19 @@ export default class AddSegmentation extends React.Component {
return 1;
}

if (
!this.validateServiceNames(
NameUtils.splitNames(this.state.destinationServiceMembers)
) ||
!this.validateServiceNames(this.state.members)
) {
this.setState({
errorMessage: 'Invalid destination service.',
saving: 'todo',
});
return 1;
}

if (
!this.state.destinationPort ||
this.state.destinationPort === ''
Expand Down

0 comments on commit f32851d

Please sign in to comment.