mqttv5-cli 0.27.1

Superior CLI tool for MQTT v5.0 - unified client and broker commands with ergonomic input design
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
#![allow(clippy::doc_markdown)]
#![allow(clippy::struct_excessive_bools)]

use anyhow::{Context, Result};
use clap::{ArgAction, Args, Subcommand};
use dialoguer::Confirm;
use mqtt5::broker::{BrokerConfig, MqttBroker};
use std::path::{Path, PathBuf};
use tokio::signal;
use tracing::{debug, info};

use super::parsers::{parse_delivery_strategy, parse_duration_secs};

fn parse_storage_backend(
    s: &str,
) -> std::result::Result<mqtt5::broker::config::StorageBackend, String> {
    match s.to_lowercase().as_str() {
        "file" => Ok(mqtt5::broker::config::StorageBackend::File),
        "memory" => Ok(mqtt5::broker::config::StorageBackend::Memory),
        _ => Err(format!(
            "unknown storage backend: {s} (expected 'file' or 'memory')"
        )),
    }
}

#[derive(Args)]
pub struct BrokerCommand {
    #[command(subcommand)]
    pub command: Option<BrokerSubcommand>,

    #[command(flatten)]
    pub run_args: RunArgs,
}

#[derive(Subcommand)]
pub enum BrokerSubcommand {
    /// Generate a full configuration file with all options
    GenerateConfig(GenerateConfigArgs),
}

#[derive(Args)]
pub struct GenerateConfigArgs {
    /// Output file path (default: stdout)
    #[arg(long, short)]
    pub output: Option<PathBuf>,

    /// Output format (json or toml)
    #[arg(long, short, default_value = "json")]
    pub format: String,
}

#[derive(Args)]
pub struct RunArgs {
    /// Configuration file path (JSON format)
    #[arg(long, short, env = "MQTT5_CONFIG")]
    pub config: Option<PathBuf>,

    /// TCP bind address (e.g., `0.0.0.0:1883` `[::]:1883`) - can be specified multiple times
    #[arg(long, short = 'H', action = ArgAction::Append, env = "MQTT5_BIND", value_delimiter = ',')]
    pub host: Vec<String>,

    /// Maximum number of concurrent clients
    #[arg(long, default_value = "10000", env = "MQTT5_MAX_CLIENTS")]
    pub max_clients: usize,

    /// Allow anonymous access (no authentication required)
    #[arg(long, num_args = 0..=1, default_missing_value = "true", env = "MQTT5_ALLOW_ANONYMOUS")]
    pub allow_anonymous: Option<bool>,

    /// Password file path (format: username:password per line)
    #[arg(long, env = "MQTT5_AUTH_PASSWORD_FILE")]
    pub auth_password_file: Option<PathBuf>,

    /// ACL file path (format: `user <username> topic <pattern> permission <type>` per line)
    #[arg(long, env = "MQTT5_ACL_FILE")]
    pub acl_file: Option<PathBuf>,

    /// Authentication method: password, scram, jwt, jwt-federated (default: password if auth file provided)
    #[arg(long, value_parser = ["password", "scram", "jwt", "jwt-federated"], env = "MQTT5_AUTH_METHOD")]
    pub auth_method: Option<String>,

    /// SCRAM credentials file path (format: username:salt:iterations:stored_key:server_key)
    #[arg(long, env = "MQTT5_SCRAM_FILE")]
    pub scram_file: Option<PathBuf>,

    /// JWT algorithm: hs256, rs256, es256
    #[arg(long, value_parser = ["hs256", "rs256", "es256"], env = "MQTT5_JWT_ALGORITHM")]
    pub jwt_algorithm: Option<String>,

    /// JWT secret file (for HS256) or public key file (for RS256/ES256)
    #[arg(long, env = "MQTT5_JWT_KEY_FILE")]
    pub jwt_key_file: Option<PathBuf>,

    /// JWT required issuer
    #[arg(long, env = "MQTT5_JWT_ISSUER")]
    pub jwt_issuer: Option<String>,

    /// JWT required audience
    #[arg(long, env = "MQTT5_JWT_AUDIENCE")]
    pub jwt_audience: Option<String>,

    /// JWT clock skew tolerance (e.g., 60, 60s, 1m)
    #[arg(long, default_value = "60", value_parser = parse_duration_secs, env = "MQTT5_JWT_CLOCK_SKEW")]
    pub jwt_clock_skew: u64,

    /// JWKS endpoint URL for federated JWT auth (e.g., <https://accounts.google.com/.well-known/jwks>)
    #[arg(long, env = "MQTT5_JWT_JWKS_URI")]
    pub jwt_jwks_uri: Option<String>,

    /// Fallback key file when JWKS is unavailable (required with --jwt-jwks-uri)
    #[arg(long, env = "MQTT5_JWT_FALLBACK_KEY")]
    pub jwt_fallback_key: Option<PathBuf>,

    /// JWKS refresh interval (e.g., 3600, 1h, 30m)
    #[arg(long, default_value = "3600", value_parser = parse_duration_secs, env = "MQTT5_JWT_JWKS_REFRESH")]
    pub jwt_jwks_refresh: u64,

    /// Claim path for extracting roles (e.g., "roles", "groups", "realm_access.roles")
    #[arg(long, env = "MQTT5_JWT_ROLE_CLAIM")]
    pub jwt_role_claim: Option<String>,

    /// Role mapping in format "claim_value:mqtt_role" (can be specified multiple times)
    #[arg(long, action = ArgAction::Append, env = "MQTT5_JWT_ROLE_MAP", value_delimiter = ',')]
    pub jwt_role_map: Vec<String>,

    /// Default roles for authenticated JWT users (comma-separated)
    #[arg(long, env = "MQTT5_JWT_DEFAULT_ROLES")]
    pub jwt_default_roles: Option<String>,

    /// Role merge mode: merge (combine with static ACL) or replace (JWT roles only) [DEPRECATED: use --jwt-auth-mode]
    #[arg(long, value_parser = ["merge", "replace"], default_value = "merge", env = "MQTT5_JWT_ROLE_MERGE_MODE")]
    pub jwt_role_merge_mode: String,

    /// Federated authentication mode: identity-only (external IdP for identity, internal ACL), claim-binding (admin-defined mappings), trusted-roles (trust JWT role claims)
    #[arg(long, value_parser = ["identity-only", "claim-binding", "trusted-roles"], env = "MQTT5_JWT_AUTH_MODE")]
    pub jwt_auth_mode: Option<String>,

    /// Claim paths for extracting trusted roles (can be specified multiple times, e.g., "roles", "groups", "realm_access.roles")
    #[arg(long, action = ArgAction::Append, env = "MQTT5_JWT_TRUSTED_ROLE_CLAIM", value_delimiter = ',')]
    pub jwt_trusted_role_claim: Vec<String>,

    /// Whether JWT-derived roles are session-scoped (cleared on disconnect)
    #[arg(long, num_args = 0..=1, default_missing_value = "true", env = "MQTT5_JWT_SESSION_SCOPED_ROLES")]
    pub jwt_session_scoped_roles: Option<bool>,

    /// Custom issuer prefix for user ID namespacing (default: issuer domain)
    #[arg(long, env = "MQTT5_JWT_ISSUER_PREFIX")]
    pub jwt_issuer_prefix: Option<String>,

    /// Federated JWT config file (JSON) for multi-issuer setup
    #[arg(long, env = "MQTT5_JWT_CONFIG_FILE")]
    pub jwt_config_file: Option<PathBuf>,

    /// TLS certificate file path (PEM format)
    #[arg(long, env = "MQTT5_TLS_CERT")]
    pub tls_cert: Option<PathBuf>,

    /// TLS private key file path (PEM format)
    #[arg(long, env = "MQTT5_TLS_KEY")]
    pub tls_key: Option<PathBuf>,

    /// TLS CA certificate file for client verification (PEM format, enables mTLS)
    #[arg(long, env = "MQTT5_TLS_CA_CERT")]
    pub tls_ca_cert: Option<PathBuf>,

    /// Require client certificates for TLS connections (mutual TLS)
    #[arg(long, env = "MQTT5_TLS_REQUIRE_CLIENT_CERT")]
    pub tls_require_client_cert: bool,

    /// TLS bind address - can be specified multiple times
    #[arg(long, action = ArgAction::Append, env = "MQTT5_TLS_BIND", value_delimiter = ',')]
    pub tls_host: Vec<String>,

    /// WebSocket bind address - can be specified multiple times
    #[arg(long, action = ArgAction::Append, env = "MQTT5_WS_BIND", value_delimiter = ',')]
    pub ws_host: Vec<String>,

    /// WebSocket TLS bind address - can be specified multiple times
    #[arg(long, action = ArgAction::Append, env = "MQTT5_WS_TLS_BIND", value_delimiter = ',')]
    pub ws_tls_host: Vec<String>,

    /// WebSocket path (e.g., /mqtt)
    #[arg(long, default_value = "/mqtt", env = "MQTT5_WS_PATH")]
    pub ws_path: String,

    /// QUIC bind address - can be specified multiple times (requires --tls-cert and --tls-key)
    #[arg(long, action = ArgAction::Append, env = "MQTT5_QUIC_BIND", value_delimiter = ',')]
    pub quic_host: Vec<String>,

    /// QUIC server delivery strategy: control-only, per-topic (default), per-publish
    #[arg(long, value_parser = parse_delivery_strategy, env = "MQTT5_QUIC_DELIVERY_STRATEGY")]
    pub quic_delivery_strategy: Option<mqtt5::broker::config::ServerDeliveryStrategy>,

    /// Enable QUIC 0-RTT early data acceptance
    #[arg(long, env = "MQTT5_QUIC_EARLY_DATA")]
    pub quic_early_data: bool,

    /// Storage directory for persistent data
    #[arg(long, default_value = "./mqtt_storage", env = "MQTT5_STORAGE_DIR")]
    pub storage_dir: PathBuf,

    /// Storage backend type: file or memory
    #[arg(long, default_value = "file", value_parser = parse_storage_backend, env = "MQTT5_STORAGE_BACKEND")]
    pub storage_backend: mqtt5::broker::config::StorageBackend,

    /// Disable message persistence
    #[arg(long, env = "MQTT5_NO_PERSISTENCE")]
    pub no_persistence: bool,

    /// Session expiry interval (e.g., 3600, 1h, 30m)
    #[arg(long, default_value = "3600", value_parser = parse_duration_secs, env = "MQTT5_SESSION_EXPIRY")]
    pub session_expiry: u64,

    /// Maximum `QoS` level supported (0, 1, or 2)
    #[arg(long, default_value = "2", env = "MQTT5_MAX_QOS")]
    pub max_qos: u8,

    /// Server keep-alive time (e.g., 60, 60s, 2m)
    #[arg(long, value_parser = parse_duration_secs, env = "MQTT5_KEEP_ALIVE")]
    pub keep_alive: Option<u64>,

    /// Response information string sent to clients that request it
    #[arg(long, env = "MQTT5_RESPONSE_INFORMATION")]
    pub response_information: Option<String>,

    /// Disable retained messages
    #[arg(long, env = "MQTT5_NO_RETAIN")]
    pub no_retain: bool,

    /// Disable wildcard subscriptions
    #[arg(long, env = "MQTT5_NO_WILDCARDS")]
    pub no_wildcards: bool,

    /// Skip prompts and use defaults
    #[arg(long, env = "MQTT5_NON_INTERACTIVE")]
    pub non_interactive: bool,

    /// OpenTelemetry OTLP endpoint (e.g., http://localhost:4317)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, env = "MQTT5_OTEL_ENDPOINT")]
    pub otel_endpoint: Option<String>,

    /// OpenTelemetry service name (default: mqttv5-broker)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, default_value = "mqttv5-broker", env = "MQTT5_OTEL_SERVICE_NAME")]
    pub otel_service_name: String,

    /// OpenTelemetry sampling ratio (0.0-1.0, default: 1.0)
    #[cfg(feature = "opentelemetry")]
    #[arg(long, default_value = "1.0", env = "MQTT5_OTEL_SAMPLING")]
    pub otel_sampling: f64,
}

pub async fn execute(cmd: BrokerCommand, verbose: bool, debug: bool) -> Result<()> {
    if let Some(BrokerSubcommand::GenerateConfig(args)) = cmd.command {
        return execute_generate_config(args).await;
    }

    execute_run(cmd.run_args, verbose, debug).await
}

fn build_example_config() -> BrokerConfig {
    use mqtt5::broker::config::{
        AuthConfig, AuthMethod, ChangeOnlyDeliveryConfig, QuicConfig, RateLimitConfig,
        StorageBackend, StorageConfig, TlsConfig, WebSocketConfig,
    };

    BrokerConfig {
        bind_addresses: vec![
            "0.0.0.0:1883".parse().unwrap(),
            "[::]:1883".parse().unwrap(),
        ],
        max_clients: 10000,
        session_expiry_interval: std::time::Duration::from_secs(3600),
        max_packet_size: 268_435_456,
        topic_alias_maximum: 65535,
        retain_available: true,
        maximum_qos: 2,
        wildcard_subscription_available: true,
        subscription_identifier_available: true,
        shared_subscription_available: true,
        max_subscriptions_per_client: 0,
        max_retained_messages: 0,
        max_retained_message_size: 0,
        client_channel_capacity: 10000,
        server_keep_alive: None,
        server_receive_maximum: None,
        response_information: None,
        auth_config: AuthConfig {
            allow_anonymous: true,
            password_file: None,
            acl_file: None,
            auth_method: AuthMethod::None,
            auth_data: None,
            scram_file: None,
            jwt_config: None,
            federated_jwt_config: None,
            rate_limit: RateLimitConfig::default(),
        },
        tls_config: Some(TlsConfig {
            cert_file: PathBuf::from("/path/to/server.crt"),
            key_file: PathBuf::from("/path/to/server.key"),
            ca_file: None,
            require_client_cert: false,
            bind_addresses: vec![
                "0.0.0.0:8883".parse().unwrap(),
                "[::]:8883".parse().unwrap(),
            ],
        }),
        websocket_config: Some(WebSocketConfig {
            bind_addresses: vec![
                "0.0.0.0:8080".parse().unwrap(),
                "[::]:8080".parse().unwrap(),
            ],
            path: "/mqtt".to_string(),
            subprotocol: "mqtt".to_string(),
            use_tls: false,
        }),
        websocket_tls_config: Some(WebSocketConfig {
            bind_addresses: vec![
                "0.0.0.0:8443".parse().unwrap(),
                "[::]:8443".parse().unwrap(),
            ],
            path: "/mqtt".to_string(),
            subprotocol: "mqtt".to_string(),
            use_tls: true,
        }),
        quic_config: Some(QuicConfig {
            cert_file: PathBuf::from("/path/to/server.crt"),
            key_file: PathBuf::from("/path/to/server.key"),
            ca_file: None,
            require_client_cert: false,
            bind_addresses: vec![
                "0.0.0.0:14567".parse().unwrap(),
                "[::]:14567".parse().unwrap(),
            ],
            enable_early_data: false,
        }),
        cluster_listener_config: None,
        storage_config: StorageConfig {
            backend: StorageBackend::File,
            base_dir: PathBuf::from("./mqtt_storage"),
            cleanup_interval: std::time::Duration::from_secs(3600),
            enable_persistence: true,
        },
        change_only_delivery_config: ChangeOnlyDeliveryConfig::default(),
        echo_suppression_config: mqtt5::broker::config::EchoSuppressionConfig::default(),
        max_outbound_rate_per_client: 0,
        server_delivery_strategy: mqtt5::broker::config::ServerDeliveryStrategy::default(),
        load_balancer: None,
        bridges: vec![],
        #[cfg(feature = "opentelemetry")]
        opentelemetry_config: None,
        event_handler: None,
    }
}

