pulsar 6.9.0

Rust client for Apache Pulsar
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
//! Pulsar Admin REST API client.
//!
//! Enabled by the `admin-api` feature flag. Requires a tokio runtime.

use std::{collections::HashMap, fmt::Write as _, sync::Arc, time::Duration};

use futures::lock::Mutex;

use crate::{
    authentication::Authentication,
    connection_manager::TlsOptions,
    error::{AdminError, Error},
    message::proto::{self, Schema},
};

/// Parses a Pulsar topic URL into (scheme, tenant, namespace, topic_name).
/// Accepts `persistent://` and `non-persistent://` prefixes, or a bare
/// `tenant/namespace/topic` string which defaults to `persistent://`.
fn parse_topic(topic: &str) -> Result<(&str, &str, &str, &str), Error> {
    let invalid = || {
        Error::Admin(AdminError::InvalidTopic(format!(
            "expected tenant/namespace/topic or a fully-qualified topic URL, got: {topic}"
        )))
    };

    let (scheme, rest) = if let Some(rest) = topic.strip_prefix("persistent://") {
        ("persistent", rest)
    } else if let Some(rest) = topic.strip_prefix("non-persistent://") {
        ("non-persistent", rest)
    } else {
        ("persistent", topic)
    };

    let mut parts = rest.splitn(3, '/');
    let tenant = parts.next().filter(|s| !s.is_empty()).ok_or_else(invalid)?;
    let namespace = parts.next().filter(|s| !s.is_empty()).ok_or_else(invalid)?;
    let name = parts.next().filter(|s| !s.is_empty()).ok_or_else(invalid)?;

    Ok((scheme, tenant, namespace, name))
}

fn encode_topic_local_name(name: &str) -> String {
    let mut encoded = String::with_capacity(name.len());
    for byte in name.bytes() {
        match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                encoded.push(byte as char)
            }
            _ => write!(&mut encoded, "%{byte:02X}").expect("writing to String cannot fail"),
        }
    }
    encoded
}

#[derive(serde::Deserialize)]
struct AdminSchemaResponse {
    #[serde(rename = "type")]
    schema_type: String,
    #[serde(default)]
    data: String,
    #[serde(default)]
    properties: HashMap<String, String>,
}

fn schema_type_from_admin(schema_type: &str) -> Result<proto::schema::Type, Error> {
    match schema_type.to_ascii_uppercase().as_str() {
        "NONE" => Ok(proto::schema::Type::None),
        "STRING" => Ok(proto::schema::Type::String),
        "JSON" => Ok(proto::schema::Type::Json),
        "PROTOBUF" => Ok(proto::schema::Type::Protobuf),
        "AVRO" => Ok(proto::schema::Type::Avro),
        "BOOL" | "BOOLEAN" => Ok(proto::schema::Type::Bool),
        "INT8" => Ok(proto::schema::Type::Int8),
        "INT16" => Ok(proto::schema::Type::Int16),
        "INT32" => Ok(proto::schema::Type::Int32),
        "INT64" => Ok(proto::schema::Type::Int64),
        "FLOAT" => Ok(proto::schema::Type::Float),
        "DOUBLE" => Ok(proto::schema::Type::Double),
        "DATE" => Ok(proto::schema::Type::Date),
        "TIME" => Ok(proto::schema::Type::Time),
        "TIMESTAMP" => Ok(proto::schema::Type::Timestamp),
        "KEYVALUE" | "KEY_VALUE" => Ok(proto::schema::Type::KeyValue),
        "INSTANT" => Ok(proto::schema::Type::Instant),
        "LOCALDATE" | "LOCAL_DATE" => Ok(proto::schema::Type::LocalDate),
        "LOCALTIME" | "LOCAL_TIME" => Ok(proto::schema::Type::LocalTime),
        "LOCALDATETIME" | "LOCAL_DATE_TIME" => Ok(proto::schema::Type::LocalDateTime),
        "PROTOBUFNATIVE" | "PROTOBUF_NATIVE" => Ok(proto::schema::Type::ProtobufNative),
        _ => Err(AdminError::InvalidSchemaType(schema_type.to_string()).into()),
    }
}

fn key_value_schema_part_bytes(value: &serde_json::Value) -> Result<Vec<u8>, Error> {
    if value.as_str() == Some("") {
        return Ok(Vec::new());
    }
    serde_json::to_vec(value).map_err(|e| AdminError::SchemaDecode(e.to_string()).into())
}

fn append_key_value_schema_part(schema_data: &mut Vec<u8>, part: &[u8]) -> Result<(), Error> {
    let len = u32::try_from(part.len()).map_err(|_| {
        AdminError::SchemaDecode("KEY_VALUE schema part is too large to encode".to_string())
    })?;
    schema_data.extend_from_slice(&len.to_be_bytes());
    schema_data.extend_from_slice(part);
    Ok(())
}

fn key_value_schema_data_from_admin(data: &str) -> Result<Vec<u8>, Error> {
    let value: serde_json::Value =
        serde_json::from_str(data).map_err(|e| AdminError::SchemaDecode(e.to_string()))?;
    let object = value.as_object().ok_or_else(|| {
        AdminError::SchemaDecode("KEY_VALUE schema data must be a JSON object".to_string())
    })?;
    let key = object
        .get("key")
        .ok_or_else(|| AdminError::SchemaDecode("KEY_VALUE schema data missing key".to_string()))?;
    let value = object.get("value").ok_or_else(|| {
        AdminError::SchemaDecode("KEY_VALUE schema data missing value".to_string())
    })?;
    let key = key_value_schema_part_bytes(key)?;
    let value = key_value_schema_part_bytes(value)?;
    let mut schema_data = Vec::with_capacity(8 + key.len() + value.len());
    append_key_value_schema_part(&mut schema_data, &key)?;
    append_key_value_schema_part(&mut schema_data, &value)?;
    Ok(schema_data)
}

fn parse_schema_response(body: &str) -> Result<Schema, Error> {
    let response: AdminSchemaResponse =
        serde_json::from_str(body).map_err(|e| AdminError::SchemaDecode(e.to_string()))?;
    let schema_type = schema_type_from_admin(&response.schema_type)?;
    let schema_data = if schema_type == proto::schema::Type::KeyValue {
        key_value_schema_data_from_admin(&response.data)?
    } else {
        response.data.into_bytes()
    };
    Ok(Schema {
        r#type: schema_type as i32,
        schema_data,
        properties: response
            .properties
            .into_iter()
            .map(|(key, value)| proto::KeyValue { key, value })
            .collect(),
        ..Default::default()
    })
}

/// Client for the Pulsar Admin REST API.
///
/// Obtain an instance via [`Pulsar::admin()`][crate::Pulsar::admin].
///
/// # Example
///
/// ```rust,no_run
/// # async fn run(pulsar: pulsar::Pulsar<pulsar::TokioExecutor>) -> Result<(), pulsar::Error> {
/// let admin = pulsar.admin("http://localhost:8080")?;
/// admin
///     .set_max_unacked_messages_per_consumer(
///         "persistent://public/default/my-topic",
///         500,
///     )
///     .await?;
/// # Ok(())
/// # }
/// ```
pub struct AdminClient {
    client: reqwest::Client,
    admin_url: String,
    auth: Option<Arc<Mutex<Box<dyn Authentication>>>>,
}

impl AdminClient {
    /// Creates a new `AdminClient`.
    ///
    /// Reuses the TLS and authentication configuration already present on the
    /// [`Pulsar`][crate::Pulsar] client. Called internally by
    /// [`Pulsar::admin()`][crate::Pulsar::admin].
    pub(crate) fn new(
        admin_url: String,
        tls_options: &TlsOptions,
        auth: Option<Arc<Mutex<Box<dyn Authentication>>>>,
    ) -> Result<Self, Error> {
        let mut builder = reqwest::ClientBuilder::new()
            .timeout(Duration::from_secs(30))
            .danger_accept_invalid_certs(tls_options.allow_insecure_connection);

        builder = builder.danger_accept_invalid_hostnames(
            tls_options.allow_insecure_connection || !tls_options.tls_hostname_verification_enabled,
        );

        if let Some(pem_bytes) = &tls_options.certificate_chain {
            let certs = pem::parse_many(pem_bytes).map_err(|e| {
                Error::Admin(AdminError::TlsConfig(format!(
                    "failed to parse certificate chain: {e}"
                )))
            })?;
            for cert in certs.iter().rev() {
                let reqwest_cert = reqwest::Certificate::from_der(cert.contents())
                    .map_err(|e| Error::Admin(AdminError::Request(e)))?;
                builder = builder.add_root_certificate(reqwest_cert);
            }
        }

        Ok(AdminClient {
            client: builder
                .build()
                .map_err(|e| Error::Admin(AdminError::Request(e)))?,
            admin_url: admin_url.trim_end_matches('/').to_string(),
            auth,
        })
    }

    async fn apply_auth(
        &self,
        req: reqwest::RequestBuilder,
    ) -> Result<reqwest::RequestBuilder, Error> {
        let Some(auth) = &self.auth else {
            return Ok(req);
        };
        let mut auth = auth.lock().await;
        let method = auth.auth_method_name();
        let data = auth.auth_data().await.map_err(Error::Authentication)?;
        let data_str = String::from_utf8(data)
            .map_err(|e| Error::Custom(format!("auth data is not valid UTF-8: {e}")))?;
        Ok(match method.as_str() {
            "token" => req.bearer_auth(data_str),
            "basic" => match data_str.split_once(':') {
                Some((user, pass)) => req.basic_auth(user, Some(pass)),
                None => req.basic_auth(&data_str, None::<&str>),
            },
            _ => req,
        })
    }

    fn topic_policy_url(&self, topic: &str, policy: &str) -> Result<String, Error> {
        let (scheme, tenant, namespace, name) = parse_topic(topic)?;
        Ok(format!(
            "{}/admin/v2/{}/{}/{}/{}/{policy}",
            self.admin_url, scheme, tenant, namespace, name
        ))
    }

    fn schema_url(&self, topic: &str, version: Option<u64>) -> Result<String, Error> {
        let (_, tenant, namespace, name) = parse_topic(topic)?;
        let name = encode_topic_local_name(name);
        let url = format!(
            "{}/admin/v2/schemas/{}/{}/{}/schema",
            self.admin_url, tenant, namespace, name
        );
        Ok(match version {
            Some(version) => format!("{url}/{version}"),
            None => url,
        })
    }

    async fn check_response(&self, resp: reqwest::Response) -> Result<(), Error> {
        if resp.status().is_success() {
            return Ok(());
        }
        let status = resp.status().as_u16();
        let body = resp.text().await.unwrap_or_default();
        Err(Error::Admin(AdminError::Http { status, body }))
    }

    /// Sets the maximum number of unacknowledged messages allowed per consumer
    /// on a topic.
    ///
    /// This is a persistent broker-side topic policy. The topic must already
    /// exist when this is called (subscribe a consumer first, then call this).
    /// Requires `topicLevelPoliciesEnabled=true` in the broker configuration.
    pub async fn set_max_unacked_messages_per_consumer(
        &self,
        topic: &str,
        max_unacked: u32,
    ) -> Result<(), Error> {
        let url = self.topic_policy_url(topic, "maxUnackedMessagesOnConsumer")?;
        let req = self
            .client
            .post(&url)
            .header("Content-Type", "application/json")
            .body(max_unacked.to_string());
        let req = self.apply_auth(req).await?;
        let resp = req
            .send()
            .await
            .map_err(|e| Error::Admin(AdminError::Request(e)))?;
        self.check_response(resp).await
    }

    /// Removes the per-topic max unacked messages override, reverting to the
    /// broker or namespace default.
    ///
    /// To disable the limit without removing the topic-level override, call
    /// [`set_max_unacked_messages_per_consumer`][Self::set_max_unacked_messages_per_consumer]
    /// with a value of `0` (unlimited).
    pub async fn remove_max_unacked_messages_per_consumer(&self, topic: &str) -> Result<(), Error> {
        let url = self.topic_policy_url(topic, "maxUnackedMessagesOnConsumer")?;
        let req = self.client.delete(&url);
        let req = self.apply_auth(req).await?;
        let resp = req
            .send()
            .await
            .map_err(|e| Error::Admin(AdminError::Request(e)))?;
        self.check_response(resp).await
    }

    async fn get_schema_with_version(
        &self,
        topic: &str,
        version: Option<u64>,
    ) -> Result<Option<Schema>, Error> {
        let url = self.schema_url(topic, version)?;
        let req = self.client.get(&url);
        let req = self.apply_auth(req).await?;
        let resp = req
            .send()
            .await
            .map_err(|e| Error::Admin(AdminError::Request(e)))?;
        if resp.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        }
        if !resp.status().is_success() {
            let status = resp.status().as_u16();
            let body = resp.text().await.unwrap_or_default();
            return Err(Error::Admin(AdminError::Http { status, body }));
        }
        let body = resp
            .text()
            .await
            .map_err(|e| Error::Admin(AdminError::Request(e)))?;
        parse_schema_response(&body).map(Some)
    }

    /// Gets the latest schema registered for a topic through the Pulsar Admin
    /// HTTP API.
    pub async fn get_schema(&self, topic: &str) -> Result<Option<Schema>, Error> {
        self.get_schema_with_version(topic, None).await
    }

    /// Gets a specific schema version registered for a topic through the Pulsar
    /// Admin HTTP API.
    pub async fn get_schema_at_version(
        &self,
        topic: &str,
        version: u64,
    ) -> Result<Option<Schema>, Error> {
        self.get_schema_with_version(topic, Some(version)).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_topic_persistent() {
        let (scheme, tenant, ns, name) =
            parse_topic("persistent://my-tenant/my-namespace/my-topic").unwrap();
        assert_eq!(scheme, "persistent");
        assert_eq!(tenant, "my-tenant");
        assert_eq!(ns, "my-namespace");
        assert_eq!(name, "my-topic");
    }

    #[test]
    fn test_parse_topic_non_persistent() {
        let (scheme, tenant, ns, name) = parse_topic("non-persistent://tenant/ns/topic").unwrap();
        assert_eq!(scheme, "non-persistent");
        assert_eq!(tenant, "tenant");
        assert_eq!(ns, "ns");
        assert_eq!(name, "topic");
    }

    #[test]
    fn test_parse_topic_bare() {
        // No prefix defaults to persistent://
        let (scheme, tenant, ns, name) = parse_topic("tenant/ns/topic").unwrap();
        assert_eq!(scheme, "persistent");
        assert_eq!(tenant, "tenant");
        assert_eq!(ns, "ns");
        assert_eq!(name, "topic");
    }

    #[test]
    fn test_parse_topic_missing_parts() {
        assert!(parse_topic("").is_err());
        assert!(parse_topic("tenant").is_err());
        assert!(parse_topic("tenant/ns").is_err());
        // trailing slash = empty topic name
        assert!(parse_topic("tenant/ns/").is_err());
        assert!(parse_topic("persistent://").is_err());
        assert!(parse_topic("persistent://tenant").is_err());
        assert!(parse_topic("persistent://tenant/ns").is_err());
        assert!(parse_topic("persistent://tenant/ns/").is_err());
    }

    #[test]
    fn test_topic_policy_url() {
        let client = AdminClient {
            client: reqwest::Client::new(),
            admin_url: "http://localhost:8080".to_string(),
            auth: None,
        };
        assert_eq!(
            client
                .topic_policy_url(
                    "persistent://public/default/my-topic",
                    "maxUnackedMessagesOnConsumer"
                )
                .unwrap(),
            "http://localhost:8080/admin/v2/persistent/public/default/my-topic/maxUnackedMessagesOnConsumer"
        );
    }

    #[test]
    fn test_schema_url_latest() {
        let client = AdminClient {
            client: reqwest::Client::new(),
            admin_url: "http://localhost:8080".to_string(),
            auth: None,
        };
        assert_eq!(
            client
                .schema_url("persistent://public/default/my-topic", None)
                .unwrap(),
            "http://localhost:8080/admin/v2/schemas/public/default/my-topic/schema"
        );
    }

    #[test]
    fn test_schema_url_version() {
        let client = AdminClient {
            client: reqwest::Client::new(),
            admin_url: "http://localhost:8080".to_string(),
            auth: None,
        };
        assert_eq!(
            client
                .schema_url("public/default/my-topic", Some(7))
                .unwrap(),
            "http://localhost:8080/admin/v2/schemas/public/default/my-topic/schema/7"
        );
    }

    #[test]
    fn test_schema_url_encodes_local_name() {
        let client = AdminClient {
            client: reqwest::Client::new(),
            admin_url: "http://localhost:8080".to_string(),
            auth: None,
        };
        assert_eq!(
            client
                .schema_url("persistent://public/default/topic?key#frag ment", None)
                .unwrap(),
            "http://localhost:8080/admin/v2/schemas/public/default/topic%3Fkey%23frag%20ment/schema"
        );
    }

    #[test]
    fn test_parse_schema_response() {
        let body = r#"{
            "version": 3,
            "type": "JSON",
            "timestamp": 1234,
            "data": "{\"type\":\"record\",\"name\":\"User\",\"fields\":[]}",
            "properties": {"k": "v"}
        }"#;

        let schema = parse_schema_response(body).unwrap();

        assert_eq!(
            schema.r#type,
            crate::message::proto::schema::Type::Json as i32
        );
        assert_eq!(
            schema.schema_data,
            b"{\"type\":\"record\",\"name\":\"User\",\"fields\":[]}"
        );
        assert_eq!(schema.properties.len(), 1);
        assert_eq!(schema.properties[0].key, "k");
        assert_eq!(schema.properties[0].value, "v");
    }

    #[test]
    fn test_parse_key_value_schema_response_converts_data() {
        let body = r#"{
            "version": 1,
            "type": "KEY_VALUE",
            "timestamp": 1234,
            "data": "{\"key\":{\"type\":\"record\",\"name\":\"Key\",\"fields\":[]},\"value\":\"\"}",
            "properties": {"kv.encoding.type": "SEPARATED"}
        }"#;

        let schema = parse_schema_response(body).unwrap();

        assert_eq!(
            schema.r#type,
            crate::message::proto::schema::Type::KeyValue as i32
        );
        let key_schema = br#"{"fields":[],"name":"Key","type":"record"}"#;
        let mut expected = Vec::new();
        expected.extend_from_slice(&(key_schema.len() as u32).to_be_bytes());
        expected.extend_from_slice(key_schema);
        expected.extend_from_slice(&0u32.to_be_bytes());
        assert_eq!(schema.schema_data, expected);
        assert_eq!(schema.properties[0].key, "kv.encoding.type");
        assert_eq!(schema.properties[0].value, "SEPARATED");
    }

    #[test]
    fn test_admin_url_trailing_slash_stripped() {
        // Trailing slash on admin_url should be normalized away
        let tls = TlsOptions::default();
        let client = AdminClient::new("http://localhost:8080/".to_string(), &tls, None).unwrap();
        assert_eq!(client.admin_url, "http://localhost:8080");
    }
}