cerbos 0.5.0

Rust SDK for working with Cerbos: an open core, language-agnostic, scalable authorization solution
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
// Copyright 2021-2025 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
#![cfg(feature = "hub")]

use anyhow::{Context, Result};
use cerbos::genpb::cerbos::cloud::store::v1::GetFilesRequest;
use cerbos::sdk::hub::auth::AuthMiddleware;
use cerbos::sdk::hub::rpc_error::RPCError;
use cerbos::sdk::hub::store::{
    FileFilterBuilder, GetFilesRequestBuilder, ListFilesRequestBuilder, ModifyFilesRequestBuilder,
    ReplaceFilesRequestBuilder, StoreClient,
};
use cerbos::sdk::hub::utils::zip_directory;
use cerbos::sdk::hub::HubClientBuilder;
use std::path::PathBuf;
use std::{env, str};

// Expected files list from the Go test
const WANT_FILES_LIST: &[&str] = &[
    "_schemas/principal.json",
    "_schemas/resources/leave_request.json",
    "_schemas/resources/purchase_order.json",
    "_schemas/resources/salary_record.json",
    "derived_roles/common_roles.yaml",
    "derived_roles/derived_roles_01.yaml",
    "derived_roles/derived_roles_02.yaml",
    "derived_roles/derived_roles_03.yaml",
    "derived_roles/derived_roles_04.yaml",
    "derived_roles/derived_roles_05.yaml",
    "export_constants/export_constants_01.yaml",
    "export_variables/export_variables_01.yaml",
    "principal_policies/policy_01.yaml",
    "principal_policies/policy_02.yaml",
    "principal_policies/policy_02_acme.hr.yaml",
    "principal_policies/policy_02_acme.sales.yaml",
    "principal_policies/policy_02_acme.yaml",
    "principal_policies/policy_03.yaml",
    "principal_policies/policy_04.yaml",
    "principal_policies/policy_05.yaml",
    "principal_policies/policy_06.yaml",
    "resource_policies/disabled_policy_01.yaml",
    "resource_policies/policy_01.yaml",
    "resource_policies/policy_02.yaml",
    "resource_policies/policy_03.yaml",
    "resource_policies/policy_04.yaml",
    "resource_policies/policy_04_test.yaml",
    "resource_policies/policy_05.yaml",
    "resource_policies/policy_05_acme.hr.uk.brighton.kemptown.yaml",
    "resource_policies/policy_05_acme.hr.uk.brighton.yaml",
    "resource_policies/policy_05_acme.hr.uk.london.yaml",
    "resource_policies/policy_05_acme.hr.uk.yaml",
    "resource_policies/policy_05_acme.hr.yaml",
    "resource_policies/policy_05_acme.yaml",
    "resource_policies/policy_06.yaml",
    "resource_policies/policy_07.yaml",
    "resource_policies/policy_07_acme.yaml",
    "resource_policies/policy_08.yaml",
    "resource_policies/policy_09.yaml",
    "resource_policies/policy_10.yaml",
    "resource_policies/policy_11.yaml",
    "resource_policies/policy_12.yaml",
    "resource_policies/policy_13.yaml",
    "resource_policies/policy_14.yaml",
    "resource_policies/policy_15.yaml",
    "resource_policies/policy_16.yaml",
    "resource_policies/policy_17.acme.sales.yaml",
    "resource_policies/policy_17.acme.yaml",
    "resource_policies/policy_17.yaml",
    "resource_policies/policy_18.yaml",
    "role_policies/policy_01_acme.hr.uk.brighton.yaml",
    "role_policies/policy_02_acme.hr.uk.brighton.yaml",
    "role_policies/policy_03_acme.hr.uk.yaml",
    "role_policies/policy_04_acme.hr.uk.yaml",
    "role_policies/policy_05_acme.hr.uk.london.yaml",
    "role_policies/policy_06_acme.hr.uk.brighton.kemptown.yaml",
    "tests/policy_04_test.yaml",
    "tests/policy_05_test.yaml",
];

