Skip to content

Commit 0dd8cf3

Browse files
committed
Use non mutable client
Signed-off-by: Max Lambrecht <[email protected]>
1 parent b35374c commit 0dd8cf3

File tree

5 files changed

+87
-60
lines changed

5 files changed

+87
-60
lines changed

spiffe/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Using an explicit socket path:
4040
```rust
4141
use spiffe::WorkloadApiClient;
4242

43-
let mut client = WorkloadApiClient::connect_to(
43+
let client = WorkloadApiClient::connect_to(
4444
"unix:///tmp/spire-agent/public/api.sock",
4545
).await?;
4646

@@ -51,7 +51,7 @@ Or via the `SPIFFE_ENDPOINT_SOCKET` environment variable:
5151
```rust
5252
use spiffe::WorkloadApiClient;
5353

54-
let mut client = WorkloadApiClient::connect_env().await?;
54+
let client = WorkloadApiClient::connect_env().await?;
5555
```
5656

5757
---

spiffe/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
5454
//! use spiffe::WorkloadApiClient;
5555
//!
56-
//! let mut client = WorkloadApiClient::connect_env().await?;
56+
//! let client = WorkloadApiClient::connect_env().await?;
5757
//!
5858
//! let audiences = &["service-a"];
5959
//! let jwt_svid = client.fetch_jwt_svid(audiences, None).await?;

spiffe/src/workload_api/client.rs

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
//! use tokio_stream::StreamExt;
2727
//!
2828
//! # async fn example() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
29-
//! let mut client = WorkloadApiClient::connect_to("unix:/tmp/spire-agent/public/api.sock").await?;
29+
//! let client = WorkloadApiClient::connect_to("unix:/tmp/spire-agent/public/api.sock").await?;
3030
//!
31-
//! let _jwt = client.fetch_jwt_token(&["service1"], None).await?;
32-
//! let _jwt_svid = client.fetch_jwt_svid(&["service1"], None).await?;
31+
//! let jwt = client.fetch_jwt_token(&["service1"], None).await?;
32+
//! let jwt_svid = client.fetch_jwt_svid(&["service1"], None).await?;
3333
//!
34-
//! let _x509_svid = client.fetch_x509_svid().await?;
35-
//! let _x509_ctx = client.fetch_x509_context().await?;
34+
//! let x509_svid = client.fetch_x509_svid().await?;
35+
//! let x509_ctx = client.fetch_x509_context().await?;
3636
//!
3737
//! let mut updates = client.stream_x509_contexts().await?;
3838
//! while let Some(update) = updates.next().await {
@@ -177,18 +177,20 @@ impl WorkloadApiClient {
177177
///
178178
/// Returns a [`GrpcClientError`] if the gRPC request fails, the response stream
179179
/// ends unexpectedly, or the received data is invalid.
180-
pub async fn fetch_x509_svid(&mut self) -> Result<X509Svid, GrpcClientError> {
180+
pub async fn fetch_x509_svid(&self) -> Result<X509Svid, GrpcClientError> {
181181
let request = X509svidRequest::default();
182182

183+
let mut client = self.client.clone();
183184
let grpc_stream_response: tonic::Response<tonic::Streaming<X509svidResponse>> =
184-
self.client.fetch_x509svid(request).await?;
185+
client.fetch_x509svid(request).await?;
185186

186187
let resp = grpc_stream_response
187188
.into_inner()
188189
.message()
189190
.await?
190191
.ok_or(GrpcClientError::EmptyResponse)?;
191-
WorkloadApiClient::parse_x509_svid_from_grpc_response(&resp)
192+
193+
Self::parse_x509_svid_from_grpc_response(&resp)
192194
}
193195

194196
/// Fetches all X.509 SVIDs available to the calling workload from the SPIFFE Workload API.
@@ -197,11 +199,13 @@ impl WorkloadApiClient {
197199
///
198200
/// Returns a [`GrpcClientError`] if the gRPC request fails, the response stream
199201
/// ends unexpectedly, or the received data is invalid.
200-
pub async fn fetch_all_x509_svids(&mut self) -> Result<Vec<X509Svid>, GrpcClientError> {
202+
pub async fn fetch_all_x509_svids(&self) -> Result<Vec<X509Svid>, GrpcClientError> {
201203
let request = X509svidRequest::default();
202204

205+
let mut client = self.client.clone();
206+
203207
let grpc_stream_response: tonic::Response<tonic::Streaming<X509svidResponse>> =
204-
self.client.fetch_x509svid(request).await?;
208+
client.fetch_x509svid(request).await?;
205209

206210
let response = grpc_stream_response
207211
.into_inner()
@@ -217,18 +221,21 @@ impl WorkloadApiClient {
217221
///
218222
/// Returns a [`GrpcClientError`] if the gRPC request fails, the response stream
219223
/// ends unexpectedly, or the received data is invalid.
220-
pub async fn fetch_x509_bundles(&mut self) -> Result<X509BundleSet, GrpcClientError> {
224+
pub async fn fetch_x509_bundles(&self) -> Result<X509BundleSet, GrpcClientError> {
221225
let request = X509BundlesRequest::default();
222226

227+
let mut client = self.client.clone();
228+
223229
let grpc_stream_response: tonic::Response<tonic::Streaming<X509BundlesResponse>> =
224-
self.client.fetch_x509_bundles(request).await?;
230+
client.fetch_x509_bundles(request).await?;
225231

226232
let response = grpc_stream_response
227233
.into_inner()
228234
.message()
229235
.await?
230236
.ok_or(GrpcClientError::EmptyResponse)?;
231-
WorkloadApiClient::parse_x509_bundle_set_from_grpc_response(response)
237+
238+
Self::parse_x509_bundle_set_from_grpc_response(response)
232239
}
233240

234241
/// Fetches the current set of JWT bundles from the SPIFFE Workload API.
@@ -240,11 +247,13 @@ impl WorkloadApiClient {
240247
///
241248
/// Returns a [`GrpcClientError`] if the gRPC request fails, the stream
242249
/// terminates unexpectedly, or an invalid response is received.
243-
pub async fn fetch_jwt_bundles(&mut self) -> Result<JwtBundleSet, GrpcClientError> {
250+
pub async fn fetch_jwt_bundles(&self) -> Result<JwtBundleSet, GrpcClientError> {
244251
let request = JwtBundlesRequest::default();
245252

253+
let mut client = self.client.clone();
254+
246255
let grpc_stream_response: tonic::Response<tonic::Streaming<JwtBundlesResponse>> =
247-
self.client.fetch_jwt_bundles(request).await?;
256+
client.fetch_jwt_bundles(request).await?;
248257

249258
let response = grpc_stream_response
250259
.into_inner()
@@ -260,11 +269,13 @@ impl WorkloadApiClient {
260269
///
261270
/// Returns a [`GrpcClientError`] if the Workload API request fails, the response
262271
/// stream terminates unexpectedly, or the received data cannot be parsed.
263-
pub async fn fetch_x509_context(&mut self) -> Result<X509Context, GrpcClientError> {
272+
pub async fn fetch_x509_context(&self) -> Result<X509Context, GrpcClientError> {
264273
let request = X509svidRequest::default();
265274

275+
let mut client = self.client.clone();
276+
266277
let grpc_stream_response: tonic::Response<tonic::Streaming<X509svidResponse>> =
267-
self.client.fetch_x509svid(request).await?;
278+
client.fetch_x509svid(request).await?;
268279

269280
let response = grpc_stream_response
270281
.into_inner()
@@ -283,7 +294,7 @@ impl WorkloadApiClient {
283294
/// Returns a [`GrpcClientError`] if the JWT-SVID request fails or the Workload API
284295
/// returns an invalid or empty response.
285296
pub async fn fetch_jwt_svid<T: AsRef<str> + ToString>(
286-
&mut self,
297+
&self,
287298
audience: &[T],
288299
spiffe_id: Option<&SpiffeId>,
289300
) -> Result<JwtSvid, GrpcClientError> {
@@ -314,7 +325,7 @@ impl WorkloadApiClient {
314325
/// Returns a [`GrpcClientError`] if the JWT-SVID request fails, the Workload API response is
315326
/// invalid or empty, or any returned token cannot be parsed.
316327
pub async fn fetch_all_jwt_svids<T: AsRef<str> + ToString>(
317-
&mut self,
328+
&self,
318329
audience: &[T],
319330
spiffe_id: Option<&SpiffeId>,
320331
) -> Result<Vec<JwtSvid>, GrpcClientError> {
@@ -348,7 +359,7 @@ impl WorkloadApiClient {
348359
/// Returns a [`GrpcClientError`] if the JWT-SVID request fails, the Workload API response is
349360
/// invalid, or no JWT-SVID with the requested hint is found.
350361
pub async fn fetch_jwt_svid_by_hint<T: AsRef<str> + ToString>(
351-
&mut self,
362+
&self,
352363
audience: &[T],
353364
spiffe_id: Option<&SpiffeId>,
354365
hint: &str,
@@ -368,7 +379,7 @@ impl WorkloadApiClient {
368379
/// Returns a [`GrpcClientError`] if the token request fails or the Workload API
369380
/// returns an invalid or empty response.
370381
pub async fn fetch_jwt_token<T: AsRef<str> + ToString>(
371-
&mut self,
382+
&self,
372383
audience: &[T],
373384
spiffe_id: Option<&SpiffeId>,
374385
) -> Result<String, GrpcClientError> {
@@ -386,7 +397,7 @@ impl WorkloadApiClient {
386397
///
387398
/// Returns a [`GrpcClientError`] if validation fails or the token cannot be parsed.
388399
pub async fn validate_jwt_token<T: AsRef<str> + ToString>(
389-
&mut self,
400+
&self,
390401
audience: T,
391402
jwt_token: &str,
392403
) -> Result<JwtSvid, GrpcClientError> {
@@ -402,17 +413,20 @@ impl WorkloadApiClient {
402413
/// Returns a [`GrpcClientError`] if the Workload API stream cannot be
403414
/// established or the initial request fails.
404415
pub async fn stream_x509_contexts(
405-
&mut self,
416+
&self,
406417
) -> Result<
407418
impl Stream<Item = Result<X509Context, GrpcClientError>> + Send + 'static,
408419
GrpcClientError,
409420
> {
410421
let request = X509svidRequest::default();
411-
let response = self.client.fetch_x509svid(request).await?;
422+
423+
let mut client = self.client.clone();
424+
425+
let response = client.fetch_x509svid(request).await?;
412426
let stream = response.into_inner().map(|message| {
413427
message
414428
.map_err(GrpcClientError::from)
415-
.and_then(WorkloadApiClient::parse_x509_context_from_grpc_response)
429+
.and_then(Self::parse_x509_context_from_grpc_response)
416430
});
417431
Ok(stream)
418432
}
@@ -424,15 +438,18 @@ impl WorkloadApiClient {
424438
/// Returns a [`GrpcClientError`] if the stream cannot be established or if a
425439
/// stream item fails to be received or parsed.
426440
pub async fn stream_x509_svids(
427-
&mut self,
428-
) -> Result<impl Stream<Item = Result<X509Svid, GrpcClientError>> + 'static, GrpcClientError>
441+
&self,
442+
) -> Result<impl Stream<Item = Result<X509Svid, GrpcClientError>> + Send + 'static, GrpcClientError>
429443
{
430444
let request = X509svidRequest::default();
431-
let response = self.client.fetch_x509svid(request).await?;
445+
446+
let mut client = self.client.clone();
447+
448+
let response = client.fetch_x509svid(request).await?;
432449
let stream = response.into_inner().map(|message| {
433450
message
434451
.map_err(GrpcClientError::from)
435-
.and_then(|resp| WorkloadApiClient::parse_x509_svid_from_grpc_response(&resp))
452+
.and_then(|resp| Self::parse_x509_svid_from_grpc_response(&resp))
436453
});
437454
Ok(stream)
438455
}
@@ -444,15 +461,18 @@ impl WorkloadApiClient {
444461
/// Returns a [`GrpcClientError`] if the Workload API stream cannot be
445462
/// established or the initial request fails.
446463
pub async fn stream_x509_bundles(
447-
&mut self,
448-
) -> Result<impl Stream<Item = Result<X509BundleSet, GrpcClientError>> + 'static, GrpcClientError>
464+
&self,
465+
) -> Result<impl Stream<Item = Result<X509BundleSet, GrpcClientError>> + Send + 'static, GrpcClientError>
449466
{
450467
let request = X509BundlesRequest::default();
451-
let response = self.client.fetch_x509_bundles(request).await?;
468+
469+
let mut client = self.client.clone();
470+
471+
let response = client.fetch_x509_bundles(request).await?;
452472
let stream = response.into_inner().map(|message| {
453473
message
454474
.map_err(GrpcClientError::from)
455-
.and_then(WorkloadApiClient::parse_x509_bundle_set_from_grpc_response)
475+
.and_then(Self::parse_x509_bundle_set_from_grpc_response)
456476
});
457477
Ok(stream)
458478
}
@@ -464,15 +484,18 @@ impl WorkloadApiClient {
464484
/// Returns a [`GrpcClientError`] if the Workload API stream cannot be
465485
/// established or the initial request fails.
466486
pub async fn stream_jwt_bundles(
467-
&mut self,
468-
) -> Result<impl Stream<Item = Result<JwtBundleSet, GrpcClientError>> + 'static, GrpcClientError>
487+
&self,
488+
) -> Result<impl Stream<Item = Result<JwtBundleSet, GrpcClientError>> + Send + 'static, GrpcClientError>
469489
{
470490
let request = JwtBundlesRequest::default();
471-
let response = self.client.fetch_jwt_bundles(request).await?;
491+
492+
let mut client = self.client.clone();
493+
494+
let response = client.fetch_jwt_bundles(request).await?;
472495
let stream = response.into_inner().map(|message| {
473496
message
474497
.map_err(GrpcClientError::from)
475-
.and_then(WorkloadApiClient::parse_jwt_bundle_set_from_grpc_response)
498+
.and_then(Self::parse_jwt_bundle_set_from_grpc_response)
476499
});
477500
Ok(stream)
478501
}
@@ -481,7 +504,7 @@ impl WorkloadApiClient {
481504
/// private
482505
impl WorkloadApiClient {
483506
async fn fetch_jwt<T: AsRef<str> + ToString>(
484-
&mut self,
507+
&self,
485508
audience: &[T],
486509
spiffe_id: Option<&SpiffeId>,
487510
) -> Result<JwtsvidResponse, GrpcClientError> {
@@ -490,11 +513,13 @@ impl WorkloadApiClient {
490513
audience: audience.iter().map(ToString::to_string).collect(),
491514
};
492515

493-
Ok(self.client.fetch_jwtsvid(request).await?.into_inner())
516+
let mut client = self.client.clone();
517+
518+
Ok(client.fetch_jwtsvid(request).await?.into_inner())
494519
}
495520

496521
async fn validate_jwt<T: AsRef<str>>(
497-
&mut self,
522+
&self,
498523
audience: T,
499524
jwt_svid: &str,
500525
) -> Result<ValidateJwtsvidResponse, GrpcClientError> {
@@ -503,7 +528,9 @@ impl WorkloadApiClient {
503528
svid: jwt_svid.into(),
504529
};
505530

506-
Ok(self.client.validate_jwtsvid(request).await?.into_inner())
531+
let mut client = self.client.clone();
532+
533+
Ok(client.validate_jwtsvid(request).await?.into_inner())
507534
}
508535

509536
fn parse_x509_svid_from_grpc_response(

spiffe/src/workload_api/x509_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ impl X509Source {
438438
return;
439439
}
440440

441-
let mut client = match (self.make_client)().await {
441+
let client = match (self.make_client)().await {
442442
Ok(c) => {
443443
backoff = self.reconnect.min_backoff;
444444
c
@@ -535,7 +535,7 @@ async fn try_sync_once(
535535
make_client: &ClientFactory,
536536
picker: Option<&dyn SvidPicker>,
537537
) -> Result<Arc<X509Context>, X509SourceError> {
538-
let mut client = (make_client)().await.map_err(X509SourceError::Grpc)?;
538+
let client = (make_client)().await.map_err(X509SourceError::Grpc)?;
539539
let mut stream = client
540540
.stream_x509_contexts()
541541
.await

0 commit comments

Comments
 (0)