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 11, 2025
1 parent 361d4de commit dd04c44
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
10 changes: 7 additions & 3 deletions lib/console/logs/provider/elastic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ 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),
do: Map.put(query, :term, term(cluster) |> term(svc))
defp add_terms(query, %Query{resource: %Service{cluster: %Cluster{} = cluster} = svc}) do
Map.put(query, :bool, %{must: [
%{term: %{"kubernetes.namespace" => svc.namespace}},
%{term: %{"cluster.handle" => cluster.handle}}
]})
end
defp add_terms(query, _), do: query

defp add_range(q, %Query{time: %Time{after: aft, before: bef}}) when not is_nil(aft) and not is_nil(bef),
Expand All @@ -79,7 +83,7 @@ defmodule Console.Logs.Provider.Elastic do
defp add_facets(q, _), do: q

defp term(q \\ %{}, resource)
defp term(q, %Cluster{handle: handle}), do: Map.put(q, :"cluster.name", %{value: handle})
defp term(q, %Cluster{handle: handle}), do: Map.put(q, :"cluster.handle", %{value: handle})
defp term(q, %Service{namespace: namespace}), do: Map.put(q, :"kubernetes.namespace", %{value: namespace})

defp facets(resp) do
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 dd04c44

Please sign in to comment.