2019-12-24 16:22:12 +00:00
|
|
|
import base64
|
2022-06-21 00:37:24 +00:00
|
|
|
from functools import lru_cache
|
2019-11-22 08:20:17 +00:00
|
|
|
import json
|
2019-05-11 22:11:44 +00:00
|
|
|
import os
|
2019-01-22 20:25:04 +00:00
|
|
|
import random
|
2022-07-20 21:20:43 +00:00
|
|
|
import shlex
|
2019-01-22 20:25:04 +00:00
|
|
|
import shutil
|
|
|
|
import string
|
2019-12-24 16:22:12 +00:00
|
|
|
import struct
|
2019-01-22 20:25:04 +00:00
|
|
|
import subprocess
|
2020-03-27 08:59:11 +00:00
|
|
|
import sys
|
2021-02-25 08:09:14 +00:00
|
|
|
from typing import List, Tuple
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
import click
|
2020-11-07 13:46:53 +00:00
|
|
|
from Crypto.Protocol.KDF import bcrypt, bcrypt_check
|
2021-02-25 08:09:14 +00:00
|
|
|
from Crypto.PublicKey import RSA
|
|
|
|
from Crypto.PublicKey.RSA import RsaKey
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
from . import exceptions, fmt
|
2019-01-22 20:25:04 +00:00
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def encrypt(text: str) -> str:
|
2020-04-04 16:22:15 +00:00
|
|
|
"""
|
2020-11-07 13:46:53 +00:00
|
|
|
Encrypt some textual content with bcrypt.
|
|
|
|
https://pycryptodome.readthedocs.io/en/latest/src/protocol/kdf.html#bcrypt
|
|
|
|
The encryption process is compatible with the password verification performed by
|
2020-04-04 16:22:15 +00:00
|
|
|
`htpasswd <https://httpd.apache.org/docs/2.4/programs/htpasswd.html>`__.
|
|
|
|
"""
|
2020-11-07 13:46:53 +00:00
|
|
|
return bcrypt(text.encode(), 12).decode()
|
2020-04-04 16:22:15 +00:00
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def verify_encrypted(encrypted: str, text: str) -> bool:
|
2020-04-04 16:22:15 +00:00
|
|
|
"""
|
|
|
|
Return True/False if the encrypted content corresponds to the unencrypted text.
|
|
|
|
"""
|
2020-11-07 13:46:53 +00:00
|
|
|
try:
|
|
|
|
bcrypt_check(text.encode(), encrypted.encode())
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
2020-04-04 16:22:15 +00:00
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def ensure_file_directory_exists(path: str) -> None:
|
2019-05-11 22:11:44 +00:00
|
|
|
"""
|
|
|
|
Create file's base directory if it does not exist.
|
|
|
|
"""
|
|
|
|
directory = os.path.dirname(path)
|
2021-03-15 22:26:38 +00:00
|
|
|
if os.path.isfile(directory):
|
|
|
|
raise exceptions.TutorError(
|
2022-02-21 10:52:46 +00:00
|
|
|
f"Attempting to create a directory, but a file with the same name already exists: {directory}"
|
2021-03-15 22:26:38 +00:00
|
|
|
)
|
|
|
|
if os.path.isdir(path):
|
|
|
|
raise exceptions.TutorError(
|
2022-02-21 10:52:46 +00:00
|
|
|
f"Attempting to write to a file, but a directory with the same name already exists: {directory}"
|
2021-03-15 22:26:38 +00:00
|
|
|
)
|
2019-05-11 22:11:44 +00:00
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def random_string(length: int) -> str:
|
2019-05-05 09:45:24 +00:00
|
|
|
return "".join(
|
|
|
|
[random.choice(string.ascii_letters + string.digits) for _ in range(length)]
|
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-11-22 08:23:59 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def list_if(services: List[Tuple[str, bool]]) -> str:
|
2019-11-22 08:20:17 +00:00
|
|
|
return json.dumps([service[0] for service in services if service[1]])
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-11-22 08:23:59 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def common_domain(d1: str, d2: str) -> str:
|
2019-03-23 23:07:50 +00:00
|
|
|
"""
|
|
|
|
Return the common domain between two domain names.
|
|
|
|
|
|
|
|
Ex: "sub1.domain.com" and "sub2.domain.com" -> "domain.com"
|
|
|
|
"""
|
|
|
|
components1 = d1.split(".")[::-1]
|
|
|
|
components2 = d2.split(".")[::-1]
|
|
|
|
common = []
|
|
|
|
for c in range(0, min(len(components1), len(components2))):
|
|
|
|
if components1[c] == components2[c]:
|
|
|
|
common.append(components1[c])
|
|
|
|
else:
|
|
|
|
break
|
|
|
|
return ".".join(common[::-1])
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def reverse_host(domain: str) -> str:
|
2019-05-11 22:11:44 +00:00
|
|
|
"""
|
|
|
|
Return the reverse domain name, java-style.
|
|
|
|
|
|
|
|
Ex: "www.google.com" -> "com.google.www"
|
|
|
|
"""
|
|
|
|
return ".".join(domain.split(".")[::-1])
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def rsa_private_key(bits: int = 2048) -> str:
|
2019-12-24 16:22:12 +00:00
|
|
|
"""
|
|
|
|
Export an RSA private key in PEM format.
|
|
|
|
"""
|
|
|
|
key = RSA.generate(bits)
|
|
|
|
return key.export_key().decode()
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def rsa_import_key(key: str) -> RsaKey:
|
2019-12-24 16:22:12 +00:00
|
|
|
"""
|
|
|
|
Import PEM-formatted RSA key and return the corresponding object.
|
|
|
|
"""
|
|
|
|
return RSA.import_key(key.encode())
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def long_to_base64(n: int) -> str:
|
2019-12-24 16:22:12 +00:00
|
|
|
"""
|
|
|
|
Borrowed from jwkest.__init__
|
|
|
|
"""
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def long2intarr(long_int: int) -> List[int]:
|
|
|
|
_bytes: List[int] = []
|
2019-12-24 16:22:12 +00:00
|
|
|
while long_int:
|
|
|
|
long_int, r = divmod(long_int, 256)
|
|
|
|
_bytes.insert(0, r)
|
|
|
|
return _bytes
|
|
|
|
|
|
|
|
bys = long2intarr(n)
|
2022-02-21 10:52:46 +00:00
|
|
|
data = struct.pack(f"{len(bys)}B", *bys)
|
2019-12-24 16:22:12 +00:00
|
|
|
if not data:
|
2021-02-25 08:54:46 +00:00
|
|
|
data = b"\x00"
|
2019-12-24 16:22:12 +00:00
|
|
|
s = base64.urlsafe_b64encode(data).rstrip(b"=")
|
|
|
|
return s.decode("ascii")
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def is_root() -> bool:
|
2020-11-16 11:17:15 +00:00
|
|
|
"""
|
|
|
|
Check whether tutor is being run as root/sudo.
|
|
|
|
"""
|
2020-11-19 10:29:44 +00:00
|
|
|
if sys.platform == "win32":
|
|
|
|
# Don't even try
|
|
|
|
return False
|
2020-11-16 11:17:15 +00:00
|
|
|
return get_user_id() == 0
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def get_user_id() -> int:
|
2020-11-07 16:48:20 +00:00
|
|
|
"""
|
|
|
|
Portable way to get user ID. Note: I have no idea if it actually works on windows...
|
|
|
|
"""
|
2021-11-23 08:25:09 +00:00
|
|
|
if sys.platform != "win32":
|
|
|
|
return os.getuid()
|
|
|
|
|
|
|
|
# Don't even try for windows
|
|
|
|
return 0
|
2020-11-07 16:48:20 +00:00
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def docker_run(*command: str) -> int:
|
2020-03-27 08:59:11 +00:00
|
|
|
args = ["run", "--rm"]
|
|
|
|
if is_a_tty():
|
|
|
|
args.append("-it")
|
|
|
|
return docker(*args, *command)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def docker(*command: str) -> int:
|
2019-01-22 20:25:04 +00:00
|
|
|
if shutil.which("docker") is None:
|
2019-05-05 09:45:24 +00:00
|
|
|
raise exceptions.TutorError(
|
|
|
|
"docker is not installed. Please follow instructions from https://docs.docker.com/install/"
|
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
return execute("docker", *command)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2022-06-22 09:28:27 +00:00
|
|
|
@lru_cache(maxsize=None)
|
2022-06-21 00:37:24 +00:00
|
|
|
def _docker_compose_command() -> Tuple[str, ...]:
|
|
|
|
"""
|
|
|
|
A helper function to determine which program to call when running docker compose
|
|
|
|
"""
|
|
|
|
if os.environ.get("TUTOR_USE_COMPOSE_SUBCOMMAND") is not None:
|
|
|
|
return ("docker", "compose")
|
|
|
|
if shutil.which("docker-compose") is not None:
|
|
|
|
return ("docker-compose",)
|
|
|
|
if shutil.which("docker") is not None:
|
|
|
|
if subprocess.run(["docker", "compose"], capture_output=True).returncode == 0:
|
|
|
|
return ("docker", "compose")
|
|
|
|
raise exceptions.TutorError(
|
|
|
|
"docker-compose is not installed. Please follow instructions from https://docs.docker.com/compose/install/"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def docker_compose(*command: str) -> int:
|
2022-06-21 00:37:24 +00:00
|
|
|
return execute(*_docker_compose_command(), *command)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def kubectl(*command: str) -> int:
|
2019-01-22 20:25:04 +00:00
|
|
|
if shutil.which("kubectl") is None:
|
|
|
|
raise exceptions.TutorError(
|
|
|
|
"kubectl is not installed. Please follow instructions from https://kubernetes.io/docs/tasks/tools/install-kubectl/"
|
|
|
|
)
|
|
|
|
return execute("kubectl", *command)
|
|
|
|
|
2020-03-27 09:17:36 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def is_a_tty() -> bool:
|
2020-03-27 08:59:11 +00:00
|
|
|
"""
|
|
|
|
Return True if stdin is able to allocate a tty. Tty allocation sometimes cannot be
|
|
|
|
enabled, for instance in cron jobs
|
|
|
|
"""
|
2021-11-23 08:25:09 +00:00
|
|
|
return sys.stdin.isatty()
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2020-03-27 09:17:36 +00:00
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def execute(*command: str) -> int:
|
2022-07-25 15:34:13 +00:00
|
|
|
click.echo(fmt.command(_shlex_join(*command)))
|
2019-01-22 20:25:04 +00:00
|
|
|
with subprocess.Popen(command) as p:
|
|
|
|
try:
|
|
|
|
result = p.wait(timeout=None)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
p.kill()
|
|
|
|
p.wait()
|
|
|
|
raise
|
2020-10-30 17:16:54 +00:00
|
|
|
except Exception as e:
|
2019-01-22 20:25:04 +00:00
|
|
|
p.kill()
|
|
|
|
p.wait()
|
2022-02-21 10:52:46 +00:00
|
|
|
raise exceptions.TutorError(f"Command failed: {' '.join(command)}") from e
|
2019-01-22 20:25:04 +00:00
|
|
|
if result > 0:
|
2019-05-05 09:45:24 +00:00
|
|
|
raise exceptions.TutorError(
|
2022-02-21 10:52:46 +00:00
|
|
|
f"Command failed with status {result}: {' '.join(command)}"
|
2019-05-05 09:45:24 +00:00
|
|
|
)
|
2021-02-25 08:09:14 +00:00
|
|
|
return result
|
2019-06-05 13:43:51 +00:00
|
|
|
|
|
|
|
|
2022-07-25 15:34:13 +00:00
|
|
|
def _shlex_join(*split_command: str) -> str:
|
|
|
|
"""
|
|
|
|
Return a shell-escaped string from *split_command.
|
|
|
|
|
|
|
|
TODO: REMOVE THIS FUNCTION AFTER 2023-06-27.
|
|
|
|
This function is a shim for the ``shlex.join`` standard library function,
|
|
|
|
which becomes available in Python 3.8 The end-of-life date for Python 3.7
|
|
|
|
is in Jan 2023 (https://endoflife.date/python). After that point, it
|
|
|
|
would be good to delete this function and just use Py3.8's ``shlex.join``.
|
|
|
|
"""
|
|
|
|
return " ".join(shlex.quote(arg) for arg in split_command)
|
|
|
|
|
|
|
|
|
2021-02-25 08:09:14 +00:00
|
|
|
def check_output(*command: str) -> bytes:
|
2022-02-21 10:52:46 +00:00
|
|
|
literal_command = " ".join(command)
|
|
|
|
click.echo(fmt.command(literal_command))
|
2019-06-05 13:43:51 +00:00
|
|
|
try:
|
|
|
|
return subprocess.check_output(command)
|
2020-11-17 08:59:51 +00:00
|
|
|
except Exception as e:
|
2022-02-21 10:52:46 +00:00
|
|
|
raise exceptions.TutorError(f"Command failed: {literal_command}") from e
|
2021-10-19 00:43:53 +00:00
|
|
|
|
|
|
|
|
2022-01-06 10:49:36 +00:00
|
|
|
def check_macos_docker_memory() -> None:
|
2021-10-19 00:43:53 +00:00
|
|
|
"""
|
2021-10-25 17:53:32 +00:00
|
|
|
Try to check that the RAM allocated to the Docker VM on macOS is at least 4 GB.
|
2022-01-06 10:49:36 +00:00
|
|
|
|
|
|
|
Parse macOS Docker settings file from user directory and return the max
|
|
|
|
allocated memory. Will raise TutorError in case of parsing/loading error.
|
2021-10-19 00:43:53 +00:00
|
|
|
"""
|
|
|
|
if sys.platform != "darwin":
|
|
|
|
return
|
|
|
|
|
2021-10-25 17:53:32 +00:00
|
|
|
settings_path = os.path.expanduser(
|
|
|
|
"~/Library/Group Containers/group.com.docker/settings.json"
|
2021-10-22 02:40:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2022-02-21 10:52:46 +00:00
|
|
|
with open(settings_path, encoding="utf-8") as fp:
|
2021-10-25 17:53:32 +00:00
|
|
|
data = json.load(fp)
|
|
|
|
memory_mib = int(data["memoryMiB"])
|
|
|
|
except OSError as e:
|
2022-01-06 10:49:36 +00:00
|
|
|
raise exceptions.TutorError(f"Error accessing Docker settings file: {e}") from e
|
2021-10-25 17:53:32 +00:00
|
|
|
except json.JSONDecodeError as e:
|
|
|
|
raise exceptions.TutorError(
|
2022-01-06 10:49:36 +00:00
|
|
|
f"Error reading {settings_path}, invalid JSON: {e}"
|
2021-10-25 17:53:32 +00:00
|
|
|
) from e
|
2022-01-06 10:49:36 +00:00
|
|
|
except ValueError as e:
|
2021-10-25 17:53:32 +00:00
|
|
|
raise exceptions.TutorError(
|
2022-01-06 10:49:36 +00:00
|
|
|
f"Unexpected JSON data in {settings_path}: {e}"
|
2021-10-25 17:53:32 +00:00
|
|
|
) from e
|
|
|
|
except KeyError as e:
|
|
|
|
# Value is absent (Docker creates the file with the default setting of 2048 explicitly
|
|
|
|
# written in, so we shouldn't need to assume a default value here.)
|
|
|
|
raise exceptions.TutorError(
|
2022-01-06 10:49:36 +00:00
|
|
|
f"key 'memoryMiB' not found in {settings_path}"
|
|
|
|
) from e
|
|
|
|
except (TypeError, OverflowError) as e:
|
|
|
|
# TypeError from open() indicates an encoding error
|
|
|
|
raise exceptions.TutorError(
|
|
|
|
f"Text encoding error in {settings_path}: {e}"
|
2021-10-25 17:53:32 +00:00
|
|
|
) from e
|
2021-10-19 00:43:53 +00:00
|
|
|
|
2021-10-22 02:40:50 +00:00
|
|
|
if memory_mib < 4096:
|
2021-10-19 00:43:53 +00:00
|
|
|
raise exceptions.TutorError(
|
2022-02-21 10:52:46 +00:00
|
|
|
f"Docker is configured to allocate {memory_mib} MiB RAM, less than the recommended {4096} MiB"
|
2021-10-19 00:43:53 +00:00
|
|
|
)
|