From 4dee9f61dadc7f3661566755a63df43592dd70e1 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sat, 1 Jun 2024 10:30:35 -0400 Subject: [PATCH] Simplify table values --- .../nts/key_establishment_response.ex | 10 ++++++++-- lib/chronoscope_web/live/index_live.ex | 1 + lib/chronoscope_web/live/index_live.html.heex | 16 ++++++++++++---- .../nts/key_establishment_response_test.exs | 7 +++++++ 4 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/chronoscope/nts/key_establishment_response.ex b/lib/chronoscope/nts/key_establishment_response.ex index 5a4bff6..5130b07 100644 --- a/lib/chronoscope/nts/key_establishment_response.ex +++ b/lib/chronoscope/nts/key_establishment_response.ex @@ -1,7 +1,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do import Bitwise - @aead_alogorithms %{ + @aead_algorithms %{ 15 => "AEAD_AES_SIV_CMAC_256", 30 => "AEAD_AES_128_GCM_SIV" } @@ -16,6 +16,12 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do 2 => "Internal Server Error" } + def aead_algorithm_to_id(algorithm) do + @aead_algorithms + |> Map.new(fn {k, v} -> {v, k} end) + |> Map.get(algorithm) + end + def parse(response) do parse_response(response, %{}) end @@ -127,7 +133,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do end defp do_parse_aead_algorithm_list([high, low | rest], acc) do - @aead_alogorithms + @aead_algorithms |> Map.get(combine_octets(high, low), "UNKNOWN") |> then(&do_parse_aead_algorithm_list(rest, [&1 | acc])) end diff --git a/lib/chronoscope_web/live/index_live.ex b/lib/chronoscope_web/live/index_live.ex index 995671d..0497db8 100644 --- a/lib/chronoscope_web/live/index_live.ex +++ b/lib/chronoscope_web/live/index_live.ex @@ -2,6 +2,7 @@ defmodule ChronoscopeWeb.IndexLive do use ChronoscopeWeb, :live_view alias Chronoscope.NTS + alias Chronoscope.NTS.KeyEstablishmentResponse @topic "nts-servers" diff --git a/lib/chronoscope_web/live/index_live.html.heex b/lib/chronoscope_web/live/index_live.html.heex index 144a507..319e135 100644 --- a/lib/chronoscope_web/live/index_live.html.heex +++ b/lib/chronoscope_web/live/index_live.html.heex @@ -37,7 +37,13 @@ <%= status %> - <%= Enum.at(response.aead_algorithms, 0) %> + <% aead_algorithm = Enum.at(response.aead_algorithms, 0) %> + + <%= KeyEstablishmentResponse.aead_algorithm_to_id(aead_algorithm) %> + + <%= aead_algorithm %> + + <%= length(response.cookies) %> @@ -46,10 +52,10 @@ <%= response.cookie_length %> - <%= response.cert_expiration %> + <%= response.cert_expiration |> DateTime.from_iso8601 |> then(fn {:ok, dt, _} -> Calendar.strftime(dt, "%Y-%m-%d %H:%M:%SZ") end)%> - <%= server.last_key_establishment %> + <%= server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %> <% else %> @@ -70,7 +76,9 @@ - - <%= server.last_key_establishment %> + <%= + server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") + %> <% end %> diff --git a/test/chronoscope/nts/key_establishment_response_test.exs b/test/chronoscope/nts/key_establishment_response_test.exs index 03b2a3f..53f392a 100644 --- a/test/chronoscope/nts/key_establishment_response_test.exs +++ b/test/chronoscope/nts/key_establishment_response_test.exs @@ -3,6 +3,13 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do import Chronoscope.NTS.KeyEstablishmentResponse + describe "Chronoscope.NTS.KeyEstablishmentResponse.aead_algorithm_to_id()" do + test "maps names to a numeric identifiers" do + assert aead_algorithm_to_id("AEAD_AES_SIV_CMAC_256") == 15 + assert aead_algorithm_to_id("AEAD_AES_128_GCM_SIV") == 30 + end + end + describe "Chronoscope.NTS.KeyEstablishmentResponse.parse()" do test "handles empty response" do assert parse([]) == %{}