Skip to content

Commit

Permalink
[8.x] [ResponseOps][Rules] Allow users to create rules with predefine…
Browse files Browse the repository at this point in the history
…d non random IDs (#199119) (#199259)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[ResponseOps][Rules] Allow users to create rules with predefined non
random IDs (#199119)](#199119)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Christos
Nasikas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-07T08:06:57Z","message":"[ResponseOps][Rules]
Allow users to create rules with predefined non random IDs
(#199119)\n\n## Summary\r\n\r\nThis PR allows users to create rules with
predefined nonrandom IDs. To\r\ntest it, try to create a rule with a
predefined ID like
`POST\r\n/api/alerting/rule/<my_rule_id>`\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/182594\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)","sha":"669761be5e04c958aa8e4a4e3279672a0d508d62","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:ResponseOps","v9.0.0","Feature:Alerting/RulesFramework","backport:prev-minor","v8.17.0"],"title":"[ResponseOps][Rules]
Allow users to create rules with predefined non random
IDs","number":199119,"url":"https://github.com/elastic/kibana/pull/199119","mergeCommit":{"message":"[ResponseOps][Rules]
Allow users to create rules with predefined non random IDs
(#199119)\n\n## Summary\r\n\r\nThis PR allows users to create rules with
predefined nonrandom IDs. To\r\ntest it, try to create a rule with a
predefined ID like
`POST\r\n/api/alerting/rule/<my_rule_id>`\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/182594\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)","sha":"669761be5e04c958aa8e4a4e3279672a0d508d62"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/199119","number":199119,"mergeCommit":{"message":"[ResponseOps][Rules]
Allow users to create rules with predefined non random IDs
(#199119)\n\n## Summary\r\n\r\nThis PR allows users to create rules with
predefined nonrandom IDs. To\r\ntest it, try to create a rule with a
predefined ID like
`POST\r\n/api/alerting/rule/<my_rule_id>`\r\n\r\nFixes:
https://github.com/elastic/kibana/issues/182594\r\n\r\n###
Checklist\r\n\r\nDelete any items that are not applicable to this
PR.\r\n\r\n- [x] [Unit or
functional\r\ntests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)\r\nwere
updated or added to match the most common scenarios\r\n\r\n### For
maintainers\r\n\r\n- [x] This was checked for breaking API changes and
was
[labeled\r\nappropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#_add_your_labels)","sha":"669761be5e04c958aa8e4a4e3279672a0d508d62"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Christos Nasikas <[email protected]>
  • Loading branch information
kibanamachine and cnasikas authored Nov 7, 2024
1 parent 0e2e24c commit 65d84cf
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 65d84cf

Please sign in to comment.