Scope enrollment membership migration and use pull-only deployed gateway

This commit is contained in:
2026-11-17 16:03:54 +01:00
parent 7fd17272d5
commit 60f8f21f54
8 changed files with 258 additions and 67 deletions
+33 -9
View File
@@ -8,6 +8,7 @@ import re
import shlex
import subprocess
import sys
import uuid
ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT / "deploy"))
@@ -72,7 +73,6 @@ $dir = Join-Path $root $hash
$dst = Join-Path $dir $name
foreach ($folder in @($root,$dir)) {
if (!(Test-Path -LiteralPath $folder)) {
if ($mode -eq 'probe') { 'missing'; exit 0 }
[void][IO.Directory]::CreateDirectory($folder)
}
$item = Get-Item -LiteralPath $folder -Force
@@ -85,12 +85,15 @@ function Verify-File($file) {
if ($item.PSIsContainer -or ($item.Attributes -band [IO.FileAttributes]::ReparsePoint) -or
(Get-FileHash -LiteralPath $file -Algorithm SHA256).Hash -ne $hash) { throw 'Invalid artifact' }
}
if (Test-Path -LiteralPath $dst) { Verify-File $dst; 'verified'; exit 0 }
$part = Join-Path $dir ('.upload.' + $upload)
if ($mode -eq 'cleanup') { [IO.File]::Delete($part); 'missing'; exit 0 }
if (Test-Path -LiteralPath $dst) {
Verify-File $dst
if ($mode -eq 'stage') { [IO.File]::Delete($part) }
'verified'; exit 0
}
if ($mode -eq 'probe') { 'missing'; exit 0 }
$part = Join-Path $dir ('.upload.' + [Guid]::NewGuid())
$out = [IO.File]::Open($part, 'CreateNew', 'Write', 'None')
try {
try { [Console]::OpenStandardInput().CopyTo($out) } finally { $out.Dispose() }
Verify-File $part
try { [IO.File]::Move($part,$dst) } catch [IO.IOException] { Verify-File $dst }
Verify-File $dst
@@ -99,10 +102,10 @@ try {
'''
def remote_command(target, digest, name, mode):
def remote_command(target, digest, name, mode, upload=""):
if target == "windows":
# name and digest have a restricted alphabet before PowerShell sees them.
script = f"$hash='{digest}'\n$name='{name}'\n$mode='{mode}'\n" + WINDOWS
script = f"$hash='{digest}'\n$name='{name}'\n$mode='{mode}'\n$upload='{upload}'\n" + WINDOWS
encoded = base64.b64encode(script.encode("utf-16le")).decode("ascii")
return "powershell.exe -NoLogo -NoProfile -NonInteractive -EncodedCommand " + encoded
return shlex.join(["sh", "-c", UNIX, "dev-stage", digest, name, mode])
@@ -117,6 +120,23 @@ def transfer(prefix, command, file=None):
return status
def windows_upload(prefix, path, digest):
# OpenSSH for Windows provides an SFTP subsystem for binary transfers.
# Refer to the validated basename so batch commands never contain a local directory name.
upload = uuid.uuid4().hex
remote = f"rmm-dev-stage/{digest}/.upload.{upload}"
sftp = ["sftp", *(part for part in prefix[1:-1] if part != "-T"), "-b", "-", prefix[-1]]
try:
result = subprocess.run(sftp, cwd=path.parent, input=f'put "{path.name}" "{remote}"\n'.encode(),
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=600)
if result.returncode:
raise RuntimeError("Windows-SFTP-Transfer fehlgeschlagen")
return transfer(prefix, remote_command("windows", digest, path.name, "stage", upload))
except (OSError, RuntimeError, subprocess.TimeoutExpired):
transfer(prefix, remote_command("windows", digest, path.name, "cleanup", upload))
raise
def main():
parser = argparse.ArgumentParser(description=__doc__, epilog=(
"Ziel: rmm-dev-stage/<SHA-256>/<Dateiname> im SSH-Benutzerprofil. "
@@ -146,8 +166,12 @@ def main():
if transfer(prefix, remote_command(args.target, digest, path.name, "probe")) == b"verified":
print("vorhanden und geprüft", destination)
continue
file.seek(0)
if transfer(prefix, remote_command(args.target, digest, path.name, "stage"), file) != b"verified":
if args.target == "windows":
status = windows_upload(prefix, path, digest)
else:
file.seek(0)
status = transfer(prefix, remote_command(args.target, digest, path.name, "stage"), file)
if status != b"verified":
raise RuntimeError("Zielhash nicht bestätigt")
print("bereitgestellt und geprüft", destination)