def aods_append(self): try: with open(self.args.inputfilename, encoding='utf8') as fd: inputdataJSON = fd.read() except (OSError, IOError) as e: logging.error('could not read inputfile, because: %s' %(repr(e))) sys.exit(1) try: appendList = json.loads(inputdataJSON) except Exception as e: raise JSONdecodeError if not isinstance(appendList, list): raise PMPInputRecNoDictError('JSON input file must contain a list of dict') if len(appendList) == 0: raise PMPInputRecNoDictError('JSON input file must contain a non-empty list of dict') if not isinstance(appendList[0], dict): raise PMPInputRecNoDictError('JSON input file: first object in list is not a dict') self.aods = self.aodsFileHandler.readFile() # does validation as well inputRecSeq = 0 for inputDataRaw in appendList: inputRec = InputRecord(inputDataRaw) wrapperRec = WrapperRecord('elements', inputRec, self.args) inputRecSeq += 1 policyDict = self.aods_read(use='internal') # get latest version logging.debug("%d rectype=%s pk=%s" % (inputRecSeq, inputRec.rec.rectype, inputRec.rec.primarykey)) inputRec.validate(policyDict) lastHash = self.aods['AODS'][self.lastSeq][0] logging.debug("%d lastHash: " % inputRecSeq + lastHash) wrapperRec_final = wrapperRec.getRec(self.lastSeq + 1, lastHash) self.aods['AODS'].append(wrapperRec_final) self.aodsFileHandler.save(self.aods, self.args.noxmlsign)
def aods_read(self, use='external') -> dict: ''' read aods from input file and transform into policyDict structure option: output policiy directory or journal in various formats ''' if not hasattr(self, 'aods'): self.aods = self.aodsFileHandler.readFile() if self.aods['AODS'][0][3][0] != 'header': raise ValidationError('Cannot locate aods header record') policyDict = {"domain": {}, "issuer": {}, "organization": {}, "revocation": {}, "userprivilege": {}} if use == 'external' and getattr(self.args, 'journal', False): dump_journal_fd = open(self.args.journal, 'w') dump_journal_fd.write('[\n') for w in self.aods['AODS']: if use == 'external' and getattr(self.args, 'journal', False): dump_journal_fd.write(json.dumps(w) + '\n') wrap = WrapperRecord('rawStruct', w, self.args) rec = ContentRecord(wrap.record) self.prevHash = self.lastHash self.lastHash = wrap.hash self.lastSeq = wrap.seq if rec.rectype == 'header': continue if wrap.validateWrap(self.prevHash) != True: raise HashChainError('AODS hash chain is broken -> data not trustworthy, revert to previous version') self.write_entry_into_policy_dict(policyDict, rec, wrap.deleteflag) if use == 'external': # avoid dumps for each append iteration if use == 'external' and getattr(self.args, 'journal', False): dump_journal_fd.write(']\n') dump_journal_fd.close() self.dump_poldir(policyDict) return policyDict
def aods_create(self): inputDataRaw = {"record": ["header", "", "columns: hash, seq, delete, [rectype, pk, a1, a2, ..], " "datetimestamp, registrant, submitter]" ], "delete": False} inputRec = InputRecord(inputDataRaw) wrapperRec = WrapperRecord('elements', inputRec, self.args) seedVal_str = str(datetime.now()) seedVal_bytes = base64.b64encode(hashlib.sha256(seedVal_str.encode('ascii')).digest()) if self.args.debug: seedVal_bytes = 'fixedValueForDebugOnly'.encode('ascii') logging.debug("0 seedVal: " + seedVal_bytes.decode('ascii')) self.aodsFileHandler.create({"AODS": [wrapperRec.getRec(0, seedVal_bytes.decode('ascii'))]}, self.args.noxmlsign)