winterbaume-route53resolver 0.2.0

Route 53 Resolver service implementation for winterbaume
Documentation
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
//! Serde-compatible view types for Route53Resolver state snapshots.

use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use winterbaume_core::{StateChangeNotifier, StateViewError, StatefulService};

use crate::handlers::Route53ResolverService;
use crate::state::Route53ResolverState;
use crate::types::{
    IpAddressEntry, ResolverDnssecConfig, ResolverEndpoint, ResolverQueryLogConfig,
    ResolverQueryLogConfigAssociation, ResolverRule, ResolverRuleAssociation, TargetAddress,
};

/// Serializable view of the entire Route53Resolver state.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Route53ResolverStateView {
    /// Resolver endpoints keyed by endpoint ID.
    #[serde(default)]
    pub endpoints: HashMap<String, ResolverEndpointView>,
    /// Resolver rules keyed by rule ID.
    #[serde(default)]
    pub resolver_rules: HashMap<String, ResolverRuleView>,
    /// Rule associations keyed by association ID.
    #[serde(default)]
    pub rule_associations: HashMap<String, ResolverRuleAssociationView>,
    /// Query log configs keyed by config ID.
    #[serde(default)]
    pub query_log_configs: HashMap<String, ResolverQueryLogConfigView>,
    /// Query log config associations keyed by association ID.
    #[serde(default)]
    pub query_log_config_associations: HashMap<String, ResolverQueryLogConfigAssociationView>,
    /// DNSSEC configs keyed by resource ID.
    #[serde(default)]
    pub dnssec_configs: HashMap<String, ResolverDnssecConfigView>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverEndpointView {
    pub id: String,
    pub arn: String,
    pub name: String,
    pub security_group_ids: Vec<String>,
    pub direction: String,
    pub ip_address_count: i32,
    pub host_vpc_id: String,
    pub status: String,
    pub status_message: String,
    pub creation_time: String,
    pub modification_time: String,
    pub creator_request_id: String,
    pub protocols: Vec<String>,
    pub resolver_endpoint_type: String,
    pub ip_addresses: Vec<IpAddressEntryView>,
    #[serde(default)]
    pub tags: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IpAddressEntryView {
    pub ip_id: String,
    pub subnet_id: String,
    pub ip: Option<String>,
    pub status: String,
    pub status_message: String,
    pub creation_time: String,
    pub modification_time: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverRuleView {
    pub id: String,
    pub arn: String,
    pub name: String,
    pub domain_name: String,
    pub rule_type: String,
    pub resolver_endpoint_id: Option<String>,
    pub target_ips: Vec<TargetAddressView>,
    pub status: String,
    pub status_message: String,
    pub owner_id: String,
    pub share_status: String,
    pub creator_request_id: String,
    pub creation_time: String,
    pub modification_time: String,
    #[serde(default)]
    pub tags: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TargetAddressView {
    pub ip: Option<String>,
    pub ipv6: Option<String>,
    pub port: Option<i32>,
    pub protocol: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverRuleAssociationView {
    pub id: String,
    pub resolver_rule_id: String,
    pub name: String,
    pub vpc_id: String,
    pub status: String,
    pub status_message: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverQueryLogConfigView {
    pub id: String,
    pub arn: String,
    pub name: String,
    pub destination_arn: String,
    pub owner_id: String,
    pub status: String,
    pub share_status: String,
    pub association_count: i32,
    pub creator_request_id: String,
    pub creation_time: String,
    #[serde(default)]
    pub tags: HashMap<String, String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverQueryLogConfigAssociationView {
    pub id: String,
    pub resolver_query_log_config_id: String,
    pub resource_id: String,
    pub status: String,
    pub error: String,
    pub error_message: String,
    pub creation_time: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolverDnssecConfigView {
    pub id: String,
    pub owner_id: String,
    pub resource_id: String,
    pub validation_status: String,
}

// ---------------------------------------------------------------------------
// From conversions
// ---------------------------------------------------------------------------

impl From<&IpAddressEntry> for IpAddressEntryView {
    fn from(e: &IpAddressEntry) -> Self {
        Self {
            ip_id: e.ip_id.clone(),
            subnet_id: e.subnet_id.clone(),
            ip: e.ip.clone(),
            status: e.status.clone(),
            status_message: e.status_message.clone(),
            creation_time: e.creation_time.to_rfc3339(),
            modification_time: e.modification_time.to_rfc3339(),
        }
    }
}

impl From<&ResolverEndpoint> for ResolverEndpointView {
    fn from(e: &ResolverEndpoint) -> Self {
        Self {
            id: e.id.clone(),
            arn: e.arn.clone(),
            name: e.name.clone(),
            security_group_ids: e.security_group_ids.clone(),
            direction: e.direction.clone(),
            ip_address_count: e.ip_address_count,
            host_vpc_id: e.host_vpc_id.clone(),
            status: e.status.clone(),
            status_message: e.status_message.clone(),
            creation_time: e.creation_time.to_rfc3339(),
            modification_time: e.modification_time.to_rfc3339(),
            creator_request_id: e.creator_request_id.clone(),
            protocols: e.protocols.clone(),
            resolver_endpoint_type: e.resolver_endpoint_type.clone(),
            ip_addresses: e
                .ip_addresses
                .iter()
                .map(IpAddressEntryView::from)
                .collect(),
            tags: e.tags.clone(),
        }
    }
}

impl From<&TargetAddress> for TargetAddressView {
    fn from(t: &TargetAddress) -> Self {
        Self {
            ip: t.ip.clone(),
            ipv6: t.ipv6.clone(),
            port: t.port,
            protocol: t.protocol.clone(),
        }
    }
}

impl From<&ResolverRule> for ResolverRuleView {
    fn from(r: &ResolverRule) -> Self {
        Self {
            id: r.id.clone(),
            arn: r.arn.clone(),
            name: r.name.clone(),
            domain_name: r.domain_name.clone(),
            rule_type: r.rule_type.clone(),
            resolver_endpoint_id: r.resolver_endpoint_id.clone(),
            target_ips: r.target_ips.iter().map(TargetAddressView::from).collect(),
            status: r.status.clone(),
            status_message: r.status_message.clone(),
            owner_id: r.owner_id.clone(),
            share_status: r.share_status.clone(),
            creator_request_id: r.creator_request_id.clone(),
            creation_time: r.creation_time.to_rfc3339(),
            modification_time: r.modification_time.to_rfc3339(),
            tags: r.tags.clone(),
        }
    }
}

impl From<&ResolverRuleAssociation> for ResolverRuleAssociationView {
    fn from(a: &ResolverRuleAssociation) -> Self {
        Self {
            id: a.id.clone(),
            resolver_rule_id: a.resolver_rule_id.clone(),
            name: a.name.clone(),
            vpc_id: a.vpc_id.clone(),
            status: a.status.clone(),
            status_message: a.status_message.clone(),
        }
    }
}

impl From<&ResolverQueryLogConfig> for ResolverQueryLogConfigView {
    fn from(c: &ResolverQueryLogConfig) -> Self {
        Self {
            id: c.id.clone(),
            arn: c.arn.clone(),
            name: c.name.clone(),
            destination_arn: c.destination_arn.clone(),
            owner_id: c.owner_id.clone(),
            status: c.status.clone(),
            share_status: c.share_status.clone(),
            association_count: c.association_count,
            creator_request_id: c.creator_request_id.clone(),
            creation_time: c.creation_time.to_rfc3339(),
            tags: c.tags.clone(),
        }
    }
}

impl From<&ResolverQueryLogConfigAssociation> for ResolverQueryLogConfigAssociationView {
    fn from(a: &ResolverQueryLogConfigAssociation) -> Self {
        Self {
            id: a.id.clone(),
            resolver_query_log_config_id: a.resolver_query_log_config_id.clone(),
            resource_id: a.resource_id.clone(),
            status: a.status.clone(),
            error: a.error.clone(),
            error_message: a.error_message.clone(),
            creation_time: a.creation_time.to_rfc3339(),
        }
    }
}

impl From<&ResolverDnssecConfig> for ResolverDnssecConfigView {
    fn from(c: &ResolverDnssecConfig) -> Self {
        Self {
            id: c.id.clone(),
            owner_id: c.owner_id.clone(),
            resource_id: c.resource_id.clone(),
            validation_status: c.validation_status.clone(),
        }
    }
}

// ---------------------------------------------------------------------------
// StatefulService implementation
// ---------------------------------------------------------------------------

impl StatefulService for Route53ResolverService {
    type StateView = Route53ResolverStateView;

    async fn snapshot(&self, account_id: &str, region: &str) -> Self::StateView {
        let state = self.state.get(account_id, region);
        let guard = state.read().await;

        Route53ResolverStateView {
            endpoints: guard
                .endpoints
                .iter()
                .map(|(k, v)| (k.clone(), ResolverEndpointView::from(v)))
                .collect(),
            resolver_rules: guard
                .resolver_rules
                .iter()
                .map(|(k, v)| (k.clone(), ResolverRuleView::from(v)))
                .collect(),
            rule_associations: guard
                .rule_associations
                .iter()
                .map(|(k, v)| (k.clone(), ResolverRuleAssociationView::from(v)))
                .collect(),
            query_log_configs: guard
                .query_log_configs
                .iter()
                .map(|(k, v)| (k.clone(), ResolverQueryLogConfigView::from(v)))
                .collect(),
            query_log_config_associations: guard
                .query_log_config_associations
                .iter()
                .map(|(k, v)| (k.clone(), ResolverQueryLogConfigAssociationView::from(v)))
                .collect(),
            dnssec_configs: guard
                .dnssec_configs
                .iter()
                .map(|(k, v)| (k.clone(), ResolverDnssecConfigView::from(v)))
                .collect(),
        }
    }

    async fn restore(
        &self,
        account_id: &str,
        region: &str,
        view: Self::StateView,
    ) -> Result<(), StateViewError> {
        let new_state = state_from_view(view);
        {
            let state = self.state.get(account_id, region);
            *state.write().await = new_state;
        }
        self.notify_state_changed(account_id, region).await;
        Ok(())
    }

    async fn merge(
        &self,
        account_id: &str,
        region: &str,
        view: Self::StateView,
    ) -> Result<(), StateViewError> {
        let incoming = state_from_view(view);
        let state = self.state.get(account_id, region);
        {
            let mut guard = state.write().await;
            guard.endpoints.extend(incoming.endpoints);
            guard.resolver_rules.extend(incoming.resolver_rules);
            guard.rule_associations.extend(incoming.rule_associations);
            guard.query_log_configs.extend(incoming.query_log_configs);
            guard
                .query_log_config_associations
                .extend(incoming.query_log_config_associations);
            guard.dnssec_configs.extend(incoming.dnssec_configs);
        }
        self.notify_state_changed(account_id, region).await;
        Ok(())
    }

    fn notifier(&self) -> &StateChangeNotifier<Self::StateView> {
        &self.notifier
    }
}

fn parse_dt(s: &str) -> DateTime<Utc> {
    DateTime::parse_from_rfc3339(s)
        .map(|dt| dt.with_timezone(&Utc))
        .unwrap_or_else(|_| Utc::now())
}

fn state_from_view(view: Route53ResolverStateView) -> Route53ResolverState {
    let mut state = Route53ResolverState::default();

    for (id, ev) in view.endpoints {
        let ip_addresses = ev
            .ip_addresses
            .into_iter()
            .map(|e| IpAddressEntry {
                ip_id: e.ip_id,
                subnet_id: e.subnet_id,
                ip: e.ip,
                status: e.status,
                status_message: e.status_message,
                creation_time: parse_dt(&e.creation_time),
                modification_time: parse_dt(&e.modification_time),
            })
            .collect();
        state.endpoints.insert(
            id,
            ResolverEndpoint {
                id: ev.id,
                arn: ev.arn,
                name: ev.name,
                security_group_ids: ev.security_group_ids,
                direction: ev.direction,
                ip_address_count: ev.ip_address_count,
                host_vpc_id: ev.host_vpc_id,
                status: ev.status,
                status_message: ev.status_message,
                creation_time: parse_dt(&ev.creation_time),
                modification_time: parse_dt(&ev.modification_time),
                creator_request_id: ev.creator_request_id,
                protocols: ev.protocols,
                resolver_endpoint_type: ev.resolver_endpoint_type,
                ip_addresses,
                tags: ev.tags,
            },
        );
    }

    for (id, rv) in view.resolver_rules {
        let target_ips = rv
            .target_ips
            .into_iter()
            .map(|t| TargetAddress {
                ip: t.ip,
                ipv6: t.ipv6,
                port: t.port,
                protocol: t.protocol,
            })
            .collect();
        state.resolver_rules.insert(
            id,
            ResolverRule {
                id: rv.id,
                arn: rv.arn,
                name: rv.name,
                domain_name: rv.domain_name,
                rule_type: rv.rule_type,
                resolver_endpoint_id: rv.resolver_endpoint_id,
                target_ips,
                status: rv.status,
                status_message: rv.status_message,
                owner_id: rv.owner_id,
                share_status: rv.share_status,
                creator_request_id: rv.creator_request_id,
                creation_time: parse_dt(&rv.creation_time),
                modification_time: parse_dt(&rv.modification_time),
                tags: rv.tags,
            },
        );
    }

    for (id, av) in view.rule_associations {
        state.rule_associations.insert(
            id,
            ResolverRuleAssociation {
                id: av.id,
                resolver_rule_id: av.resolver_rule_id,
                name: av.name,
                vpc_id: av.vpc_id,
                status: av.status,
                status_message: av.status_message,
            },
        );
    }

    for (id, cv) in view.query_log_configs {
        state.query_log_configs.insert(
            id,
            ResolverQueryLogConfig {
                id: cv.id,
                arn: cv.arn,
                name: cv.name,
                destination_arn: cv.destination_arn,
                owner_id: cv.owner_id,
                status: cv.status,
                share_status: cv.share_status,
                association_count: cv.association_count,
                creator_request_id: cv.creator_request_id,
                creation_time: parse_dt(&cv.creation_time),
                tags: cv.tags,
            },
        );
    }

    for (id, av) in view.query_log_config_associations {
        state.query_log_config_associations.insert(
            id,
            ResolverQueryLogConfigAssociation {
                id: av.id,
                resolver_query_log_config_id: av.resolver_query_log_config_id,
                resource_id: av.resource_id,
                status: av.status,
                error: av.error,
                error_message: av.error_message,
                creation_time: parse_dt(&av.creation_time),
            },
        );
    }

    for (id, dv) in view.dnssec_configs {
        state.dnssec_configs.insert(
            id,
            ResolverDnssecConfig {
                id: dv.id,
                owner_id: dv.owner_id,
                resource_id: dv.resource_id,
                validation_status: dv.validation_status,
            },
        );
    }

    state
}