optify 0.10.3__tar.gz → 0.11.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. {optify-0.10.3 → optify-0.11.0}/PKG-INFO +6 -1
  2. {optify-0.10.3 → optify-0.11.0}/README.md +5 -0
  3. {optify-0.10.3 → optify-0.11.0}/pyproject.toml +2 -2
  4. {optify-0.10.3 → optify-0.11.0}/python/optify/Cargo.lock +2 -2
  5. {optify-0.10.3 → optify-0.11.0}/python/optify/Cargo.toml +2 -2
  6. {optify-0.10.3 → optify-0.11.0}/python/optify/README.md +5 -0
  7. {optify-0.10.3 → optify-0.11.0}/rust/optify/Cargo.toml +1 -1
  8. {optify-0.10.3 → optify-0.11.0}/rust/optify/README.md +6 -0
  9. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/builder_impl.rs +11 -1
  10. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/loading_result.rs +2 -1
  11. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/get_options_preferences.rs +17 -0
  12. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/provider_impl.rs +85 -1
  13. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/provider_trait.rs +20 -1
  14. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/watcher.rs +21 -1
  15. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/schema/feature.rs +5 -2
  16. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/schema/mod.rs +1 -0
  17. optify-0.11.0/rust/optify/src/schema/policies.rs +88 -0
  18. {optify-0.10.3 → optify-0.11.0}/LICENSE.txt +0 -0
  19. {optify-0.10.3 → optify-0.11.0}/optify/__init__.py +0 -0
  20. {optify-0.10.3 → optify-0.11.0}/optify/optify.pyi +0 -0
  21. {optify-0.10.3 → optify-0.11.0}/optify/py.typed +0 -0
  22. {optify-0.10.3 → optify-0.11.0}/python/optify/.gitignore +0 -0
  23. {optify-0.10.3 → optify-0.11.0}/python/optify/.vscode/settings.json +0 -0
  24. {optify-0.10.3 → optify-0.11.0}/python/optify/LICENSE.txt +0 -0
  25. {optify-0.10.3 → optify-0.11.0}/python/optify/src/lib.rs +0 -0
  26. {optify-0.10.3 → optify-0.11.0}/python/optify/src/preferences.rs +0 -0
  27. {optify-0.10.3 → optify-0.11.0}/python/optify/src/provider.rs +0 -0
  28. {optify-0.10.3 → optify-0.11.0}/python/optify/src/watcher.rs +0 -0
  29. {optify-0.10.3 → optify-0.11.0}/python/optify/tests/test_builder.py +0 -0
  30. {optify-0.10.3 → optify-0.11.0}/python/optify/tests/test_dacite.py +0 -0
  31. {optify-0.10.3 → optify-0.11.0}/python/optify/tests/test_optify.py +0 -0
  32. {optify-0.10.3 → optify-0.11.0}/python/optify/tests/test_provider.py +0 -0
  33. {optify-0.10.3 → optify-0.11.0}/python/optify/tests/test_pydantic.py +0 -0
  34. {optify-0.10.3 → optify-0.11.0}/rust/Cargo.toml +0 -0
  35. {optify-0.10.3 → optify-0.11.0}/rust/optify/benches/caching_benchmark.rs +0 -0
  36. {optify-0.10.3 → optify-0.11.0}/rust/optify/benches/get_all_options_benchmark.rs +0 -0
  37. {optify-0.10.3 → optify-0.11.0}/rust/optify/benches/get_options_benchmark.rs +0 -0
  38. {optify-0.10.3 → optify-0.11.0}/rust/optify/benches/loading_benchmark.rs +0 -0
  39. {optify-0.10.3 → optify-0.11.0}/rust/optify/build.rs +0 -0
  40. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/builder_options.rs +0 -0
  41. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/builder_trait.rs +0 -0
  42. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/extract_configurable_string_files_from_config.rs +0 -0
  43. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/extract_files_from_config.rs +0 -0
  44. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/get_canonical_feature_name.rs +0 -0
  45. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/get_supported_extensions.rs +0 -0
  46. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/mod.rs +0 -0
  47. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/builder/watcher_builder.rs +0 -0
  48. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/configurable_string/configurable_string_impl.rs +0 -0
  49. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/configurable_string/mod.rs +0 -0
  50. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/configurable_values/configurable_list_impl.rs +0 -0
  51. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/configurable_values/locator.rs +0 -0
  52. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/configurable_values/mod.rs +0 -0
  53. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/json/escape_json_pointer.rs +0 -0
  54. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/json/merge.rs +0 -0
  55. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/json/mod.rs +0 -0
  56. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/json/reader.rs +0 -0
  57. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/lib.rs +0 -0
  58. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/constraints.rs +0 -0
  59. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/mod.rs +0 -0
  60. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/provider/watcher_options.rs +0 -0
  61. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/schema/conditions.rs +0 -0
  62. {optify-0.10.3 → optify-0.11.0}/rust/optify/src/schema/metadata.rs +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: optify
