Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update policies #57

Merged
merged 71 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
3ba7553
add alpine.js
PizieDust Sep 30, 2024
4ee6373
edit and update policies
PizieDust Sep 30, 2024
b1bb8a3
lint
PizieDust Sep 30, 2024
7e6ea11
add functions work with policies
PizieDust Sep 30, 2024
eb3fa13
update styles
PizieDust Sep 30, 2024
1cfe5ca
Merge branch 'main' of github.com:robur-coop/mollymawk into policy_
PizieDust Sep 30, 2024
f3dd177
use root policy
PizieDust Sep 30, 2024
96ed1cc
get available resources
PizieDust Oct 1, 2024
cb442ff
display available
PizieDust Oct 1, 2024
c6720ef
user existing policy and remain resources
PizieDust Oct 1, 2024
46a5c2e
pass values to the multiselect
PizieDust Oct 1, 2024
8ce420d
update styles
PizieDust Oct 1, 2024
a639bc9
fix minor bugs
PizieDust Oct 1, 2024
8da5473
empty arrays instead of array of empty string
PizieDust Oct 1, 2024
b9fa898
Update albatross.ml
PizieDust Oct 1, 2024
cb4fc27
query albatross properly
PizieDust Oct 1, 2024
3b0bab1
Merge branch 'policy_' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 1, 2024
20a9c1b
Update albatross.ml
PizieDust Oct 1, 2024
46a29fc
Update albatross.ml
PizieDust Oct 1, 2024
a0434c0
Update albatross.ml
PizieDust Oct 1, 2024
3addb82
Update albatross.ml
PizieDust Oct 1, 2024
5310a1c
Update albatross.ml
PizieDust Oct 1, 2024
3c54ec4
Update albatross.ml
PizieDust Oct 1, 2024
da92b25
add comments
PizieDust Oct 1, 2024
c7cf1fe
formatted code
Oct 1, 2024
1c8b252
review changes
PizieDust Oct 1, 2024
b90ef16
Merge branch 'policy_' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 1, 2024
93d7948
move json to albatross_json
PizieDust Oct 1, 2024
f11e196
Merge branch 'main' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 1, 2024
1905f75
review changes
PizieDust Oct 1, 2024
f242766
return policies
PizieDust Oct 1, 2024
3b129c5
check policy is usable
PizieDust Oct 1, 2024
fcca123
set policy
PizieDust Oct 1, 2024
c2e0e4a
add error logs
PizieDust Oct 1, 2024
0819340
Update albatross_json.ml
PizieDust Oct 1, 2024
1fcf02b
check if sub policy is possible
PizieDust Oct 1, 2024
02d21d5
Merge branch 'policy_' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 1, 2024
44dfc61
properly manage root policy errors
PizieDust Oct 1, 2024
195f119
log invalid cpuids
PizieDust Oct 1, 2024
9e0a3fa
uuid bug fix
PizieDust Oct 2, 2024
c8d95c3
Update albatross_json.ml
PizieDust Oct 2, 2024
11818f2
Update albatross_json.ml
PizieDust Oct 2, 2024
197b077
Update albatross_json.ml
PizieDust Oct 2, 2024
360a9ff
review suggestions by @hannesm
PizieDust Oct 2, 2024
e522cfd
Update unikernel.ml
PizieDust Oct 2, 2024
0c49d80
Update unikernel.ml
PizieDust Oct 2, 2024
b715074
Update unikernel.ml
PizieDust Oct 2, 2024
b51fcc9
Update unikernel.ml
PizieDust Oct 2, 2024
d11ef08
Update update_policy.ml
PizieDust Oct 2, 2024
1f89d7d
refactor
PizieDust Oct 2, 2024
11d2200
Update user_single.ml
PizieDust Oct 2, 2024
e363eea
Update assets/main.js
PizieDust Oct 2, 2024
743a0c6
Update update_policy.ml
PizieDust Oct 2, 2024
580de08
Update albatross_json.ml
PizieDust Oct 2, 2024
2ef3bb9
Update assets/main.js
PizieDust Oct 2, 2024
724905b
Update albatross_json.ml
PizieDust Oct 2, 2024
5d954e1
Update albatross_json.ml
PizieDust Oct 2, 2024
ce8aa3c
Update unikernel.ml
PizieDust Oct 2, 2024
dc96fb9
formatted code
Oct 2, 2024
a4129a9
renmae and lint
PizieDust Oct 2, 2024
7fb1cda
Merge branch 'policy_' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 2, 2024
1825516
fix last occurence of 'allowed_vms'
hannesm Oct 2, 2024
2c2f545
computer total available resources correctly
PizieDust Oct 2, 2024
4bfbb90
some styling
PizieDust Oct 2, 2024
aaeeeb5
Merge branch 'policy_' of github.com:robur-coop/mollymawk into policy_
PizieDust Oct 2, 2024
d85a02c
better layout
PizieDust Oct 2, 2024
6f6378d
Update update_policy.ml
PizieDust Oct 2, 2024
434d32a
Update update_policy.ml
PizieDust Oct 2, 2024
f241818
formatted code
Oct 2, 2024
739db37
changes suggested by @hannesm
PizieDust Oct 2, 2024
be9d97d
minor change
PizieDust Oct 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions albatross.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ struct
mutable policies : Vmm_core.Policy.t Vmm_trie.t;
}