struct TestSetup {
    store_client: StoreClient<AuthMiddleware>,
    store_id: String,
}

impl TestSetup {
    async fn new() -> Result<Self> {
        let api_endpoint = env::var("CERBOS_HUB_API_ENDPOINT")
            .unwrap_or_else(|_| "https://api.cerbos.cloud".to_string());

        let store_id = env::var("CERBOS_HUB_STORE_ID")
            .expect("CERBOS_HUB_STORE_ID environment variable must be set for integration tests");

        let hub_client = HubClientBuilder::new()
            .with_api_endpoint(api_endpoint)
            .build()
            .await?;

        let store_client = hub_client.store_client();

        Ok(TestSetup {
            store_client,
            store_id,
        })
    }

    async fn reset_store(&mut self) -> Result<()> {
        let test_data_path = get_test_data_path(&["replace_files", "success"]);

        let zip_data = zip_directory(&test_data_path).context("failed to zip data")?;

        let request =
            ReplaceFilesRequestBuilder::new(&self.store_id, "Reset store for test", zip_data)
                .build();

        self.store_client.replace_files_lenient(request).await?;

        self.check_store_has_expected_files().await
    }

    async fn check_store_has_expected_files(&mut self) -> Result<()> {
        let list_request = ListFilesRequestBuilder::new(&self.store_id).build();
        let list_response = self.store_client.list_files(list_request).await?;

        let mut want_files: Vec<String> = WANT_FILES_LIST.iter().map(|s| s.to_string()).collect();
        let mut have_files = list_response.files.clone();
        want_files.sort();
        have_files.sort();

        assert_eq!(want_files, have_files, "Store does not have expected files");
        Ok(())
    }
}

fn get_test_data_path(subpath: &[&str]) -> PathBuf {
    let mut path = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
    path.push("tests");
    path.push("testdata");
    subpath.iter().for_each(|p| path.push(p));
    path
}
#[tokio::test]
async fn test_auth_error() -> Result<(), Box<dyn std::error::Error>> {
    let api_endpoint = env::var("CERBOS_HUB_API_ENDPOINT")
        .unwrap_or_else(|_| "https://api.cerbos.cloud".to_string());

    let store_id = env::var("CERBOS_HUB_STORE_ID")
        .expect("CERBOS_HUB_STORE_ID environment variable must be set for integration tests");

    let hub_client = HubClientBuilder::new()
        .with_api_endpoint(api_endpoint)
        .with_client_credentials("not a client", "not a secret")
        .build()
        .await?;

    let mut store_client = hub_client.store_client();
    let files = vec!["wibble.yaml".to_string()];
    let request = GetFilesRequest { store_id, files };
    let result = store_client.get_files(request.clone()).await;
    if let Err(RPCError::Unknown {
        message: _,
        underlying: ref e,
    }) = result
    {
        if e.code() == tonic::Code::ResourceExhausted {
            eprintln!("\x1b[91mSkipping test due to too many requests response\x1b[0m");
            return Ok(());
        }
    }
    assert!(
        matches!(
            &result,
            Err(RPCError::AuthenticationFailed {
                message: _,
                underlying: _
            })
        ),
        "{result:?}"
    );
    let result = store_client.get_files(request).await;
    assert!(matches!(
        result,
        Err(RPCError::AuthenticationFailed {
            message: msg,
            underlying: _
        }) if msg.contains("short-circuiting")
    ));
    Ok(())
}
#[tokio::test]
async fn test_replace_files() -> Result<(), Box<dyn std::error::Error>> {
    let mut setup = TestSetup::new().await?;
    setup.reset_store().await?;

    // Test invalid request
    test_replace_files_invalid_request(&mut setup).await?;

    // Test invalid files
    test_replace_files_invalid_files(&mut setup).await?;

    // Test unusable files
    test_replace_files_unusable_files(&mut setup).await?;

    // Test unsuccessful condition
    test_replace_files_unsuccessful_condition(&mut setup).await?;

    Ok(())
}

