def republish_entries(): # Republish given entries to a given routing key current_app.logger.info("Republishing entries") payload = request.get_json() if 'entries' not in payload or not payload['entries']: raise ApplicationError( "Payload must contain 'entries' with a list of entries to republish", "RP400", 400) if 'routing_key' not in payload or not payload['routing_key']: raise ApplicationError( "Payload must contain 'routing_key' with a routing key to publish using", "RP400", 400) current_app.logger.info("Republishing entries '%s'", payload['entries']) result = {"republished_entries": [], "entries_not_found": []} for entry_number in payload['entries']: if republish_entry(entry_number, payload['routing_key']): result['republished_entries'].append(entry_number) else: result['entries_not_found'].append(entry_number) if result['entries_not_found']: status = 404 else: status = 200 current_app.logger.info("Republish finished '%s'", result) return Response(json.dumps(result), status=status, mimetype='application/json')
def consistency_proof(self, tree_1_size, tree_2_size): if tree_1_size <= 0: raise ApplicationError( "Cannot calculate consistency for a tree size of 0", "E103") if tree_2_size < tree_1_size: raise ApplicationError( "Cannot calculate consistency where tree 2 is smaller than tree 1", "E104") return self._sub_consistency_proof(tree_1_size, tree_2_size, 0, True)
def get_entry_proof(entry_number, total_entries, proof_identifier): current_app.logger.info("Get entry proof for entry %s of %s", str(entry_number), str()) if proof_identifier != 'merkle:sha-256': current_app.logger.warning("Invalid proof identifier supplied: %s", proof_identifier) raise ApplicationError("Invalid proof identifier", "E400", 400) cursor = start() try: mtree_data = MerkleData(cursor) mtree = MerkleTree(mtree_data) path = mtree.entry_proof(int(entry_number), int(total_entries)) result = { "proof-identifier": "merkle:sha-256", "entry-number": entry_number, "merkle-audit-path": [] } for item in path: current_app.logger.debug("{}".format(type(item))) result['merkle-audit-path'].append("sha-256:" + b16encode(item).decode()) current_app.logger.info("Return entry proof (path length %d)", len(result['merkle-audit-path'])) return Response(json.dumps(result), mimetype='application/json') finally: commit(cursor)
def get_consistency_proof(total_entries_1, total_entries_2, proof_identifier): current_app.logger.info("Get consistency proof for trees with sizes %s and %s", str(total_entries_1), str(total_entries_2)) if proof_identifier != 'merkle:sha-256': current_app.logger.warning("Invalid proof identifier supplied: %s", proof_identifier) raise ApplicationError("Invalid proof identifier", "E400", 400) cursor = start() try: mtree_data = MerkleData(cursor) mtree = MerkleTree(mtree_data) nodes = mtree.consistency_proof(int(total_entries_1), int(total_entries_2)) result = { "proof-identifier": "merkle:sha-256", "merkle-consistency-nodes": [] } for item in nodes: current_app.logger.debug("{}".format(type(item))) result['merkle-consistency-nodes'].append("sha-256:" + b16encode(item).decode()) current_app.logger.info("Return consistency proof (%d nodes)", len(result['merkle-consistency-nodes'])) return Response(json.dumps(result), mimetype='application/json') finally: commit(cursor)
def get_record(field_value): current_app.logger.info("Get record by field value %s", field_value) result = read_record_by_field_value(field_value) if result is None: current_app.logger.warning("Record with field value %s not found", field_value) raise ApplicationError("Not found", "E404", 404) current_app.logger.info("Return record") return Response(json.dumps(result), mimetype='application/json')
def get_entry(entry_number): current_app.logger.info("Get entry %s", entry_number) register_entry = read_entry(entry_number) if register_entry is None: current_app.logger.warning("Entry %s not found", entry_number) raise ApplicationError("Not found", "E404", 404) current_app.logger.info("Returning entry") return Response(json.dumps(register_entry), mimetype='application/json')
def add_headers(self, headers=None): if self.count is None: raise ApplicationError("Count not set on PaginatedResource", "WGAF") if headers is None: headers = {} headers['Link'] = self._get_link_header() return headers
def _k(self, n): if n <= 1: raise ApplicationError( "Cannot calculate k for values of 1 or less", "E102") s = 1 while s < n: s <<= 1 return s >> 1
def get_item_entries(item_hash): app.logger.info("Get item entries %s", item_hash) item_data = read_item_entries(item_hash) if item_data is None: app.logger.warning("Item %s not found", item_hash) raise ApplicationError("Not found", "E404", 404) app.logger.info("Returning item entries") return Response(json.dumps(item_data), status=200, mimetype='application/json')
def get_current_timestamp(): cur = None conn = None try: conn = psycopg2.connect(current_app.config['SQLALCHEMY_DATABASE_URI']) cur = conn.cursor() cur.execute("SELECT CURRENT_TIMESTAMP;") result = cur.fetchone() return result[0] except psycopg2.DataError as e: raise ApplicationError('Input data error: ' + str(e), 'DB', http_code=400) except (psycopg2.OperationalError, psycopg2.ProgrammingError) as e: raise ApplicationError('Database error: ' + str(e), 'DB', http_code=400) finally: if cur: cur.close() if conn: conn.close()
def get_register_proof(proof_identifier): current_app.logger.info("Get register proof") if proof_identifier != 'merkle:sha-256': current_app.logger.warning("Invalid proof identifier supplied: %s", proof_identifier) raise ApplicationError("Invalid proof identifier", "E400", 400) cursor = start() try: mtree_data = MerkleData(cursor) mtree = MerkleTree(mtree_data) result = { "proof-identifier": "merkle:sha-256", "tree-size": mtree.tree_size(), "timestamp": datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f'), "root-hash": "sha-256:" + b16encode(mtree.root_hash()).decode(), "tree-head-signature": "TODO" # TODO(signature) } current_app.logger.info("Returning register proof") return Response(json.dumps(result), mimetype='application/json') finally: commit(cursor)
def test_health_cascade_no_postgres(self, requests, pg): pg.get_current_timestamp.side_effect = ApplicationError("blah", 400) response = self.app.get('/health/cascade/4') self.assertEqual(response.status_code, 500)
def get_items(): # pragma: no cover # TODO(Spec is unclear... all items, or latest versions of each? Not in reference implementation.) current_app.logger.warning("Get items is not implemented") raise ApplicationError("Not implemented", "E501", 501)
def get_record_proof(total_entries, field_value, proof_identifier): # pragma: no cover # Marked as experimental in the spec and not implemented in the reference implementation current_app.logger.warning("Get record proof is not implemented") raise ApplicationError("Not implemented", "E501", 501)