let empty_policy =
Vmm_core.Policy.
{
vms = 0;
cpuids = Vmm_core.IS.of_list [];
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
memory = 0;
block = Some 0;
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
bridges = Vmm_core.String_set.of_list [];
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
}

let policy ?domain t =
let ( let* ) = Result.bind in
let* path =
Expand Down Expand Up @@ -38,6 +48,35 @@ struct
in
Ok (Vmm_trie.fold path t.policies (fun name p acc -> (name, p) :: acc) [])

let policy_resource_used () t =
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
let root_policy =
match policy t with
| Ok p -> ( match p with Some p -> p | None -> empty_policy)
| Error _ -> empty_policy
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
in
let policies = match policies t with Ok p -> p | Error _err -> [] in
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
let vms_used, memory_used, storage_used =
List.fold_left
(fun (total_vms, total_memory, total_block) (name, policy) ->
if name <> Vmm_core.Name.root then
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
( total_vms + policy.Vmm_core.Policy.vms,
total_memory + policy.memory,
total_block + match policy.block with Some b -> b | None -> 0 )
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
else (total_vms, total_memory, total_block))
(0, 0, 0) policies
in
Vmm_core.Policy.
{
vms = root_policy.vms - vms_used;
cpuids = root_policy.cpuids;
memory = root_policy.memory - memory_used;
block =
(match root_policy.block with
| Some b -> if b > 0 then Some (b - storage_used) else None
| None -> None);
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
bridges = root_policy.bridges;
}