async fn execute_generate_config(args: GenerateConfigArgs) -> Result<()> {
    let config = build_example_config();

    let output = match args.format.to_lowercase().as_str() {
        "json" => {
            serde_json::to_string_pretty(&config).context("Failed to serialize config to JSON")?
        }
        "toml" => toml::to_string_pretty(&config).context("Failed to serialize config to TOML")?,
        _ => anyhow::bail!("Unsupported format: {}. Use 'json' or 'toml'", args.format),
    };

    if let Some(path) = args.output {
        tokio::fs::write(&path, &output)
            .await
            .with_context(|| format!("Failed to write config to {}", path.display()))?;
        eprintln!("Configuration written to: {}", path.display());
    } else {
        println!("{output}");
    }

    Ok(())
}

fn print_startup_info(config: &BrokerConfig, max_clients: usize, config_path: Option<&PathBuf>) {
    println!("🚀 MQTT v5.0 broker starting...");
    println!(
        "  📡 TCP: {}",
        config
            .bind_addresses
            .iter()
            .map(ToString::to_string)
            .collect::<Vec<_>>()
            .join(", ")
    );
    if let Some(ref tls_cfg) = config.tls_config {
        println!(
            "  🔒 TLS: {}",
            tls_cfg
                .bind_addresses
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", ")
        );
    }
    if let Some(ref ws_cfg) = config.websocket_config {
        println!(
            "  🌐 WebSocket: {} (path: {})",
            ws_cfg
                .bind_addresses
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", "),
            ws_cfg.path
        );
    }
    if let Some(ref ws_tls_cfg) = config.websocket_tls_config {
        println!(
            "  🔐 WebSocket TLS: {} (path: {})",
            ws_tls_cfg
                .bind_addresses
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", "),
            ws_tls_cfg.path
        );
    }
    if let Some(ref quic_cfg) = config.quic_config {
        println!(
            "  🚀 QUIC: {}{}",
            quic_cfg
                .bind_addresses
                .iter()
                .map(ToString::to_string)
                .collect::<Vec<_>>()
                .join(", "),
            if quic_cfg.enable_early_data {
                " (0-RTT enabled)"
            } else {
                ""
            }
        );
    }
    println!("  👥 Max clients: {max_clients}");
    #[cfg(feature = "opentelemetry")]
    if let Some(ref otel_config) = config.opentelemetry_config {
        println!(
            "  📊 OpenTelemetry: {} (service: {})",
            otel_config.otlp_endpoint, otel_config.service_name
        );
    }
    if config_path.is_some() {
        println!("  🔄 Hot-reload: enabled (edit config file or send SIGHUP to reload)");
    }
    println!("  📝 Press Ctrl+C to stop");
}

async fn execute_run(mut cmd: RunArgs, verbose: bool, debug: bool) -> Result<()> {
    #[cfg(feature = "opentelemetry")]
    let has_otel = cmd.otel_endpoint.is_some();

    #[cfg(not(feature = "opentelemetry"))]
    let has_otel = false;

    if !has_otel {
        crate::init_basic_tracing(verbose, debug);
    }

    info!("Starting MQTT v5.0 broker...");

    let (config, config_path) = if let Some(config_path) = &cmd.config {
        debug!("Loading configuration from: {:?}", config_path);
        let cfg = load_config_from_file(config_path)
            .await
            .with_context(|| format!("Failed to load config from {}", config_path.display()))?;
        (cfg, Some(config_path.clone()))
    } else {
        (create_interactive_config(&mut cmd).await?, None)
    };

    config
        .validate()
        .context("Configuration validation failed")?;

    info!(
        "Creating broker with bind addresses: {:?}",
        config.bind_addresses
    );
    let mut broker = if let Some(ref path) = config_path {
        MqttBroker::with_config_file(config.clone(), path.clone())
            .await
            .context("Failed to create MQTT broker with hot-reload")?
    } else {
        MqttBroker::with_config(config.clone())
            .await
            .context("Failed to create MQTT broker")?
    };

    print_startup_info(&config, cmd.max_clients, config_path.as_ref());

    let reload_sender = broker.manual_reload_sender();

    #[cfg(unix)]
    let mut sighup_task = None;
    #[cfg(unix)]
    if let Some(sender) = reload_sender {
        let mut sighup_stream = signal::unix::signal(signal::unix::SignalKind::hangup())
            .context("Failed to register SIGHUP handler")?;
        sighup_task = Some(tokio::spawn(async move {
            loop {
                sighup_stream.recv().await;
                info!("Received SIGHUP, triggering config reload");
                if sender.send(()).await.is_err() {
                    break;
                }
            }
        }));
    }

    let shutdown_signal = async {
        match signal::ctrl_c().await {
            Ok(()) => {
                println!("\n🛑 Received Ctrl+C, shutting down gracefully...");
            }
            Err(err) => {
                tracing::error!("Unable to listen for shutdown signal: {err}");
            }
        }
    };

    tokio::select! {
        result = broker.run() => {
            match result {
                Ok(()) => {
                    info!("Broker stopped normally");
                }
                Err(e) => {
                    anyhow::bail!("Broker error: {e}");
                }
            }
        }
        () = shutdown_signal => {
            info!("Shutdown signal received, stopping broker...");
        }
    }

    #[cfg(unix)]
    if let Some(handle) = sighup_task {
        handle.abort();
    }

    println!("✓ MQTT broker stopped");
    Ok(())
}

