|
20 | 20 | """Tool for managing VM templates."""
|
21 | 21 |
|
22 | 22 | import argparse
|
| 23 | +import base64 |
23 | 24 | import collections
|
24 | 25 | import configparser
|
25 | 26 | import datetime
|
|
59 | 60 | LOCK_FILE = '/var/tmp/qvm-template.lck'
|
60 | 61 | DATE_FMT = '%Y-%m-%d %H:%M:%S'
|
61 | 62 | TAR_HEADER_BYTES = 512
|
| 63 | +WRAPPER_PAYLOAD_BEGIN = "###!Q!BEGIN-QUBES-WRAPPER!Q!###" |
| 64 | +WRAPPER_PAYLOAD_END = "###!Q!END-QUBES-WRAPPER!Q!###" |
62 | 65 |
|
63 | 66 | UPDATEVM = str('global UpdateVM')
|
64 | 67 |
|
@@ -502,9 +505,48 @@ def check_newline(string, name):
|
502 | 505 | check_newline(spec, 'template name')
|
503 | 506 | payload += spec + '\n'
|
504 | 507 | payload += '---\n'
|
| 508 | + |
| 509 | + repo_config = "" |
505 | 510 | for path in args.repo_files:
|
506 | 511 | with open(path, 'r', encoding='utf-8') as fd:
|
507 |
| - payload += fd.read() + '\n' |
| 512 | + repo_config += fd.read() + '\n' |
| 513 | + payload += repo_config |
| 514 | + |
| 515 | + # Add base64 encoded files to payload if needed |
| 516 | + def encode_key(path): |
| 517 | + if path.startswith("file://"): |
| 518 | + path = path[7:] |
| 519 | + |
| 520 | + if not path.startswith( |
| 521 | + "/etc/qubes/repo-templates/keys/") or not os.path.isfile(path): |
| 522 | + return "" |
| 523 | + |
| 524 | + encoded_key = "#" + path + "\n" |
| 525 | + with open(path, "rb") as key: |
| 526 | + encoded_key += f"#{base64.b64encode(key.read()).decode('ascii')}\n" |
| 527 | + return encoded_key |
| 528 | + |
| 529 | + def append_keys(payload): |
| 530 | + config = configparser.ConfigParser() |
| 531 | + try: |
| 532 | + config.read_string(payload) |
| 533 | + except RuntimeError: |
| 534 | + return "" |
| 535 | + |
| 536 | + file_list = set() |
| 537 | + for section in config.sections(): |
| 538 | + for option in ["gpgkey", "sslclientcert", "sslclientkey"]: |
| 539 | + if config.has_option(section, option): |
| 540 | + file_list.add(config.get(section, option)) |
| 541 | + |
| 542 | + encoded_keys = "".join(encode_key(file_path) for file_path in file_list) |
| 543 | + if not encoded_keys: |
| 544 | + return "" |
| 545 | + |
| 546 | + return f"\n{WRAPPER_PAYLOAD_BEGIN}\n{encoded_keys}{WRAPPER_PAYLOAD_END}" |
| 547 | + |
| 548 | + payload += append_keys(repo_config) |
| 549 | + |
508 | 550 | return payload
|
509 | 551 |
|
510 | 552 |
|
|
0 commit comments