let key_ids exts pub issuer =
let open X509 in
let auth = (Some (Public_key.id issuer), General_name.empty, None) in
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
72 changes: 72 additions & 0 deletions assets/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,75 @@ async function toggleUserActiveStatus(uuid) {
async function toggleUserAdminStatus(uuid) {
await toggleUserStatus(uuid, "/api/admin/user/admin/toggle");
}


function multiselect(selected, options) {
return {
isOpen: false,
selected: selected,
options: options,
toggleDropdown() {
this.isOpen = !this.isOpen;
},
updateSelection(event, option) {
if (event.target.checked) {
this.selected.push(option);
} else {
this.selected = this.selected.filter(item => item !== option);
}
},
removeItem(index) {
this.selected.splice(index, 1);
}
};
}

async function updatePolicy() {
const vm_count = document.getElementById("f_allowed_vms").innerText;
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
const mem_size = document.getElementById("f_allowed_memory").innerText;
const storage_size = document.getElementById("f_allowed_storage").innerText;
const cpuids = document.getElementById("selectedCPUs").value;
const bridges = document.getElementById("selectedBridges").value;
const formAlert = document.getElementById("form-alert");
const user_id = document.getElementById("user_id").innerText;
const policyButton = document.getElementById("set-policy-btn");
try {
buttonLoading(policyButton, true, "Processing...")
const response = await fetch("/api/admin/u/policy/update", {
method: 'POST',
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(
{
"vms": Number(vm_count),
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
"memory": Number(mem_size),
"block": Number(storage_size),
"cpuids": cpuids,
"bridges": bridges,
"user_uuid": user_id
})
})
const data = await response.json();
if (data.status === 200) {
formAlert.classList.remove("hidden", "text-secondary-500");
formAlert.classList.add("text-primary-500");
formAlert.textContent = "Succesfully updated";
postAlert("bg-primary-300", data.data);
setTimeout(function () {
window.history.back();
}, 2000);
buttonLoading(policyButton, false, "Set Policy")
} else {
formAlert.classList.remove("hidden", "text-primary-500");
formAlert.classList.add("text-secondary-500");
formAlert.textContent = data.data
buttonLoading(policyButton, false, "Set Policy")
}
} catch (error) {
formAlert.classList.remove("hidden", "text-primary-500");
formAlert.classList.add("text-secondary-500");
formAlert.textContent = error
buttonLoading(policyButton, false, "Set Policy")
}
}
2 changes: 1 addition & 1 deletion assets/style.css

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions header_layout.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ let header ?(page_title = "Mollymawk") ~icon () =
~href:"https://unpkg.com/[email protected]/dist/aos.css" ();
script ~a:[ a_src "https://unpkg.com/[email protected]/dist/aos.js" ] (txt "");
link ~rel:[ `Icon ] ~href:icon ();
script
~a:
[
a_defer ();
a_src
"https://cdn.jsdelivr.net/npm/[email protected]/dist/cdn.min.js";
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
]
(txt "");
])
133 changes: 133 additions & 0 deletions unikernel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -779,6 +779,127 @@ struct
~icon:"/images/robur.png" ())
`Not_found)

let edit_policy albatross store uuid reqd (user : User_model.user) =
let users = User_model.create_user_uuid_map (snd store).Storage.users in
match User_model.find_user_by_key uuid users with
| Some u ->
let user_policy =
match Albatross.policy albatross ~domain:u.name with
| Ok p -> (
match p with Some p -> p | None -> Albatross.empty_policy)
| Error _ -> Albatross.empty_policy
in
let policy_avalaible = Albatross.policy_resource_used () albatross in
Lwt.return
(reply reqd ~content_type:"text/html"
(Dashboard.dashboard_layout user
~page_title:(String.capitalize_ascii u.name ^ " | Mollymawk")
~content:
(Update_policy.update_policy_layout u ~user_policy
~root_policy:policy_avalaible)
~icon:"/images/robur.png" ())
`OK)
| None ->
let status =
{
Utils.Status.code = 404;
title = "Error";
data = "Couldn't find account with uuid: " ^ uuid;
success = false;
}
in
Lwt.return
(reply reqd ~content_type:"text/html"
(Guest_layout.guest_layout ~page_title:"404 | Mollymawk"
~content:(Error_page.error_layout status)
~icon:"/images/robur.png" ())
`Not_found)

let update_policy store albatross reqd _user =
decode_request_body reqd >>= fun data ->
let json =
try Ok (Yojson.Basic.from_string data)
with Yojson.Json_error s -> Error (`Msg s)
in
match json with
| Error (`Msg err) ->
Logs.warn (fun m -> m "Failed to parse JSON: %s" err);
http_response reqd ~title:"Error" ~data:(String.escaped err)
`Bad_request
| Ok json -> (
match json with
| `Assoc xs -> (
match
Utils.Json.
( get "vms" xs,
get "memory" xs,
get "block" xs,
get "cpuids" xs,
get "bridges" xs,
get "user_uuid" xs )
with
| ( Some (`Int vms),
Some (`Int memory),
Some (`Int block),
Some (`String cpuids),
Some (`String bridges),
Some (`String user_uuid) ) -> (
PizieDust marked this conversation as resolved.
Show resolved Hide resolved
let users =
User_model.create_user_uuid_map (snd store).Storage.users
in
match User_model.find_user_by_key user_uuid users with
| Some u ->
let policy_data =
{
Vmm_core.Policy.vms;
memory;
block = Some block;
cpuids =
(if cpuids = "" then Vmm_core.IS.empty
hannesm marked this conversation as resolved.
Show resolved Hide resolved
else
Vmm_core.IS.of_list
(List.map int_of_string
hannesm marked this conversation as resolved.
Show resolved Hide resolved
(String.split_on_char ',' cpuids)));
bridges =
(if bridges = "" then Vmm_core.String_set.empty
else
Vmm_core.String_set.of_list
(String.split_on_char ',' bridges));
}
hannesm marked this conversation as resolved.
Show resolved Hide resolved
in
(Albatross.query albatross ~domain:u.name
(`Policy_cmd (`Policy_add policy_data))
>|= function
| Error msg ->
Logs.err (fun m ->
m "error while communicating with albatross: %s"
msg);
[]
| Ok (_hdr, `Success (`Policies policies)) -> policies
| Ok reply ->
Logs.err (fun m ->
m "expected a policy info reply, received %a"
(Vmm_commands.pp_wire ~verbose:false)
reply);
[])
>>= fun _policies ->
http_response reqd ~title:"Success"
~data:"Policy updated succesfully" `OK
| None ->
Logs.warn (fun m ->
m "Failed to find user with uuid: %s" user_uuid);
http_response reqd ~title:"Error" ~data:"User not found"
`Not_found)
| _ ->
http_response reqd ~title:"Error"
~data:
(Fmt.str "policy: unexpected types, got %s"
(Yojson.Basic.to_string (`Assoc xs)))
`Bad_request)
| _ ->
http_response reqd ~title:"Error" ~data:"Expected a dictionary"
`Bad_request)

let request_handler stack albatross js_file css_file imgs store
(_ipaddr, _port) reqd =
Lwt.async (fun () ->
Expand Down Expand Up @@ -855,6 +976,14 @@ struct
let uuid = String.sub path 12 (String.length path - 12) in
authenticate ~check_admin:true !store reqd
(view_user !albatross !store uuid reqd))
| path
when String.(
length path >= 21 && sub path 0 21 = "/admin/u/policy/edit/")
->
check_meth `GET (fun () ->
let uuid = String.sub path 21 (String.length path - 21) in
authenticate ~check_admin:true !store reqd
(edit_policy !albatross !store uuid reqd))
| "/admin/settings" ->
check_meth `GET (fun () ->
authenticate ~check_admin:true !store reqd
Expand All @@ -863,6 +992,10 @@ struct
check_meth `POST (fun () ->
authenticate ~check_admin:true ~api_meth:true !store reqd
(update_settings stack store albatross reqd))
| "/api/admin/u/policy/update" ->
check_meth `POST (fun () ->
authenticate ~check_admin:true ~api_meth:true !store reqd
(update_policy !store !albatross reqd))
| "/api/admin/user/activate/toggle" ->
check_meth `POST (fun () ->
authenticate ~check_admin:true ~api_meth:true !store reqd
Expand Down
Loading
Loading