async fn test_replace_files_invalid_request(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let invalid_zip = b"invalid zip data exceeding 22 bytes in length";
    let request =
        ReplaceFilesRequestBuilder::new(&setup.store_id, "Invalid request", invalid_zip.to_vec())
            .build();

    let result = setup.store_client.replace_files(request).await;
    assert!(matches!(
        result,
        Err(RPCError::InvalidRequest {
            message: _,
            underlying: _
        })
    ));

    Ok(())
}

async fn test_replace_files_invalid_files(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let test_data_path = get_test_data_path(&["replace_files", "invalid"]);

    let zip_data = zip_directory(&test_data_path)?;
    let request =
        ReplaceFilesRequestBuilder::new(&setup.store_id, "Invalid files test", zip_data).build();

    let result = setup.store_client.replace_files(request).await;
    assert!(
        matches!(result, Err(RPCError::ValidationFailure {message: _, underlying: _, validation_errors}) if validation_errors.len() == 1)
    );

    setup.check_store_has_expected_files().await?;
    Ok(())
}

async fn test_replace_files_unusable_files(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let test_data_path = get_test_data_path(&["replace_files", "unusable"]);

    let zip_data = zip_directory(&test_data_path)?;
    let request =
        ReplaceFilesRequestBuilder::new(&setup.store_id, "Unusable files test", zip_data).build();

    let result = setup.store_client.replace_files(request).await;
    assert!(
        matches!(result, Err(RPCError::NoUsableFiles {message: _, underlying: _, ignored_files: fs}) if fs.len() == 2 && fs.iter().all(|f| f == ".hidden.yaml" || f == "README.md"))
    );

    Ok(())
}

async fn test_replace_files_unsuccessful_condition(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let test_data_path = get_test_data_path(&["replace_files", "conditional"]);

    let zip_data = zip_directory(&test_data_path)?;
    let request = ReplaceFilesRequestBuilder::new(&setup.store_id, "Conditional test", zip_data)
        .only_if_version_equals(i64::MAX) // This should fail
        .build();

    let result = setup.store_client.replace_files(request).await;
    assert!(matches!(
        result,
        Err(RPCError::ConditionUnsatisfied {
            message: _,
            underlying: _
        })
    ));

    setup.check_store_has_expected_files().await?;
    Ok(())
}

#[tokio::test]
async fn test_modify_files() -> Result<(), Box<dyn std::error::Error>> {
    let mut setup = TestSetup::new().await?;
    setup.reset_store().await?;

    // Test successful modification
    test_modify_files_success(&mut setup).await?;

    // Test invalid request
    test_modify_files_invalid_request(&mut setup).await?;

    // Test invalid files
    test_modify_files_invalid_files(&mut setup).await?;

    // Test unsuccessful condition
    test_modify_files_unsuccessful_condition(&mut setup).await?;

    Ok(())
}

async fn test_modify_files_success(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let example_content = std::fs::read_to_string(get_test_data_path(&[
        "modify_files",
        "success",
        "example.yaml",
    ]))?;
    let request = ModifyFilesRequestBuilder::new(&setup.store_id, "Test modification")
        .add_or_update_file("example.yaml", example_content.as_bytes().to_vec())
        .build();

    let response = setup.store_client.modify_files(request).await?;
    assert!(response.new_store_version > 0);

    // Verify the file was added
    let get_request = GetFilesRequestBuilder::new(&setup.store_id, vec!["example.yaml"]).build();
    let get_response = setup.store_client.get_files(get_request).await?;

    assert_eq!(get_response.files.len(), 1);

    let retrieved_content = &get_response
        .files
        .iter()
        .find(|&x| x.path == "example.yaml")
        .unwrap()
        .contents;

    assert_eq!(
        std::str::from_utf8(retrieved_content)?.trim(),
        example_content.trim()
    );

    Ok(())
}

