How to use the synapse.lib.msgpack.un function in synapse

To help you get started, we’ve selected a few synapse examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github vertexproject / synapse / synapse / lib / crypto / tinfoil.py View on Github external
def dec(self, byts):
        '''
        Decode an envelope dict and decrypt the given bytes.

        Args:
            byts (bytes): Bytes to decrypt.

        Returns:
            bytes: Decrypted message.
        '''
        envl = s_msgpack.un(byts)
        iv = envl.get('iv', b'')
        asscd = envl.get('asscd', b'')
        data = envl.get('data', b'')

        decryptor = AESGCM(self.ekey)

        try:
            data = decryptor.decrypt(iv, data, asscd)
        except Exception:
            logger.exception('Error decrypting data')
            return None
        return data
github vertexproject / synapse / synapse / neuron.py View on Github external
def decrypt(self, ciphertext):

        plaintext = self._rx_tinh.dec(ciphertext)
        if plaintext is None:
            logger.error('Message decryption failure')
            raise s_exc.CryptoErr(mesg='Message decryption failure')

        seqn = next(self._rx_sn)

        sn, mesg = s_msgpack.un(plaintext)
        if sn != seqn:
            logger.error('Message out of sequence: got %d expected %d', sn, seqn)
            raise s_exc.CryptoErr(mesg='Message out of sequence', expected=seqn, got=sn)

        return mesg
github vertexproject / synapse / synapse / lib / lmdblayer.py View on Github external
tagprop = f'#{tag}:{prop}'

        abrv_p = self.getNameAbrv(prop)
        abrv_tp = self.getNameAbrv(tagprop)
        abrv_ftp = self.getNameAbrv(f'{form}{tagprop}')

        bpkey = buid + tagprop.encode()

        curb = self.layrslab.pop(bpkey, db=self.bybuid)

        # this *should* be completely impossible
        if curb is None: # pragma: no cover
            logger.warning('_storTagPropDel has no current value!')
            return

        curv, curi = s_msgpack.un(curb)

        self.layrslab.delete(abrv_p + curi, val=buid, db=self.by_tp_pi)
        self.layrslab.delete(abrv_tp + curi, val=buid, db=self.by_tp_tpi)
        self.layrslab.delete(abrv_ftp + curi, val=buid, db=self.by_tp_ftpi)

        self._popBuidCache(buid, tagprop)
github vertexproject / synapse / synapse / lib / lmdblayer.py View on Github external
if prop:
            bpkey = buid + prop.encode()
        else:
            bpkey = buid + b'*' + form.encode()
            # we are deleting the primary property. wipe data.
            self._wipeNodeData(buid)

        univ = info.get('univ')

        byts = self.layrslab.pop(bpkey, db=self.bybuid)
        if byts is None:
            return

        del self.buidcache[buid]

        oldv, oldi = s_msgpack.un(byts)

        pvvalu = s_msgpack.en((buid,))

        if oldi is not None:

            if isinstance(oldi, bytes):

                self.layrslab.delete(fenc + penc + oldi, pvvalu, db=self.byprop)

                if univ:
                    self.layrslab.delete(penc + oldi, pvvalu, db=self.byuniv)

            else:
                for oldibyts in oldi:

                    self.layrslab.delete(fenc + penc + oldibyts, pvvalu, db=self.byprop)
github vertexproject / synapse / synapse / lib / lmdblayer.py View on Github external
async def iterUnivRows(self, prop):
        '''
        Iterate (buid, valu) rows for the given universal prop
        '''
        penc = prop.encode()
        pref = penc + b'\x00'

        for _, pval in self.layrslab.scanByPref(pref, db=self.byuniv):
            buid = s_msgpack.un(pval)[0]

            byts = self.layrslab.get(buid + penc, db=self.bybuid)
            if byts is None:
                continue

            valu, indx = s_msgpack.un(byts)

            yield buid, valu
github vertexproject / synapse / synapse / tools / loadrows.py View on Github external
storconf = {'rev:storage': False}
    if opts.revstorage:  # pragma: no cover
        storconf['rev:storage'] = True

    with open(opts.input, 'rb') as fd:
        gen = s_msgpack.iterfd(fd)
        if discard_first_event:
            next(gen)
        with s_cortex.openstore(opts.store, storconf=storconf) as store:
            outp.printf('Starting row level restore')
            tick = time.time()
            i = 0
            nrows = 0
            for event in gen:
                if decompress and 'rows' in event[1]:
                    event[1]['rows'] = s_msgpack.un(gzip.decompress(event[1].get('rows')))
                i += 1
                if i % 250 == 0:
                    outp.printf('Loaded {} events'.format(i))
                store.loadbus.dist(event)
                _nrows = len(event[1].get('rows', ()))
                nrows += _nrows
                if _nrows and i % 10 == 0:
                    logger.debug('Loaded %s rows', nrows)

            tock = time.time()
            outp.printf('Done loading events - took {} seconds.'.format(tock - tick))
    outp.printf('Fin')
    return 0
github vertexproject / synapse / synapse / lib / lmdblayer.py View on Github external
def _storPropSetCommon(self, buid, penc, bpkey, pvpref, univ, valu, indx):

        bpval = s_msgpack.en((valu, indx))
        pvvalu = s_msgpack.en((buid,))

        byts = self.layrslab.replace(bpkey, bpval, db=self.bybuid)
        if byts is not None:

            oldv, oldi = s_msgpack.un(byts)
            if oldi is not None:

                if isinstance(oldi, bytes):

                    self.layrslab.delete(pvpref + oldi, pvvalu, db=self.byprop)
                    if univ:
                        self.layrslab.delete(penc + oldi, pvvalu, db=self.byuniv)

                else:
                    for oldibyts in oldi:
                        self.layrslab.delete(pvpref + oldibyts, pvvalu, db=self.byprop)
                        if univ:
                            self.layrslab.delete(penc + oldibyts, pvvalu, db=self.byuniv)

        if indx is not None:
github vertexproject / synapse / synapse / lib / provenance.py View on Github external
def getProvStack(self, iden: bytes):
        '''
        Returns the provenance stack given the iden to it
        '''
        retn = self.slab.get(iden, db=self.db)
        if retn is None:
            return None

        return s_msgpack.un(retn)
github vertexproject / synapse / synapse / lib / slabseqn.py View on Github external
def last(self):

        last = self.slab.last(db=self.db)
        if last is None:
            return None

        lkey, lval = last

        indx = s_common.int64un(lkey)
        return indx, s_msgpack.un(lval)
github vertexproject / synapse / synapse / cores / lmdb.py View on Github external
def _getRowByPkValEnc(self, txn, pk_enc):
        row = txn.get(pk_enc, db=self.rows)
        if row is None:
            raise s_common.BadCoreStore(store='lmdb', mesg='Index val has no corresponding row')
        return s_msgpack.un(row)