Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for an ODBC backend #319

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,589 changes: 1,124 additions & 465 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ mobile_secure_element = ["askar-crypto/p256_hardware"]
pg_test = ["askar-storage/pg_test"]
postgres = ["askar-storage/postgres"]
sqlite = ["askar-storage/sqlite"]
odbc = ["askar-storage/odbc"]

[dependencies]
async-lock = "3.0"
Expand Down
4 changes: 4 additions & 0 deletions askar-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ migration = ["dep:rmp-serde", "dep:sqlx", "sqlx?/macros"]
pg_test = ["postgres"]
postgres = ["dep:sqlx", "sqlx?/postgres", "sqlx?/tls-rustls"]
sqlite = ["dep:sqlx", "sqlx?/sqlite"]
odbc = ["dep:odbc-api", "dep:r2d2", "dep:lazy_static"]

[dependencies]
arc-swap = "1.6"
Expand All @@ -48,6 +49,9 @@ tokio = { version = "1.5", features = ["rt-multi-thread", "time"] }
url = { version = "2.1", default-features = false }
uuid = { version = "1.2", features = ["v4"] }
zeroize = "1.5"
odbc-api = { version = "8.1.4", optional = true }
r2d2 = { version = "0.8.10", optional = true }
lazy_static = { version = "1.5.0", optional = true }

[dependencies.askar-crypto]
default-features = false
Expand Down
23 changes: 23 additions & 0 deletions askar-storage/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use super::postgres;
#[cfg(feature = "sqlite")]
use super::sqlite;

#[cfg(feature = "odbc")]
use super::odbc;

/// A dynamic store backend instance
#[derive(Clone, Debug)]
pub struct AnyBackend(Arc<dyn Backend<Session = AnyBackendSession>>);
Expand Down Expand Up @@ -288,6 +291,13 @@ impl<'a> ManageBackend<'a> for &'a str {
Ok(into_any_backend(mgr))
}

#[cfg(feature = "odbc")]
"odbc" => {
let opts = odbc::OdbcStoreOptions::new(opts)?;
let mgr = opts.open(method, pass_key, profile).await?;
Ok(into_any_backend(mgr))
}

_ => Err(err_msg!(
Unsupported,
"Unsupported backend: {}",
Expand Down Expand Up @@ -323,6 +333,13 @@ impl<'a> ManageBackend<'a> for &'a str {
Ok(into_any_backend(mgr))
}

#[cfg(feature = "odbc")]
"odbc" => {
let opts = odbc::OdbcStoreOptions::new(opts)?;
let mgr = opts.provision(method, pass_key, profile, recreate).await?;
Ok(into_any_backend(mgr))
}

_ => Err(err_msg!(
Unsupported,
"Unsupported backend: {}",
Expand Down Expand Up @@ -350,6 +367,12 @@ impl<'a> ManageBackend<'a> for &'a str {
Ok(opts.remove().await?)
}

#[cfg(feature = "odbc")]
"odbc" => {
let opts = odbc::OdbcStoreOptions::new(opts)?;
Ok(opts.remove().await?)
}

_ => Err(err_msg!(
Unsupported,
"Unsupported backend: {}",
Expand Down
7 changes: 6 additions & 1 deletion askar-storage/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
protect::{PassKey, StoreKeyMethod},
};

#[cfg(any(feature = "postgres", feature = "sqlite"))]
#[cfg(any(feature = "postgres", feature = "sqlite", feature = "odbc"))]
pub(crate) mod db_utils;

#[cfg(feature = "postgres")]
Expand All @@ -22,6 +22,11 @@ pub mod postgres;
/// Sqlite database support
pub mod sqlite;

#[cfg(feature = "odbc")]
#[cfg_attr(docsrs, doc(cfg(feature = "odbc")))]
/// odbc database support
pub mod odbc;

/// Enum to support custom ordering in record queries
#[derive(Debug)]
pub enum OrderBy {
Expand Down
51 changes: 51 additions & 0 deletions askar-storage/src/backend/odbc/db2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

-- This SQL file contains the database schema for a DB2 server.
--

CREATE TABLE config (
name VARCHAR(1022) NOT NULL,
value CLOB,
PRIMARY KEY(name)
);

CREATE TABLE profiles (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,
name VARCHAR(1022) NOT NULL,
reference CLOB NULL,
profile_key BLOB NULL,
PRIMARY KEY(id)
);

CREATE UNIQUE INDEX ix_profile_name ON profiles(name);

CREATE TABLE items (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,
profile_id BIGINT NOT NULL,
kind SMALLINT NOT NULL,
category VARCHAR(500) NOT NULL,
name VARCHAR(500) NOT NULL,
value BLOB NOT NULL,
expiry TIMESTAMP NULL,
PRIMARY KEY(id),
FOREIGN KEY(profile_id) REFERENCES profiles(id)
ON DELETE CASCADE
);

CREATE UNIQUE INDEX ix_items_uniq ON items(profile_id, kind, category, name);
CREATE INDEX ix_items_profile_id ON items(profile_id);

CREATE TABLE items_tags (
id BIGINT NOT NULL GENERATED ALWAYS AS IDENTITY,
item_id BIGINT NOT NULL,
name VARCHAR(500) NOT NULL,
value VARCHAR(500) NOT NULL,
plaintext SMALLINT NOT NULL,
PRIMARY KEY(id),
FOREIGN KEY(item_id) REFERENCES items(id)
ON DELETE CASCADE
);

CREATE INDEX ix_items_tags_item_id ON items_tags(item_id);
CREATE INDEX ix_items_tags_name ON items_tags(name, value);

COMMIT;
Loading