| 1 |
#! /usr/bin/python3 |
| 2 |
|
| 3 |
# This is just enough of the Insights REST API to make the following |
| 4 |
# work: |
| 5 |
# |
| 6 |
# insights-client --register |
| 7 |
# insights-client --status |
| 8 |
# insights-client --check-results |
| 9 |
# insights-client --unregister |
| 10 |
# |
| 11 |
# You need these in your insights-client.conf: |
| 12 |
# |
| 13 |
# auto_config=False |
| 14 |
# auto_update=False |
| 15 |
# base_url=127.0.0.1:8443/r/insights |
| 16 |
# cert_verify=/var/lib/insights/mock-certs/ca.crt |
| 17 |
# username=admin |
| 18 |
# password=foobar |
| 19 |
|
| 20 |
import email |
| 21 |
import json |
| 22 |
import os |
| 23 |
import re |
| 24 |
import ssl |
| 25 |
import subprocess |
| 26 |
from http.server import BaseHTTPRequestHandler, HTTPServer |
| 27 |
|
| 28 |
systems: dict[str, object] = {} |
| 29 |
|
| 30 |
|
| 31 |
class handler(BaseHTTPRequestHandler): |
| 32 |
def match(self, p): |
| 33 |
return re.fullmatch(p, self.path) |
| 34 |
|
| 35 |
def do_GET(self): |
| 36 |
|
| 37 |
m = self.match("/r/insights") |
| 38 |
if m: |
| 39 |
self.send_response(200) |
| 40 |
self.end_headers() |
| 41 |
self.wfile.write(b"lub-dup") |
| 42 |
return |
| 43 |
|
| 44 |
m = self.match("/r/insights/v1/static/uploader.v2.json") |
| 45 |
if m: |
| 46 |
# This is not a valid response and will cause the client |
| 47 |
# to fall back to the builtin rules. |
| 48 |
self.send_response(200) |
| 49 |
self.end_headers() |
| 50 |
self.wfile.write(b"{ }\n") |
| 51 |
return |
| 52 |
|
| 53 |
m = self.match("/r/insights/v1/systems/([^/]+)") |
| 54 |
if m: |
| 55 |
machine_id = m[1] |
| 56 |
for system in systems.values(): |
| 57 |
if system["machine_id"] == machine_id: |
| 58 |
self.send_response(200) |
| 59 |
self.end_headers() |
| 60 |
self.wfile.write(json.dumps(system).encode() + b"\n") |
| 61 |
return |
| 62 |
self.send_response(404) |
| 63 |
self.end_headers() |
| 64 |
return |
| 65 |
|
| 66 |
m = self.match("/r/insights/v1/branch_info") |
| 67 |
if m: |
| 68 |
self.send_response(200) |
| 69 |
self.end_headers() |
| 70 |
self.wfile.write(b'{ "remote_branch": -1, "remote_leaf": -1 }\n') |
| 71 |
return |
| 72 |
|
| 73 |
m = self.match("/r/insights/platform/inventory/v1/hosts\\?insights_id=(.*)") |
| 74 |
if m: |
| 75 |
insights_id = m[1] |
| 76 |
self.send_response(200) |
| 77 |
self.end_headers() |
| 78 |
res = { |
| 79 |
"total": 0, |
| 80 |
"results": [], |
| 81 |
} |
| 82 |
for system in systems.values(): |
| 83 |
if system["insights_id"] == insights_id: |
| 84 |
res["total"] += 1 |
| 85 |
res["results"].append(system) |
| 86 |
self.wfile.write(json.dumps(res).encode("utf-8") + b"\n") |
| 87 |
return |
| 88 |
|
| 89 |
m = self.match("/r/insights/platform/inventory/v1/host_exists\\?insights_id=(.*)") |
| 90 |
if m: |
| 91 |
insights_id = m[1] |
| 92 |
self.send_response(200) |
| 93 |
self.end_headers() |
| 94 |
res = {} |
| 95 |
for system in systems.values(): |
| 96 |
if system["insights_id"] == insights_id: |
| 97 |
res["id"] = system["id"] |
| 98 |
break |
| 99 |
self.wfile.write(json.dumps(res).encode("utf-8") + b"\n") |
| 100 |
return |
| 101 |
|
| 102 |
m = self.match("/r/insights/platform/insights/v1/system/([^/]+)/reports/") |
| 103 |
if m: |
| 104 |
inventory_id = m[1] |
| 105 |
self.send_response(200) |
| 106 |
self.end_headers() |
| 107 |
if inventory_id in systems: |
| 108 |
self.wfile.write(b'[ { "rule": { "total_risk": 3 } }, { "rule": { "total_risk": 2 } }, { "rule": { "total_risk": 1 } }]\n') |
| 109 |
else: |
| 110 |
self.wfile.write(b'[ ]\n') |
| 111 |
return |
| 112 |
|
| 113 |
self.send_response(404) |
| 114 |
self.end_headers() |
| 115 |
|
| 116 |
def do_POST(self): |
| 117 |
content_length = int(self.headers.get('content-length', 0)) |
| 118 |
data = self.rfile.read(content_length) |
| 119 |
|
| 120 |
m = self.match("/r/insights/v1/systems") |
| 121 |
if m: |
| 122 |
s = json.loads(data) |
| 123 |
s["unregistered_at"] = None |
| 124 |
s["account_number"] = "123456" |
| 125 |
s["insights_id"] = s["machine_id"] |
| 126 |
s["id"] = "123-nice-id" |
| 127 |
print(s) |
| 128 |
systems[s["id"]] = s |
| 129 |
self.send_response(200) |
| 130 |
self.end_headers() |
| 131 |
self.wfile.write(data) |
| 132 |
return |
| 133 |
|
| 134 |
m = self.match("/r/insights/uploads/([^/])+") |
| 135 |
if m: |
| 136 |
self.send_response(200) |
| 137 |
self.end_headers() |
| 138 |
self.wfile.write(b'{ "reports": [ "foo", "bar" ] }\n') |
| 139 |
return |
| 140 |
|
| 141 |
m = self.match("/r/insights/platform/ingress/v1/upload") |
| 142 |
if m: |
| 143 |
# The metadata of the system is in the multipart MIME data |
| 144 |
# sent by the system for the upload; to pick it and use it |
| 145 |
# we need to unpack the multipart MIME data. |
| 146 |
|
| 147 |
# First, create prologue to the data, so it can be recognized |
| 148 |
# as multipart MIME. |
| 149 |
multipart_data = ( |
| 150 |
b"""MIME-Version: 1.0 |
| 151 |
Content-Type: """ |
| 152 |
+ self.headers.get("content-type").encode("utf-8") |
| 153 |
+ b""" |
| 154 |
|
| 155 |
""" |
| 156 |
) |
| 157 |
multipart_data += data |
| 158 |
|
| 159 |
# Parse the multipart MIME data, and then look for a "metadata" |
| 160 |
# file part which contains the system metadata as JSON. |
| 161 |
message = email.message_from_bytes(multipart_data) |
| 162 |
for part in message.walk(): |
| 163 |
if part.get_filename() == "metadata": |
| 164 |
s = json.loads(part.get_payload()) |
| 165 |
s["id"] = "123-nice-id" |
| 166 |
print(s) |
| 167 |
systems[s["id"]] = s |
| 168 |
|
| 169 |
self.send_response(202) |
| 170 |
self.end_headers() |
| 171 |
self.wfile.write( |
| 172 |
b"{" |
| 173 |
b' "request_id": "some-upload", ' |
| 174 |
b' "upload": { ' |
| 175 |
b' "account": "123456", ' |
| 176 |
b' "org_id": "123456" ' |
| 177 |
b" }" |
| 178 |
b"}\n" |
| 179 |
) |
| 180 |
return |
| 181 |
|
| 182 |
self.send_response(400) |
| 183 |
self.end_headers() |
| 184 |
return |
| 185 |
|
| 186 |
self.send_response(404) |
| 187 |
self.end_headers() |
| 188 |
|
| 189 |
def do_DELETE(self): |
| 190 |
|
| 191 |
m = self.match("/r/insights/v1/systems/([^/]+)") |
| 192 |
if m: |
| 193 |
machine_id = m[1] |
| 194 |
self.send_response(200) |
| 195 |
self.end_headers() |
| 196 |
systems.pop(machine_id, None) |
| 197 |
return |
| 198 |
|
| 199 |
m = self.match("/r/insights/platform/inventory/v1/hosts/([^/]+)") |
| 200 |
if m: |
| 201 |
inventory_id = m[1] |
| 202 |
if inventory_id in systems: |
| 203 |
del systems[inventory_id] |
| 204 |
self.send_response(200) |
| 205 |
self.end_headers() |
| 206 |
return |
| 207 |
self.send_response(404) |
| 208 |
self.end_headers() |
| 209 |
return |
| 210 |
|
| 211 |
self.send_response(404) |
| 212 |
self.end_headers() |
| 213 |
|
| 214 |
|
| 215 |
def insights_server(port): |
| 216 |
# Let's put the certs into /var/lib/insights so that SELinux |
| 217 |
# allows insights-client to actually read ca.crt when running as a |
| 218 |
# systemd service. |
| 219 |
certdir = "/var/lib/insights/mock-certs" |
| 220 |
if not os.path.exists(certdir): |
| 221 |
os.makedirs(certdir) |
| 222 |
subprocess.check_call(["sscg"], cwd=certdir) |
| 223 |
|
| 224 |
httpd = HTTPServer(('', port), handler) |
| 225 |
ctx = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) |
| 226 |
ctx.check_hostname = False |
| 227 |
ctx.verify_mode = ssl.CERT_OPTIONAL |
| 228 |
# with newer Pythons this is ctx.minimum_version = ssl.TLSVersion.TLSv1_2 |
| 229 |
ctx.options |= ssl.OP_NO_TLSv1 | ssl.OP_NO_TLSv1_1 |
| 230 |
ctx.load_cert_chain(f'{certdir}/service.pem', f'{certdir}/service-key.pem') |
| 231 |
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True) |
| 232 |
httpd.serve_forever() |
| 233 |
|
| 234 |
|
| 235 |
insights_server(8443) |