Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.28.36] cockpit: code fix and CI fixes from subscription-manager-cockpit #3445

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletions cockpit/src/insights.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -368,17 +368,15 @@ function jump_to_timer() {
function monitor_last_upload() {
let self = {
timestamp: 0,
results: null,

close: close
};

cockpit.event_target(self);

let results_file = cockpit.file("/etc/insights-client/.last-upload.results", { syntax: JSON });
results_file.watch(data => {
self.results = data;
cockpit.spawn([ "stat", "-c", "%Y", "/etc/insights-client/.last-upload.results" ], { err: "message" })
let results_file = cockpit.file("/etc/insights-client/.lastupload");
results_file.watch(() => {
cockpit.spawn([ "stat", "-c", "%Y", "/etc/insights-client/.lastupload" ], { err: "message" })
.then(ts => {
self.timestamp = parseInt(ts);
self.dispatchEvent("changed");
Expand All @@ -387,7 +385,7 @@ function monitor_last_upload() {
self.timestamp = 0;
self.dispatchEvent("changed");
});
});
}, { read: false });

function close() {
results_file.close();
Expand Down
3 changes: 1 addition & 2 deletions integration-tests/check-subscriptions
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ auto_config=False
auto_update=False
base_url={hostname}:8443/r/insights
cert_verify=/var/lib/insights/mock-certs/ca.crt
username=admin
password=foobar
authmethod=CERT
""")

m.upload(["files/mock-insights"], "/var/tmp")
Expand Down
97 changes: 81 additions & 16 deletions test/files/mock-insights
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# auto_update=False
# base_url=127.0.0.1:8443/r/insights
# cert_verify=/var/lib/insights/mock-certs/ca.crt
# username=admin
# password=foobar
# authmethod=CERT

import email
from http.server import *
import json
import os
Expand Down Expand Up @@ -52,13 +52,14 @@ class handler(BaseHTTPRequestHandler):
m = self.match("/r/insights/v1/systems/([^/]+)")
if m:
machine_id = m[1]
if machine_id not in systems:
self.send_response(404)
self.end_headers()
return
self.send_response(200)
for system in systems.values():
if system["machine_id"] == machine_id:
self.send_response(200)
self.end_headers()
self.wfile.write(json.dumps(system).encode("utf-8") + b"\n")
return
self.send_response(404)
self.end_headers()
self.wfile.write(json.dumps(systems[machine_id]).encode("utf-8") + b"\n")
return

m = self.match("/r/insights/v1/branch_info")
Expand All @@ -70,21 +71,26 @@ class handler(BaseHTTPRequestHandler):

m = self.match("/r/insights/platform/inventory/v1/hosts\\?insights_id=(.*)")
if m:
machine_id = m[1]
insights_id = m[1]
self.send_response(200)
self.end_headers()
if machine_id in systems:
self.wfile.write(b'{ "total": 1, "results": [ { "id": "123-nice-id" } ] }\n')
else:
self.wfile.write(b'{ "total": 0, "results": [ ] }\n')
res = {
"total": 0,
"results": [],
}
for system in systems.values():
if system["insights_id"] == insights_id:
res["total"] += 1
res["results"].append(system)
self.wfile.write(json.dumps(res).encode("utf-8") + b"\n")
return

m = self.match("/r/insights/platform/insights/v1/system/([^/]+)/reports/")
if m:
platform_id = m[1]
inventory_id = m[1]
self.send_response(200)
self.end_headers()
if platform_id == "123-nice-id":
if inventory_id in systems:
self.wfile.write(b'[ { "rule": { "total_risk": 3 } }, { "rule": { "total_risk": 2 } }, { "rule": { "total_risk": 1 } }]\n')
else:
self.wfile.write(b'[ ]\n')
Expand All @@ -102,8 +108,10 @@ class handler(BaseHTTPRequestHandler):
s = json.loads(data)
s["unregistered_at"] = None
s["account_number"] = "123456"
s["insights_id"] = s["machine_id"]
s["id"] = "123-nice-id"
print(s)
systems[s["machine_id"]] = s
systems[s["id"]] = s
self.send_response(200)
self.end_headers()
self.wfile.write(data)
Expand All @@ -116,6 +124,51 @@ class handler(BaseHTTPRequestHandler):
self.wfile.write(b'{ "reports": [ "foo", "bar" ] }\n')
return

m = self.match("/r/insights/platform/ingress/v1/upload")
if m:
# The metadata of the system is in the multipart MIME data
# sent by the system for the upload; to pick it and use it
# we need to unpack the multipart MIME data.

# First, create prologue to the data, so it can be recognized
# as multipart MIME.
multipart_data = (
b"""MIME-Version: 1.0
Content-Type: """
+ self.headers.get("content-type").encode("utf-8")
+ b"""

"""
)
multipart_data += data

# Parse the multipart MIME data, and then look for a "metadata"
# file part which contains the system metadata as JSON.
message = email.message_from_bytes(multipart_data)
for part in message.walk():
if part.get_filename() == "metadata":
s = json.loads(part.get_payload())
s["id"] = "123-nice-id"
print(s)
systems[s["id"]] = s

self.send_response(202)
self.end_headers()
self.wfile.write(
b"{"
b' "request_id": "some-upload", '
b' "upload": { '
b' "account": "123456", '
b' "org_id": "123456" '
b" }"
b"}\n"
)
return

self.send_response(400)
self.end_headers()
return

self.send_response(404)
self.end_headers()

Expand All @@ -130,6 +183,18 @@ class handler(BaseHTTPRequestHandler):
del systems[machine_id]
return

m = self.match("/r/insights/platform/inventory/v1/hosts/([^/]+)")
if m:
inventory_id = m[1]
if inventory_id in systems:
del systems[inventory_id]
self.send_response(200)
self.end_headers()
return
self.send_response(404)
self.end_headers()
return

self.send_response(404)
self.end_headers()

Expand Down
Loading