From ff759397f6742781a722dab029d695c4bbcf794a Mon Sep 17 00:00:00 2001 From: Nils Domrose Date: Sun, 26 May 2019 23:02:41 +0200 Subject: [PATCH] initial mysql support --- Cargo.toml | 9 +- Dockerfile | 12 +- .../2018-01-14-171611_create_tables/down.sql | 0 .../2018-01-14-171611_create_tables/up.sql | 6 +- .../down.sql | 0 .../up.sql | 4 +- .../down.sql | 0 .../up.sql | 6 +- .../down.sql | 0 .../up.sql | 0 .../down.sql | 0 .../up.sql | 0 .../down.sql | 0 .../up.sql | 0 .../down.sql | 0 .../up.sql | 6 +- .../2018-08-27-172114_update_ciphers/down.sql | 0 .../2018-08-27-172114_update_ciphers/up.sql | 0 .../2018-09-10-111213_add_invites/down.sql | 0 .../2018-09-10-111213_add_invites/up.sql | 0 .../down.sql | 0 .../2018-09-19-144557_add_kdf_columns/up.sql | 0 .../down.sql | 0 .../up.sql | 3 + .../down.sql | 7 + .../up.sql | 7 + .../2018-01-14-171611_create_tables/down.sql | 9 + .../2018-01-14-171611_create_tables/up.sql | 62 +++++++ .../down.sql | 8 + .../up.sql | 31 ++++ .../down.sql | 0 .../up.sql | 34 ++++ .../down.sql | 1 + .../up.sql | 5 + .../down.sql | 0 .../up.sql | 14 ++ .../down.sql | 1 + .../up.sql | 3 + .../down.sql | 8 + .../up.sql | 15 ++ .../2018-08-27-172114_update_ciphers/down.sql | 0 .../2018-08-27-172114_update_ciphers/up.sql | 3 + .../2018-09-10-111213_add_invites/down.sql | 1 + .../2018-09-10-111213_add_invites/up.sql | 3 + .../down.sql | 0 .../2018-09-19-144557_add_kdf_columns/up.sql | 7 + .../down.sql | 0 .../up.sql | 2 +- .../down.sql | 7 + .../up.sql | 7 + src/config.rs | 4 +- src/db/mod.rs | 12 ++ src/db/{ => schemas/mysql}/schema.rs | 0 src/db/schemas/sqlite/schema.rs | 172 ++++++++++++++++++ src/main.rs | 6 +- 55 files changed, 454 insertions(+), 21 deletions(-) rename migrations/{ => mysql}/2018-01-14-171611_create_tables/down.sql (100%) rename migrations/{ => mysql}/2018-01-14-171611_create_tables/up.sql (94%) rename migrations/{ => mysql}/2018-02-17-205753_create_collections_and_orgs/down.sql (100%) rename migrations/{ => mysql}/2018-02-17-205753_create_collections_and_orgs/up.sql (93%) rename migrations/{ => mysql}/2018-04-27-155151_create_users_ciphers/down.sql (100%) rename migrations/{ => mysql}/2018-04-27-155151_create_users_ciphers/up.sql (87%) rename migrations/{ => mysql}/2018-05-08-161616_create_collection_cipher_map/down.sql (100%) rename migrations/{ => mysql}/2018-05-08-161616_create_collection_cipher_map/up.sql (100%) rename migrations/{ => mysql}/2018-05-25-232323_update_attachments_reference/down.sql (100%) rename migrations/{ => mysql}/2018-05-25-232323_update_attachments_reference/up.sql (100%) rename migrations/{ => mysql}/2018-06-01-112529_update_devices_twofactor_remember/down.sql (100%) rename migrations/{ => mysql}/2018-06-01-112529_update_devices_twofactor_remember/up.sql (100%) rename migrations/{ => mysql}/2018-07-11-181453_create_u2f_twofactor/down.sql (100%) rename migrations/{ => mysql}/2018-07-11-181453_create_u2f_twofactor/up.sql (75%) rename migrations/{ => mysql}/2018-08-27-172114_update_ciphers/down.sql (100%) rename migrations/{ => mysql}/2018-08-27-172114_update_ciphers/up.sql (100%) rename migrations/{ => mysql}/2018-09-10-111213_add_invites/down.sql (100%) rename migrations/{ => mysql}/2018-09-10-111213_add_invites/up.sql (100%) rename migrations/{ => mysql}/2018-09-19-144557_add_kdf_columns/down.sql (100%) rename migrations/{ => mysql}/2018-09-19-144557_add_kdf_columns/up.sql (100%) rename migrations/{ => mysql}/2018-11-27-152651_add_att_key_columns/down.sql (100%) create mode 100644 migrations/mysql/2018-11-27-152651_add_att_key_columns/up.sql create mode 100644 migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/down.sql create mode 100644 migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/up.sql create mode 100644 migrations/sqlite/2018-01-14-171611_create_tables/down.sql create mode 100644 migrations/sqlite/2018-01-14-171611_create_tables/up.sql create mode 100644 migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/down.sql create mode 100644 migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/up.sql create mode 100644 migrations/sqlite/2018-04-27-155151_create_users_ciphers/down.sql create mode 100644 migrations/sqlite/2018-04-27-155151_create_users_ciphers/up.sql create mode 100644 migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/down.sql create mode 100644 migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/up.sql create mode 100644 migrations/sqlite/2018-05-25-232323_update_attachments_reference/down.sql create mode 100644 migrations/sqlite/2018-05-25-232323_update_attachments_reference/up.sql create mode 100644 migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/down.sql create mode 100644 migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/up.sql create mode 100644 migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/down.sql create mode 100644 migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/up.sql create mode 100644 migrations/sqlite/2018-08-27-172114_update_ciphers/down.sql create mode 100644 migrations/sqlite/2018-08-27-172114_update_ciphers/up.sql create mode 100644 migrations/sqlite/2018-09-10-111213_add_invites/down.sql create mode 100644 migrations/sqlite/2018-09-10-111213_add_invites/up.sql create mode 100644 migrations/sqlite/2018-09-19-144557_add_kdf_columns/down.sql create mode 100644 migrations/sqlite/2018-09-19-144557_add_kdf_columns/up.sql create mode 100644 migrations/sqlite/2018-11-27-152651_add_att_key_columns/down.sql rename migrations/{ => sqlite}/2018-11-27-152651_add_att_key_columns/up.sql (72%) create mode 100644 migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/down.sql create mode 100644 migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/up.sql rename src/db/{ => schemas/mysql}/schema.rs (100%) create mode 100644 src/db/schemas/sqlite/schema.rs diff --git a/Cargo.toml b/Cargo.toml index 148ebc81..292bfb37 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ build = "build.rs" [features] # Empty to keep compatibility, prefer to set USE_SYSLOG=true enable_syslog = [] +mysql = [] +sqlite = [] [target."cfg(not(windows))".dependencies] syslog = "4.0.1" @@ -47,8 +49,11 @@ log = "0.4.6" fern = { version = "0.5.8", features = ["syslog-4"] } # A safe, extensible ORM and Query builder -diesel = { version = "1.4.2", features = ["mysql", "chrono", "r2d2"] } -diesel_migrations = { version = "1.4.0", features = ["mysql"] } +diesel = { version = "1.4.2", features = ["mysql", "sqlite", "chrono", "r2d2"] } +diesel_migrations = { version = "1.4.0", features = ["mysql", "sqlite"] } + +# Bundled SQLite +libsqlite3-sys = { version = "0.12.0", features = ["bundled"] } # Crypto library ring = "0.14.6" diff --git a/Dockerfile b/Dockerfile index 8faca0ed..cfa1e293 100644 --- a/Dockerfile +++ b/Dockerfile @@ -23,6 +23,9 @@ RUN ls # we need the Rust compiler and Cargo tooling FROM rust as build +# set sqlite as default for DB ARG for backward comaptibility +ARG DB=sqlite + # Using bundled SQLite, no need to install it # RUN apt-get update && apt-get install -y\ # sqlite3\ @@ -31,8 +34,8 @@ FROM rust as build # Install MySQL package RUN apt-get update && apt-get install -y \ - libmysql++-dev \ - --no-install-recommends \ + libmariadb-dev\ + --no-install-recommends\ && rm -rf /var/lib/apt/lists/* # Creates a dummy project used to grab dependencies @@ -47,7 +50,7 @@ COPY ./build.rs ./build.rs # Builds your dependencies and removes the # dummy project, except the target folder # This folder contains the compiled dependencies -RUN cargo build --release +RUN cargo build --features ${DB} --release RUN find . -not -path "./target*" -delete # Copies the complete project @@ -59,7 +62,7 @@ RUN touch src/main.rs # Builds again, this time it'll just be # your actual source files being built -RUN cargo build --release +RUN cargo build --features ${DB} --release ######################## RUNTIME IMAGE ######################## # Create a new stage with a minimal image @@ -74,6 +77,7 @@ ENV ROCKET_WORKERS=10 RUN apt-get update && apt-get install -y\ openssl\ ca-certificates\ + libmariadbclient-dev\ --no-install-recommends\ && rm -rf /var/lib/apt/lists/* diff --git a/migrations/2018-01-14-171611_create_tables/down.sql b/migrations/mysql/2018-01-14-171611_create_tables/down.sql similarity index 100% rename from migrations/2018-01-14-171611_create_tables/down.sql rename to migrations/mysql/2018-01-14-171611_create_tables/down.sql diff --git a/migrations/2018-01-14-171611_create_tables/up.sql b/migrations/mysql/2018-01-14-171611_create_tables/up.sql similarity index 94% rename from migrations/2018-01-14-171611_create_tables/up.sql rename to migrations/mysql/2018-01-14-171611_create_tables/up.sql index 0a1fc8c9..244c3c80 100644 --- a/migrations/2018-01-14-171611_create_tables/up.sql +++ b/migrations/mysql/2018-01-14-171611_create_tables/up.sql @@ -8,7 +8,7 @@ CREATE TABLE users ( salt BLOB NOT NULL, password_iterations INTEGER NOT NULL, password_hint TEXT, - akey TEXT NOT NULL, + `key` TEXT NOT NULL, private_key TEXT, public_key TEXT, totp_secret TEXT, @@ -24,7 +24,7 @@ CREATE TABLE devices ( updated_at DATETIME NOT NULL, user_uuid VARCHAR(40) NOT NULL REFERENCES users (uuid), name TEXT NOT NULL, - atype INTEGER NOT NULL, + type INTEGER NOT NULL, push_token TEXT, refresh_token TEXT NOT NULL ); @@ -36,7 +36,7 @@ CREATE TABLE ciphers ( user_uuid VARCHAR(40) NOT NULL REFERENCES users (uuid), folder_uuid VARCHAR(40) REFERENCES folders (uuid), organization_uuid VARCHAR(40), - atype INTEGER NOT NULL, + type INTEGER NOT NULL, name TEXT NOT NULL, notes TEXT, fields TEXT, diff --git a/migrations/2018-02-17-205753_create_collections_and_orgs/down.sql b/migrations/mysql/2018-02-17-205753_create_collections_and_orgs/down.sql similarity index 100% rename from migrations/2018-02-17-205753_create_collections_and_orgs/down.sql rename to migrations/mysql/2018-02-17-205753_create_collections_and_orgs/down.sql diff --git a/migrations/2018-02-17-205753_create_collections_and_orgs/up.sql b/migrations/mysql/2018-02-17-205753_create_collections_and_orgs/up.sql similarity index 93% rename from migrations/2018-02-17-205753_create_collections_and_orgs/up.sql rename to migrations/mysql/2018-02-17-205753_create_collections_and_orgs/up.sql index 8d5508ff..c5200a76 100644 --- a/migrations/2018-02-17-205753_create_collections_and_orgs/up.sql +++ b/migrations/mysql/2018-02-17-205753_create_collections_and_orgs/up.sql @@ -22,9 +22,9 @@ CREATE TABLE users_organizations ( org_uuid VARCHAR(40) NOT NULL REFERENCES organizations (uuid), access_all BOOLEAN NOT NULL, - akey TEXT NOT NULL, + `key` TEXT NOT NULL, status INTEGER NOT NULL, - atype INTEGER NOT NULL, + type INTEGER NOT NULL, UNIQUE (user_uuid, org_uuid) ); diff --git a/migrations/2018-04-27-155151_create_users_ciphers/down.sql b/migrations/mysql/2018-04-27-155151_create_users_ciphers/down.sql similarity index 100% rename from migrations/2018-04-27-155151_create_users_ciphers/down.sql rename to migrations/mysql/2018-04-27-155151_create_users_ciphers/down.sql diff --git a/migrations/2018-04-27-155151_create_users_ciphers/up.sql b/migrations/mysql/2018-04-27-155151_create_users_ciphers/up.sql similarity index 87% rename from migrations/2018-04-27-155151_create_users_ciphers/up.sql rename to migrations/mysql/2018-04-27-155151_create_users_ciphers/up.sql index 00989e4a..65c53d54 100644 --- a/migrations/2018-04-27-155151_create_users_ciphers/up.sql +++ b/migrations/mysql/2018-04-27-155151_create_users_ciphers/up.sql @@ -7,7 +7,7 @@ CREATE TABLE ciphers ( user_uuid VARCHAR(40) REFERENCES users (uuid), -- Make this optional organization_uuid VARCHAR(40) REFERENCES organizations (uuid), -- Add reference to orgs table -- Remove folder_uuid - atype INTEGER NOT NULL, + type INTEGER NOT NULL, name TEXT NOT NULL, notes TEXT, fields TEXT, @@ -22,8 +22,8 @@ CREATE TABLE folders_ciphers ( PRIMARY KEY (cipher_uuid, folder_uuid) ); -INSERT INTO ciphers (uuid, created_at, updated_at, user_uuid, organization_uuid, atype, name, notes, fields, data, favorite) -SELECT uuid, created_at, updated_at, user_uuid, organization_uuid, atype, name, notes, fields, data, favorite FROM oldCiphers; +INSERT INTO ciphers (uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite) +SELECT uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite FROM oldCiphers; INSERT INTO folders_ciphers (cipher_uuid, folder_uuid) SELECT uuid, folder_uuid FROM oldCiphers WHERE folder_uuid IS NOT NULL; diff --git a/migrations/2018-05-08-161616_create_collection_cipher_map/down.sql b/migrations/mysql/2018-05-08-161616_create_collection_cipher_map/down.sql similarity index 100% rename from migrations/2018-05-08-161616_create_collection_cipher_map/down.sql rename to migrations/mysql/2018-05-08-161616_create_collection_cipher_map/down.sql diff --git a/migrations/2018-05-08-161616_create_collection_cipher_map/up.sql b/migrations/mysql/2018-05-08-161616_create_collection_cipher_map/up.sql similarity index 100% rename from migrations/2018-05-08-161616_create_collection_cipher_map/up.sql rename to migrations/mysql/2018-05-08-161616_create_collection_cipher_map/up.sql diff --git a/migrations/2018-05-25-232323_update_attachments_reference/down.sql b/migrations/mysql/2018-05-25-232323_update_attachments_reference/down.sql similarity index 100% rename from migrations/2018-05-25-232323_update_attachments_reference/down.sql rename to migrations/mysql/2018-05-25-232323_update_attachments_reference/down.sql diff --git a/migrations/2018-05-25-232323_update_attachments_reference/up.sql b/migrations/mysql/2018-05-25-232323_update_attachments_reference/up.sql similarity index 100% rename from migrations/2018-05-25-232323_update_attachments_reference/up.sql rename to migrations/mysql/2018-05-25-232323_update_attachments_reference/up.sql diff --git a/migrations/2018-06-01-112529_update_devices_twofactor_remember/down.sql b/migrations/mysql/2018-06-01-112529_update_devices_twofactor_remember/down.sql similarity index 100% rename from migrations/2018-06-01-112529_update_devices_twofactor_remember/down.sql rename to migrations/mysql/2018-06-01-112529_update_devices_twofactor_remember/down.sql diff --git a/migrations/2018-06-01-112529_update_devices_twofactor_remember/up.sql b/migrations/mysql/2018-06-01-112529_update_devices_twofactor_remember/up.sql similarity index 100% rename from migrations/2018-06-01-112529_update_devices_twofactor_remember/up.sql rename to migrations/mysql/2018-06-01-112529_update_devices_twofactor_remember/up.sql diff --git a/migrations/2018-07-11-181453_create_u2f_twofactor/down.sql b/migrations/mysql/2018-07-11-181453_create_u2f_twofactor/down.sql similarity index 100% rename from migrations/2018-07-11-181453_create_u2f_twofactor/down.sql rename to migrations/mysql/2018-07-11-181453_create_u2f_twofactor/down.sql diff --git a/migrations/2018-07-11-181453_create_u2f_twofactor/up.sql b/migrations/mysql/2018-07-11-181453_create_u2f_twofactor/up.sql similarity index 75% rename from migrations/2018-07-11-181453_create_u2f_twofactor/up.sql rename to migrations/mysql/2018-07-11-181453_create_u2f_twofactor/up.sql index 7da3a51b..4fd57175 100644 --- a/migrations/2018-07-11-181453_create_u2f_twofactor/up.sql +++ b/migrations/mysql/2018-07-11-181453_create_u2f_twofactor/up.sql @@ -1,15 +1,15 @@ CREATE TABLE twofactor ( uuid VARCHAR(40) NOT NULL PRIMARY KEY, user_uuid VARCHAR(40) NOT NULL REFERENCES users (uuid), - atype INTEGER NOT NULL, + type INTEGER NOT NULL, enabled BOOLEAN NOT NULL, data TEXT NOT NULL, - UNIQUE (user_uuid, atype) + UNIQUE (user_uuid, type) ); -INSERT INTO twofactor (uuid, user_uuid, atype, enabled, data) +INSERT INTO twofactor (uuid, user_uuid, type, enabled, data) SELECT UUID(), uuid, 0, 1, u.totp_secret FROM users u where u.totp_secret IS NOT NULL; UPDATE users SET totp_secret = NULL; -- Instead of recreating the table, just leave the columns empty diff --git a/migrations/2018-08-27-172114_update_ciphers/down.sql b/migrations/mysql/2018-08-27-172114_update_ciphers/down.sql similarity index 100% rename from migrations/2018-08-27-172114_update_ciphers/down.sql rename to migrations/mysql/2018-08-27-172114_update_ciphers/down.sql diff --git a/migrations/2018-08-27-172114_update_ciphers/up.sql b/migrations/mysql/2018-08-27-172114_update_ciphers/up.sql similarity index 100% rename from migrations/2018-08-27-172114_update_ciphers/up.sql rename to migrations/mysql/2018-08-27-172114_update_ciphers/up.sql diff --git a/migrations/2018-09-10-111213_add_invites/down.sql b/migrations/mysql/2018-09-10-111213_add_invites/down.sql similarity index 100% rename from migrations/2018-09-10-111213_add_invites/down.sql rename to migrations/mysql/2018-09-10-111213_add_invites/down.sql diff --git a/migrations/2018-09-10-111213_add_invites/up.sql b/migrations/mysql/2018-09-10-111213_add_invites/up.sql similarity index 100% rename from migrations/2018-09-10-111213_add_invites/up.sql rename to migrations/mysql/2018-09-10-111213_add_invites/up.sql diff --git a/migrations/2018-09-19-144557_add_kdf_columns/down.sql b/migrations/mysql/2018-09-19-144557_add_kdf_columns/down.sql similarity index 100% rename from migrations/2018-09-19-144557_add_kdf_columns/down.sql rename to migrations/mysql/2018-09-19-144557_add_kdf_columns/down.sql diff --git a/migrations/2018-09-19-144557_add_kdf_columns/up.sql b/migrations/mysql/2018-09-19-144557_add_kdf_columns/up.sql similarity index 100% rename from migrations/2018-09-19-144557_add_kdf_columns/up.sql rename to migrations/mysql/2018-09-19-144557_add_kdf_columns/up.sql diff --git a/migrations/2018-11-27-152651_add_att_key_columns/down.sql b/migrations/mysql/2018-11-27-152651_add_att_key_columns/down.sql similarity index 100% rename from migrations/2018-11-27-152651_add_att_key_columns/down.sql rename to migrations/mysql/2018-11-27-152651_add_att_key_columns/down.sql diff --git a/migrations/mysql/2018-11-27-152651_add_att_key_columns/up.sql b/migrations/mysql/2018-11-27-152651_add_att_key_columns/up.sql new file mode 100644 index 00000000..e77599fc --- /dev/null +++ b/migrations/mysql/2018-11-27-152651_add_att_key_columns/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE attachments + ADD COLUMN + `key` TEXT; diff --git a/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/down.sql b/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/down.sql new file mode 100644 index 00000000..8a87d193 --- /dev/null +++ b/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/down.sql @@ -0,0 +1,7 @@ +ALTER TABLE attachments CHANGE COLUMN akey `key` TEXT; +ALTER TABLE ciphers CHANGE COLUMN atype type INTEGER NOT NULL; +ALTER TABLE devices CHANGE COLUMN atype type INTEGER NOT NULL; +ALTER TABLE twofactor CHANGE COLUMN atype type INTEGER NOT NULL; +ALTER TABLE users CHANGE COLUMN akey `key` TEXT; +ALTER TABLE users_organizations CHANGE COLUMN akey `key` TEXT; +ALTER TABLE users_organizations CHANGE COLUMN atype type INTEGER NOT NULL; \ No newline at end of file diff --git a/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/up.sql b/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/up.sql new file mode 100644 index 00000000..cee0351e --- /dev/null +++ b/migrations/mysql/2019-05-26-216651_rename_key_and_type_columns/up.sql @@ -0,0 +1,7 @@ +ALTER TABLE attachments CHANGE COLUMN `key` akey TEXT; +ALTER TABLE ciphers CHANGE COLUMN type atype INTEGER NOT NULL; +ALTER TABLE devices CHANGE COLUMN type atype INTEGER NOT NULL; +ALTER TABLE twofactor CHANGE COLUMN type atype INTEGER NOT NULL; +ALTER TABLE users CHANGE COLUMN `key` akey TEXT; +ALTER TABLE users_organizations CHANGE COLUMN `key` akey TEXT; +ALTER TABLE users_organizations CHANGE COLUMN type atype INTEGER NOT NULL; \ No newline at end of file diff --git a/migrations/sqlite/2018-01-14-171611_create_tables/down.sql b/migrations/sqlite/2018-01-14-171611_create_tables/down.sql new file mode 100644 index 00000000..1025a2c0 --- /dev/null +++ b/migrations/sqlite/2018-01-14-171611_create_tables/down.sql @@ -0,0 +1,9 @@ +DROP TABLE users; + +DROP TABLE devices; + +DROP TABLE ciphers; + +DROP TABLE attachments; + +DROP TABLE folders; \ No newline at end of file diff --git a/migrations/sqlite/2018-01-14-171611_create_tables/up.sql b/migrations/sqlite/2018-01-14-171611_create_tables/up.sql new file mode 100644 index 00000000..263ed8f3 --- /dev/null +++ b/migrations/sqlite/2018-01-14-171611_create_tables/up.sql @@ -0,0 +1,62 @@ +CREATE TABLE users ( + uuid TEXT NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + email TEXT NOT NULL UNIQUE, + name TEXT NOT NULL, + password_hash BLOB NOT NULL, + salt BLOB NOT NULL, + password_iterations INTEGER NOT NULL, + password_hint TEXT, + key TEXT NOT NULL, + private_key TEXT, + public_key TEXT, + totp_secret TEXT, + totp_recover TEXT, + security_stamp TEXT NOT NULL, + equivalent_domains TEXT NOT NULL, + excluded_globals TEXT NOT NULL +); + +CREATE TABLE devices ( + uuid TEXT NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + user_uuid TEXT NOT NULL REFERENCES users (uuid), + name TEXT NOT NULL, + type INTEGER NOT NULL, + push_token TEXT, + refresh_token TEXT NOT NULL +); + +CREATE TABLE ciphers ( + uuid TEXT NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + user_uuid TEXT NOT NULL REFERENCES users (uuid), + folder_uuid TEXT REFERENCES folders (uuid), + organization_uuid TEXT, + type INTEGER NOT NULL, + name TEXT NOT NULL, + notes TEXT, + fields TEXT, + data TEXT NOT NULL, + favorite BOOLEAN NOT NULL +); + +CREATE TABLE attachments ( + id TEXT NOT NULL PRIMARY KEY, + cipher_uuid TEXT NOT NULL REFERENCES ciphers (uuid), + file_name TEXT NOT NULL, + file_size INTEGER NOT NULL + +); + +CREATE TABLE folders ( + uuid TEXT NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + user_uuid TEXT NOT NULL REFERENCES users (uuid), + name TEXT NOT NULL +); + \ No newline at end of file diff --git a/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/down.sql b/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/down.sql new file mode 100644 index 00000000..b6446cd8 --- /dev/null +++ b/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/down.sql @@ -0,0 +1,8 @@ +DROP TABLE collections; + +DROP TABLE organizations; + + +DROP TABLE users_collections; + +DROP TABLE users_organizations; diff --git a/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/up.sql b/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/up.sql new file mode 100644 index 00000000..29601a4a --- /dev/null +++ b/migrations/sqlite/2018-02-17-205753_create_collections_and_orgs/up.sql @@ -0,0 +1,31 @@ +CREATE TABLE collections ( + uuid TEXT NOT NULL PRIMARY KEY, + org_uuid TEXT NOT NULL REFERENCES organizations (uuid), + name TEXT NOT NULL +); + +CREATE TABLE organizations ( + uuid TEXT NOT NULL PRIMARY KEY, + name TEXT NOT NULL, + billing_email TEXT NOT NULL +); + + +CREATE TABLE users_collections ( + user_uuid TEXT NOT NULL REFERENCES users (uuid), + collection_uuid TEXT NOT NULL REFERENCES collections (uuid), + PRIMARY KEY (user_uuid, collection_uuid) +); + +CREATE TABLE users_organizations ( + uuid TEXT NOT NULL PRIMARY KEY, + user_uuid TEXT NOT NULL REFERENCES users (uuid), + org_uuid TEXT NOT NULL REFERENCES organizations (uuid), + + access_all BOOLEAN NOT NULL, + key TEXT NOT NULL, + status INTEGER NOT NULL, + type INTEGER NOT NULL, + + UNIQUE (user_uuid, org_uuid) +); diff --git a/migrations/sqlite/2018-04-27-155151_create_users_ciphers/down.sql b/migrations/sqlite/2018-04-27-155151_create_users_ciphers/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2018-04-27-155151_create_users_ciphers/up.sql b/migrations/sqlite/2018-04-27-155151_create_users_ciphers/up.sql new file mode 100644 index 00000000..747f9e29 --- /dev/null +++ b/migrations/sqlite/2018-04-27-155151_create_users_ciphers/up.sql @@ -0,0 +1,34 @@ +ALTER TABLE ciphers RENAME TO oldCiphers; + +CREATE TABLE ciphers ( + uuid TEXT NOT NULL PRIMARY KEY, + created_at DATETIME NOT NULL, + updated_at DATETIME NOT NULL, + user_uuid TEXT REFERENCES users (uuid), -- Make this optional + organization_uuid TEXT REFERENCES organizations (uuid), -- Add reference to orgs table + -- Remove folder_uuid + type INTEGER NOT NULL, + name TEXT NOT NULL, + notes TEXT, + fields TEXT, + data TEXT NOT NULL, + favorite BOOLEAN NOT NULL +); + +CREATE TABLE folders_ciphers ( + cipher_uuid TEXT NOT NULL REFERENCES ciphers (uuid), + folder_uuid TEXT NOT NULL REFERENCES folders (uuid), + + PRIMARY KEY (cipher_uuid, folder_uuid) +); + +INSERT INTO ciphers (uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite) +SELECT uuid, created_at, updated_at, user_uuid, organization_uuid, type, name, notes, fields, data, favorite FROM oldCiphers; + +INSERT INTO folders_ciphers (cipher_uuid, folder_uuid) +SELECT uuid, folder_uuid FROM oldCiphers WHERE folder_uuid IS NOT NULL; + + +DROP TABLE oldCiphers; + +ALTER TABLE users_collections ADD COLUMN read_only BOOLEAN NOT NULL DEFAULT 0; -- False diff --git a/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/down.sql b/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/down.sql new file mode 100644 index 00000000..ba973f4f --- /dev/null +++ b/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/down.sql @@ -0,0 +1 @@ +DROP TABLE ciphers_collections; \ No newline at end of file diff --git a/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/up.sql b/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/up.sql new file mode 100644 index 00000000..9fdd7066 --- /dev/null +++ b/migrations/sqlite/2018-05-08-161616_create_collection_cipher_map/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE ciphers_collections ( + cipher_uuid TEXT NOT NULL REFERENCES ciphers (uuid), + collection_uuid TEXT NOT NULL REFERENCES collections (uuid), + PRIMARY KEY (cipher_uuid, collection_uuid) +); \ No newline at end of file diff --git a/migrations/sqlite/2018-05-25-232323_update_attachments_reference/down.sql b/migrations/sqlite/2018-05-25-232323_update_attachments_reference/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2018-05-25-232323_update_attachments_reference/up.sql b/migrations/sqlite/2018-05-25-232323_update_attachments_reference/up.sql new file mode 100644 index 00000000..b7adb77e --- /dev/null +++ b/migrations/sqlite/2018-05-25-232323_update_attachments_reference/up.sql @@ -0,0 +1,14 @@ +ALTER TABLE attachments RENAME TO oldAttachments; + +CREATE TABLE attachments ( + id TEXT NOT NULL PRIMARY KEY, + cipher_uuid TEXT NOT NULL REFERENCES ciphers (uuid), + file_name TEXT NOT NULL, + file_size INTEGER NOT NULL + +); + +INSERT INTO attachments (id, cipher_uuid, file_name, file_size) +SELECT id, cipher_uuid, file_name, file_size FROM oldAttachments; + +DROP TABLE oldAttachments; \ No newline at end of file diff --git a/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/down.sql b/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/down.sql new file mode 100644 index 00000000..291a97c5 --- /dev/null +++ b/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/down.sql @@ -0,0 +1 @@ +-- This file should undo anything in `up.sql` \ No newline at end of file diff --git a/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/up.sql b/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/up.sql new file mode 100644 index 00000000..aaad8eab --- /dev/null +++ b/migrations/sqlite/2018-06-01-112529_update_devices_twofactor_remember/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE devices + ADD COLUMN + twofactor_remember TEXT; \ No newline at end of file diff --git a/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/down.sql b/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/down.sql new file mode 100644 index 00000000..84b6ee63 --- /dev/null +++ b/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/down.sql @@ -0,0 +1,8 @@ +UPDATE users +SET totp_secret = ( + SELECT twofactor.data FROM twofactor + WHERE twofactor.type = 0 + AND twofactor.user_uuid = users.uuid +); + +DROP TABLE twofactor; \ No newline at end of file diff --git a/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/up.sql b/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/up.sql new file mode 100644 index 00000000..3e78e721 --- /dev/null +++ b/migrations/sqlite/2018-07-11-181453_create_u2f_twofactor/up.sql @@ -0,0 +1,15 @@ +CREATE TABLE twofactor ( + uuid TEXT NOT NULL PRIMARY KEY, + user_uuid TEXT NOT NULL REFERENCES users (uuid), + type INTEGER NOT NULL, + enabled BOOLEAN NOT NULL, + data TEXT NOT NULL, + + UNIQUE (user_uuid, type) +); + + +INSERT INTO twofactor (uuid, user_uuid, type, enabled, data) +SELECT lower(hex(randomblob(16))) , uuid, 0, 1, u.totp_secret FROM users u where u.totp_secret IS NOT NULL; + +UPDATE users SET totp_secret = NULL; -- Instead of recreating the table, just leave the columns empty \ No newline at end of file diff --git a/migrations/sqlite/2018-08-27-172114_update_ciphers/down.sql b/migrations/sqlite/2018-08-27-172114_update_ciphers/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2018-08-27-172114_update_ciphers/up.sql b/migrations/sqlite/2018-08-27-172114_update_ciphers/up.sql new file mode 100644 index 00000000..624efd66 --- /dev/null +++ b/migrations/sqlite/2018-08-27-172114_update_ciphers/up.sql @@ -0,0 +1,3 @@ +ALTER TABLE ciphers + ADD COLUMN + password_history TEXT; \ No newline at end of file diff --git a/migrations/sqlite/2018-09-10-111213_add_invites/down.sql b/migrations/sqlite/2018-09-10-111213_add_invites/down.sql new file mode 100644 index 00000000..af3776cf --- /dev/null +++ b/migrations/sqlite/2018-09-10-111213_add_invites/down.sql @@ -0,0 +1 @@ +DROP TABLE invitations; \ No newline at end of file diff --git a/migrations/sqlite/2018-09-10-111213_add_invites/up.sql b/migrations/sqlite/2018-09-10-111213_add_invites/up.sql new file mode 100644 index 00000000..b42e9a2a --- /dev/null +++ b/migrations/sqlite/2018-09-10-111213_add_invites/up.sql @@ -0,0 +1,3 @@ +CREATE TABLE invitations ( + email TEXT NOT NULL PRIMARY KEY +); \ No newline at end of file diff --git a/migrations/sqlite/2018-09-19-144557_add_kdf_columns/down.sql b/migrations/sqlite/2018-09-19-144557_add_kdf_columns/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2018-09-19-144557_add_kdf_columns/up.sql b/migrations/sqlite/2018-09-19-144557_add_kdf_columns/up.sql new file mode 100644 index 00000000..bd98f40e --- /dev/null +++ b/migrations/sqlite/2018-09-19-144557_add_kdf_columns/up.sql @@ -0,0 +1,7 @@ +ALTER TABLE users + ADD COLUMN + client_kdf_type INTEGER NOT NULL DEFAULT 0; -- PBKDF2 + +ALTER TABLE users + ADD COLUMN + client_kdf_iter INTEGER NOT NULL DEFAULT 5000; diff --git a/migrations/sqlite/2018-11-27-152651_add_att_key_columns/down.sql b/migrations/sqlite/2018-11-27-152651_add_att_key_columns/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/2018-11-27-152651_add_att_key_columns/up.sql b/migrations/sqlite/2018-11-27-152651_add_att_key_columns/up.sql similarity index 72% rename from migrations/2018-11-27-152651_add_att_key_columns/up.sql rename to migrations/sqlite/2018-11-27-152651_add_att_key_columns/up.sql index 858d3778..4a73a8c9 100644 --- a/migrations/2018-11-27-152651_add_att_key_columns/up.sql +++ b/migrations/sqlite/2018-11-27-152651_add_att_key_columns/up.sql @@ -1,3 +1,3 @@ ALTER TABLE attachments ADD COLUMN - akey TEXT; + key TEXT; \ No newline at end of file diff --git a/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/down.sql b/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/down.sql new file mode 100644 index 00000000..4e6902aa --- /dev/null +++ b/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/down.sql @@ -0,0 +1,7 @@ +ALTER TABLE attachments RENAME COLUMN akey TO key; +ALTER TABLE ciphers RENAME COLUMN atype TO type; +ALTER TABLE devices RENAME COLUMN atype TO type; +ALTER TABLE twofactor RENAME COLUMN atype TO type; +ALTER TABLE users RENAME COLUMN akey TO key; +ALTER TABLE users_organizations RENAME COLUMN akey TO key; +ALTER TABLE users_organizations RENAME COLUMN atype TO type; \ No newline at end of file diff --git a/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/up.sql b/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/up.sql new file mode 100644 index 00000000..ab2c5b52 --- /dev/null +++ b/migrations/sqlite/2019-05-26-216651_rename_key_and_type_columns/up.sql @@ -0,0 +1,7 @@ +ALTER TABLE attachments RENAME COLUMN key TO akey; +ALTER TABLE ciphers RENAME COLUMN type TO atype; +ALTER TABLE devices RENAME COLUMN type TO atype; +ALTER TABLE twofactor RENAME COLUMN type TO atype; +ALTER TABLE users RENAME COLUMN key TO akey; +ALTER TABLE users_organizations RENAME COLUMN key TO akey; +ALTER TABLE users_organizations RENAME COLUMN type TO atype; \ No newline at end of file diff --git a/src/config.rs b/src/config.rs index b599bedc..be0b5152 100644 --- a/src/config.rs +++ b/src/config.rs @@ -204,8 +204,8 @@ make_config! { data_folder: String, false, def, "data".to_string(); /// Database URL - /// docker run -e MYSQL_ROOT_PASSWORD=my-secret-pw -e MYSQL_DATABASE=bitwarden -p 3306:3306 -d mysql:5.7 - database_url: String, false, auto, |_c| format!("mysql://root:my-secret-pw@0.0.0.0:3306/bitwarden"); + /// Database URL + database_url: String, false, auto, |c| format!("{}/{}", c.data_folder, "db.sqlite3"); /// Icon chache folder icon_cache_folder: String, false, auto, |c| format!("{}/{}", c.data_folder, "icon_cache"); /// Attachments folder diff --git a/src/db/mod.rs b/src/db/mod.rs index 18bf0969..6a60ae43 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,6 +2,9 @@ use std::ops::Deref; use diesel::r2d2; use diesel::r2d2::ConnectionManager; +#[cfg(feature = "sqlite")] +use diesel::sqlite::SqliteConnection; +#[cfg(feature = "mysql")] use diesel::mysql::MysqlConnection; use diesel::{Connection as DieselConnection, ConnectionError}; @@ -12,6 +15,9 @@ use rocket::{Outcome, Request, State}; use crate::CONFIG; /// An alias to the database connection used +#[cfg(feature = "sqlite")] +type Connection = SqliteConnection; +#[cfg(feature = "mysql")] type Connection = MysqlConnection; /// An alias to the type for a pool of Diesel MySQL connections. @@ -21,7 +27,13 @@ type Pool = r2d2::Pool>; pub struct DbConn(pub r2d2::PooledConnection>); pub mod models; +#[cfg(feature = "sqlite")] +#[path = "schemas/sqlite/schema.rs"] pub mod schema; +#[cfg(feature = "mysql")] +#[path = "schemas/mysql/schema.rs"] +pub mod schema; + /// Initializes a database pool. pub fn init_pool() -> Pool { diff --git a/src/db/schema.rs b/src/db/schemas/mysql/schema.rs similarity index 100% rename from src/db/schema.rs rename to src/db/schemas/mysql/schema.rs diff --git a/src/db/schemas/sqlite/schema.rs b/src/db/schemas/sqlite/schema.rs new file mode 100644 index 00000000..1bc924c9 --- /dev/null +++ b/src/db/schemas/sqlite/schema.rs @@ -0,0 +1,172 @@ +table! { + attachments (id) { + id -> Text, + cipher_uuid -> Text, + file_name -> Text, + file_size -> Integer, + akey -> Nullable, + } +} + +table! { + ciphers (uuid) { + uuid -> Text, + created_at -> Timestamp, + updated_at -> Timestamp, + user_uuid -> Nullable, + organization_uuid -> Nullable, + atype -> Integer, + name -> Text, + notes -> Nullable, + fields -> Nullable, + data -> Text, + favorite -> Bool, + password_history -> Nullable, + } +} + +table! { + ciphers_collections (cipher_uuid, collection_uuid) { + cipher_uuid -> Text, + collection_uuid -> Text, + } +} + +table! { + collections (uuid) { + uuid -> Text, + org_uuid -> Text, + name -> Text, + } +} + +table! { + devices (uuid) { + uuid -> Text, + created_at -> Timestamp, + updated_at -> Timestamp, + user_uuid -> Text, + name -> Text, + atype -> Integer, + push_token -> Nullable, + refresh_token -> Text, + twofactor_remember -> Nullable, + } +} + +table! { + folders (uuid) { + uuid -> Text, + created_at -> Timestamp, + updated_at -> Timestamp, + user_uuid -> Text, + name -> Text, + } +} + +table! { + folders_ciphers (cipher_uuid, folder_uuid) { + cipher_uuid -> Text, + folder_uuid -> Text, + } +} + +table! { + invitations (email) { + email -> Text, + } +} + +table! { + organizations (uuid) { + uuid -> Text, + name -> Text, + billing_email -> Text, + } +} + +table! { + twofactor (uuid) { + uuid -> Text, + user_uuid -> Text, + atype -> Integer, + enabled -> Bool, + data -> Text, + } +} + +table! { + users (uuid) { + uuid -> Text, + created_at -> Timestamp, + updated_at -> Timestamp, + email -> Text, + name -> Text, + password_hash -> Binary, + salt -> Binary, + password_iterations -> Integer, + password_hint -> Nullable, + akey -> Text, + private_key -> Nullable, + public_key -> Nullable, + totp_secret -> Nullable, + totp_recover -> Nullable, + security_stamp -> Text, + equivalent_domains -> Text, + excluded_globals -> Text, + client_kdf_type -> Integer, + client_kdf_iter -> Integer, + } +} + +table! { + users_collections (user_uuid, collection_uuid) { + user_uuid -> Text, + collection_uuid -> Text, + read_only -> Bool, + } +} + +table! { + users_organizations (uuid) { + uuid -> Text, + user_uuid -> Text, + org_uuid -> Text, + access_all -> Bool, + akey -> Text, + status -> Integer, + atype -> Integer, + } +} + +joinable!(attachments -> ciphers (cipher_uuid)); +joinable!(ciphers -> organizations (organization_uuid)); +joinable!(ciphers -> users (user_uuid)); +joinable!(ciphers_collections -> ciphers (cipher_uuid)); +joinable!(ciphers_collections -> collections (collection_uuid)); +joinable!(collections -> organizations (org_uuid)); +joinable!(devices -> users (user_uuid)); +joinable!(folders -> users (user_uuid)); +joinable!(folders_ciphers -> ciphers (cipher_uuid)); +joinable!(folders_ciphers -> folders (folder_uuid)); +joinable!(twofactor -> users (user_uuid)); +joinable!(users_collections -> collections (collection_uuid)); +joinable!(users_collections -> users (user_uuid)); +joinable!(users_organizations -> organizations (org_uuid)); +joinable!(users_organizations -> users (user_uuid)); + +allow_tables_to_appear_in_same_query!( + attachments, + ciphers, + ciphers_collections, + collections, + devices, + folders, + folders_ciphers, + invitations, + organizations, + twofactor, + users, + users_collections, + users_organizations, +); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index c34eb2d0..21e55eb5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -191,7 +191,11 @@ fn check_web_vault() { // https://docs.rs/diesel_migrations/*/diesel_migrations/macro.embed_migrations.html #[allow(unused_imports)] mod migrations { - embed_migrations!(); + + #[cfg(feature = "sqlite")] + embed_migrations!("migrations/sqlite"); + #[cfg(feature = "mysql")] + embed_migrations!("migrations/mysql"); pub fn run_migrations() { // Make sure the database is up to date (create if it doesn't exist, or run the migrations)