Package io.axoniq.dataprotection.cryptoengine

## 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 the `CryptoEngine` interface, 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
    --- | --- | ---
    `void` | `deleteKey​(String id)` | Deletes the key for a given id.
    `String` | `getCreateTableStatement()` | Returns a DDL statement to create the table for key storage, using the SQL names that have been configured.
    `protected String` | `getDeleteStatement()` | Generates the DELETE statement to remove a key:
    `protected String` | `getInsertStatement()` | Generates the INSERT statement to store a key:
    `SecretKey` | `getKey​(String id)` | Retrieves the key for a given id.
    `protected String` | `getKeyDataColumnName()` | Returns the SQL name of the key data column as configured during construction.
    `protected String` | `getKeyIdColumnName()` | Returns the SQL name of the key id column as configured during construction.
    `protected String` | `getSelectStatement()` | Generates the SELECT statement to retrieve the key data based on the key id:
    `protected String` | `getTableName()` | Returns the SQL name of the table as configured during construction.
    `protected SecretKey` | `putKeyIfAbsent​(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`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### JdbcCryptoEngine

          public JdbcCryptoEngine​(DataSource dataSource,
                                  String tableName,
                                  String keyIdColumnName,
                                  String keyDataColumnName)

      Constructs a new JdbcCryptoEngine.
      Parameters:
        `dataSource` - the DataSource
        `tableName` - the SQL name table in which keys will be stored
        `keyIdColumnName` - the SQL name of the column in which the key id will be stored
        `keyDataColumnName` - 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 DataSource
        `tableName` - 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 use `sendStringParametersAsUnicode=false`, or change this to `NVARCHAR`, 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: `DatabaseBackedCryptoEngine`
      Stores 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 from `ConcurrentHashMap.putIfAbsent(Object, Object)`, which always returns the prior value belonging to key.)
      Specified by:
        `putKeyIfAbsent` in class `DatabaseBackedCryptoEngine`
      Parameters:
        `id` - the id for which to store/retrieve the secret key
        `secretKeySpec` - the `SecretKeySpec` to store if none has been stored yet for id
      Returns:
        the effective `SecretKeySpec` for id

    - #### getKey

          public SecretKey getKey​(String id)

      Description copied from interface: `CryptoEngine`
      Retrieves the key for a given id.
      Parameters:
        `id` - the id of the `SecretKey`
      Returns:
        the existing `SecretKey` associated with the id, or `null` if no such key exists

    - #### deleteKey

          public void deleteKey​(String id)

      Description copied from interface: `CryptoEngine`
      Deletes the key for a given id. Does nothing if the key doesn't exist.
      Parameters:
        `id` - the id of the `SecretKey`