[Fleet] do not allow empty name for agent policy (#124853)

This commit is contained in:
Nicolas Chaulet 2022-02-07 17:38:10 -05:00 committed by GitHub
parent ca10263ed6
commit 912f4b910f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 2 deletions

View file

@ -206,7 +206,12 @@ export const CreatePackagePolicyPage: React.FunctionComponent = () => {
updatedAgentPolicy: NewAgentPolicy
) => {
if (selectedTab === SelectedPolicyTab.NEW) {
if (!updatedAgentPolicy.name || !updatedAgentPolicy.namespace) {
if (
!updatedAgentPolicy.name ||
updatedAgentPolicy.name.trim() === '' ||
!updatedAgentPolicy.namespace ||
updatedAgentPolicy.namespace.trim() === ''
) {
setHasAgentPolicyError(true);
} else {
setHasAgentPolicyError(false);

View file

@ -11,9 +11,15 @@ import { agentPolicyStatuses, dataTypes } from '../../../common';
import { PackagePolicySchema, NamespaceSchema } from './package_policy';
function validateNonEmptyString(val: string) {
if (val.trim() === '') {
return 'Invalid empty string';
}
}
export const AgentPolicyBaseSchema = {
id: schema.maybe(schema.string()),
name: schema.string({ minLength: 1 }),
name: schema.string({ minLength: 1, validate: validateNonEmptyString }),
namespace: NamespaceSchema,
description: schema.maybe(schema.string()),
is_managed: schema.maybe(schema.boolean()),

View file

@ -101,6 +101,17 @@ export default function (providerContext: FtrProviderContext) {
.expect(400);
});
it('should return a 400 with an empty name', async () => {
await supertest
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send({
name: ' ',
namespace: 'default',
})
.expect(400);
});
it('should return a 400 with an invalid namespace', async () => {
await supertest
.post(`/api/fleet/agent_policies`)