fn resolve_auth_settings(cmd: &RunArgs) -> Result<bool> {
    if let Some(allow_anon) = cmd.allow_anonymous {
        return Ok(allow_anon);
    }

    if cmd.auth_password_file.is_some() {
        info!("Password file provided, anonymous access disabled by default");
        return Ok(false);
    }

    if cmd.scram_file.is_some() {
        info!("SCRAM credentials file provided, anonymous access disabled by default");
        return Ok(false);
    }

    if cmd.jwt_key_file.is_some() {
        info!("JWT key file provided, anonymous access disabled by default");
        return Ok(false);
    }

    if cmd.jwt_jwks_uri.is_some() || cmd.jwt_config_file.is_some() {
        info!("Federated JWT auth configured, anonymous access disabled by default");
        return Ok(false);
    }

    if cmd.non_interactive {
        anyhow::bail!(
            "No authentication configured.\n\
             Use one of:\n  \
             --allow-anonymous             Allow connections without credentials\n  \
             --auth-password-file <path>   Require password authentication\n  \
             --scram-file <path>           Require SCRAM-SHA-256 authentication\n  \
             --jwt-key-file <path>         Require JWT authentication\n  \
             --jwt-jwks-uri <url>          Require federated JWT authentication"
        );
    }

    eprintln!("⚠️  No authentication configured.");
    let allow_anon = Confirm::new()
        .with_prompt("Allow anonymous connections? (not recommended for production)")
        .default(false)
        .interact()
        .context("Failed to get authentication choice")?;

    Ok(allow_anon)
}

fn resolve_auth_method(cmd: &RunArgs) -> Result<mqtt5::broker::config::AuthMethod> {
    use mqtt5::broker::config::AuthMethod;

    match cmd.auth_method.as_deref() {
        Some("scram") => {
            let Some(scram_file) = &cmd.scram_file else {
                anyhow::bail!("--scram-file is required when using --auth-method scram");
            };
            if !scram_file.exists() {
                anyhow::bail!("SCRAM credentials file not found: {}", scram_file.display());
            }
            Ok(AuthMethod::ScramSha256)
        }
        Some("jwt") => {
            let Some(jwt_key_file) = &cmd.jwt_key_file else {
                anyhow::bail!("--jwt-key-file is required when using --auth-method jwt");
            };
            if !jwt_key_file.exists() {
                anyhow::bail!("JWT key file not found: {}", jwt_key_file.display());
            }
            if cmd.jwt_algorithm.is_none() {
                anyhow::bail!("--jwt-algorithm is required when using --auth-method jwt");
            }
            Ok(AuthMethod::Jwt)
        }
        Some("jwt-federated") => {
            if cmd.jwt_jwks_uri.is_none() && cmd.jwt_config_file.is_none() {
                anyhow::bail!(
                    "--jwt-jwks-uri or --jwt-config-file is required when using --auth-method jwt-federated"
                );
            }
            if cmd.jwt_jwks_uri.is_some() {
                let Some(fallback) = &cmd.jwt_fallback_key else {
                    anyhow::bail!("--jwt-fallback-key is required when using --jwt-jwks-uri");
                };
                if !fallback.exists() {
                    anyhow::bail!("JWT fallback key file not found: {}", fallback.display());
                }
                if cmd.jwt_issuer.is_none() {
                    anyhow::bail!("--jwt-issuer is required when using --jwt-jwks-uri");
                }
            }
            if let Some(config_file) = &cmd.jwt_config_file {
                if !config_file.exists() {
                    anyhow::bail!("JWT config file not found: {}", config_file.display());
                }
            }
            Ok(AuthMethod::JwtFederated)
        }
        Some("password") | None => {
            if cmd.auth_password_file.is_some() {
                Ok(AuthMethod::Password)
            } else {
                Ok(AuthMethod::None)
            }
        }
        Some(other) => anyhow::bail!("Unknown auth method: {other}"),
    }
}

