scconfig-rs 0.1.3

Async Rust client for Spring Cloud Config Server
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
use std::time::Duration;

use reqwest::{
    Client, Url,
    header::{ACCEPT, CONTENT_TYPE, HeaderMap, HeaderName, HeaderValue},
};

use crate::{
    ConfigDocument, ConfigResource, DocumentFormat, Environment, EnvironmentFormat,
    EnvironmentRequest, Error, ResourceRequest, Result,
};

#[derive(Debug, Clone)]
enum Auth {
    Basic {
        username: String,
        password: Option<String>,
    },
    Bearer(String),
}

/// Builder for [`SpringConfigClient`].
#[derive(Debug, Clone)]
pub struct SpringConfigClientBuilder {
    base_url: Url,
    default_label: Option<String>,
    auth: Option<Auth>,
    accept_invalid_certs: bool,
    accept_invalid_hostnames: bool,
    timeout: Option<Duration>,
    connect_timeout: Option<Duration>,
    user_agent: Option<String>,
    headers: HeaderMap,
}

impl SpringConfigClientBuilder {
    /// Sets a default label used when a request does not provide one explicitly.
    pub fn default_label(mut self, label: impl Into<String>) -> Self {
        let label = label.into().trim().to_string();
        self.default_label = if label.is_empty() { None } else { Some(label) };
        self
    }

    /// Configures HTTP Basic authentication.
    pub fn basic_auth(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
        self.auth = Some(Auth::Basic {
            username: username.into(),
            password: Some(password.into()),
        });
        self
    }

    /// Configures Bearer token authentication.
    pub fn bearer_auth(mut self, token: impl Into<String>) -> Self {
        self.auth = Some(Auth::Bearer(token.into()));
        self
    }

    /// Disables TLS certificate validation.
    ///
    /// This should only be enabled for development or controlled test environments
    /// that use self-signed or otherwise untrusted certificates.
    pub fn danger_accept_invalid_certs(mut self, enabled: bool) -> Self {
        self.accept_invalid_certs = enabled;
        self
    }

    /// Disables TLS hostname validation.
    ///
    /// This should only be enabled for development or controlled test environments
    /// where the certificate hostname does not match the requested host.
    pub fn danger_accept_invalid_hostnames(mut self, enabled: bool) -> Self {
        self.accept_invalid_hostnames = enabled;
        self
    }

    /// Disables both TLS certificate and hostname validation.
    ///
    /// This is a convenience method for local development or smoke tests against
    /// environments with broken or private TLS setups. Do not enable this in production.
    pub fn danger_accept_invalid_tls(mut self, enabled: bool) -> Self {
        self.accept_invalid_certs = enabled;
        self.accept_invalid_hostnames = enabled;
        self
    }

    /// Sets the total request timeout.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.timeout = Some(timeout);
        self
    }

    /// Sets the connect timeout.
    pub fn connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = Some(timeout);
        self
    }

    /// Sets the User-Agent header.
    pub fn user_agent(mut self, value: impl Into<String>) -> Self {
        self.user_agent = Some(value.into());
        self
    }

    /// Adds a default HTTP header that will be sent with every request.
    pub fn header(mut self, name: impl AsRef<str>, value: impl AsRef<str>) -> Result<Self> {
        let name_string = name.as_ref().to_string();
        let value_string = value.as_ref().to_string();

        let name = HeaderName::from_bytes(name_string.as_bytes())
            .map_err(|_| Error::InvalidHeaderName(name_string.clone()))?;
        let value =
            HeaderValue::from_str(&value_string).map_err(|_| Error::InvalidHeaderValue {
                name: name_string,
                value: value_string,
            })?;

        self.headers.insert(name, value);
        Ok(self)
    }

    /// Builds the client.
    pub fn build(self) -> Result<SpringConfigClient> {
        let mut builder = Client::builder().default_headers(self.headers);

        if self.accept_invalid_certs {
            builder = builder.danger_accept_invalid_certs(true);
        }

        if self.accept_invalid_hostnames {
            builder = builder.danger_accept_invalid_hostnames(true);
        }

        if let Some(timeout) = self.timeout {
            builder = builder.timeout(timeout);
        }

        if let Some(connect_timeout) = self.connect_timeout {
            builder = builder.connect_timeout(connect_timeout);
        }

        builder =
            builder.user_agent(self.user_agent.unwrap_or_else(|| {
                format!("{}/{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"))
            }));

        let http_client = builder.build().map_err(|source| Error::Transport {
            url: self.base_url.to_string(),
            source,
        })?;

        Ok(SpringConfigClient {
            base_url: self.base_url,
            default_label: self.default_label,
            auth: self.auth,
            http_client,
        })
    }
}

