-
Notifications
You must be signed in to change notification settings - Fork 14
PMM-15138 Fix alerting permissions #1014
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
Open
ademidoff
wants to merge
4
commits into
main
Choose a base branch
from
PMM-15138-fix-alerting-permissions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
e2e_tests/tests/api/alerting/alertingPermissions.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import { APIResponse, expect } from '@playwright/test'; | ||
| import pmmTest from '@fixtures/pmmTest'; | ||
| import apiEndpoints from '@helpers/apiEndpoints'; | ||
| import GrafanaHelper from '@helpers/grafana.helper'; | ||
|
|
||
| // Set by the test, deleted by the afterEach hook so cleanup runs even on failure. | ||
| let viewerId: number | undefined; | ||
| let editorId: number | undefined; | ||
|
|
||
| pmmTest.afterEach(async ({ request }) => { | ||
| const admin = GrafanaHelper.getAuthHeader(); | ||
|
|
||
| if (viewerId) await request.delete(`graph/api/admin/users/${viewerId}`, { headers: admin }); | ||
| if (editorId) await request.delete(`graph/api/admin/users/${editorId}`, { headers: admin }); | ||
| }); | ||
|
|
||
| // PMM-15138: a Viewer may list alert templates but must not create, update or delete | ||
| // templates, nor create rules. An Editor must be allowed to manage them. The auth layer | ||
| // denies a forbidden write with HTTP 403 / code 7 (PermissionDenied) before the request | ||
| // reaches the backend, so the request body is irrelevant for the Viewer assertions. | ||
| pmmTest( | ||
| 'PMM-15138 - Viewer cannot create/update/delete alert templates or rules; Editor can @alerting-api', | ||
| async ({ request }) => { | ||
| const suffix = Date.now(); | ||
| const viewer = { login: `viewer-${suffix}`, password: 'Viewer-pw-12345' }; | ||
| const editor = { login: `editor-${suffix}`, password: 'Editor-pw-12345' }; | ||
| const admin = GrafanaHelper.getAuthHeader(); | ||
|
|
||
| // Created users default to the Viewer org role; the editor is promoted below. | ||
| const createUser = async ({ login, password }: { login: string; password: string }) => { | ||
| const res = await request.post('graph/api/admin/users', { | ||
| data: { login, name: login, OrgId: 1, password }, | ||
| headers: admin, | ||
| }); | ||
|
|
||
| expect(res.status(), `create user ${login}`).toBe(200); | ||
|
|
||
| return (await res.json()).id as number; | ||
| }; | ||
|
|
||
| const templateName = `pmm15138_${suffix}`; | ||
| const templatePath = `${apiEndpoints.alerting.templates}/${templateName}`; | ||
| // Deliberately invalid: the Viewer is rejected by auth first, the Editor by the backend. | ||
| const yamlBody = { yaml: 'placeholder' }; | ||
|
|
||
| // Every write the ticket forbids for a Viewer, parameterised by the caller's auth header. | ||
| const writes = (headers: Record<string, string>): { label: string; send: () => Promise<APIResponse> }[] => [ | ||
| { | ||
| label: 'POST templates', | ||
| send: () => request.post(apiEndpoints.alerting.templates, { data: yamlBody, headers }), | ||
| }, | ||
| { | ||
| label: 'PUT template', | ||
| send: () => request.put(templatePath, { data: { name: templateName, ...yamlBody }, headers }), | ||
| }, | ||
| { | ||
| label: 'DELETE template', | ||
| send: () => request.delete(templatePath, { headers }), | ||
| }, | ||
| { | ||
| label: 'POST rule', | ||
| send: () => request.post(apiEndpoints.alerting.rules, { data: { template_name: templateName }, headers }), | ||
| }, | ||
| ]; | ||
|
|
||
| viewerId = await createUser(viewer); | ||
| editorId = await createUser(editor); | ||
|
|
||
| const roleRes = await request.patch(`graph/api/org/users/${editorId}`, { | ||
| data: { role: 'Editor' }, | ||
| headers: admin, | ||
| }); | ||
|
|
||
| expect(roleRes.status(), 'promote editor').toBe(200); | ||
|
|
||
| const asViewer = GrafanaHelper.getAuthHeader(viewer.login, viewer.password); | ||
| const asEditor = GrafanaHelper.getAuthHeader(editor.login, editor.password); | ||
|
|
||
| await pmmTest.step('Viewer can list alert templates', async () => { | ||
| const res = await request.get(apiEndpoints.alerting.templates, { headers: asViewer }); | ||
|
|
||
| expect(res.status()).toBe(200); | ||
| }); | ||
|
|
||
| await pmmTest.step('Viewer is denied every alerting write with 403', async () => { | ||
| for (const { label, send } of writes(asViewer)) { | ||
| const res = await send(); | ||
|
|
||
| expect(res.status(), `viewer ${label}`).toBe(403); | ||
| // 7 == codes.PermissionDenied | ||
| expect((await res.json()).code, `viewer ${label} code`).toBe(7); | ||
| } | ||
| }); | ||
|
|
||
| await pmmTest.step('Editor is authorized for every alerting write (not 403)', async () => { | ||
| for (const { label, send } of writes(asEditor)) { | ||
| const res = await send(); | ||
|
|
||
| expect(res.status(), `editor ${label}`).not.toBe(403); | ||
| } | ||
| }); | ||
| }, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unused.