summaryrefslogtreecommitdiffstats
path: root/custodia/store
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2015-03-25 15:18:34 -0400
committerSimo Sorce <simo@redhat.com>2015-03-25 15:19:35 -0400
commit98085f982e98466b994c033be55047ce370dcac5 (patch)
tree12660a599e5bcd95619b9bafe45573d6b2c0ffdc /custodia/store
parent136e1ae76a79ada048a5eb5808b40b8969c7aaf2 (diff)
downloadcustodia-98085f982e98466b994c033be55047ce370dcac5.tar.gz
custodia-98085f982e98466b994c033be55047ce370dcac5.tar.xz
custodia-98085f982e98466b994c033be55047ce370dcac5.zip
Initial sinple store infrastructure
Diffstat (limited to 'custodia/store')
-rw-r--r--custodia/store/__init__.py0
-rw-r--r--custodia/store/interface.py17
-rw-r--r--custodia/store/sqlite.py62
3 files changed, 79 insertions, 0 deletions
diff --git a/custodia/store/__init__.py b/custodia/store/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/custodia/store/__init__.py
diff --git a/custodia/store/interface.py b/custodia/store/interface.py
new file mode 100644
index 0000000..c804670
--- /dev/null
+++ b/custodia/store/interface.py
@@ -0,0 +1,17 @@
+# Copyright (C) 2015 Custodia Project Contributors - see LICENSE file
+
+
+class CSStoreError(Exception):
+ pass
+
+
+class CSStore(object):
+
+ def get(self, key):
+ raise NotImplementedError
+
+ def set(self, key, value):
+ raise NotImplementedError
+
+ def list(self, keyfilter=None):
+ raise NotImplementedError
diff --git a/custodia/store/sqlite.py b/custodia/store/sqlite.py
new file mode 100644
index 0000000..f5908d0
--- /dev/null
+++ b/custodia/store/sqlite.py
@@ -0,0 +1,62 @@
+# Copyright (C) 2015 Custodia Project Contributors - see LICENSE file
+
+from custodia.store.interface import CSStore, CSStoreError
+import os
+import sqlite3
+import sys
+
+
+def log_error(error):
+ print >> sys.stderr, error
+
+
+class SqliteStore(CSStore):
+
+ 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"
+
+ 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')
+ return value
+
+ def _create(self, cur):
+ create = "CREATE TABLE IF NOT EXISTS %s (key, value)" % self.table
+ cur.execute(create)
+
+ def set(self, key, value):
+ setdata = "INSERT OR REPLACE into %s VALUES (?, ?)" % self.table
+ try:
+ conn = sqlite3.connect(self.dburi)
+ with conn:
+ c = conn.cursor()
+ self._create(c)
+ c.execute(setdata, (key, value))
+ 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 list(self, keyfilter='/'):
+ search = "SELECT * FROM %s WHERE key LIKE ?" % self.table
+ key = os.path.join(keyfilter, '%')
+ try:
+ conn = sqlite3.connect(self.dburi)
+ r = conn.execute(search, (key,))
+ value = 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')
+ return value