Simplify table values

This commit is contained in:
Mike Cifelli 2024-06-01 10:30:35 -04:00
parent 7e218dfb5d
commit 4dee9f61da
Signed by: mike
GPG Key ID: 6B08C6BE47D08E4C
4 changed files with 28 additions and 6 deletions

View File

@ -1,7 +1,7 @@
defmodule Chronoscope.NTS.KeyEstablishmentResponse do defmodule Chronoscope.NTS.KeyEstablishmentResponse do
import Bitwise import Bitwise
@aead_alogorithms %{ @aead_algorithms %{
15 => "AEAD_AES_SIV_CMAC_256", 15 => "AEAD_AES_SIV_CMAC_256",
30 => "AEAD_AES_128_GCM_SIV" 30 => "AEAD_AES_128_GCM_SIV"
} }
@ -16,6 +16,12 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do
2 => "Internal Server Error" 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 def parse(response) do
parse_response(response, %{}) parse_response(response, %{})
end end
@ -127,7 +133,7 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponse do
end end
defp do_parse_aead_algorithm_list([high, low | rest], acc) do defp do_parse_aead_algorithm_list([high, low | rest], acc) do
@aead_alogorithms @aead_algorithms
|> Map.get(combine_octets(high, low), "UNKNOWN") |> Map.get(combine_octets(high, low), "UNKNOWN")
|> then(&do_parse_aead_algorithm_list(rest, [&1 | acc])) |> then(&do_parse_aead_algorithm_list(rest, [&1 | acc]))
end end

View File

@ -2,6 +2,7 @@ defmodule ChronoscopeWeb.IndexLive do
use ChronoscopeWeb, :live_view use ChronoscopeWeb, :live_view
alias Chronoscope.NTS alias Chronoscope.NTS
alias Chronoscope.NTS.KeyEstablishmentResponse
@topic "nts-servers" @topic "nts-servers"

View File

@ -37,7 +37,13 @@
<%= status %> <%= status %>
</td> </td>
<td class="py-4 px-6 whitespace-nowrap"> <td class="py-4 px-6 whitespace-nowrap">
<%= Enum.at(response.aead_algorithms, 0) %> <% aead_algorithm = Enum.at(response.aead_algorithms, 0) %>
<span class="group relative">
<%= KeyEstablishmentResponse.aead_algorithm_to_id(aead_algorithm) %>
<span class="pointer-events-none absolute -top-9 left-0 w-max p-1 rounded-lg bg-zinc-300 dark:bg-zinc-600 opacity-0 transition-opacity group-hover:opacity-100">
<%= aead_algorithm %>
</span>
</span>
</td> </td>
<td class="py-4 px-6 whitespace-nowrap"> <td class="py-4 px-6 whitespace-nowrap">
<%= length(response.cookies) %> <%= length(response.cookies) %>
@ -46,10 +52,10 @@
<%= response.cookie_length %> <%= response.cookie_length %>
</td> </td>
<td class="py-4 px-6 whitespace-nowrap"> <td class="py-4 px-6 whitespace-nowrap">
<%= response.cert_expiration %> <%= response.cert_expiration |> DateTime.from_iso8601 |> then(fn {:ok, dt, _} -> Calendar.strftime(dt, "%Y-%m-%d %H:%M:%SZ") end)%>
</td> </td>
<td class="py-4 px-6 whitespace-nowrap"> <td class="py-4 px-6 whitespace-nowrap">
<%= server.last_key_establishment %> <%= server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ") %>
</td> </td>
<% else %> <% else %>
@ -70,7 +76,9 @@
<td class="py-4 px-6 whitespace-nowrap"> - </td> <td class="py-4 px-6 whitespace-nowrap"> - </td>
<td class="py-4 px-6 whitespace-nowrap"> <td class="py-4 px-6 whitespace-nowrap">
<%= server.last_key_establishment %> <%=
server.last_key_establishment |> Calendar.strftime("%Y-%m-%d %H:%M:%SZ")
%>
</td> </td>
<% end %> <% end %>
</tr> </tr>

View File

@ -3,6 +3,13 @@ defmodule Chronoscope.NTS.KeyEstablishmentResponseTest do
import Chronoscope.NTS.KeyEstablishmentResponse 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 describe "Chronoscope.NTS.KeyEstablishmentResponse.parse()" do
test "handles empty response" do test "handles empty response" do
assert parse([]) == %{} assert parse([]) == %{}