Skip to content

Commit

Permalink
fix service queries
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino committed Jan 10, 2025
1 parent 361d4de commit b1ef4ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lib/console/logs/provider/elastic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ defmodule Console.Logs.Provider.Elastic do

defp add_terms(query, %Query{resource: %Cluster{} = cluster}),
do: Map.put(query, :term, term(cluster))
defp add_terms(query, %Query{resource: %Service{cluster: %Cluster{} = cluster}} = svc),
defp add_terms(query, %Query{resource: %Service{cluster: %Cluster{} = cluster} = svc}),
do: Map.put(query, :term, term(cluster) |> term(svc))
defp add_terms(query, _), do: query

Expand Down
10 changes: 6 additions & 4 deletions lib/console/logs/query.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Console.Logs.Query do
alias Console.Repo
alias Console.Logs.Time
alias Console.Schema.{User, Project, Cluster, Service}
alias Console.Deployments.Policies
Expand All @@ -24,23 +25,24 @@ defmodule Console.Logs.Query do
def limit(%__MODULE__{limit: l}) when is_integer(l), do: l
def limit(_), do: @default_limit

def preload(%__MODULE__{resource: %Service{} = svc} = query),
do: %{query | resource: Repo.preload(svc, [:cluster])}
def preload(q), do: q

def accessible(%__MODULE__{project_id: project_id} = q, %User{} = user) when is_binary(project_id),
do: check_access(Project, project_id, user, q)

def accessible(%__MODULE__{cluster_id: id} = q, %User{} = user) when is_binary(id),
do: check_access(Cluster, id, user, q)

def accessible(%__MODULE__{service_id: id} = q, %User{} = user) when is_binary(id),
do: check_access(Service, id, user, q)

def accessible(_, _), do: {:error, "forbidden"}

defp check_access(model, id, user, query) do
Console.Repo.get!(model, id)
|> Policies.allow(user, :read)
|> case do
{:ok, resource} -> {:ok, %{query | resource: resource}}
{:ok, resource} ->
{:ok, preload(%{query | resource: resource})}
err -> err
end
end
Expand Down

0 comments on commit b1ef4ff

Please sign in to comment.