3
- Version: 0.10.3
3
+ Version: 0.11.0
4
4
  Classifier: Programming Language :: Rust
5
5
  Classifier: Programming Language :: Python :: Implementation :: CPython
6
6
  Classifier: Programming Language :: Python :: Implementation :: PyPy
@@ -30,6 +30,7 @@ This is just meant to be minimal to get started and help build a Python library.
30
30
  See the [homepage] for details about how feature files are combined to build the options to process at runtime.
31
31
 
32
32
  ## Usage
33
+
33
34
  ```Python
34
35
  import json
35
36
  from optify import OptionsProvider
@@ -40,6 +41,7 @@ print(config)
40
41
  ```
41
42
 
42
43
  Outputs:
44
+
43
45
  ```JSON
44
46
  {
45
47
  "myArray": [
@@ -174,12 +176,15 @@ pytest
174
176
  ```
175
177
 
176
178
  ### Formatting
179
+
177
180
  To automatically change the Rust code, run:
181
+
178
182
  ```shell
179
183
  cargo fmt && cargo clippy --fix --allow-dirty --allow-staged
180
184
  ```
181
185
 
182
186
  # Publishing
187
+
183
188
  A GitHub Action will automatically publish new versions: https://github.com/juharris/optify/actions/workflows/python_publish.yml
184
189
 
185
190
  [homepage]: https://github.com/juharris/optify
@@ -10,6 +10,7 @@ This is just meant to be minimal to get started and help build a Python library.
10
10
  See the [homepage] for details about how feature files are combined to build the options to process at runtime.
11
11
 
12
12
  ## Usage
13
+
13
14
  ```Python
14
15
  import json
15
16
  from optify import OptionsProvider
@@ -20,6 +21,7 @@ print(config)
20
21
  ```
21
22
 
22
23
  Outputs:
24
+
23
25
  ```JSON
24
26
  {
25
27
  "myArray": [
@@ -154,12 +156,15 @@ pytest
154
156
  ```
155
157
 
156
158
  ### Formatting
159
+
157
160
  To automatically change the Rust code, run:
161
+
158
162
  ```shell
159
163
  cargo fmt && cargo clippy --fix --allow-dirty --allow-staged
160
164
  ```
161
165
 
162
166
  # Publishing
167
+
163
168
  A GitHub Action will automatically publish new versions: https://github.com/juharris/optify/actions/workflows/python_publish.yml
164
169
 
165
170
  [homepage]: https://github.com/juharris/optify
@@ -26,11 +26,11 @@ build-backend = "maturin"
26
26
 
27
27
  [package]
28
28
  name = "optify"
29
- version = "0.10.3"
29
+ version = "0.11.0"
30
30
 
31
31
  [tool.poetry]
32
32
  name = "optify"
33
- version = "0.10.3"
33
+ version = "0.11.0"
34
34
  description = "Simplifies getting the right configuration options for a process using pre-loaded configurations from files to manage options for experiments or flights."
35
35
  authors = ["Justin D. Harris"]
36
36
  maintainers = ["Justin D. Harris"]
@@ -941,7 +941,7 @@ checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
941
941
 
942
942
  [[package]]
943
943
  name = "optify"
