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

Alerting: Fix deleting rules in a folder with matching UID in another organization #78258

Merged
merged 9 commits into from
Dec 4, 2023

Conversation

papagian
Copy link
Contributor

What is this feature?

The deleteFolderAlertRules() function that is declared inside dashboard service has become obsolete since #67259 (alert rules get deleted here using DeleteInFolder()) but it was still got called.
Unfortunately, this function doesn't take into account the org_id. As a result:

  • trying to delete a folder without alerts with matching folder UID in another organization (with alerts) wrongly fails with folder cannot be deleted: folder contains alert rules
  • deleting a folder using forceDeleteRules=true would wrongly delete alert rules with matching folder (namespace_uid) UID in another organization (with alerts)

Why do we need this feature?

It fixes the above issues by removing the obsolete call.

Who is this feature for?

[Add information on what kind of user the feature is for.]

Which issue(s) does this PR fix?:

Fixes #

Special notes for your reviewer:

Please check that:

  • It works as expected from a user's perspective.
  • If this is a pre-GA feature, it is behind a feature toggle.
  • The docs are updated, and if this is a notable improvement, it's added to our What's New doc.

@papagian papagian requested a review from a team as a code owner November 16, 2023 11:38
@papagian papagian requested review from rwwiv, JacobsonMT, yuri-tceretian and grobinson-grafana and removed request for a team November 16, 2023 11:38
Copy link
Contributor

@grobinson-grafana grobinson-grafana left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM from a code perspective. Do you need help testing this?

