Class JdbcCryptoEngine
- java.lang.Object
-
- io.axoniq.dataprotection.cryptoengine.DatabaseBackedCryptoEngine
-
- io.axoniq.dataprotection.cryptoengine.JdbcCryptoEngine
-
- All Implemented Interfaces:
CryptoEngine
public class JdbcCryptoEngine extends DatabaseBackedCryptoEngine
JDBC-based implementation of theCryptoEngineinterface, included for users who wish to store keys in a relational database but do not wish to use JPA.
-
-
Constructor Summary
Constructors Constructor Description JdbcCryptoEngine(DataSource dataSource)Constructs a new JdbcCryptoEngine, using "id" as the SQL name of the key id column, "secret_key" as the SQL name of the key data column, and "data_protection_keys" as the SQL name of the table.JdbcCryptoEngine(DataSource dataSource, String tableName)Constructs a new JdbcCryptoEngine, using "id" as the SQL name of the key id column, and "secret_key" as the SQL name of the key data column.JdbcCryptoEngine(DataSource dataSource, String tableName, String keyIdColumnName, String keyDataColumnName)Constructs a new JdbcCryptoEngine.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddeleteKey(String id)Deletes the key for a given id.StringgetCreateTableStatement()Returns a DDL statement to create the table for key storage, using the SQL names that have been configured.protected StringgetDeleteStatement()Generates the DELETE statement to remove a key:protected StringgetInsertStatement()Generates the INSERT statement to store a key:SecretKeygetKey(String id)Retrieves the key for a given id.protected StringgetKeyDataColumnName()Returns the SQL name of the key data column as configured during construction.protected StringgetKeyIdColumnName()Returns the SQL name of the key id column as configured during construction.protected StringgetSelectStatement()Generates the SELECT statement to retrieve the key data based on the key id:protected StringgetTableName()Returns the SQL name of the table as configured during construction.protected SecretKeyputKeyIfAbsent(String id, SecretKeySpec secretKeySpec)Stores the key for the given id, if no key is currently registered for this id.-
Methods inherited from class io.axoniq.dataprotection.cryptoengine.DatabaseBackedCryptoEngine
createCipher, createDigestCipher, getKeyType, getOrCreateKey, setKeyType
-
-
-
-
Constructor Detail
-
JdbcCryptoEngine
public JdbcCryptoEngine(DataSource dataSource, String tableName, String keyIdColumnName, String keyDataColumnName)
Constructs a new JdbcCryptoEngine.- Parameters:
dataSource- the DataSourcetableName- the SQL name table in which keys will be storedkeyIdColumnName- the SQL name of the column in which the key id will be storedkeyDataColumnName- the SQL name of the column in which the key data will be stored
-
JdbcCryptoEngine
public JdbcCryptoEngine(DataSource dataSource, String tableName)
Constructs a new JdbcCryptoEngine, using "id" as the SQL name of the key id column, and "secret_key" as the SQL name of the key data column.- Parameters:
dataSource- the DataSourcetableName- the SQL name table in which keys will be stored
-
JdbcCryptoEngine
public JdbcCryptoEngine(DataSource dataSource)
Constructs a new JdbcCryptoEngine, using "id" as the SQL name of the key id column, "secret_key" as the SQL name of the key data column, and "data_protection_keys" as the SQL name of the table.- Parameters:
dataSource- the DataSource
-
-
Method Detail
-
getTableName
protected String getTableName()
Returns the SQL name of the table as configured during construction.- Returns:
- the name
-
getKeyIdColumnName
protected String getKeyIdColumnName()
Returns the SQL name of the key id column as configured during construction.- Returns:
- the name
-
getKeyDataColumnName
protected String getKeyDataColumnName()
Returns the SQL name of the key data column as configured during construction.- Returns:
- the name
-
getCreateTableStatement
public String getCreateTableStatement()
Returns a DDL statement to create the table for key storage, using the SQL names that have been configured. This is never executed automatically by the module. It is purely here to facilitate developer who need to create the table automatically, e.g. for automatic tests.This statement uses
VARCHAR(255)as the data type for the columns, since this will work universally across SQL databases. Please note that for MS SQL Server, you may wish to either usesendStringParametersAsUnicode=false, or change this toNVARCHAR, to avoid a performance hit on the primary key index.- Returns:
- the CREATE TABLE statement
- See Also:
- SQL Server Customer Advisory Team
-
getSelectStatement
protected String getSelectStatement()
Generates the SELECT statement to retrieve the key data based on the key id:SELECT [key data column] FROM [key table] WHERE [key id column] = ?- Returns:
- the SELECT statement
-
getInsertStatement
protected String getInsertStatement()
Generates the INSERT statement to store a key:INSERT INTO [key table]([key id column], [key data column]) VALUES (?, ?)- Returns:
- the INSERT statement
-
getDeleteStatement
protected String getDeleteStatement()
Generates the DELETE statement to remove a key:DELETE FROM [key table] WHERE [key id column] = ?- Returns:
- the DELETE statement
-
putKeyIfAbsent
protected SecretKey putKeyIfAbsent(String id, SecretKeySpec secretKeySpec)
Description copied from class:DatabaseBackedCryptoEngineStores the key for the given id, if no key is currently registered for this id. Returns the new key belonging to id, which is either the key that was already registered or the key provided as the 2nd argument if no key was registered yet. (Please note that this is different behaviour fromConcurrentHashMap.putIfAbsent(Object, Object), which always returns the prior value belonging to key.)- Specified by:
putKeyIfAbsentin classDatabaseBackedCryptoEngine- Parameters:
id- the id for which to store/retrieve the secret keysecretKeySpec- theSecretKeySpecto store if none has been stored yet for id- Returns:
- the effective
SecretKeySpecfor id
-
getKey
public SecretKey getKey(String id)
Description copied from interface:CryptoEngineRetrieves the key for a given id.
-
deleteKey
public void deleteKey(String id)
Description copied from interface:CryptoEngineDeletes the key for a given id. Does nothing if the key doesn't exist.- Parameters:
id- the id of theSecretKey
-
-