/
agent.rs
560 lines (502 loc) · 20.2 KB
/
agent.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Bootstrap-related APIs.
use super::client::Client as BootstrapAgentClient;
use super::config::{Config, BOOTSTRAP_AGENT_PORT};
use super::ddm_admin_client::{DdmAdminClient, DdmError};
use super::params::SledAgentRequest;
use super::rss_handle::RssHandle;
use super::server::TrustQuorumMembership;
use super::trust_quorum::{
RackSecret, SerializableShareDistribution, ShareDistribution,
TrustQuorumError,
};
use super::views::SledAgentResponse;
use crate::config::Config as SledConfig;
use crate::illumos::dladm::{self, Dladm, PhysicalLink};
use crate::illumos::zone::Zones;
use crate::server::Server as SledServer;
use crate::sp::SpHandle;
use omicron_common::address::{get_sled_address, Ipv6Subnet};
use omicron_common::api::external::{Error as ExternalError, MacAddr};
use omicron_common::backoff::{
internal_service_policy, retry_notify, BackoffError,
};
use serde::{Deserialize, Serialize};
use slog::Logger;
use std::borrow::Cow;
use std::collections::HashSet;
use std::net::{Ipv6Addr, SocketAddrV6};
use std::path::{Path, PathBuf};
use std::sync::Arc;
use thiserror::Error;
use tokio::sync::Mutex;
/// Initial octet of IPv6 for bootstrap addresses.
pub(crate) const BOOTSTRAP_PREFIX: u16 = 0xfdb0;
/// IPv6 prefix mask for bootstrap addresses.
pub(crate) const BOOTSTRAP_MASK: u8 = 64;
/// Describes errors which may occur while operating the bootstrap service.
#[derive(Error, Debug)]
pub enum BootstrapError {
#[error("IO error: {message}: {err}")]
Io {
message: String,
#[source]
err: std::io::Error,
},
#[error("Error contacting ddmd: {0}")]
DdmError(#[from] DdmError),
#[error("Error starting sled agent: {0}")]
SledError(String),
#[error("Error deserializing toml from {path}: {err}")]
Toml { path: PathBuf, err: toml::de::Error },
#[error(transparent)]
TrustQuorum(#[from] TrustQuorumError),
#[error("Error collecting peer addresses: {0}")]
PeerAddresses(String),
#[error("Failed to initialize bootstrap address: {err}")]
BootstrapAddress { err: crate::illumos::zone::EnsureGzAddressError },
}
impl From<BootstrapError> for ExternalError {
fn from(err: BootstrapError) -> Self {
Self::internal_error(&err.to_string())
}
}
/// The entity responsible for bootstrapping an Oxide rack.
pub(crate) struct Agent {
/// Debug log
log: Logger,
/// Store the parent log - without "component = BootstrapAgent" - so
/// other launched components can set their own value.
parent_log: Logger,
address: Ipv6Addr,
/// Our share of the rack secret, if we have one.
share: Mutex<Option<ShareDistribution>>,
rss: Mutex<Option<RssHandle>>,
sled_agent: Mutex<Option<SledServer>>,
sled_config: SledConfig,
sp: Option<SpHandle>,
ddmd_client: DdmAdminClient,
}
fn get_sled_agent_request_path() -> PathBuf {
Path::new(omicron_common::OMICRON_CONFIG_PATH)
.join("sled-agent-request.toml")
}
fn mac_to_socket_addr(mac: MacAddr) -> SocketAddrV6 {
let mac_bytes = mac.into_array();
assert_eq!(6, mac_bytes.len());
let address = Ipv6Addr::new(
BOOTSTRAP_PREFIX,
((mac_bytes[0] as u16) << 8) | mac_bytes[1] as u16,
((mac_bytes[2] as u16) << 8) | mac_bytes[3] as u16,
((mac_bytes[4] as u16) << 8) | mac_bytes[5] as u16,
0,
0,
0,
1,
);
SocketAddrV6::new(address, BOOTSTRAP_AGENT_PORT, 0, 0)
}
// TODO(https://github.com/oxidecomputer/omicron/issues/945): This address
// could be randomly generated when it no longer needs to be durable.
pub fn bootstrap_address(
link: PhysicalLink,
) -> Result<SocketAddrV6, dladm::GetMacError> {
let mac = Dladm::get_mac(link)?;
Ok(mac_to_socket_addr(mac))
}
impl Agent {
pub async fn new(
log: Logger,
sled_config: SledConfig,
address: Ipv6Addr,
sp: Option<SpHandle>,
) -> Result<(Self, TrustQuorumMembership), BootstrapError> {
let ba_log = log.new(o!(
"component" => "BootstrapAgent",
"server" => sled_config.id.to_string(),
));
// We expect this directory to exist - ensure that it does, before any
// subsequent operations which may write configs here.
info!(
log, "Ensuring config directory exists";
"path" => omicron_common::OMICRON_CONFIG_PATH,
);
tokio::fs::create_dir_all(omicron_common::OMICRON_CONFIG_PATH)
.await
.map_err(|err| BootstrapError::Io {
message: format!(
"Creating config directory {}",
omicron_common::OMICRON_CONFIG_PATH
),
err,
})?;
let etherstub = Dladm::ensure_etherstub().map_err(|e| {
BootstrapError::SledError(format!(
"Can't access etherstub device: {}",
e
))
})?;
let etherstub_vnic =
Dladm::ensure_etherstub_vnic(ðerstub).map_err(|e| {
BootstrapError::SledError(format!(
"Can't access etherstub VNIC device: {}",
e
))
})?;
Zones::ensure_has_global_zone_v6_address(
etherstub_vnic,
address,
"bootstrap6",
)
.map_err(|err| BootstrapError::BootstrapAddress { err })?;
// Start trying to notify ddmd of our bootstrap address so it can
// advertise it to other sleds.
let ddmd_client = DdmAdminClient::new(log.clone())?;
ddmd_client.advertise_prefix(Ipv6Subnet::new(address));
let agent = Agent {
log: ba_log,
parent_log: log,
address,
share: Mutex::new(None),
rss: Mutex::new(None),
sled_agent: Mutex::new(None),
sled_config,
sp,
ddmd_client,
};
let request_path = get_sled_agent_request_path();
let trust_quorum = if request_path.exists() {
info!(agent.log, "Sled already configured, loading sled agent");
let sled_request: PersistentSledAgentRequest = toml::from_str(
&tokio::fs::read_to_string(&request_path).await.map_err(
|err| BootstrapError::Io {
message: format!(
"Reading subnet path from {request_path:?}"
),
err,
},
)?,
)
.map_err(|err| BootstrapError::Toml { path: request_path, err })?;
let trust_quorum_share =
sled_request.trust_quorum_share.map(ShareDistribution::from);
agent
.request_agent(&sled_request.request, &trust_quorum_share)
.await?;
TrustQuorumMembership::Known(Arc::new(trust_quorum_share))
} else {
TrustQuorumMembership::Uninitialized
};
Ok((agent, trust_quorum))
}
/// Initializes the Sled Agent on behalf of the RSS.
///
/// If the Sled Agent has already been initialized:
/// - This method is idempotent for the same request
/// - Thie method returns an error for different requests
pub async fn request_agent(
&self,
request: &SledAgentRequest,
trust_quorum_share: &Option<ShareDistribution>,
) -> Result<SledAgentResponse, BootstrapError> {
info!(&self.log, "Loading Sled Agent: {:?}", request);
let sled_address = get_sled_address(request.subnet);
let mut maybe_agent = self.sled_agent.lock().await;
if let Some(server) = &*maybe_agent {
// Server already exists, return it.
info!(&self.log, "Sled Agent already loaded");
if &server.address().ip() != sled_address.ip() {
let err_str = format!(
"Sled Agent already running on address {}, but {} was requested",
server.address().ip(),
sled_address.ip(),
);
return Err(BootstrapError::SledError(err_str));
}
// Bail out if this request includes a trust quorum share that
// doesn't match ours. TODO-correctness Do we need to handle a
// partially-initialized rack where we may have a share from a
// previously-started-but-not-completed init process? If rerunning
// it produces different shares this check will fail.
if *trust_quorum_share != *self.share.lock().await {
let err_str = concat!(
"Sled Agent already running with",
" a different trust quorum share"
)
.to_string();
return Err(BootstrapError::SledError(err_str));
}
return Ok(SledAgentResponse { id: server.id() });
}
if let Some(share) = trust_quorum_share.clone() {
self.establish_sled_quorum(share.clone()).await?;
*self.share.lock().await = Some(share);
}
// Server does not exist, initialize it.
let server = SledServer::start(
&self.sled_config,
self.parent_log.clone(),
sled_address,
request.clone(),
)
.await
.map_err(|e| {
BootstrapError::SledError(format!(
"Could not start sled agent server: {e}"
))
})?;
maybe_agent.replace(server);
info!(&self.log, "Sled Agent loaded; recording configuration");
// Record this request so the sled agent can be automatically
// initialized on the next boot.
//
// danger handling: `serialized_request` contains our trust quorum
// share; we do not log it and only write it to the designated path.
let serialized_request = PersistentSledAgentRequest {
request: Cow::Borrowed(request),
trust_quorum_share: trust_quorum_share.clone().map(Into::into),
}
.danger_serialize_as_toml()
.expect("Cannot serialize request");
let path = get_sled_agent_request_path();
tokio::fs::write(&path, &serialized_request).await.map_err(|err| {
BootstrapError::Io {
message: format!("Recording Sled Agent request to {path:?}"),
err,
}
})?;
// Start trying to notify ddmd of our sled prefix so it can
// advertise it to other sleds.
//
// TODO-security This ddmd_client is used to advertise both this
// (underlay) address and our bootstrap address. Bootstrap addresses are
// unauthenticated (connections made on them are auth'd via sprockets),
// but underlay addresses should be exchanged via authenticated channels
// between ddmd instances. It's TBD how that will work, but presumably
// we'll need to do something different here for underlay vs bootstrap
// addrs (either talk to a differently-configured ddmd, or include info
// indicating which kind of address we're advertising).
self.ddmd_client.advertise_prefix(request.subnet);
Ok(SledAgentResponse { id: self.sled_config.id })
}
/// Communicates with peers, sharing secrets, until the rack has been
/// sufficiently unlocked.
async fn establish_sled_quorum(
&self,
share: ShareDistribution,
) -> Result<RackSecret, BootstrapError> {
let ddm_admin_client = DdmAdminClient::new(self.log.clone())?;
let rack_secret = retry_notify(
internal_service_policy(),
|| async {
let other_agents = {
// Manually build up a `HashSet` instead of `.collect()`ing
// so we can log if we see any duplicates.
let mut addrs = HashSet::new();
for addr in ddm_admin_client
.peer_addrs()
.await
.map_err(BootstrapError::DdmError)
.map_err(|err| BackoffError::transient(err))?
{
// We should never see duplicates; that would mean
// maghemite thinks two different sleds have the same
// bootstrap address!
if !addrs.insert(addr) {
let msg = format!("Duplicate peer addresses received from ddmd: {addr}");
error!(&self.log, "{}", msg);
return Err(BackoffError::permanent(
BootstrapError::PeerAddresses(msg),
));
}
}
addrs
};
info!(
&self.log,
"Bootstrap: Communicating with peers: {:?}", other_agents
);
// "-1" to account for ourselves.
if other_agents.len() < share.threshold - 1 {
warn!(
&self.log,
"Not enough peers to start establishing quorum"
);
return Err(BackoffError::transient(
BootstrapError::TrustQuorum(
TrustQuorumError::NotEnoughPeers,
),
));
}
info!(
&self.log,
"Bootstrap: Enough peers to start share transfer"
);
// Retrieve verified rack_secret shares from a quorum of agents
let other_agents: Vec<BootstrapAgentClient> = other_agents
.into_iter()
.map(|addr| {
let addr = SocketAddrV6::new(
addr,
BOOTSTRAP_AGENT_PORT,
0,
0,
);
BootstrapAgentClient::new(
addr,
&self.sp,
&share.member_device_id_certs,
self.log.new(o!(
"BootstrapAgentClient" => addr.to_string()),
),
)
})
.collect();
// TODO: Parallelize this and keep track of whose shares we've
// already retrieved and don't resend. See
// https://github.com/oxidecomputer/omicron/issues/514
let mut shares = vec![share.share.clone()];
for agent in &other_agents {
let share = agent.request_share().await
.map_err(|e| {
info!(&self.log, "Bootstrap: failed to retreive share from peer: {:?}", e);
BackoffError::transient(
BootstrapError::TrustQuorum(e.into()),
)
})?;
info!(
&self.log,
"Bootstrap: retreived share from peer: {}",
agent.addr()
);
shares.push(share);
}
let rack_secret = RackSecret::combine_shares(
share.threshold,
share.total_shares(),
&shares,
)
.map_err(|e| {
warn!(
&self.log,
"Bootstrap: failed to construct rack secret: {:?}", e
);
// TODO: We probably need to actually write an error
// handling routine that gives up in some cases based on
// the error returned from `RackSecret::combine_shares`.
// See https://github.com/oxidecomputer/omicron/issues/516
BackoffError::transient(
BootstrapError::TrustQuorum(
TrustQuorumError::RackSecretConstructionFailed(e),
),
)
})?;
info!(self.log, "RackSecret computed from shares.");
Ok(rack_secret)
},
|error, duration| {
warn!(
self.log,
"Failed to unlock sleds (will retry after {:?}: {:#}",
duration,
error,
)
},
)
.await?;
Ok(rack_secret)
}
/// Initializes the Rack Setup Service, if requested by `config`.
pub async fn start_rss(
&self,
config: &Config,
) -> Result<(), BootstrapError> {
if let Some(rss_config) = &config.rss_config {
info!(&self.log, "bootstrap service initializing RSS");
let rss = RssHandle::start_rss(
&self.parent_log,
rss_config.clone(),
self.address,
self.sp.clone(),
// TODO-cleanup: Remove this arg once RSS can discover the trust
// quorum members over the management network.
config
.sp_config
.as_ref()
.map(|sp_config| sp_config.trust_quorum_members.clone())
.unwrap_or_default(),
);
self.rss.lock().await.replace(rss);
}
Ok(())
}
}
// We intentionally DO NOT derive `Debug` or `Serialize`; both provide avenues
// by which we may accidentally log the contents of our trust quorum share.
#[derive(Deserialize, PartialEq)]
struct PersistentSledAgentRequest<'a> {
request: Cow<'a, SledAgentRequest>,
trust_quorum_share: Option<SerializableShareDistribution>,
}
impl PersistentSledAgentRequest<'_> {
/// On success, the returned string will contain our raw
/// `trust_quorum_share`. This method is named `danger_*` to remind the
/// caller that they must not log this string.
fn danger_serialize_as_toml(&self) -> Result<String, toml::ser::Error> {
#[derive(Serialize)]
#[serde(remote = "PersistentSledAgentRequest")]
struct PersistentSledAgentRequestDef<'a> {
request: Cow<'a, SledAgentRequest>,
trust_quorum_share: Option<SerializableShareDistribution>,
}
let mut out = String::with_capacity(128);
let mut serializer = toml::Serializer::new(&mut out);
PersistentSledAgentRequestDef::serialize(self, &mut serializer)?;
Ok(out)
}
}
#[cfg(test)]
mod tests {
use super::*;
use macaddr::MacAddr6;
use uuid::Uuid;
#[test]
fn test_mac_to_socket_addr() {
let mac = MacAddr("a8:40:25:10:00:01".parse::<MacAddr6>().unwrap());
assert_eq!(
mac_to_socket_addr(mac).ip(),
&"fdb0:a840:2510:1::1".parse::<Ipv6Addr>().unwrap(),
);
}
#[test]
fn persistent_sled_agent_request_serialization_round_trips() {
let secret = RackSecret::new();
let (mut shares, verifier) = secret.split(2, 4).unwrap();
let request = PersistentSledAgentRequest {
request: Cow::Owned(SledAgentRequest {
id: Uuid::new_v4(),
rack_id: Uuid::new_v4(),
gateway: crate::bootstrap::params::Gateway {
address: None,
mac: MacAddr6::nil(),
},
subnet: Ipv6Subnet::new(Ipv6Addr::LOCALHOST),
}),
trust_quorum_share: Some(
ShareDistribution {
threshold: 2,
verifier,
share: shares.pop().unwrap(),
member_device_id_certs: vec![],
}
.into(),
),
};
let serialized = request.danger_serialize_as_toml().unwrap();
let deserialized: PersistentSledAgentRequest =
toml::from_slice(serialized.as_bytes()).unwrap();
assert!(request == deserialized, "serialization round trip failed");
}
}