return err
}
if alertRulesInFolder > 0 {
return folder.ErrFolderContainsAlertRules.Errorf("")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should Errorf("") have a message here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary because Errorf argument changes the private message and log messages. Whereas the public message gets returned to the user here

resp := JSON(grafanaErr.Reason.Status().HTTPStatus(), grafanaErr.Public())
resp.errMessage = string(grafanaErr.Reason.Status())
resp.err = grafanaErr
and in a few other places.

Anyway, I think we can return just folder.ErrFolderContainsAlertRules because errutil.Base implements the Error interface.

As a side note, I think we should add method Error to the errutil.Base so we can get just a plain simple errutil.Error and avoid controversial Errorf("").

@@ -16,6 +16,7 @@ var ErrDatabaseError = errutil.Internal("folder.database-error")
var ErrInternal = errutil.Internal("folder.internal")
var ErrCircularReference = errutil.BadRequest("folder.circular-reference", errutil.WithPublicMessage("Circular reference detected"))
var ErrTargetRegistrySrvConflict = errutil.Internal("folder.target-registry-srv-conflict")
var ErrFolderContainsAlertRules = errutil.BadRequest("folder.contains-alert-rules", errutil.WithPublicMessage("Folder cannot be deleted: folder contains alert rules"))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would be better to have msgID more generic "folder.non-empty". It's simpler to change the public message than msgId.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in a869362

Copy link
Contributor

@yuri-tceretian yuri-tceretian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good. However, there is one missing piece in this flow: the service should validate that user has permissions to delete alert rules in this folder, i.e. action alert.rules:delete. We can add it to the folder service for now and later move to the service that will be wrapper for the DbStore.

@papagian
Copy link
Contributor Author

I think I have addressed all the review suggestions: you can check once again

Copy link
Contributor

@yuri-tceretian yuri-tceretian left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and everything works fine. The only problem is that the 403 response does not have much info about why user access was denied but I think we can address it later.

LGTM!

@papagian papagian merged commit 6d4625a into main Dec 4, 2023
14 checks passed
@papagian papagian deleted the papagian/fix-folder-delete-remove-obsolete-function branch December 4, 2023 09:34
Copy link
Contributor

The backport to v10.1.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-78258-to-v10.1.x origin/v10.1.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x 6d4625ad52b2e2ad3b2b88e394f69e8fde83c04b

When the conflicts are resolved, stage and commit the changes:

git add . && git cherry-pick --continue

If you have the GitHub CLI installed:

# Push the branch to GitHub:
git push --set-upstream origin backport-78258-to-v10.1.x
# Create the PR body template
PR_BODY=$(gh pr view 78258 --json body --template 'Backport 6d4625ad52b2e2ad3b2b88e394f69e8fde83c04b from #78258{{ "\n\n---\n\n" }}{{ index . "body" }}')
# Create the PR on GitHub
echo "${PR_BODY}" | gh pr create --title "[v10.1.x] Alerting: Fix deleting rules in a folder with matching UID in another organization" --body-file - --label "type/bug" --label "area/backend" --label "add to changelog" --label "backport" --base v10.1.x --milestone 10.1.x --web

Or, if you don't have the GitHub CLI installed (we recommend you install it!):

# Push the branch to GitHub:
git push --set-upstream origin backport-78258-to-v10.1.x

# Create a pull request where the `base` branch is `v10.1.x` and the `compare`/`head` branch is `backport-78258-to-v10.1.x`.

# Remove the local backport branch
git switch main
git branch -D backport-78258-to-v10.1.x

@grafana-delivery-bot grafana-delivery-bot bot added the backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. label Dec 4, 2023
Copy link
Contributor

The backport to v10.2.x failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new branch
git switch --create backport-78258-to-v10.2.x origin/v10.2.x
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x 6d4625ad52b2e2ad3b2b88e394f69e8fde83c04b

When the conflicts are resolved, stage and commit the changes:

git add . && git cherry-pick --continue

If you have the GitHub CLI installed:

# Push the branch to GitHub:
git push --set-upstream origin backport-78258-to-v10.2.x
# Create the PR body template
PR_BODY=$(gh pr view 78258 --json body --template 'Backport 6d4625ad52b2e2ad3b2b88e394f69e8fde83c04b from #78258{{ "\n\n---\n\n" }}{{ index . "body" }}')
# Create the PR on GitHub
echo "${PR_BODY}" | gh pr create --title "[v10.2.x] Alerting: Fix deleting rules in a folder with matching UID in another organization" --body-file - --label "type/bug" --label "area/backend" --label "add to changelog" --label "backport" --base v10.2.x --milestone 10.2.x --web

Or, if you don't have the GitHub CLI installed (we recommend you install it!):

# Push the branch to GitHub:
git push --set-upstream origin backport-78258-to-v10.2.x

# Create a pull request where the `base` branch is `v10.2.x` and the `compare`/`head` branch is `backport-78258-to-v10.2.x`.

# Remove the local backport branch
git switch main
git branch -D backport-78258-to-v10.2.x

papagian added a commit that referenced this pull request Dec 4, 2023
… organization (#78258)

* Remove usage of obsolete function for deleting alert rules under folder

* Apply suggestion from code review

* Update tests

(cherry picked from commit 6d4625a)
papagian added a commit that referenced this pull request Dec 4, 2023
… organization (#78258)

* Remove usage of obsolete function for deleting alert rules under folder

* Apply suggestion from code review

* Update tests

(cherry picked from commit 6d4625a)
papagian added a commit that referenced this pull request Dec 5, 2023
…in another organization (#79011)

Alerting: Fix deleting rules in a folder with matching UID in another organization (#78258)

* Remove usage of obsolete function for deleting alert rules under folder

* Apply suggestion from code review

* Update tests

(cherry picked from commit 6d4625a)
papagian added a commit that referenced this pull request Dec 5, 2023
…in another organization (#79007)

* Alerting: Fix deleting rules in a folder with matching UID in another organization (#78258)

* Remove usage of obsolete function for deleting alert rules under folder

* Apply suggestion from code review

* Update tests

(cherry picked from commit 6d4625a)

* fixup
@aangelisc aangelisc modified the milestones: 10.3.x, 10.2.3 Dec 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
add to changelog area/backend backport v10.1.x backport v10.2.x backport-failed Failed to generate backport PR. Please resolve conflicts and create one manually. type/bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants