ave-bridge 0.11.0

Application bridge for embedding and configuring the Ave runtime
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
use config::Config;
use std::collections::HashSet;
use tracing::{error, warn};

pub mod command;
use crate::config::Config as BridgeConfig;
use crate::error::BridgeError;

pub fn build_config(file: &str) -> Result<BridgeConfig, BridgeError> {
    // file configuration (json, yaml or toml)
    let bridge_config = if !file.is_empty() {
        let mut config = Config::builder();

        config = config.add_source(config::File::with_name(file));

        let config = config.build().map_err(|e| {
            error!(file = %file, error = %e, "Failed to build configuration");
            BridgeError::ConfigBuild(e.to_string())
        })?;

        config.try_deserialize().map_err(|e| {
            error!(file = %file, error = %e, "Failed to deserialize configuration");
            BridgeError::ConfigDeserialize(e.to_string())
        })?
    } else {
        BridgeConfig::default()
    };

    // Validate HTTPS configuration
    validate_https_config(&bridge_config)?;

    // Validate network configuration
    validate_network_config(&bridge_config)?;

    // Mix configurations.
    Ok(bridge_config)
}

/// Validate network configuration
fn validate_network_config(config: &BridgeConfig) -> Result<(), BridgeError> {
    let network = &config.node.network;

    network.memory_limits.validate().map_err(|e| {
        error!(error = %e, "Invalid network configuration");
        BridgeError::ConfigBuild(e)
    })?;

    if network.max_app_message_bytes == 0 {
        let msg =
            "network.max_app_message_bytes must be greater than 0".to_owned();
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if network.max_pending_outbound_bytes_per_peer > 0
        && network.max_pending_outbound_bytes_per_peer
            < network.max_app_message_bytes
    {
        let msg = format!(
            "network.max_pending_outbound_bytes_per_peer ({}) must be >= network.max_app_message_bytes ({})",
            network.max_pending_outbound_bytes_per_peer,
            network.max_app_message_bytes
        );
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if network.max_pending_inbound_bytes_per_peer > 0
        && network.max_pending_inbound_bytes_per_peer
            < network.max_app_message_bytes
    {
        let msg = format!(
            "network.max_pending_inbound_bytes_per_peer ({}) must be >= network.max_app_message_bytes ({})",
            network.max_pending_inbound_bytes_per_peer,
            network.max_app_message_bytes
        );
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if network.max_pending_outbound_bytes_total > 0
        && network.max_pending_outbound_bytes_total
            < network.max_app_message_bytes
    {
        let msg = format!(
            "network.max_pending_outbound_bytes_total ({}) must be >= network.max_app_message_bytes ({})",
            network.max_pending_outbound_bytes_total,
            network.max_app_message_bytes
        );
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if network.max_pending_inbound_bytes_total > 0
        && network.max_pending_inbound_bytes_total
            < network.max_app_message_bytes
    {
        let msg = format!(
            "network.max_pending_inbound_bytes_total ({}) must be >= network.max_app_message_bytes ({})",
            network.max_pending_inbound_bytes_total,
            network.max_app_message_bytes
        );
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    for addr in &network.listen_addresses {
        if addr.trim().is_empty() {
            let msg =
                "network.listen_addresses contains an empty address".to_owned();
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    for addr in &network.external_addresses {
        if addr.trim().is_empty() {
            let msg = "network.external_addresses contains an empty address"
                .to_owned();
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    for (index, node) in network.boot_nodes.iter().enumerate() {
        if node.peer_id.trim().is_empty() {
            let msg = format!("network.boot_nodes[{index}].peer_id is empty");
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
        if node.address.is_empty() {
            let msg = format!(
                "network.boot_nodes[{index}] must contain at least one address"
            );
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
        if node.address.iter().any(|addr| addr.trim().is_empty()) {
            let msg = format!(
                "network.boot_nodes[{index}] contains an empty address"
            );
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    let control_list = &network.control_list;
    if control_list.get_interval_request().is_zero() {
        let msg =
            "network.control_list.interval_request must be greater than 0"
                .to_owned();
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if control_list.get_request_timeout().is_zero() {
        let msg = "network.control_list.request_timeout must be greater than 0"
            .to_owned();
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    if control_list.get_request_timeout() > control_list.get_interval_request()
    {
        let msg = format!(
            "network.control_list.request_timeout ({:?}) must be <= network.control_list.interval_request ({:?})",
            control_list.get_request_timeout(),
            control_list.get_interval_request()
        );
        error!(error = %msg, "Invalid network configuration");
        return Err(BridgeError::ConfigBuild(msg));
    }

    // `max_concurrent_requests = 0` is accepted and normalized at runtime to 1
    // (see network/utils.rs request_peer_lists buffer_unordered max(1)).

    for service in control_list.get_service_allow_list() {
        if !(service.starts_with("http://") || service.starts_with("https://"))
        {
            let msg = format!(
                "network.control_list.service_allow_list contains an invalid URL: {service}"
            );
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    for service in control_list.get_service_block_list() {
        if !(service.starts_with("http://") || service.starts_with("https://"))
        {
            let msg = format!(
                "network.control_list.service_block_list contains an invalid URL: {service}"
            );
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    if control_list.get_enable() {
        let has_allow_source = !control_list.get_allow_list().is_empty()
            || !control_list.get_service_allow_list().is_empty()
            || !network.boot_nodes.is_empty();
        if !has_allow_source {
            let msg = "network.control_list.enable is true but there are no allow sources (allow_list, service_allow_list or boot_nodes)".to_owned();
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }

        let allow: HashSet<String> = control_list
            .get_allow_list()
            .into_iter()
            .map(|peer| peer.trim().to_owned())
            .collect();
        let block: HashSet<String> = control_list
            .get_block_list()
            .into_iter()
            .map(|peer| peer.trim().to_owned())
            .collect();
        if let Some(peer) = allow.intersection(&block).next() {
            let msg = format!(
                "network.control_list has peer present in both allow_list and block_list: {peer}"
            );
            error!(error = %msg, "Invalid network configuration");
            return Err(BridgeError::ConfigBuild(msg));
        }
    }

    Ok(())
}

/// Validate HTTPS configuration consistency
fn validate_https_config(config: &BridgeConfig) -> Result<(), BridgeError> {
    let http = &config.http;

    if http.https_address.is_some()
        && (http.https_cert_path.is_none()
            || http.https_private_key_path.is_none())
    {
        let msg = "HTTPS is enabled (https_address is set) but https_cert_path \
                   and/or https_private_key_path are missing";
        error!(error = %msg, "Invalid HTTPS configuration");
        return Err(BridgeError::ConfigBuild(msg.to_owned()));
    }

    if http.self_signed_cert.enabled && http.https_address.is_none() {
        warn!(
            "self_signed_cert.enabled is true but https_address is not set, \
             self-signed certificates will not be used"
        );
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use std::{
        collections::{BTreeMap, BTreeSet},
        path::PathBuf,
        time::Duration,
    };

    use ave_common::identity::{HashAlgorithm, KeyPairAlgorithm};
    use ave_core::{
        config::{
            AveExternalDBFeatureConfig, AveInternalDBFeatureConfig,
            LoggingOutput, LoggingRotation, MachineSpec, SinkQueuePolicy,
            SinkRoutingStrategy, SinkServer,
        },
        subject::sinkdata::SinkTypes,
    };
    use ave_network::{MemoryLimitsConfig, NodeType, RoutingNode};
    use tempfile::TempPath;

    use crate::{
        config::Config as BridgeConfig, error::BridgeError,
        settings::build_config,
    };

    const FULL_TOML: &str = r#"
keys_path = "/custom/keys"

[node]
keypair_algorithm = "Ed25519"
hash_algorithm = "Blake3"
contracts_path = "/contracts_proof"
always_accept = true
tracking_size = 200
is_service = true
only_clear_events = true

[node.sync]
ledger_batch_size = 150

[node.sync.governance]
interval_secs = 20
sample_size = 2
response_timeout_secs = 7

[node.sync.tracker]
interval_secs = 30
page_size = 200
response_timeout_secs = 8
update_batch_size = 2
update_timeout_secs = 6

[node.internal_db]
db = "/data/ave.db"
durability = true

[node.external_db]
db = "/data/ext.db"
durability = true

[node.spec]
custom = { ram_mb = 2048, cpu_cores = 4 }

[node.network]
node_type = "Addressable"
listen_addresses = ["/ip4/127.0.0.1/tcp/5001", "/ip4/127.0.0.1/tcp/5002"]
external_addresses = ["/ip4/10.0.0.1/tcp/7000"]
boot_nodes = [
    { peer_id = "12D3KooWNode1", address = ["/ip4/1.1.1.1/tcp/1000"] },
    { peer_id = "12D3KooWNode2", address = ["/ip4/2.2.2.2/tcp/2000"] }
]
max_app_message_bytes = 2097152
max_pending_outbound_bytes_per_peer = 16777216
max_pending_inbound_bytes_per_peer = 8388608
max_pending_outbound_bytes_total = 33554432
max_pending_inbound_bytes_total = 25165824

[node.network.routing]
dht_random_walk = false
discovery_only_if_under_num = 25
allow_private_address_in_dht = true
allow_dns_address_in_dht = true
allow_loop_back_address_in_dht = true
kademlia_disjoint_query_paths = false

[node.network.control_list]
enable = true
allow_list = ["Peer200", "Peer300"]
block_list = ["Peer1", "Peer2"]
service_allow_list = ["http://allow.local/list"]
service_block_list = ["http://block.local/list"]
interval_request = 42
request_timeout = 7
max_concurrent_requests = 16

[node.network.memory_limits]
type = "percentage"
value = 0.8

[logging]
output = { stdout = false, file = true, api = true }
api_url = "https://example.com/logs"
file_path = "/tmp/my.log"
rotation = "hourly"
max_size = 52428800
max_files = 5
level = "debug"

[sink]
auth = "https://auth.service"
username = "sink-user"

[[sink.sinks.primary]]
server = "SinkOne"
events = ["Create", "All"]
url = "https://sink.one"
auth = true
concurrency = 4
queue_capacity = 2048
queue_policy = "drop_oldest"
routing_strategy = "unordered_round_robin"
connect_timeout_ms = 5000
request_timeout_ms = 30000
max_retries = 5

[[sink.sinks.primary]]
server = "SinkTwo"
events = ["Transfer"]
url = "https://sink.two"
auth = false
concurrency = 2
queue_capacity = 512
queue_policy = "drop_newest"
routing_strategy = "ordered_by_subject"
connect_timeout_ms = 3000
request_timeout_ms = 15000
max_retries = 1

[auth]
enable = true
database_path = "/var/db/auth.db"
superadmin = "admin:supersecret"
durability = true

[auth.api_key]
default_ttl_seconds = 3600
max_keys_per_user = 20
prefix = "custom_prefix_"

[auth.lockout]
max_attempts = 3
duration_seconds = 600

[auth.rate_limit]
enable = false
window_seconds = 120
max_requests = 50
limit_by_key = false
limit_by_ip = true
cleanup_interval_seconds = 1800

[[auth.rate_limit.sensitive_endpoints]]
endpoint = "/login"
max_requests = 5
window_seconds = 30

[auth.session]
audit_enable = false
audit_retention_days = 30
audit_max_entries = 1000000

[http]
http_address = "127.0.0.1:4000"
https_address = "127.0.0.1:4443"
https_cert_path = "/certs/cert.pem"
https_private_key_path = "/certs/key.pem"
enable_doc = true

[http.proxy]
trusted_proxies = ["10.0.0.1"]
trust_x_forwarded_for = false
trust_x_real_ip = false

[http.cors]
enabled = false
allow_any_origin = false
allowed_origins = ["https://app.example.com"]
allow_credentials = true

[http.self_signed_cert]
enabled = true
common_name = "localhost"
san = ["127.0.0.1", "::1"]
validity_days = 365
renew_before_days = 30
check_interval_secs = 3600
"#;

    const FULL_YAML: &str = r#"
keys_path: /custom/keys
node:
  keypair_algorithm: Ed25519
  hash_algorithm: Blake3
  internal_db:
    db: /data/ave.db
    durability: true
  external_db:
    db: /data/ext.db
    durability: true
  spec:
    custom:
      ram_mb: 2048
      cpu_cores: 4
  contracts_path: /contracts_proof
  always_accept: true
  tracking_size: 200
  is_service: true
  only_clear_events: true
  sync:
    ledger_batch_size: 150
    governance:
      interval_secs: 20
      sample_size: 2
      response_timeout_secs: 7
    tracker:
      interval_secs: 30
      page_size: 200
      response_timeout_secs: 8
      update_batch_size: 2
      update_timeout_secs: 6
  network:
    node_type: Addressable
    listen_addresses:
      - /ip4/127.0.0.1/tcp/5001
      - /ip4/127.0.0.1/tcp/5002
    external_addresses:
      - /ip4/10.0.0.1/tcp/7000
    boot_nodes:
      - peer_id: 12D3KooWNode1
        address:
          - /ip4/1.1.1.1/tcp/1000
      - peer_id: 12D3KooWNode2
        address:
          - /ip4/2.2.2.2/tcp/2000
    max_app_message_bytes: 2097152
    max_pending_outbound_bytes_per_peer: 16777216
    max_pending_inbound_bytes_per_peer: 8388608
    max_pending_outbound_bytes_total: 33554432
    max_pending_inbound_bytes_total: 25165824
    routing:
      dht_random_walk: false
      discovery_only_if_under_num: 25
      allow_private_address_in_dht: true
      allow_dns_address_in_dht: true
      allow_loop_back_address_in_dht: true
      kademlia_disjoint_query_paths: false
    control_list:
      enable: true
      allow_list: [Peer200, Peer300]
      block_list: [Peer1, Peer2]
      service_allow_list: [http://allow.local/list]
      service_block_list: [http://block.local/list]
      interval_request: 42
      request_timeout: 7
      max_concurrent_requests: 16
    memory_limits:
      type: percentage
      value: 0.8
logging:
  output:
    stdout: false
    file: true
    api: true
  api_url: https://example.com/logs
  file_path: /tmp/my.log
  rotation: hourly
  max_size: 52428800
  max_files: 5
  level: debug
sink:
  auth: https://auth.service
  username: sink-user
  sinks:
    primary:
      - server: SinkOne
        events: [Create, All]
        url: https://sink.one
        auth: true
        concurrency: 4
        queue_capacity: 2048
        queue_policy: drop_oldest
        routing_strategy: unordered_round_robin
        connect_timeout_ms: 5000
        request_timeout_ms: 30000
        max_retries: 5
      - server: SinkTwo
        events: [Transfer]
        url: https://sink.two
        auth: false
        concurrency: 2
        queue_capacity: 512
        queue_policy: drop_newest
        routing_strategy: ordered_by_subject
        connect_timeout_ms: 3000
        request_timeout_ms: 15000
        max_retries: 1
auth:
  enable: true
  database_path: /var/db/auth.db
  superadmin: admin:supersecret
  durability: true
  api_key:
    default_ttl_seconds: 3600
    max_keys_per_user: 20
    prefix: custom_prefix_
  lockout:
    max_attempts: 3
    duration_seconds: 600
  rate_limit:
    enable: false
    window_seconds: 120
    max_requests: 50
    limit_by_key: false
    limit_by_ip: true
    cleanup_interval_seconds: 1800
    sensitive_endpoints:
      - endpoint: /login
        max_requests: 5
        window_seconds: 30
  session:
    audit_enable: false
    audit_retention_days: 30
    audit_max_entries: 1000000
http:
  http_address: 127.0.0.1:4000
  https_address: 127.0.0.1:4443
  https_cert_path: /certs/cert.pem
  https_private_key_path: /certs/key.pem
  enable_doc: true
  proxy:
    trusted_proxies:
      - 10.0.0.1
    trust_x_forwarded_for: false
    trust_x_real_ip: false
  cors:
    enabled: false
    allow_any_origin: false
    allowed_origins:
      - https://app.example.com
    allow_credentials: true
  self_signed_cert:
    enabled: true
    common_name: localhost
    san:
      - "127.0.0.1"
      - "::1"
    validity_days: 365
    renew_before_days: 30
    check_interval_secs: 3600
"#;

    const FULL_JSON: &str = r#"
{
  "keys_path": "/custom/keys",
  "node": {
    "keypair_algorithm": "Ed25519",
    "hash_algorithm": "Blake3",
    "internal_db": {
      "db": "/data/ave.db",
      "durability": true
    },
    "external_db": {
      "db": "/data/ext.db",
      "durability": true
    },
    "spec": {
      "custom": {
        "ram_mb": 2048,
        "cpu_cores": 4
      }
    },
    "contracts_path": "/contracts_proof",
    "always_accept": true,
    "tracking_size": 200,
    "is_service": true,
    "only_clear_events": true,
    "sync": {
      "ledger_batch_size": 150,
      "governance": {
        "interval_secs": 20,
        "sample_size": 2,
        "response_timeout_secs": 7
      },
      "tracker": {
        "interval_secs": 30,
        "page_size": 200,
        "response_timeout_secs": 8,
        "update_batch_size": 2,
        "update_timeout_secs": 6
      }
    },
    "network": {
      "node_type": "Addressable",
      "listen_addresses": [
        "/ip4/127.0.0.1/tcp/5001",
        "/ip4/127.0.0.1/tcp/5002"
      ],
      "external_addresses": [
        "/ip4/10.0.0.1/tcp/7000"
      ],
      "boot_nodes": [
        {
          "peer_id": "12D3KooWNode1",
          "address": ["/ip4/1.1.1.1/tcp/1000"]
        },
        {
          "peer_id": "12D3KooWNode2",
          "address": ["/ip4/2.2.2.2/tcp/2000"]
        }
      ],
      "max_app_message_bytes": 2097152,
      "max_pending_outbound_bytes_per_peer": 16777216,
      "max_pending_inbound_bytes_per_peer": 8388608,
      "max_pending_outbound_bytes_total": 33554432,
      "max_pending_inbound_bytes_total": 25165824,
      "routing": {
        "dht_random_walk": false,
        "discovery_only_if_under_num": 25,
        "allow_private_address_in_dht": true,
        "allow_dns_address_in_dht": true,
        "allow_loop_back_address_in_dht": true,
        "kademlia_disjoint_query_paths": false
      },
      "control_list": {
        "enable": true,
        "allow_list": ["Peer200", "Peer300"],
        "block_list": ["Peer1", "Peer2"],
        "service_allow_list": ["http://allow.local/list"],
        "service_block_list": ["http://block.local/list"],
        "interval_request": 42,
        "request_timeout": 7,
        "max_concurrent_requests": 16
      },
      "memory_limits": {
        "type": "percentage",
        "value": 0.8
      }
    }
  },
  "logging": {
    "output": {
      "stdout": false,
      "file": true,
      "api": true
    },
    "api_url": "https://example.com/logs",
    "file_path": "/tmp/my.log",
    "rotation": "hourly",
    "max_size": 52428800,
    "max_files": 5,
    "level": "debug"
  },
  "sink": {
    "auth": "https://auth.service",
    "username": "sink-user",
    "sinks": {
      "primary": [
        {
          "server": "SinkOne",
          "events": ["Create", "All"],
          "url": "https://sink.one",
          "auth": true,
          "concurrency": 4,
          "queue_capacity": 2048,
          "queue_policy": "drop_oldest",
          "routing_strategy": "unordered_round_robin",
          "connect_timeout_ms": 5000,
          "request_timeout_ms": 30000,
          "max_retries": 5
        },
        {
          "server": "SinkTwo",
          "events": ["Transfer"],
          "url": "https://sink.two",
          "auth": false,
          "concurrency": 2,
          "queue_capacity": 512,
          "queue_policy": "drop_newest",
          "routing_strategy": "ordered_by_subject",
          "connect_timeout_ms": 3000,
          "request_timeout_ms": 15000,
          "max_retries": 1
        }
      ]
    }
  },
  "auth": {
    "enable": true,
    "database_path": "/var/db/auth.db",
    "superadmin": "admin:supersecret",
    "durability": true,
    "api_key": {
      "default_ttl_seconds": 3600,
      "max_keys_per_user": 20,
      "prefix": "custom_prefix_"
    },
    "lockout": {
      "max_attempts": 3,
      "duration_seconds": 600
    },
    "rate_limit": {
      "enable": false,
      "window_seconds": 120,
      "max_requests": 50,
      "limit_by_key": false,
      "limit_by_ip": true,
      "cleanup_interval_seconds": 1800,
      "sensitive_endpoints": [
        { "endpoint": "/login", "max_requests": 5, "window_seconds": 30 }
      ]
    },
    "session": {
      "audit_enable": false,
      "audit_retention_days": 30,
      "audit_max_entries": 1000000
    }
  },
  "http": {
    "http_address": "127.0.0.1:4000",
    "https_address": "127.0.0.1:4443",
    "https_cert_path": "/certs/cert.pem",
    "https_private_key_path": "/certs/key.pem",
    "enable_doc": true,
    "proxy": {
      "trusted_proxies": ["10.0.0.1"],
      "trust_x_forwarded_for": false,
      "trust_x_real_ip": false
    },
    "cors": {
      "enabled": false,
      "allow_any_origin": false,
      "allowed_origins": ["https://app.example.com"],
      "allow_credentials": true
    },
    "self_signed_cert": {
      "enabled": true,
      "common_name": "localhost",
      "san": ["127.0.0.1", "::1"],
      "validity_days": 365,
      "renew_before_days": 30,
      "check_interval_secs": 3600
    }
  }
}
"#;

    const PARTIAL_TOML: &str = r#"
keys_path = "/partial/keys"

[auth]
enable = true

[http]
http_address = "127.0.0.1:8888"
enable_doc = true
"#;

    const PARTIAL_YAML: &str = r#"
keys_path: /partial/keys
auth:
  enable: true
http:
  http_address: 127.0.0.1:8888
  enable_doc: true
"#;

    const PARTIAL_JSON: &str = r#"
{
  "keys_path": "/partial/keys",
  "auth": {
    "enable": true
  },
  "http": {
    "http_address": "127.0.0.1:8888",
    "enable_doc": true
  }
}
"#;

    #[test]
    fn build_config_reads_full_toml() {
        let path = write_config("toml", FULL_TOML);
        let config = build_config(path.to_str().unwrap()).expect("toml config");
        assert_full_config(config);
    }

    #[test]
    fn build_config_reads_full_yaml() {
        let path = write_config("yaml", FULL_YAML);
        let config = build_config(path.to_str().unwrap()).expect("yaml config");
        assert_full_config(config);
    }

    #[test]
    fn build_config_reads_full_json() {
        let path = write_config("json", FULL_JSON);
        let config = build_config(path.to_str().unwrap()).expect("json config");
        assert_full_config(config);
    }

    #[test]
    fn build_config_fills_defaults_for_partial_toml() {
        let path = write_config("toml", PARTIAL_TOML);
        let config =
            build_config(path.to_str().unwrap()).expect("partial toml config");
        assert_partial_defaults(config);
    }

    #[test]
    fn build_config_fills_defaults_for_partial_yaml() {
        let path = write_config("yaml", PARTIAL_YAML);
        let config =
            build_config(path.to_str().unwrap()).expect("partial yaml config");
        assert_partial_defaults(config);
    }

    #[test]
    fn build_config_fills_defaults_for_partial_json() {
        let path = write_config("json", PARTIAL_JSON);
        let config =
            build_config(path.to_str().unwrap()).expect("partial json config");
        assert_partial_defaults(config);
    }

    fn write_config(extension: &str, content: &str) -> TempPath {
        let file = tempfile::Builder::new()
            .suffix(&format!(".{extension}"))
            .tempfile()
            .expect("create temp config file");
        std::fs::write(file.path(), content).expect("write temp config");
        file.into_temp_path()
    }

    fn assert_full_config(config: BridgeConfig) {
        assert_eq!(config.keys_path, PathBuf::from("/custom/keys"));

        let node = &config.node;
        assert_eq!(node.keypair_algorithm, KeyPairAlgorithm::Ed25519);
        assert_eq!(node.hash_algorithm, HashAlgorithm::Blake3);
        assert!(node.always_accept);
        assert_eq!(node.contracts_path, PathBuf::from("/contracts_proof"));
        assert_eq!(node.tracking_size, 200);
        assert!(node.is_service);
        assert!(node.only_clear_events);
        assert_eq!(node.sync.ledger_batch_size, 150);
        assert_eq!(node.sync.governance.interval_secs, 20);
        assert_eq!(node.sync.governance.sample_size, 2);
        assert_eq!(node.sync.governance.response_timeout_secs, 7);
        assert_eq!(node.sync.tracker.interval_secs, 30);
        assert_eq!(node.sync.tracker.page_size, 200);
        assert_eq!(node.sync.tracker.response_timeout_secs, 8);
        assert_eq!(node.sync.tracker.update_batch_size, 2);
        assert_eq!(node.sync.tracker.update_timeout_secs, 6);
        assert_eq!(
            node.internal_db.db,
            AveInternalDBFeatureConfig::build(&PathBuf::from("/data/ave.db"))
        );

        assert!(node.internal_db.durability);
        match &node.spec {
            Some(MachineSpec::Custom { ram_mb, cpu_cores }) => {
                assert_eq!(*ram_mb, 2048);
                assert_eq!(*cpu_cores, 4);
            }
            _ => panic!("Expected MachineSpec::Custom"),
        }
        assert_eq!(
            node.external_db.db,
            AveExternalDBFeatureConfig::build(&PathBuf::from("/data/ext.db"))
        );
        assert!(node.external_db.durability);

        assert_eq!(node.network.node_type, NodeType::Addressable);
        assert_eq!(
            node.network.listen_addresses,
            vec![
                "/ip4/127.0.0.1/tcp/5001".to_owned(),
                "/ip4/127.0.0.1/tcp/5002".to_owned()
            ]
        );
        assert_eq!(
            node.network.external_addresses,
            vec!["/ip4/10.0.0.1/tcp/7000".to_owned()]
        );
        let expected_boot_nodes = vec![
            RoutingNode {
                peer_id: "12D3KooWNode1".to_owned(),
                address: vec!["/ip4/1.1.1.1/tcp/1000".to_owned()],
            },
            RoutingNode {
                peer_id: "12D3KooWNode2".to_owned(),
                address: vec!["/ip4/2.2.2.2/tcp/2000".to_owned()],
            },
        ];
        assert_eq!(node.network.boot_nodes.len(), expected_boot_nodes.len());
        for expected in expected_boot_nodes {
            let Some(actual) = node
                .network
                .boot_nodes
                .iter()
                .find(|node| node.peer_id == expected.peer_id)
            else {
                panic!("boot node {} missing", expected.peer_id);
            };
            assert_eq!(actual.address, expected.address);
        }
        assert!(!node.network.routing.get_dht_random_walk());
        assert_eq!(node.network.routing.get_discovery_limit(), 25);
        assert!(node.network.routing.get_allow_private_address_in_dht());
        assert!(node.network.routing.get_allow_dns_address_in_dht());
        assert!(node.network.routing.get_allow_loop_back_address_in_dht());
        assert!(!node.network.routing.get_kademlia_disjoint_query_paths());
        assert!(node.network.control_list.get_enable());
        assert_eq!(
            node.network.control_list.get_allow_list(),
            vec!["Peer200", "Peer300"]
        );
        assert_eq!(
            node.network.control_list.get_block_list(),
            vec!["Peer1", "Peer2"]
        );
        assert_eq!(
            node.network.control_list.get_service_allow_list(),
            vec!["http://allow.local/list"]
        );
        assert_eq!(
            node.network.control_list.get_service_block_list(),
            vec!["http://block.local/list"]
        );
        assert_eq!(
            node.network.control_list.get_interval_request(),
            Duration::from_secs(42)
        );
        assert_eq!(
            node.network.control_list.get_request_timeout(),
            Duration::from_secs(7)
        );
        assert_eq!(node.network.control_list.get_max_concurrent_requests(), 16);
        assert_eq!(
            node.network.memory_limits,
            MemoryLimitsConfig::Percentage { value: 0.8 }
        );
        assert_eq!(node.network.max_app_message_bytes, 2097152);
        assert_eq!(node.network.max_pending_outbound_bytes_per_peer, 16777216);
        assert_eq!(node.network.max_pending_inbound_bytes_per_peer, 8388608);
        assert_eq!(node.network.max_pending_outbound_bytes_total, 33554432);
        assert_eq!(node.network.max_pending_inbound_bytes_total, 25165824);
        let logging = &config.logging;
        assert_eq!(
            logging.output,
            LoggingOutput {
                stdout: false,
                file: true,
                api: true
            }
        );
        assert_eq!(
            logging.api_url.as_deref(),
            Some("https://example.com/logs")
        );
        assert_eq!(logging.file_path, PathBuf::from("/tmp/my.log"));
        assert_eq!(logging.rotation, LoggingRotation::Hourly);
        assert_eq!(logging.max_size, 52_428_800);
        assert_eq!(logging.max_files, 5);
        assert_eq!(logging.level, "debug");

        let mut expected_sinks = BTreeMap::new();
        expected_sinks.insert(
            "primary".to_owned(),
            vec![
                SinkServer {
                    server: "SinkOne".to_owned(),
                    events: BTreeSet::from([SinkTypes::All, SinkTypes::Create]),
                    url: "https://sink.one".to_owned(),
                    auth: true,
                    concurrency: 4,
                    queue_capacity: 2048,
                    queue_policy: SinkQueuePolicy::DropOldest,
                    routing_strategy: SinkRoutingStrategy::UnorderedRoundRobin,
                    connect_timeout_ms: 5_000,
                    request_timeout_ms: 30_000,
                    max_retries: 5,
                },
                SinkServer {
                    server: "SinkTwo".to_owned(),
                    events: BTreeSet::from([SinkTypes::Transfer]),
                    url: "https://sink.two".to_owned(),
                    auth: false,
                    concurrency: 2,
                    queue_capacity: 512,
                    queue_policy: SinkQueuePolicy::DropNewest,
                    routing_strategy: SinkRoutingStrategy::OrderedBySubject,
                    connect_timeout_ms: 3_000,
                    request_timeout_ms: 15_000,
                    max_retries: 1,
                },
            ],
        );
        assert_eq!(config.sink.sinks, expected_sinks);
        assert_eq!(config.sink.auth, "https://auth.service");
        assert_eq!(config.sink.username, "sink-user");

        let auth = &config.auth;
        assert!(auth.enable);
        assert!(auth.durability);
        assert_eq!(auth.database_path, PathBuf::from("/var/db/auth.db"));
        assert_eq!(auth.superadmin, "admin:supersecret");
        assert_eq!(auth.api_key.default_ttl_seconds, 3600);
        assert_eq!(auth.api_key.max_keys_per_user, 20);
        assert_eq!(auth.api_key.prefix, "custom_prefix_");
        assert_eq!(auth.lockout.max_attempts, 3);
        assert_eq!(auth.lockout.duration_seconds, 600);
        assert!(!auth.rate_limit.enable);
        assert_eq!(auth.rate_limit.window_seconds, 120);
        assert_eq!(auth.rate_limit.max_requests, 50);
        assert!(!auth.rate_limit.limit_by_key);
        assert!(auth.rate_limit.limit_by_ip);
        assert_eq!(auth.rate_limit.cleanup_interval_seconds, 1800);
        assert_eq!(auth.rate_limit.sensitive_endpoints.len(), 1);
        assert_eq!(auth.rate_limit.sensitive_endpoints[0].endpoint, "/login");
        assert_eq!(auth.rate_limit.sensitive_endpoints[0].max_requests, 5);
        assert_eq!(
            auth.rate_limit.sensitive_endpoints[0].window_seconds,
            Some(30)
        );
        assert!(!auth.session.audit_enable);
        assert_eq!(auth.session.audit_retention_days, 30);
        assert_eq!(auth.session.audit_max_entries, 1_000_000);

        let http = &config.http;
        assert_eq!(http.http_address, "127.0.0.1:4000");
        assert_eq!(http.https_address.as_deref(), Some("127.0.0.1:4443"));
        assert_eq!(
            http.https_cert_path.as_deref(),
            Some(PathBuf::from("/certs/cert.pem").as_path())
        );
        assert_eq!(
            http.https_private_key_path.as_deref(),
            Some(PathBuf::from("/certs/key.pem").as_path())
        );
        assert!(http.enable_doc);
        assert_eq!(http.proxy.trusted_proxies, vec!["10.0.0.1".to_owned()]);
        assert!(!http.proxy.trust_x_forwarded_for);
        assert!(!http.proxy.trust_x_real_ip);
        assert!(!http.cors.enabled);
        assert!(!http.cors.allow_any_origin);
        assert_eq!(http.cors.allowed_origins, vec!["https://app.example.com"]);
        assert!(http.cors.allow_credentials);
        assert!(http.self_signed_cert.enabled);
        assert_eq!(http.self_signed_cert.common_name, "localhost");
        assert_eq!(
            http.self_signed_cert.san,
            vec!["127.0.0.1".to_owned(), "::1".to_owned()]
        );
        assert_eq!(http.self_signed_cert.validity_days, 365);
        assert_eq!(http.self_signed_cert.renew_before_days, 30);
        assert_eq!(http.self_signed_cert.check_interval_secs, 3600);
    }

    fn assert_partial_defaults(config: BridgeConfig) {
        assert_eq!(config.keys_path, PathBuf::from("/partial/keys"));
        assert!(config.auth.enable);
        assert_eq!(config.http.http_address, "127.0.0.1:8888");
        assert!(config.http.enable_doc);

        // Defaults remain for everything not provided.
        assert_eq!(config.logging.output.stdout, true);
        assert_eq!(config.logging.output.file, false);
        assert_eq!(config.logging.rotation, LoggingRotation::Size);
        assert_eq!(config.logging.file_path, PathBuf::from("logs"));
        assert_eq!(config.logging.max_files, 3);
        assert_eq!(config.sink.sinks.len(), 0);

        assert_eq!(config.node.keypair_algorithm, KeyPairAlgorithm::Ed25519);
        assert_eq!(config.node.hash_algorithm, HashAlgorithm::Blake3);
        assert_eq!(config.node.contracts_path, PathBuf::from("contracts"));
        assert_eq!(
            config.node.internal_db.db,
            AveInternalDBFeatureConfig::default()
        );
        assert_eq!(
            config.node.external_db.db,
            AveExternalDBFeatureConfig::default()
        );
        assert_eq!(config.node.tracking_size, 100);
        assert!(!config.node.is_service);
        assert!(!config.node.only_clear_events);
        assert_eq!(config.node.sync.ledger_batch_size, 100);
        assert_eq!(config.node.sync.governance.interval_secs, 60);
        assert_eq!(config.node.sync.governance.sample_size, 3);
        assert_eq!(config.node.sync.governance.response_timeout_secs, 10);
        assert_eq!(config.node.sync.tracker.interval_secs, 30);
        assert_eq!(config.node.sync.tracker.page_size, 50);
        assert_eq!(config.node.sync.tracker.response_timeout_secs, 10);
        assert_eq!(config.node.sync.tracker.update_batch_size, 2);
        assert_eq!(config.node.sync.tracker.update_timeout_secs, 10);
        assert_eq!(config.node.network.node_type, NodeType::Bootstrap);
        assert!(config.node.network.listen_addresses.is_empty());
        assert!(config.node.network.external_addresses.is_empty());
        assert!(config.node.network.boot_nodes.is_empty());
        assert_eq!(
            config.node.network.control_list.get_interval_request(),
            Duration::from_secs(60)
        );
        assert_eq!(
            config.node.network.control_list.get_request_timeout(),
            Duration::from_secs(5)
        );
        assert_eq!(
            config
                .node
                .network
                .control_list
                .get_max_concurrent_requests(),
            8
        );
        assert_eq!(config.node.network.max_app_message_bytes, 1024 * 1024);
        assert_eq!(
            config.node.network.max_pending_outbound_bytes_per_peer,
            8 * 1024 * 1024
        );
        assert_eq!(
            config.node.network.max_pending_inbound_bytes_per_peer,
            8 * 1024 * 1024
        );
        assert_eq!(config.node.network.max_pending_outbound_bytes_total, 0);
        assert_eq!(config.node.network.max_pending_inbound_bytes_total, 0);
        assert!(config.node.spec.is_none());

        // node defaults
        assert!(!config.node.always_accept);
        assert!(!config.node.internal_db.durability);
        assert!(!config.node.external_db.durability);
        assert_eq!(
            config.node.network.memory_limits,
            MemoryLimitsConfig::Disabled
        );

        // auth defaults
        assert!(!config.auth.durability);
        assert_eq!(config.auth.api_key.prefix, "ave_node_");

        // http.cors defaults
        assert!(config.http.cors.enabled);
        assert!(config.http.cors.allow_any_origin);
        assert!(config.http.cors.allowed_origins.is_empty());
        assert!(!config.http.cors.allow_credentials);

        // http.proxy defaults
        assert!(config.http.proxy.trusted_proxies.is_empty());
        assert!(config.http.proxy.trust_x_forwarded_for);
        assert!(config.http.proxy.trust_x_real_ip);

        // http.self_signed_cert defaults
        assert!(!config.http.self_signed_cert.enabled);
        assert_eq!(config.http.self_signed_cert.common_name, "localhost");
        assert_eq!(
            config.http.self_signed_cert.san,
            vec!["127.0.0.1".to_owned(), "::1".to_owned()]
        );
        assert_eq!(config.http.self_signed_cert.validity_days, 365);
        assert_eq!(config.http.self_signed_cert.renew_before_days, 30);
        assert_eq!(config.http.self_signed_cert.check_interval_secs, 3600);
    }

    #[test]
    fn build_config_rejects_invalid_network_memory_limits() {
        const INVALID_TOML: &str = r#"
        [node.network.memory_limits]
        type = "percentage"
        value = 2.0
        "#;

        let path = write_config("toml", INVALID_TOML);
        let err =
            build_config(path.to_str().unwrap()).expect_err("invalid config");

        match err {
            BridgeError::ConfigBuild(msg) => {
                assert!(msg.contains("network.memory_limits percentage"));
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn build_config_rejects_invalid_network_message_limits() {
        const INVALID_TOML: &str = r#"
        [node.network]
        max_app_message_bytes = 0
        "#;

        let path = write_config("toml", INVALID_TOML);
        let err =
            build_config(path.to_str().unwrap()).expect_err("invalid config");

        match err {
            BridgeError::ConfigBuild(msg) => {
                assert!(msg.contains("max_app_message_bytes"));
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn build_config_rejects_invalid_control_list_timeout() {
        const INVALID_TOML: &str = r#"
        [node.network.control_list]
        interval_request = 30
        request_timeout = 40
        "#;

        let path = write_config("toml", INVALID_TOML);
        let err =
            build_config(path.to_str().unwrap()).expect_err("invalid config");

        match err {
            BridgeError::ConfigBuild(msg) => {
                assert!(msg.contains("request_timeout"));
            }
            other => panic!("unexpected error: {other:?}"),
        }
    }

    #[test]
    fn build_config_allows_zero_control_list_max_concurrency() {
        const ZERO_TOML: &str = r#"
        [node.network.control_list]
        max_concurrent_requests = 0
        "#;

        let path = write_config("toml", ZERO_TOML);
        let config = build_config(path.to_str().unwrap())
            .expect("zero max_concurrent_requests should be accepted");
        assert_eq!(
            config
                .node
                .network
                .control_list
                .get_max_concurrent_requests(),
            0
        );
    }

    #[test]
    fn build_config_allows_zero_pending_queue_limits() {
        const ZERO_LIMITS_TOML: &str = r#"
        [node.network]
        max_pending_outbound_bytes_per_peer = 0
        max_pending_inbound_bytes_per_peer = 0
        max_pending_outbound_bytes_total = 0
        max_pending_inbound_bytes_total = 0
        "#;

        let path = write_config("toml", ZERO_LIMITS_TOML);
        let config = build_config(path.to_str().unwrap())
            .expect("zero queue limits should be accepted");

        assert_eq!(config.node.network.max_pending_outbound_bytes_per_peer, 0);
        assert_eq!(config.node.network.max_pending_inbound_bytes_per_peer, 0);
        assert_eq!(config.node.network.max_pending_outbound_bytes_total, 0);
        assert_eq!(config.node.network.max_pending_inbound_bytes_total, 0);
    }
}