async fn test_modify_files_invalid_request(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    // Empty operations should be invalid
    let request = ModifyFilesRequestBuilder::new(&setup.store_id, "Empty modification").build();

    let result = setup.store_client.modify_files(request).await;
    assert!(matches!(
        result,
        Err(RPCError::InvalidRequest {
            message: _,
            underlying: _
        })
    ));

    Ok(())
}

async fn test_modify_files_invalid_files(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let invalid_content = "invalid yaml content: [[[";

    let request = ModifyFilesRequestBuilder::new(&setup.store_id, "Invalid file modification")
        .add_or_update_file("invalid.yaml", invalid_content.as_bytes().to_vec())
        .build();

    let result = setup.store_client.modify_files(request).await;
    assert!(
        matches!(result, Err(RPCError::ValidationFailure { message: _, underlying: _, validation_errors }) if validation_errors.len() == 1 )
    );

    Ok(())
}

async fn test_modify_files_unsuccessful_condition(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let content = std::fs::read_to_string(get_test_data_path(&[
        "modify_files",
        "conditional",
        "new_policy.yaml",
    ]))?;

    let request = ModifyFilesRequestBuilder::new(&setup.store_id, "Conditional modification")
        .add_or_update_file("new_policy.yaml", content.as_bytes().to_vec())
        .only_if_version_equals(i64::MAX) // This should fail
        .build();

    let result = setup.store_client.modify_files(request).await;

    assert!(
        matches!(
            result,
            Err(RPCError::ConditionUnsatisfied {
                message: _,
                underlying: _
            },)
        ),
        "Expected ConditionUnsatisfied, got: {result:?}"
    );
    Ok(())
}

#[tokio::test]
async fn test_list_files() -> Result<(), Box<dyn std::error::Error>> {
    let mut setup = TestSetup::new().await?;
    setup.reset_store().await?;

    // Test filter with matches
    test_list_files_with_filter_match(&mut setup).await?;

    // Test filter with no matches
    test_list_files_with_no_filter_match(&mut setup).await?;

    Ok(())
}

async fn test_list_files_with_filter_match(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let request = ListFilesRequestBuilder::new(&setup.store_id)
        .with_file_filter(FileFilterBuilder::path_contains("export_"))
        .build();

    let response = setup.store_client.list_files(request).await?;

    let mut want_files = vec![
        "export_constants/export_constants_01.yaml".to_string(),
        "export_variables/export_variables_01.yaml".to_string(),
    ];

    let mut have_files = response.files.clone();
    have_files.sort();
    want_files.sort();

    assert_eq!(have_files, want_files);

    Ok(())
}

async fn test_list_files_with_no_filter_match(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let request = ListFilesRequestBuilder::new(&setup.store_id)
        .with_file_filter(FileFilterBuilder::path_contains("wibble"))
        .build();

    let response = setup.store_client.list_files(request).await?;
    assert_eq!(response.files.len(), 0);

    Ok(())
}

#[tokio::test]
async fn test_get_files() -> Result<(), Box<dyn std::error::Error>> {
    let mut setup = TestSetup::new().await?;
    setup.reset_store().await?;

    // Test non-existent file
    test_get_files_non_existent(&mut setup).await?;

    // Test invalid request
    test_get_files_invalid_request(&mut setup).await?;

    Ok(())
}

async fn test_get_files_non_existent(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let request = GetFilesRequestBuilder::new(&setup.store_id, vec!["wibble.yaml"]).build();
    let response = setup.store_client.get_files(request).await?;

    assert_eq!(response.files.len(), 0);

    Ok(())
}

async fn test_get_files_invalid_request(
    setup: &mut TestSetup,
) -> Result<(), Box<dyn std::error::Error>> {
    let request = GetFilesRequestBuilder::new(&setup.store_id, Vec::<String>::new()).build();
    let result = setup.store_client.get_files(request).await;

    assert!(matches!(
        result,
        Err(RPCError::InvalidRequest {
            message: _,
            underlying: _
        })
    ));

    Ok(())
}