9ec0f975e8c9b9b6f1e827700f7e13d40bca8e72
[anni] / lib / pleroma / config / release_runtime_provider.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.ReleaseRuntimeProvider do
6   @moduledoc """
7   Imports runtime config and `{env}.exported_from_db.secret.exs` for releases.
8   """
9   @behaviour Config.Provider
10
11   @impl true
12   def init(opts), do: opts
13
14   @impl true
15   def load(config, opts) do
16     with_defaults = Config.Reader.merge(config, Pleroma.Config.Holder.release_defaults())
17
18     config_path =
19       opts[:config_path] || System.get_env("PLEROMA_CONFIG_PATH") || "/etc/pleroma/config.exs"
20
21     with_runtime_config =
22       if File.exists?(config_path) do
23         # <https://git.pleroma.social/pleroma/pleroma/-/issues/3135>
24         %File.Stat{mode: mode} = File.lstat!(config_path)
25
26         if Bitwise.band(mode, 0o007) > 0 do
27           raise "Configuration at #{config_path} has world-permissions, execute the following: chmod o= #{config_path}"
28         end
29
30         if Bitwise.band(mode, 0o020) > 0 do
31           raise "Configuration at #{config_path} has group-wise write permissions, execute the following: chmod g-w #{config_path}"
32         end
33
34         # Note: Elixir doesn't provides a getuid(2)
35         # so cannot forbid group-read only when config is owned by us
36
37         runtime_config = Config.Reader.read!(config_path)
38
39         with_defaults
40         |> Config.Reader.merge(pleroma: [config_path: config_path])
41         |> Config.Reader.merge(runtime_config)
42       else
43         warning = [
44           IO.ANSI.red(),
45           IO.ANSI.bright(),
46           "!!! Config path is not declared! Please ensure it exists and that PLEROMA_CONFIG_PATH is unset or points to an existing file",
47           IO.ANSI.reset()
48         ]
49
50         IO.puts(warning)
51         with_defaults
52       end
53
54     exported_config_path =
55       opts[:exported_config_path] ||
56         config_path
57         |> Path.dirname()
58         |> Path.join("#{Pleroma.Config.get(:env)}.exported_from_db.secret.exs")
59
60     with_exported =
61       if File.exists?(exported_config_path) do
62         exported_config = Config.Reader.read!(exported_config_path)
63         Config.Reader.merge(with_runtime_config, exported_config)
64       else
65         with_runtime_config
66       end
67
68     with_exported
69   end
70 end