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

Add nfs server path validation #548

Merged
merged 1 commit into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ import {
validateContainerImage,
validateFingerprint,
validateK8sName,
validateNFSMount,
validatePublicCert,
validateURL,
} from '../../validators/common';
} from '../common';

describe('validator', () => {
// Tests for validateContainerImage
Expand Down Expand Up @@ -147,4 +148,31 @@ BAYTAkFVMRMwEQYDVQQIDApTb21lLVN 0YXRlMSEwHwYDVQQKDBhJ=
expect(validateK8sName('k8sname-')).toBe(false);
});
});

describe('validateNFSMount', () => {
it('should validate correct NFS paths', () => {
const validNFSPaths = [
'10.10.0.10:/backups',
'192.168.0.1:/exports',
'my-nfs-server.com:/exports',
];

validNFSPaths.forEach((nfsPath) => {
expect(validateNFSMount(nfsPath)).toBe(true);
});
});

it('should not validate incorrect NFS paths', () => {
const invalidNFSPaths = [
'10.10.0.10:backups', // missing leading slash in path
'my-nfs-server:/exports', // missing .com or similar in the hostname
'10.10.0.10:', // missing path
'http://10.10.0.10:/backups', // protocol included in the path
];

invalidNFSPaths.forEach((nfsPath) => {
expect(validateNFSMount(nfsPath)).toBe(false);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const URL_REGEX = new RegExp(
`^${PROTOCOL}((${IPV4})|(${HOSTNAME}))((${PORT})(${PATH})?(${QUERY_PARAMS})?)?$`,
);

// validate NFS mount NFS_SERVER:EXPORTED_DIRECTORY
// example: 10.10.0.10:/backups
const NFS_PATH = '(\\/[^ ]*)+';
const NFS_REGEX = new RegExp(`^((${IPV4})|(${HOSTNAME})):(${NFS_PATH})$`);

// validate CA certification.
const CERTIFICATE_HEADER = '-----BEGIN CERTIFICATE-----';
const CERTIFICATE_FOOTER = '-----END CERTIFICATE-----';
Expand Down Expand Up @@ -55,6 +60,10 @@ export function validateURL(url: string) {
return URL_REGEX.test(url);
}

export function validateNFSMount(nfsPath: string) {
return NFS_REGEX.test(nfsPath);
}

export function validatePublicCert(ca: string) {
return CERTIFICATE_REGEX.test(ca);
}
Expand Down
Loading