def get(self, key): self.logger.debug("Fetching key %s", key) try: result = self.etcd.get(self._absolute_key(key)) except EtcdException: self.logger.exception("Error fetching key %s", key) raise CSStoreError('Error occurred while trying to get key') self.logger.debug("Fetched key %s got result: %r", key, result) return result.value
def span(self, key): path = self._absolute_key(key) self.logger.debug("Creating directory %s", path) try: self.etcd.write(path, None, dir=True, prevExist=False) except EtcdAlreadyExist as err: raise CSStoreExists(str(err)) except EtcdException: self.logger.exception("Error storing key %s", key) raise CSStoreError('Error occurred while trying to store key')
def get(self, key): value = super(EncryptedStore, self).get(key) if value is None: return None try: jwe = JWE() jwe.deserialize(value, self.mkey) return jwe.payload.decode('utf-8') except Exception: self.logger.exception("Error parsing key %s", key) raise CSStoreError('Error occurred while trying to parse key')
def get(self, key): value = self.store.get(key) if value is None: return None try: jwe = JWE() jwe.deserialize(value, self.mkey) return jwe.payload.decode('utf-8') except Exception as err: self.logger.error("Error parsing key %s: [%r]" % (key, repr(err))) raise CSStoreError('Error occurred while trying to parse key')
def set(self, key, value, replace=False): self.logger.debug("Setting key %s to value %s (replace=%s)", key, value, replace) path = self._absolute_key(key) try: self.etcd.write(path, value, prevExist=replace) except EtcdAlreadyExist as err: raise CSStoreExists(str(err)) except EtcdException: self.logger.exception("Error storing key %s", key) raise CSStoreError('Error occurred while trying to store key')
def cut(self, key): self.logger.debug("Removing key %s", key) try: self.etcd.delete(self._absolute_key(key)) except EtcdKeyNotFound: self.logger.debug("Key %s not found", key) return False except EtcdException: self.logger.exception("Error removing key %s", key) raise CSStoreError('Error occurred while trying to cut key') self.logger.debug("Key %s removed", key) return True
def cut(self, key): query = "DELETE from %s WHERE key=?" % self.table try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() r = c.execute(query, (key, )) except sqlite3.Error as err: log_error("Error removing key %s: [%r]" % (key, repr(err))) raise CSStoreError('Error occurred while trying to cut key') if r.rowcount > 0: return True return False
def get(self, key): query = "SELECT value from %s WHERE key=?" % self.table try: conn = sqlite3.connect(self.dburi) c = conn.cursor() r = c.execute(query, (key, )) value = r.fetchall() except sqlite3.Error as err: log_error("Error fetching key %s: [%r]" % (key, repr(err))) raise CSStoreError('Error occurred while trying to get key') if len(value) > 0: return value[0][0] else: return None
def span(self, key): name = key.rstrip('/') self.logger.debug("Creating container %s", name) query = "INSERT into %s VALUES (?, '')" setdata = query % (self.table, ) try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() self._create(c) c.execute(setdata, (name, )) except sqlite3.IntegrityError as err: raise CSStoreExists(str(err)) except sqlite3.Error: self.logger.exception("Error creating key %s", name) raise CSStoreError('Error occurred while trying to span container')
def get(self, key): self.logger.debug("Fetching key %s", key) query = "SELECT value from %s WHERE key=?" % self.table try: conn = sqlite3.connect(self.dburi) c = conn.cursor() r = c.execute(query, (key, )) value = r.fetchall() except sqlite3.Error: self.logger.exception("Error fetching key %s", key) raise CSStoreError('Error occurred while trying to get key') self.logger.debug("Fetched key %s got result: %r", key, value) if len(value) > 0: return value[0][0] else: return None
def cut(self, key): self.logger.debug("Removing key %s", key) query = "DELETE from %s WHERE key=?" % self.table try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() r = c.execute(query, (key, )) except sqlite3.Error: self.logger.error("Error removing key %s", key) raise CSStoreError('Error occurred while trying to cut key') self.logger.debug("Key %s %s", key, "removed" if r.rowcount > 0 else "not found") if r.rowcount > 0: return True return False
def set(self, key, value, replace=False): if replace: query = "INSERT OR REPLACE into %s VALUES (?, ?)" else: query = "INSERT into %s VALUES (?, ?)" setdata = query % (self.table, ) try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() self._create(c) c.execute(setdata, (key, value)) except sqlite3.IntegrityError as err: raise CSStoreExists(str(err)) except sqlite3.Error as err: log_error("Error storing key %s: [%r]" % (key, repr(err))) raise CSStoreError('Error occurred while trying to store key')
def __init__(self, config): super(EtcdStore, self).__init__(config) self.server = config.get('etcd_server', '127.0.0.1') self.port = int(config.get('etcd_port', 4001)) self.namespace = config.get('namespace', "/custodia") # Initialize the DB by trying to create the default table try: self.etcd = Client(self.server, self.port) self.etcd.write(self.namespace, None, dir=True) except EtcdNotFile: # Already exists pass except EtcdException: self.logger.exception("Error creating namespace %s", self.namespace) raise CSStoreError('Error occurred while trying to init db')
def list(self, keyfilter='/'): search = "SELECT key FROM %s WHERE key LIKE ?" % self.table key = "%s%%" % (keyfilter, ) try: conn = sqlite3.connect(self.dburi) r = conn.execute(search, (key, )) rows = r.fetchall() except sqlite3.Error as err: log_error("Error listing (filter: %s): [%r]" % (key, repr(err))) raise CSStoreError('Error occurred while trying to list keys') if len(rows) > 0: value = list() for row in rows: value.append(row[0]) return sorted(value) else: return None
def __init__(self, config): if 'dburi' not in config: raise ValueError('Missing "dburi" for Sqlite Store') self.dburi = config['dburi'] if 'table' in config: self.table = config['table'] else: self.table = "CustodiaSecrets" # Initialize the DB by trying to create the default table try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() self._create(c) except sqlite3.Error as err: log_error("Error creating table %s: [%r]" % (self.table, repr(err))) raise CSStoreError('Error occurred while trying to init db')
def __init__(self, config): super(SqliteStore, self).__init__(config) if 'dburi' not in config: raise ValueError('Missing "dburi" for Sqlite Store') self.dburi = config['dburi'] if 'table' in config: self.table = config['table'] else: self.table = "CustodiaSecrets" # Initialize the DB by trying to create the default table try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() self._create(c) except sqlite3.Error: self.logger.exception("Error creating table %s", self.table) raise CSStoreError('Error occurred while trying to init db')
def set(self, key, value, replace=False): self.logger.debug("Setting key %s to value %s (replace=%s)", key, value, replace) if key.endswith('/'): raise ValueError('Invalid Key name, cannot end in "/"') if replace: query = "INSERT OR REPLACE into %s VALUES (?, ?)" else: query = "INSERT into %s VALUES (?, ?)" setdata = query % (self.table, ) try: conn = sqlite3.connect(self.dburi) with conn: c = conn.cursor() self._create(c) c.execute(setdata, (key, value)) except sqlite3.IntegrityError as err: raise CSStoreExists(str(err)) except sqlite3.Error as err: self.logger.exception("Error storing key %s", key) raise CSStoreError('Error occurred while trying to store key')
def list(self, keyfilter='/'): path = self._absolute_key(keyfilter) if path != '/': path = path.rstrip('/') self.logger.debug("Listing keys matching %s", path) try: result = self.etcd.read(path, recursive=True) except EtcdKeyNotFound: return None except EtcdException: self.logger.exception("Error listing %s", keyfilter) raise CSStoreError('Error occurred while trying to list keys') self.logger.debug("Searched for %s got result: %r", path, result) value = set() for entry in result.get_subtree(): if entry.key == path: continue name = entry.key[len(path):] if entry.dir and not name.endswith('/'): name += '/' value.add(name.lstrip('/')) return sorted(value)
def list(self, keyfilter=''): path = keyfilter.rstrip('/') self.logger.debug("Listing keys matching %s", path) child_prefix = path if path == '' else path + '/' search = "SELECT key FROM %s WHERE key LIKE ?" % self.table key = "%s%%" % (path, ) try: conn = sqlite3.connect(self.dburi) r = conn.execute(search, (key, )) rows = r.fetchall() except sqlite3.Error: self.logger.exception("Error listing %s: [%r]", keyfilter) raise CSStoreError('Error occurred while trying to list keys') self.logger.debug("Searched for %s got result: %r", path, rows) if len(rows) > 0: parent_exists = False value = list() for row in rows: if row[0] == path or row[0] == child_prefix: parent_exists = True continue if not row[0].startswith(child_prefix): continue value.append(row[0][len(child_prefix):].lstrip('/')) if value: self.logger.debug("Returning sorted values %r", value) return sorted(value) elif parent_exists: self.logger.debug("Returning empty list") return [] elif keyfilter == '': self.logger.debug("Returning empty list") return [] self.logger.debug("Returning 'Not Found'") return None