summaryrefslogtreecommitdiffstats
path: root/custodia/store/sqlite.py
blob: f5908d0b07d8a9e6387f37e68bed1a80b0c4d202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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