canton-admin 0.1.2

Canton admin client: party management, user self-inspect, and topology read (gRPC).
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
//! In-process tests that need a controllable server but no live node.
//!
//! Uses the dev-only `canton-proto/server` feature to stand up mock
//! `PartyManagementService` / `UserManagementService` servers, exercising the
//! client logic (pagination, not-retried allocation, error mapping, self-inspect
//! filtering) that the live tests only smoke-check.
#![allow(clippy::unwrap_used, clippy::expect_used)]

use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::time::Duration;

use canton_admin::{AdminClient, Config, RetryConfig};
use canton_proto::com::daml::ledger::api::v2::admin as pb;
use pb::party_management_service_server::{PartyManagementService, PartyManagementServiceServer};
use pb::user_management_service_server::{UserManagementService, UserManagementServiceServer};
use tokio::net::TcpListener;
use tokio_stream::wrappers::TcpListenerStream;
use tonic::transport::Server;
use tonic::{Request, Response, Status};

// ---- PartyManagementService mock -------------------------------------------

/// A configurable `PartyManagementService`:
/// - `list_known_parties` serves five parties across three token-keyed pages
///   (2 + 2 + 1), or `PermissionDenied` when `deny` is set;
/// - `allocate_party` counts calls and fails with `Unavailable` (retriable)
///   when `allocate_fails` is set, to prove the client does not retry it;
/// - `get_parties` echoes requested parties that start with `party-`.
#[derive(Clone, Default)]
struct MockParty {
    allocate_calls: Arc<AtomicUsize>,
    allocate_fails: bool,
    deny: bool,
}

fn party(n: usize) -> pb::PartyDetails {
    pb::PartyDetails {
        party: format!("party-{n}::fp"),
        is_local: true,
        ..Default::default()
    }
}

#[tonic::async_trait]
impl PartyManagementService for MockParty {
    async fn get_participant_id(
        &self,
        _r: Request<pb::GetParticipantIdRequest>,
    ) -> Result<Response<pb::GetParticipantIdResponse>, Status> {
        Ok(Response::new(pb::GetParticipantIdResponse {
            participant_id: "participant::mock".to_string(),
        }))
    }

    async fn list_known_parties(
        &self,
        request: Request<pb::ListKnownPartiesRequest>,
    ) -> Result<Response<pb::ListKnownPartiesResponse>, Status> {
        if self.deny {
            return Err(Status::permission_denied("needs ParticipantAdmin"));
        }
        let (party_details, next_page_token) = match request.into_inner().page_token.as_str() {
            "" => (vec![party(0), party(1)], "p1".to_string()),
            "p1" => (vec![party(2), party(3)], "p2".to_string()),
            "p2" => (vec![party(4)], String::new()),
            other => return Err(Status::invalid_argument(format!("bad token {other}"))),
        };
        Ok(Response::new(pb::ListKnownPartiesResponse {
            party_details,
            next_page_token,
        }))
    }

    async fn get_parties(
        &self,
        request: Request<pb::GetPartiesRequest>,
    ) -> Result<Response<pb::GetPartiesResponse>, Status> {
        // Echo only the parties this participant "knows" (drop the rest).
        let party_details = request
            .into_inner()
            .parties
            .into_iter()
            .filter(|p| p.starts_with("party-"))
            .map(|party| pb::PartyDetails {
                party,
                is_local: true,
                ..Default::default()
            })
            .collect();
        Ok(Response::new(pb::GetPartiesResponse { party_details }))
    }

    async fn allocate_party(
        &self,
        request: Request<pb::AllocatePartyRequest>,
    ) -> Result<Response<pb::AllocatePartyResponse>, Status> {
        self.allocate_calls.fetch_add(1, Ordering::SeqCst);
        if self.allocate_fails {
            return Err(Status::unavailable("try again"));
        }
        let hint = request.into_inner().party_id_hint;
        Ok(Response::new(pb::AllocatePartyResponse {
            party_details: Some(pb::PartyDetails {
                party: format!("{hint}::fp"),
                is_local: true,
                ..Default::default()
            }),
        }))
    }

