1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//! The in-process backend (feature `embedded`): the server's rules inside
//! this process, over a data directory, with no server to run.
//!
//! [`open`] takes a data directory with the layout `gv-server local` uses
//! (`vault.db` and a file journal under `journal-root/`, mode 0700), locks
//! it, replays its journal, and returns an [`Embedded`] transport. Give it to
//! [`Api::new`](crate::client::Api::new), and everything the SDK does over
//! HTTP it does in-process, through `galata-vault-server-core`. Every request is signed
//! exactly as it would be for HTTP, and the core authenticates it, checks its
//! scope and the read allow-list, applies the quotas and preconditions,
//! audits it, and journals what is critical. There is no socket, no HTTP,
//! no async runtime, no signal handler, and no output.
//!
//! ```no_run
//! use galata_vault::client::Api;
//! use galata_vault::embedded;
//!
//! let api = Api::new(embedded::open("/var/lib/my-app/vaults")?);
//! let vault = galata_vault::Vault::with_api("gvt1_…", &api)?;
//! # Ok::<(), galata_vault::Error>(())
//! ```
//!
//! The owner API reaches it through a connector:
//! `Owner::with_connector(move |_server| Ok(api.clone()))`. Pin such a
//! project to `http://127.0.0.1:8750`, the address `gv-server local` serves
//! a data directory on, and the same project can later move to a local
//! server on the same directory without a change.
//!
//! # When to use it
//!
//! * A single-process application, a test, or a tool that should work with
//! no server running.
//! * Not a fleet. A data directory has one opener at a time: a second
//! [`open`], in this process or another, and a `gv-server local` on the
//! same directory, are refused with `data_dir_in_use`. Several processes
//! share vaults by running `gv-server local` and talking to it over HTTP.
//! A directory written through here can be served by `gv-server local`
//! once it is closed, and the other way round.
//!
//! The policy is the default one: no proof of work, no request budget, and
//! no vault ever expires for inactivity.
//!
//! # What it does not protect against
//!
//! The embedding process can open `vault.db` and change it directly, around
//! the core. Scopes, the read allow-list, quotas and audit rows bind only
//! what goes through [`Embedded`]; the same is true of any process that runs
//! as the same user as a `gv-server local`. What still binds the embedding
//! process is the cryptography the SDK checks on every read:
//!
//! * the vault's owner key is verified against the vault id the caller
//! holds, in its token or key;
//! * descriptors, bundles and every record are verified against owner-signed
//! keys, so a record the embedding process forged or edited is an
//! integrity error, never a value;
//! * values open only with keys the caller holds;
//! * [`Vault::verify_audit`](crate::Vault::verify_audit) detects a rewritten
//! or rolled-back audit chain from a head the caller kept.
//!
//! A process with the file can still withhold or delete what it holds.
use Path;
use Arc;
use crate;
use crate;
use crate;
use crate;
use crate;
/// A data directory open in this process: a [`Transport`] that serves every
/// request from `galata-vault-server-core`. Dropping it (and every `Api` sharing it)
/// closes the directory for the next opener.
/// Open `data_dir`, creating it with mode 0700 if it does not exist, and
/// replay its journal. Refused with `data_dir_in_use` while another opener
/// has it, and with `invalid_data_dir` when it is not a directory, its group
/// or others can reach it, or (outside Unix) its permissions cannot be
/// checked.