To ensure sensitive information such as API keys and passwords are not included in plain text in the source code.
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres,
password: 'plaintextpassword',
username: 'admin'
end
This code is vulnerable because the database password is stored in plain text in the source code. Anyone with access to the source code can access the database, posing a serious security risk.
defmodule MyApp.Repo do
use Ecto.Repo,
otp_app: :my_app,
adapter: Ecto.Adapters.Postgres,
password: System.get_env('DB_PASSWORD') |> Cloak.Cipher.decrypt(),
username: System.get_env('DB_USERNAME') |> Cloak.Cipher.decrypt()
end
In the secure code, the database credentials are encrypted and stored as environment variables. The Cloak library is used to decrypt the credentials when they are accessed. This prevents them from being exposed in plain text in the source code.