(self, to, privatekey=None, inner_path="content.json", remove_missing_optional=False, update_changed_files=False, response_ok=True)
| 399 | |
| 400 | # Sign content.json |
| 401 | def actionSiteSign(self, to, privatekey=None, inner_path="content.json", remove_missing_optional=False, update_changed_files=False, response_ok=True): |
| 402 | self.log.debug("Signing: %s" % inner_path) |
| 403 | site = self.site |
| 404 | extend = {} # Extended info for signing |
| 405 | |
| 406 | # Change to the file's content.json |
| 407 | file_info = site.content_manager.getFileInfo(inner_path) |
| 408 | if not inner_path.endswith("content.json"): |
| 409 | if not file_info: |
| 410 | raise Exception("Invalid content.json file: %s" % inner_path) |
| 411 | inner_path = file_info["content_inner_path"] |
| 412 | |
| 413 | # Add certificate to user files |
| 414 | is_user_content = file_info and ("cert_signers" in file_info or "cert_signers_pattern" in file_info) |
| 415 | if is_user_content and privatekey is None: |
| 416 | cert = self.user.getCert(self.site.address) |
| 417 | extend["cert_auth_type"] = cert["auth_type"] |
| 418 | extend["cert_user_id"] = self.user.getCertUserId(site.address) |
| 419 | extend["cert_sign"] = cert["cert_sign"] |
| 420 | self.log.debug("Extending content.json with cert %s" % extend["cert_user_id"]) |
| 421 | |
| 422 | if not self.hasFilePermission(inner_path): |
| 423 | self.log.error("SiteSign error: you don't own this site & site owner doesn't allow you to do so.") |
| 424 | return self.response(to, {"error": "Forbidden, you can only modify your own sites"}) |
| 425 | |
| 426 | if privatekey == "stored": # Get privatekey from sites.json |
| 427 | privatekey = self.user.getSiteData(self.site.address).get("privatekey") |
| 428 | if not privatekey: |
| 429 | self.cmd("notification", ["error", _["Content signing failed"] + "<br><small>Private key not found in sites.json </small>"]) |
| 430 | self.response(to, {"error": "Site sign failed: Private key not stored."}) |
| 431 | self.log.error("Site sign failed: %s: Private key not stored in sites.json" % inner_path) |
| 432 | return |
| 433 | if not privatekey: # Get privatekey from users.json auth_address |
| 434 | privatekey = self.user.getAuthPrivatekey(self.site.address) |
| 435 | |
| 436 | # Signing |
| 437 | # Reload content.json, ignore errors to make it up-to-date |
| 438 | site.content_manager.loadContent(inner_path, add_bad_files=False, force=True) |
| 439 | # Sign using private key sent by user |
| 440 | try: |
| 441 | site.content_manager.sign(inner_path, privatekey, extend=extend, update_changed_files=update_changed_files, remove_missing_optional=remove_missing_optional) |
| 442 | except (VerifyError, SignError) as err: |
| 443 | self.cmd("notification", ["error", _["Content signing failed"] + "<br><small>%s</small>" % err]) |
| 444 | self.response(to, {"error": "Site sign failed: %s" % err}) |
| 445 | self.log.error("Site sign failed: %s: %s" % (inner_path, Debug.formatException(err))) |
| 446 | return |
| 447 | except Exception as err: |
| 448 | self.cmd("notification", ["error", _["Content signing error"] + "<br><small>%s</small>" % Debug.formatException(err)]) |
| 449 | self.response(to, {"error": "Site sign error: %s" % Debug.formatException(err)}) |
| 450 | self.log.error("Site sign error: %s: %s" % (inner_path, Debug.formatException(err))) |
| 451 | return |
| 452 | |
| 453 | site.content_manager.loadContent(inner_path, add_bad_files=False) # Load new content.json, ignore errors |
| 454 | |
| 455 | if update_changed_files: |
| 456 | self.site.updateWebsocket(file_done=inner_path) |
| 457 | |
| 458 | if response_ok: |
no test coverage detected