944
- version = "1.3.3"
944
+ version = "1.4.0"
945
945
  dependencies = [
946
946
  "config",
947
947
  "cow-utils",
@@ -961,7 +961,7 @@ dependencies = [
961
961
 
962
962
  [[package]]
963
963
  name = "optify-python"
964
- version = "0.10.3"
964
+ version = "0.11.0"
965
965
  dependencies = [
966
966
  "optify",
967
967
  "pyo3",
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "optify-python"
3
- version = "0.10.3"
3
+ version = "0.11.0"
4
4
  edition = "2021"
5
5
 
6
6
  description = "Simplifies getting the right configuration options for a process using pre-loaded configurations from files (JSON, YAML, etc.) to manage options for experiments or flights. This library is mainly made to support building implementations for other languages such as Node.js, Python, and Ruby. It is not meant to be consumed directly yet."
@@ -15,5 +15,5 @@ name = "optify"
15
15
  crate-type = ["cdylib"]
16
16
 
17
17
  [dependencies]
18
- optify = { path = "../../rust/optify", version = "1.3.3" }
18
+ optify = { path = "../../rust/optify", version = "1.4.0" }
19
19
  pyo3 = "0.29.0"
@@ -10,6 +10,7 @@ This is just meant to be minimal to get started and help build a Python library.
10
10
  See the [homepage] for details about how feature files are combined to build the options to process at runtime.
11
11
 
12
12
  ## Usage
13
+
13
14
  ```Python
14
15
  import json
15
16
  from optify import OptionsProvider
@@ -20,6 +21,7 @@ print(config)
20
21
  ```
21
22
 
22
23
  Outputs:
24
+
23
25
  ```JSON
24
26
  {
25
27
  "myArray": [
@@ -154,12 +156,15 @@ pytest
154
156
  ```
155
157
 
156
158
  ### Formatting
159
+
157
160
  To automatically change the Rust code, run:
161
+
158
162
  ```shell
159
163
  cargo fmt && cargo clippy --fix --allow-dirty --allow-staged
160
164
  ```
161
165
 
162
166
  # Publishing
167
+
163
168
  A GitHub Action will automatically publish new versions: https://github.com/juharris/optify/actions/workflows/python_publish.yml
164
169
 
165
170
  [homepage]: https://github.com/juharris/optify
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "optify"
3
- version = "1.3.3"
3
+ version = "1.4.0"
4
4
  edition = "2021"
5
5
  build = "build.rs"
6
6
 
@@ -72,18 +72,23 @@ Optionally, when working locally, there is support to watch for changes to the c
72
72
  ## Testing
73
73
 
74
74
  Run:
75
+
75
76
  ```shell
76
77
  cargo test
77
78
  ```
78
79
 
79
80
  ## Formatting
81
+
80
82
  To automatically change code, run:
83
+
81
84
  ```shell
82
85
  cargo fmt && cargo clippy --fix --allow-dirty --allow-staged
83
86
  ```
84
87
 
85
88
  ## Benchmarking
89
+
86
90
  Run:
91
+
87
92
  ```shell
88
93
  cargo build --release
89
94
  cargo bench
@@ -97,6 +102,7 @@ open target/criterion/*/report/index.html
97
102
  ```
98
103
 
99
104
  ## Publishing
105
+
100
106
  ```shell
101
107
  cargo login
102
108
  cargo publish
@@ -17,7 +17,8 @@ use crate::configurable_values::locator::{find_configurable_values, Configurable
17
17
  use crate::json::merge::{merge_json_with_defaults, FrozenPaths};
18
18
  use crate::json::reader::read_json_from_file_as;
19
19
  use crate::provider::{
20
- Aliases, Conditions, Features, OptionsProvider, ReferencedFileToFeatureNames, Sources,
20
+ Aliases, Conditions, Features, OptionsProvider, PoliciesMap, ReferencedFileToFeatureNames,
21
+ Sources,
21
22
  };
22
23
  use crate::schema::feature::FeatureConfiguration;
23
24
  use crate::schema::metadata::OptionsMetadata;
@@ -39,6 +40,7 @@ pub struct OptionsProviderBuilder {
39
40
  features: Features,
40
41
  imports: Imports,
41
42
  loaded_files: LoadedFiles,
43
+ policies: PoliciesMap,
42
44
  /// A map of files to the features that reference them.
43
45
  /// The keys are relative file paths and the values are lists of canonical feature names.
44
46
  /// This is only populated if the `BuilderOptions` enable file reference tracking.
@@ -197,6 +199,7 @@ impl OptionsProviderBuilder {
197
199
  features: Features::new(),
198
200
  imports: HashMap::new(),
199
201
  loaded_files: LoadedFiles::new(),
202
+ policies: PoliciesMap::new(),
200
203
  referenced_file_to_feature_names: HashMap::new(),
201
204
  schema: None,
202
205
  sources: Sources::new(),
@@ -237,6 +240,7 @@ impl OptionsProviderBuilder {
237
240
  keyed_configurable_string_pointers,
238
241
  std::mem::take(&mut self.conditions),
239
242
  std::mem::take(&mut self.features),
243
+ std::mem::take(&mut self.policies),
240
244
  referenced_file_to_feature_names,
241
245
  std::mem::take(&mut self.loaded_files),
242
246
  std::mem::take(&mut self.sources),
@@ -339,6 +343,10 @@ impl OptionsProviderBuilder {
339
343
  self.conditions
340
344
  .insert(canonical_feature_name.clone(), conditions);
341
345
  }
346
+ if let Some(policies) = info.policies {
347
+ self.policies
348
+ .insert(canonical_feature_name.clone(), policies);
349
+ }
342
350
  if let Some(imports) = info.imports {
343
351
  self.imports.insert(canonical_feature_name.clone(), imports);
344
352
  }
@@ -525,6 +533,7 @@ fn process_config_file_entry(
525
533
  configurable_value_pointers,
526
534
  imports: feature_config.imports,
527
535
  metadata,
536
+ policies: feature_config.policies,
528
537
  source,
529
538
  }))
530
539
  }
@@ -685,6 +694,7 @@ impl OptionsRegistryBuilder<OptionsProvider> for OptionsProviderBuilder {
685
694
  keyed_configurable_string_pointers,
686
695
  self.conditions.clone(),
687
696
  self.features.clone(),
697
+ self.policies.clone(),
688
698
  referenced_file_to_feature_names,
689
699
  self.loaded_files.clone(),
690
700
  self.sources.clone(),
@@ -1,7 +1,7 @@
1
1
  use crate::{
2
2
  configurable_values::locator::ConfigurableValuePointers,
3
3
  provider::SourceValue,
4
- schema::{conditions::ConditionExpression, metadata::OptionsMetadata},
4
+ schema::{conditions::ConditionExpression, metadata::OptionsMetadata, policies::Policies},
5
5
  };
6
6
 
7
7
  #[allow(clippy::large_enum_variant)]
@@ -20,6 +20,7 @@ pub(crate) struct FeatureLoadingResult {
20
20
  pub configurable_value_pointers: ConfigurableValuePointers,
21
21
  pub imports: Option<Vec<String>>,
22
22
  pub metadata: OptionsMetadata,
23
+ pub policies: Option<Policies>,
23
24
  pub source: SourceValue,
24
25
  }
25
26
 
@@ -12,6 +12,21 @@ pub struct GetOptionsPreferences {
12
12
  pub constraints: Option<Constraints>,
13
13
  /// Overrides to apply after the built configuration.
14
14
  pub overrides: Option<SourceValue>,
15
+ /// The identifier of the requester making this request.
16
+ ///
17
+ /// Used to enforce `policies.requester` on features.
18
+ ///
19
+ /// **Important**: If `None`, requester-based policies are **not** checked, meaning callers
20
+ /// that do not set this field bypass requester policy enforcement entirely.
21
+ /// Only trusted internal callers that are not subject to requester policies should omit this field.
22
+ /// For all external or user-facing requests, always set this to the requester's identifier.
23
+ pub requester: Option<String>,
24
+ /// Whether to raise an error if a feature's policies deny the requester.
25
+ ///
26
+ /// When `true` and `requester` is set, any feature denied by its policies will cause an error.
27
+ /// When `false` (the default), denied features are silently filtered out, matching the behaviour
28
+ /// of condition-based filtering.
29
+ pub raise_if_policy_denied: bool,
15
30
  /// Determines if the feature names should be converted to canonical feature names.
16
31
  /// Defaults to false: given features names will be converted to canonical feature names before looking for features or options.
17
32
  pub skip_feature_name_conversion: bool,
@@ -30,6 +45,8 @@ impl GetOptionsPreferences {
30
45
  are_configurable_values_enabled: false,
31
46
  constraints: None,
32
47
  overrides: None,
48
+ raise_if_policy_denied: false,
49
+ requester: None,
33
50
  skip_feature_name_conversion: false,
34
51
  }
35
52
  }
@@ -8,7 +8,11 @@ use crate::{
8
8
  configurable_string::LoadedFiles,
9
9
  json::merge::{merge_json_with_defaults, FrozenPaths},
10
10
  provider::GetOptionsPreferences,
11
- schema::{conditions::ConditionExpression, metadata::OptionsMetadata},
11
+ schema::{
12
+ conditions::ConditionExpression,
13
+ metadata::OptionsMetadata,
14
+ policies::{Policies, PolicyDeniedError},
15
+ },
12
16
  };
13
17
 
14
18
  use super::OptionsRegistry;
@@ -22,6 +26,7 @@ pub(crate) type SourceValue = serde_json::Value;
22
26
  pub(crate) type Aliases = HashMap<unicase::UniCase<String>, String>;
23
27
  pub(crate) type Conditions = HashMap<String, ConditionExpression>;
24
28
  pub(crate) type Features = HashMap<String, OptionsMetadata>;
29
+ pub(crate) type PoliciesMap = HashMap<String, Policies>;
25
30
  pub(crate) type ReferencedFileToFeatureNames = HashMap<String, Vec<String>>;
26
31
  pub(crate) type Sources = HashMap<String, SourceValue>;
27
32
 
@@ -40,6 +45,7 @@ pub struct OptionsProvider {
40
45
  aliases: Aliases,
41
46
  conditions: Conditions,
42
47
  features: Features,
48
+ policies: PoliciesMap,
43
49
  /// A map of files to their referencing features.
44
50
  /// The keys are relative file paths and the values are lists of canonical feature names.
45
51
  /// This allows fast lookup of features when a specific file is modified.
@@ -63,6 +69,7 @@ impl OptionsProvider {
63
69
  keyed_configurable_string_pointers: HashMap<String, Vec<String>>,
64
70
  conditions: Conditions,
65
71
  features: Features,
72
+ policies: PoliciesMap,
66
73
  referenced_file_to_feature_names: Option<ReferencedFileToFeatureNames>,
67
74
  loaded_files: LoadedFiles,
68
75
  sources: Sources,
@@ -75,6 +82,7 @@ impl OptionsProvider {
75
82
  aliases,
76
83
  conditions,
77
84
  features,
85
+ policies,
78
86
  referenced_file_to_feature_names,
79
87
  loaded_files,
80
88
  sources,
@@ -425,6 +433,32 @@ impl OptionsProvider {
425
433
  }
426
434
  Ok(())
427
435
  }
436
+
437
+ /// Checks whether the requester is permitted for the given feature.
438
+ ///
439
+ /// Returns `Ok(true)` if permitted, no policy is set, or no requester is given.
440
+ /// Returns `Ok(false)` if denied and `raise_if_policy_denied` is false.
441
+ /// Returns `Err(message)` if denied and `raise_if_policy_denied` is true.
442
+ fn is_feature_permitted_for_requester(
443
+ &self,
444
+ canonical_feature_name: &str,
445
+ requester: Option<&str>,
446
+ raise_if_policy_denied: bool,
447
+ ) -> Result<bool, String> {
448
+ if let Some(requester) = requester {
449
+ if let Some(policies) = self.policies.get(canonical_feature_name) {
450
+ if !policies.is_requester_permitted(requester) {
451
+ if raise_if_policy_denied {
452
+ return Err(
453
+ PolicyDeniedError::new(canonical_feature_name, requester).to_string()
454
+ );
455
+ }
456
+ return Ok(false);
457
+ }
458
+ }
459
+ }
460
+ Ok(true)
461
+ }
428
462
  }
429
463
 
430
464
  impl OptionsRegistry for OptionsProvider {
@@ -539,9 +573,13 @@ impl OptionsRegistry for OptionsProvider {
539
573
  ) -> Result<Vec<String>, String> {
540
574
  let mut skip_feature_name_conversion = false;
541
575
  let mut constraints = None;
576
+ let mut requester: Option<&str> = None;
577
+ let mut raise_if_policy_denied = false;
542
578
  if let Some(preferences) = preferences {
543
579
  skip_feature_name_conversion = preferences.skip_feature_name_conversion;
544
580
  constraints = preferences.constraints.as_ref();
581
+ requester = preferences.requester.as_deref();
582
+ raise_if_policy_denied = preferences.raise_if_policy_denied;
545
583
  }
546
584
 
547
585
  let mut result = Vec::new();
@@ -562,6 +600,15 @@ impl OptionsRegistry for OptionsProvider {
562
600
  continue;
563
601
  }
564
602
  }
603
+
604
+ if !self.is_feature_permitted_for_requester(
605
+ &canonical_feature_name,
606
+ requester,
607
+ raise_if_policy_denied,
608
+ )? {
609
+ continue;
610
+ }
611
+
565
612
  result.push(canonical_feature_name);
566
613
  }
567
614
 
@@ -626,6 +673,29 @@ impl OptionsRegistry for OptionsProvider {
626
673
  self.conditions.contains_key(canonical_feature_name)
627
674
  }
628
675
 
676
+ fn check_policies(
677
+ &self,
678
+ requester: &str,
679
+ feature_names: &[impl AsRef<str>],
680
+ _cache_options: Option<&CacheOptions>,
681
+ ) -> Result<(), String> {
682
+ for feature_name in feature_names {
683
+ let canonical_feature_name = self.get_canonical_feature_name(feature_name.as_ref())?;
684
+ if let Some(policies) = self.policies.get(&canonical_feature_name) {
685
+ if !policies.is_requester_permitted(requester) {
686
+ return Err(
687
+ PolicyDeniedError::new(&canonical_feature_name, requester).to_string()
688
+ );
689
+ }
690
+ }
691
+ }
692
+ Ok(())
693
+ }
694
+
695
+ fn get_policies(&self, canonical_feature_name: &str) -> Option<Policies> {
696
+ self.policies.get(canonical_feature_name).cloned()
697
+ }
698
+
629
699
  fn map_feature_names(
630
700
  &self,
631
701
  feature_names: &[impl AsRef<str>],
@@ -633,9 +703,13 @@ impl OptionsRegistry for OptionsProvider {
633
703
  ) -> Result<Vec<Option<String>>, String> {
634
704
  let mut skip_feature_name_conversion = false;
635
705
  let mut constraints = None;
706
+ let mut requester: Option<&str> = None;
707
+ let mut raise_if_policy_denied = false;
636
708
  if let Some(preferences) = preferences {
637
709
  skip_feature_name_conversion = preferences.skip_feature_name_conversion;
638
710
  constraints = preferences.constraints.as_ref();
711
+ requester = preferences.requester.as_deref();
712
+ raise_if_policy_denied = preferences.raise_if_policy_denied;
639
713
  }
640
714
 
641
715
  let mut result = Vec::new();
@@ -657,6 +731,16 @@ impl OptionsRegistry for OptionsProvider {
657
731
  continue;
658
732
  }
659
733
  }
734
+
735
+ if !self.is_feature_permitted_for_requester(
736
+ &canonical_feature_name,
737
+ requester,
738
+ raise_if_policy_denied,
739
+ )? {
740
+ result.push(None);
741
+ continue;
742
+ }
743
+
660
744
  result.push(Some(canonical_feature_name));
661
745
  }
662
746
 
@@ -6,7 +6,7 @@ use crate::builder::builder_options::BuilderOptions;
6
6
 
7
7
  use crate::{
8
8
  provider::{CacheOptions, Features, GetOptionsPreferences},
9
- schema::metadata::OptionsMetadata,
9
+ schema::{metadata::OptionsMetadata, policies::Policies},
10
10
  };
11
11
 
12
12
  /// Trait defining the core functionality for an options provider
@@ -125,6 +125,25 @@ pub trait OptionsRegistry {
125
125
  /// Indicates if the feature has conditions.
126
126
  fn has_conditions(&self, canonical_feature_name: &str) -> bool;
127
127
 
128
+ /// Checks policies for a list of features for the given requester.
129
+ /// `CacheOptions` is not used yet.
130
+ ///
131
+ /// Returns `Ok(())` if there are no policy issues (or no policies defined).
132
+ /// Returns `Err(String)` with an error message for the first feature that is not allowed.
133
+ fn check_policies(
134
+ &self,
135
+ requester: &str,
136
+ feature_names: &[impl AsRef<str>],
137
+ cache_options: Option<&CacheOptions>,
138
+ ) -> Result<(), String>;
139
+
140
+ /// Returns the policies for the given canonical feature name, if any.
141
+ ///
142
+ /// Policies are only checked for top-level features in a request.
143
+ /// Features that are imported by other features may have policies, but those policies
144
+ /// are not checked when the feature is used as an import.
145
+ fn get_policies(&self, canonical_feature_name: &str) -> Option<Policies>;
146
+
128
147
  /// Filters `feature_names` based on the preferences,
129
148
  /// such as the `preferences.constraints`.
130
149
  /// Returns a Vec of optional strings matching the input order.
@@ -8,7 +8,7 @@ use crate::builder::{OptionsRegistryBuilder, OptionsWatcherBuilder};
8
8
  use crate::provider::{
9
9
  CacheOptions, Features, GetOptionsPreferences, OptionsProvider, OptionsRegistry, WatcherOptions,
10
10
  };
11
- use crate::schema::metadata::OptionsMetadata;
11
+ use crate::schema::{metadata::OptionsMetadata, policies::Policies};
12
12
 
13
13
  pub type OptionsWatcherListener = Arc<dyn Fn(&HashSet<PathBuf>) + Send + Sync>;
14
14
 
@@ -329,6 +329,19 @@ impl OptionsRegistry for OptionsWatcher {
329
329
  .get_options_with_preferences(key, feature_names, cache_options, preferences)
330
330
  }
331
331
 
332
+ fn check_policies(
333
+ &self,
334
+ requester: &str,
335
+ feature_names: &[impl AsRef<str>],
336
+ cache_options: Option<&CacheOptions>,
337
+ ) -> Result<(), String> {
338
+ self.current_provider.read().unwrap().check_policies(
339
+ requester,
340
+ feature_names,
341
+ cache_options,
342
+ )
343
+ }
344
+
332
345
  fn has_conditions(&self, canonical_feature_name: &str) -> bool {
333
346
  self.current_provider
334
347
  .read()
@@ -336,6 +349,13 @@ impl OptionsRegistry for OptionsWatcher {
336
349
  .has_conditions(canonical_feature_name)
337
350
  }
338
351
 
352
+ fn get_policies(&self, canonical_feature_name: &str) -> Option<Policies> {
353
+ self.current_provider
354
+ .read()
355
+ .unwrap()
356
+ .get_policies(canonical_feature_name)
357
+ }
358
+
339
359
  fn map_feature_names(
340
360
  &self,
341
361
  feature_names: &[impl AsRef<str>],
@@ -1,9 +1,8 @@
1
- // Similar to https://github.com/juharris/dotnet-OptionsProvider/blob/main/src/OptionsProvider/OptionsProvider/FeatureConfiguration.cs
2
-
3
1
  use serde::Deserialize;
4
2
 
5
3
  use super::conditions::ConditionExpression;
6
4
  use super::metadata::OptionsMetadata;
5
+ use super::policies::Policies;
7
6
 
8
7
  pub(crate) type ConfigurationOptions = serde_json::Value;
9
8
 
@@ -16,5 +15,9 @@ pub(crate) struct FeatureConfiguration {
16
15
  ///
17
16
  /// More details in the JSON schema.
18
17
  pub conditions: Option<ConditionExpression>,
18
+ /// Policies that restrict which requesters may use this feature.
19
+ ///
20
+ /// More details in the JSON schema.
21
+ pub policies: Option<Policies>,
19
22
  pub options: Option<ConfigurationOptions>,
20
23
  }
@@ -1,3 +1,4 @@
1
1
  pub mod conditions;
2
2
  pub(crate) mod feature;
3
3
  pub mod metadata;
4
+ pub mod policies;
@@ -0,0 +1,88 @@
1
+ use std::collections::HashSet;
2
+ use std::fmt;
3
+
4
+ use serde::{Deserialize, Serialize};
5
+
6
+ /// Error returned when a requester is not permitted to use a feature.
7
+ #[derive(Clone, Debug)]
8
+ pub struct PolicyDeniedError {
9
+ pub feature_name: String,
10
+ pub requester: String,
11
+ }
12
+
13
+ impl PolicyDeniedError {
14
+ pub fn new(feature_name: impl Into<String>, requester: impl Into<String>) -> Self {
15
+ Self {
16
+ feature_name: feature_name.into(),
17
+ requester: requester.into(),
18
+ }
19
+ }
20
+ }
21
+
22
+ impl fmt::Display for PolicyDeniedError {
23
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24
+ write!(
25
+ f,
26
+ "Requester \"{}\" is not permitted to use feature \"{}\".",
27
+ self.requester, self.feature_name
28
+ )
29
+ }
30
+ }
31
+
32
+ impl std::error::Error for PolicyDeniedError {}
33
+
34
+ /// The policy for the requester identifier passed via preferences.
35
+ ///
36
+ /// Either `allow` or `block` must be specified, not both.
37
+ ///
38
+ /// - `allow`: Only the listed requesters may use this feature.
39
+ /// An empty set means no requester is currently allowed.
40
+ /// - `block`: The listed requesters may not use this feature.
41
+ /// All other requesters are allowed.
42
+ ///
43
+ /// See https://github.com/juharris/optify#policies for more information.
44
+ #[derive(Clone, Debug, Deserialize, Serialize)]
45
+ #[serde(untagged)]
46
+ pub enum RequesterPolicy {
47
+ Allow { allow: HashSet<String> },
48
+ Block { block: HashSet<String> },
49
+ }
50
+
51
+ impl RequesterPolicy {
52
+ /// Returns `true` if the given requester is permitted by this policy.
53
+ pub fn is_permitted(&self, value: &str) -> bool {
54
+ match self {
55
+ Self::Allow { allow } => allow.contains(value),
56
+ Self::Block { block } => !block.contains(value),
57
+ }
58
+ }
59
+ }
60
+
61
+ /// Policies that restrict access to a feature based on values in the request's preferences.
62
+ ///
63
+ /// Policies are checked for the **top-level features** in a request.
64
+ /// Unlike conditions, policies are **not** evaluated on imported features — a feature may
65
+ /// freely import another feature that has policies without those policies being enforced.
66
+ ///
67
+ /// See https://github.com/juharris/optify#policies for details and a comparison with conditions.
68
+ #[derive(Clone, Debug, Deserialize, Serialize)]
69
+ #[serde(deny_unknown_fields)]
70
+ pub struct Policies {
71
+ /// Restrictions based on the requester identifier passed via preferences.
72
+ ///
73
+ /// Use `allow` to specify an allowlist (only those requesters may use this feature).
74
+ /// Use `block` to specify a denylist (all requesters except those listed may use this feature).
75
+ /// `allow` and `block` are mutually exclusive.
76
+ pub requester: Option<RequesterPolicy>,
77
+ }
78
+
79
+ impl Policies {
80
+ /// Returns `true` if the given requester is permitted to use the feature.
81
+ ///
82
+ /// If no `requester` policy is set, all requesters are permitted.
83
+ pub fn is_requester_permitted(&self, requester: &str) -> bool {
84
+ self.requester
85
+ .as_ref()
86
+ .is_none_or(|p| p.is_permitted(requester))
87
+ }
88
+ }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes