Skip to content

Commit

Permalink
Agent policy names must be unique across Fleet (#80506)
Browse files Browse the repository at this point in the history
  • Loading branch information
John Schulz authored Oct 14, 2020
1 parent 5514eca commit b96638b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 5 additions & 8 deletions x-pack/plugins/ingest_manager/server/services/agent_policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ class AgentPolicyService {

public async requireUniqueName(
soClient: SavedObjectsClientContract,
givenPolicy: { id?: string; name: string; namespace: string }
givenPolicy: { id?: string; name: string }
) {
const results = await soClient.find<AgentPolicySOAttributes>({
type: SAVED_OBJECT_TYPE,
searchFields: ['namespace', 'name'],
search: `${givenPolicy.namespace} + ${escapeSearchQueryPhrase(givenPolicy.name)}`,
searchFields: ['name'],
search: escapeSearchQueryPhrase(givenPolicy.name),
});
const idsWithName = results.total && results.saved_objects.map(({ id }) => id);
if (Array.isArray(idsWithName)) {
Expand All @@ -144,9 +144,7 @@ class AgentPolicyService {
? `Agent Policy '${idsWithName[0]}' already exists`
: `Agent Policies '${idsWithName.join(',')}' already exist`;

throw new AgentPolicyNameExistsError(
`${existClause} in '${givenPolicy.namespace}' namespace with name '${givenPolicy.name}'`
);
throw new AgentPolicyNameExistsError(`${existClause} with name '${givenPolicy.name}'`);
}
}
}
Expand Down Expand Up @@ -236,11 +234,10 @@ class AgentPolicyService {
agentPolicy: Partial<AgentPolicy>,
options?: { user?: AuthenticatedUser }
): Promise<AgentPolicy> {
if (agentPolicy.name && agentPolicy.namespace) {
if (agentPolicy.name) {
await this.requireUniqueName(soClient, {
id,
name: agentPolicy.name,
namespace: agentPolicy.namespace,
});
}
return this._update(soClient, id, agentPolicy, options?.user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,9 @@ export default function ({ getService }: FtrProviderContext) {
.post(`/api/fleet/agent_policies`)
.set('kbn-xsrf', 'xxxx')
.send(sharedBody)
.expect(200);
.expect(409);

expect(body.message).to.match(/already exists?/);
});
});
});
Expand Down

0 comments on commit b96638b

Please sign in to comment.