Compare commits
1 Commits
auth-daemo
...
debian-vm-
| Author | SHA1 | Date | |
|---|---|---|---|
|
3b25fe54e4
|
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ result-*
|
|||||||
|
|
||||||
# Nix VM
|
# Nix VM
|
||||||
*.qcow2
|
*.qcow2
|
||||||
|
.nixos-test-history
|
||||||
|
|
||||||
# Packaging
|
# Packaging
|
||||||
!/assets/debian/config.toml
|
!/assets/debian/config.toml
|
||||||
|
|||||||
@@ -1,53 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Authorization daemon for Muscl
|
|
||||||
|
|
||||||
[Service]
|
|
||||||
Type=notify
|
|
||||||
ExecStart=/usr/local/bin/muscl_auth_daemon.py
|
|
||||||
|
|
||||||
# WatchdogSec=15
|
|
||||||
|
|
||||||
User=muscl
|
|
||||||
Group=muscl
|
|
||||||
DynamicUser=yes
|
|
||||||
|
|
||||||
; ConfigurationDirectory=muscl
|
|
||||||
; RuntimeDirectory=muscl
|
|
||||||
|
|
||||||
; # This is required to read unix user/group details.
|
|
||||||
; PrivateUsers=false
|
|
||||||
|
|
||||||
; # Needed to communicate with MySQL.
|
|
||||||
; PrivateNetwork=false
|
|
||||||
; PrivateIPC=false
|
|
||||||
|
|
||||||
; AmbientCapabilities=
|
|
||||||
; CapabilityBoundingSet=
|
|
||||||
; DeviceAllow=
|
|
||||||
; DevicePolicy=closed
|
|
||||||
; LockPersonality=true
|
|
||||||
; MemoryDenyWriteExecute=true
|
|
||||||
; NoNewPrivileges=true
|
|
||||||
; PrivateDevices=true
|
|
||||||
; PrivateMounts=true
|
|
||||||
; PrivateTmp=yes
|
|
||||||
; ProcSubset=pid
|
|
||||||
; ProtectClock=true
|
|
||||||
; ProtectControlGroups=strict
|
|
||||||
; ProtectHome=true
|
|
||||||
; ProtectHostname=true
|
|
||||||
; ProtectKernelLogs=true
|
|
||||||
; ProtectKernelModules=true
|
|
||||||
; ProtectKernelTunables=true
|
|
||||||
; ProtectProc=invisible
|
|
||||||
; ProtectSystem=strict
|
|
||||||
; RemoveIPC=true
|
|
||||||
; RestrictAddressFamilies=AF_UNIX AF_INET AF_INET6
|
|
||||||
; RestrictNamespaces=true
|
|
||||||
; RestrictRealtime=true
|
|
||||||
; RestrictSUIDSGID=true
|
|
||||||
; SocketBindDeny=any
|
|
||||||
; SystemCallArchitectures=native
|
|
||||||
; SystemCallFilter=@system-service
|
|
||||||
; SystemCallFilter=~@privileged @resources
|
|
||||||
; UMask=0777
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
[Unit]
|
|
||||||
Description=Authorization daemon for Muscl
|
|
||||||
WantedBy=sockets.target
|
|
||||||
|
|
||||||
[Socket]
|
|
||||||
ListenStream=/run/muscl/muscl-auth-daemon.socket
|
|
||||||
Accept=no
|
|
||||||
SocketMode=0660
|
|
||||||
@@ -1,84 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
|
|
||||||
# TODO: create pool of workers to handle requests concurrently
|
|
||||||
# the socket should be a listener socket and each worker should accept connections from it
|
|
||||||
# the socket should accept requests as newline-separated JSON objects
|
|
||||||
# there should be a watchdog to monitor worker health and restart them if they die
|
|
||||||
# graceful shutdown should be implemented for the workers
|
|
||||||
# optional logging of requests and responses
|
|
||||||
# use systemd notify to signal readiness and amount of connections handled
|
|
||||||
|
|
||||||
import json
|
|
||||||
import os
|
|
||||||
from socket import AF_UNIX, SOCK_DGRAM, SOCK_STREAM, fromfd, socket
|
|
||||||
from multiprocessing import Pool
|
|
||||||
|
|
||||||
|
|
||||||
def get_listener_from_systemd() -> socket:
|
|
||||||
listen_fds = int(os.getenv("LISTEN_FDS", "0"))
|
|
||||||
listen_pid = int(os.getenv("LISTEN_PID", "0"))
|
|
||||||
if listen_fds != 1 or listen_pid != os.getpid():
|
|
||||||
raise RuntimeError("No socket passed from systemd")
|
|
||||||
assert listen_fds == 1
|
|
||||||
sock = fromfd(3, AF_UNIX, SOCK_STREAM)
|
|
||||||
sock.setblocking(False)
|
|
||||||
return sock
|
|
||||||
|
|
||||||
|
|
||||||
def get_notify_socket_from_systemd() -> socket:
|
|
||||||
notify_socket_path = os.getenv("NOTIFY_SOCKET")
|
|
||||||
if not notify_socket_path:
|
|
||||||
raise RuntimeError("No notify socket path found in environment")
|
|
||||||
sock = socket(AF_UNIX, SOCK_DGRAM)
|
|
||||||
sock.connect(notify_socket_path)
|
|
||||||
return sock
|
|
||||||
|
|
||||||
|
|
||||||
def run_auth_daemon(sock: socket):
|
|
||||||
sock.listen()
|
|
||||||
print("Auth daemon is running and listening for connections...")
|
|
||||||
with Pool() as worker_pool:
|
|
||||||
with get_notify_socket_from_systemd() as notify_socket:
|
|
||||||
notify_socket.sendall(b"READY=1\n")
|
|
||||||
while True:
|
|
||||||
conn, _ = sock.accept()
|
|
||||||
worker_pool.apply_async(session_handler, args=(conn,))
|
|
||||||
|
|
||||||
|
|
||||||
def session_handler(sock: socket):
|
|
||||||
buffer = ""
|
|
||||||
while True:
|
|
||||||
data = sock.recv(4096).decode("utf-8")
|
|
||||||
if not data:
|
|
||||||
print("Connection closed by client")
|
|
||||||
break
|
|
||||||
buffer += data
|
|
||||||
if buffer.endswith("\n"):
|
|
||||||
requests = buffer.strip().split("\n")
|
|
||||||
buffer = ""
|
|
||||||
for request in requests:
|
|
||||||
try:
|
|
||||||
req_json = json.loads(request)
|
|
||||||
username = req_json.get("username", "")
|
|
||||||
groups = req_json.get("groups", [])
|
|
||||||
resource_type = req_json.get("resource_type", "")
|
|
||||||
resource = req_json.get("resource", "")
|
|
||||||
allowed = process_request(username, groups, resource_type, resource)
|
|
||||||
response = {"allowed": allowed}
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
response = {"error": "Invalid JSON"}
|
|
||||||
sock.sendall((json.dumps(response) + "\n").encode("utf-8"))
|
|
||||||
|
|
||||||
|
|
||||||
def process_request(
|
|
||||||
username: str,
|
|
||||||
groups: list[str],
|
|
||||||
resource_type: str,
|
|
||||||
resource: str,
|
|
||||||
) -> bool:
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
listener_socket = get_listener_from_systemd()
|
|
||||||
run_auth_daemon(listener_socket)
|
|
||||||
21
flake.lock
generated
21
flake.lock
generated
@@ -15,6 +15,26 @@
|
|||||||
"type": "github"
|
"type": "github"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"nix-vm-test": {
|
||||||
|
"inputs": {
|
||||||
|
"nixpkgs": [
|
||||||
|
"nixpkgs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"locked": {
|
||||||
|
"lastModified": 1763976673,
|
||||||
|
"narHash": "sha256-QPeI8WR+brwodiy4YNfOnLI7rOHJfFPrGm+xT/HmtT4=",
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "nix-vm-test",
|
||||||
|
"rev": "8611bdd7a49750a880be9ee2ea9f68c53f8c9299",
|
||||||
|
"type": "github"
|
||||||
|
},
|
||||||
|
"original": {
|
||||||
|
"owner": "numtide",
|
||||||
|
"repo": "nix-vm-test",
|
||||||
|
"type": "github"
|
||||||
|
}
|
||||||
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1768127708,
|
"lastModified": 1768127708,
|
||||||
@@ -34,6 +54,7 @@
|
|||||||
"root": {
|
"root": {
|
||||||
"inputs": {
|
"inputs": {
|
||||||
"crane": "crane",
|
"crane": "crane",
|
||||||
|
"nix-vm-test": "nix-vm-test",
|
||||||
"nixpkgs": "nixpkgs",
|
"nixpkgs": "nixpkgs",
|
||||||
"rust-overlay": "rust-overlay"
|
"rust-overlay": "rust-overlay"
|
||||||
}
|
}
|
||||||
|
|||||||
10
flake.nix
10
flake.nix
@@ -6,9 +6,12 @@
|
|||||||
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
rust-overlay.inputs.nixpkgs.follows = "nixpkgs";
|
||||||
|
|
||||||
crane.url = "github:ipetkov/crane";
|
crane.url = "github:ipetkov/crane";
|
||||||
|
|
||||||
|
nix-vm-test.url = "github:numtide/nix-vm-test";
|
||||||
|
nix-vm-test.inputs.nixpkgs.follows = "nixpkgs";
|
||||||
};
|
};
|
||||||
|
|
||||||
outputs = { self, nixpkgs, rust-overlay, crane }:
|
outputs = { self, nixpkgs, rust-overlay, crane, nix-vm-test }:
|
||||||
let
|
let
|
||||||
inherit (nixpkgs) lib;
|
inherit (nixpkgs) lib;
|
||||||
|
|
||||||
@@ -95,6 +98,8 @@
|
|||||||
muscl = import ./nix/module.nix;
|
muscl = import ./nix/module.nix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
# vmlib = forAllSystems(system: _: _: nix-vm-test.lib.${system});
|
||||||
|
|
||||||
packages = forAllSystems (system: pkgs: _:
|
packages = forAllSystems (system: pkgs: _:
|
||||||
let
|
let
|
||||||
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
|
||||||
@@ -105,7 +110,6 @@
|
|||||||
fileset = lib.fileset.unions [
|
fileset = lib.fileset.unions [
|
||||||
(craneLib.fileset.commonCargoSources ./.)
|
(craneLib.fileset.commonCargoSources ./.)
|
||||||
./assets
|
./assets
|
||||||
./examples
|
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
in {
|
in {
|
||||||
@@ -131,6 +135,8 @@
|
|||||||
filteredSource = pkgs.runCommandLocal "filtered-source" { } ''
|
filteredSource = pkgs.runCommandLocal "filtered-source" { } ''
|
||||||
ln -s ${src} $out
|
ln -s ${src} $out
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
debianVm = import ./nix/debian-vm-configuration.nix { inherit nix-vm-test nixpkgs system pkgs; };
|
||||||
});
|
});
|
||||||
|
|
||||||
checks = forAllSystems (system: pkgs: _: {
|
checks = forAllSystems (system: pkgs: _: {
|
||||||
|
|||||||
49
nix/debian-vm-configuration.nix
Normal file
49
nix/debian-vm-configuration.nix
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
{ nix-vm-test, nixpkgs, system, pkgs, ... }:
|
||||||
|
let
|
||||||
|
image = nix-vm-test.lib.${system}.debian.images."13";
|
||||||
|
|
||||||
|
generic = import "${nix-vm-test}/generic" { inherit pkgs nixpkgs; inherit (pkgs) lib; };
|
||||||
|
|
||||||
|
makeVmTestForImage =
|
||||||
|
image:
|
||||||
|
{
|
||||||
|
testScript,
|
||||||
|
sharedDirs ? {},
|
||||||
|
diskSize ? null,
|
||||||
|
config ? { }
|
||||||
|
}:
|
||||||
|
generic.makeVmTest {
|
||||||
|
inherit
|
||||||
|
system
|
||||||
|
testScript
|
||||||
|
sharedDirs;
|
||||||
|
image = nix-vm-test.lib.${system}.debian.prepareDebianImage {
|
||||||
|
inherit diskSize;
|
||||||
|
hostPkgs = pkgs;
|
||||||
|
originalImage = image;
|
||||||
|
};
|
||||||
|
machineConfigModule = config;
|
||||||
|
};
|
||||||
|
|
||||||
|
vmTest = makeVmTestForImage image {
|
||||||
|
diskSize = "10G";
|
||||||
|
sharedDirs = {
|
||||||
|
debDir = {
|
||||||
|
source = "${./.}";
|
||||||
|
target = "/mnt";
|
||||||
|
};
|
||||||
|
};
|
||||||
|
testScript = ''
|
||||||
|
vm.wait_for_unit("multi-user.target")
|
||||||
|
vm.succeed("apt-get update && apt-get -y install mariadb-server build-essential curl")
|
||||||
|
vm.succeed("curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y")
|
||||||
|
vm.succeed("source /root/.cargo/env && cargo install cargo-deb")
|
||||||
|
vm.succeed("cp -r /mnt /root/src && chmod -R +w /root/src")
|
||||||
|
vm.succeed("source /root/.cargo/env && cd /root/src && ./create-deb.sh")
|
||||||
|
'';
|
||||||
|
config.nodes.vm = {
|
||||||
|
virtualisation.memorySize = 8192;
|
||||||
|
virtualisation.cpus = 4;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
in vmTest.driverInteractive
|
||||||
@@ -85,9 +85,6 @@ buildFunction ({
|
|||||||
install -Dm644 assets/systemd/muscl.service -t "$out/lib/systemd/system"
|
install -Dm644 assets/systemd/muscl.service -t "$out/lib/systemd/system"
|
||||||
substituteInPlace "$out/lib/systemd/system/muscl.service" \
|
substituteInPlace "$out/lib/systemd/system/muscl.service" \
|
||||||
--replace-fail '/usr/bin/muscl-server' "$out/bin/muscl-server"
|
--replace-fail '/usr/bin/muscl-server' "$out/bin/muscl-server"
|
||||||
|
|
||||||
mkdir -p "$out/share/muscl"
|
|
||||||
cp -r examples "$out/share/muscl"
|
|
||||||
'';
|
'';
|
||||||
|
|
||||||
meta = with lib; {
|
meta = with lib; {
|
||||||
|
|||||||
@@ -27,31 +27,6 @@ in
|
|||||||
}.${level};
|
}.${level};
|
||||||
};
|
};
|
||||||
|
|
||||||
authHandler = lib.mkOption {
|
|
||||||
type = with lib.types; nullOr lines;
|
|
||||||
default = null;
|
|
||||||
description = "Custom authentication handler, written in python";
|
|
||||||
example = ''
|
|
||||||
def process_request(
|
|
||||||
username: str,
|
|
||||||
groups: list[str],
|
|
||||||
resource_type: str,
|
|
||||||
resource: str,
|
|
||||||
) -> bool:
|
|
||||||
if resource_type == "database":
|
|
||||||
if resource.startswith(username) or any(
|
|
||||||
resource.startswith(group) for group in groups
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
elif resource_type == "user":
|
|
||||||
if resource.startswith(username) or any(
|
|
||||||
resource.startswith(group) for group in groups
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
'';
|
|
||||||
};
|
|
||||||
|
|
||||||
settings = lib.mkOption {
|
settings = lib.mkOption {
|
||||||
default = { };
|
default = { };
|
||||||
type = lib.types.submodule {
|
type = lib.types.submodule {
|
||||||
@@ -216,72 +191,5 @@ in
|
|||||||
++ (lib.optionals (cfg.settings.mysql.host != null) [ "AF_INET" "AF_INET6" ]);
|
++ (lib.optionals (cfg.settings.mysql.host != null) [ "AF_INET" "AF_INET6" ]);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
systemd.sockets."muscl-auth-daemon" = lib.mkIf (cfg.authHandler != null) {
|
|
||||||
description = "Authorization daemon for Muscl";
|
|
||||||
wantedBy = [ "sockets.target" ];
|
|
||||||
socketConfig = {
|
|
||||||
ListenStream = "/run/muscl/muscl-auth-daemon.sock";
|
|
||||||
Accept = "no";
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
systemd.services."muscl-auth-daemon" = lib.mkIf (cfg.authHandler != null) {
|
|
||||||
description = "Authorization daemon for Muscl";
|
|
||||||
requires = [ "muscl-auth-daemon.socket" ];
|
|
||||||
serviceConfig = {
|
|
||||||
Type = "notify";
|
|
||||||
ExecStart = let
|
|
||||||
authScript = lib.pipe ../examples/auth_daemon_python/muscl_auth_daemon.py [
|
|
||||||
lib.fileContents
|
|
||||||
(lib.replaceString ''
|
|
||||||
def process_request(
|
|
||||||
username: str,
|
|
||||||
groups: list[str],
|
|
||||||
resource_type: str,
|
|
||||||
resource: str,
|
|
||||||
) -> bool:
|
|
||||||
...
|
|
||||||
'' cfg.authHandler)
|
|
||||||
(pkgs.writers.writePyPy3Bin "muscl-auth-handler.py" { })
|
|
||||||
];
|
|
||||||
in lib.getExe authScript;
|
|
||||||
|
|
||||||
User = "muscl-auth-daemon";
|
|
||||||
Group = "muscl-auth-daemon";
|
|
||||||
DynamicUser = true;
|
|
||||||
|
|
||||||
AmbientCapabilities = [ "" ];
|
|
||||||
CapabilityBoundingSet = [ "" ];
|
|
||||||
DeviceAllow = [ "" ];
|
|
||||||
LockPersonality = true;
|
|
||||||
NoNewPrivileges = true;
|
|
||||||
PrivateDevices = true;
|
|
||||||
PrivateMounts = true;
|
|
||||||
PrivateTmp = "yes";
|
|
||||||
ProcSubset = "pid";
|
|
||||||
ProtectClock = true;
|
|
||||||
ProtectControlGroups = "strict";
|
|
||||||
ProtectHome = true;
|
|
||||||
ProtectHostname = true;
|
|
||||||
ProtectKernelLogs = true;
|
|
||||||
ProtectKernelModules = true;
|
|
||||||
ProtectKernelTunables = true;
|
|
||||||
ProtectProc = "invisible";
|
|
||||||
ProtectSystem = "strict";
|
|
||||||
RemoveIPC = true;
|
|
||||||
UMask = "0777";
|
|
||||||
RestrictNamespaces = true;
|
|
||||||
RestrictRealtime = true;
|
|
||||||
RestrictSUIDSGID = true;
|
|
||||||
SystemCallArchitectures = "native";
|
|
||||||
SocketBindDeny = [ "any" ];
|
|
||||||
SystemCallFilter = [
|
|
||||||
"@system-service"
|
|
||||||
"~@privileged"
|
|
||||||
"~@resources"
|
|
||||||
];
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,25 +56,6 @@ nixpkgs.lib.nixosSystem {
|
|||||||
enable = true;
|
enable = true;
|
||||||
logLevel = "trace";
|
logLevel = "trace";
|
||||||
createLocalDatabaseUser = true;
|
createLocalDatabaseUser = true;
|
||||||
authHandler = ''
|
|
||||||
def process_request(
|
|
||||||
username: str,
|
|
||||||
groups: list[str],
|
|
||||||
resource_type: str,
|
|
||||||
resource: str,
|
|
||||||
) -> bool:
|
|
||||||
if resource_type == "database":
|
|
||||||
if resource.startswith(username) or any(
|
|
||||||
resource.startswith(group) for group in groups
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
elif resource_type == "user":
|
|
||||||
if resource.startswith(username) or any(
|
|
||||||
resource.startswith(group) for group in groups
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
programs.vim = {
|
programs.vim = {
|
||||||
|
|||||||
Reference in New Issue
Block a user