-
Notifications
You must be signed in to change notification settings - Fork 0
/
certifier-rotate.rs
659 lines (638 loc) · 24.5 KB
/
certifier-rotate.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
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
use {
aargvark::{
Aargvark,
AargvarkJson,
},
certifier::{
decide_sig,
rotation_buffer,
rotation_period,
sign_duration,
BuilderPubKey,
BuilderSigner,
RotateConfig,
CA_FQDN,
ENV_ROTATE_CONFIG,
VERSION_STATE_DESTROYED,
VERSION_STATE_DESTROY_SCHEDULED,
VERSION_STATE_DISABLED,
VERSION_STATE_ENABLED,
},
chrono::{
DateTime,
Duration,
Utc,
},
der::{
DecodePem,
EncodePem,
},
google_cloudkms1::{
api::{
AsymmetricSignRequest,
CryptoKeyVersion,
DestroyCryptoKeyVersionRequest,
},
CloudKMS,
FieldMask,
},
google_storage1::{
api::Object,
Storage,
},
loga::{
ea,
DebugDisplay,
ErrContext,
Log,
ResultContext,
},
mime::Mime,
spaghettinuum::utils::tls_util::{
rand_serial,
to_x509_time,
},
std::{
cell::RefCell,
collections::{
HashMap,
HashSet,
},
io::Cursor,
rc::Rc,
str::FromStr,
},
tokio::time::sleep,
x509_cert::{
builder::{
Builder,
CertificateBuilder,
Profile,
},
der::asn1::{
BitString,
Ia5String,
},
ext::pkix::{
constraints::name::GeneralSubtree,
name::GeneralName,
NameConstraints,
},
spki::SubjectPublicKeyInfoOwned,
},
yup_oauth2::{
authenticator::ApplicationDefaultCredentialsTypes,
ApplicationDefaultCredentialsAuthenticator,
ApplicationDefaultCredentialsFlowOpts,
},
};
#[derive(Aargvark)]
struct Args {
pub config: Option<AargvarkJson<RotateConfig>>,
}
const TAG_ISSUE_START: &'static str = "start";
const TAG_ISSUE_END: &'static str = "end";
const BUCKET_PREFIX_VERSION: &'static str = "generations";
fn get_ver_short_id(full_id: &str) -> Result<String, loga::Error> {
return Ok(
full_id
.rsplit_once("/")
.map(|(_, r)| r.to_string())
.context_with("Error parsing id from version full id", ea!(id = full_id))?,
);
}
struct ViewObject_ {
object_key: String,
version_short_id: String,
create_time: DateTime<Utc>,
issue_start: Option<DateTime<Utc>>,
issue_end: Option<DateTime<Utc>>,
}
#[derive(Clone)]
struct ViewObject(Rc<RefCell<ViewObject_>>);
struct ViewVersion_ {
full_id: String,
short_id: String,
}
#[derive(Clone)]
struct ViewVersion(Rc<RefCell<ViewVersion_>>);
struct View {
objects: Vec<ViewObject>,
versions: HashMap<String, ViewVersion>,
}
async fn generate_version(
log: &Log,
kms_client: &CloudKMS<yup_oauth2::hyper_rustls::HttpsConnector<yup_oauth2::hyper::client::HttpConnector>>,
storage_client: &Storage<yup_oauth2::hyper_rustls::HttpsConnector<yup_oauth2::hyper::client::HttpConnector>>,
key_gcpid: &str,
bucket_gcpid: &str,
view: &mut View,
) -> Result<ViewObject, loga::Error> {
let now = Utc::now();
// Generate new keypair version + get public key
let (_, version) = kms_client.projects().locations_key_rings_crypto_keys_crypto_key_versions_create(CryptoKeyVersion {
state: Some(VERSION_STATE_DISABLED.to_string()),
..Default::default()
}, key_gcpid).doit().await.stack_context(log, "Error rotating key")?;
let ver_full_id = version.name.unwrap();
let ver_short_id = get_ver_short_id(&ver_full_id)?;
let (_, ca_pubkey) =
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_get_public_key(&ver_full_id)
.doit()
.await
.stack_context(log, "Failed to get new public key")?;
// Build CA cert
let ca_keyinfo = SubjectPublicKeyInfoOwned::from_pem(&ca_pubkey.pem.unwrap()).unwrap();
let (sig_digest_fn, sig_algorithm) = decide_sig(&ca_keyinfo)?;
let ca_signer = BuilderSigner {
key: BuilderPubKey(ca_keyinfo.clone()),
alg: sig_algorithm,
};
let mut cert_builder = CertificateBuilder::new(
Profile::Root,
// Timestamp, 1h granularity (don't publish two issued within an hour/don't issue
// two within an hour)
rand_serial(),
x509_cert::time::Validity {
not_before: to_x509_time(now - Duration::hours(24)),
not_after: to_x509_time(now + rotation_period() * 2 + rotation_buffer() + sign_duration()),
},
x509_cert::name::RdnSequence::from_str(&format!("CN={}", CA_FQDN)).unwrap(),
ca_keyinfo.clone(),
&ca_signer,
).unwrap();
cert_builder.add_extension(&NameConstraints {
permitted_subtrees: Some(vec![GeneralSubtree {
base: GeneralName::DnsName(Ia5String::new(".s").unwrap()),
minimum: 0,
maximum: None,
}]),
excluded_subtrees: None,
}).unwrap();
let cert_csr_der = cert_builder.finalize().unwrap();
let signature =
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_asymmetric_sign(AsymmetricSignRequest {
digest: Some(sig_digest_fn(&cert_csr_der)),
..Default::default()
}, &ver_full_id)
.doit()
.await
.stack_context(log, "Error signing new CA cert CSR")?
.1
.signature
.stack_context(log, "Signing request response missing signature data")?;
let cert_pem =
cert_builder
.assemble(BitString::from_bytes(&signature).stack_context(log, "Error building signature bitstring")?)
.stack_context(log, "Error assembling cert")?
.to_pem(der::pem::LineEnding::LF)
.stack_context(log, "Error building PEM for cert")?;
// Log and store
let object_key = format!("{}/{}", BUCKET_PREFIX_VERSION, ver_short_id);
storage_client
.objects()
.insert(Object {
name: Some(object_key.clone()),
..Default::default()
}, &bucket_gcpid)
.upload_resumable(Cursor::new(cert_pem.as_bytes()), Mime::from_str("application/x-pem-file").unwrap())
.await
.stack_context_with(log, "Error uploading new cert", ea!(bucket = bucket_gcpid, object = object_key))?;
eprintln!("\nNew root cert is:\n{}", cert_pem);
// Update view + return
let version = ViewVersion(Rc::new(RefCell::new(ViewVersion_ {
full_id: ver_full_id,
short_id: ver_short_id.clone(),
})));
let object = ViewObject(Rc::new(RefCell::new(ViewObject_ {
object_key: object_key,
version_short_id: ver_short_id.clone(),
create_time: now,
issue_start: None,
issue_end: None,
})));
view.versions.insert(ver_short_id, version);
view.objects.push(object.clone());
return Ok(object);
}
#[tokio::main]
async fn main() {
async fn inner() -> Result<(), loga::Error> {
let args = aargvark::vark::<Args>();
let log = Log::new_root(loga::INFO);
let log = &log;
let config = if let Some(p) = args.config {
p.value
} else if let Some(c) = match std::env::var(ENV_ROTATE_CONFIG) {
Ok(c) => Some(c),
Err(e) => match e {
std::env::VarError::NotPresent => None,
std::env::VarError::NotUnicode(_) => {
return Err(loga::err_with("Error parsing env var as unicode", ea!(env = ENV_ROTATE_CONFIG)))
},
},
} {
let log = log.fork(ea!(source = "env"));
serde_json::from_str::<RotateConfig>(&c).stack_context(&log, "Parsing config")?
} else {
return Err(
log.err_with(
"No config passed on command line, and no config set in env var",
ea!(env = ENV_ROTATE_CONFIG),
),
);
};
let hc =
yup_oauth2
::hyper
::Client
::builder().build(
yup_oauth2::hyper_rustls::HttpsConnectorBuilder::new()
.with_webpki_roots()
.https_or_http()
.enable_http1()
.build(),
);
let auth =
match ApplicationDefaultCredentialsAuthenticator::with_client(
ApplicationDefaultCredentialsFlowOpts::default(),
hc.clone(),
).await {
ApplicationDefaultCredentialsTypes::InstanceMetadata(auth) => auth
.build()
.await
.expect("Unable to create instance metadata authenticator"),
ApplicationDefaultCredentialsTypes::ServiceAccount(auth) => auth
.build()
.await
.expect("Unable to create service account authenticator"),
};
let kms_client = CloudKMS::new(hc.clone(), auth.clone());
let storage_client = Storage::new(hc, auth);
// # 1. Gather all obj + version info
let mut view = View {
objects: vec![],
versions: HashMap::new(),
};
{
let mut after = <Option<String>>::None;
loop {
let mut req = storage_client.objects().list(&config.bucket);
if let Some(after) = after {
req = req.page_token(&after);
}
let (_, page) = req.doit().await.stack_context(log, "Failed to retrieve cert bucket page")?;
let Some(new_objects) = page.items else {
break;
};
for v in new_objects {
let Some(object_key) = v.name else {
log.log(loga::WARN, "Received obj missing name/id! Skipping...");
continue;
};
let Some(version_short_id) =
object_key
.strip_suffix("/")
.unwrap_or(&object_key)
.strip_prefix(&format!("{}/", BUCKET_PREFIX_VERSION)) else {
continue;
};
let metadata = v.metadata.unwrap_or_default();
let create_time =
v.time_created.stack_context(log, "Missing created time for object from gcp api")?;
let issue_start = match metadata.get(TAG_ISSUE_START) {
Some(tag_value) => {
match DateTime::parse_from_rfc3339(tag_value.as_str()) {
Ok(stamp) => Some(<DateTime<Utc>>::from(stamp)),
Err(e) => {
log.log_err(
loga::WARN,
e.context_with("Error parsing tag", ea!(tag = TAG_ISSUE_START)),
);
continue;
},
}
},
None => {
None
},
};
let issue_end = match metadata.get(TAG_ISSUE_END) {
Some(tag_value) => {
match DateTime::parse_from_rfc3339(tag_value.as_str()) {
Ok(stamp) => Some(<DateTime<Utc>>::from(stamp)),
Err(e) => {
log.log_err(
loga::WARN,
e.context_with("Error parsing tag", ea!(tag = TAG_ISSUE_END)),
);
continue;
},
}
},
None => {
None
},
};
log.log_with(
loga::INFO,
"Surveying, found cert",
ea!(id = object_key, issue_start = issue_start.dbg_str(), issue_end = issue_end.dbg_str()),
);
view.objects.push(ViewObject(Rc::new(RefCell::new(ViewObject_ {
object_key: object_key.clone(),
version_short_id: version_short_id.to_string(),
create_time: create_time,
issue_start: issue_start,
issue_end: issue_end,
}))));
}
after = page.next_page_token.map(|x| x.to_string());
if after.is_none() {
break;
}
}
}
view.objects.sort_by_cached_key(|e| e.0.borrow().create_time);
{
let mut after = <Option<String>>::None;
loop {
let mut req =
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_list(&config.key_gcpid);
if let Some(after) = after {
req = req.page_token(&after);
}
let (_, page) =
req.doit().await.stack_context(log, "Failed to retrieve key versions to update to semi-latest")?;
let Some(new_versions) = page.crypto_key_versions else {
break;
};
for v in new_versions {
let Some(state) = v.state else {
log.log(loga::WARN, "Received version missing state! Skipping...");
continue;
};
if state == VERSION_STATE_DESTROY_SCHEDULED || state == VERSION_STATE_DESTROYED {
continue;
}
let Some(full_id) = v.name else {
log.log(loga::WARN, "Received version missing name/id! Skipping...");
continue;
};
let short_id = get_ver_short_id(&full_id)?;
log.log_with(loga::INFO, "Surveying, found key version", ea!(id = short_id, state = state));
view.versions.insert(short_id.clone(), ViewVersion(Rc::new(RefCell::new(ViewVersion_ {
full_id: full_id,
short_id: short_id,
}))));
}
after = page.next_page_token.map(|x| x.to_string());
if after.is_none() {
break;
}
}
}
// # Decide/identiy new active, pending, and old active objects
let mut old_current = None;
let mut current = None;
let mut next = None;
{
let mut offset = 0usize;
for (i, o) in view.objects.iter().enumerate() {
let o1 = o.0.borrow();
if o1.issue_start.is_some() && o1.issue_end.is_none() &&
view.versions.contains_key(&o1.version_short_id) {
old_current = Some(o.clone());
log.log_with(loga::INFO, "Selected old current key version", ea!(id = o1.version_short_id));
offset = i + 1;
break;
}
}
for o in view.objects.iter().skip(offset) {
offset += 1;
let o1 = o.0.borrow();
if o1.issue_start.is_none() && o1.issue_end.is_none() &&
view.versions.contains_key(&o1.version_short_id) {
log.log_with(loga::INFO, "Selected current key version", ea!(id = o1.version_short_id));
current = Some(o.clone());
break;
}
}
for o in view.objects.iter().skip(offset) {
let o1 = o.0.borrow();
if !o1.issue_start.is_some() && !o1.issue_end.is_some() &&
view.versions.contains_key(&o1.version_short_id) {
log.log_with(loga::INFO, "Selected next key version", ea!(id = o1.version_short_id));
next = Some(o.clone());
break;
}
}
}
// # Ensure a current version, enable the key and mark it as having started use
let current = match current {
None => {
log.log(loga::INFO, "No available current key version, creating");
generate_version(
log,
&kms_client,
&storage_client,
&config.key_gcpid,
&config.bucket,
&mut view,
).await?
},
Some(current) => {
if next.is_none() {
log.log(loga::INFO, "No available next key version, creating");
generate_version(
log,
&kms_client,
&storage_client,
&config.key_gcpid,
&config.bucket,
&mut view,
).await?;
}
current
},
};
log.log_with(
loga::INFO,
"Activating selected current key version",
ea!(id = current.0.borrow().version_short_id),
);
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_patch(CryptoKeyVersion {
state: Some(VERSION_STATE_ENABLED.to_string()),
..Default::default()
}, &view.versions[¤t.0.borrow().version_short_id].0.borrow().full_id)
.update_mask(FieldMask::new(&["state"]))
.doit()
.await
.stack_context(log, "Error disabling old current key version")?;
storage_client
.objects()
.update(Object {
metadata: Some({
let mut new_tags = HashMap::new();
new_tags.insert(TAG_ISSUE_START.to_string(), Utc::now().to_rfc3339());
new_tags
}),
..Default::default()
}, &config.bucket, &urlencoding::encode(¤t.0.borrow().object_key))
.doit()
.await
.stack_context_with(
log,
"Failed to set new current version issue start tag",
ea!(object = current.0.borrow().object_key),
)?;
// # Disable the old current
//
// Wait for transactions with the old current key to finish...
if let Some(old_current) = old_current {
log.log_with(
loga::INFO,
"Waiting for a fixed period to allow outstanding requests to finish before deactivating old current version",
ea!(id = old_current.0.borrow().version_short_id),
);
sleep(Duration::minutes(1).to_std().unwrap()).await;
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_patch(CryptoKeyVersion {
state: Some(VERSION_STATE_DISABLED.to_string()),
..Default::default()
}, &view.versions[&old_current.0.borrow().version_short_id].0.borrow().full_id)
.update_mask(FieldMask::new(&["state"]))
.doit()
.await
.stack_context(log, "Error disabling old current key version")?;
let end_time = Utc::now();
storage_client
.objects()
.update(Object {
metadata: Some({
let mut new_tags = HashMap::new();
new_tags.insert(TAG_ISSUE_END.to_string(), end_time.to_rfc3339());
new_tags
}),
..Default::default()
}, &config.bucket, &urlencoding::encode(&old_current.0.borrow().object_key))
.doit()
.await
.stack_context_with(
log,
"Failed to set old current version issue end tag",
ea!(object = old_current.0.borrow().object_key),
)?;
old_current.0.borrow_mut().issue_end = Some(end_time);
}
// # Clean up objects and versions
let epoch = Utc::now() - sign_duration();
let mut keep_objects = vec![];
for o in view.objects.drain(..) {
let o1 = o.0.borrow();
let Some(issue_end) = o1.issue_end else {
drop(o1);
keep_objects.push(o);
continue;
};
if issue_end >= epoch {
drop(o1);
keep_objects.push(o);
continue;
}
log.log_with(loga::INFO, "Deleting obsolete cert", ea!(id = o.0.borrow().version_short_id));
storage_client
.objects()
.delete(&config.bucket, &urlencoding::encode(&o1.object_key))
.doit()
.await
.stack_context_with(log, "Error deleting expired cert object", ea!(object = o1.object_key))?;
}
view.objects = keep_objects;
let mut active_versions = HashSet::new();
for o in &view.objects {
let o1 = o.0.borrow();
active_versions.insert(o1.version_short_id.clone());
}
for v in view.versions.values() {
let v0 = v.0.borrow();
if active_versions.contains(&v0.short_id) {
continue;
}
log.log_with(loga::INFO, "Deleting unrooted key version", ea!(id = v.0.borrow().short_id));
kms_client
.projects()
.locations_key_rings_crypto_keys_crypto_key_versions_destroy(
DestroyCryptoKeyVersionRequest::default(),
&v0.full_id,
)
.doit()
.await
.stack_context_with(log, "Failed to destroy old key", ea!(id = v0.short_id))?;
}
// Build CA bundle artifact
let mut certs = vec![];
for o in view.objects {
let o0 = o.0.borrow();
let log = &log.fork(ea!(id = o0.object_key));
log.log(loga::INFO, "Adding cert to artifact bundle");
let resp =
storage_client
.objects()
.get(&config.bucket, &urlencoding::encode(&o0.object_key))
.param("alt", "media")
.doit()
.await
.stack_context_with(log, "Error requesting cert from bucket", ea!(object = o0.object_key))?
.0;
if !resp.status().is_success() {
return Err(log.err("Received error response to request for cert"))
}
let mut body =
String::from_utf8(
yup_oauth2::hyper::body::to_bytes(resp.into_body())
.await
.stack_context(log, "Error downloading cert body")?
.to_vec(),
).stack_context(log, "Downloaded cert PEM has invalid utf-8")?;
if body.is_empty() {
return Err(log.err("Got empty cert body from server"));
}
if *body.as_bytes().last().unwrap() != '\n' as u8 {
body.push('\n');
}
if o.0.as_ptr() == current.0.as_ptr() {
eprintln!("\nActive signing cert is:\n{}", body);
}
certs.extend(body.as_bytes());
}
let artifact_name = "spaghettinuum_s.crt";
let url = format!("https://storage.googleapis.com/{}/{}", config.bucket, artifact_name);
storage_client
.objects()
.insert(Object {
name: Some(artifact_name.to_string()),
..Default::default()
}, &config.bucket)
.upload_resumable(Cursor::new(&certs), Mime::from_str("application/pem-certificate-chain").unwrap())
.await
.stack_context_with(log, "Error uploading bundle", ea!(object = url))?;
eprintln!("Bundle was uploaded to {}", url);
return Ok(());
}
match inner().await {
Ok(_) => { },
Err(e) => {
loga::fatal(e);
},
}
}