forked from mongodb/mongo-rust-driver
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstate_machine.rs
171 lines (165 loc) · 7.43 KB
/
state_machine.rs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
use std::convert::TryInto;
use bson::{Document, RawDocument, RawDocumentBuf};
use futures_util::{stream, TryStreamExt};
use mongocrypt::ctx::{Ctx, State};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
sync::oneshot,
};
use crate::{
client::options::ServerAddress,
cmap::options::StreamOptions,
error::{Error, Result},
operation::{RawOutput, RunCommand},
runtime::AsyncStream,
Client,
};
impl Client {
pub(crate) async fn run_mongocrypt_ctx(
&self,
ctx: Ctx,
db: Option<&str>,
) -> Result<RawDocumentBuf> {
let guard = self.inner.csfle.read().await;
let csfle = match guard.as_ref() {
Some(csfle) => csfle,
None => return Err(Error::internal("no csfle state for mongocrypt ctx")),
};
let mut result = None;
// This needs to be a `Result` so that the `Ctx` can be temporarily owned by the processing
// thread for crypto finalization. An `Option` would also work here, but `Result` means we
// can return a helpful error if things get into a broken state rather than panicing.
let mut ctx = Ok(ctx);
loop {
let state = result_ref(&ctx)?.state()?;
match state {
State::NeedMongoCollinfo => {
let ctx = result_mut(&mut ctx)?;
let filter = raw_to_doc(ctx.mongo_op()?)?;
let metadata_client = csfle
.aux_clients
.metadata_client
.as_ref()
.and_then(|w| w.upgrade())
.ok_or_else(|| {
Error::internal("metadata_client required for NeedMongoCollinfo state")
})?;
let db = metadata_client.database(db.as_ref().ok_or_else(|| {
Error::internal("db required for NeedMongoCollinfo state")
})?);
let mut cursor = db.list_collections(filter, None).await?;
if cursor.advance().await? {
ctx.mongo_feed(cursor.current())?;
}
ctx.mongo_done()?;
}
State::NeedMongoMarkings => {
let ctx = result_mut(&mut ctx)?;
let command = ctx.mongo_op()?.to_raw_document_buf();
let db = db.as_ref().ok_or_else(|| {
Error::internal("db required for NeedMongoMarkings state")
})?;
let op = RawOutput(RunCommand::new_raw(db.to_string(), command, None, None)?);
let mongocryptd_client = csfle
.mongocryptd_client
.as_ref()
.ok_or_else(|| Error::internal("mongocryptd client not found"))?;
let response = mongocryptd_client.execute_operation(op, None).await?;
ctx.mongo_feed(response.raw_body())?;
ctx.mongo_done()?;
}
State::NeedMongoKeys => {
let ctx = result_mut(&mut ctx)?;
let filter = raw_to_doc(ctx.mongo_op()?)?;
let kv_ns = &csfle.opts.key_vault_namespace;
let kv_client = csfle
.aux_clients
.key_vault_client
.upgrade()
.ok_or_else(|| Error::internal("key vault client dropped"))?;
let kv_coll = kv_client
.database(&kv_ns.db)
.collection::<RawDocumentBuf>(&kv_ns.coll);
let mut cursor = kv_coll.find(filter, None).await?;
while cursor.advance().await? {
ctx.mongo_feed(cursor.current())?;
}
ctx.mongo_done()?;
}
State::NeedKms => {
let ctx = result_mut(&mut ctx)?;
let scope = ctx.kms_scope();
let mut kms_ctxen: Vec<Result<_>> = vec![];
while let Some(kms_ctx) = scope.next_kms_ctx() {
kms_ctxen.push(Ok(kms_ctx));
}
stream::iter(kms_ctxen)
.try_for_each_concurrent(None, |mut kms_ctx| async move {
let endpoint = kms_ctx.endpoint()?;
let addr = ServerAddress::parse(endpoint)?;
let provider = kms_ctx.kms_provider()?;
let tls_options = csfle
.opts()
.tls_options
.as_ref()
.and_then(|tls| tls.get(&provider))
.cloned()
.unwrap_or_default();
let mut stream = AsyncStream::connect(
StreamOptions::builder()
.address(addr)
.tls_options(tls_options)
.build(),
)
.await?;
stream.write_all(kms_ctx.message()?).await?;
let mut buf = vec![];
while kms_ctx.bytes_needed() > 0 {
let buf_size = kms_ctx.bytes_needed().try_into().map_err(|e| {
Error::internal(format!("buffer size overflow: {}", e))
})?;
buf.resize(buf_size, 0);
let count = stream.read(&mut buf).await?;
kms_ctx.feed(&buf[0..count])?;
}
Ok(())
})
.await?;
}
State::NeedKmsCredentials => todo!("RUST-1314"),
State::Ready => {
let (tx, rx) = oneshot::channel();
let mut thread_ctx = std::mem::replace(
&mut ctx,
Err(Error::internal("crypto context not present")),
)?;
csfle.crypto_threads.spawn(move || {
let result = thread_ctx.finalize().map(|doc| doc.to_owned());
let _ = tx.send((thread_ctx, result));
});
let (ctx_again, output) = rx
.await
.map_err(|_| Error::internal("crypto thread dropped"))?;
ctx = Ok(ctx_again);
result = Some(output?);
}
State::Done => break,
s => return Err(Error::internal(format!("unhandled state {:?}", s))),
}
}
match result {
Some(doc) => Ok(doc),
None => Err(Error::internal("libmongocrypt terminated without output")),
}
}
}
fn result_ref<T>(r: &Result<T>) -> Result<&T> {
r.as_ref().map_err(Error::clone)
}
fn result_mut<T>(r: &mut Result<T>) -> Result<&mut T> {
r.as_mut().map_err(|e| e.clone())
}
fn raw_to_doc(raw: &RawDocument) -> Result<Document> {
raw.try_into()
.map_err(|e| Error::internal(format!("could not parse raw document: {}", e)))
}