    async fn allocate_external_party(
        &self,
        _r: Request<pb::AllocateExternalPartyRequest>,
    ) -> Result<Response<pb::AllocateExternalPartyResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn generate_external_party_topology(
        &self,
        _r: Request<pb::GenerateExternalPartyTopologyRequest>,
    ) -> Result<Response<pb::GenerateExternalPartyTopologyResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn update_party_details(
        &self,
        _r: Request<pb::UpdatePartyDetailsRequest>,
    ) -> Result<Response<pb::UpdatePartyDetailsResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn update_party_identity_provider_id(
        &self,
        _r: Request<pb::UpdatePartyIdentityProviderIdRequest>,
    ) -> Result<Response<pb::UpdatePartyIdentityProviderIdResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
}

async fn start_party_server(mock: MockParty) -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    let incoming = TcpListenerStream::new(listener);
    tokio::spawn(async move {
        Server::builder()
            .serve_with_incoming(PartyManagementServiceServer::new(mock), incoming)
            .await
            .unwrap();
    });
    tokio::time::sleep(Duration::from_millis(150)).await;
    format!("http://localhost:{port}")
}

#[tokio::test]
async fn participant_id_round_trips() {
    let url = start_party_server(MockParty::default()).await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();
    assert_eq!(client.participant_id().await.unwrap(), "participant::mock");
}

#[tokio::test]
async fn list_known_parties_follows_pagination() {
    let url = start_party_server(MockParty::default()).await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    let all = client.list_known_parties().await.unwrap();
    let names: Vec<_> = all.into_iter().map(|p| p.party).collect();
    assert_eq!(
        names,
        vec![
            "party-0::fp",
            "party-1::fp",
            "party-2::fp",
            "party-3::fp",
            "party-4::fp",
        ],
        "every page should be collected in order"
    );
}

#[tokio::test]
async fn list_known_parties_page_threads_token_and_ends_with_none() {
    let url = start_party_server(MockParty::default()).await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    // First page carries a continuation token…
    let (page, next) = client.list_known_parties_page(0, None).await.unwrap();
    assert_eq!(page.len(), 2);
    assert_eq!(next.as_deref(), Some("p1"));

    // …and the final page returns no token.
    let (last, done) = client
        .list_known_parties_page(0, Some("p2".to_string()))
        .await
        .unwrap();
    assert_eq!(last.len(), 1);
    assert_eq!(done, None, "the last page must not carry a token");
}

#[tokio::test]
async fn allocate_party_is_not_retried() {
    // Retry is ENABLED, but allocation must still be attempted exactly once:
    // a non-idempotent topology mutation must not be replayed on a lost response.
    let calls = Arc::new(AtomicUsize::new(0));
    let url = start_party_server(MockParty {
        allocate_calls: calls.clone(),
        allocate_fails: true,
        ..Default::default()
    })
    .await;
    let client = AdminClient::connect_lazy(
        Config::new(url).with_retry(
            RetryConfig::default()
                .with_max_attempts(5)
                .with_initial_backoff(Duration::from_millis(1))
                .with_max_backoff(Duration::from_millis(1)),
        ),
    )
    .unwrap();

    let result = client.allocate_party(Some("sdk")).await;
    assert!(result.is_err(), "the transient failure should surface");
    assert_eq!(
        calls.load(Ordering::SeqCst),
        1,
        "allocate_party must be attempted exactly once even with retry enabled"
    );
}

#[tokio::test]
async fn get_parties_drops_unknown_and_handles_empty() {
    let url = start_party_server(MockParty::default()).await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    let got = client
        .get_parties(vec!["party-1::fp".to_string(), "unknown::0".to_string()])
        .await
        .unwrap();
    let names: Vec<_> = got.into_iter().map(|p| p.party).collect();
    assert_eq!(names, vec!["party-1::fp"], "unknown parties are dropped");

    assert!(
        client.get_parties(vec![]).await.unwrap().is_empty(),
        "empty input returns an empty list, not an error"
    );
}

#[tokio::test]
async fn permission_denied_surfaces_as_a_non_retriable_status() {
    let url = start_party_server(MockParty {
        deny: true,
        ..Default::default()
    })
    .await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    let error = client
        .list_known_parties()
        .await
        .expect_err("an under-privileged token must be rejected");
    assert_eq!(error.code(), Some(tonic::Code::PermissionDenied));
    assert!(
        !error.is_retriable(),
        "PermissionDenied must not be retriable"
    );
}

// ---- UserManagementService mock (self-inspect) -----------------------------

/// A `UserManagementService` whose `get_user` echoes the requested id (or the
/// synthetic `self-user` for the empty/self id, and `None` for `"missing"`),
/// and whose `list_user_rights` returns a deliberate mix of right kinds.
#[derive(Clone, Default)]
struct MockUsers;

