Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Admin setting for private network CIDR #446

Closed
wants to merge 8 commits into from
7 changes: 4 additions & 3 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,10 @@ export interface ForgotPassword {
}

export interface SystemInformation {
domain_name: string;
sip_domain_name: string;
monitoring_domain_name: string;
domain_name: null | string;
sip_domain_name: null | string;
monitoring_domain_name: null | string;
private_network_cidr: null | string;
}

export interface TtsCache {
Expand Down
49 changes: 41 additions & 8 deletions src/containers/internal/views/settings/admin-settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { PasswordSettings, SystemInformation, TtsCache } from "src/api/types";
import { toastError, toastSuccess } from "src/store";
import { Selector } from "src/components/forms";
import { hasValue } from "src/utils";
import { hasValue, isvalidIpv4OrCidr } from "src/utils";
import { PASSWORD_LENGTHS_OPTIONS, PASSWORD_MIN } from "src/api/constants";
import { Modal } from "src/components";

Expand All @@ -26,6 +26,7 @@ export const AdminSettings = () => {
const [requireSpecialCharacter, setRequireSpecialCharacter] = useState(false);
const [domainName, setDomainName] = useState("");
const [sipDomainName, setSipDomainName] = useState("");
const [privateNetworkCidr, setPrivateNetworkCidr] = useState("");
const [monitoringDomainName, setMonitoringDomainName] = useState("");
const [clearTtsCacheFlag, setClearTtsCacheFlag] = useState(false);

Expand All @@ -44,10 +45,21 @@ export const AdminSettings = () => {
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();

if (privateNetworkCidr) {
const cidrs = privateNetworkCidr.split(",");
for (const cidr of cidrs) {
if (!isvalidIpv4OrCidr(cidr)) {
toastError(`Invalid private network CIDR for "${cidr}"`);
return;
}
}
}

const systemInformationPayload: Partial<SystemInformation> = {
domain_name: domainName,
sip_domain_name: sipDomainName,
monitoring_domain_name: monitoringDomainName,
domain_name: domainName || null,
sip_domain_name: sipDomainName || null,
monitoring_domain_name: monitoringDomainName || null,
private_network_cidr: privateNetworkCidr || null,
};
const passwordSettingsPayload: Partial<PasswordSettings> = {
min_password_length: minPasswordLength,
Expand All @@ -61,7 +73,7 @@ export const AdminSettings = () => {
.then(() => {
passwordSettingsFetcher();
systemInformationFetcher();
toastSuccess("Password settings successfully updated");
toastSuccess("Admin settings updated successfully");
})
.catch((error) => {
toastError(error.msg);
Expand All @@ -79,9 +91,21 @@ export const AdminSettings = () => {
}
}
if (hasValue(systemInformation)) {
setDomainName(systemInformation.domain_name);
setSipDomainName(systemInformation.sip_domain_name);
setMonitoringDomainName(systemInformation.monitoring_domain_name);
if (systemInformation.domain_name) {
setDomainName(systemInformation.domain_name);
}

if (systemInformation.sip_domain_name) {
setSipDomainName(systemInformation.sip_domain_name);
}

if (systemInformation.monitoring_domain_name) {
setMonitoringDomainName(systemInformation.monitoring_domain_name);
}

if (systemInformation.private_network_cidr) {
setPrivateNetworkCidr(systemInformation.private_network_cidr);
}
}
}, [passwordSettings, systemInformation]);

Expand All @@ -107,6 +131,15 @@ export const AdminSettings = () => {
value={sipDomainName}
onChange={(e) => setSipDomainName(e.target.value)}
/>
<label htmlFor="name">Private Network CIDR</label>
<input
id="private_network_cidr"
type="text"
name="private_network_cidr"
placeholder="Private network CIDR"
value={privateNetworkCidr}
onChange={(e) => setPrivateNetworkCidr(e.target.value)}
/>
<label htmlFor="name">Monitoring Domain Name</label>
<input
id="monitor_domain_name"
Expand Down
16 changes: 16 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ export const getIpValidationType = (ipv4: string): IpType => {
return type;
};

function isValidIPV4(ip: string): boolean {
const ipv4Pattern =
/^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
return ipv4Pattern.test(ip);
}

function isValidCIDR(cidr: string): boolean {
const cidrPattern =
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(\/([0-9]|[1-2][0-9]|3[0-2]))$/;
return cidrPattern.test(cidr);
}

export function isvalidIpv4OrCidr(input: string): boolean {
return isValidIPV4(input) || isValidCIDR(input);
}

export const getObscured = (str: string, sub = 4, char = "*") => {
const len = str.length - sub;
const obscured = str.substring(0, len).replace(/[a-zA-Z0-9]/g, char);
Expand Down
Loading