1.3 KiB · text History 4869a7f
defmodule GitGud.Packages.SigningKey do
  @moduledoc """
  A repository's keypair for one registry format.

  The public half is served to clients verbatim — it is what a consumer
  pins with `mix hex.repo add --public-key` — and the private half is
  encrypted at rest with the `GitGud.Secrets` master key. Rows keep the
  `key_id` they were wrapped under so a master-key rotation can re-wrap
  them the same way secret rows are.
  """
  use Ecto.Schema

  import Ecto.Changeset

  alias GitGud.Repositories.Repository

  schema "package_signing_keys" do
    field :format, :string
    field :public_key, :string
    field :private_key_ciphertext, :binary, redact: true
    field :private_key_iv, :binary, redact: true
    field :private_key_tag, :binary, redact: true
    field :key_id, :string

    belongs_to :repository, Repository

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(key, attrs) do
    key
    |> cast(attrs, [
      :format,
      :public_key,
      :private_key_ciphertext,
      :private_key_iv,
      :private_key_tag,
      :key_id
    ])
    |> validate_required([
      :format,
      :public_key,
      :private_key_ciphertext,
      :private_key_iv,
      :private_key_tag,
      :key_id
    ])
    |> unique_constraint([:repository_id, :format])
  end
end