fn right(kind: pb::right::Kind) -> pb::Right {
    pb::Right { kind: Some(kind) }
}

#[tonic::async_trait]
impl UserManagementService for MockUsers {
    async fn get_user(
        &self,
        request: Request<pb::GetUserRequest>,
    ) -> Result<Response<pb::GetUserResponse>, Status> {
        let user_id = request.into_inner().user_id;
        let user = match user_id.as_str() {
            "missing" => None,
            "" => Some(pb::User {
                id: "self-user".to_string(),
                ..Default::default()
            }),
            id => Some(pb::User {
                id: id.to_string(),
                ..Default::default()
            }),
        };
        Ok(Response::new(pb::GetUserResponse { user }))
    }

    async fn list_user_rights(
        &self,
        _r: Request<pb::ListUserRightsRequest>,
    ) -> Result<Response<pb::ListUserRightsResponse>, Status> {
        use pb::right::{CanActAs, CanActAsAnyParty, CanReadAs, Kind, ParticipantAdmin};
        Ok(Response::new(pb::ListUserRightsResponse {
            rights: vec![
                right(Kind::CanActAs(CanActAs {
                    party: "p1".to_string(),
                })),
                right(Kind::CanReadAs(CanReadAs {
                    party: "p2".to_string(),
                })),
                right(Kind::ParticipantAdmin(ParticipantAdmin {})),
                right(Kind::CanActAsAnyParty(CanActAsAnyParty {})),
            ],
        }))
    }

    async fn create_user(
        &self,
        _r: Request<pb::CreateUserRequest>,
    ) -> Result<Response<pb::CreateUserResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn update_user(
        &self,
        _r: Request<pb::UpdateUserRequest>,
    ) -> Result<Response<pb::UpdateUserResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn delete_user(
        &self,
        _r: Request<pb::DeleteUserRequest>,
    ) -> Result<Response<pb::DeleteUserResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn list_users(
        &self,
        _r: Request<pb::ListUsersRequest>,
    ) -> Result<Response<pb::ListUsersResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn grant_user_rights(
        &self,
        _r: Request<pb::GrantUserRightsRequest>,
    ) -> Result<Response<pb::GrantUserRightsResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn revoke_user_rights(
        &self,
        _r: Request<pb::RevokeUserRightsRequest>,
    ) -> Result<Response<pb::RevokeUserRightsResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
    async fn update_user_identity_provider_id(
        &self,
        _r: Request<pb::UpdateUserIdentityProviderIdRequest>,
    ) -> Result<Response<pb::UpdateUserIdentityProviderIdResponse>, Status> {
        Err(Status::unimplemented("test"))
    }
}

async fn start_user_server() -> String {
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();
    let incoming = TcpListenerStream::new(listener);
    tokio::spawn(async move {
        Server::builder()
            .serve_with_incoming(UserManagementServiceServer::new(MockUsers), incoming)
            .await
            .unwrap();
    });
    tokio::time::sleep(Duration::from_millis(150)).await;
    format!("http://localhost:{port}")
}

#[tokio::test]
async fn current_user_and_get_user_by_id() {
    let url = start_user_server().await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    // current_user sends an empty id → the authenticated ("self") user.
    assert_eq!(client.current_user().await.unwrap().id, "self-user");
    // get_user threads the requested id through.
    assert_eq!(client.get_user("alice").await.unwrap().id, "alice");
}

#[tokio::test]
async fn get_user_with_no_user_is_an_unexpected_response() {
    let url = start_user_server().await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    let error = client
        .get_user("missing")
        .await
        .expect_err("a response with no user must error");
    assert!(
        matches!(error, canton_admin::Error::UnexpectedResponse(_)),
        "got {error:?}"
    );
}

#[tokio::test]
async fn acting_parties_keeps_only_can_act_as() {
    let url = start_user_server().await;
    let client = AdminClient::connect_lazy(Config::new(url)).unwrap();

    // Of {CanActAs(p1), CanReadAs(p2), ParticipantAdmin, CanActAsAnyParty},
    // only the specific CanActAs(p1) is returned.
    let acting = client.acting_parties().await.unwrap();
    assert_eq!(
        acting,
        vec!["p1"],
        "read/admin/any-party rights are excluded"
    );

    // And the full rights list is available for callers that need the wildcard.
    assert_eq!(client.current_user_rights().await.unwrap().len(), 4);
}