-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathmod.rs
More file actions
1481 lines (1343 loc) · 54.5 KB
/
Copy pathmod.rs
File metadata and controls
1481 lines (1343 loc) · 54.5 KB
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
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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/.
//! Nexus, the service that operates much of the control plane in an Oxide fleet
use self::external_endpoints::NexusCertResolver;
use self::saga::SagaExecutor;
use crate::DropshotServer;
use crate::app::background::BackgroundTasksData;
use crate::app::background::CurrentSitrep;
use crate::app::background::SagaRecoveryHelpers;
use crate::app::background::resolve_mgd_clients;
use crate::app::update::UpdateStatusHandle;
use crate::populate::PopulateArgs;
use crate::populate::PopulateStatus;
use crate::populate::populate_start;
use ::oximeter::types::ProducerRegistry;
use anyhow::anyhow;
use internal_dns_resolver::ResolveError;
use internal_dns_types::names::ServiceName;
use nexus_background_task_interface::BackgroundTasks;
use nexus_config::NexusConfig;
use nexus_config::RegionAllocationStrategy;
use nexus_config::Tunables;
use nexus_db_model::AllSchemaVersions;
use nexus_db_queries::authn;
use nexus_db_queries::authz;
use nexus_db_queries::context::OpContext;
use nexus_db_queries::db;
use nexus_db_queries::db::datastore::IdentityCheckPolicy;
use nexus_mgs_updates::ArtifactCache;
use nexus_mgs_updates::MgsUpdateDriver;
use nexus_types::deployment::PendingMgsUpdates;
use nexus_types::deployment::ReconfiguratorConfigParam;
use omicron_common::address::MGS_PORT;
use omicron_common::api::external::ByteCount;
use omicron_common::api::external::Error;
use omicron_uuid_kinds::OmicronZoneUuid;
use omicron_uuid_kinds::RackUuid;
use oximeter_producer::Server as ProducerServer;
use sagas::common_storage::PooledPantryClient;
use sagas::common_storage::make_pantry_connection_pool;
use sled_agent_types::early_networking::SwitchSlot;
use slog::Logger;
use slog_error_chain::InlineErrorChain;
use std::collections::HashMap;
use std::net::SocketAddrV6;
use std::net::{IpAddr, Ipv6Addr};
use std::num::NonZeroU32;
use std::sync::Arc;
use std::sync::OnceLock;
use tokio::sync::mpsc;
use tokio::sync::watch;
use update_common::artifacts::ArtifactsWithPlan;
use uuid::Uuid;
// The implementation of Nexus is large, and split into a number of submodules
// by resource.
mod address_lot;
mod affinity;
mod alert;
mod allow_list;
mod audit_log;
pub(crate) mod background;
mod bfd;
mod bgp;
mod certificate;
pub mod crucible;
mod deployment;
mod device_auth;
mod disk;
mod external_dns;
pub(crate) mod external_endpoints;
mod external_ip;
mod external_subnet;
mod iam;
mod image;
mod instance;
mod instance_network;
mod instance_platform;
mod internet_gateway;
mod ip_pool;
mod lldp;
mod login;
mod metrics;
pub(crate) mod multicast;
mod network_interface;
pub(crate) mod oximeter;
mod probe;
mod project;
mod quiesce;
mod quota;
mod rack;
pub(crate) mod saga;
mod scim;
mod session;
mod silo;
mod sled;
mod sled_instance;
mod snapshot;
mod ssh_key;
mod subnet_pool;
pub(crate) mod support_bundles;
mod switch;
mod switch_interface;
mod switch_port;
mod system_networking;
pub mod test_interfaces;
mod trust_quorum;
mod unnumbered;
mod update;
mod utilization;
mod volume;
mod vpc;
mod vpc_router;
mod vpc_subnet;
mod webhook;
// Sagas are not part of the "Nexus" implementation, but they are
// application logic.
pub(crate) mod sagas;
// TODO: When referring to API types, we should try to include
// the prefix unless it is unambiguous.
pub(crate) use self::deployment::SetTargetReleaseIntent;
use crate::app::quiesce::NexusQuiesceHandle;
pub(crate) use nexus_db_model::MAX_NICS_PER_INSTANCE;
pub(crate) use nexus_db_queries::db::queries::disk::MAX_DISKS_PER_INSTANCE;
use nexus_mgs_updates::DEFAULT_RETRY_TIMEOUT;
use nexus_types::internal_api::views::MgsUpdateDriverStatus;
use sagas::demo::CompletingDemoSagas;
// XXX: Might want to recast as max *floating* IPs, we have at most one
// ephemeral (so bounded in saga by design).
// The value here is arbitrary, but we need *a* limit for the instance
// create saga to have a bounded DAG. We might want to only enforce
// this during instance create (rather than live attach) in future.
pub(crate) const MAX_EXTERNAL_IPS_PER_INSTANCE: usize =
nexus_db_queries::db::queries::external_ip::MAX_EXTERNAL_IPS_PER_INSTANCE
as usize;
pub(crate) const MAX_EPHEMERAL_IPS_PER_INSTANCE: usize = 2;
pub(crate) const MAX_MULTICAST_GROUPS_PER_INSTANCE: usize = 32;
pub const MAX_VCPU_PER_INSTANCE: u16 = 254;
pub const MIN_MEMORY_BYTES_PER_INSTANCE: u32 = 1 << 30; // 1 GiB
// This is larger than total memory (let alone reservoir) on some sleds; it is
// not to guard against overallocation, but to keep instance memory sizes in
// ranges that we've tested. It is bounded only by the intersection of
// large-memory hardware configurations and tested instance sizes.
//
// Propolis has a similar limit in MAX_PHYSMEM. There, we would like to remove
// the limit entirely. Here, we may want to make the max size operator
// configurable as it may have implications on migratability for racks with
// mixed sled configurations.
//
// Before raising or removing this limit, testing has been valuable. See:
// * illumos bug #17403
// * Propolis issue #903
// * Propolis issue #907
pub const MAX_MEMORY_BYTES_PER_INSTANCE: u64 = 1536 * (1 << 30); // 1.5 TiB
pub const MIN_DISK_SIZE_BYTES: u32 = 1 << 30; // 1 GiB
pub const MAX_DISK_SIZE_BYTES: u64 = 1023 * (1 << 30); // 1023 GiB
/// This was number was chosen as the best-ish measured on a Cosmo when more or
/// less fully dedicating an SN861 as a disk for a single VM. This is certainly
/// higher than necessary for Gimlet, and is chosen far higher than may be
/// appropriate if the disk is shared across several or more instances.
pub const LOCAL_STORAGE_WORKERS: NonZeroU32 = NonZeroU32::new(30).unwrap();
/// This value is aribtrary
pub const MAX_SSH_KEYS_PER_INSTANCE: u32 = 100;
/// The amount of disk space to reserve for non-Crucible / control plane
/// storage. This amount represents a buffer that the region allocation query
/// will not use for each U2.
///
/// See oxidecomputer/omicron#7875 for the 250G determination.
pub const CONTROL_PLANE_STORAGE_BUFFER: ByteCount =
ByteCount::from_gibibytes_u32(250);
/// Manages an Oxide fleet -- the heart of the control plane
pub struct Nexus {
/// uuid for this nexus instance.
id: OmicronZoneUuid,
/// uuid for this rack
rack_id: RackUuid,
/// general server log
log: Logger,
/// persistent storage for resources in the control plane
db_datastore: Arc<db::DataStore>,
/// handle to global authz information
authz: Arc<authz::Authz>,
/// saga execution coordinator (SEC)
sagas: Arc<SagaExecutor>,
/// External dropshot servers
external_server: std::sync::Mutex<Option<DropshotServer>>,
/// External dropshot server that listens on the internal network to allow
/// connections from the tech port; see RFD 431.
techport_external_server: std::sync::Mutex<Option<DropshotServer>>,
/// Internal dropshot server
internal_server: std::sync::Mutex<Option<DropshotServer>>,
/// Lockstep dropshot server
lockstep_server: std::sync::Mutex<Option<DropshotServer>>,
/// Status of background task to populate database
populate_status: watch::Receiver<PopulateStatus>,
/// The metric producer server from which oximeter collects metric data.
producer_server: std::sync::Mutex<Option<ProducerServer>>,
/// Reusable `reqwest::Client`, to be cloned and used with the Progenitor-
/// generated `Client::new_with_client`.
///
/// (This does not need to be in an `Arc` because `reqwest::Client` uses
/// `Arc` internally.)
reqwest_client: reqwest::Client,
/// Client to the timeseries database.
timeseries_client: oximeter_db::Client,
/// `reqwest` client used for webhook delivery requests.
///
/// This lives on the Nexus struct as we would like to use the same client
/// pool for the webhook deliverator background task and the webhook probe
/// API.
webhook_delivery_client: reqwest::Client,
/// The tunable parameters from a configuration file
tunables: Tunables,
/// Whether multicast functionality is enabled - used by sagas and API endpoints to check if multicast operations should proceed
multicast_enabled: bool,
/// Operational context used for Instance allocation
opctx_alloc: OpContext,
/// Operational context used for external request authentication
opctx_external_authn: OpContext,
/// Max issue delay for samael crate - used only for testing
// the samael crate has an extra check (beyond the check against the SAML
// response NotOnOrAfter) that fails if the issue instant was too long ago.
// this amount of time is called "max issue delay" and we have to set that
// in order for our integration tests that POST static SAML responses to
// Nexus to not all fail.
samael_max_issue_delay: std::sync::Mutex<Option<chrono::Duration>>,
/// Conection pool for Crucible pantries
pantry_connection_pool: qorb::pool::Pool<PooledPantryClient>,
/// DNS resolver for internal services
internal_resolver: internal_dns_resolver::Resolver,
/// DNS resolver Nexus uses to resolve an external host
external_resolver: Arc<external_dns::Resolver>,
/// DNS servers used in `external_resolver`, used to provide DNS servers to
/// instances via DHCP
// TODO: This needs to be moved to the database.
// https://github.com/oxidecomputer/omicron/issues/3732
external_dns_servers: Vec<IpAddr>,
/// Background task driver
background_tasks_driver: OnceLock<background::Driver>,
/// Handles to various specific tasks
background_tasks: BackgroundTasks,
/// Internal state related to background tasks
background_tasks_internal: background::BackgroundTasksInternal,
/// Default Crucible region allocation strategy
default_region_allocation_strategy: RegionAllocationStrategy,
/// List of demo sagas awaiting a request to complete them
demo_sagas: Arc<std::sync::Mutex<sagas::demo::CompletingDemoSagas>>,
/// Sender for TUF repository artifacts temporarily stored in this zone to
/// be replicated out to sleds in the background
tuf_artifact_replication_tx: mpsc::Sender<ArtifactsWithPlan>,
/// reports status of pending MGS-managed updates
mgs_update_status_rx: watch::Receiver<MgsUpdateDriverStatus>,
/// DNS resolver used by MgsUpdateDriver for MGS
// We don't need to do anything with this, but we can't let it be dropped
// while Nexus is running.
#[allow(dead_code)]
mgs_resolver: Box<dyn qorb::resolver::Resolver>,
/// DNS resolver used by MgsUpdateDriver for Repo Depot
// We don't need to do anything with this, but we can't let it be dropped
// while Nexus is running.
#[allow(dead_code)]
repo_depot_resolver: Box<dyn qorb::resolver::Resolver>,
/// Watch channel containing the currently-loaded fault management sitrep.
#[allow(dead_code)]
sitrep_load_rx: watch::Receiver<Option<CurrentSitrep>>,
/// handle to pull update status data
update_status: UpdateStatusHandle,
/// state of overall Nexus quiesce activity
quiesce: NexusQuiesceHandle,
}
impl Nexus {
/// Create a new Nexus instance for the given rack id `rack_id`
///
/// If this function fails, the pool remains unterminated.
// TODO-polish revisit rack metadata
#[allow(clippy::too_many_arguments)]
pub(crate) async fn new_with_id(
rack_id: RackUuid,
log: Logger,
resolver: internal_dns_resolver::Resolver,
qorb_resolver: internal_dns_resolver::QorbResolver,
pool: Arc<db::Pool>,
producer_registry: &ProducerRegistry,
config: &NexusConfig,
authz: Arc<authz::Authz>,
) -> Result<Arc<Nexus>, String> {
let all_versions = config
.pkg
.schema
.as_ref()
.map(|s| AllSchemaVersions::load(&s.schema_dir))
.transpose()
.map_err(|error| format!("{error:#}"))?;
let nexus_id = config.deployment.id;
let db_datastore = Arc::new(
db::DataStore::new_with_timeout(
&log,
Arc::clone(&pool),
all_versions.as_ref(),
config.pkg.tunables.load_timeout,
IdentityCheckPolicy::CheckAndTakeover { nexus_id },
)
.await?,
);
db_datastore.register_producers(producer_registry);
let my_sec_id = db::SecId::from(config.deployment.id);
let sec_store = Arc::new(db::CockroachDbSecStore::new(
my_sec_id,
Arc::clone(&db_datastore),
log.new(o!("component" => "SecStore")),
)) as Arc<dyn steno::SecStore>;
let sec_client = Arc::new(steno::sec(
log.new(o!(
"component" => "SEC",
"sec_id" => my_sec_id.to_string()
)),
sec_store,
));
let (blueprint_load_tx, blueprint_load_rx) = watch::channel(None);
let quiesce_log = log.new(o!("component" => "NexusQuiesceHandle"));
let quiesce_opctx = OpContext::for_background(
quiesce_log,
Arc::clone(&authz),
authn::Context::internal_api(),
Arc::clone(&db_datastore) as Arc<dyn nexus_auth::storage::Storage>,
);
let quiesce = NexusQuiesceHandle::new(
db_datastore.clone(),
config.deployment.id,
blueprint_load_rx.clone(),
quiesce_opctx,
);
// It's a bit of a red flag to use an unbounded channel.
//
// This particular channel is used to send a Uuid from the saga executor
// to the saga recovery background task each time a saga is started.
//
// The usual argument for keeping a channel bounded is to ensure
// backpressure. But we don't really want that here. These items don't
// represent meaningful work for the saga recovery task, such that if it
// were somehow processing these slowly, we'd want to slow down the saga
// dispatch process. Under normal conditions, we'd expect this queue to
// grow as we dispatch new sagas until the saga recovery task runs, at
// which point the queue will quickly be drained. The only way this
// could really grow without bound is if the saga recovery task gets
// completely wedged and stops receiving these messages altogether. In
// this case, the maximum size this queue could grow over time is the
// number of sagas we can launch in that time. That's not ever likely
// to be a significant amount of memory.
//
// We could put our money where our mouth is: pick a sufficiently large
// bound and panic if we reach it. But "sufficiently large" depends on
// the saga creation rate and the period of the saga recovery background
// task. If someone changed the config, they'd have to remember to
// update this here. This doesn't seem worth it.
let (saga_create_tx, saga_recovery_rx) = mpsc::unbounded_channel();
let sagas = Arc::new(SagaExecutor::new(
Arc::clone(&sec_client),
log.new(o!("component" => "SagaExecutor")),
saga_create_tx,
quiesce.sagas(),
));
// Create a channel for replicating repository artifacts. 16 is a
// dubious bound for the channel but it seems unlikely that an operator
// would want to upload more than one at a time, and at most have two
// or three on the system during an upgrade (we've sized the artifact
// datasets to fit at most 10 repositories for this reason).
let (tuf_artifact_replication_tx, tuf_artifact_replication_rx) =
mpsc::channel(16);
let reqwest_client = reqwest::ClientBuilder::new()
.connect_timeout(std::time::Duration::from_secs(15))
.timeout(std::time::Duration::from_secs(15))
.build()
.map_err(|e| InlineErrorChain::new(&e).to_string())?;
// Client to the ClickHouse database.
let timeseries_client = match &config.pkg.timeseries_db.address {
None => {
let native_resolver =
qorb_resolver.for_service(ServiceName::OximeterReader);
oximeter_db::Client::new_with_resolver(
native_resolver,
"nexus-oximeter-reader",
&log,
)
}
Some(address) => oximeter_db::Client::new(*address, &log),
};
// TODO-cleanup We may want to make the populator a first-class
// background task.
let populate_ctx = OpContext::for_background(
log.new(o!("component" => "DataLoader")),
Arc::clone(&authz),
authn::Context::internal_db_init(),
Arc::clone(&db_datastore) as Arc<dyn nexus_auth::storage::Storage>,
);
let populate_args = PopulateArgs::new(rack_id);
let populate_status = populate_start(
populate_ctx,
Arc::clone(&db_datastore),
populate_args,
);
let background_ctx = OpContext::for_background(
log.new(o!("component" => "BackgroundTasks")),
Arc::clone(&authz),
authn::Context::internal_api(),
Arc::clone(&db_datastore) as Arc<dyn nexus_auth::storage::Storage>,
);
let (
background_tasks_initializer,
background_tasks,
background_tasks_internal,
) = background::BackgroundTasksInitializer::new();
let external_resolver = {
if config.deployment.external_dns_servers.is_empty() {
return Err("expected at least 1 external DNS server".into());
}
Arc::new(external_dns::Resolver::new(
&config.deployment.external_dns_servers,
))
};
let webhook_delivery_client = {
// The webhook delivery HTTP client will send requests to endpoints
// external to the rack, so apply the configuration for external
// HTTP clients.
let builder = external_http_client_builder(
&config.deployment.external_http_clients,
&external_resolver,
);
webhook::delivery_client(builder).map_err(|e| {
format!(
"failed to build webhook delivery client: {}",
InlineErrorChain::new(&e)
)
})?
};
let mut mgs_resolver =
qorb_resolver.for_service(ServiceName::ManagementGatewayService);
let mut repo_depot_resolver =
qorb_resolver.for_service(ServiceName::RepoDepot);
let (mgs_updates_tx, mgs_updates_rx) =
watch::channel(PendingMgsUpdates::new());
let artifact_cache = Arc::new(ArtifactCache::new(
log.new(o!("component" => "ArtifactCache")),
repo_depot_resolver.monitor(),
));
let mgs_update_driver = MgsUpdateDriver::new(
log.new(o!("component" => "MgsUpdateDriver")),
artifact_cache,
mgs_updates_rx,
mgs_resolver.monitor(),
DEFAULT_RETRY_TIMEOUT,
);
let mgs_update_status_rx = mgs_update_driver.status_rx();
let _mgs_driver_task = tokio::spawn(mgs_update_driver.run());
let (sitrep_load_tx, sitrep_load_rx) = watch::channel(None);
let nexus = Nexus {
id: config.deployment.id,
rack_id,
log: log.new(o!()),
db_datastore: Arc::clone(&db_datastore),
authz: Arc::clone(&authz),
sagas,
external_server: std::sync::Mutex::new(None),
techport_external_server: std::sync::Mutex::new(None),
internal_server: std::sync::Mutex::new(None),
lockstep_server: std::sync::Mutex::new(None),
producer_server: std::sync::Mutex::new(None),
populate_status,
reqwest_client,
timeseries_client,
webhook_delivery_client,
tunables: config.pkg.tunables.clone(),
// Whether multicast functionality is enabled.
// This is used by instance-related sagas and API endpoints to check
// if multicast operations should proceed.
//
// NOTE: This is separate from the RPW reconciler timing config, which
// only controls how often the background task runs.
multicast_enabled: config.pkg.multicast.enabled,
opctx_alloc: OpContext::for_background(
log.new(o!("component" => "InstanceAllocator")),
Arc::clone(&authz),
authn::Context::internal_read(),
Arc::clone(&db_datastore)
as Arc<dyn nexus_auth::storage::Storage>,
),
opctx_external_authn: OpContext::for_background(
log.new(o!("component" => "ExternalAuthn")),
Arc::clone(&authz),
authn::Context::external_authn(),
Arc::clone(&db_datastore)
as Arc<dyn nexus_auth::storage::Storage>,
),
samael_max_issue_delay: std::sync::Mutex::new(None),
pantry_connection_pool: make_pantry_connection_pool(&qorb_resolver),
internal_resolver: resolver.clone(),
external_resolver,
external_dns_servers: config
.deployment
.external_dns_servers
.clone(),
background_tasks_driver: OnceLock::new(),
background_tasks,
background_tasks_internal,
default_region_allocation_strategy: config
.pkg
.default_region_allocation_strategy
.clone(),
demo_sagas: Arc::new(std::sync::Mutex::new(
CompletingDemoSagas::new(),
)),
tuf_artifact_replication_tx,
mgs_update_status_rx,
mgs_resolver,
repo_depot_resolver,
update_status: UpdateStatusHandle::new(blueprint_load_rx),
quiesce,
sitrep_load_rx,
};
// TODO-cleanup all the extra Arcs here seems wrong
let nexus = Arc::new(nexus);
nexus.sagas.set_nexus(nexus.clone());
let saga_recovery_opctx = OpContext::for_background(
log.new(o!("component" => "SagaRecoverer")),
Arc::clone(&authz),
authn::Context::internal_saga_recovery(),
Arc::clone(&db_datastore) as Arc<dyn nexus_auth::storage::Storage>,
);
// Wait to start background tasks until after the populate step
// finishes. Among other things, the populate step installs role
// assignments for internal identities that are used by the background
// tasks. If we don't do this here, those tasks would fail spuriously
// on startup and not be retried for a while.
let task_nexus = nexus.clone();
let task_log = nexus.log.clone();
let task_registry = producer_registry.clone();
let task_config = config.clone();
tokio::spawn(async move {
match task_nexus.wait_for_populate().await {
Ok(_) => {
info!(task_log, "populate complete");
}
Err(_) => {
error!(task_log, "populate failed");
}
};
// Before starting our background tasks, inject an initial set of
// reconfigurator configuration if we're configured with one.
// This is only provided by the test suite, where we have an initial
// config to disable automatic blueprint planning.
if let Some(config) = task_config.pkg.initial_reconfigurator_config
{
let config = ReconfiguratorConfigParam { version: 1, config };
if let Err(err) = db_datastore
.reconfigurator_config_insert_latest_version(
&background_ctx,
config,
)
.await
{
error!(
task_log,
"failed to insert initial reconfigurator config";
InlineErrorChain::new(&err),
);
}
}
// That said, even if the populate step fails, we may as well try to
// start the background tasks so that whatever can work will work.
info!(task_log, "activating background tasks");
let console_session_absolute_timeout =
chrono::TimeDelta::try_minutes(
task_config
.pkg
.console
.session_absolute_timeout_minutes
.into(),
)
.expect("session_absolute_timeout_minutes out of range");
let driver = background_tasks_initializer.start(
&task_nexus.background_tasks,
BackgroundTasksData {
opctx: background_ctx,
datastore: db_datastore,
config: task_config.pkg.background_tasks,
multicast_enabled: task_config.pkg.multicast.enabled,
rack_id,
nexus_id: task_config.deployment.id,
resolver,
saga_starter: task_nexus.sagas.clone(),
producer_registry: task_registry,
webhook_delivery_client: task_nexus
.webhook_delivery_client
.clone(),
nexus_quiesce: task_nexus.quiesce.clone(),
saga_recovery: SagaRecoveryHelpers {
recovery_opctx: saga_recovery_opctx,
maker: task_nexus.clone(),
sec_client: sec_client.clone(),
registry: sagas::ACTION_REGISTRY.clone(),
sagas_started_rx: saga_recovery_rx,
quiesce: task_nexus.quiesce.sagas(),
},
tuf_artifact_replication_rx,
mgs_updates_tx,
blueprint_load_tx,
sitrep_load_tx,
console_session_absolute_timeout,
},
);
if let Err(_) = task_nexus.background_tasks_driver.set(driver) {
panic!("multiple initialization of background_tasks_driver");
}
});
Ok(nexus)
}
/// Return the ID for this Nexus instance.
pub fn id(&self) -> OmicronZoneUuid {
self.id
}
/// Return the rack ID for this Nexus instance.
pub fn rack_id(&self) -> RackUuid {
self.rack_id
}
/// Return the tunable configuration parameters, e.g. for use in tests.
pub fn tunables(&self) -> &Tunables {
&self.tunables
}
pub fn authz(&self) -> &Arc<authz::Authz> {
&self.authz
}
pub fn multicast_enabled(&self) -> bool {
self.multicast_enabled
}
pub(crate) async fn wait_for_populate(&self) -> Result<(), anyhow::Error> {
let mut my_rx = self.populate_status.clone();
loop {
my_rx
.changed()
.await
.map_err(|error| anyhow!(error.to_string()))?;
match &*my_rx.borrow() {
PopulateStatus::NotDone => (),
PopulateStatus::Done => return Ok(()),
PopulateStatus::Failed(error) => {
return Err(anyhow!(error.clone()));
}
};
}
}
// Waits for Nexus to determine whether sagas are supposed to be quiesced
//
// This is used by the test suite because most tests assume that sagas are
// operational as soon as they start.
pub(crate) async fn wait_for_saga_determination(&self) {
self.quiesce.sagas().wait_for_determination().await;
}
pub(crate) async fn external_tls_config(
&self,
tls_enabled: bool,
) -> Option<rustls::ServerConfig> {
// Wait for the background task to complete at least once. We don't
// care about its value. To do this, we need our own copy of the
// channel.
let mut rx = self.background_tasks_internal.external_endpoints.clone();
let _ = rx.wait_for(|s| s.is_some()).await;
if !tls_enabled {
return None;
}
let mut rustls_cfg = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_cert_resolver(Arc::new(NexusCertResolver::new(
self.log.new(o!("component" => "NexusCertResolver")),
self.background_tasks_internal.external_endpoints.clone(),
)));
rustls_cfg.alpn_protocols = vec![b"h2".to_vec(), b"http/1.1".to_vec()];
Some(rustls_cfg)
}
// Called to trigger inventory collection.
pub(crate) fn activate_inventory_collection(&self) {
self.background_tasks
.activate(&self.background_tasks.task_inventory_collection);
}
// Called to trigger propagation of service firewall rules.
pub(crate) fn activate_service_firewall_propagation(&self) {
self.background_tasks
.activate(&self.background_tasks.task_service_firewall_propagation);
}
// Called to hand off management of external servers to Nexus.
pub(crate) async fn set_servers(
&self,
external_server: DropshotServer,
techport_external_server: DropshotServer,
internal_server: DropshotServer,
lockstep_server: DropshotServer,
producer_server: ProducerServer,
) {
// If any servers already exist, close them.
let _ = self.close_servers().await;
// Insert the new servers.
self.external_server.lock().unwrap().replace(external_server);
self.techport_external_server
.lock()
.unwrap()
.replace(techport_external_server);
self.internal_server.lock().unwrap().replace(internal_server);
self.lockstep_server.lock().unwrap().replace(lockstep_server);
self.producer_server.lock().unwrap().replace(producer_server);
}
/// Fully terminates Nexus.
///
/// Closes all running servers and the connection to the datastore.
pub(crate) async fn terminate(&self) -> Result<(), String> {
let mut res = Ok(());
res = res.and(self.close_servers().await);
self.datastore().terminate().await;
res
}
/// Terminates all servers.
///
/// This function also waits for the servers to shut down.
pub(crate) async fn close_servers(&self) -> Result<(), String> {
// NOTE: All these take the lock and swap out of the option immediately,
// because they are synchronous mutexes, which cannot be held across the
// await point these `close()` methods expose.
let external_server = self.external_server.lock().unwrap().take();
let mut res = Ok(());
let extend_err =
|mut res: &mut Result<(), String>, mut new: Result<(), String>| {
match (&mut res, &mut new) {
(Err(s), Err(new_err)) => {
s.push_str(&format!(", {new_err}"))
}
(Ok(()), Err(_)) => *res = new,
(_, Ok(())) => (),
}
};
if let Some(server) = external_server {
extend_err(&mut res, server.close().await);
}
let techport_external_server =
self.techport_external_server.lock().unwrap().take();
if let Some(server) = techport_external_server {
extend_err(&mut res, server.close().await);
}
let internal_server = self.internal_server.lock().unwrap().take();
if let Some(server) = internal_server {
extend_err(&mut res, server.close().await);
}
let lockstep_server = self.lockstep_server.lock().unwrap().take();
if let Some(server) = lockstep_server {
extend_err(&mut res, server.close().await);
}
let producer_server = self.producer_server.lock().unwrap().take();
if let Some(server) = producer_server {
extend_err(
&mut res,
server.close().await.map_err(|e| e.to_string()),
);
}
res
}
/// Awaits termination without triggering it.
///
/// To trigger termination, see:
/// - [`Self::close_servers`] or [`Self::terminate`]
pub(crate) async fn wait_for_shutdown(&self) -> Result<(), String> {
// The internal server is the last server to be closed.
//
// We don't wait for the external servers to be closed; we just expect
// that they'll be closed before the internal server.
let server_fut = self
.internal_server
.lock()
.unwrap()
.as_ref()
.map(|s| s.wait_for_shutdown());
if let Some(server_fut) = server_fut {
server_fut.await?;
}
Ok(())
}
pub(crate) fn get_external_server_address(
&self,
) -> Option<std::net::SocketAddr> {
self.external_server
.lock()
.unwrap()
.as_ref()
.map(|server| server.local_addr())
}
pub(crate) fn get_techport_server_address(
&self,
) -> Option<std::net::SocketAddr> {
self.techport_external_server
.lock()
.unwrap()
.as_ref()
.map(|server| server.local_addr())
}
pub(crate) fn get_internal_server_address(
&self,
) -> Option<std::net::SocketAddr> {
self.internal_server
.lock()
.unwrap()
.as_ref()
.map(|server| server.local_addr())
}
pub(crate) fn get_lockstep_server_address(
&self,
) -> Option<std::net::SocketAddr> {
self.lockstep_server
.lock()
.unwrap()
.as_ref()
.map(|server| server.local_addr())
}
/// Returns an [`OpContext`] used for authenticating external requests
pub fn opctx_external_authn(&self) -> &OpContext {
&self.opctx_external_authn
}
/// Returns an [`OpContext`] used for balancing services.
pub(crate) fn opctx_for_service_balancer(&self) -> OpContext {
OpContext::for_background(
self.log.new(o!("component" => "ServiceBalancer")),
Arc::clone(&self.authz),
authn::Context::internal_service_balancer(),
Arc::clone(&self.db_datastore)
as Arc<dyn nexus_auth::storage::Storage>,
)
}
/// Returns an [`OpContext`] used for internal API calls.
pub(crate) fn opctx_for_internal_api(&self) -> OpContext {
OpContext::for_background(
self.log.new(o!("component" => "InternalApi")),
Arc::clone(&self.authz),
authn::Context::internal_api(),
Arc::clone(&self.db_datastore)
as Arc<dyn nexus_auth::storage::Storage>,
)
}
/// Used as the body of a "stub" endpoint -- one that's currently
/// unimplemented but that we eventually intend to implement
///
/// Even though an endpoint is unimplemented, it's useful if it implements
/// the correct authn/authz behaviors behaviors for unauthenticated and
/// authenticated, unauthorized requests. This allows us to maintain basic
/// authn/authz test coverage for stub endpoints, which in turn helps us
/// ensure that all endpoints are covered.
///
/// In order to implement the correct authn/authz behavior, we need to know
/// a little about the endpoint. This is given by the `visibility`
/// argument. See the examples below.
///
/// # Examples
///
/// ## A top-level API endpoint (always visible)
///
/// For example, "/my-new-kind-of-resource". The assumption is that the
/// _existence_ of this endpoint is not a secret. Use:
///
/// ```
/// use nexus_db_queries::context::OpContext;
/// use nexus_db_queries::db::DataStore;
/// use omicron_nexus::app::Nexus;
/// use omicron_nexus::app::Unimpl;
/// use omicron_common::api::external::Error;
///
/// async fn my_things_list(
/// nexus: &Nexus,
/// datastore: &DataStore,
/// opctx: &OpContext,
/// ) -> Result<(), Error>
/// {
/// Err(nexus.unimplemented_todo(opctx, Unimpl::Public).await)
/// }
/// ```
///
/// ## An authz-protected resource under the top level
///
/// For example, "/my-new-kind-of-resource/demo" (where "demo" is the name
/// of a specific resource of type "my-new-kind-of-resource"). Use:
///
/// ```
/// use nexus_db_queries::context::OpContext;
/// use nexus_db_queries::db::model::Name;
/// use nexus_db_queries::db::DataStore;
/// use omicron_nexus::app::Nexus;
/// use omicron_nexus::app::Unimpl;
/// use omicron_common::api::external::Error;
/// use omicron_common::api::external::LookupType;
/// use omicron_common::api::external::ResourceType;
///
/// async fn my_thing_fetch(
/// nexus: &Nexus,
/// datastore: &DataStore,
/// opctx: &OpContext,