logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

release_runtime_provider.ex (2292B)


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