Skip to content

Commit

Permalink
[ResponseOps][Rules] Allow users to create rules with predefined non …
Browse files Browse the repository at this point in the history
…random IDs (elastic#199119)

## Summary

This PR allows users to create rules with predefined nonrandom IDs. To
test it, try to create a rule with a predefined ID like `POST
/api/alerting/rule/<my_rule_id>`

Fixes: elastic#182594

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)
  • Loading branch information
cnasikas authored Nov 7, 2024
1 parent a4adb56 commit 669761b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
5 changes: 5 additions & 0 deletions x-pack/plugins/alerting/server/saved_objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ export function setupSavedObjects(
// Encrypted attributes
encryptedSavedObjects.registerType({
type: RULE_SAVED_OBJECT_TYPE,
/**
* We disable enforcing random SO IDs for the rule SO
* to allow users creating rules with a predefined ID.
*/
enforceRandomId: false,
attributesToEncrypt: new Set(RuleAttributesToEncrypt),
attributesToIncludeInAAD: new Set(RuleAttributesIncludedInAAD),
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,20 +395,18 @@ export default function createAlertTests({ getService }: FtrProviderContext) {
});
});

it('should not allow providing simple custom ids (non uuid)', async () => {
const customId = '1';
it('should create a rule with a predefined non random ID', async () => {
const ruleId = 'my_id';

const response = await supertest
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule/${customId}`)
.post(`${getUrlPrefix(Spaces.space1.id)}/api/alerting/rule/${ruleId}`)
.set('kbn-xsrf', 'foo')
.send(getTestRuleData());
.send(getTestRuleData())
.expect(200);

expect(response.status).to.eql(400);
expect(response.body).to.eql({
statusCode: 400,
error: 'Bad Request',
message:
'Predefined IDs are not allowed for saved objects with encrypted attributes unless the ID is a UUID.: Bad Request',
});
objectRemover.add(Spaces.space1.id, response.body.id, 'rule', 'alerting');

expect(response.body.id).to.eql(ruleId);
});

it('should return 409 when document with id already exists', async () => {
Expand Down

0 comments on commit 669761b

Please sign in to comment.