-
-
Notifications
You must be signed in to change notification settings - Fork 563
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
feat(rust): persist application data in a database #6913
Merged
etorreborre
merged 1 commit into
develop
from
etorreborre/refactor/new-storage-prototype
Nov 28, 2023
Merged
feat(rust): persist application data in a database #6913
etorreborre
merged 1 commit into
develop
from
etorreborre/refactor/new-storage-prototype
Nov 28, 2023
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
9 times, most recently
from
November 17, 2023 18:14
ecb02d1
to
417e22f
Compare
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
14 times, most recently
from
November 20, 2023 19:13
35f865d
to
b10a06b
Compare
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Outdated
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Outdated
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Outdated
Show resolved
Hide resolved
...ons/rust/ockam/ockam_node/src/storage/database/migrations/20231006100000_create_database.sql
Outdated
Show resolved
Hide resolved
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
2 times, most recently
from
November 21, 2023 11:55
139b5e8
to
82f3f07
Compare
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_database.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_types.rs
Outdated
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_types.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_types.rs
Show resolved
Hide resolved
implementations/rust/ockam/ockam_node/src/storage/database/sqlx_types.rs
Outdated
Show resolved
Hide resolved
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
from
November 22, 2023 11:05
ab3bac7
to
69ab0e6
Compare
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
15 times, most recently
from
November 28, 2023 14:45
86b37ba
to
000a53b
Compare
polvorin
previously approved these changes
Nov 28, 2023
etorreborre
force-pushed
the
etorreborre/refactor/new-storage-prototype
branch
from
November 28, 2023 16:55
000a53b
to
0334aeb
Compare
polvorin
approved these changes
Nov 28, 2023
Merged
5 tasks
24 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR replaces the current storage of persistent data in separated files with the storage of data is a SQLite database.
Motivation
There is a large amount of data need to run an Ockam node or application:
All this data is inter-related:
At the moment, everything is stored in files (and some information just lives in memory, like a started TCP inlet).
Managing those files separately makes it hard to process the relationships between the data and creates some operational issues (there's no way to have atomic transactions to modify data for example), so a database fits better our data model.
Changes
This is a large PR because the access to data, via the
CliState
struct was not very well encapsulated. As a result it was not possible to just replace one persistence solution with another to leave most of the code unchanged.The following sections describe the major changes, from low-level crates to high-level ones.
ockam_node
In this crate we introduce a
storage/database
folder with anSqlxDatabase
.sqlx
is the Rust library used to handle the database connections and queries. It has been chosen because it supports several "drivers", SQLite, but also Postgres, if we need to migrate to Postgres in the future.When an
SqlxDatabase
is started on a given path, a migration file is applied to create or migrate all the SQL tables we need to persist data. The migration files are available insrc/storage/database/migration
.Additionally there are traits and conversion functions to allow the data types used in Ockam:
Identifier
,ChangeHistory
,PurposeKey
etc... to be mapped to simpler SQLite types when inserting data.Note that there was no real attempt to make the current schema optimum. But with migrations we should be able to evolve it gracefully.
ockam_vault
In that crate we replaced the persistence of secrets in JSON file with key/values to a table in the database.
Note that the access to secrets is done via an interface,
SecretsRepository
, and that the implementation currently uses one single database for all data but we could as well persist this information in a separated database file since we never do queries across the table storing secrets and other tables.ockam_identity
In that crate we have now 3 repositories (each of them backed by a
SqlxDatabase
):ChangeHistoryRepository
to store theChangeHistories
. Note that we don't storeVerifiedChanges
in order to avoid someone changing the history of an identity by just accessing the database file. When aChangeHistory
is loaded from the database it has to be verified in order to get a fullIdentity
IdentityAttributesRepository
to store the attributes associated to an identifier (I reunited the read and the write interface because I don't think that the split is that useful)PurposeKeysRepository
to store keys and their associated purpose (secure channel or credentials)Then most of the changes come from:
IdentitiesRepository
intoChangeHistoryRepository
+IdentityAttributesRepository
Credential
ockam_abac
A new
PolicyRepository
interface has been added to that crate, with the correspondingPolicySqlxDatabase
implementation.The previous implementations have been removed.
ockam_api
This is the crate with the largest amount of changes. The
CliState
struct holds all the data that we are interested in via a set of repositories:IdentitiesRepository
to store named identities, i.e. the fact that we can associate a name to an identity and set a default identityCredentialsRepository
to store named credentialsVaultsRepository
to store named vaultsNodesRepository
to store data about background nodes (their name, their pid, etc...)TrustContextsRepository
to store data about trust contextsProjectsRepository
to store project dataSpacesRepository
to store space data (note that the association betweenSpace
andProject
is not exposed at the repository level but is implicit with thespace_name
field inProject
)EnrollmentsRepository
to keep track of enrolling information for usersUsersRepository
to store user info as retrieved after authenticationModelStateRepository
to store data for the desktop application: created inlets and outletsThe functions exposed by the repositories cannot be accessed directly. They are hidden behind proper methods on the
CliState
. For example creating aNode
involves several repositories and this logic is hidden behind theCliState::create_node
function (or one of its variants).The main
CliState
functions are provided in files, domain by domain:nodes
credentials
projects
spaces
identities
vaults
nodes
trust_contexts
users
enrollments
policies
secure_channels
ockam_command
Most of the functions changed in that crate now call a "facade" function on
CliState
to access or modify the local state.In other cases like getting the list of all projects an
InMemoryNode
is started to first synchronize the local state with theController
state. This kind of code could be moved to theCliState
as well since it has all the necessary information without having to start anInMemoryNode
.ockam_app_lib
The changes here are very similar to the changes to the
ockam_command
crate. Most of the places whereCliState
was accessed to get or modify data, I had to modify calls.One notable addition is the
ModelStateRepository
to store "outlet statuses" and "incoming services" in the database.