1use std::borrow::Cow;
37
38use crate::common::Error;
39use crate::common::config::abstract_config::ValuesReader;
40use crate::common::config::config_def::{
41 CaseInsensitiveValidString, ConfigDef, DefaultValue, Importance, Range, Type, ValidList,
42};
43use crate::common::config::types::Password;
44
45impl SaslConfigs {
54 pub const SASL_MECHANISM: &str = "sasl.mechanism";
59
60 pub const SASL_JAAS_CONFIG: &str = "sasl.jaas.config";
65
66 pub const GSSAPI_MECHANISM: &str = "GSSAPI";
68
69 pub(crate) const PLAIN_MECHANISM: &str = "PLAIN";
71
72 pub const DEFAULT_SASL_MECHANISM: &str = Self::GSSAPI_MECHANISM;
74}
75
76#[derive(Debug)]
88#[non_exhaustive]
89#[doc(alias = "org.apache.kafka.common.config.SaslConfigs")]
90pub struct SaslConfigs {
91 pub(crate) mechanism: String,
94
95 pub(crate) jaas_config: Option<Password>,
111
112 pub(crate) username: Option<String>,
115
116 pub(crate) password: Option<Password>,
123}
124
125impl Default for SaslConfigs {
126 fn default() -> Self {
127 SaslConfigs {
128 mechanism: SaslConfigs::DEFAULT_SASL_MECHANISM.to_owned(),
129 jaas_config: None,
130 username: None,
131 password: None,
132 }
133 }
134}
135
136impl SaslConfigs {
137 pub(crate) fn from_values(values: &mut ValuesReader<'_>) -> Result<SaslConfigs, Error> {
147 Ok(SaslConfigs {
148 mechanism: values.string(SaslConfigs::SASL_MECHANISM)?.to_string(),
149 jaas_config: values
150 .password_or_null(SaslConfigs::SASL_JAAS_CONFIG)?
151 .filter(|jaas| !jaas.value().is_empty())
152 .cloned(),
153 username: None,
154 password: None,
155 })
156 }
157
158 fn parse_jaas_option<'a>(jaas: &'a str, key: &str) -> Option<Cow<'a, str>> {
191 let bytes = jaas.as_bytes();
192 let n = bytes.len();
193
194 fn skip_quoted(bytes: &[u8], mut i: usize) -> usize {
200 let quote = bytes[i];
201 i += 1;
202 while i < bytes.len() {
203 if bytes[i] == b'\\' {
204 i += 2; continue;
206 }
207 if bytes[i] == quote {
208 return i + 1;
209 }
210 if bytes[i] == b'\n' || bytes[i] == b'\r' {
213 return i;
214 }
215 i += 1;
216 }
217 i
218 }
219
220 let mut i = 0;
223 while i < n {
224 let c = bytes[i];
225
226 if c == b'"' || c == b'\'' {
228 i = skip_quoted(bytes, i);
229 continue;
230 }
231
232 let at_option_start = i == 0 || bytes[i - 1].is_ascii_whitespace();
234 if at_option_start && jaas[i..].starts_with(key) {
235 let mut j = i + key.len();
237 while j < n && bytes[j].is_ascii_whitespace() {
238 j += 1;
239 }
240 if j < n && bytes[j] == b'=' {
244 j += 1; while j < n && bytes[j].is_ascii_whitespace() {
248 j += 1;
249 }
250 if j >= n {
251 return None;
252 }
253
254 let quote = bytes[j];
256 if quote == b'"' || quote == b'\'' {
257 let value_start = j + 1;
258 let mut k = value_start;
259 while k < n {
260 if bytes[k] == b'\\' {
261 k += 2; continue;
263 }
264 if bytes[k] == quote {
265 return Some(SaslConfigs::decode_quoted_escapes(&jaas[value_start..k]));
266 }
267 if bytes[k] == b'\n' || bytes[k] == b'\r' {
274 return None;
275 }
276 k += 1;
277 }
278 return None;
280 }
281
282 let value_end = jaas[j..]
284 .find(|ch: char| ch.is_ascii_whitespace() || ch == ';')
285 .map(|e| j + e)
286 .unwrap_or(n);
287 return Some(Cow::Borrowed(&jaas[j..value_end]));
288 }
289 }
290
291 i += 1;
292 }
293
294 None
295 }
296
297 fn decode_quoted_escapes(raw: &str) -> Cow<'_, str> {
314 if !raw.contains('\\') {
315 return Cow::Borrowed(raw);
316 }
317 let mut decoded = String::with_capacity(raw.len());
318 let mut chars = raw.chars().peekable();
319 while let Some(c) = chars.next() {
320 if c != '\\' {
321 decoded.push(c);
322 continue;
323 }
324 let Some(escaped) = chars.next() else {
327 break;
328 };
329 match escaped {
330 'a' => decoded.push('\u{7}'),
331 'b' => decoded.push('\u{8}'),
332 'f' => decoded.push('\u{c}'),
333 'n' => decoded.push('\n'),
334 'r' => decoded.push('\r'),
335 't' => decoded.push('\t'),
336 'v' => decoded.push('\u{b}'),
337 '0'..='7' => {
338 let mut code = escaped as u32 - '0' as u32;
339 if let Some(second) = chars.peek().and_then(|c| c.to_digit(8)) {
340 chars.next();
341 code = code * 8 + second;
342 if escaped <= '3'
343 && let Some(third) = chars.peek().and_then(|c| c.to_digit(8))
344 {
345 chars.next();
346 code = code * 8 + third;
347 }
348 }
349 decoded.extend(char::from_u32(code));
351 },
352 other => decoded.push(other),
353 }
354 }
355 Cow::Owned(decoded)
356 }
357
358 pub(crate) fn resolve_username(&self) -> Option<Cow<'_, str>> {
361 if let Some(ref u) = self.username {
362 return Some(Cow::Borrowed(u.as_str()));
363 }
364 if let Some(ref jaas) = self.jaas_config {
365 return SaslConfigs::parse_jaas_option(jaas.value(), "username");
366 }
367 None
368 }
369
370 pub(crate) fn resolve_password(&self) -> Option<Cow<'_, str>> {
376 if let Some(ref p) = self.password {
377 return Some(Cow::Borrowed(p.value()));
378 }
379 if let Some(ref jaas) = self.jaas_config {
380 return SaslConfigs::parse_jaas_option(jaas.value(), "password");
381 }
382 None
383 }
384}
385
386impl SaslConfigs {
387 pub(crate) fn add_client_sasl_support(config: ConfigDef) -> ConfigDef {
398 config
399 .define(
400 "sasl.kerberos.service.name",
401 Type::String,
402 DefaultValue::Null,
403 Importance::Medium,
404 )
405 .define("sasl.kerberos.kinit.cmd", Type::String, "/usr/bin/kinit", Importance::Low)
406 .define("sasl.kerberos.ticket.renew.window.factor", Type::Double, 0.80, Importance::Low)
407 .define("sasl.kerberos.ticket.renew.jitter", Type::Double, 0.05, Importance::Low)
408 .define("sasl.kerberos.min.time.before.relogin", Type::Long, 60_000i64, Importance::Low)
409 .define_with_validator(
410 "sasl.login.refresh.window.factor",
411 Type::Double,
412 0.80,
413 Range::between(0.5, 1.0),
414 Importance::Low,
415 )
416 .define_with_validator(
417 "sasl.login.refresh.window.jitter",
418 Type::Double,
419 0.05,
420 Range::between(0.0, 0.25),
421 Importance::Low,
422 )
423 .define_with_validator(
424 "sasl.login.refresh.min.period.seconds",
425 Type::Short,
426 60i16,
427 Range::between(0, 900),
428 Importance::Low,
429 )
430 .define_with_validator(
431 "sasl.login.refresh.buffer.seconds",
432 Type::Short,
433 300i16,
434 Range::between(0, 3600),
435 Importance::Low,
436 )
437 .define(
438 SaslConfigs::SASL_MECHANISM,
439 Type::String,
440 SaslConfigs::DEFAULT_SASL_MECHANISM,
441 Importance::Medium,
442 )
443 .define(
444 SaslConfigs::SASL_JAAS_CONFIG,
445 Type::Password,
446 DefaultValue::Null,
447 Importance::Medium,
448 )
449 .define("sasl.login.connect.timeout.ms", Type::Int, DefaultValue::Null, Importance::Low)
450 .define("sasl.login.read.timeout.ms", Type::Int, DefaultValue::Null, Importance::Low)
451 .define("sasl.login.retry.backoff.max.ms", Type::Long, 10_000i64, Importance::Low)
452 .define("sasl.login.retry.backoff.ms", Type::Long, 100i64, Importance::Low)
453 .define("sasl.oauthbearer.scope", Type::String, DefaultValue::Null, Importance::Medium)
454 .define(
455 "sasl.oauthbearer.client.credentials.client.id",
456 Type::String,
457 DefaultValue::Null,
458 Importance::Medium,
459 )
460 .define(
461 "sasl.oauthbearer.client.credentials.client.secret",
462 Type::Password,
463 DefaultValue::Null,
464 Importance::Medium,
465 )
466 .define_with_validator(
467 "sasl.oauthbearer.assertion.algorithm",
468 Type::String,
469 "RS256",
470 CaseInsensitiveValidString::r#in(&["ES256", "RS256"]),
471 Importance::Medium,
472 )
473 .define(
474 "sasl.oauthbearer.assertion.claim.aud",
475 Type::String,
476 DefaultValue::Null,
477 Importance::Medium,
478 )
479 .define_with_validator(
480 "sasl.oauthbearer.assertion.claim.exp.seconds",
481 Type::Int,
482 300,
483 Range::between(0, 86400),
484 Importance::Low,
485 )
486 .define(
487 "sasl.oauthbearer.assertion.claim.iss",
488 Type::String,
489 DefaultValue::Null,
490 Importance::Medium,
491 )
492 .define(
493 "sasl.oauthbearer.assertion.claim.jti.include",
494 Type::Boolean,
495 false,
496 Importance::Medium,
497 )
498 .define_with_validator(
499 "sasl.oauthbearer.assertion.claim.nbf.seconds",
500 Type::Int,
501 60,
502 Range::between(0, 3600),
503 Importance::Low,
504 )
505 .define(
506 "sasl.oauthbearer.assertion.claim.sub",
507 Type::String,
508 DefaultValue::Null,
509 Importance::Medium,
510 )
511 .define(
512 "sasl.oauthbearer.assertion.file",
513 Type::String,
514 DefaultValue::Null,
515 Importance::Medium,
516 )
517 .define(
518 "sasl.oauthbearer.assertion.private.key.file",
519 Type::String,
520 DefaultValue::Null,
521 Importance::Medium,
522 )
523 .define(
524 "sasl.oauthbearer.assertion.private.key.passphrase",
525 Type::Password,
526 DefaultValue::Null,
527 Importance::Medium,
528 )
529 .define(
530 "sasl.oauthbearer.assertion.template.file",
531 Type::String,
532 DefaultValue::Null,
533 Importance::Medium,
534 )
535 .define("sasl.oauthbearer.scope.claim.name", Type::String, "scope", Importance::Low)
536 .define("sasl.oauthbearer.sub.claim.name", Type::String, "sub", Importance::Low)
537 .define(
538 "sasl.oauthbearer.token.endpoint.url",
539 Type::String,
540 DefaultValue::Null,
541 Importance::Medium,
542 )
543 .define(
544 "sasl.oauthbearer.jwks.endpoint.url",
545 Type::String,
546 DefaultValue::Null,
547 Importance::Medium,
548 )
549 .define(
550 "sasl.oauthbearer.jwks.endpoint.refresh.ms",
551 Type::Long,
552 3_600_000i64,
553 Importance::Low,
554 )
555 .define(
556 "sasl.oauthbearer.jwks.endpoint.retry.backoff.max.ms",
557 Type::Long,
558 10_000i64,
559 Importance::Low,
560 )
561 .define(
562 "sasl.oauthbearer.jwks.endpoint.retry.backoff.ms",
563 Type::Long,
564 100i64,
565 Importance::Low,
566 )
567 .define("sasl.oauthbearer.clock.skew.seconds", Type::Int, 30, Importance::Low)
568 .define(
569 "sasl.oauthbearer.allow.unverified.audience",
570 Type::Boolean,
571 false,
572 Importance::Medium,
573 )
574 .define_with_validator(
575 "sasl.oauthbearer.expected.audience",
576 Type::List,
577 Vec::<String>::new(),
578 ValidList::any_non_duplicate_values(true, false),
579 Importance::High,
580 )
581 .define(
582 "sasl.oauthbearer.allow.unverified.issuer",
583 Type::Boolean,
584 false,
585 Importance::Medium,
586 )
587 .define(
588 "sasl.oauthbearer.expected.issuer",
589 Type::String,
590 DefaultValue::Null,
591 Importance::High,
592 )
593 .define("sasl.oauthbearer.header.urlencode", Type::Boolean, false, Importance::Low)
594 }
595}
596
597#[cfg(test)]
598mod tests {
599 use super::*;
600
601 #[test]
602 fn test_default() {
603 let config = SaslConfigs::default();
604 assert_eq!(config.mechanism, "GSSAPI");
605 assert!(config.jaas_config.is_none());
606 assert!(config.username.is_none());
607 assert!(config.password.is_none());
608 }
609
610 #[test]
611 fn test_resolve_username_from_direct_field() {
612 let config = SaslConfigs { username: Some("alice".to_owned()), ..SaslConfigs::default() };
613 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
614 }
615
616 #[test]
617 fn test_resolve_password_from_direct_field() {
618 let config = SaslConfigs { password: Some(Password::new("secret")), ..SaslConfigs::default() };
619 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
620 }
621
622 #[test]
623 fn test_resolve_username_from_jaas_config() {
624 let config = SaslConfigs {
625 jaas_config: Some(Password::new(
626 "org.apache.kafka.common.security.plain.PlainLoginModule required \
627 username=\"alice\" password=\"secret\";",
628 )),
629 ..SaslConfigs::default()
630 };
631 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
632 }
633
634 #[test]
635 fn test_resolve_password_from_jaas_config() {
636 let config = SaslConfigs {
637 jaas_config: Some(Password::new(
638 "org.apache.kafka.common.security.plain.PlainLoginModule required \
639 username=\"alice\" password=\"secret\";",
640 )),
641 ..SaslConfigs::default()
642 };
643 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
644 }
645
646 #[test]
647 fn test_direct_fields_take_precedence_over_jaas() {
648 let config = SaslConfigs {
649 jaas_config: Some(Password::new(
650 "org.apache.kafka.common.security.plain.PlainLoginModule required \
651 username=\"jaas_user\" password=\"jaas_pass\";",
652 )),
653 username: Some("direct_user".to_owned()),
654 password: Some(Password::new("direct_pass")),
655 ..SaslConfigs::default()
656 };
657 assert_eq!(config.resolve_username().as_deref(), Some("direct_user"));
658 assert_eq!(config.resolve_password().as_deref(), Some("direct_pass"));
659 }
660
661 #[test]
662 fn test_resolve_from_empty_config() {
663 let config = SaslConfigs::default();
664 assert_eq!(config.resolve_username().as_deref(), None);
665 assert_eq!(config.resolve_password().as_deref(), None);
666 }
667
668 #[test]
669 fn test_jaas_config_unquoted_values() {
670 let config = SaslConfigs {
671 jaas_config: Some(Password::new("PlainLoginModule required username=alice password=secret;")),
672 ..SaslConfigs::default()
673 };
674 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
675 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
676 }
677
678 #[test]
679 fn test_jaas_config_missing_field() {
680 let config = SaslConfigs {
681 jaas_config: Some(Password::new("PlainLoginModule required username=\"alice\";")),
682 ..SaslConfigs::default()
683 };
684 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
685 assert_eq!(config.resolve_password().as_deref(), None);
686 }
687
688 #[test]
689 fn test_jaas_config_malformed_no_closing_quote() {
690 let config = SaslConfigs {
691 jaas_config: Some(Password::new("PlainLoginModule required username=\"alice password=\"secret\";")),
692 ..SaslConfigs::default()
693 };
694 assert_eq!(config.resolve_username().as_deref(), Some("alice password="));
698 }
699
700 #[test]
701 fn test_jaas_config_empty_string() {
702 let config = SaslConfigs { jaas_config: Some(Password::new("")), ..SaslConfigs::default() };
703 assert_eq!(config.resolve_username().as_deref(), None);
704 assert_eq!(config.resolve_password().as_deref(), None);
705 }
706
707 #[test]
708 fn test_jaas_config_no_options() {
709 let config = SaslConfigs {
710 jaas_config: Some(Password::new("PlainLoginModule required;")),
711 ..SaslConfigs::default()
712 };
713 assert_eq!(config.resolve_username().as_deref(), None);
714 assert_eq!(config.resolve_password().as_deref(), None);
715 }
716
717 #[test]
718 fn test_jaas_config_with_extra_whitespace() {
719 let config = SaslConfigs {
720 jaas_config: Some(Password::new(
721 "PlainLoginModule required username=\"bob\" password=\"pass123\" ;",
722 )),
723 ..SaslConfigs::default()
724 };
725 assert_eq!(config.resolve_username().as_deref(), Some("bob"));
726 assert_eq!(config.resolve_password().as_deref(), Some("pass123"));
727 }
728
729 #[test]
730 fn test_jaas_config_key_as_substring_not_matched() {
731 let config = SaslConfigs {
733 jaas_config: Some(Password::new(
734 "PlainLoginModule required myusername=\"wrong\" username=\"right\";",
735 )),
736 ..SaslConfigs::default()
737 };
738 assert_eq!(config.resolve_username().as_deref(), Some("right"));
739 }
740
741 #[test]
742 fn test_jaas_config_spaces_around_equals() {
743 let config = SaslConfigs {
745 jaas_config: Some(Password::new(
746 "PlainLoginModule required username = \"alice\" password = \"secret\";",
747 )),
748 ..SaslConfigs::default()
749 };
750 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
751 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
752 }
753
754 #[test]
755 fn test_jaas_config_single_quoted_values() {
756 let config = SaslConfigs {
757 jaas_config: Some(Password::new("PlainLoginModule required username='alice' password='secret';")),
758 ..SaslConfigs::default()
759 };
760 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
761 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
762 }
763
764 #[test]
765 fn test_jaas_config_single_quoted_with_spaces() {
766 let config = SaslConfigs {
767 jaas_config: Some(Password::new(
768 "PlainLoginModule required username = 'alice' password = 'secret';",
769 )),
770 ..SaslConfigs::default()
771 };
772 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
773 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
774 }
775
776 #[test]
777 fn test_jaas_config_bare_value_with_spaces_around_equals() {
778 let config = SaslConfigs {
779 jaas_config: Some(Password::new("PlainLoginModule required username = alice password = secret;")),
780 ..SaslConfigs::default()
781 };
782 assert_eq!(config.resolve_username().as_deref(), Some("alice"));
783 assert_eq!(config.resolve_password().as_deref(), Some("secret"));
784 }
785
786 #[test]
787 fn test_jaas_config_service_name_does_not_false_match() {
788 let config = SaslConfigs {
791 jaas_config: Some(Password::new(
792 "com.sun.security.auth.module.Krb5LoginModule required \
793 serviceName=\"kafka\" foo.username=\"wrong\" username=\"right\" \
794 password=\"pass\";",
795 )),
796 ..SaslConfigs::default()
797 };
798 assert_eq!(config.resolve_username().as_deref(), Some("right"));
799 assert_eq!(config.resolve_password().as_deref(), Some("pass"));
800 }
801
802 #[test]
803 fn test_jaas_config_namespace_does_not_false_match_name() {
804 assert_eq!(
808 SaslConfigs::parse_jaas_option("m required usernamespace=\"x\";", "username"),
809 None
810 );
811 }
812
813 #[test]
814 fn test_jaas_config_key_inside_a_quoted_value_does_not_match() {
815 let config = SaslConfigs {
820 jaas_config: Some(Password::new(
821 "PlainLoginModule required password=\"username=x\" username=\"right\";",
822 )),
823 ..SaslConfigs::default()
824 };
825 assert_eq!(config.resolve_username().as_deref(), Some("right"));
826 assert_eq!(config.resolve_password().as_deref(), Some("username=x"));
827 }
828
829 #[test]
830 fn test_jaas_config_escaped_quote_in_value_does_not_split_or_misalign() {
831 let config = SaslConfigs {
836 jaas_config: Some(Password::new(
837 "PlainLoginModule required password=\"a\\\"b\" username=\"right\";",
838 )),
839 ..SaslConfigs::default()
840 };
841 assert_eq!(config.resolve_username().as_deref(), Some("right"));
842 assert_eq!(config.resolve_password().as_deref(), Some("a\"b"));
843 }
844
845 fn jaas_password(value: &str) -> Option<String> {
846 let config = SaslConfigs {
847 jaas_config: Some(Password::new(format!(
848 "org.apache.kafka.common.security.plain.PlainLoginModule required username=\"u\" password={value};"
849 ))),
850 ..SaslConfigs::default()
851 };
852 config.resolve_password().map(Cow::into_owned)
853 }
854
855 #[test]
859 fn test_jaas_config_quoted_value_escapes_are_decoded_like_java() {
860 let cases: &[(&str, &str)] = &[
861 (r#""b\\a\"ck""#, "b\\a\"ck"),
863 (r"'it\'s'", "it's"),
865 (r#""\a\b\f\n\r\t\v""#, "\u{7}\u{8}\u{c}\n\r\t\u{b}"),
867 (r#""\101\0\477\8\q\08\377\400""#, "A\u{0}'78q\u{0}8\u{ff} 0"),
872 (r#""tab\there""#, "tab\there"),
873 (r#""é\é""#, "éé"),
875 (r#""C:\\temp\\x""#, "C:\\temp\\x"),
877 ];
878 for (value, expected) in cases {
879 assert_eq!(jaas_password(value).as_deref(), Some(*expected), "password={value}");
880 }
881 }
882
883 #[test]
890 fn test_jaas_config_raw_line_terminator_ends_a_quoted_value() {
891 for raw in ["\n", "\r"] {
892 assert_eq!(jaas_password(&format!("\"ab{raw}cd\"")), None, "{raw:?}");
893 }
894 assert_eq!(jaas_password("\"ab\\\ncd\"").as_deref(), Some("ab\ncd"));
895
896 let config = SaslConfigs {
897 jaas_config: Some(Password::new("PlainLoginModule required username=\"u\"\n password=\"abcd\";")),
898 ..SaslConfigs::default()
899 };
900 assert_eq!(config.resolve_username().as_deref(), Some("u"));
901 assert_eq!(config.resolve_password().as_deref(), Some("abcd"));
902 }
903
904 #[test]
911 fn test_jaas_config_bare_value_with_a_backslash_is_returned_as_written() {
912 assert_eq!(
913 SaslConfigs::parse_jaas_option("PlainLoginModule required password=a\\b;", "password").as_deref(),
914 Some("a\\b")
915 );
916 }
917
918 #[test]
920 fn test_jaas_config_value_without_escapes_is_borrowed() {
921 let jaas = "PlainLoginModule required username=\"alice\" password='secret';";
922 for key in ["username", "password"] {
923 assert!(
924 matches!(SaslConfigs::parse_jaas_option(jaas, key), Some(Cow::Borrowed(_))),
925 "{key}"
926 );
927 }
928 }
929
930 #[test]
931 fn test_config_key_constants() {
932 assert_eq!(SaslConfigs::SASL_MECHANISM, "sasl.mechanism");
933 assert_eq!(SaslConfigs::SASL_JAAS_CONFIG, "sasl.jaas.config");
934 assert_eq!(SaslConfigs::DEFAULT_SASL_MECHANISM, "GSSAPI");
935 assert_eq!(SaslConfigs::GSSAPI_MECHANISM, "GSSAPI");
936 }
937
938 #[test]
942 fn test_debug_redacts_jaas_config_and_password() {
943 let config = SaslConfigs {
944 mechanism: "PLAIN".to_owned(),
945 jaas_config: Some(Password::new(
946 "org.apache.kafka.common.security.plain.PlainLoginModule required \
947 username=\"jaas-user\" password=\"jaas-S3cr3t-Pa55\";",
948 )),
949 username: Some("direct-user".to_owned()),
950 password: Some(Password::new("direct-S3cr3t-Pa55")),
951 };
952
953 let rendered = format!("{config:?}");
954 assert_eq!(
955 rendered,
956 "SaslConfigs { mechanism: \"PLAIN\", jaas_config: Some([hidden]), \
957 username: Some(\"direct-user\"), password: Some([hidden]) }"
958 );
959 for secret in [
960 "direct-S3cr3t-Pa55",
961 "jaas-S3cr3t-Pa55",
962 "PlainLoginModule",
963 "jaas-user",
964 ] {
965 assert!(!rendered.contains(secret), "{secret:?} leaked: {rendered}");
966 }
967 let pretty = format!("{config:#?}");
968 assert!(pretty.contains("mechanism: \"PLAIN\""), "{pretty}");
969 assert!(pretty.contains("username: Some("), "{pretty}");
970 assert!(!pretty.contains("S3cr3t"), "{pretty}");
971 assert!(!pretty.contains("PlainLoginModule"), "{pretty}");
972
973 assert_eq!(config.resolve_username().as_deref(), Some("direct-user"));
975 assert_eq!(config.resolve_password().as_deref(), Some("direct-S3cr3t-Pa55"));
976
977 let jaas_only = SaslConfigs { username: None, password: None, ..config };
980 let rendered = format!("{jaas_only:?}");
981 assert!(!rendered.contains("jaas-S3cr3t-Pa55"), "{rendered}");
982 assert!(!rendered.contains("jaas-user"), "{rendered}");
983 assert!(rendered.contains("jaas_config: Some([hidden])"), "{rendered}");
984 assert_eq!(jaas_only.resolve_username().as_deref(), Some("jaas-user"));
985 assert_eq!(jaas_only.resolve_password().as_deref(), Some("jaas-S3cr3t-Pa55"));
986 }
987}