Add versioned script library, reconcile permit refusals and compact helper captures
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
CREATE TABLE script_revisions (
|
||||
tenant_id uuid NOT NULL REFERENCES tenants,
|
||||
revision_id uuid NOT NULL DEFAULT gen_random_uuid(),
|
||||
name text NOT NULL CHECK (length(name) BETWEEN 1 AND 120),
|
||||
version integer NOT NULL CHECK (version > 0),
|
||||
interpreter text NOT NULL CHECK (interpreter IN ('powershell','posix_sh','bash')),
|
||||
execution_identity text NOT NULL CHECK (execution_identity IN ('agent_service','root','system')),
|
||||
script_utf8 text NOT NULL CHECK (octet_length(script_utf8) BETWEEN 1 AND 32768),
|
||||
created_by text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (tenant_id, revision_id),
|
||||
UNIQUE (tenant_id, name, version),
|
||||
CHECK (execution_identity = 'agent_service' OR
|
||||
(execution_identity = 'system') = (interpreter = 'powershell'))
|
||||
);
|
||||
|
||||
-- Provision the same editable starting revision for existing and future tenants.
|
||||
CREATE FUNCTION seed_windows_update_script(target_tenant uuid) RETURNS void
|
||||
LANGUAGE sql SET search_path = pg_catalog, public AS $seed$
|
||||
INSERT INTO public.script_revisions(tenant_id, name, version, interpreter, execution_identity, created_by, script_utf8)
|
||||
VALUES (target_tenant, 'Windows-Updates installieren', 1, 'powershell', 'system', 'system:bootstrap', $ps$# Install available software updates from the configured Windows Update source.
|
||||
# See https://learn.microsoft.com/windows/win32/wua_sdk/windows-update-agent-object-model
|
||||
$ErrorActionPreference = 'Stop'
|
||||
$session = New-Object -ComObject Microsoft.Update.Session
|
||||
$session.ClientApplicationID = 'Rust Discovery'
|
||||
$searcher = $session.CreateUpdateSearcher()
|
||||
$search = $searcher.Search("IsInstalled=0 and IsHidden=0 and Type='Software'")
|
||||
if ($search.ResultCode -ne 2) { throw "Update search returned $($search.ResultCode)" }
|
||||
$failed = $false
|
||||
$systemInfo = New-Object -ComObject Microsoft.Update.SystemInfo
|
||||
if ($systemInfo.RebootRequired) {
|
||||
Write-Output '{"reboot_required":true,"status":"restart_pending"}'
|
||||
exit 1
|
||||
}
|
||||
foreach ($update in $search.Updates) {
|
||||
$result = [ordered]@{ id = $update.Identity.UpdateID; title = $update.Title; status = 'pending' }
|
||||
try {
|
||||
if ($update.InstallationBehavior.CanRequestUserInput -or !$update.EulaAccepted) {
|
||||
$result.status = 'operator_input_required'
|
||||
$failed = $true
|
||||
} else {
|
||||
$batch = New-Object -ComObject Microsoft.Update.UpdateColl
|
||||
[void]$batch.Add($update)
|
||||
if (!$update.IsDownloaded) {
|
||||
$downloader = $session.CreateUpdateDownloader()
|
||||
$downloader.Updates = $batch
|
||||
$download = $downloader.Download().GetUpdateResult(0)
|
||||
if ($download.ResultCode -ne 2) { throw "Download HRESULT $($download.HResult)" }
|
||||
}
|
||||
$installer = $session.CreateUpdateInstaller()
|
||||
$installer.AllowSourcePrompts = $false
|
||||
$installer.Updates = $batch
|
||||
$install = $installer.Install().GetUpdateResult(0)
|
||||
$result.status = [int]$install.ResultCode
|
||||
$result.hresult = $install.HResult
|
||||
$result.reboot_required = [bool]$install.RebootRequired
|
||||
if ($install.ResultCode -ne 2) { $failed = $true }
|
||||
}
|
||||
} catch {
|
||||
$result.status = 'failed'
|
||||
$result.error = $_.Exception.Message
|
||||
$failed = $true
|
||||
}
|
||||
Write-Output ($result | ConvertTo-Json -Compress)
|
||||
}
|
||||
Write-Output (@{ found = $search.Updates.Count; reboot_required = [bool]$systemInfo.RebootRequired } | ConvertTo-Json -Compress)
|
||||
if ($failed) { exit 1 }
|
||||
$ps$);
|
||||
$seed$;
|
||||
REVOKE ALL ON FUNCTION seed_windows_update_script(uuid) FROM PUBLIC;
|
||||
SELECT seed_windows_update_script(tenant_id) FROM tenants;
|
||||
CREATE FUNCTION seed_tenant_script_library() RETURNS trigger LANGUAGE plpgsql
|
||||
SET search_path = pg_catalog, public AS $$
|
||||
BEGIN
|
||||
PERFORM public.seed_windows_update_script(NEW.tenant_id);
|
||||
RETURN NEW;
|
||||
END
|
||||
$$;
|
||||
CREATE TRIGGER tenant_script_library AFTER INSERT ON tenants
|
||||
FOR EACH ROW EXECUTE FUNCTION seed_tenant_script_library();
|
||||
|
||||
ALTER TABLE script_revisions ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE script_revisions FORCE ROW LEVEL SECURITY;
|
||||
CREATE POLICY tenant_scope ON script_revisions
|
||||
USING (tenant_id::text = current_setting('app.tenant_id', true))
|
||||
WITH CHECK (tenant_id::text = current_setting('app.tenant_id', true));
|
||||
GRANT SELECT, INSERT ON script_revisions TO rmm_app;
|
||||
CREATE TRIGGER immutable_content BEFORE UPDATE OR DELETE ON script_revisions
|
||||
FOR EACH ROW EXECUTE FUNCTION immutable_plan();
|
||||
Reference in New Issue
Block a user