def _getHash(self, usrFacingUrlPrefix):
     scan_range = Range(srow=usrFacingUrlPrefix, scf=self.__cf, scq="hash",
                        erow=usrFacingUrlPrefix, ecf=self.__cf, ecq="hash")
     for entry in self.__connection.scan(self.__table, scanrange=scan_range):
         return str(entry.val)
     else:
         return None
def get_single_entry(conn, table, row=None, cf=None, cq=None):
    """ Get the single entry with the given key fields (row, column
        family, and column qualifier) from an Accumulo database.
        
        Returns:

        None if no entries were found
        The found entry if exactly one was found
        Raises an assertion error if more than one entry was found
    """

    if cf is None:
        assert cq is None
        cols = None
    elif cq is None:
        cols = [[cf]]
    else:
        cols = [[cf, cq]]

    candidates = conn.scan(table, Range(srow=row, erow=row), cols=cols)

    try:
        first = next(candidates)
    except StopIteration:
        # No candidates; return None
        return None

    try:
        more = next(candidates)
        assert False  # only supposed to have one result
    except StopIteration:
        return first
 def _getNofChunks(self, usrFacingUrlPrefix):
     '''
     Get the number of chunks the static contents is stored
     '''
     scan_range = Range(srow=usrFacingUrlPrefix, scf=self.__cf, scq="nofchunks",
                        erow=usrFacingUrlPrefix, ecf=self.__cf, ecq="nofchunks")
     for entry in self.__connection.scan(self.__table, scanrange=scan_range):
         return int(entry.val)
     return 0
示例#4
0
    def attributes_by_user(self, userid):
        """ Returns a list of the attributes the given user has.

            Arguments:
            userid (string) - the ID of the user whose attributes to retrieve

            Returns:
            [string] - a (potentially empty) list of the attributes that the 
                given user has 
        """
        # Scan the user-to-attribute table for the row for this user
        raw_attrs = self.conn.scan(self.user_attr_table,
                                    Range(srow=userid, erow=userid))

        # Grab the column family (where the attribute is stored) for each entry
        return [entry.cf for entry in raw_attrs]
示例#5
0
    def from_elem(self, elem):
        """ Check if an element is in the database. If so, return an
            embedded node pointing to that element's leaf node in the
            skiplist. If not, return None.
        """

        key = sha256(elem.serialize()).digest()

        candidates = self.conn.scan(self.table, Range(srow=key, erow=key))

        try:
            first = next(candidates)
        except StopIteration:
            # It's not there, so return None
            return None

        # Otherwise, this row is there, so return the node with the right name
        return EmbeddedNode(self, key)
def entry_exists(conn, table, row, cf=None, cq=None):
    """ Check to see if at least one entry with the given information
        exists in the given Accumulo table. Takes at least a row,
        and possibly a column family & qualifier as well.

        Arguments:

        conn : Accumulo - a connection to the Accumulo instance to check
        table : string - the name of the table to check
        row : string - the name of the row to look for within the given table
        cf : optional string - the column family to look for, if desired
        cq : optional string - the column qualifier to look for, if desired

        Returns:

        bool - whether there is at least one entry in the given connection with
               the given fields

        Assumptions:

        - The given table exists on the connection
        - If cf is not specified/None, then neither is cq
    """

    if cf is None:
        assert cq is None
        cols = None
    elif cq is None:
        cols = [[cf]]
    else:
        cols = [[cf, cq]]

    candidates = conn.scan(table, Range(srow=row, erow=row), cols=cols)

    try:
        first = next(candidates)
    except StopIteration:
        # Nothing found
        return False
    else:
        # Something found
        return True
    def _split_entry(self, entry):
        tup = (entry.row, entry.cf, entry.cq, entry.cv, None, entry.val)
        cell_string = str(tup)
        lookup_string = str(tup[:-1])

        sig_entries = [
            x for x in self.conn.scan(self.table,
                                      scanrange=Range(srow=lookup_string,
                                                      erow=lookup_string))
        ]

        assert len(sig_entries) == 1

        sig_entry = sig_entries[0]
        metadata, sig = sig_entry.val.split(',')

        return (SignedEntry(cell_string=cell_string,
                            metadata=metadata,
                            sig=sig), entry
                )  # no metadata in the entry, so just return it
示例#8
0
    def users_by_attribute(self, attr):
        """ Return the list of all users who are currently authorized to
            read data with the given attribute.

            Arguments:

            attr : string - the attribute to query

            Returns:

            users : [string] - a (potentially empty) list of usernames
                    (as strings) that are all authorized to have the given
                    attribute `attr`
        """
        # Scan the attribute-to-user table for the row for this attribute
        raw_users = self.conn.scan(self.attr_user_table,
                                   Range(srow=attr, erow=attr))

        # Grab the column family (where the user is stored) for each entry
        return [entry.cf for entry in raw_users]