fn build_jwt_config(cmd: &RunArgs) -> Result<Option<mqtt5::broker::config::JwtConfig>> {
    use mqtt5::broker::config::{JwtAlgorithm, JwtConfig};

    let algorithm = match cmd.jwt_algorithm.as_deref() {
        Some("hs256") => JwtAlgorithm::HS256,
        Some("rs256") => JwtAlgorithm::RS256,
        Some("es256") => JwtAlgorithm::ES256,
        _ => anyhow::bail!("Invalid JWT algorithm"),
    };
    let mut jwt_cfg = JwtConfig::new(algorithm, cmd.jwt_key_file.clone().unwrap());
    jwt_cfg.clock_skew_secs = cmd.jwt_clock_skew;
    if let Some(ref issuer) = cmd.jwt_issuer {
        jwt_cfg.issuer = Some(issuer.clone());
    }
    if let Some(ref audience) = cmd.jwt_audience {
        jwt_cfg.audience = Some(audience.clone());
    }
    Ok(Some(jwt_cfg))
}

async fn build_federated_jwt_config(
    cmd: &RunArgs,
) -> Result<Option<mqtt5::broker::config::FederatedJwtConfig>> {
    use mqtt5::broker::config::{
        ClaimPattern, FederatedAuthMode, FederatedJwtConfig, JwtIssuerConfig, JwtKeySource,
        JwtRoleMapping, RoleMergeMode,
    };

    if let Some(config_file) = &cmd.jwt_config_file {
        let content = tokio::fs::read_to_string(config_file)
            .await
            .with_context(|| {
                format!("Failed to read JWT config file: {}", config_file.display())
            })?;
        let config: FederatedJwtConfig = serde_json::from_str(&content)
            .with_context(|| "Failed to parse JWT config file as JSON")?;
        return Ok(Some(config));
    }

    let auth_mode = match cmd.jwt_auth_mode.as_deref() {
        Some("claim-binding") => FederatedAuthMode::ClaimBinding,
        Some("trusted-roles") => FederatedAuthMode::TrustedRoles,
        None => match cmd.jwt_role_merge_mode.as_str() {
            "replace" => FederatedAuthMode::TrustedRoles,
            _ => FederatedAuthMode::ClaimBinding,
        },
        Some("identity-only" | _) => FederatedAuthMode::IdentityOnly,
    };

    let merge_mode = match cmd.jwt_role_merge_mode.as_str() {
        "replace" => RoleMergeMode::Replace,
        _ => RoleMergeMode::Merge,
    };

    let default_roles: Vec<String> = cmd
        .jwt_default_roles
        .as_ref()
        .map(|s| s.split(',').map(|r| r.trim().to_string()).collect())
        .unwrap_or_default();

    let mut role_mappings = Vec::new();
    if let Some(claim_path) = &cmd.jwt_role_claim {
        for mapping in &cmd.jwt_role_map {
            if let Some((value, role)) = mapping.split_once(':') {
                role_mappings.push(JwtRoleMapping::new(
                    claim_path.clone(),
                    ClaimPattern::Equals(value.to_string()),
                    vec![role.to_string()],
                ));
            }
        }
    }

    let mut issuer_config = JwtIssuerConfig::new(
        "cli-issuer",
        cmd.jwt_issuer.clone().unwrap(),
        JwtKeySource::Jwks {
            uri: cmd.jwt_jwks_uri.clone().unwrap(),
            fallback_key_file: cmd.jwt_fallback_key.clone().unwrap(),
            refresh_interval_secs: cmd.jwt_jwks_refresh,
            cache_ttl_secs: cmd.jwt_jwks_refresh * 24,
        },
    )
    .with_auth_mode(auth_mode)
    .with_default_roles(default_roles);

    #[allow(deprecated)]
    {
        issuer_config.role_merge_mode = merge_mode;
    }

    if let Some(ref audience) = cmd.jwt_audience {
        issuer_config = issuer_config.with_audience(audience.clone());
    }

    if !cmd.jwt_trusted_role_claim.is_empty() {
        issuer_config
            .trusted_role_claims
            .clone_from(&cmd.jwt_trusted_role_claim);
    }

    if let Some(session_scoped) = cmd.jwt_session_scoped_roles {
        issuer_config.session_scoped_roles = session_scoped;
    }

    if let Some(ref prefix) = cmd.jwt_issuer_prefix {
        issuer_config.issuer_prefix = Some(prefix.clone());
    }

    issuer_config.role_mappings = role_mappings;

    Ok(Some(FederatedJwtConfig {
        issuers: vec![issuer_config],
        clock_skew_secs: cmd.jwt_clock_skew,
    }))
}