/// Async Spring Cloud Config client for Rust applications.
#[derive(Debug, Clone)]
pub struct SpringConfigClient {
    base_url: Url,
    default_label: Option<String>,
    auth: Option<Auth>,
    http_client: Client,
}

impl SpringConfigClient {
    /// Creates a new builder from the Config Server base URL.
    ///
    /// The base URL may already contain a Config Server prefix such as `/config`.
    pub fn builder(base_url: impl AsRef<str>) -> Result<SpringConfigClientBuilder> {
        let base_url_string = base_url.as_ref().trim().to_string();
        let base_url = Url::parse(&base_url_string)
            .map_err(|_| Error::InvalidBaseUrl(base_url_string.clone()))?;

        if base_url.query().is_some() || base_url.fragment().is_some() {
            return Err(Error::InvalidBaseUrlShape(base_url_string));
        }

        Ok(SpringConfigClientBuilder {
            base_url,
            default_label: None,
            auth: None,
            accept_invalid_certs: false,
            accept_invalid_hostnames: false,
            timeout: None,
            connect_timeout: None,
            user_agent: None,
            headers: HeaderMap::new(),
        })
    }

    /// Fetches the Spring `Environment` JSON payload.
    pub async fn fetch_environment(&self, request: &EnvironmentRequest) -> Result<Environment> {
        let url = self.environment_url(request, None)?;
        let response = self.send(url.clone()).await?;
        let body = self.read_text(response, &url).await?;

        serde_json::from_str(&body).map_err(|source| Error::Json {
            url: url.to_string(),
            source,
        })
    }

    /// Fetches the effective configuration and deserializes it into a Rust type.
    pub async fn fetch_typed<T>(&self, request: &EnvironmentRequest) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        self.fetch_environment(request).await?.deserialize()
    }

    /// Fetches an alternative-format environment representation as UTF-8 text.
    pub async fn fetch_environment_as_text(
        &self,
        request: &EnvironmentRequest,
        format: EnvironmentFormat,
    ) -> Result<String> {
        let url = self.environment_url(request, Some(format))?;
        let response = self.send(url.clone()).await?;
        self.read_text(response, &url).await
    }

    /// Fetches an alternative-format environment representation and parses it into a document.
    pub async fn fetch_environment_document(
        &self,
        request: &EnvironmentRequest,
        format: EnvironmentFormat,
    ) -> Result<ConfigDocument> {
        let origin = self.environment_url(request, Some(format))?.to_string();
        let text = self.fetch_environment_as_text(request, format).await?;
        let document_format = match format {
            EnvironmentFormat::Yml | EnvironmentFormat::Yaml => DocumentFormat::Yaml,
            EnvironmentFormat::Properties => DocumentFormat::Properties,
        };

        ConfigDocument::from_text(&origin, document_format, text)
    }

    /// Fetches a resource from the plain-text Spring endpoint.
    ///
    /// The request always includes `Accept: application/octet-stream` so the same API works
    /// for both text and binary files.
    pub async fn fetch_resource(&self, request: &ResourceRequest) -> Result<ConfigResource> {
        let url = self.resource_url(request)?;
        let response = self
            .send_with_header(
                url.clone(),
                ACCEPT,
                HeaderValue::from_static("application/octet-stream"),
            )
            .await?;

        let content_type = response
            .headers()
            .get(CONTENT_TYPE)
            .and_then(|value| value.to_str().ok())
            .map(ToOwned::to_owned);

        let bytes = response
            .bytes()
            .await
            .map_err(|source| Error::Transport {
                url: url.to_string(),
                source,
            })?
            .to_vec();

        Ok(ConfigResource::new(
            request.path().to_string(),
            url.to_string(),
            content_type,
            bytes,
        ))
    }

    /// Fetches and parses a resource into a [`ConfigDocument`].
    pub async fn fetch_resource_document(
        &self,
        request: &ResourceRequest,
    ) -> Result<ConfigDocument> {
        self.fetch_resource(request).await?.parse()
    }

    /// Fetches a resource and deserializes it into a Rust type.
    pub async fn fetch_resource_typed<T>(&self, request: &ResourceRequest) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        self.fetch_resource(request).await?.deserialize()
    }

    async fn send(&self, url: Url) -> Result<reqwest::Response> {
        let request = self.apply_auth(self.http_client.get(url.clone()));
        let response = request.send().await.map_err(|source| Error::Transport {
            url: url.to_string(),
            source,
        })?;

        Self::ensure_success(url, response).await
    }

    async fn send_with_header(
        &self,
        url: Url,
        header_name: HeaderName,
        header_value: HeaderValue,
    ) -> Result<reqwest::Response> {
        let request = self
            .apply_auth(self.http_client.get(url.clone()))
            .header(header_name, header_value);
        let response = request.send().await.map_err(|source| Error::Transport {
            url: url.to_string(),
            source,
        })?;

        Self::ensure_success(url, response).await
    }

    async fn ensure_success(url: Url, response: reqwest::Response) -> Result<reqwest::Response> {
        let status = response.status();
        if status.is_success() {
            Ok(response)
        } else {
            let body = response.text().await.unwrap_or_default();
            Err(Error::HttpStatus {
                status,
                url: url.to_string(),
                body,
            })
        }
    }

    async fn read_text(&self, response: reqwest::Response, url: &Url) -> Result<String> {
        let bytes = response
            .bytes()
            .await
            .map_err(|source| Error::Transport {
                url: url.to_string(),
                source,
            })?
            .to_vec();

        String::from_utf8(bytes).map_err(|source| Error::Utf8 {
            url: url.to_string(),
            source,
        })
    }

    fn apply_auth(&self, request: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
        match &self.auth {
            Some(Auth::Basic { username, password }) => {
                request.basic_auth(username, password.clone())
            }
            Some(Auth::Bearer(token)) => request.bearer_auth(token),
            None => request,
        }
    }

    fn environment_url(
        &self,
        request: &EnvironmentRequest,
        format: Option<EnvironmentFormat>,
    ) -> Result<Url> {
        let mut url = self.base_url.clone();
        let error_url = url.to_string();
        let application = encode_segment(request.application());
        let profiles = encode_segment(&request.joined_profiles());
        let effective_label = request
            .label_ref()
            .or(self.default_label.as_deref())
            .map(encode_segment);

        {
            let mut segments = url
                .path_segments_mut()
                .map_err(|_| Error::InvalidBaseUrl(error_url.clone()))?;

            segments.push(&application);

            match (format, effective_label.as_deref()) {
                (None, Some(label)) => {
                    segments.push(&profiles);
                    segments.push(label);
                }
                (None, None) => {
                    segments.push(&profiles);
                }
                (Some(format), Some(label)) => {
                    segments.push(&profiles);
                    segments.push(&format!("{label}{}", format.suffix()));
                }
                (Some(format), None) => {
                    segments.push(&format!("{profiles}{}", format.suffix()));
                }
            }
        }

        if format.is_some() && request.resolve_placeholders_enabled() {
            url.query_pairs_mut()
                .append_pair("resolvePlaceholders", "true");
        }

        Ok(url)
    }

    fn resource_url(&self, request: &ResourceRequest) -> Result<Url> {
        let mut url = self.base_url.clone();
        let error_url = url.to_string();
        let application = encode_segment(request.application());
        let profiles = encode_segment(&request.joined_profiles());
        let effective_label = request
            .label_ref()
            .or(self.default_label.as_deref())
            .map(encode_segment);
        let resource_segments = request.path_segments();

        {
            let mut segments = url
                .path_segments_mut()
                .map_err(|_| Error::InvalidBaseUrl(error_url.clone()))?;

            segments.push(&application);
            segments.push(&profiles);

            if let Some(label) = effective_label.as_deref() {
                segments.push(label);
            }

            for segment in &resource_segments {
                segments.push(segment);
            }
        }

        if effective_label.is_none() {
            url.query_pairs_mut().append_pair("useDefaultLabel", "true");
        }

        Ok(url)
    }
}

fn encode_segment(value: &str) -> String {
    value.trim().replace('/', "(_)")
}