Skip to content

Commit 21d2ef2

Browse files
Authorize notification before sending it
1 parent f9f08eb commit 21d2ef2

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

notifications/lib/notifications/auth.ex

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ defmodule Notifications.Auth do
1212
authorize(user_id, org_id, "organization.notifications.manage")
1313
end
1414

15-
defp authorize(user_id, org_id, permission) do
16-
req = Request.new(user_id: user_id, org_id: org_id)
15+
def can_view_project?(user_id, org_id, project_id) do
16+
authorize(user_id, org_id, project_id, "project.view")
17+
end
18+
19+
defp authorize(user_id, org_id, permission), do: authorize(user_id, org_id, "", permission)
20+
21+
defp authorize(user_id, org_id, project_id, permission) do
22+
req = Request.new(user_id: user_id, org_id: org_id, project_id: project_id)
1723
endpoint = Application.fetch_env!(:notifications, :rbac_endpoint)
1824
{:ok, channel} = GRPC.Stub.connect(endpoint)
1925

notifications/lib/notifications/workers/coordinator.ex

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ defmodule Notifications.Workers.Coordinator do
5050
organization: organization
5151
}
5252

53-
rules |> Enum.each(fn rule -> process(request_id, rule, data) end)
53+
rules
54+
|> Enum.filter(&authorized?(&1.notification.creator_id, &1.org_id, project.metadata.id))
55+
|> Enum.each(fn rule -> process(request_id, rule, data) end)
5456

5557
Logger.info("#{request_id} #{event.pipeline_id}")
5658
end)
@@ -87,6 +89,15 @@ defmodule Notifications.Workers.Coordinator do
8789
Logger.info("#{request_id} [done]")
8890
end
8991

92+
defp authorized?(_creator_id = nil, _org_id, _project_id), do: true
93+
94+
defp authorized?(creator_id, org_id, project_id) do
95+
case Notifications.Auth.can_view_project?(creator_id, org_id, project_id) do
96+
{:ok, :authorized} -> true
97+
_ -> false
98+
end
99+
end
100+
90101
defp map_result_to_string(enum), do: enum |> Atom.to_string() |> String.downcase()
91102
end
92103
end

notifications/lib/notifications/workers/coordinator/api.ex

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ defmodule Notifications.Workers.Coordinator.Api do
44
alias InternalApi.RepoProxy.DescribeRequest
55

66
req = DescribeRequest.new(hook_id: hook_id)
7-
87
endpoint = Application.fetch_env!(:notifications, :repo_proxy_endpoint)
98
{:ok, channel} = GRPC.Stub.connect(endpoint)
109

@@ -39,7 +38,6 @@ defmodule Notifications.Workers.Coordinator.Api do
3938
alias InternalApi.Plumber.PipelineService.Stub
4039

4140
req = InternalApi.Plumber.DescribeRequest.new(ppl_id: pipeline_id, detailed: true)
42-
4341
endpoint = Application.get_env(:notifications, :pipeline_endpoint)
4442
{:ok, channel} = GRPC.Stub.connect(endpoint)
4543

notifications/lib/notifications/workers/coordinator/filter.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ defmodule Notifications.Workers.Coordinator.Filter do
1010
|> with_pattern([branch, pr_branch], "branch")
1111
|> with_pattern(pipeline, "pipeline")
1212
|> with_pattern(result, "result")
13+
|> preload(:notification)
1314
|> Repo.all()
1415
end
1516

0 commit comments

Comments
 (0)