fn log_auth_summary(
    auth_method: mqtt5::broker::config::AuthMethod,
    allow_anonymous: bool,
    cmd: &RunArgs,
) {
    use mqtt5::broker::config::AuthMethod;

    match auth_method {
        AuthMethod::None if allow_anonymous => {
            info!("Anonymous access enabled - clients can connect without credentials");
        }
        AuthMethod::Password => {
            info!(
                "Password authentication enabled (file: {:?})",
                cmd.auth_password_file.as_ref().unwrap()
            );
        }
        AuthMethod::ScramSha256 => {
            info!(
                "SCRAM-SHA-256 authentication enabled (file: {:?})",
                cmd.scram_file.as_ref().unwrap()
            );
        }
        AuthMethod::Jwt => {
            info!(
                "JWT authentication enabled (algorithm: {:?}, key: {:?})",
                cmd.jwt_algorithm.as_ref().unwrap(),
                cmd.jwt_key_file.as_ref().unwrap()
            );
        }
        AuthMethod::JwtFederated => {
            if let Some(config_file) = &cmd.jwt_config_file {
                info!(
                    "Federated JWT authentication enabled (config: {:?})",
                    config_file
                );
            } else {
                info!(
                    "Federated JWT authentication enabled (issuer: {:?}, jwks: {:?})",
                    cmd.jwt_issuer.as_ref().unwrap(),
                    cmd.jwt_jwks_uri.as_ref().unwrap()
                );
            }
        }
        AuthMethod::None => {}
    }

    if let Some(acl_file) = &cmd.acl_file {
        info!("ACL authorization enabled (file: {:?})", acl_file);
    }
}

fn configure_tls(config: &mut BrokerConfig, cmd: &RunArgs) -> Result<()> {
    use mqtt5::broker::config::TlsConfig;

    if let (Some(cert), Some(key)) = (&cmd.tls_cert, &cmd.tls_key) {
        if !cert.exists() {
            anyhow::bail!("TLS certificate file not found: {}", cert.display());
        }
        if !key.exists() {
            anyhow::bail!("TLS key file not found: {}", key.display());
        }

        let tls_addrs: Result<Vec<std::net::SocketAddr>> = if cmd.tls_host.is_empty() {
            Ok(vec![
                "0.0.0.0:8883".parse().unwrap(),
                "[::]:8883".parse().unwrap(),
            ])
        } else {
            cmd.tls_host
                .iter()
                .map(|h| {
                    h.parse()
                        .with_context(|| format!("Invalid TLS bind address: {h}"))
                })
                .collect()
        };

        let mut tls_config =
            TlsConfig::new(cert.clone(), key.clone()).with_bind_addresses(tls_addrs?);

        if let Some(ca_cert) = &cmd.tls_ca_cert {
            if !ca_cert.exists() {
                anyhow::bail!("TLS CA certificate file not found: {}", ca_cert.display());
            }
            tls_config = tls_config
                .with_ca_file(ca_cert.clone())
                .with_require_client_cert(cmd.tls_require_client_cert);
            info!("TLS enabled with mTLS (client certificate verification)");
        } else if cmd.tls_require_client_cert {
            anyhow::bail!("--tls-ca-cert is required when --tls-require-client-cert is set");
        } else {
            info!("TLS enabled");
        }

        config.tls_config = Some(tls_config);
    } else if cmd.tls_cert.is_some() || cmd.tls_key.is_some() {
        anyhow::bail!("Both --tls-cert and --tls-key must be provided together");
    } else if cmd.tls_ca_cert.is_some() || cmd.tls_require_client_cert {
        anyhow::bail!("--tls-cert and --tls-key must be provided to use --tls-ca-cert or --tls-require-client-cert");
    }

    Ok(())
}

fn configure_websocket(config: &mut BrokerConfig, cmd: &RunArgs) -> Result<()> {
    use mqtt5::broker::config::WebSocketConfig;

    if !cmd.ws_host.is_empty() {
        let ws_addrs: Result<Vec<std::net::SocketAddr>> = cmd
            .ws_host
            .iter()
            .map(|h| {
                h.parse()
                    .with_context(|| format!("Invalid WebSocket bind address: {h}"))
            })
            .collect();

        let ws_config = WebSocketConfig::default()
            .with_bind_addresses(ws_addrs?)
            .with_path(cmd.ws_path.clone());
        config.websocket_config = Some(ws_config);
        info!("WebSocket enabled");
    }

    if !cmd.ws_tls_host.is_empty() {
        if let (Some(cert), Some(key)) = (&cmd.tls_cert, &cmd.tls_key) {
            if !cert.exists() {
                anyhow::bail!("TLS certificate file not found: {}", cert.display());
            }
            if !key.exists() {
                anyhow::bail!("TLS key file not found: {}", key.display());
            }

            let ws_tls_addrs: Result<Vec<std::net::SocketAddr>> = cmd
                .ws_tls_host
                .iter()
                .map(|h| {
                    h.parse()
                        .with_context(|| format!("Invalid WebSocket TLS bind address: {h}"))
                })
                .collect();

            let ws_tls_config = WebSocketConfig::default()
                .with_bind_addresses(ws_tls_addrs?)
                .with_path(cmd.ws_path.clone())
                .with_tls(true);
            config.websocket_tls_config = Some(ws_tls_config);
            info!("WebSocket TLS enabled");
        } else {
            anyhow::bail!(
                "Both --tls-cert and --tls-key must be provided when using --ws-tls-host"
            );
        }
    }

    Ok(())
}

