Skip to content

Commit

Permalink
feat(deploy): add validation to contactPoint field
Browse files Browse the repository at this point in the history
Closes #2863
  • Loading branch information
smbea committed Apr 4, 2022
1 parent 516018d commit 0bf8a56
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ export default class DeploymentPlugin extends PureComponent {
id: generateId(),
targetType: CAMUNDA_CLOUD,
authType: AUTH_TYPES.NONE,
contactPoint: '0.0.0.0:26500',
contactPoint: '',
oauthURL: '',
audience: '',
clientId: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ export const SELF_HOSTED_TEXT = 'Camunda Platform 8 Self-Managed';
export const OAUTH_TEXT = 'OAuth';
export const NONE = 'None';
export const CAMUNDA_CLOUD_TEXT = 'Camunda Platform 8 SaaS';
export const CONTACT_POINT = 'Contact Point';
export const CONTACT_POINT = 'Cluster endpoint';
export const DEPLOYMENT_NAME_HINT = 'Default value is the file name.';
export const CONTACT_POINT_HINT = 'Default value is 0.0.0.0:26500';
export const CONTACT_POINT_HINT = '0.0.0.0:26500';
export const OAUTH_URL = 'OAuth URL';
export const AUDIENCE = 'Audience';
export const CLIENT_ID = 'Client ID';
Expand All @@ -30,7 +30,9 @@ export const CLUSTER_URL = 'Cluster URL';
export const REMEMBER_CREDENTIALS = 'Remember credentials';

export const MUST_PROVIDE_A_VALUE = 'Must provide a value.';
export const CONTACT_POINT_MUST_NOT_BE_EMPTY = 'Contact point must not be empty.';
export const CONTACT_POINT_MUST_NOT_BE_EMPTY = 'Cluster endpoint must not be empty.';
export const CONTACT_POINT_MUST_HAVE_VALID_PORT = 'Cluster endpoint must contain a valid port.';
export const CONTACT_POINT_MUST_BE_URL_OR_IP = 'Cluster endpoint must a valid URL or IP address.';
export const OAUTH_URL_MUST_NOT_BE_EMPTY = 'OAuth URL must not be empty.';
export const AUDIENCE_MUST_NOT_BE_EMPTY = 'Audience must not be empty.';
export const CLIENT_ID_MUST_NOT_BE_EMPTY = 'Client ID must not be empty.';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ export default class DeploymentPluginOverlay extends React.PureComponent {
name="endpoint.contactPoint"
component={ TextInput }
label={ CONTACT_POINT }
validate={ validatorFunctionsByFieldNames.contactPoint }
fieldError={ this.endpointConfigurationFieldError }
hint={ CONTACT_POINT_HINT }
autoFocus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
AUDIENCE_MUST_NOT_BE_EMPTY,
CLIENT_ID_MUST_NOT_BE_EMPTY,
CLIENT_SECRET_MUST_NOT_BE_EMPTY,
CLUSTER_URL_MUST_BE_VALID_CLOUD_URL
CLUSTER_URL_MUST_BE_VALID_CLOUD_URL,
CONTACT_POINT_MUST_HAVE_VALID_PORT,
CONTACT_POINT_MUST_BE_URL_OR_IP
} from './DeploymentPluginConstants';

import { AUTH_TYPES } from '../shared/ZeebeAuthTypes';
Expand All @@ -39,12 +41,12 @@ export default class DeploymentPluginValidator {
return value ? null : message;
}

validateUrl = (value, message = CLUSTER_URL_MUST_BE_VALID_CLOUD_URL) => {
return validCloudUrl(value) ? null : message;
}

validateZeebeContactPoint = (value) => {
return this.validateNonEmpty(value, CONTACT_POINT_MUST_NOT_BE_EMPTY);
const invalidEndpoint = validateUrl(value, true) && validateIPAdress(value, true);

return this.validateNonEmpty(value, CONTACT_POINT_MUST_NOT_BE_EMPTY) ||
(invalidEndpoint ? CONTACT_POINT_MUST_BE_URL_OR_IP : null) ||
validatePort(value, CONTACT_POINT_MUST_HAVE_VALID_PORT);
}

validateOAuthURL = (value) => {
Expand All @@ -64,7 +66,7 @@ export default class DeploymentPluginValidator {
}

validateClusterUrl = (value) => {
return this.validateUrl(value, CLUSTER_URL_MUST_BE_VALID_CLOUD_URL);
return validCloudUrl(value) ? null : CLUSTER_URL_MUST_BE_VALID_CLOUD_URL;
}

validateConnection = (endpoint) => {
Expand Down Expand Up @@ -311,3 +313,52 @@ function shallowEquals(a, b) {
function validCloudUrl(url) {
return /^(https:\/\/|)[a-z\d-]+\.[a-z]+-\d+\.zeebe\.camunda\.io(:443|)\/?/.test(url);
}

const checkNumberLimits = (string, min, max) => {
const number = parseInt(string);
const ret = !isNaN(number) && number >= min && number <= max;
return ret;
};

const validateUrl = (value, message) => {
let fullURL = value;

if (!(value.startsWith('https://') || value.startsWith('http://'))) {
fullURL = 'http://' + value;
}

try {
new URL(fullURL);
console.log(fullURL);
return null;

} catch (e) {
console.log('exception');
return message;
}
};

const validateIPAdress = (value, message) => {
var parts = value.split(':');
var ip = parts[0].split('.');

if (!(ip.length == 4 &&
ip.every(function(segment) {
return checkNumberLimits(segment, 0, 255);
}))) {
return message;
}

return null;
};

const validatePort = (value, message) => {
var parts = value.split(':');
var port = (parts.length > 1) ? parts[parts.length - 1] : null;

if (!(port && checkNumberLimits(port, 0, 65535))) {
return message;
}

return null;
};
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
CLIENT_ID_MUST_NOT_BE_EMPTY,
CLIENT_SECRET_MUST_NOT_BE_EMPTY,
CLUSTER_URL_MUST_BE_VALID_CLOUD_URL,
CONTACT_POINT_MUST_HAVE_VALID_PORT,
} from '../DeploymentPluginConstants';


Expand All @@ -28,18 +29,37 @@ describe('<DeploymentPluginValidator> (Zeebe)', () => {

const validator = new DeploymentPluginValidator(null);

it('should validate Zeebe contact point', () => {
it('should validate Zeebe contact point - IP address', () => {

// given
const nonValidZeebeContactPoint = '';
const validZeebeContactPoint = 'validZeebeContactPoint';
const emptyZeebeContactPoint = '';
const noPortZeebeContactPoint = '0.0.0.0';
const validZeebeContactPoint = '0.0.0.0:0001';

// then
expect(validator.validateZeebeContactPoint(nonValidZeebeContactPoint)).to.eql(CONTACT_POINT_MUST_NOT_BE_EMPTY);
expect(validator.validateZeebeContactPoint(emptyZeebeContactPoint)).to.eql(CONTACT_POINT_MUST_NOT_BE_EMPTY);
expect(validator.validateZeebeContactPoint(noPortZeebeContactPoint)).to.eql(CONTACT_POINT_MUST_HAVE_VALID_PORT);
expect(validator.validateZeebeContactPoint(validZeebeContactPoint)).to.not.exist;
});


it('should validate Zeebe contact point - URL', () => {

// given
const emptyZeebeContactPoint = '';
const noPortZeebeContactPoint = 'foo.bar';
const validZeebeContactPoint = 'foo.bar:0001';
const validZeebeContactPointFullURL = 'https://foo.bar:0001';

// then
expect(validator.validateZeebeContactPoint(emptyZeebeContactPoint)).to.eql(CONTACT_POINT_MUST_NOT_BE_EMPTY);
expect(validator.validateZeebeContactPoint(noPortZeebeContactPoint)).to.eql(CONTACT_POINT_MUST_HAVE_VALID_PORT);
expect(validator.validateZeebeContactPoint(validZeebeContactPoint)).to.not.exist;
expect(validator.validateZeebeContactPoint(validZeebeContactPointFullURL)).to.not.exist;

});


it('should validate OAuth URL', () => {

// given
Expand Down Expand Up @@ -152,7 +172,7 @@ describe('<DeploymentPluginValidator> (Zeebe)', () => {
endpoint: {
targetType: 'selfHosted',
authType: 'none',
contactPoint: 'https://camunda.com'
contactPoint: 'https://camunda.com:0001'
}
};

Expand All @@ -167,7 +187,7 @@ describe('<DeploymentPluginValidator> (Zeebe)', () => {

// then
expect(Object.keys(validator.validateConfig(config))).to.have.lengthOf(0);
expect(Object.keys(validator.validateConfig(wrongConfig))).to.have.lengthOf(1);
expect(Object.keys(validator.validateConfig(wrongConfig))).to.have.lengthOf(2);
});


Expand All @@ -181,7 +201,7 @@ describe('<DeploymentPluginValidator> (Zeebe)', () => {
endpoint: {
targetType: 'selfHosted',
authType: 'oauth',
contactPoint: 'https://camunda.com',
contactPoint: 'https://camunda.com:0001',
oauthURL: 'https://camunda.com',
audience: 'bearer',
clientId: 'test',
Expand All @@ -203,7 +223,7 @@ describe('<DeploymentPluginValidator> (Zeebe)', () => {

// then
expect(Object.keys(validator.validateConfig(config))).to.have.lengthOf(0);
expect(Object.keys(validator.validateConfig(wrongConfig))).to.have.lengthOf(3);
expect(Object.keys(validator.validateConfig(wrongConfig))).to.have.lengthOf(4);
});
});

Expand Down

0 comments on commit 0bf8a56

Please sign in to comment.