Archive and detach managed resources on hosting nodes
This commit is contained in:
1 parent
f1d0d8ff3e
commit
47a4d4e6cb
13 files changed
+512
-12
No files matched your search
@@ -5,6 +5,7 @@ from dataclasses import dataclass
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .node_requests import serve_apply_request
|
from .node_requests import serve_apply_request
|
||||||
|
from .node_retirement import RetirementRequest, parse_retirement
|
||||||
from .service_access import ServiceAccessRequest, parse_service_request
|
from .service_access import ServiceAccessRequest, parse_service_request
|
||||||
|
|
||||||
MANIFEST_VERSION = 1
|
MANIFEST_VERSION = 1
|
||||||
@@ -102,9 +103,24 @@ def main(argv=None):
|
|||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
if args.command == "apply-request":
|
if args.command == "apply-request":
|
||||||
from .database_node_apply import apply_database, set_database_access
|
from .database_node_apply import (
|
||||||
|
apply_database,
|
||||||
|
retire_database,
|
||||||
|
set_database_access,
|
||||||
|
)
|
||||||
|
|
||||||
|
def parse(data):
|
||||||
|
return parse_retirement(
|
||||||
|
data,
|
||||||
|
DatabaseSpec.from_manifest,
|
||||||
|
lambda values: parse_service_request(
|
||||||
|
values, DatabaseSpec.from_manifest
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
def apply(request):
|
def apply(request):
|
||||||
|
if isinstance(request, RetirementRequest):
|
||||||
|
return retire_database(request, DEFAULT_SOCKET)
|
||||||
if isinstance(request, ServiceAccessRequest):
|
if isinstance(request, ServiceAccessRequest):
|
||||||
return set_database_access(
|
return set_database_access(
|
||||||
request.resource, DEFAULT_SOCKET, request.suspended
|
request.resource, DEFAULT_SOCKET, request.suspended
|
||||||
@@ -113,9 +129,7 @@ def main(argv=None):
|
|||||||
|
|
||||||
return serve_apply_request(
|
return serve_apply_request(
|
||||||
MANIFEST_VERSION,
|
MANIFEST_VERSION,
|
||||||
lambda data: parse_service_request(
|
parse,
|
||||||
data, DatabaseSpec.from_manifest
|
|
||||||
),
|
|
||||||
apply,
|
apply,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ from .credential_rotation import (
|
|||||||
valid_generation,
|
valid_generation,
|
||||||
)
|
)
|
||||||
from .node_files import read_root_file, require_root_directory, write_new_file
|
from .node_files import read_root_file, require_root_directory, write_new_file
|
||||||
|
from .node_retirement import RetirementRecord
|
||||||
|
|
||||||
STATE_DIRECTORY = Path("/var/lib/coulobre/database-node")
|
STATE_DIRECTORY = Path("/var/lib/coulobre/database-node")
|
||||||
MANAGED_COMMENT_PREFIX = "Coulobre managed database "
|
MANAGED_COMMENT_PREFIX = "Coulobre managed database "
|
||||||
@@ -212,6 +213,10 @@ def check_restore_complete(record_path):
|
|||||||
|
|
||||||
|
|
||||||
def install_database(cursor, database, socket, record_path):
|
def install_database(cursor, database, socket, record_path):
|
||||||
|
if os.path.lexists(record_path.with_suffix(".retired")):
|
||||||
|
raise ValueError(
|
||||||
|
"This database is retained for a retired hosting account."
|
||||||
|
)
|
||||||
if os.path.lexists(rotation_path(record_path)):
|
if os.path.lexists(rotation_path(record_path)):
|
||||||
raise ValueError(ROTATION_IN_PROGRESS)
|
raise ValueError(ROTATION_IN_PROGRESS)
|
||||||
check_restore_complete(record_path)
|
check_restore_complete(record_path)
|
||||||
@@ -395,6 +400,12 @@ def set_database_access(database, socket, suspended):
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Apply the database manifest before changing its access."
|
"Apply the database manifest before changing its access."
|
||||||
)
|
)
|
||||||
|
if not suspended and os.path.lexists(
|
||||||
|
record_path.with_suffix(".retired")
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"This database is retained for a retired account."
|
||||||
|
)
|
||||||
checkpoint = load_access_checkpoint(record_path, record)
|
checkpoint = load_access_checkpoint(record_path, record)
|
||||||
marker = record_path.with_suffix(".suspended")
|
marker = record_path.with_suffix(".suspended")
|
||||||
if suspended and checkpoint is None:
|
if suspended and checkpoint is None:
|
||||||
@@ -424,3 +435,28 @@ def set_database_access(database, socket, suspended):
|
|||||||
)
|
)
|
||||||
if not suspended:
|
if not suspended:
|
||||||
marker.unlink()
|
marker.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def retire_database(request, socket):
|
||||||
|
database = request.resource
|
||||||
|
socket = socket.resolve()
|
||||||
|
record_path = STATE_DIRECTORY / f"{database.name}.json"
|
||||||
|
with locked_cursor(database, socket) as (driver, cursor):
|
||||||
|
retirement = RetirementRecord(STATE_DIRECTORY, request)
|
||||||
|
if retirement.is_complete:
|
||||||
|
return
|
||||||
|
record = checked_credentials(
|
||||||
|
driver, cursor, database, socket, record_path, verify_login=False
|
||||||
|
)
|
||||||
|
if record is None or not login_is_locked(cursor, database.name):
|
||||||
|
raise ValueError(
|
||||||
|
"Suspend the managed database login before retirement."
|
||||||
|
)
|
||||||
|
retirement.begin()
|
||||||
|
cursor.execute("KILL USER %s", (database.name,))
|
||||||
|
marker = record_path.with_suffix(".retired")
|
||||||
|
if not os.path.lexists(marker):
|
||||||
|
write_new_file(marker, retirement.document, mode=0o600)
|
||||||
|
elif read_root_file(marker, mode=0o600) != retirement.document:
|
||||||
|
raise ValueError("The database has a different retirement record.")
|
||||||
|
retirement.finish()
|
||||||
@@ -10,6 +10,7 @@ import idna
|
|||||||
|
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
from .node_requests import serve_apply_request
|
from .node_requests import serve_apply_request
|
||||||
|
from .node_retirement import RetirementRequest, parse_retirement
|
||||||
|
|
||||||
BASE_MANIFEST_VERSION = 1
|
BASE_MANIFEST_VERSION = 1
|
||||||
MAIL_RECORD_MANIFEST_VERSION = 2
|
MAIL_RECORD_MANIFEST_VERSION = 2
|
||||||
@@ -336,14 +337,27 @@ def main(argv=None):
|
|||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
|
|
||||||
if args.command == "apply-request":
|
if args.command == "apply-request":
|
||||||
from .dns_node_apply import apply_zone
|
from .dns_node_apply import apply_zone, retire_zone
|
||||||
|
|
||||||
|
def apply(request):
|
||||||
|
if isinstance(request, RetirementRequest):
|
||||||
|
retire_zone(request)
|
||||||
|
else:
|
||||||
|
apply_zone(request)
|
||||||
|
|
||||||
|
def identity(request):
|
||||||
|
if isinstance(request, RetirementRequest):
|
||||||
|
return request.identity()
|
||||||
|
return request.manifest_identity()
|
||||||
|
|
||||||
return serve_apply_request(
|
return serve_apply_request(
|
||||||
BASE_MANIFEST_VERSION,
|
BASE_MANIFEST_VERSION,
|
||||||
DnsZone.from_manifest,
|
lambda data: parse_retirement(
|
||||||
apply_zone,
|
data, DnsZone.from_manifest, DnsZone.from_manifest
|
||||||
|
),
|
||||||
|
apply,
|
||||||
limit=DNS_REQUEST_LIMIT,
|
limit=DNS_REQUEST_LIMIT,
|
||||||
identity=DnsZone.manifest_identity,
|
identity=identity,
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -8,7 +8,13 @@ from pathlib import Path
|
|||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
|
|
||||||
from .dns_node import MAX_SOA_SERIAL
|
from .dns_node import MAX_SOA_SERIAL
|
||||||
from .node_files import read_root_file, require_root_directory, write_new_file
|
from .node_files import (
|
||||||
|
locked_root_directory,
|
||||||
|
read_root_file,
|
||||||
|
require_root_directory,
|
||||||
|
write_new_file,
|
||||||
|
)
|
||||||
|
from .node_retirement import RetirementRecord, archive_path
|
||||||
|
|
||||||
ZONE_DIRECTORY = Path("/etc/bind/coulobre")
|
ZONE_DIRECTORY = Path("/etc/bind/coulobre")
|
||||||
RNDC = ("rndc", "-s", "127.0.0.1")
|
RNDC = ("rndc", "-s", "127.0.0.1")
|
||||||
@@ -30,7 +36,7 @@ def apply_zone(zone):
|
|||||||
|
|
||||||
def install_zone(zone, path, directory):
|
def install_zone(zone, path, directory):
|
||||||
domain = zone.domain
|
domain = zone.domain
|
||||||
config = f'{{ type primary; file "{path}"; allow-update {{ "none"; }}; }};'
|
config = zone_configuration(path)
|
||||||
registered = check_registration(domain, config)
|
registered = check_registration(domain, config)
|
||||||
wanted = zone_file(zone)
|
wanted = zone_file(zone)
|
||||||
previous = (
|
previous = (
|
||||||
@@ -78,6 +84,33 @@ def install_zone(zone, path, directory):
|
|||||||
wait_for_serial(domain, zone.serial)
|
wait_for_serial(domain, zone.serial)
|
||||||
|
|
||||||
|
|
||||||
|
def zone_configuration(path):
|
||||||
|
return f'{{ type primary; file "{path}"; allow-update {{ "none"; }}; }};'
|
||||||
|
|
||||||
|
|
||||||
|
def retire_zone(request):
|
||||||
|
zone = request.resource
|
||||||
|
with locked_root_directory(ZONE_DIRECTORY):
|
||||||
|
record = RetirementRecord(ZONE_DIRECTORY, request)
|
||||||
|
if record.is_complete:
|
||||||
|
return
|
||||||
|
path = ZONE_DIRECTORY / zone.domain
|
||||||
|
managed = check_registration(zone.domain, zone_configuration(path))
|
||||||
|
if os.path.lexists(path):
|
||||||
|
contents = read_root_file(path, mode=0o644)
|
||||||
|
if contents != zone_file(zone):
|
||||||
|
raise ValueError("The managed zone changed since the preview.")
|
||||||
|
elif managed or not record.matches(record.started):
|
||||||
|
raise ValueError("The managed zone file is missing.")
|
||||||
|
record.begin()
|
||||||
|
if managed:
|
||||||
|
run(*RNDC, "delzone", zone.domain)
|
||||||
|
if check_registration(zone.domain, zone_configuration(path)):
|
||||||
|
raise ValueError("BIND9 still has the zone registered.")
|
||||||
|
archive_path(path, record.directory / "zone")
|
||||||
|
record.finish()
|
||||||
|
|
||||||
|
|
||||||
def check_registration(domain, config):
|
def check_registration(domain, config):
|
||||||
missing = (
|
missing = (
|
||||||
"rndc: 'showzone' failed: not found\n"
|
"rndc: 'showzone' failed: not found\n"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ from pathlib import Path
|
|||||||
|
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
from .node_requests import serve_apply_request
|
from .node_requests import serve_apply_request
|
||||||
|
from .node_retirement import RetirementRequest, parse_retirement
|
||||||
from .service_access import ServiceAccessRequest, parse_service_request
|
from .service_access import ServiceAccessRequest, parse_service_request
|
||||||
|
|
||||||
MANIFEST_VERSION = 1
|
MANIFEST_VERSION = 1
|
||||||
@@ -102,6 +103,12 @@ class MailboxQuotaUpdate:
|
|||||||
|
|
||||||
|
|
||||||
def parse_mailbox_request(data):
|
def parse_mailbox_request(data):
|
||||||
|
return parse_retirement(
|
||||||
|
data, MailboxSpec.from_manifest, parse_mailbox_apply
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_mailbox_apply(data):
|
||||||
if (
|
if (
|
||||||
isinstance(data, dict)
|
isinstance(data, dict)
|
||||||
and data.keys() == {"operation", "mailbox"}
|
and data.keys() == {"operation", "mailbox"}
|
||||||
@@ -112,7 +119,11 @@ def parse_mailbox_request(data):
|
|||||||
|
|
||||||
|
|
||||||
def apply_mailbox_request(request):
|
def apply_mailbox_request(request):
|
||||||
if isinstance(request, ServiceAccessRequest):
|
if isinstance(request, RetirementRequest):
|
||||||
|
from .mail_node_retirement import retire_mailbox
|
||||||
|
|
||||||
|
retire_mailbox(request)
|
||||||
|
elif isinstance(request, ServiceAccessRequest):
|
||||||
from .mail_node_quota import set_mailbox_access
|
from .mail_node_quota import set_mailbox_access
|
||||||
|
|
||||||
set_mailbox_access(request.resource, request.suspended)
|
set_mailbox_access(request.resource, request.suspended)
|
||||||
|
|||||||
@@ -82,6 +82,10 @@ def credential_path(mailbox):
|
|||||||
return STATE_DIRECTORY / f"{key}.json"
|
return STATE_DIRECTORY / f"{key}.json"
|
||||||
|
|
||||||
|
|
||||||
|
def retirement_guard(mailbox):
|
||||||
|
return credential_path(mailbox).with_suffix(".retiring")
|
||||||
|
|
||||||
|
|
||||||
def mailbox_home(mailbox):
|
def mailbox_home(mailbox):
|
||||||
return MAIL_ROOT / mailbox.domain / mailbox.local_part
|
return MAIL_ROOT / mailbox.domain / mailbox.local_part
|
||||||
|
|
||||||
@@ -161,6 +165,8 @@ def mailbox_status(mailbox):
|
|||||||
|
|
||||||
|
|
||||||
def configured_mail_account(mailbox):
|
def configured_mail_account(mailbox):
|
||||||
|
if os.path.lexists(retirement_guard(mailbox)):
|
||||||
|
raise ValueError("This mailbox is being retired.")
|
||||||
for directory in (POSTFIX_DOMAINS.parent, DOVECOT_USERS.parent):
|
for directory in (POSTFIX_DOMAINS.parent, DOVECOT_USERS.parent):
|
||||||
require_root_directory(directory)
|
require_root_directory(directory)
|
||||||
account, auth_group, modern = check_services(mailbox)
|
account, auth_group, modern = check_services(mailbox)
|
||||||
@@ -248,6 +254,8 @@ def mailbox_can_authenticate(record):
|
|||||||
|
|
||||||
|
|
||||||
def install_mailbox(mailbox, state_directory):
|
def install_mailbox(mailbox, state_directory):
|
||||||
|
if os.path.lexists(retirement_guard(mailbox)):
|
||||||
|
raise ValueError("This mailbox is being retired.")
|
||||||
account, auth_group, modern = check_services(mailbox)
|
account, auth_group, modern = check_services(mailbox)
|
||||||
records = [
|
records = [
|
||||||
load_credentials(path)
|
load_credentials(path)
|
||||||
@@ -308,7 +316,16 @@ def install_mailbox(mailbox, state_directory):
|
|||||||
else:
|
else:
|
||||||
ensure_mail_home(home, account, create=False)
|
ensure_mail_home(home, account, create=False)
|
||||||
|
|
||||||
tables = lookup_tables(prepared, account, auth_group, modern=modern)
|
tables = lookup_tables(
|
||||||
|
[
|
||||||
|
record
|
||||||
|
for record in prepared
|
||||||
|
if not os.path.lexists(retirement_guard(record.mailbox))
|
||||||
|
],
|
||||||
|
account,
|
||||||
|
auth_group,
|
||||||
|
modern=modern,
|
||||||
|
)
|
||||||
publish_table(DOVECOT_USERS, tables[DOVECOT_USERS], ready[DOVECOT_USERS])
|
publish_table(DOVECOT_USERS, tables[DOVECOT_USERS], ready[DOVECOT_USERS])
|
||||||
wait_for_dovecot_user(mailbox, home, modern)
|
wait_for_dovecot_user(mailbox, home, modern)
|
||||||
for path in (POSTFIX_MAILBOXES, POSTFIX_DOMAINS):
|
for path in (POSTFIX_MAILBOXES, POSTFIX_DOMAINS):
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ def update_mailbox(mailbox, *, suspended=None):
|
|||||||
path = mail.credential_path(mailbox)
|
path = mail.credential_path(mailbox)
|
||||||
if state_directory is None or not os.path.lexists(path):
|
if state_directory is None or not os.path.lexists(path):
|
||||||
raise ValueError("Apply the mailbox manifest before updating it.")
|
raise ValueError("Apply the mailbox manifest before updating it.")
|
||||||
|
if os.path.lexists(mail.retirement_guard(mailbox)):
|
||||||
|
raise ValueError("This mailbox is being retired.")
|
||||||
if os.path.lexists(rotation_path(path)):
|
if os.path.lexists(rotation_path(path)):
|
||||||
raise ValueError(ROTATION_IN_PROGRESS)
|
raise ValueError(ROTATION_IN_PROGRESS)
|
||||||
original = mail.load_credentials(path)
|
original = mail.load_credentials(path)
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
|
from . import mail_node_apply as mail
|
||||||
|
from .credential_rotation import rotation_path
|
||||||
|
from .node_files import read_root_file, require_root_directory, write_new_file
|
||||||
|
from .node_retirement import (
|
||||||
|
RetirementRecord,
|
||||||
|
archive_data_directory,
|
||||||
|
archive_path,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def retire_mailbox(request):
|
||||||
|
mailbox = request.resource
|
||||||
|
path = mail.credential_path(mailbox)
|
||||||
|
with mail.locked_state(create=False, exclusive=True) as state:
|
||||||
|
if state is None:
|
||||||
|
raise ValueError("The managed mailbox registry is missing.")
|
||||||
|
retirement = RetirementRecord(mail.STATE_DIRECTORY, request)
|
||||||
|
guard = mail.retirement_guard(mailbox)
|
||||||
|
if retirement.is_complete:
|
||||||
|
clear_guard(guard, retirement)
|
||||||
|
return
|
||||||
|
if os.path.lexists(rotation_path(path)):
|
||||||
|
raise ValueError(
|
||||||
|
"Finish the mailbox password change before retirement."
|
||||||
|
)
|
||||||
|
account, auth_group, modern = mail.check_services(mailbox)
|
||||||
|
started = retirement.matches(retirement.started)
|
||||||
|
home = mail.mailbox_home(mailbox)
|
||||||
|
if os.path.lexists(home):
|
||||||
|
for directory in (mail.MAIL_ROOT, home.parent):
|
||||||
|
mail.ensure_mail_directory(directory, create=False)
|
||||||
|
mail.ensure_mail_home(home, account, create=False)
|
||||||
|
elif not (
|
||||||
|
started
|
||||||
|
and (mail.MAIL_ROOT / ".retired" / request.operation_id).is_dir()
|
||||||
|
):
|
||||||
|
raise ValueError("The managed mailbox data directory is missing.")
|
||||||
|
if os.path.lexists(path):
|
||||||
|
record = mail.load_credentials(path)
|
||||||
|
mail.check_registered_manifest(record, mailbox)
|
||||||
|
if not record.suspended or (
|
||||||
|
not started and not mail.home_is_prepared(record)
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"Suspend the managed mailbox before retiring it."
|
||||||
|
)
|
||||||
|
elif (
|
||||||
|
not started or not (retirement.directory / "credentials").is_file()
|
||||||
|
):
|
||||||
|
raise ValueError("The managed mailbox credentials are missing.")
|
||||||
|
|
||||||
|
retirement.begin()
|
||||||
|
if not os.path.lexists(guard):
|
||||||
|
write_new_file(guard, retirement.document, mode=0o600)
|
||||||
|
os.fsync(state)
|
||||||
|
elif read_root_file(guard, mode=0o600) != retirement.document:
|
||||||
|
raise ValueError(
|
||||||
|
"A different job is already retiring this mailbox."
|
||||||
|
)
|
||||||
|
|
||||||
|
remove_delivery(mailbox, account, auth_group, modern)
|
||||||
|
archive_data_directory(
|
||||||
|
mail.mailbox_home(mailbox), mail.MAIL_ROOT, request.operation_id
|
||||||
|
)
|
||||||
|
archive_path(
|
||||||
|
path.with_suffix(".home-ready"),
|
||||||
|
retirement.directory / "home-ready",
|
||||||
|
)
|
||||||
|
archive_path(path, retirement.directory / "credentials")
|
||||||
|
retirement.finish()
|
||||||
|
clear_guard(guard, retirement)
|
||||||
|
os.fsync(state)
|
||||||
|
|
||||||
|
|
||||||
|
def clear_guard(guard, retirement):
|
||||||
|
if (
|
||||||
|
os.path.lexists(guard)
|
||||||
|
and read_root_file(guard, mode=0o600) == retirement.document
|
||||||
|
):
|
||||||
|
guard.unlink()
|
||||||
|
|
||||||
|
|
||||||
|
def remove_delivery(mailbox, account, auth_group, modern):
|
||||||
|
records = []
|
||||||
|
for path in mail.STATE_DIRECTORY.glob("*.json"):
|
||||||
|
record = mail.load_credentials(path)
|
||||||
|
if record.mailbox.address == mailbox.address or mail.home_is_prepared(
|
||||||
|
record
|
||||||
|
):
|
||||||
|
records.append(
|
||||||
|
mail.published_mailbox_credentials(
|
||||||
|
record, account, auth_group, modern
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
original = mail.lookup_tables(records, account, auth_group, modern=modern)
|
||||||
|
current = mail.lookup_tables(
|
||||||
|
[
|
||||||
|
record
|
||||||
|
for record in records
|
||||||
|
if not os.path.lexists(mail.retirement_guard(record.mailbox))
|
||||||
|
],
|
||||||
|
account,
|
||||||
|
auth_group,
|
||||||
|
modern=modern,
|
||||||
|
)
|
||||||
|
for path in original:
|
||||||
|
require_root_directory(path.parent)
|
||||||
|
observed = {
|
||||||
|
path: mail.check_lookup_table(path, table)
|
||||||
|
for path, table in original.items()
|
||||||
|
}
|
||||||
|
for path, table in current.items():
|
||||||
|
mail.publish_table(path, table, observed[path])
|
||||||
|
mail.run("postfix", "reload")
|
||||||
|
mail.run("doveadm", "auth", "cache", "flush", mailbox.address)
|
||||||
|
for _ in range(20):
|
||||||
|
if (
|
||||||
|
mail.dovecot_home(mailbox) is None
|
||||||
|
and mail.postfix_lookup(mailbox.address, mail.POSTFIX_MAILBOXES)
|
||||||
|
is None
|
||||||
|
):
|
||||||
|
mail.run("doveadm", "kick", mailbox.address)
|
||||||
|
return
|
||||||
|
time.sleep(0.25)
|
||||||
|
raise ValueError("The mail node still resolves the retired recipient.")
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import os
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from . import mail_node_apply as mail
|
from . import mail_node_apply as mail
|
||||||
@@ -17,6 +18,8 @@ def rotate_mailbox_credentials(request):
|
|||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Apply the mailbox manifest before rotating its login."
|
"Apply the mailbox manifest before rotating its login."
|
||||||
)
|
)
|
||||||
|
if os.path.lexists(mail.retirement_guard(mailbox)):
|
||||||
|
raise ValueError("This mailbox is being retired.")
|
||||||
original = mail.load_credentials(path)
|
original = mail.load_credentials(path)
|
||||||
mail.check_registered_manifest(original, mailbox)
|
mail.check_registered_manifest(original, mailbox)
|
||||||
if original.suspended:
|
if original.suspended:
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
import json
|
||||||
|
import os
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from .node_files import read_root_file, require_root_directory, write_new_file
|
||||||
|
|
||||||
|
OPERATION = "retire_resource"
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class RetirementRequest:
|
||||||
|
resource: object
|
||||||
|
operation_id: str
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
try:
|
||||||
|
if str(UUID(self.operation_id)) == self.operation_id:
|
||||||
|
return
|
||||||
|
except (AttributeError, TypeError, ValueError):
|
||||||
|
pass
|
||||||
|
raise ValueError("Expected a valid retirement operation ID.")
|
||||||
|
|
||||||
|
def identity(self):
|
||||||
|
return {"operation": OPERATION, "operation_id": self.operation_id}
|
||||||
|
|
||||||
|
def manifest(self):
|
||||||
|
return {
|
||||||
|
"operation": OPERATION,
|
||||||
|
"resource": self.resource.manifest(),
|
||||||
|
"operation_id": self.operation_id,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def parse_retirement(data, parse_resource, fallback):
|
||||||
|
if not isinstance(data, dict) or data.get("operation") != OPERATION:
|
||||||
|
return fallback(data)
|
||||||
|
if data.keys() != {"operation", "resource", "operation_id"}:
|
||||||
|
raise ValueError("Expected a retirement request.")
|
||||||
|
return RetirementRequest(
|
||||||
|
parse_resource(data["resource"]), data["operation_id"]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RetirementRecord:
|
||||||
|
def __init__(self, base, request):
|
||||||
|
self.base = base / ".retired"
|
||||||
|
self.directory = self.base / request.operation_id
|
||||||
|
self.document = json.dumps(request.manifest(), sort_keys=True) + "\n"
|
||||||
|
self.started = self.directory / "request"
|
||||||
|
self.completed = self.directory / "complete"
|
||||||
|
|
||||||
|
def matches(self, marker):
|
||||||
|
if not os.path.lexists(marker):
|
||||||
|
return False
|
||||||
|
require_root_directory(self.directory)
|
||||||
|
if read_root_file(marker, mode=0o600) != self.document:
|
||||||
|
raise ValueError("The retirement record has a different identity.")
|
||||||
|
return True
|
||||||
|
|
||||||
|
@property
|
||||||
|
def is_complete(self):
|
||||||
|
return self.matches(self.completed)
|
||||||
|
|
||||||
|
def begin(self):
|
||||||
|
require_root_directory(self.base, create_mode=0o700)
|
||||||
|
require_root_directory(self.directory, create_mode=0o700)
|
||||||
|
if not self.matches(self.started):
|
||||||
|
self.write(self.started)
|
||||||
|
sync_directory(self.base)
|
||||||
|
sync_directory(self.base.parent)
|
||||||
|
|
||||||
|
def finish(self):
|
||||||
|
self.write(self.completed)
|
||||||
|
|
||||||
|
def write(self, path):
|
||||||
|
write_new_file(path, self.document, mode=0o600)
|
||||||
|
sync_directory(self.directory)
|
||||||
|
|
||||||
|
|
||||||
|
def sync_directory(path):
|
||||||
|
fd = os.open(path, os.O_RDONLY | os.O_DIRECTORY)
|
||||||
|
try:
|
||||||
|
os.fsync(fd)
|
||||||
|
finally:
|
||||||
|
os.close(fd)
|
||||||
|
|
||||||
|
|
||||||
|
def archive_path(source, destination):
|
||||||
|
if not os.path.lexists(source):
|
||||||
|
return
|
||||||
|
if os.path.lexists(destination):
|
||||||
|
raise ValueError("The retirement destination already contains data.")
|
||||||
|
os.rename(source, destination)
|
||||||
|
sync_directory(source.parent)
|
||||||
|
sync_directory(destination.parent)
|
||||||
|
|
||||||
|
|
||||||
|
def archive_data_directory(source, root, operation_id):
|
||||||
|
if not os.path.lexists(source):
|
||||||
|
return
|
||||||
|
base = root / ".retired"
|
||||||
|
require_root_directory(base, create_mode=0o700)
|
||||||
|
info = source.lstat()
|
||||||
|
if (
|
||||||
|
not source.is_dir()
|
||||||
|
or source.is_symlink()
|
||||||
|
or info.st_dev != base.stat().st_dev
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"Move the managed data to the retirement filesystem first."
|
||||||
|
)
|
||||||
|
# A rename avoids traversing user-controlled files and links.
|
||||||
|
archive_path(source, base / operation_id)
|
||||||
|
sync_directory(base.parent)
|
||||||
@@ -10,6 +10,7 @@ from string import Template
|
|||||||
from .directadmin import DOCUMENT_ROOTS
|
from .directadmin import DOCUMENT_ROOTS
|
||||||
from .domain_names import canonical_domain
|
from .domain_names import canonical_domain
|
||||||
from .node_requests import serve_apply_request
|
from .node_requests import serve_apply_request
|
||||||
|
from .node_retirement import RetirementRequest, parse_retirement
|
||||||
from .php_pool_settings import PhpPoolSettings
|
from .php_pool_settings import PhpPoolSettings
|
||||||
from .service_access import ServiceAccessRequest, parse_service_request
|
from .service_access import ServiceAccessRequest, parse_service_request
|
||||||
|
|
||||||
@@ -215,6 +216,10 @@ class WebPhpRequest:
|
|||||||
|
|
||||||
|
|
||||||
def parse_web_request(data):
|
def parse_web_request(data):
|
||||||
|
return parse_retirement(data, WebSite.from_manifest, parse_web_apply)
|
||||||
|
|
||||||
|
|
||||||
|
def parse_web_apply(data):
|
||||||
if (
|
if (
|
||||||
isinstance(data, dict)
|
isinstance(data, dict)
|
||||||
and data.keys() == {"operation", "site"}
|
and data.keys() == {"operation", "site"}
|
||||||
@@ -247,6 +252,12 @@ def parse_web_request(data):
|
|||||||
|
|
||||||
|
|
||||||
def apply_web_request(request):
|
def apply_web_request(request):
|
||||||
|
if isinstance(request, RetirementRequest):
|
||||||
|
from .web_node_retirement import retire_site
|
||||||
|
|
||||||
|
retire_site(request)
|
||||||
|
return None
|
||||||
|
|
||||||
if isinstance(request, ServiceAccessRequest):
|
if isinstance(request, ServiceAccessRequest):
|
||||||
from .web_node_access import set_website_access
|
from .web_node_access import set_website_access
|
||||||
|
|
||||||
|
|||||||
@@ -189,6 +189,8 @@ def check_services():
|
|||||||
|
|
||||||
def reserve_account(site, user, paths):
|
def reserve_account(site, user, paths):
|
||||||
marker = STATE_DIRECTORY / f"{site.account_id}.json"
|
marker = STATE_DIRECTORY / f"{site.account_id}.json"
|
||||||
|
if os.path.lexists(marker.with_suffix(".retired")):
|
||||||
|
raise ValueError("This website belongs to a retired hosting account.")
|
||||||
manifest = site.manifest()
|
manifest = site.manifest()
|
||||||
for existing in STATE_DIRECTORY.glob("*.json"):
|
for existing in STATE_DIRECTORY.glob("*.json"):
|
||||||
saved = json.loads(read_root_file(existing))
|
saved = json.loads(read_root_file(existing))
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
import fcntl
|
||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from . import web_node_apply as web
|
||||||
|
from .node_files import (
|
||||||
|
check_root_file,
|
||||||
|
read_root_file,
|
||||||
|
require_root_directory,
|
||||||
|
write_new_file,
|
||||||
|
)
|
||||||
|
from .node_retirement import (
|
||||||
|
RetirementRecord,
|
||||||
|
archive_data_directory,
|
||||||
|
archive_path,
|
||||||
|
)
|
||||||
|
from .web_node_aliases import alias_directory, disable_alias_vhosts, read_alias
|
||||||
|
from .web_node_php import read_managed_php_pool
|
||||||
|
from .web_node_sftp import AUTHORIZED_KEYS
|
||||||
|
from .web_node_tls import (
|
||||||
|
delete_alias_certificate,
|
||||||
|
https_paths,
|
||||||
|
render_https_configuration,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def retire_site(request):
|
||||||
|
site = request.resource
|
||||||
|
paths = web.SitePaths.for_site(site)
|
||||||
|
with web.locked_web_node(fcntl.LOCK_EX):
|
||||||
|
retirement = RetirementRecord(web.STATE_DIRECTORY, request)
|
||||||
|
if retirement.is_complete:
|
||||||
|
return
|
||||||
|
reservation = web.STATE_DIRECTORY / f"{site.account_id}.json"
|
||||||
|
started = retirement.matches(retirement.started)
|
||||||
|
if os.path.lexists(reservation) or not started:
|
||||||
|
web.check_reservation(site)
|
||||||
|
elif not (retirement.directory / "site").is_file():
|
||||||
|
raise ValueError("The original website reservation is missing.")
|
||||||
|
check_aliases(site)
|
||||||
|
for directory in (
|
||||||
|
web.APACHE_AVAILABLE,
|
||||||
|
web.APACHE_ENABLED,
|
||||||
|
web.PHP_POOL_DIRECTORY,
|
||||||
|
):
|
||||||
|
require_root_directory(directory)
|
||||||
|
if os.path.lexists(paths.account):
|
||||||
|
require_root_directory(paths.account)
|
||||||
|
elif not (
|
||||||
|
started
|
||||||
|
and (web.WEB_ROOT / ".retired" / request.operation_id).is_dir()
|
||||||
|
):
|
||||||
|
raise ValueError("The managed website data directory is missing.")
|
||||||
|
key_file = AUTHORIZED_KEYS / site.configuration()["service_user"]
|
||||||
|
if os.path.lexists(key_file):
|
||||||
|
require_root_directory(AUTHORIZED_KEYS)
|
||||||
|
check_root_file(key_file, mode=0o644)
|
||||||
|
if os.path.lexists(paths.pool):
|
||||||
|
read_managed_php_pool(site, paths.pool)
|
||||||
|
apache = managed_vhosts(site, paths)
|
||||||
|
retirement.begin()
|
||||||
|
detach_site(site, paths, key_file, apache, retirement)
|
||||||
|
marker = reservation.with_suffix(".retired")
|
||||||
|
if not os.path.lexists(marker):
|
||||||
|
write_new_file(marker, retirement.document, mode=0o600)
|
||||||
|
elif read_root_file(marker, mode=0o600) != retirement.document:
|
||||||
|
raise ValueError("The website has a different retirement record.")
|
||||||
|
archive_path(reservation, retirement.directory / "site")
|
||||||
|
retirement.finish()
|
||||||
|
|
||||||
|
|
||||||
|
def check_aliases(site):
|
||||||
|
directory = alias_directory()
|
||||||
|
if not os.path.lexists(directory):
|
||||||
|
return
|
||||||
|
require_root_directory(directory)
|
||||||
|
if any(
|
||||||
|
read_alias(marker).site.account_id == site.account_id
|
||||||
|
for marker in directory.glob("*.json")
|
||||||
|
):
|
||||||
|
raise ValueError(
|
||||||
|
"Remove the managed website aliases before retiring the site."
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def managed_vhosts(site, paths):
|
||||||
|
https, enabled = https_paths(paths)
|
||||||
|
return (
|
||||||
|
(paths.apache, paths.enabled, site.render("apache"), "apache-http"),
|
||||||
|
(https, enabled, render_https_configuration(site), "apache-https"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def detach_site(site, paths, key_file, apache, retirement):
|
||||||
|
links = []
|
||||||
|
for path, enabled, content, _ in apache:
|
||||||
|
if os.path.lexists(path):
|
||||||
|
web.check_configuration(path, content)
|
||||||
|
if os.path.lexists(enabled):
|
||||||
|
target = Path("../sites-available") / path.name
|
||||||
|
web.check_site_link(enabled, target)
|
||||||
|
links.append((enabled, target))
|
||||||
|
disable_alias_vhosts(links)
|
||||||
|
for path, _, _, label in apache:
|
||||||
|
archive_path(path, retirement.directory / label)
|
||||||
|
archive_path(paths.pool, retirement.directory / "php-pool")
|
||||||
|
web.run("/usr/sbin/php-fpm8.5", "--test")
|
||||||
|
web.run("/usr/sbin/service", web.PHP_SERVICE, "reload")
|
||||||
|
archive_path(key_file, retirement.directory / "authorized-keys")
|
||||||
|
archive_data_directory(
|
||||||
|
paths.account, web.WEB_ROOT, retirement.directory.name
|
||||||
|
)
|
||||||
|
delete_alias_certificate(site)
|
||||||
Reference in new issue
Block a user