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