Skip to content

Commit

Permalink
minor cleanups, exclude version from settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
robur-team committed Aug 19, 2024
1 parent 7306abc commit df271e6
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 84 deletions.
7 changes: 2 additions & 5 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,15 @@ function filterUnikernels() {
}
}

function openConfigForm(version, ip, port, certificate, p_key) {
function openConfigForm(ip, port, certificate, p_key) {
const formSection = document.getElementById("config-form");
const configSection = document.getElementById("config-body");
const versionInput = document.getElementById("config-version");
const ipInput = document.getElementById("server-ip");
const portInput = document.getElementById("server-port");
const certificateInput = document.getElementById("certificate");
const pkeyInput = document.getElementById("private-key");
const configBtn = document.getElementById("config-button");
const addConfigBtn = document.getElementById("add-config")
versionInput.value = version;
ipInput.value = ip;
portInput.value = port;
certificateInput.value = certificate;
Expand All @@ -46,7 +44,6 @@ function openConfigForm(version, ip, port, certificate, p_key) {

async function saveConfig() {

const version = document.getElementById("config-version").value;
const ipInput = document.getElementById("server-ip").value;
const portInput = document.getElementById("server-port").value;
const certificateInput = document.getElementById("certificate").value;
Expand All @@ -66,7 +63,7 @@ async function saveConfig() {
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ "version": Number(version), "server_ip": ipInput, "server_port": Number(portInput), "certificate": certificateInput, "private_key": pkeyInput })
body: JSON.stringify({ "server_ip": ipInput, "server_port": Number(portInput), "certificate": certificateInput, "private_key": pkeyInput })
})
const data = await response.json();
if (data.status === 200) {
Expand Down
62 changes: 55 additions & 7 deletions configuration.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ type t = {
private_key : X509.Private_key.t;
server_ip : Ipaddr.t;
server_port : int;
version : int;
updated_at : Ptime.t;
}

Expand All @@ -33,7 +32,6 @@ let empty () =
private_key = key;
server_ip = Ipaddr.(V4 V4.any);
server_port = 1025;
version = 1;
updated_at = Ptime.epoch;
}

Expand All @@ -52,7 +50,55 @@ let to_json t =
("updated_at", `String (Utils.TimeHelper.string_of_ptime t.updated_at));
]

let of_json json now =
let of_json_from_http json now =
match json with
| `Assoc xs -> (
match
( get "certificate" xs,
get "private_key" xs,
get "server_ip" xs,
get "server_port" xs )
with
| ( Some (`String cert),
Some (`String key),
Some (`String server_ip),
Some (`Int server_port) ) ->
let ( let* ) = Result.bind in
let* certificate =
X509.Certificate.decode_pem (Cstruct.of_string cert)
in
let* private_key =
X509.Private_key.decode_pem (Cstruct.of_string key)
in
let* () =
if
not
(Cstruct.equal
(X509.Public_key.fingerprint
(X509.Certificate.public_key certificate))
(X509.Public_key.fingerprint
(X509.Private_key.public private_key)))
then Error (`Msg "certificate and private key do not match")
else Ok ()
in
let* server_ip = Ipaddr.of_string server_ip in

Ok
{
certificate;
private_key;
server_ip;
server_port;
updated_at = now;
}
| _ ->
Error
(`Msg
(Fmt.str "configuration: unexpected types, got %s"
(Yojson.Basic.to_string (`Assoc xs)))))
| _ -> Error (`Msg "configuration: expected a dictionary")

let of_json json =
match json with
| `Assoc xs -> (
match get "version" xs with
Expand All @@ -63,12 +109,14 @@ let of_json json now =
( get "certificate" xs,
get "private_key" xs,
get "server_ip" xs,
get "server_port" xs )
get "server_port" xs,
get "updated_at" xs )
with
| ( Some (`String cert),
Some (`String key),
Some (`String server_ip),
Some (`Int server_port) ) ->
Some (`Int server_port),
Some (`String updated_at) ) ->
let ( let* ) = Result.bind in
let* certificate =
X509.Certificate.decode_pem (Cstruct.of_string cert)
Expand All @@ -88,15 +136,15 @@ let of_json json now =
else Ok ()
in
let* server_ip = Ipaddr.of_string server_ip in
let* updated_at = Utils.TimeHelper.ptime_of_string updated_at in

Ok
{
certificate;
private_key;
server_ip;
server_port;
version = v;
updated_at = now;
updated_at;
}
| _ ->
Error
Expand Down
64 changes: 2 additions & 62 deletions settings_page.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ let settings_layout (configuration : Configuration.t) =
let private_key =
Cstruct.to_string (X509.Private_key.encode_pem configuration.private_key)
in
let version = string_of_int configuration.version in
let last_update = Utils.TimeHelper.string_of_ptime configuration.updated_at in
Tyxml_html.(
section
Expand All @@ -28,7 +27,7 @@ let settings_layout (configuration : Configuration.t) =
button
~a:
[
a_onclick "openConfigForm('','','','','')";
a_onclick "openConfigForm('','','','')";
a_class
[
"inline-flex items-center gap-x-2 text-sm \
Expand All @@ -54,43 +53,6 @@ let settings_layout (configuration : Configuration.t) =
]
[
p ~a:[ a_id "form-alert"; a_class [ "my-4 hidden" ] ] [];
div
~a:[ a_class [ "bg-gray-50 px-4 pb-4 pt-5 sm:p-6 sm:pb-4 my-4" ] ]
[
div
[
label
~a:
[
a_class [ "block text-sm font-medium" ];
a_label_for "version";
]
[ txt "Version*" ];
input
~a:
[
a_autocomplete `Off;
a_input_type `Number;
a_name "config-version";
a_id "config-version";
a_class
[
"ring-primary-100 mt-1.5 transition \
appearance-none block w-full px-3 py-3 \
rounded-xl shadow-sm border \
hover:border-primary-200\n\
\ \
focus:border-primary-300 bg-primary-50 \
bg-opacity-0 hover:bg-opacity-50 \
focus:bg-opacity-50 ring-primary-200 \
focus:ring-primary-200\n\
\ \
focus:ring-[1px] focus:outline-none";
];
]
();
];
];
div
~a:[ a_class [ "bg-gray-50 px-4 pb-4 pt-5 sm:p-6 sm:pb-4 my-4" ] ]
[
Expand Down Expand Up @@ -284,17 +246,6 @@ let settings_layout (configuration : Configuration.t) =
[
tr
[
th
~a:
[
a_class
[
"px-6 py-3 text-start text-xs \
font-bold text-primary-600 \
uppercase";
];
]
[ txt "Version" ];
th
~a:
[
Expand Down Expand Up @@ -366,16 +317,6 @@ let settings_layout (configuration : Configuration.t) =
[
tr
[
td
~a:
[
a_class
[
"px-6 py-4 whitespace-nowrap text-sm \
font-medium text-gray-800";
];
]
[ txt version ];
td
~a:
[
Expand Down Expand Up @@ -440,8 +381,7 @@ let settings_layout (configuration : Configuration.t) =
~a:
[
a_onclick
("openConfigForm('" ^ version
^ "','" ^ ip ^ "','" ^ port ^ "','"
("openConfigForm('" ^ ip ^ "','" ^ port ^ "','"
^ String.escaped certificate ^ "','"
^ String.escaped private_key ^ "')");
a_class
Expand Down
8 changes: 4 additions & 4 deletions storage.ml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ let t_to_json t =
("configuration", Configuration.to_json t.configuration);
]

let t_of_json now json =
let t_of_json json =
match json with
| `Assoc xs -> (
let ( let* ) = Result.bind in
Expand All @@ -38,7 +38,7 @@ let t_of_json now json =
Ok (user :: acc))
(Ok []) users
in
let* configuration = Configuration.of_json configuration now in
let* configuration = Configuration.of_json configuration in
Ok { version = v; users; configuration }
| _ -> Error (`Msg "invalid data: no version and users field"))
| _ -> Error (`Msg "invalid data: not an assoc")
Expand All @@ -55,15 +55,15 @@ module Make (BLOCK : Mirage_block.S) = struct
let write_data (disk, t) =
Stored_data.write disk (Yojson.Basic.to_string (t_to_json t))

let read_data now disk =
let read_data disk =
Stored_data.read disk >|= function
| Ok (Some s) ->
let ( let* ) = Result.bind in
let* json =
try Ok (Yojson.Basic.from_string s)
with Yojson.Json_error msg -> Error (`Msg ("Invalid json: " ^ msg))
in
let* t = t_of_json now json in
let* t = t_of_json json in
Ok (disk, t)
| Ok None ->
Ok
Expand Down
8 changes: 2 additions & 6 deletions unikernel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,7 @@ struct
in
Lwt.return (reply ~content_type:"application/json" res)
| Ok json -> (
match Configuration.of_json json now with
match Configuration.of_json_from_http json now with
| Ok configuration_settings -> (
Store.update_configuration !store configuration_settings
>>= function
Expand Down Expand Up @@ -734,7 +734,7 @@ struct
images assets >>= fun imgs ->
create_html_form assets >>= fun html ->
Store.Stored_data.connect storage >>= fun stored_data ->
Store.read_data (Ptime.v (P.now_d_ps ())) stored_data >>= function
Store.read_data stored_data >>= function
| Error (`Msg msg) -> failwith msg
| Ok data ->
let store = ref data in
Expand All @@ -747,10 +747,6 @@ struct
Logs.info (fun m ->
m "Initialise an HTTP server (no HTTPS) on http://127.0.0.1:%u/"
port);
(* TODO we need a web thingy to edit and upload the albatross configuration:
ip address, port, certificate, private_key
and once updated, we need to (a) dump to the disk (b) update the "albatross" value (and call Albatross.init key)
*)
let request_handler _flow =
request_handler stack albatross js_file css_file imgs html store
in
Expand Down

0 comments on commit df271e6

Please sign in to comment.