|
1 | 1 | import os
|
2 | 2 | import subprocess
|
| 3 | +import time |
| 4 | +import click |
3 | 5 | from pathlib import Path
|
4 | 6 | from urllib.parse import urlparse
|
| 7 | +from slugify import slugify |
| 8 | +from ctfcli.utils.config import generate_session |
5 | 9 |
|
6 |
| -from ctfcli.utils.images import build_image, export_image, get_exposed_ports |
| 10 | +from ctfcli.utils.images import ( |
| 11 | + build_image, |
| 12 | + export_image, |
| 13 | + get_exposed_ports, |
| 14 | + push_image, |
| 15 | + login_registry, |
| 16 | +) |
7 | 17 |
|
8 | 18 |
|
9 |
| -def ssh(challenge, host): |
| 19 | +def format_connection_info(protocol, hostname, tcp_hostname, tcp_port): |
| 20 | + if protocol is None: |
| 21 | + connection_info = hostname |
| 22 | + elif protocol.startswith("http"): |
| 23 | + connection_info = f"{protocol}://{hostname}" |
| 24 | + elif protocol == "tcp": |
| 25 | + connection_info = f"nc {tcp_hostname} {tcp_port}" |
| 26 | + else: |
| 27 | + connection_info = hostname |
| 28 | + |
| 29 | + return connection_info |
| 30 | + |
| 31 | + |
| 32 | +def ssh(challenge, host, protocol): |
10 | 33 | # Build image
|
11 | 34 | image_name = build_image(challenge=challenge)
|
12 | 35 | print(f"Built {image_name}")
|
@@ -39,17 +62,111 @@ def ssh(challenge, host):
|
39 | 62 | os.remove(image_path)
|
40 | 63 | print(f"Cleaned up {image_path}")
|
41 | 64 |
|
42 |
| - return True, domain, exposed_port |
| 65 | + status = True |
| 66 | + domain = domain |
| 67 | + port = exposed_port |
| 68 | + connect_info = format_connection_info( |
| 69 | + protocol=protocol, hostname=domain, tcp_hostname=domain, tcp_port=port, |
| 70 | + ) |
| 71 | + return status, domain, port, connect_info |
43 | 72 |
|
44 | 73 |
|
45 |
| -def registry(challenge, host): |
| 74 | +def registry(challenge, host, protocol): |
46 | 75 | # Build image
|
47 | 76 | image_name = build_image(challenge=challenge)
|
48 |
| - print(f"Built {image_name}") |
49 | 77 | url = urlparse(host)
|
50 | 78 | tag = f"{url.netloc}{url.path}"
|
51 |
| - subprocess.call(["docker", "tag", image_name, tag]) |
52 |
| - subprocess.call(["docker", "push", tag]) |
| 79 | + push_image(local_tag=image_name, location=tag) |
| 80 | + status = True |
| 81 | + domain = "" |
| 82 | + port = "" |
| 83 | + connect_info = format_connection_info( |
| 84 | + protocol=protocol, hostname=domain, tcp_hostname=domain, tcp_port=port, |
| 85 | + ) |
| 86 | + return status, domain, port, connect_info |
| 87 | + |
| 88 | + |
| 89 | +def cloud(challenge, host, protocol): |
| 90 | + name = challenge["name"] |
| 91 | + slug = slugify(name) |
| 92 | + |
| 93 | + s = generate_session() |
| 94 | + # Detect whether we have the appropriate endpoints |
| 95 | + check = s.get(f"/api/v1/images", json=True) |
| 96 | + if check.ok is False: |
| 97 | + click.secho( |
| 98 | + f"Target instance does not have deployment endpoints", fg="red", |
| 99 | + ) |
| 100 | + return False, domain, port, connect_info |
| 101 | + |
| 102 | + # Try to find an appropriate image. |
| 103 | + images = s.get(f"/api/v1/images", json=True).json()["data"] |
| 104 | + image = None |
| 105 | + for i in images: |
| 106 | + if i["location"].endswith(f"/{slug}"): |
| 107 | + image = i |
| 108 | + break |
| 109 | + else: |
| 110 | + # Create the image if we did not find it. |
| 111 | + image = s.post(f"/api/v1/images", json={"name": slug}).json()["data"] |
| 112 | + |
| 113 | + # Build image |
| 114 | + image_name = build_image(challenge=challenge) |
| 115 | + location = image["location"] |
| 116 | + |
| 117 | + # TODO: Authenticate to Registry |
| 118 | + |
| 119 | + # Push image |
| 120 | + push_image(image_name, location) |
| 121 | + |
| 122 | + # Look for existing service |
| 123 | + services = s.get(f"/api/v1/services", json=True).json()["data"] |
| 124 | + service = None |
| 125 | + for srv in services: |
| 126 | + if srv["name"] == slug: |
| 127 | + service = srv |
| 128 | + # Update the service |
| 129 | + s.patch( |
| 130 | + f"/api/v1/services/{service['id']}", json={"image": location} |
| 131 | + ).raise_for_status() |
| 132 | + service = s.get(f"/api/v1/services/{service['id']}", json=True).json()[ |
| 133 | + "data" |
| 134 | + ] |
| 135 | + break |
| 136 | + else: |
| 137 | + # Could not find the service. Create it using our pushed image. |
| 138 | + # Deploy the image by creating service |
| 139 | + service = s.post( |
| 140 | + f"/api/v1/services", json={"name": slug, "image": location,} |
| 141 | + ).json()["data"] |
| 142 | + |
| 143 | + # Get connection details |
| 144 | + service_id = service["id"] |
| 145 | + service = s.get(f"/api/v1/services/{service_id}", json=True).json()["data"] |
| 146 | + |
| 147 | + while service["hostname"] is None: |
| 148 | + click.secho( |
| 149 | + f"Waiting for challenge hostname", fg="yellow", |
| 150 | + ) |
| 151 | + service = s.get(f"/api/v1/services/{service_id}", json=True).json()["data"] |
| 152 | + time.sleep(10) |
| 153 | + |
| 154 | + # Expose port if we are using tcp |
| 155 | + if protocol == "tcp": |
| 156 | + service = s.patch(f"/api/v1/services/{service['id']}", json={"expose": True}) |
| 157 | + service.raise_for_status() |
| 158 | + service = s.get(f"/api/v1/services/{service_id}", json=True).json()["data"] |
| 159 | + |
| 160 | + status = True |
| 161 | + domain = "" |
| 162 | + port = "" |
| 163 | + connect_info = format_connection_info( |
| 164 | + protocol=protocol, |
| 165 | + hostname=service["hostname"], |
| 166 | + tcp_hostname=service["tcp_hostname"], |
| 167 | + tcp_port=service["tcp_port"], |
| 168 | + ) |
| 169 | + return status, domain, port, connect_info |
53 | 170 |
|
54 | 171 |
|
55 |
| -DEPLOY_HANDLERS = {"ssh": ssh, "registry": registry} |
| 172 | +DEPLOY_HANDLERS = {"ssh": ssh, "registry": registry, "cloud": cloud} |
0 commit comments