Commit edfe5b61 authored by Vincent Pelletier's avatar Vincent Pelletier

storage: Factorise self._table_prefix application.

Also, this provides a handy location to log all queries when debugging.
Also, some minor cleanups.
parent 9c772060
......@@ -142,12 +142,20 @@ class SQLite3Storage(local):
CREATE TABLE IF NOT EXISTS %(prefix)sconfig_once (
name TEXT PRIMARY KEY,
value TEXT
)
);
''' % {
'prefix': table_prefix,
'key_id_constraint': 'UNIQUE' if enforce_unique_key_id else '',
})
def _execute(self, cursor, sql, parameters=()):
return cursor.execute(
sql % {
'prefix': self._table_prefix,
},
parameters,
)
def _incrementCounter(self, name, increment=1, initial=0):
"""
Increment counter with <name> by <increment> and return resulting value.
......@@ -155,9 +163,7 @@ class SQLite3Storage(local):
Does not commit.
"""
row = self._executeSingleRow(
'SELECT value FROM %scounter WHERE name = ? LIMIT 2' % (
self._table_prefix,
),
'SELECT value FROM %(prefix)scounter WHERE name = ? LIMIT 2',
(name, ),
)
if row is None:
......@@ -165,10 +171,9 @@ class SQLite3Storage(local):
else:
value = row['value']
value += increment
self._db.cursor().execute(
'INSERT OR REPLACE INTO %scounter (name, value) VALUES (?, ?)' % (
self._table_prefix,
),
self._execute(
self._db.cursor(),
'INSERT OR REPLACE INTO %(prefix)scounter (name, value) VALUES (?, ?)',
(name, value),
)
return value
......@@ -177,7 +182,7 @@ class SQLite3Storage(local):
"""
Execute <sql>, raise if it produces more than 1 row, and return it.
"""
result_list = self._db.cursor().execute(sql, parameters).fetchall()
result_list = self._execute(self._db.cursor(), sql, parameters).fetchall()
if result_list:
result, = result_list
return result
......@@ -190,9 +195,7 @@ class SQLite3Storage(local):
"""
with self._db:
result = self._executeSingleRow(
'SELECT value FROM %sconfig_once WHERE name = ?' % (
self._table_prefix,
),
'SELECT value FROM %(prefix)sconfig_once WHERE name = ?',
(name, ),
)
if result is None:
......@@ -206,10 +209,9 @@ class SQLite3Storage(local):
"""
try:
with self._db as db:
db.cursor().execute(
'INSERT INTO %sconfig_once (name, value) VALUES (?, ?)' % (
self._table_prefix,
),
self._execute(
db.cursor(),
'INSERT INTO %(prefix)sconfig_once (name, value) VALUES (?, ?)',
(name, value),
)
except sqlite3.IntegrityError:
......@@ -223,10 +225,9 @@ class SQLite3Storage(local):
with self._db as db:
c = db.cursor()
if prune:
c.execute(
'DELETE FROM %sca WHERE expiration_date < ?' % (
self._table_prefix,
),
self._execute(
c,
'DELETE FROM %(prefix)sca WHERE expiration_date < ?',
(time(), ),
)
return [
......@@ -234,10 +235,9 @@ class SQLite3Storage(local):
'crt_pem': toBytes(x['crt']),
'key_pem': toBytes(x['key']),
}
for x in db.cursor().execute(
'SELECT key, crt FROM %sca ORDER BY expiration_date ASC' % (
self._table_prefix,
),
for x in self._execute(
c,
'SELECT key, crt FROM %(prefix)sca ORDER BY expiration_date ASC',
).fetchall()
]
......@@ -246,14 +246,14 @@ class SQLite3Storage(local):
Store a certificate authority key pair.
expiration_timestamp (int)
Unix GMT timestamp of CA certificate "valid until" date.
key_pair (dict with 'key' and 'crt' items)
key_pair (dict with 'key_pem' and 'crt_pem' items)
CA key pair to store, as bytes.
"""
with self._db as db:
db.cursor().execute(
'INSERT INTO %sca (expiration_date, key, crt) VALUES (?, ?, ?)' % (
self._table_prefix,
),
self._execute(
db.cursor(),
'INSERT INTO %(prefix)sca '
'(expiration_date, key, crt) VALUES (?, ?, ?)',
(
expiration_timestamp,
key_pair['key_pem'],
......@@ -274,9 +274,7 @@ class SQLite3Storage(local):
"""
with self._db as db:
known_csr = self._executeSingleRow(
'SELECT id FROM %scrt WHERE csr = ? LIMIT 2' % (
self._table_prefix,
),
'SELECT id FROM %(prefix)scrt WHERE csr = ? LIMIT 2',
(csr_pem, ),
)
if known_csr is not None:
......@@ -288,28 +286,24 @@ class SQLite3Storage(local):
requested_count = None
else:
if self._executeSingleRow(
'SELECT COUNT(*) FROM %scrt WHERE crt IS NULL' % (
self._table_prefix,
)
'SELECT COUNT(*) FROM %(prefix)scrt WHERE crt IS NULL',
)[0] >= self._max_csr_amount:
raise NoStorage
requested_count = self._incrementCounter('received_csr')
csr_id = getrandbits(63)
c = db.cursor()
c.execute(
'INSERT INTO %scrt (id, key_id, csr) VALUES (?, ?, ?)' % (
self._table_prefix,
),
self._execute(
c,
'INSERT INTO %(prefix)scrt (id, key_id, csr) VALUES (?, ?, ?)',
(
csr_id,
key_id,
csr_pem,
),
)
c.execute(
'DELETE FROM %scrt WHERE expiration_date < ?' % (
self._table_prefix,
),
self._execute(
c,
'DELETE FROM %(prefix)scrt WHERE expiration_date < ?',
(time(), ),
)
return csr_id, requested_count
......@@ -322,10 +316,9 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute(
'DELETE FROM %scrt WHERE id = ? AND crt IS NULL' % (
self._table_prefix,
),
self._execute(
c,
'DELETE FROM %(prefix)scrt WHERE id = ? AND crt IS NULL',
(csr_id, ),
)
if c.rowcount == 1:
......@@ -343,9 +336,7 @@ class SQLite3Storage(local):
"""
with self._db:
result = self._executeSingleRow(
'SELECT csr FROM %scrt WHERE id = ?' % (
self._table_prefix,
),
'SELECT csr FROM %(prefix)scrt WHERE id = ?',
(csr_id, ),
)
if result is None:
......@@ -368,10 +359,9 @@ class SQLite3Storage(local):
# to then have to unicode-ify, just unicode-ify here.
'csr': toUnicode(x['csr']),
}
for x in db.cursor().execute(
'SELECT id, csr FROM %scrt WHERE crt IS NULL' % (
self._table_prefix,
),
for x in self._execute(
db.cursor(),
'SELECT id, csr FROM %(prefix)scrt WHERE crt IS NULL',
).fetchall()
]
......@@ -384,11 +374,10 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute(
'UPDATE %scrt SET crt=?, expiration_date = ? '
'WHERE id = ? AND crt IS NULL' % (
self._table_prefix,
),
self._execute(
c,
'UPDATE %(prefix)scrt SET crt=?, expiration_date = ? '
'WHERE id = ? AND crt IS NULL',
(
crt,
int(time() + self._crt_keep_time),
......@@ -410,20 +399,17 @@ class SQLite3Storage(local):
"""
with self._db as db:
row = self._executeSingleRow(
'SELECT crt, expiration_date FROM %scrt '
'WHERE id = ? AND crt IS NOT NULL' % (
self._table_prefix,
),
'SELECT crt, expiration_date FROM %(prefix)scrt '
'WHERE id = ? AND crt IS NOT NULL',
(crt_id, ),
)
if row is None:
raise NotFound
new_expiration_date = int(time() + self._crt_read_keep_time)
if row['expiration_date'] > new_expiration_date:
db.cursor().execute(
'UPDATE %scrt SET expiration_date = ? WHERE id = ?' % (
self._table_prefix,
),
self._execute(
db.cursor(),
'UPDATE %(prefix)scrt SET expiration_date = ? WHERE id = ?',
(
new_expiration_date,
crt_id,
......@@ -440,9 +426,7 @@ class SQLite3Storage(local):
"""
with self._db:
row = self._executeSingleRow(
'SELECT crt FROM %scrt WHERE key_id = ? AND crt IS NOT NULL' % (
self._table_prefix,
),
'SELECT crt FROM %(prefix)scrt WHERE key_id = ? AND crt IS NOT NULL',
(key_id, ),
)
if row is None:
......@@ -455,9 +439,10 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute('SELECT crt FROM %scrt WHERE crt IS NOT NULL' % (
self._table_prefix,
))
self._execute(
c,
'SELECT crt FROM %(prefix)scrt WHERE crt IS NOT NULL',
)
while True:
row = c.fetchone()
if row is None:
......@@ -477,21 +462,18 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute('DELETE FROM %scrl' % (
self._table_prefix,
))
self._execute(c, 'DELETE FROM %(prefix)scrl')
try:
c.execute(
'INSERT INTO %srevoked '
self._execute(
c,
'INSERT INTO %(prefix)srevoked '
'(serial, revocation_date, expiration_date) '
'VALUES (?, ?, ?)' % (
self._table_prefix,
),
'VALUES (?, ?, ?)',
(
str(serial),
int(time()),
expiration_date,
)
),
)
except sqlite3.IntegrityError:
raise Found
......@@ -526,13 +508,10 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute('DELETE FROM %scrl' % (
self._table_prefix,
))
c.execute(
'INSERT INTO %scrl (expiration_date, crl) VALUES (?, ?)' % (
self._table_prefix,
),
self._execute(c, 'DELETE FROM %(prefix)scrl')
self._execute(
c,
'INSERT INTO %(prefix)scrl (expiration_date, crl) VALUES (?, ?)',
(
int(expiration_date),
crl,
......@@ -551,10 +530,9 @@ class SQLite3Storage(local):
"""
with self._db as db:
c = db.cursor()
c.execute(
'DELETE FROM %srevoked WHERE expiration_date < ?' % (
self._table_prefix,
),
self._execute(
c,
'DELETE FROM %(prefix)srevoked WHERE expiration_date < ?',
(time(), ),
)
return [
......@@ -562,10 +540,9 @@ class SQLite3Storage(local):
'revocation_date': int(x['revocation_date']),
'serial': int(x['serial']),
}
for x in c.execute(
'SELECT revocation_date, serial FROM %srevoked' % (
self._table_prefix,
),
for x in self._execute(
c,
'SELECT revocation_date, serial FROM %(prefix)srevoked',
)
]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment