2019-12-24 16:22:12 +00:00
|
|
|
import base64
|
2020-04-04 16:22:15 +00:00
|
|
|
from crypt import crypt
|
|
|
|
from hmac import compare_digest
|
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
|
|
|
|
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
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
import click
|
2019-12-24 16:22:12 +00:00
|
|
|
from Crypto.PublicKey import RSA
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
from . import exceptions
|
|
|
|
from . import fmt
|
|
|
|
|
|
|
|
|
2020-04-04 16:22:15 +00:00
|
|
|
def encrypt(text):
|
|
|
|
"""
|
|
|
|
Encrypt some textual content. The method employed is the same as suggested in the
|
|
|
|
`python docs <https://docs.python.org/3/library/crypt.html#examples>`__. The
|
|
|
|
encryption process is compatible with the password verification performed by
|
|
|
|
`htpasswd <https://httpd.apache.org/docs/2.4/programs/htpasswd.html>`__.
|
|
|
|
"""
|
|
|
|
hashed = crypt(text)
|
|
|
|
return crypt(text, hashed)
|
|
|
|
|
|
|
|
|
|
|
|
def verify_encrypted(encrypted, text):
|
|
|
|
"""
|
|
|
|
Return True/False if the encrypted content corresponds to the unencrypted text.
|
|
|
|
"""
|
|
|
|
return compare_digest(crypt(text, encrypted), encrypted)
|
|
|
|
|
|
|
|
|
2019-05-11 22:11:44 +00:00
|
|
|
def ensure_file_directory_exists(path):
|
|
|
|
"""
|
|
|
|
Create file's base directory if it does not exist.
|
|
|
|
"""
|
|
|
|
directory = os.path.dirname(path)
|
|
|
|
if not os.path.exists(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def random_string(length):
|
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
|
|
|
|
2019-11-22 08:20:17 +00:00
|
|
|
def list_if(services):
|
|
|
|
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
|
|
|
|
2019-03-23 23:07:50 +00:00
|
|
|
def common_domain(d1, d2):
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2019-05-11 22:11:44 +00:00
|
|
|
def reverse_host(domain):
|
|
|
|
"""
|
|
|
|
Return the reverse domain name, java-style.
|
|
|
|
|
|
|
|
Ex: "www.google.com" -> "com.google.www"
|
|
|
|
"""
|
|
|
|
return ".".join(domain.split(".")[::-1])
|
|
|
|
|
|
|
|
|
2019-12-24 16:22:12 +00:00
|
|
|
def rsa_private_key(bits=2048):
|
|
|
|
"""
|
|
|
|
Export an RSA private key in PEM format.
|
|
|
|
"""
|
|
|
|
key = RSA.generate(bits)
|
|
|
|
return key.export_key().decode()
|
|
|
|
|
|
|
|
|
|
|
|
def rsa_import_key(key):
|
|
|
|
"""
|
|
|
|
Import PEM-formatted RSA key and return the corresponding object.
|
|
|
|
"""
|
|
|
|
return RSA.import_key(key.encode())
|
|
|
|
|
|
|
|
|
|
|
|
def long_to_base64(n):
|
|
|
|
"""
|
|
|
|
Borrowed from jwkest.__init__
|
|
|
|
"""
|
|
|
|
|
|
|
|
def long2intarr(long_int):
|
|
|
|
_bytes = []
|
|
|
|
while long_int:
|
|
|
|
long_int, r = divmod(long_int, 256)
|
|
|
|
_bytes.insert(0, r)
|
|
|
|
return _bytes
|
|
|
|
|
|
|
|
bys = long2intarr(n)
|
|
|
|
data = struct.pack("%sB" % len(bys), *bys)
|
|
|
|
if not data:
|
|
|
|
data = "\x00"
|
|
|
|
s = base64.urlsafe_b64encode(data).rstrip(b"=")
|
|
|
|
return s.decode("ascii")
|
|
|
|
|
|
|
|
|
2019-05-09 07:51:06 +00:00
|
|
|
def walk_files(path):
|
|
|
|
"""
|
|
|
|
Iterate on file paths located in directory.
|
|
|
|
"""
|
|
|
|
for dirpath, _, filenames in os.walk(path):
|
|
|
|
for filename in filenames:
|
|
|
|
yield os.path.join(dirpath, filename)
|
|
|
|
|
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def docker_run(*command):
|
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
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def docker(*command):
|
|
|
|
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
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def docker_compose(*command):
|
|
|
|
if shutil.which("docker-compose") is None:
|
2019-05-05 09:45:24 +00:00
|
|
|
raise exceptions.TutorError(
|
|
|
|
"docker-compose is not installed. Please follow instructions from https://docs.docker.com/compose/install/"
|
|
|
|
)
|
2019-01-22 20:25:04 +00:00
|
|
|
return execute("docker-compose", *command)
|
|
|
|
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def kubectl(*command):
|
|
|
|
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
|
|
|
|
2020-03-27 08:59:11 +00:00
|
|
|
def is_a_tty():
|
|
|
|
"""
|
|
|
|
Return True if stdin is able to allocate a tty. Tty allocation sometimes cannot be
|
|
|
|
enabled, for instance in cron jobs
|
|
|
|
"""
|
|
|
|
return os.isatty(sys.stdin.fileno())
|
2019-04-23 07:57:55 +00:00
|
|
|
|
2020-03-27 09:17:36 +00:00
|
|
|
|
2019-01-22 20:25:04 +00:00
|
|
|
def execute(*command):
|
|
|
|
click.echo(fmt.command(" ".join(command)))
|
|
|
|
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()
|
2020-10-30 17:16:54 +00:00
|
|
|
raise exceptions.TutorError(
|
|
|
|
"Command failed: {}".format(" ".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(
|
|
|
|
"Command failed with status {}: {}".format(result, " ".join(command))
|
|
|
|
)
|
2019-06-05 13:43:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
def check_output(*command):
|
|
|
|
click.echo(fmt.command(" ".join(command)))
|
|
|
|
try:
|
|
|
|
return subprocess.check_output(command)
|
|
|
|
except:
|
|
|
|
fmt.echo_error("Command failed: {}".format(" ".join(command)))
|
|
|
|
raise
|