ЛР 4: S3-вложения, доступ и отказоустойчивость — на 3

This commit is contained in:
2026-11-20 18:13:30 +03:00
parent ec540a1a72
commit 1e18644624
34 changed files with 1929 additions and 33 deletions
+52
View File
@@ -0,0 +1,52 @@
$ErrorActionPreference = 'Stop'
$root = Split-Path -Parent $PSScriptRoot
$utf8 = [Text.UTF8Encoding]::new($false)
$secretDir = Join-Path $root 'secrets'
[void][IO.Directory]::CreateDirectory($secretDir)
# Compose bind-mounts these files via Docker Desktop. Keep the host ACL private.
$sid = [Security.Principal.WindowsIdentity]::GetCurrent().User
if (Test-Path -LiteralPath $secretDir -PathType Container) {
$security = [Security.AccessControl.DirectorySecurity]::new()
$security.SetOwner($sid)
$security.SetAccessRuleProtection($true, $false)
$inheritance = [Security.AccessControl.InheritanceFlags]'ContainerInherit, ObjectInherit'
foreach ($principal in @($sid, [Security.Principal.SecurityIdentifier]::new('S-1-5-18'))) {
$rule = [Security.AccessControl.FileSystemAccessRule]::new(
$principal, 'FullControl', $inheritance, 'None', 'Allow'
)
$security.AddAccessRule($rule)
}
Set-Acl -LiteralPath $secretDir -AclObject $security
}
function New-HexSecret([string]$name, [int]$length) {
$file = Join-Path $secretDir $name
if (-not (Test-Path -LiteralPath $file)) {
$bytes = New-Object byte[] $length
$rng = [Security.Cryptography.RandomNumberGenerator]::Create()
try { $rng.GetBytes($bytes) } finally { $rng.Dispose() }
$value = [BitConverter]::ToString($bytes).Replace('-', '').ToLowerInvariant()
[IO.File]::WriteAllText($file, $value, $utf8)
}
}
New-HexSecret 'postgres_password' 32
New-HexSecret 's3_access_key_id' 10
New-HexSecret 's3_secret_access_key' 24
$password = [IO.File]::ReadAllText((Join-Path $secretDir 'postgres_password')).Trim()
if ($password -cnotmatch '^[a-f0-9]{64}$') { throw 'Invalid local PostgreSQL secret format.' }
$url = Join-Path $secretDir 'database_url'
$expected = "postgresql://messenger:${password}@postgres:5432/messenger"
if (-not (Test-Path -LiteralPath $url)) {
[IO.File]::WriteAllText($url, $expected, $utf8)
}
if ([IO.File]::ReadAllText($url).Trim() -cne $expected) {
throw 'Existing database_url and postgres_password disagree.'
}
$envFile = Join-Path $root '.env'
if (-not (Test-Path -LiteralPath $envFile)) {
Copy-Item -LiteralPath (Join-Path $root '.env.example') -Destination $envFile
}
Write-Output 'Lab 4 local configuration ready.'
+18
View File
@@ -0,0 +1,18 @@
#!/bin/sh
# Explicit Compose labels scope the socket to this local test project.
case "${LAB4_PROJECT:-}" in
mtusi-lab4-grade3|mtusi-lab4-grade3-*) ;;
*) echo 'LAB4_PROJECT must identify the Lab 4 test stack' >&2; exit 2 ;;
esac
container() {
ids=$(docker ps -aq --filter "label=com.docker.compose.project=$LAB4_PROJECT" \
--filter "label=com.docker.compose.service=$1" \
--filter 'label=com.docker.compose.oneoff=False')
set -- $ids
if [ "$#" -ne 1 ]; then
echo 'Expected one Lab 4 service container' >&2
return 2
fi
printf '%s\n' "$1"
}
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
. "$(dirname "$0")/lab-containers.sh"
postgres=$(container postgres)
s3=$(container s3)
api1=$(container api1)
api2=$(container api2)
docker restart --time 4 "$postgres" "$s3"
docker restart --time 4 "$api1" "$api2"
+9
View File
@@ -0,0 +1,9 @@
#!/bin/sh
set -eu
case "${TARGET_INSTANCE_ID:-}" in
api1|api2) ;;
*) echo 'TARGET_INSTANCE_ID must be api1 or api2' >&2; exit 2 ;;
esac
. "$(dirname "$0")/lab-containers.sh"
id=$(container "$TARGET_INSTANCE_ID")
docker stop --time 4 "$id"