zookeeper_client/sasl/
gssapi.rs

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
use std::borrow::Cow;

use rsasl::callback::{Context, Request, SessionCallback, SessionData};
use rsasl::mechanisms::gssapi::properties::GssService;
use rsasl::prelude::*;
use rsasl::property::Hostname;

use super::{Result, SaslInitiator, SaslInnerOptions, SaslOptions, SaslSession};

impl From<GssapiSaslOptions> for SaslOptions {
    fn from(options: GssapiSaslOptions) -> Self {
        Self(SaslInnerOptions::Gssapi(options))
    }
}

/// GSSAPI SASL options.
///
/// Uses [SaslOptions::gssapi] to construct one.
#[derive(Clone, Debug)]
pub struct GssapiSaslOptions {
    username: Cow<'static, str>,
    hostname: Option<Cow<'static, str>>,
}

impl GssapiSaslOptions {
    pub(crate) fn new() -> Self {
        Self { username: Cow::from("zookeeper"), hostname: None }
    }

    /// Specifies the primary part of Kerberos principal.
    ///
    /// It is `zookeeper.sasl.client.username` in Java client, but the word "client" is misleading
    /// as it is the username of targeting server.
    ///
    /// Defaults to "zookeeper".
    pub fn with_username(self, username: impl Into<Cow<'static, str>>) -> Self {
        Self { username: username.into(), ..self }
    }

    /// Specifies the instance part of Kerberos principal.
    ///
    /// Defaults to hostname or ip of targeting server in connecting string.
    pub fn with_hostname(self, hostname: impl Into<Cow<'static, str>>) -> Self {
        Self { hostname: Some(hostname.into()), ..self }
    }

    fn hostname_or(&self, hostname: &str) -> Cow<'static, str> {
        match self.hostname.as_ref() {
            None => Cow::Owned(hostname.to_string()),
            Some(hostname) => hostname.clone(),
        }
    }
}

impl SaslInitiator for GssapiSaslOptions {
    fn new_session(&self, hostname: &str) -> Result<SaslSession> {
        struct GssapiOptionsProvider {
            username: Cow<'static, str>,
            hostname: Cow<'static, str>,
        }
        impl SessionCallback for GssapiOptionsProvider {
            fn callback(
                &self,
                _session_data: &SessionData,
                _context: &Context,
                request: &mut Request<'_>,
            ) -> Result<(), SessionError> {
                if request.is::<Hostname>() {
                    request.satisfy::<Hostname>(&self.hostname)?;
                } else if request.is::<GssService>() {
                    request.satisfy::<GssService>(&self.username)?;
                }
                Ok(())
            }
        }
        let provider = GssapiOptionsProvider { username: self.username.clone(), hostname: self.hostname_or(hostname) };
        let config = SASLConfig::builder().with_defaults().with_callback(provider).unwrap();
        let client = SASLClient::new(config);
        let session = client.start_suggested(&[Mechname::parse(b"GSSAPI").unwrap()]).unwrap();
        SaslSession::new(session)
    }
}