fn configure_quic(config: &mut BrokerConfig, cmd: &RunArgs) -> Result<()> {
    use mqtt5::broker::config::QuicConfig;

    if cmd.quic_host.is_empty() {
        return Ok(());
    }

    if let (Some(cert), Some(key)) = (&cmd.tls_cert, &cmd.tls_key) {
        if !cert.exists() {
            anyhow::bail!("TLS certificate file not found: {}", cert.display());
        }
        if !key.exists() {
            anyhow::bail!("TLS key file not found: {}", key.display());
        }

        let quic_addrs: Result<Vec<std::net::SocketAddr>> = cmd
            .quic_host
            .iter()
            .map(|h| {
                h.parse()
                    .with_context(|| format!("Invalid QUIC bind address: {h}"))
            })
            .collect();

        let mut quic_config =
            QuicConfig::new(cert.clone(), key.clone()).with_bind_addresses(quic_addrs?);

        if let Some(ca_cert) = &cmd.tls_ca_cert {
            if !ca_cert.exists() {
                anyhow::bail!("TLS CA certificate file not found: {}", ca_cert.display());
            }
            quic_config = quic_config
                .with_ca_file(ca_cert.clone())
                .with_require_client_cert(cmd.tls_require_client_cert);
        }

        if cmd.quic_early_data {
            quic_config = quic_config.with_early_data(true);
        }
        config.quic_config = Some(quic_config);
        if let Some(strategy) = cmd.quic_delivery_strategy {
            config.server_delivery_strategy = strategy;
        }
        info!("QUIC enabled on {:?}", cmd.quic_host);
    } else {
        anyhow::bail!("Both --tls-cert and --tls-key must be provided when using --quic-host");
    }

    Ok(())
}

async fn create_interactive_config(cmd: &mut RunArgs) -> Result<BrokerConfig> {
    use mqtt5::broker::config::{AuthConfig, AuthMethod, RateLimitConfig, StorageConfig};

    let mut config = BrokerConfig::new();

    let bind_addrs: Result<Vec<std::net::SocketAddr>> = if cmd.host.is_empty() {
        Ok(vec![
            "0.0.0.0:1883".parse().unwrap(),
            "[::]:1883".parse().unwrap(),
        ])
    } else {
        cmd.host
            .iter()
            .map(|h| {
                h.parse()
                    .with_context(|| format!("Invalid bind address: {h}"))
            })
            .collect()
    };
    config = config.with_bind_addresses(bind_addrs?);

    config = config.with_max_clients(cmd.max_clients);
    config.session_expiry_interval = std::time::Duration::from_secs(cmd.session_expiry);
    config.maximum_qos = cmd.max_qos;
    config.retain_available = !cmd.no_retain;
    config.wildcard_subscription_available = !cmd.no_wildcards;

    if let Some(keep_alive) = cmd.keep_alive {
        config.server_keep_alive = Some(std::time::Duration::from_secs(keep_alive));
    }

    if let Some(ref response_info) = cmd.response_information {
        config.response_information = Some(response_info.clone());
    }

    let allow_anonymous = resolve_auth_settings(cmd)?;
    let auth_method = resolve_auth_method(cmd)?;

    if let Some(password_file) = &cmd.auth_password_file {
        if !password_file.exists() {
            anyhow::bail!(
                "Authentication password file not found: {}",
                password_file.display()
            );
        }
    }

    if let Some(acl_file) = &cmd.acl_file {
        if !acl_file.exists() {
            anyhow::bail!("ACL file not found: {}", acl_file.display());
        }
    }

    let jwt_config = if auth_method == AuthMethod::Jwt {
        build_jwt_config(cmd)?
    } else {
        None
    };

    let federated_jwt_config = if auth_method == AuthMethod::JwtFederated {
        build_federated_jwt_config(cmd).await?
    } else {
        None
    };

    let auth_config = AuthConfig {
        allow_anonymous,
        password_file: cmd.auth_password_file.clone(),
        acl_file: cmd.acl_file.clone(),
        auth_method,
        auth_data: None,
        scram_file: cmd.scram_file.clone(),
        jwt_config,
        federated_jwt_config,
        rate_limit: RateLimitConfig::default(),
    };
    config = config.with_auth(auth_config);

    log_auth_summary(auth_method, allow_anonymous, cmd);

    configure_tls(&mut config, cmd)?;
    configure_websocket(&mut config, cmd)?;
    configure_quic(&mut config, cmd)?;

    let storage_config = StorageConfig {
        enable_persistence: !cmd.no_persistence,
        base_dir: cmd.storage_dir.clone(),
        backend: cmd.storage_backend,
        cleanup_interval: std::time::Duration::from_secs(300),
    };
    config.storage_config = storage_config;

    #[cfg(feature = "opentelemetry")]
    if let Some(endpoint) = &cmd.otel_endpoint {
        use mqtt5::telemetry::TelemetryConfig;
        let telemetry_config = TelemetryConfig::new(&cmd.otel_service_name)
            .with_endpoint(endpoint)
            .with_sampling_ratio(cmd.otel_sampling);
        config = config.with_opentelemetry(telemetry_config);
        info!(
            "OpenTelemetry enabled: endpoint={}, service={}, sampling={}",
            endpoint, cmd.otel_service_name, cmd.otel_sampling
        );
    }

    Ok(config)
}

async fn load_config_from_file(config_path: &Path) -> Result<BrokerConfig> {
    let contents = tokio::fs::read_to_string(config_path)
        .await
        .context("Failed to read config file")?;

    serde_json::from_str(&contents).context("Failed to parse config file as JSON")
}