Add warning for disallowed keys

This commit is contained in:
FloatingGhost 2023-08-15 17:21:05 +01:00
parent 7e1732c654
commit 2500cca3ce
3 changed files with 45 additions and 2 deletions

View file

@ -85,14 +85,20 @@ def allowed_groups, do: @allowed_groups
def enabled, do: Config.get(:configurable_from_database)
# the whitelist check can be called from either the loader or the
# doc generator, which is spitting out strings
defp maybe_stringified_atom_equal(a, b) do
a == inspect(b) || a == b
end
def whitelisted_config?(group, key) do
allowed_groups()
|> Enum.any?(fn
{whitelisted_group} ->
group == inspect(whitelisted_group)
maybe_stringified_atom_equal(group, whitelisted_group)
{whitelisted_group, whitelisted_key} ->
group == inspect(whitelisted_group) && key == inspect(whitelisted_key)
maybe_stringified_atom_equal(group, whitelisted_group) && maybe_stringified_atom_equal(key, whitelisted_key)
end)
end

View file

@ -8,6 +8,7 @@ defmodule Pleroma.Config.TransferTask do
alias Pleroma.Config
alias Pleroma.ConfigDB
alias Pleroma.Repo
alias Pleroma.Config.ConfigurableFromDatabase
require Logger
@ -91,6 +92,17 @@ defp invalid_key_or_group(%ConfigDB{group: :invalid_atom}), do: true
defp invalid_key_or_group(_), do: false
defp merge_with_default(%{group: group, key: key, value: value} = setting) do
if !ConfigurableFromDatabase.whitelisted_config?(setting) do
Logger.warning(~s[
config #{inspect(group)}, #{inspect(key)} is set in the database,
but it is not explicitly allowed to be there. Consider removing it
with
MIX: mix pleroma.config delete #{group} #{key}
OTP: ./bin/pleroma_ctl config delete #{group} #{key}
and setting it in your .exs file instead
])
end
default =
if group == :pleroma do
Config.get([key], Config.Holder.default_config(group, key))

View file

@ -93,6 +93,31 @@ test "transfer config values with full subkey update" do
assert assets_env[:mascots] == [a: 1, b: 2]
end
test "transfer config values that are not explicitly whitelisted" do
insert(:config, key: :whoops, value: [not_allowed: true])
log =
capture_log(fn ->
TransferTask.start_link([])
end)
assert log =~ "config :pleroma, :whoops is set in the database"
assert log =~ "Consider removing it"
assert log =~ "mix pleroma.config delete pleroma whoops"
end
test "transferring whitelisted values should not warn" do
insert(:config, key: :emoji, value: [allowed: true])
insert(:config, key: Pleroma.Workers.PurgeExpiredActivity, value: [allowed: true])
log =
capture_log(fn ->
TransferTask.start_link([])
end)
refute log =~ "Consider removing it"
end
describe "pleroma restart" do
setup do
on_exit(fn ->