Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove config field usage #2168

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion docs/docs.logflare.com/docs/self-hosting/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ Encryption keys must be Base64 encoded.

Cipher used is AES with a 256-bit key in GCM mode.

If `LOGFLARE_DB_ENCRYPTION_KEY` environement variable is not provided, a default hardcoded encryption key will be used.

### Rolling Encryption Keys

In order to roll encryption keys and migrate existing encrypted data, use the `LOGFLARE_DB_ENCRYPTION_KEY_RETIRED` environment variable.
Expand All @@ -75,7 +77,7 @@ Steps to perform the migration are:
2. Generate a new encryption key and set it to `LOGFLARE_DB_ENCRYPTION_KEY`.
3. Restart or deploy the server with the new environment variables.
4. Upon successful server startup, an `info` log will be emitted that says that an retired encryption key is detected, and the migration will be initiated to transition all data encrypted with the retired key to be encrypted with the new key.
5. Once the migration is complete, the retired encryption key can be safely removed.
5. Once the migration is complete, the retired encryption key can be safely removed. There will be an `info` log that will be emitted once the migration is complete.

## BigQuery Setup

Expand Down
13 changes: 6 additions & 7 deletions lib/logflare/backends/backend.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ defmodule Logflare.Backends.Backend do
field(:description, :string)
field(:token, Ecto.UUID, autogenerate: true)
field(:type, Ecto.Enum, values: Map.keys(@adaptor_mapping))
# TODO(Ziinc): make virtual once cluster is using encrypted fields fully
field(:config, :map)
field(:config, :map, virtual: true)
field(:config_encrypted, Logflare.Ecto.EncryptedMap)
many_to_many(:sources, Source, join_through: "sources_backends")
belongs_to(:user, User)
Expand All @@ -41,17 +40,15 @@ defmodule Logflare.Backends.Backend do
|> cast(attrs, [:type, :config, :user_id, :name, :description, :metadata])
|> validate_required([:user_id, :type, :config, :name])
|> validate_inclusion(:type, Map.keys(@adaptor_mapping))
|> do_config_change()
|> validate_config()
|> do_config_change()
end

# temp function
defp do_config_change(%Ecto.Changeset{changes: %{config: config}} = changeset) do
changeset
|> put_change(:config_encrypted, config)

# TODO(Ziinc): uncomment once cluster is using encrypted fields fully
# |> delete_change(:config)
|> delete_change(:config)
end

defp do_config_change(changeset), do: changeset
Expand Down Expand Up @@ -80,7 +77,9 @@ defmodule Logflare.Backends.Backend do
type = value.type

values =
Map.take(value, [
value
|> Map.put(:config, value.config_encrypted)
|> Map.take([
:name,
:token,
:description,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule Logflare.Repo.Migrations.NilifyConfigColumnForBackendsTable do
use Ecto.Migration
import Ecto.Query
alias Logflare.Ecto.EncryptedMap

def up do
from(b in "backends", update: [set: [config: nil]])
|> Logflare.Repo.update_all([])
end

def down do
{:ok, pid} = Logflare.Vault.start_link()

# copy configs over
Logflare.Repo.all(from b in "backends", select: [:id, :config_encrypted])
|> Enum.each(fn %{id: id} = backend ->
{:ok, config} = EncryptedMap.load(backend.config_encrypted)

from(b in "backends",
where: b.id == ^id,
update: [set: [config: ^config]]
)
|> Logflare.Repo.update_all([])
end)
# stop the vault
Process.unlink(pid)
Process.exit(pid, :kill)
:timer.sleep(100)
end
end
2 changes: 0 additions & 2 deletions test/logflare/backends_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ defmodule Logflare.BackendsTest do
end

describe "encryption" do
# TODO(Ziinc): unskip once cluster is using encrypted fields fully
@tag :skip
test "backend config is encrypted to the :config_encrypted field" do
insert(:backend, config_encrypted: %{some_value: "testing"})

Expand Down
3 changes: 1 addition & 2 deletions test/logflare/vault_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ defmodule Logflare.VaultTest do
defp get_config_encrypted() do
[
%{
# TODO(Ziinc): to uncomment once fully migrated over
# config: nil,
config: nil,
config_encrypted: encrypted_str
}
] = Repo.all(from b in "backends", select: [:config, :config_encrypted])
Expand Down
Loading