1 parent
360ecc0af2
commit
8cec7841f1
10 files changed
+186
-23
No files matched your search
@@ -11,6 +11,7 @@ from coulobre.sftp_keys import normalize_public_keys
|
||||
from coulobre.website_files import directory_parts
|
||||
|
||||
from .access import customer_for_user
|
||||
from .installation_features import panel_feature_allowed
|
||||
from .models import FolderSftpLogin, HostingAccount
|
||||
from .resource_requests import (
|
||||
lock_hosting_account,
|
||||
@@ -119,12 +120,16 @@ def folder_sftp_users(request, pk):
|
||||
initial={"public_keys": login.public_keys} if edit else {},
|
||||
)
|
||||
enabled = settings.LOCAL_FOLDER_SFTP
|
||||
accepts_keys = panel_feature_allowed(
|
||||
"LOCAL_WEB_SFTP"
|
||||
) and panel_feature_allowed("LOCAL_FOLDER_SFTP")
|
||||
problem = ""
|
||||
if request.method == "POST":
|
||||
if (
|
||||
not enabled
|
||||
or action not in {"create", "keys", "remove"}
|
||||
or ((action != "create") != edit)
|
||||
or (action != "remove" and not accepts_keys)
|
||||
):
|
||||
raise Http404
|
||||
if action == "remove" or form.is_valid():
|
||||
@@ -150,7 +155,10 @@ def folder_sftp_users(request, pk):
|
||||
"form": form,
|
||||
"login": login,
|
||||
"enabled": enabled,
|
||||
"accepts_keys": accepts_keys,
|
||||
"logins": account.folder_sftp_logins.order_by("label"),
|
||||
"problem": problem,
|
||||
"sftp_host": settings.SFTP_HOST,
|
||||
"sftp_port": settings.SFTP_PORT,
|
||||
},
|
||||
)
|
||||
@@ -107,6 +107,22 @@ FEATURES = (
|
||||
"Install the task worker and allow tasks in the hosting package.",
|
||||
"coulobre-website-jobs",
|
||||
),
|
||||
Feature(
|
||||
"LOCAL_WEB_SFTP",
|
||||
"Website SFTP access",
|
||||
"OpenSSH Server and the website SFTP worker",
|
||||
"set-up-sftp-access-for-hosted-websites",
|
||||
"Check OpenSSH Server and enable the website SFTP worker.",
|
||||
"coulobre-sftp-apply",
|
||||
),
|
||||
Feature(
|
||||
"LOCAL_FOLDER_SFTP",
|
||||
"Folder-scoped SFTP accounts",
|
||||
"Website SFTP access and ID-mapped mounts",
|
||||
"enable-folder-scoped-sftp-logins",
|
||||
"Enable website SFTP and check the filesystem for ID-mapped mounts.",
|
||||
"coulobre-folder-sftp",
|
||||
),
|
||||
Feature(
|
||||
"LOCAL_DNS_PUBLISHING",
|
||||
"DNS publishing",
|
||||
|
||||
@@ -68,6 +68,8 @@ OPERATOR_FEATURE_SECTIONS = {
|
||||
"LOCAL_WEB_ACTIVATION",
|
||||
"WEB_FILE_MANAGER",
|
||||
"WEBSITE_JOBS",
|
||||
"LOCAL_WEB_SFTP",
|
||||
"LOCAL_FOLDER_SFTP",
|
||||
"LOCAL_DNS_PUBLISHING",
|
||||
"DEFAULT_DNS_ZONES",
|
||||
),
|
||||
@@ -251,6 +253,7 @@ class FeatureConfigurationView(UserPassesTestMixin, FormView):
|
||||
key != "account-backups"
|
||||
or settings.LOCAL_DATABASE_PROVISIONING
|
||||
)
|
||||
and (key != "folder-sftp" or settings.LOCAL_WEB_SFTP)
|
||||
}
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
|
||||
@@ -9,13 +9,14 @@
|
||||
<header class="section-heading"><div><h2>Primary hosting login</h2><p>Access to the hosting account, including files in <code>/backups</code>.</p></div>
|
||||
<a class="button secondary" href="{% url 'panel:sftp_keys' account.pk %}">Manage SSH keys</a></header>
|
||||
</section>
|
||||
<p>Connect to {% if sftp_host %}<code>{{ sftp_host }}</code>{% else %}your web node's SSH hostname{% endif %} on port <code>{{ sftp_port }}</code> with the username shown for each login.</p>
|
||||
<section class="surface"><div class="table-scroll"><table>
|
||||
<thead><tr><th scope="col">Folder login</th><th scope="col">Website folder</th><th scope="col">State</th><th scope="col">Actions</th></tr></thead>
|
||||
<tbody>{% for entry in logins %}
|
||||
<tr><th scope="row">{{ entry.label }}<br><code>{{ entry.spec.username }}</code></th><td>{{ entry.folder|default:"Entire website" }}</td>
|
||||
<td>{{ entry.state|capfirst }}{% if entry.problem %}<p>{{ entry.problem }}</p>{% endif %}</td>
|
||||
<td>{% if enabled and entry.finished_at %}
|
||||
<a class="button secondary" href="?login={{ entry.pk }}">Manage keys</a>
|
||||
{% if accepts_keys %}<a class="button secondary" href="?login={{ entry.pk }}">Manage keys</a>{% endif %}
|
||||
<form method="post" class="inline-form" action="?login={{ entry.pk }}">{% csrf_token %}
|
||||
<button class="button secondary" name="action" value="remove" type="submit">Remove access</button>
|
||||
</form>
|
||||
@@ -23,9 +24,10 @@
|
||||
</tr>
|
||||
{% empty %}<tr><td colspan="4">No folder logins.</td></tr>{% endfor %}</tbody>
|
||||
</table></div></section>
|
||||
{% if enabled %}
|
||||
{% if enabled and accepts_keys %}
|
||||
<section class="surface editor-surface">
|
||||
<h2>{% if login %}SSH keys for {{ login.label }}{% else %}Add folder login{% endif %}</h2>
|
||||
<p>Generate a key on your computer with <code>ssh-keygen -t ed25519</code>. Paste the contents of the <code>.pub</code> file here and keep the private key on your computer.</p>
|
||||
<form method="post" class="editor">{% csrf_token %}{{ form.as_div }}
|
||||
<div class="form-actions">
|
||||
<button class="button primary" name="action" value="{% if login %}keys{% else %}create{% endif %}" type="submit">{% if login %}Save keys{% else %}Add folder login{% endif %}</button>
|
||||
@@ -33,5 +35,6 @@
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
{% elif enabled %}<p class="notice">New folder access and key changes are paused.</p>
|
||||
{% else %}<p class="notice">Folder logins require the scoped SFTP worker on the web server.</p>{% endif %}
|
||||
{% endblock %}
|
||||
@@ -35,7 +35,7 @@
|
||||
<p>Run this command on your computer.</p>
|
||||
<pre><code>ssh-keygen -t ed25519</code></pre>
|
||||
<p>Choose a passphrase when prompted. Use a different filename if you already have a key. With the default filename, upload <code>~/.ssh/id_ed25519.pub</code>. Keep the private key on your computer.</p>
|
||||
<p>Use the web node's SSH hostname and port with the username <code>coulobre-{{ account.pk }}</code>. The SFTP session starts in <code>/public</code>.</p>
|
||||
<p>Connect as <code>coulobre-{{ account.pk }}</code> on {% if sftp_host %}<code>{{ sftp_host }}</code>{% else %}your web node's SSH hostname{% endif %}, port <code>{{ sftp_port }}</code>. The SFTP session starts in <code>/public</code>.</p>
|
||||
</div>
|
||||
</section>
|
||||
{{ form.non_field_errors }}
|
||||
@@ -79,15 +79,15 @@
|
||||
</div>
|
||||
</section>
|
||||
{% endif %}
|
||||
{% if can_edit_keys or can_revoke_keys %}
|
||||
{% if can_edit_keys and can_add_keys or can_revoke_keys %}
|
||||
<section class="surface editor-surface" aria-label="SSH key management">
|
||||
<form class="editor" method="post" enctype="multipart/form-data">
|
||||
{% include "hosting/customer_action_fields.html" %}
|
||||
{% if can_edit_keys %}
|
||||
{% if can_edit_keys and can_add_keys %}
|
||||
{{ form.public_keys.as_field_group }}
|
||||
{% endif %}
|
||||
<div class="form-actions">
|
||||
{% if can_edit_keys %}<button class="button primary" type="submit" name="operation" value="add">Upload SSH keys</button>{% endif %}
|
||||
{% if can_edit_keys and can_add_keys %}<button class="button primary" type="submit" name="operation" value="add">Upload SSH keys</button>{% endif %}
|
||||
{% if can_revoke_keys %}
|
||||
<button class="button secondary" type="submit" name="operation" value="revoke">Revoke SFTP access</button>
|
||||
{% endif %}
|
||||
@@ -95,5 +95,6 @@
|
||||
</form>
|
||||
</section>
|
||||
{% endif %}
|
||||
{% if not can_add_keys %}<p class="notice">New SFTP access is paused.</p>{% endif %}
|
||||
{% endwith %}
|
||||
{% endblock %}
|
||||
@@ -1854,6 +1854,9 @@ class SiteSFTPKeys(PermissionRequiredMixin, FormView):
|
||||
for key in keys
|
||||
],
|
||||
can_edit_keys=editable and idle and site_is_active(self.account),
|
||||
can_add_keys=panel_feature_allowed("LOCAL_WEB_SFTP"),
|
||||
sftp_host=settings.SFTP_HOST,
|
||||
sftp_port=settings.SFTP_PORT,
|
||||
can_revoke_keys=editable
|
||||
and idle
|
||||
and status is not None
|
||||
@@ -1867,6 +1870,9 @@ class SiteSFTPKeys(PermissionRequiredMixin, FormView):
|
||||
def form_valid(self, form):
|
||||
customer = customer_for_user(self.request.user)
|
||||
operation = form.cleaned_data["operation"]
|
||||
if operation == "add" and not panel_feature_allowed("LOCAL_WEB_SFTP"):
|
||||
form.add_error(None, "New SFTP access is paused in the panel.")
|
||||
return self.form_invalid(form)
|
||||
try:
|
||||
with support_write_session(self.request) as session:
|
||||
change = queue_sftp_keys(
|
||||
|
||||
@@ -10,6 +10,7 @@ import subprocess
|
||||
from dataclasses import dataclass
|
||||
from importlib.metadata import version
|
||||
from pathlib import Path
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from . import local_update
|
||||
from .linked_updates import REMOTE_MODES, receiver_status
|
||||
@@ -26,6 +27,12 @@ PACKAGE_ASSETS = Path("/usr/share/coulobre-panel/source/deploy")
|
||||
SYSTEMD = Path("/etc/systemd/system")
|
||||
SYSTEMCTL = "/usr/bin/systemctl"
|
||||
CERTBOT = Path("/usr/bin/certbot")
|
||||
SSHD = Path("/usr/sbin/sshd")
|
||||
SFTP_SSHD_CONFIG = Path("/etc/ssh/sshd_config.d/00-coulobre-sftp.conf")
|
||||
|
||||
|
||||
class UnsupportedFolderSftpError(ValueError):
|
||||
pass
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -35,6 +42,7 @@ class LocalFeature:
|
||||
socket: str
|
||||
worker: str = ""
|
||||
drop_in: str = ""
|
||||
extra_units: tuple[str, ...] = ()
|
||||
|
||||
@property
|
||||
def units(self):
|
||||
@@ -43,6 +51,7 @@ class LocalFeature:
|
||||
f"{self.socket}@.service",
|
||||
*((f"{self.worker}.service",) if self.worker else ()),
|
||||
*((self.drop_in,) if self.drop_in else ()),
|
||||
*self.extra_units,
|
||||
)
|
||||
|
||||
@property
|
||||
@@ -65,6 +74,18 @@ LOCAL_FEATURES = {
|
||||
"coulobre-website-jobs",
|
||||
worker="coulobre-website-job-worker",
|
||||
),
|
||||
"website-sftp": LocalFeature(
|
||||
"LOCAL_WEB_SFTP",
|
||||
"coulobre-sftp-provisioning",
|
||||
"coulobre-sftp-apply",
|
||||
worker="coulobre-sftp-provisioner",
|
||||
),
|
||||
"folder-sftp": LocalFeature(
|
||||
"LOCAL_FOLDER_SFTP",
|
||||
"coulobre-sftp-provisioning",
|
||||
"coulobre-folder-sftp",
|
||||
extra_units=("coulobre-folder-sftp-mounts.service",),
|
||||
),
|
||||
"account-backups": LocalFeature(
|
||||
"LOCAL_ACCOUNT_BACKUPS",
|
||||
"coulobre-backup-requests",
|
||||
@@ -174,8 +195,71 @@ def prepare_https(assets, command):
|
||||
command("/usr/sbin/apache2ctl", "configtest")
|
||||
|
||||
|
||||
def prepare_sftp(assets, command):
|
||||
from .web_node_sftp import AUTHORIZED_KEYS, SFTP_GROUP
|
||||
|
||||
if not SSHD.is_file():
|
||||
raise ValueError("Install OpenSSH Server before enabling SFTP.")
|
||||
command(str(SSHD), "-t")
|
||||
require_root_directory(AUTHORIZED_KEYS, create_mode=0o755)
|
||||
create_service_identity(SFTP_GROUP, "", command)
|
||||
created = not os.path.lexists(SFTP_SSHD_CONFIG)
|
||||
install_files(
|
||||
(
|
||||
(
|
||||
assets / "openssh/coulobre-sftp.conf.example",
|
||||
SFTP_SSHD_CONFIG,
|
||||
0o644,
|
||||
),
|
||||
),
|
||||
package_assets=assets == PACKAGE_ASSETS,
|
||||
)
|
||||
try:
|
||||
command(str(SSHD), "-t")
|
||||
if active("ssh.service"):
|
||||
command(SYSTEMCTL, "reload", "ssh.service")
|
||||
except (OSError, ValueError):
|
||||
if created:
|
||||
SFTP_SSHD_CONFIG.unlink()
|
||||
raise
|
||||
|
||||
|
||||
def check_folder_sftp_filesystem():
|
||||
from .web_node_apply import WEB_ROOT
|
||||
from .web_node_folder_sftp import (
|
||||
JAIL_ROOT,
|
||||
bind_folder,
|
||||
is_bound,
|
||||
run_mount,
|
||||
)
|
||||
|
||||
require_root_directory(WEB_ROOT, create_mode=0o755)
|
||||
require_root_directory(JAIL_ROOT, create_mode=0o755)
|
||||
with (
|
||||
TemporaryDirectory(dir=WEB_ROOT) as source,
|
||||
TemporaryDirectory(dir=JAIL_ROOT) as target,
|
||||
):
|
||||
mountpoint = Path(target)
|
||||
fd = os.open(source, os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW)
|
||||
try:
|
||||
bind_folder(fd, os.fstat(fd), mountpoint, 65534, 65534)
|
||||
except ValueError:
|
||||
raise UnsupportedFolderSftpError(
|
||||
"The filesystem or kernel does not support ID-mapped SFTP "
|
||||
"mounts for the website directory."
|
||||
) from None
|
||||
finally:
|
||||
os.close(fd)
|
||||
if is_bound(mountpoint):
|
||||
run_mount("/usr/bin/umount", str(mountpoint))
|
||||
|
||||
|
||||
def prepare_feature(key, assets, command):
|
||||
feature = LOCAL_FEATURES[key]
|
||||
if key == "website-sftp":
|
||||
prepare_sftp(assets, command)
|
||||
if key == "folder-sftp":
|
||||
check_folder_sftp_filesystem()
|
||||
install_files(
|
||||
(
|
||||
(assets / "systemd" / unit, SYSTEMD / unit, 0o644)
|
||||
@@ -253,6 +337,11 @@ def enable_feature(key, command):
|
||||
os.environ.get("COULOBRE_LOCAL_DATABASE_PROVISIONING") != "1"
|
||||
):
|
||||
raise ValueError("Enable local database provisioning first.")
|
||||
if key == "folder-sftp" and (
|
||||
os.environ.get("COULOBRE_LOCAL_WEB_SFTP") != "1"
|
||||
or not active("coulobre-sftp-apply.socket")
|
||||
):
|
||||
raise ValueError("Enable website SFTP access first.")
|
||||
require_root_directory(PACKAGE_ASSETS)
|
||||
config = local_update.CONFIG
|
||||
require_root_directory(config.parent)
|
||||
@@ -281,7 +370,9 @@ def apply_feature(key, hostname, config, command):
|
||||
updated = enable_setting(original, LOCAL_FEATURES[key].setting)
|
||||
feature = prepare_feature(key, PACKAGE_ASSETS, command)
|
||||
unit = feature.activation_unit
|
||||
units = tuple(dict.fromkeys((unit, f"{feature.socket}.socket")))
|
||||
units = tuple(
|
||||
dict.fromkeys((unit, f"{feature.socket}.socket", *feature.extra_units))
|
||||
)
|
||||
inactive = tuple(name for name in units if not active(name))
|
||||
already_running = bool(feature.worker and unit not in inactive)
|
||||
panel_restart_started = False
|
||||
@@ -321,6 +412,7 @@ def main():
|
||||
from .operator_node import command
|
||||
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--skip-unsupported-folder-sftp", action="store_true")
|
||||
parser.add_argument("assets", type=Path)
|
||||
parser.add_argument("feature", choices=LOCAL_FEATURES, nargs="+")
|
||||
args = parser.parse_args()
|
||||
@@ -328,11 +420,13 @@ def main():
|
||||
parser.exit(1, "Run local feature setup as root.\n")
|
||||
try:
|
||||
for key in args.feature:
|
||||
print(
|
||||
prepare_feature(
|
||||
key, args.assets.resolve(), command
|
||||
).activation_unit
|
||||
)
|
||||
try:
|
||||
feature = prepare_feature(key, args.assets.resolve(), command)
|
||||
except UnsupportedFolderSftpError:
|
||||
if not args.skip_unsupported_folder_sftp:
|
||||
raise
|
||||
continue
|
||||
print(feature.activation_unit)
|
||||
except (OSError, ValueError) as error:
|
||||
parser.exit(1, f"{error}\n")
|
||||
|
||||
|
||||
@@ -243,6 +243,10 @@ LOCAL_WEB_HTTPS = os.environ.get("COULOBRE_LOCAL_WEB_HTTPS") == "1"
|
||||
WEB_FILE_MANAGER = os.environ.get("COULOBRE_WEB_FILE_MANAGER") == "1"
|
||||
WEBSITE_JOBS = os.environ.get("COULOBRE_WEBSITE_JOBS") == "1"
|
||||
LOCAL_WEB_SFTP = os.environ.get("COULOBRE_LOCAL_WEB_SFTP") == "1"
|
||||
SFTP_HOST = os.environ.get("COULOBRE_SFTP_HOST", "").strip()
|
||||
SFTP_PORT = int(os.environ.get("COULOBRE_SFTP_PORT", "22"))
|
||||
if not 1 <= SFTP_PORT <= 65535:
|
||||
raise ImproperlyConfigured("COULOBRE_SFTP_PORT must be a valid TCP port.")
|
||||
SYSTEM_BACKUPS = os.environ.get("COULOBRE_SYSTEM_BACKUPS") == "1"
|
||||
SERVER_PACKAGE_UPDATES = (
|
||||
os.environ.get("COULOBRE_SERVER_PACKAGE_UPDATES") == "1"
|
||||
|
||||
Reference in new issue
Block a user