示例#9
0
    def get_metadatas(self, user, attr):
        """ Get all metadatas that a given user has for a particular attribute.

            Arguments:

            self - the KeyStore object to delete elements from
            userid : string - the ID of the user whose metadata is
                     being fetched
            attr : string - the attribute whose metadata is being fetched

            Returns:

            metadatas : string set - a set of metadata strings
        """
        # Scan the keywrap metadata table for the metadatas
        # Row: user
        # Col. Fam: attribute
        raw_metas = self.conn.scan(self.meta_table,
                                   Range(srow=user, erow=user),
                                   cols=[[attr]])

        return set([entry.cq for entry in raw_metas])
    def _run_search(self, config, row, cols, correct_cells):
        '''
        Tests the encrypting search functionality
        '''
        #create range & mutation to search for
        mut1 = Mutation('arow')
        mut1.put(cf='cf1', cq='cq1', cv='', ts=1, val='val1')
        mut1.put(cf='cf2', cq='cq2', cv='', ts=2, val='val2')
        mut1.put(cf='cf1', cq='cq1', cv='', ts=3, val='val3')
        mut1.put(cf='cf2', cq='cq3', cv='', ts=4, val='val4')
        mut1.put(cf='cf3', cq='cq4', cv='', ts=5, val='val5')
        mut2 = Mutation('brow')
        mut2.put(cf='cf1', cq='cq1', cv='', ts=6, val='val1')
        mut2.put(cf='cf2', cq='cq2', cv='', ts=7, val='val2')
        ae = AccumuloEncrypt(StringIO(config), self.pki)
        enc_muts1 = ae.encrypt(mut1)
        enc_muts2 = ae.encrypt(mut2)
        enc_row, enc_cols = ae.encrypt_search(row, cols)

        #write mutation along fake connection
        conn = FakeConnection()
        conn.create_table('enc_test')
        for mut in enc_muts1 + enc_muts2:
            conn.write('enc_test', mut)

        #retrieve encrypted mutation with search
        dec_cells = []
        for c in conn.scan('enc_test',
                           scanrange=Range(srow=enc_row,
                                           erow=enc_row,
                                           sinclude=True,
                                           einclude=True),
                           cols=enc_cols):
            dec_cells.append(ae.decrypt(c))

        self.assertEqual(sorted(dec_cells), sorted(correct_cells))
示例#11
0
table = "pythontest"

conn = Accumulo(host=settings.HOST, port=settings.PORT, user=settings.USER, password=settings.PASSWORD)

if conn.table_exists(table):
    conn.delete_table(table)

conn.create_table(table)
wr = conn.create_batch_writer(table)

print "Ingesting some data ..."
for num in range(1, 100):
    label = '%03d'%num
    mut = Mutation('r_%s'%label)
    mut.put(cf='cf_%s'%label, cq='cq1', val='value_%s'%label)
    mut.put(cf='cf_%s'%label, cq='cq2', val='value_%s'%label)
    wr.add_mutation(mut)
wr.close()


print "Rows 001 through 003 ..."
for entry in conn.scan(table, scanrange=Range(srow='r_001', erow='r_003'), cols=[]):
    print entry

print "Rows 001 and 011 ..."
for entry in conn.batch_scan(table, scanranges=[Range(srow='r_001', erow='r_001'), Range(srow='r_011', erow='r_011')]):
    print entry

conn.close()
示例#12
0
    def batch_retrieve(self, userid, metadata, attr=None):
        """ Fetch all of a user's keys at once. Optionally, fetch only their
            keys either for a specified attribute or with no attribute at all.
            
            Arguments:

            self - the KeyStore object being read from
            userid : string - the ID of the user whose keys to fetch
            metadata : string - the metadata of the keys to search for
            attr : optional string - the attribute to search for. Default
                   value: None. If this argument is None, this method should
                   return all of the given user's keys. If this argument is
                   the empty string, it should return that user's non-attribute
                   keys. If this argument is a non-empty string, it should
                   return all of that user's keys for that attribute, including
                   all versions and metadata options.

            Returns:

            [KeyInfo] - a non-empty list of KeyInfo objects (that is, 
            (attr, vers, metadata, keywrap, keylen) tuples). If the attr
            argument was None, the attr field of each tuple will be the
            attribute corresponding to the returned version, metadata, and
            keywrap; otherwise, the attr field of each tuple will be equal to
            the attr argument.

            Raises:

            PKILookupError - if there is no information to be returned
        """

        tabname = metadata

        if not self.conn.table_exists(tabname):
            raise PKILookupError('Error: no such table %s' %tabname)

        if attr is None:
            # Get everything!
            scan_range = Range(srow=userid, erow=userid)
        else:
            # Get only things from the corresponding row
            scan_range = Range(srow=userid, erow=userid, scf=attr, ecf=attr)

        ret = []

        for c in self.conn.scan(tabname, scan_range):
            try:
                vers = int(c.cq)
            except ValueError:
                raise PKILookupException('Retrieved version must be int')

            keywrap, raw_keylen = c.val.rsplit(',', 1)

            try:
                keylen = int(raw_keylen)
            except ValueError:
                raise PKILookupError('Error: found non-integer key length')

            ret.append(KeyInfo(metadata=metadata, attr=c.cf,
                               vers=vers, keywrap=keywrap, keylen=keylen))

        if not ret:
            # If we found no elements, that's an error
            raise PKILookupError(
                'Error: no results found for batch key retrieval')
        else:
            return ret
示例#13
0
from pyaccumulo.proxy.ttypes import IteratorSetting, IteratorScope
from examples.util import hashcode
import hashlib, re
import settings
import sys

conn = Accumulo(host=settings.HOST, port=settings.PORT, user=settings.USER, password=settings.PASSWORD)

table = sys.argv[1]
if not conn.table_exists(table):
    print "Table '%s' does not exist."%table
    sys.exit(1)

search_terms = [term.lower() for term in sys.argv[2:] if len(term) > 3]

if len(search_terms) < 2:
    print "More than one term of length > 3 is required for this example"
    sys.exit(1)

uuids = []
for e in conn.batch_scan(table, scanranges=[Range(srow="s", erow="t")], iterators=[IntersectingIterator(priority=21, terms=search_terms)]):
    uuids.append(e.cq)

if len(uuids) > 0:
    for doc in conn.batch_scan(table, scanranges=[Range(srow=uuid, erow=uuid) for uuid in uuids]):
        print doc.val
else:
    print "No results found"

conn.close()