Packages

OCI/Docker image metadata validator and resolver for Elixir

Current section

Files

Jump to
regc lib regc oci image config.ex
Raw

lib/regc/oci/image/config.ex

defmodule Regc.Oci.Image.Config do
@moduledoc """
Decoded, digest-verified OCI image configuration.
"""
alias Regc.Oci.{Error, Reference}
alias Regc.Oci.Image.Config.Validator
defstruct [
:created,
:architecture,
:os,
:variant,
:os_version,
:author,
:config,
:rootfs_type,
os_features: [],
rootfs_diff_ids: [],
history: [],
raw: %{}
]
@type t :: %__MODULE__{
created: String.t() | nil,
architecture: String.t(),
os: String.t(),
variant: String.t() | nil,
os_version: String.t() | nil,
os_features: [String.t()],
author: String.t() | nil,
config: map() | nil,
rootfs_type: String.t(),
rootfs_diff_ids: [String.t()],
history: [map()],
raw: map()
}
@spec decode(binary(), Reference.t(), String.t() | nil) ::
{:ok, t()} | {:error, Error.t()}
def decode(bytes, %Reference{} = reference, media_type \\ nil) when is_binary(bytes) do
with {:ok, document} <- decode_json(bytes, reference),
:ok <- Validator.validate(document, media_type, reference) do
{:ok, from_document(document)}
end
end
defp from_document(document) do
rootfs = document["rootfs"]
%__MODULE__{
created: document["created"],
architecture: document["architecture"],
os: document["os"],
variant: document["variant"],
os_version: document["os.version"],
os_features: document["os.features"] || [],
author: document["author"],
config: document["config"],
rootfs_type: rootfs["type"],
rootfs_diff_ids: rootfs["diff_ids"] || [],
history: document["history"] || [],
raw: document
}
end
defp decode_json(bytes, reference) do
case JSON.decode(bytes) do
{:ok, document} when is_map(document) ->
{:ok, document}
{:ok, _other} ->
error("image config JSON must be an object", reference)
{:error, cause} ->
error("image config contains invalid JSON", reference, cause: cause)
end
end
defp error(message, reference, opts \\ []) do
Error.result(
:config_decode,
:invalid_config,
message,
reference: reference,
cause: Keyword.get(opts, :cause)
)
end
end