@@ -28,7 +28,7 @@ pub struct ConfigAndUsers {
28
28
impl ConfigAndUsers {
29
29
/// Load configuration from disk or use defaults.
30
30
pub fn load ( config_path : & PathBuf , users_path : & PathBuf ) -> Result < Self , Error > {
31
- let config: Config = if let Ok ( config) = read_to_string ( config_path) {
31
+ let mut config: Config = if let Ok ( config) = read_to_string ( config_path) {
32
32
let config = match toml:: from_str ( & config) {
33
33
Ok ( config) => config,
34
34
Err ( err) => return Err ( Error :: config ( & config, err) ) ,
@@ -43,18 +43,11 @@ impl ConfigAndUsers {
43
43
Config :: default ( )
44
44
} ;
45
45
46
- if config. admin . random ( ) {
47
- #[ cfg( debug_assertions) ]
48
- info ! ( "[debug only] admin password: {}" , config. admin. password) ;
49
- #[ cfg( not( debug_assertions) ) ]
50
- warn ! ( "admin password has been randomly generated" ) ;
51
- }
52
-
53
46
if config. multi_tenant . is_some ( ) {
54
47
info ! ( "multi-tenant protection enabled" ) ;
55
48
}
56
49
57
- let users: Users = if let Ok ( users) = read_to_string ( users_path) {
50
+ let mut users: Users = if let Ok ( users) = read_to_string ( users_path) {
58
51
let mut users: Users = toml:: from_str ( & users) ?;
59
52
users. check ( & config) ;
60
53
info ! ( "loaded \" {}\" " , users_path. display( ) ) ;
@@ -67,6 +60,19 @@ impl ConfigAndUsers {
67
60
Users :: default ( )
68
61
} ;
69
62
63
+ // Override admin set in pgdog.toml
64
+ // with what's in users.toml.
65
+ if let Some ( admin) = users. admin . take ( ) {
66
+ config. admin = admin;
67
+ }
68
+
69
+ if config. admin . random ( ) {
70
+ #[ cfg( debug_assertions) ]
71
+ info ! ( "[debug only] admin password: {}" , config. admin. password) ;
72
+ #[ cfg( not( debug_assertions) ) ]
73
+ warn ! ( "admin password has been randomly generated" ) ;
74
+ }
75
+
70
76
Ok ( ConfigAndUsers {
71
77
config,
72
78
users,
@@ -473,4 +479,83 @@ exposure = 0.75
473
479
. get_mirroring_config( "source_db" , "non_existent" )
474
480
. is_none( ) ) ;
475
481
}
482
+
483
+ #[ test]
484
+ fn test_admin_override_from_users_toml ( ) {
485
+ use std:: io:: Write ;
486
+ use tempfile:: NamedTempFile ;
487
+
488
+ let pgdog_config = r#"
489
+ [admin]
490
+ name = "pgdog_admin"
491
+ user = "pgdog_admin_user"
492
+ password = "pgdog_admin_password"
493
+ "# ;
494
+
495
+ let users_config = r#"
496
+ [admin]
497
+ name = "users_admin"
498
+ user = "users_admin_user"
499
+ password = "users_admin_password"
500
+ "# ;
501
+
502
+ let mut pgdog_file = NamedTempFile :: new ( ) . unwrap ( ) ;
503
+ let mut users_file = NamedTempFile :: new ( ) . unwrap ( ) ;
504
+
505
+ pgdog_file. write_all ( pgdog_config. as_bytes ( ) ) . unwrap ( ) ;
506
+ users_file. write_all ( users_config. as_bytes ( ) ) . unwrap ( ) ;
507
+
508
+ pgdog_file. flush ( ) . unwrap ( ) ;
509
+ users_file. flush ( ) . unwrap ( ) ;
510
+
511
+ let config_and_users =
512
+ ConfigAndUsers :: load ( & pgdog_file. path ( ) . into ( ) , & users_file. path ( ) . into ( ) ) . unwrap ( ) ;
513
+
514
+ assert_eq ! ( config_and_users. config. admin. name, "users_admin" ) ;
515
+ assert_eq ! ( config_and_users. config. admin. user, "users_admin_user" ) ;
516
+ assert_eq ! (
517
+ config_and_users. config. admin. password,
518
+ "users_admin_password"
519
+ ) ;
520
+ assert ! ( config_and_users. users. admin. is_none( ) ) ;
521
+ }
522
+
523
+ #[ test]
524
+ fn test_admin_override_with_default_config ( ) {
525
+ use std:: io:: Write ;
526
+ use tempfile:: NamedTempFile ;
527
+
528
+ let pgdog_config = r#"
529
+ [general]
530
+ host = "0.0.0.0"
531
+ port = 6432
532
+ "# ;
533
+
534
+ let users_config = r#"
535
+ [admin]
536
+ name = "users_admin"
537
+ user = "users_admin_user"
538
+ password = "users_admin_password"
539
+ "# ;
540
+
541
+ let mut pgdog_file = NamedTempFile :: new ( ) . unwrap ( ) ;
542
+ let mut users_file = NamedTempFile :: new ( ) . unwrap ( ) ;
543
+
544
+ pgdog_file. write_all ( pgdog_config. as_bytes ( ) ) . unwrap ( ) ;
545
+ users_file. write_all ( users_config. as_bytes ( ) ) . unwrap ( ) ;
546
+
547
+ pgdog_file. flush ( ) . unwrap ( ) ;
548
+ users_file. flush ( ) . unwrap ( ) ;
549
+
550
+ let config_and_users =
551
+ ConfigAndUsers :: load ( & pgdog_file. path ( ) . into ( ) , & users_file. path ( ) . into ( ) ) . unwrap ( ) ;
552
+
553
+ assert_eq ! ( config_and_users. config. admin. name, "users_admin" ) ;
554
+ assert_eq ! ( config_and_users. config. admin. user, "users_admin_user" ) ;
555
+ assert_eq ! (
556
+ config_and_users. config. admin. password,
557
+ "users_admin_password"
558
+ ) ;
559
+ assert ! ( config_and_users. users. admin. is_none( ) ) ;
560
+ }
476
561
}
0 commit comments