2019-06-14 15:45:05 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
defmodule Pleroma.ConfigDB do
|
2019-06-14 15:45:05 +00:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2020-01-17 08:45:44 +00:00
|
|
|
import Ecto.Query
|
2019-07-10 09:25:58 +00:00
|
|
|
import Pleroma.Web.Gettext
|
2019-06-14 15:45:05 +00:00
|
|
|
alias __MODULE__
|
|
|
|
alias Pleroma.Repo
|
|
|
|
|
|
|
|
@type t :: %__MODULE__{}
|
|
|
|
|
|
|
|
schema "config" do
|
|
|
|
field(:key, :string)
|
2019-06-23 05:16:16 +00:00
|
|
|
field(:group, :string)
|
2019-06-14 15:45:05 +00:00
|
|
|
field(:value, :binary)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2020-01-17 08:45:44 +00:00
|
|
|
@spec get_all_as_keyword() :: keyword()
|
|
|
|
def get_all_as_keyword do
|
|
|
|
ConfigDB
|
|
|
|
|> select([c], {c.group, c.key, c.value})
|
|
|
|
|> Repo.all()
|
|
|
|
|> Enum.reduce([], fn {group, key, value}, acc ->
|
|
|
|
group = ConfigDB.from_string(group)
|
|
|
|
key = ConfigDB.from_string(key)
|
|
|
|
value = from_binary(value)
|
|
|
|
|
|
|
|
Keyword.update(acc, group, [{key, value}], &Keyword.merge(&1, [{key, value}]))
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec get_by_params(map()) :: ConfigDB.t() | nil
|
|
|
|
def get_by_params(params), do: Repo.get_by(ConfigDB, params)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec changeset(ConfigDB.t(), map()) :: Changeset.t()
|
2019-06-14 15:45:05 +00:00
|
|
|
def changeset(config, params \\ %{}) do
|
2019-09-29 08:17:38 +00:00
|
|
|
params = Map.put(params, :value, transform(params[:value]))
|
|
|
|
|
2019-06-14 15:45:05 +00:00
|
|
|
config
|
2019-06-23 05:16:16 +00:00
|
|
|
|> cast(params, [:key, :group, :value])
|
|
|
|
|> validate_required([:key, :group, :value])
|
|
|
|
|> unique_constraint(:key, name: :config_group_key_index)
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
|
2019-06-23 05:16:16 +00:00
|
|
|
def create(params) do
|
2020-01-16 05:50:27 +00:00
|
|
|
%ConfigDB{}
|
2019-09-29 08:17:38 +00:00
|
|
|
|> changeset(params)
|
2019-06-14 15:45:05 +00:00
|
|
|
|> Repo.insert()
|
|
|
|
end
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec update(ConfigDB.t(), map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
|
|
|
|
def update(%ConfigDB{} = config, %{value: value}) do
|
2019-06-14 15:45:05 +00:00
|
|
|
config
|
2019-09-29 08:17:38 +00:00
|
|
|
|> changeset(%{value: value})
|
2019-06-14 15:45:05 +00:00
|
|
|
|> Repo.update()
|
|
|
|
end
|
|
|
|
|
2019-12-06 12:12:56 +00:00
|
|
|
@full_key_update [
|
|
|
|
{:pleroma, :ecto_repos},
|
2020-01-10 15:18:09 +00:00
|
|
|
{:pleroma, :assets},
|
2019-12-06 12:12:56 +00:00
|
|
|
{:quack, :meta},
|
|
|
|
{:mime, :types},
|
|
|
|
{:cors_plug, [:max_age, :methods, :expose, :headers]},
|
|
|
|
{:auto_linker, :opts},
|
2019-12-19 07:19:56 +00:00
|
|
|
{:swarm, :node_blacklist},
|
|
|
|
{:logger, :backends}
|
2019-12-06 12:12:56 +00:00
|
|
|
]
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
defp only_full_update?(%ConfigDB{} = config) do
|
|
|
|
config_group = ConfigDB.from_string(config.group)
|
|
|
|
config_key = ConfigDB.from_string(config.key)
|
2019-12-06 12:12:56 +00:00
|
|
|
|
|
|
|
Enum.any?(@full_key_update, fn
|
|
|
|
{group, key} when is_list(key) ->
|
|
|
|
config_group == group and config_key in key
|
|
|
|
|
|
|
|
{group, key} ->
|
|
|
|
config_group == group and config_key == key
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
defp can_be_partially_updated?(%ConfigDB{} = config), do: not only_full_update?(config)
|
2019-12-06 12:12:56 +00:00
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec update_or_create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
|
2019-06-23 05:16:16 +00:00
|
|
|
def update_or_create(params) do
|
2019-09-29 08:17:38 +00:00
|
|
|
search_opts = Map.take(params, [:group, :key])
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
|
2019-12-06 12:12:56 +00:00
|
|
|
{:partial_update, true, config} <-
|
|
|
|
{:partial_update, can_be_partially_updated?(config), config},
|
|
|
|
old_value <- from_binary(config.value),
|
|
|
|
transformed_value <- do_transform(params[:value]),
|
|
|
|
{:can_be_merged, true, config} <- {:can_be_merged, is_list(transformed_value), config},
|
2020-01-06 11:05:32 +00:00
|
|
|
new_value <- DeepMerge.deep_merge(old_value, transformed_value) do
|
2020-01-16 05:50:27 +00:00
|
|
|
ConfigDB.update(config, %{value: new_value, transformed?: true})
|
2019-06-14 15:45:05 +00:00
|
|
|
else
|
2019-12-06 12:12:56 +00:00
|
|
|
{reason, false, config} when reason in [:partial_update, :can_be_merged] ->
|
2020-01-16 05:50:27 +00:00
|
|
|
ConfigDB.update(config, params)
|
2019-12-06 12:12:56 +00:00
|
|
|
|
|
|
|
nil ->
|
2020-01-16 05:50:27 +00:00
|
|
|
ConfigDB.create(params)
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
@spec delete(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()} | {:ok, nil}
|
2019-06-23 05:16:16 +00:00
|
|
|
def delete(params) do
|
2019-09-29 08:17:38 +00:00
|
|
|
search_opts = Map.delete(params, :subkeys)
|
|
|
|
|
2020-01-16 05:50:27 +00:00
|
|
|
with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
|
2019-09-29 08:17:38 +00:00
|
|
|
{config, sub_keys} when is_list(sub_keys) <- {config, params[:subkeys]},
|
2019-12-06 12:12:56 +00:00
|
|
|
old_value <- from_binary(config.value),
|
2019-09-29 08:17:38 +00:00
|
|
|
keys <- Enum.map(sub_keys, &do_transform_string(&1)),
|
|
|
|
new_value <- Keyword.drop(old_value, keys) do
|
2020-01-16 05:50:27 +00:00
|
|
|
ConfigDB.update(config, %{value: new_value})
|
2019-09-29 08:17:38 +00:00
|
|
|
else
|
|
|
|
{config, nil} ->
|
2019-08-03 18:16:09 +00:00
|
|
|
Repo.delete(config)
|
|
|
|
{:ok, nil}
|
2019-09-29 08:17:38 +00:00
|
|
|
|
2019-07-10 09:25:58 +00:00
|
|
|
nil ->
|
|
|
|
err =
|
|
|
|
dgettext("errors", "Config with params %{params} not found", params: inspect(params))
|
|
|
|
|
|
|
|
{:error, err}
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec from_binary(binary()) :: term()
|
2019-07-11 13:02:13 +00:00
|
|
|
def from_binary(binary), do: :erlang.binary_to_term(binary)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
@spec from_binary_with_convert(binary()) :: any()
|
|
|
|
def from_binary_with_convert(binary) do
|
2019-09-29 08:17:38 +00:00
|
|
|
binary
|
|
|
|
|> from_binary()
|
2019-06-14 15:45:05 +00:00
|
|
|
|> do_convert()
|
|
|
|
end
|
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
@spec from_string(String.t()) :: atom() | no_return()
|
|
|
|
def from_string(":" <> entity), do: String.to_existing_atom(entity)
|
|
|
|
|
|
|
|
def from_string(entity) when is_binary(entity) do
|
|
|
|
if is_module_name?(entity) do
|
|
|
|
String.to_existing_atom("Elixir.#{entity}")
|
|
|
|
else
|
|
|
|
entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-01-17 08:45:44 +00:00
|
|
|
@spec convert(any()) :: any()
|
|
|
|
def convert(entity), do: do_convert(entity)
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_list(entity) do
|
|
|
|
for v <- entity, into: [], do: do_convert(v)
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-08-30 11:04:21 +00:00
|
|
|
defp do_convert(%Regex{} = entity), do: inspect(entity)
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_map(entity) do
|
|
|
|
for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
|
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-12-17 16:51:01 +00:00
|
|
|
defp do_convert({:proxy_url, {type, :localhost, port}}) do
|
|
|
|
%{"tuple" => [":proxy_url", %{"tuple" => [do_convert(type), "localhost", port]}]}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_convert({:proxy_url, {type, host, port}}) when is_tuple(host) do
|
|
|
|
ip =
|
|
|
|
host
|
|
|
|
|> :inet_parse.ntoa()
|
|
|
|
|> to_string()
|
|
|
|
|
|
|
|
%{
|
|
|
|
"tuple" => [
|
|
|
|
":proxy_url",
|
|
|
|
%{"tuple" => [do_convert(type), ip, port]}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_convert({:proxy_url, {type, host, port}}) do
|
|
|
|
%{
|
|
|
|
"tuple" => [
|
|
|
|
":proxy_url",
|
|
|
|
%{"tuple" => [do_convert(type), to_string(host), port]}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
# TODO: will become useless after removing hackney
|
2019-07-23 19:17:00 +00:00
|
|
|
defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_tuple(entity),
|
|
|
|
do: %{"tuple" => do_convert(Tuple.to_list(entity))}
|
2019-06-22 14:30:53 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity),
|
|
|
|
do: entity
|
2019-06-22 06:01:30 +00:00
|
|
|
|
2019-12-25 12:31:51 +00:00
|
|
|
defp do_convert(entity)
|
|
|
|
when is_atom(entity) and entity in [:"tlsv1.1", :"tlsv1.2", :"tlsv1.3"] do
|
|
|
|
":#{to_string(entity)}"
|
|
|
|
end
|
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
defp do_convert(entity) when is_atom(entity), do: inspect(entity)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_convert(entity) when is_binary(entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
@spec transform(any()) :: binary() | no_return()
|
2019-07-11 13:02:13 +00:00
|
|
|
def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
|
2019-09-29 08:17:38 +00:00
|
|
|
entity
|
|
|
|
|> do_transform()
|
2019-12-06 12:12:56 +00:00
|
|
|
|> to_binary()
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2019-12-06 12:12:56 +00:00
|
|
|
def transform(entity), do: to_binary(entity)
|
|
|
|
|
|
|
|
@spec to_binary(any()) :: binary()
|
|
|
|
def to_binary(entity), do: :erlang.term_to_binary(entity)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-08-30 11:04:21 +00:00
|
|
|
defp do_transform(%Regex{} = entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-12-17 16:51:01 +00:00
|
|
|
defp do_transform(%{"tuple" => [":proxy_url", %{"tuple" => [type, host, port]}]}) do
|
|
|
|
{:proxy_url, {do_transform_string(type), parse_host(host), port}}
|
|
|
|
end
|
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
# TODO: will become useless after removing hackney
|
2019-07-23 19:17:00 +00:00
|
|
|
defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
|
2020-01-17 13:28:44 +00:00
|
|
|
{partial_chain, []} =
|
|
|
|
entity
|
|
|
|
|> String.replace(~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
|
|
|
|
|> Code.eval_string()
|
|
|
|
|
2019-07-23 19:17:00 +00:00
|
|
|
{:partial_chain, partial_chain}
|
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(%{"tuple" => entity}) do
|
|
|
|
Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
|
2019-06-22 14:30:53 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_map(entity) do
|
|
|
|
for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_list(entity) do
|
|
|
|
for v <- entity, into: [], do: do_transform(v)
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity) when is_binary(entity) do
|
2019-09-29 08:17:38 +00:00
|
|
|
entity
|
|
|
|
|> String.trim()
|
2019-06-22 14:30:53 +00:00
|
|
|
|> do_transform_string()
|
|
|
|
end
|
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform(entity), do: entity
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-12-17 16:51:01 +00:00
|
|
|
defp parse_host("localhost"), do: :localhost
|
|
|
|
|
|
|
|
defp parse_host(host) do
|
|
|
|
charlist = to_charlist(host)
|
|
|
|
|
|
|
|
case :inet.parse_address(charlist) do
|
|
|
|
{:error, :einval} ->
|
|
|
|
charlist
|
|
|
|
|
|
|
|
{:ok, ip} ->
|
|
|
|
ip
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
@delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
|
|
|
|
|
|
|
|
defp find_valid_delimiter([], _string, _),
|
|
|
|
do: raise(ArgumentError, message: "valid delimiter for Regex expression not found")
|
2019-08-30 11:04:21 +00:00
|
|
|
|
2019-09-29 08:17:38 +00:00
|
|
|
defp find_valid_delimiter([{leading, closing} = delimiter | others], pattern, regex_delimiter)
|
|
|
|
when is_tuple(delimiter) do
|
|
|
|
if String.contains?(pattern, closing) do
|
|
|
|
find_valid_delimiter(others, pattern, regex_delimiter)
|
|
|
|
else
|
|
|
|
{:ok, {leading, closing}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp find_valid_delimiter([delimiter | others], pattern, regex_delimiter) do
|
|
|
|
if String.contains?(pattern, delimiter) do
|
|
|
|
find_valid_delimiter(others, pattern, regex_delimiter)
|
|
|
|
else
|
|
|
|
{:ok, {delimiter, delimiter}}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@regex_parts ~r/^~r(?'delimiter'[\/|"'([{<]{1})(?'pattern'.+)[\/|"')\]}>]{1}(?'modifier'[uismxfU]*)/u
|
|
|
|
|
|
|
|
defp do_transform_string("~r" <> _pattern = regex) do
|
|
|
|
with %{"modifier" => modifier, "pattern" => pattern, "delimiter" => regex_delimiter} <-
|
|
|
|
Regex.named_captures(@regex_parts, regex),
|
|
|
|
{:ok, {leading, closing}} <- find_valid_delimiter(@delimiters, pattern, regex_delimiter),
|
|
|
|
{result, _} <- Code.eval_string("~r#{leading}#{pattern}#{closing}#{modifier}") do
|
|
|
|
result
|
2019-08-30 11:04:21 +00:00
|
|
|
end
|
2019-07-11 13:02:13 +00:00
|
|
|
end
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform_string(":" <> atom), do: String.to_atom(atom)
|
2019-06-14 15:45:05 +00:00
|
|
|
|
2019-07-11 13:02:13 +00:00
|
|
|
defp do_transform_string(value) do
|
2019-09-29 08:17:38 +00:00
|
|
|
if is_module_name?(value) do
|
|
|
|
String.to_existing_atom("Elixir." <> value)
|
|
|
|
else
|
|
|
|
value
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec is_module_name?(String.t()) :: boolean()
|
|
|
|
def is_module_name?(string) do
|
2019-12-21 10:54:22 +00:00
|
|
|
Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack)\./, string) or
|
|
|
|
string in ["Oban", "Ueberauth", "ExSyslogger"]
|
2019-06-14 15:45:05 +00:00
|
|
|
end
|
|
|
|
end
|